diff options
author | Pedro Alves <palves@redhat.com> | 2015-03-07 15:14:14 +0000 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2015-03-07 15:14:14 +0000 |
commit | 492d29ea1c9a8b2c7d5193908119a4e27c045687 (patch) | |
tree | ea592a514ec482e6c57020670e9bc447f827e298 /gdb/python/py-value.c | |
parent | ece957c859c00fbea7152a2275674d7061dc468a (diff) | |
download | binutils-492d29ea1c9a8b2c7d5193908119a4e27c045687.zip binutils-492d29ea1c9a8b2c7d5193908119a4e27c045687.tar.gz binutils-492d29ea1c9a8b2c7d5193908119a4e27c045687.tar.bz2 |
Split TRY_CATCH into TRY + CATCH
This patch splits the TRY_CATCH macro into three, so that we go from
this:
~~~
volatile gdb_exception ex;
TRY_CATCH (ex, RETURN_MASK_ERROR)
{
}
if (ex.reason < 0)
{
}
~~~
to this:
~~~
TRY
{
}
CATCH (ex, RETURN_MASK_ERROR)
{
}
END_CATCH
~~~
Thus, we'll be getting rid of the local volatile exception object, and
declaring the caught exception in the catch block.
This allows reimplementing TRY/CATCH in terms of C++ exceptions when
building in C++ mode, while still allowing to build GDB in C mode
(using setjmp/longjmp), as a transition step.
TBC, after this patch, is it _not_ valid to have code between the TRY
and the CATCH blocks, like:
TRY
{
}
// some code here.
CATCH (ex, RETURN_MASK_ERROR)
{
}
END_CATCH
Just like it isn't valid to do that with C++'s native try/catch.
By switching to creating the exception object inside the CATCH block
scope, we can get rid of all the explicitly allocated volatile
exception objects all over the tree, and map the CATCH block more
directly to C++'s catch blocks.
The majority of the TRY_CATCH -> TRY+CATCH+END_CATCH conversion was
done with a script, rerun from scratch at every rebase, no manual
editing involved. After the mechanical conversion, a few places
needed manual intervention, to fix preexisting cases where we were
using the exception object outside of the TRY_CATCH block, and cases
where we were using "else" after a 'if (ex.reason) < 0)' [a CATCH
after this patch]. The result was folded into this patch so that GDB
still builds at each incremental step.
END_CATCH is necessary for two reasons:
First, because we name the exception object in the CATCH block, which
requires creating a scope, which in turn must be closed somewhere.
Declaring the exception variable in the initializer field of a for
block, like:
#define CATCH(EXCEPTION, mask) \
for (struct gdb_exception EXCEPTION; \
exceptions_state_mc_catch (&EXCEPTION, MASK); \
EXCEPTION = exception_none)
would avoid needing END_CATCH, but alas, in C mode, we build with C90,
which doesn't allow mixed declarations and code.
Second, because when TRY/CATCH are wired to real C++ try/catch, as
long as we need to handle cleanup chains, even if there's no CATCH
block that wants to catch the exception, we need for stop at every
frame in the unwind chain and run cleanups, then rethrow. That will
be done in END_CATCH.
After we require C++, we'll still need TRY/CATCH/END_CATCH until
cleanups are completely phased out -- TRY/CATCH in C++ mode will
save/restore the current cleanup chain, like in C mode, and END_CATCH
catches otherwise uncaugh exceptions, runs cleanups and rethrows, so
that C++ cleanups and exceptions can coexist.
IMO, this still makes the TRY/CATCH code look a bit more like a
newcomer would expect, so IMO worth it even if we weren't considering
C++.
gdb/ChangeLog.
2015-03-07 Pedro Alves <palves@redhat.com>
* common/common-exceptions.c (struct catcher) <exception>: No
longer a pointer to volatile exception. Now an exception value.
<mask>: Delete field.
(exceptions_state_mc_init): Remove all parameters. Adjust.
(exceptions_state_mc): No longer pop the catcher here.
(exceptions_state_mc_catch): New function.
(throw_exception): Adjust.
* common/common-exceptions.h (exceptions_state_mc_init): Remove
all parameters.
(exceptions_state_mc_catch): Declare.
(TRY_CATCH): Rename to ...
(TRY): ... this. Remove EXCEPTION and MASK parameters.
(CATCH, END_CATCH): New.
All callers adjusted.
gdb/gdbserver/ChangeLog:
2015-03-07 Pedro Alves <palves@redhat.com>
Adjust all callers of TRY_CATCH to use TRY/CATCH/END_CATCH
instead.
Diffstat (limited to 'gdb/python/py-value.c')
-rw-r--r-- | gdb/python/py-value.c | 228 |
1 files changed, 153 insertions, 75 deletions
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 5a13777..6622d11 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -172,10 +172,9 @@ gdbpy_preserve_values (const struct extension_language_defn *extlang, static PyObject * valpy_dereference (PyObject *self, PyObject *args) { - volatile struct gdb_exception except; PyObject *result = NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *res_val; struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -184,7 +183,11 @@ valpy_dereference (PyObject *self, PyObject *args) result = value_to_value_object (res_val); do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -200,10 +203,9 @@ valpy_dereference (PyObject *self, PyObject *args) static PyObject * valpy_referenced_value (PyObject *self, PyObject *args) { - volatile struct gdb_exception except; PyObject *result = NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *self_val, *res_val; struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -225,7 +227,11 @@ valpy_referenced_value (PyObject *self, PyObject *args) result = value_to_value_object (res_val); do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -235,11 +241,10 @@ static PyObject * valpy_get_address (PyObject *self, void *closure) { value_object *val_obj = (value_object *) self; - volatile struct gdb_exception except; if (!val_obj->address) { - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *res_val; struct cleanup *cleanup @@ -249,11 +254,12 @@ valpy_get_address (PyObject *self, void *closure) val_obj->address = value_to_value_object (res_val); do_cleanups (cleanup); } - if (except.reason < 0) + CATCH (except, RETURN_MASK_ALL) { val_obj->address = Py_None; Py_INCREF (Py_None); } + END_CATCH } Py_XINCREF (val_obj->address); @@ -283,7 +289,6 @@ static PyObject * valpy_get_dynamic_type (PyObject *self, void *closure) { value_object *obj = (value_object *) self; - volatile struct gdb_exception except; struct type *type = NULL; if (obj->dynamic_type != NULL) @@ -292,7 +297,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure) return obj->dynamic_type; } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *val = obj->value; struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -331,7 +336,11 @@ valpy_get_dynamic_type (PyObject *self, void *closure) do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH if (type == NULL) obj->dynamic_type = valpy_get_type (self, NULL); @@ -358,13 +367,12 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) const char *user_encoding = NULL; static char *keywords[] = { "encoding", "length", NULL }; PyObject *str_obj = NULL; - volatile struct gdb_exception except; if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, &user_encoding, &length)) return NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -377,7 +385,11 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return str_obj; } @@ -394,7 +406,6 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw) int length = -1; gdb_byte *buffer; struct value *value = ((value_object *) self)->value; - volatile struct gdb_exception except; PyObject *unicode; const char *encoding = NULL; const char *errors = NULL; @@ -407,11 +418,15 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw) &user_encoding, &errors, &length)) return NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; unicode = PyUnicode_Decode ((const char *) buffer, @@ -429,7 +444,6 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) { PyObject *type_obj, *result = NULL; struct type *type; - volatile struct gdb_exception except; if (! PyArg_ParseTuple (args, "O", &type_obj)) return NULL; @@ -442,7 +456,7 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) return NULL; } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *val = ((value_object *) self)->value; struct value *res_val; @@ -461,7 +475,11 @@ valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) result = value_to_value_object (res_val); do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -508,7 +526,6 @@ value_has_field (struct value *v, PyObject *field) struct type *parent_type, *val_type; enum type_code type_code; PyObject *type_object = PyObject_GetAttrString (field, "parent_type"); - volatile struct gdb_exception except; int has_field = 0; if (type_object == NULL) @@ -524,7 +541,7 @@ value_has_field (struct value *v, PyObject *field) return -1; } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { val_type = value_type (v); val_type = check_typedef (val_type); @@ -539,7 +556,11 @@ value_has_field (struct value *v, PyObject *field) else has_field = 0; } - GDB_PY_SET_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_SET_HANDLE_EXCEPTION (except); + } + END_CATCH return has_field; } @@ -591,11 +612,11 @@ get_field_type (PyObject *field) static PyObject * valpy_getitem (PyObject *self, PyObject *key) { + struct gdb_exception except = exception_none; value_object *self_value = (value_object *) self; char *field = NULL; struct type *base_class_type = NULL, *field_type = NULL; long bitpos = -1; - volatile struct gdb_exception except; PyObject *result = NULL; if (gdbpy_is_string (key)) @@ -673,7 +694,7 @@ valpy_getitem (PyObject *self, PyObject *key) } } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *tmp = self_value->value; struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -723,6 +744,11 @@ valpy_getitem (PyObject *self, PyObject *key) result = value_to_value_object (res_val); do_cleanups (cleanup); } + CATCH (ex, RETURN_MASK_ALL) + { + except = ex; + } + END_CATCH xfree (field); GDB_PY_HANDLE_EXCEPTION (except); @@ -744,18 +770,21 @@ static PyObject * valpy_call (PyObject *self, PyObject *args, PyObject *keywords) { Py_ssize_t args_count; - volatile struct gdb_exception except; struct value *function = ((value_object *) self)->value; struct value **vargs = NULL; struct type *ftype = NULL; struct value *mark = value_mark (); PyObject *result = NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { ftype = check_typedef (value_type (function)); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) { @@ -790,7 +819,7 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords) } } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark); struct value *return_value; @@ -799,7 +828,11 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords) result = value_to_value_object (return_value); do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -812,12 +845,11 @@ valpy_str (PyObject *self) char *s = NULL; PyObject *result; struct value_print_options opts; - volatile struct gdb_exception except; get_user_print_options (&opts); opts.deref_ref = 0; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct ui_file *stb = mem_fileopen (); struct cleanup *old_chain = make_cleanup_ui_file_delete (stb); @@ -828,7 +860,11 @@ valpy_str (PyObject *self) do_cleanups (old_chain); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL); xfree (s); @@ -842,13 +878,16 @@ valpy_get_is_optimized_out (PyObject *self, void *closure) { struct value *value = ((value_object *) self)->value; int opt = 0; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { opt = value_optimized_out (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH if (opt) Py_RETURN_TRUE; @@ -862,13 +901,16 @@ valpy_get_is_lazy (PyObject *self, void *closure) { struct value *value = ((value_object *) self)->value; int opt = 0; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { opt = value_lazy (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH if (opt) Py_RETURN_TRUE; @@ -881,14 +923,17 @@ static PyObject * valpy_fetch_lazy (PyObject *self, PyObject *args) { struct value *value = ((value_object *) self)->value; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { if (value_lazy (value)) value_fetch_lazy (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH Py_RETURN_NONE; } @@ -926,10 +971,9 @@ enum valpy_opcode static PyObject * valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) { - volatile struct gdb_exception except; PyObject *result = NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *arg1, *arg2; struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -1049,7 +1093,11 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -1103,10 +1151,9 @@ valpy_power (PyObject *self, PyObject *other, PyObject *unused) static PyObject * valpy_negative (PyObject *self) { - volatile struct gdb_exception except; PyObject *result = NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { /* Perhaps overkill, but consistency has some virtue. */ struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -1116,7 +1163,11 @@ valpy_negative (PyObject *self) result = value_to_value_object (val); do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return result; } @@ -1131,10 +1182,9 @@ static PyObject * valpy_absolute (PyObject *self) { struct value *value = ((value_object *) self)->value; - volatile struct gdb_exception except; int isabs = 1; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ()); @@ -1143,7 +1193,11 @@ valpy_absolute (PyObject *self) do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH if (isabs) return valpy_positive (self); @@ -1155,12 +1209,12 @@ valpy_absolute (PyObject *self) static int valpy_nonzero (PyObject *self) { - volatile struct gdb_exception except; + struct gdb_exception except = exception_none; value_object *self_value = (value_object *) self; struct type *type; int nonzero = 0; /* Appease GCC warning. */ - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { type = check_typedef (value_type (self_value->value)); @@ -1176,6 +1230,12 @@ valpy_nonzero (PyObject *self) /* All other values are True. */ nonzero = 1; } + CATCH (ex, RETURN_MASK_ALL) + { + except = ex; + } + END_CATCH + /* This is not documented in the Python documentation, but if this function fails, return -1 as slot_nb_nonzero does (the default Python nonzero function). */ @@ -1189,13 +1249,16 @@ static PyObject * valpy_invert (PyObject *self) { struct value *val = NULL; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { val = value_complement (((value_object *) self)->value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return value_to_value_object (val); } @@ -1241,7 +1304,6 @@ static PyObject * valpy_richcompare (PyObject *self, PyObject *other, int op) { int result = 0; - volatile struct gdb_exception except; if (other == Py_None) /* Comparing with None is special. From what I can tell, in Python @@ -1262,7 +1324,7 @@ valpy_richcompare (PyObject *self, PyObject *other, int op) return NULL; } - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { struct value *value_other, *mark = value_mark (); struct cleanup *cleanup; @@ -1307,7 +1369,11 @@ valpy_richcompare (PyObject *self, PyObject *other, int op) do_cleanups (cleanup); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH /* In this case, the Python exception has already been set. */ if (result < 0) @@ -1327,16 +1393,19 @@ valpy_int (PyObject *self) struct value *value = ((value_object *) self)->value; struct type *type = value_type (value); LONGEST l = 0; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { if (!is_integral_type (type)) error (_("Cannot convert value to int.")); l = value_as_long (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return gdb_py_object_from_longest (l); } @@ -1349,9 +1418,8 @@ valpy_long (PyObject *self) struct value *value = ((value_object *) self)->value; struct type *type = value_type (value); LONGEST l = 0; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { CHECK_TYPEDEF (type); @@ -1361,7 +1429,11 @@ valpy_long (PyObject *self) l = value_as_long (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return gdb_py_long_from_longest (l); } @@ -1373,9 +1445,8 @@ valpy_float (PyObject *self) struct value *value = ((value_object *) self)->value; struct type *type = value_type (value); double d = 0; - volatile struct gdb_exception except; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { CHECK_TYPEDEF (type); @@ -1384,7 +1455,11 @@ valpy_float (PyObject *self) d = value_as_double (value); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return PyFloat_FromDouble (d); } @@ -1431,12 +1506,11 @@ struct value * convert_value_from_python (PyObject *obj) { struct value *value = NULL; /* -Wall */ - volatile struct gdb_exception except; int cmp; gdb_assert (obj != NULL); - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { if (PyBool_Check (obj)) { @@ -1532,13 +1606,14 @@ convert_value_from_python (PyObject *obj) PyString_AsString (PyObject_Str (obj))); #endif } - if (except.reason < 0) + CATCH (except, RETURN_MASK_ALL) { PyErr_Format (except.reason == RETURN_QUIT ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, "%s", except.message); return NULL; } + END_CATCH return value; } @@ -1549,16 +1624,19 @@ gdbpy_history (PyObject *self, PyObject *args) { int i; struct value *res_val = NULL; /* Initialize to appease gcc warning. */ - volatile struct gdb_exception except; if (!PyArg_ParseTuple (args, "i", &i)) return NULL; - TRY_CATCH (except, RETURN_MASK_ALL) + TRY { res_val = access_value_history (i); } - GDB_PY_HANDLE_EXCEPTION (except); + CATCH (except, RETURN_MASK_ALL) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + END_CATCH return value_to_value_object (res_val); } |