mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-06-03 13:23:00 +08:00
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.
This commit is contained in:
@ -341,15 +341,18 @@ typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
|
||||
{
|
||||
PyObject *py_type = self;
|
||||
PyObject *result = NULL, *iter = NULL;
|
||||
volatile struct gdb_exception except;
|
||||
struct type *type = ((type_object *) py_type)->type;
|
||||
struct type *checked_type = type;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
CHECK_TYPEDEF (checked_type);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
if (checked_type != type)
|
||||
py_type = type_to_type_object (checked_type);
|
||||
@ -449,13 +452,16 @@ static PyObject *
|
||||
typy_strip_typedefs (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = check_typedef (type);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -466,15 +472,18 @@ typy_strip_typedefs (PyObject *self, PyObject *args)
|
||||
static struct type *
|
||||
typy_get_composite (struct type *type)
|
||||
{
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
CHECK_TYPEDEF (type);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
if (TYPE_CODE (type) != TYPE_CODE_PTR
|
||||
&& TYPE_CODE (type) != TYPE_CODE_REF)
|
||||
@ -505,7 +514,6 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
|
||||
PyObject *n2_obj = NULL;
|
||||
struct type *array = NULL;
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj))
|
||||
return NULL;
|
||||
@ -535,13 +543,17 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
array = lookup_array_range_type (type, n1, n2);
|
||||
if (is_vector)
|
||||
make_vector_type (array);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (array);
|
||||
}
|
||||
@ -567,13 +579,16 @@ static PyObject *
|
||||
typy_pointer (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = lookup_pointer_type (type);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -648,13 +663,16 @@ static PyObject *
|
||||
typy_reference (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = lookup_reference_type (type);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -680,13 +698,16 @@ static PyObject *
|
||||
typy_const (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = make_cv_type (1, 0, type, NULL);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -696,13 +717,16 @@ static PyObject *
|
||||
typy_volatile (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = make_cv_type (0, 1, type, NULL);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -712,13 +736,16 @@ static PyObject *
|
||||
typy_unqualified (PyObject *self, PyObject *args)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = make_cv_type (0, 0, type, NULL);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type_to_type_object (type);
|
||||
}
|
||||
@ -728,12 +755,16 @@ static PyObject *
|
||||
typy_get_sizeof (PyObject *self, void *closure)
|
||||
{
|
||||
struct type *type = ((type_object *) self)->type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
check_typedef (type);
|
||||
}
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
/* Ignore exceptions. */
|
||||
|
||||
return gdb_py_long_from_longest (TYPE_LENGTH (type));
|
||||
@ -743,9 +774,8 @@ static struct type *
|
||||
typy_lookup_typename (const char *type_name, const struct block *block)
|
||||
{
|
||||
struct type *type = NULL;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
if (startswith (type_name, "struct "))
|
||||
type = lookup_struct (type_name + 7, NULL);
|
||||
@ -757,7 +787,11 @@ typy_lookup_typename (const char *type_name, const struct block *block)
|
||||
type = lookup_typename (python_language, python_gdbarch,
|
||||
type_name, block, 0);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return type;
|
||||
}
|
||||
@ -769,7 +803,6 @@ typy_lookup_type (struct demangle_component *demangled,
|
||||
struct type *type, *rtype = NULL;
|
||||
char *type_name = NULL;
|
||||
enum demangle_component_type demangled_type;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
/* Save the type: typy_lookup_type() may (indirectly) overwrite
|
||||
memory pointed by demangled. */
|
||||
@ -784,7 +817,7 @@ typy_lookup_type (struct demangle_component *demangled,
|
||||
if (! type)
|
||||
return NULL;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
/* If the demangled_type matches with one of the types
|
||||
below, run the corresponding function and save the type
|
||||
@ -806,7 +839,11 @@ typy_lookup_type (struct demangle_component *demangled,
|
||||
break;
|
||||
}
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
}
|
||||
|
||||
/* If we have a type from the switch statement above, just return
|
||||
@ -837,7 +874,6 @@ typy_legacy_template_argument (struct type *type, const struct block *block,
|
||||
const char *err;
|
||||
struct type *argtype;
|
||||
struct cleanup *cleanup;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
if (TYPE_NAME (type) == NULL)
|
||||
{
|
||||
@ -845,12 +881,16 @@ typy_legacy_template_argument (struct type *type, const struct block *block,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
/* Note -- this is not thread-safe. */
|
||||
info = cp_demangled_name_to_comp (TYPE_NAME (type), &err);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
if (! info)
|
||||
{
|
||||
@ -903,7 +943,6 @@ typy_template_argument (PyObject *self, PyObject *args)
|
||||
PyObject *block_obj = NULL;
|
||||
struct symbol *sym;
|
||||
struct value *val = NULL;
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj))
|
||||
return NULL;
|
||||
@ -919,13 +958,17 @@ typy_template_argument (PyObject *self, PyObject *args)
|
||||
}
|
||||
}
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
type = check_typedef (type);
|
||||
if (TYPE_CODE (type) == TYPE_CODE_REF)
|
||||
type = check_typedef (TYPE_TARGET_TYPE (type));
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
/* We might not have DW_TAG_template_*, so try to parse the type's
|
||||
name. This is inefficient if we do not have a template type --
|
||||
@ -950,11 +993,15 @@ typy_template_argument (PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
val = value_of_variable (sym, block);
|
||||
}
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
return value_to_value_object (val);
|
||||
}
|
||||
@ -962,12 +1009,11 @@ typy_template_argument (PyObject *self, PyObject *args)
|
||||
static PyObject *
|
||||
typy_str (PyObject *self)
|
||||
{
|
||||
volatile struct gdb_exception except;
|
||||
char *thetype = NULL;
|
||||
long length = 0;
|
||||
PyObject *result;
|
||||
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
struct cleanup *old_chain;
|
||||
struct ui_file *stb;
|
||||
@ -981,11 +1027,12 @@ typy_str (PyObject *self)
|
||||
thetype = ui_file_xstrdup (stb, &length);
|
||||
do_cleanups (old_chain);
|
||||
}
|
||||
if (except.reason < 0)
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
xfree (thetype);
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
|
||||
result = PyUnicode_Decode (thetype, length, host_charset (), NULL);
|
||||
xfree (thetype);
|
||||
@ -1001,7 +1048,6 @@ typy_richcompare (PyObject *self, PyObject *other, int op)
|
||||
int result = Py_NE;
|
||||
struct type *type1 = type_object_to_type (self);
|
||||
struct type *type2 = type_object_to_type (other);
|
||||
volatile struct gdb_exception except;
|
||||
|
||||
/* We can only compare ourselves to another Type object, and only
|
||||
for equality or inequality. */
|
||||
@ -1015,13 +1061,17 @@ typy_richcompare (PyObject *self, PyObject *other, int op)
|
||||
result = Py_EQ;
|
||||
else
|
||||
{
|
||||
TRY_CATCH (except, RETURN_MASK_ALL)
|
||||
TRY
|
||||
{
|
||||
result = types_deeply_equal (type1, type2);
|
||||
}
|
||||
/* If there is a GDB exception, a comparison is not capable
|
||||
(or trusted), so exit. */
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
/* If there is a GDB exception, a comparison is not capable
|
||||
(or trusted), so exit. */
|
||||
GDB_PY_HANDLE_EXCEPTION (except);
|
||||
}
|
||||
END_CATCH
|
||||
}
|
||||
|
||||
if (op == (result ? Py_EQ : Py_NE))
|
||||
|
Reference in New Issue
Block a user