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:
Pedro Alves 2015-03-07 15:14:14 +00:00
parent ece957c859
commit 492d29ea1c
112 changed files with 2194 additions and 1299 deletions

View file

@ -201,9 +201,8 @@ mi_should_print (struct symbol *sym, enum mi_print_types type)
static enum ext_lang_bt_status
py_print_type (struct ui_out *out, struct value *val)
{
volatile struct gdb_exception except;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
struct type *type;
struct ui_file *stb;
@ -216,11 +215,12 @@ py_print_type (struct ui_out *out, struct value *val)
ui_out_field_stream (out, "type", stb);
do_cleanups (cleanup);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
return EXT_LANG_BT_ERROR;
}
END_CATCH
return EXT_LANG_BT_OK;
}
@ -242,7 +242,6 @@ py_print_value (struct ui_out *out, struct value *val,
const struct language_defn *language)
{
int should_print = 0;
volatile struct gdb_exception except;
int local_indent = (4 * indent);
/* Never set an indent level for common_val_print if MI. */
@ -257,15 +256,16 @@ py_print_value (struct ui_out *out, struct value *val,
{
struct type *type = NULL;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
type = check_typedef (value_type (val));
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
return EXT_LANG_BT_ERROR;
}
END_CATCH
if (args_type == MI_PRINT_ALL_VALUES)
should_print = 1;
@ -280,7 +280,7 @@ py_print_value (struct ui_out *out, struct value *val,
if (should_print)
{
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
struct ui_file *stb;
struct cleanup *cleanup;
@ -291,11 +291,12 @@ py_print_value (struct ui_out *out, struct value *val,
ui_out_field_stream (out, "value", stb);
do_cleanups (cleanup);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
return EXT_LANG_BT_OK;
@ -363,7 +364,6 @@ py_print_single_arg (struct ui_out *out,
const struct language_defn *language)
{
struct value *val;
volatile struct gdb_exception except;
enum ext_lang_bt_status retval = EXT_LANG_BT_OK;
if (fa != NULL)
@ -376,7 +376,7 @@ py_print_single_arg (struct ui_out *out,
else
val = fv;
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
@ -473,8 +473,11 @@ py_print_single_arg (struct ui_out *out,
do_cleanups (cleanups);
}
if (except.reason < 0)
gdbpy_convert_exception (except);
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
}
END_CATCH
return retval;
}
@ -499,7 +502,6 @@ enumerate_args (PyObject *iter,
{
PyObject *item;
struct value_print_options opts;
volatile struct gdb_exception except;
get_user_print_options (&opts);
@ -511,15 +513,16 @@ enumerate_args (PyObject *iter,
opts.deref_ref = 1;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
annotate_frame_args ();
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
/* Collect the first argument outside of the loop, so output of
commas in the argument output is correct. At the end of the
@ -578,16 +581,17 @@ enumerate_args (PyObject *iter,
goto error;
}
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
read_frame_arg (sym, frame, &arg, &entryarg);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
xfree (sym_name);
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
/* The object has not provided a value, so this is a frame
argument to be read by GDB. In this case we have to
@ -612,12 +616,12 @@ enumerate_args (PyObject *iter,
{
if (arg.entry_kind != print_entry_values_only)
{
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
ui_out_text (out, ", ");
ui_out_wrap_hint (out, " ");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
xfree (arg.error);
xfree (entryarg.error);
@ -625,6 +629,7 @@ enumerate_args (PyObject *iter,
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
}
if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
@ -664,30 +669,32 @@ enumerate_args (PyObject *iter,
item = PyIter_Next (iter);
if (item != NULL)
{
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
ui_out_text (out, ", ");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
Py_DECREF (item);
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
}
else if (PyErr_Occurred ())
goto error;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
annotate_arg_end ();
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
Py_DECREF (item);
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
}
return EXT_LANG_BT_OK;
@ -729,7 +736,6 @@ enumerate_locals (PyObject *iter,
struct value *val;
enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
struct symbol *sym;
volatile struct gdb_exception except;
int local_indent = 8 + (8 * indent);
struct cleanup *locals_cleanups;
@ -761,16 +767,17 @@ enumerate_locals (PyObject *iter,
/* If the object did not provide a value, read it. */
if (val == NULL)
{
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
val = read_var_value (sym, frame);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (locals_cleanups);
goto error;
}
END_CATCH
}
/* With PRINT_NO_VALUES, MI does not emit a tuple normally as
@ -781,7 +788,7 @@ enumerate_locals (PyObject *iter,
if (print_args_field || args_type != NO_VALUES)
make_cleanup_ui_out_tuple_begin_end (out, NULL);
}
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
if (! ui_out_is_mi_like_p (out))
{
@ -794,12 +801,13 @@ enumerate_locals (PyObject *iter,
if (! ui_out_is_mi_like_p (out))
ui_out_text (out, " = ");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (locals_cleanups);
goto error;
}
END_CATCH
if (args_type == MI_PRINT_SIMPLE_VALUES)
{
@ -838,15 +846,16 @@ enumerate_locals (PyObject *iter,
do_cleanups (locals_cleanups);
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
ui_out_text (out, "\n");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
goto error;
}
END_CATCH
}
if (item == NULL && PyErr_Occurred ())
@ -947,40 +956,41 @@ py_print_args (PyObject *filter,
{
PyObject *args_iter = get_py_iter_from_func (filter, "frame_args");
struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter);
volatile struct gdb_exception except;
if (args_iter == NULL)
goto args_error;
make_cleanup_ui_out_list_begin_end (out, "args");
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
annotate_frame_args ();
if (! ui_out_is_mi_like_p (out))
ui_out_text (out, " (");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
goto args_error;
}
END_CATCH
if (args_iter != Py_None)
if (enumerate_args (args_iter, out, args_type, 0, frame)
== EXT_LANG_BT_ERROR)
goto args_error;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
if (! ui_out_is_mi_like_p (out))
ui_out_text (out, ")");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
gdbpy_convert_exception (except);
goto args_error;
}
END_CATCH
do_cleanups (old_chain);
return EXT_LANG_BT_OK;
@ -1018,7 +1028,6 @@ py_print_frame (PyObject *filter, int flags,
struct value_print_options opts;
PyObject *py_inf_frame;
int print_level, print_frame_info, print_args, print_locals;
volatile struct gdb_exception except;
/* Extract print settings from FLAGS. */
print_level = (flags & PRINT_LEVEL) ? 1 : 0;
@ -1042,15 +1051,16 @@ py_print_frame (PyObject *filter, int flags,
if (frame == NULL)
return EXT_LANG_BT_ERROR;
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
gdbarch = get_frame_arch (frame);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
return EXT_LANG_BT_ERROR;
}
END_CATCH
/* stack-list-variables. */
if (print_locals && print_args && ! print_frame_info)
@ -1074,16 +1084,17 @@ py_print_frame (PyObject *filter, int flags,
and are printed with indention. */
if (indent > 0)
{
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
ui_out_spaces (out, indent*4);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
/* The address is required for frame annotations, and also for
@ -1113,11 +1124,10 @@ py_print_frame (PyObject *filter, int flags,
{
struct frame_info **slot;
int level;
volatile struct gdb_exception except;
slot = (struct frame_info **) htab_find_slot (levels_printed,
frame, INSERT);
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
level = frame_relative_level (frame);
@ -1137,12 +1147,13 @@ py_print_frame (PyObject *filter, int flags,
level);
}
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
if (print_frame_info)
@ -1151,19 +1162,20 @@ py_print_frame (PyObject *filter, int flags,
print nothing. */
if (opts.addressprint && has_addr)
{
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
annotate_frame_address ();
ui_out_field_core_addr (out, "addr", gdbarch, address);
annotate_frame_address_end ();
ui_out_text (out, " in ");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
/* Print frame function name. */
@ -1218,7 +1230,7 @@ py_print_frame (PyObject *filter, int flags,
return EXT_LANG_BT_ERROR;
}
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
annotate_frame_function_name ();
if (function == NULL)
@ -1226,12 +1238,14 @@ py_print_frame (PyObject *filter, int flags,
else
ui_out_field_string (out, "func", function);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
do_cleanups (py_func_cleanup);
}
}
@ -1251,16 +1265,17 @@ py_print_frame (PyObject *filter, int flags,
/* File name/source/line number information. */
if (print_frame_info)
{
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
annotate_frame_source_begin ();
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
if (PyObject_HasAttrString (filter, "filename"))
{
@ -1285,7 +1300,7 @@ py_print_frame (PyObject *filter, int flags,
}
make_cleanup (xfree, filename);
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
ui_out_wrap_hint (out, " ");
ui_out_text (out, " at ");
@ -1293,12 +1308,13 @@ py_print_frame (PyObject *filter, int flags,
ui_out_field_string (out, "file", filename);
annotate_frame_source_file_end ();
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
do_cleanups (py_fn_cleanup);
}
@ -1319,18 +1335,19 @@ py_print_frame (PyObject *filter, int flags,
if (py_line != Py_None)
{
line = PyLong_AsLong (py_line);
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
ui_out_text (out, ":");
annotate_frame_source_line ();
ui_out_field_int (out, "line", line);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
do_cleanups (py_line_cleanup);
}
@ -1340,17 +1357,18 @@ py_print_frame (PyObject *filter, int flags,
elided frames, so if MI output detected do not send newline. */
if (! ui_out_is_mi_like_p (out))
{
TRY_CATCH (except, RETURN_MASK_ERROR)
TRY
{
annotate_frame_end ();
ui_out_text (out, "\n");
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ERROR)
{
gdbpy_convert_exception (except);
do_cleanups (cleanup_stack);
return EXT_LANG_BT_ERROR;
}
END_CATCH
}
if (print_locals)
@ -1503,22 +1521,22 @@ gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
struct cleanup *cleanups;
enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
PyObject *iterable;
volatile struct gdb_exception except;
PyObject *item;
htab_t levels_printed;
if (!gdb_python_initialized)
return EXT_LANG_BT_NO_FILTERS;
TRY_CATCH (except, RETURN_MASK_ALL)
TRY
{
gdbarch = get_frame_arch (frame);
}
if (except.reason < 0)
CATCH (except, RETURN_MASK_ALL)
{
/* Let gdb try to print the stack trace. */
return EXT_LANG_BT_NO_FILTERS;
}
END_CATCH
cleanups = ensure_python_env (gdbarch, current_language);