Re: [patch] [python] Use TRY_CATCH in some functions.

Phil Muldoon
2011-10-24T13:30:33Z

Tom Tromey <tromey@redhat.com> writes:

> Tom> Why not GDB_PY_HANDLE_EXCEPTION here?
> Tom> (And also fix up typy_lookup_typename to do the same.)
>
> Phil> Because typy_lookup_typename and typy_lookup_type are helper
> Phil> functions that return struct type, while GDB_PY_HANDLE_EXCEPTION
> Phil> returns a PyObject (from gdbpy_convert_exception). Should we have
> Phil> a macro that builds an exception without returning one? We should,
> Phil> I think. But I was not going to do that in this patch context.
>
> Ok, I see. Thanks.
> I think both those functions should use gdbpy_convert_exception, though,
> instead of replicating the exception-conversion logic themselves.

Updated patch applied. ChangeLog has not changed.

OK?

Cheers,

Phil

--

Index: gdb/python/py-breakpoint.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-breakpoint.c,v
retrieving revision 1.27
diff -u -r1.27 py-breakpoint.c
--- gdb/python/py-breakpoint.c 20 Oct 2011 13:34:17 -0000 1.27
+++ gdb/python/py-breakpoint.c 24 Oct 2011 13:27:35 -0000
@@ -150,6 +150,7 @@
{
breakpoint_object *self_bp = (breakpoint_object *) self;
int cmp;
+ volatile struct gdb_exception except;

BPPY_SET_REQUIRE_VALID (self_bp);

@@ -170,10 +171,16 @@
cmp = PyObject_IsTrue (newvalue);
if (cmp < 0)
return -1;
- else if (cmp == 1)
- enable_breakpoint (self_bp->bp);
- else
- disable_breakpoint (self_bp->bp);
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (cmp == 1)
+ enable_breakpoint (self_bp->bp);
+ else
+ disable_breakpoint (self_bp->bp);
+ }
+ GDB_PY_SET_HANDLE_EXCEPTION (except);
+
return 0;
}

@@ -255,6 +262,8 @@
{
breakpoint_object *self_bp = (breakpoint_object *) self;
long id;
+ int valid_id = 0;
+ volatile struct gdb_exception except;

BPPY_SET_REQUIRE_VALID (self_bp);

@@ -269,7 +278,13 @@
if (! gdb_py_int_as_long (newvalue, &id))
return -1;

- if (! valid_task_id (id))
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ valid_id = valid_task_id (id);
+ }
+ GDB_PY_SET_HANDLE_EXCEPTION (except);
+
+ if (! valid_id)
{
PyErr_SetString (PyExc_RuntimeError,
_("Invalid task ID."));
@@ -299,10 +314,15 @@
bppy_delete_breakpoint (PyObject *self, PyObject *args)
{
breakpoint_object *self_bp = (breakpoint_object *) self;
+ volatile struct gdb_exception except;

BPPY_REQUIRE_VALID (self_bp);

- delete_breakpoint (self_bp->bp);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ delete_breakpoint (self_bp->bp);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);

Py_RETURN_NONE;
}
Index: gdb/python/py-frame.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-frame.c,v
retrieving revision 1.20
diff -u -r1.20 py-frame.c
--- gdb/python/py-frame.c 20 Oct 2011 12:31:30 -0000 1.20
+++ gdb/python/py-frame.c 24 Oct 2011 13:27:35 -0000
@@ -101,9 +101,15 @@
static PyObject *
frapy_is_valid (PyObject *self, PyObject *args)
{
- struct frame_info *frame;
+ struct frame_info *frame = NULL;
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ frame = frame_object_to_frame_info ((frame_object *) self);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);

- frame = frame_object_to_frame_info ((frame_object *) self);
if (frame == NULL)
Py_RETURN_FALSE;

@@ -276,6 +282,7 @@
frame_info_to_frame_object (struct frame_info *frame)
{
frame_object *frame_obj;
+ volatile struct gdb_exception except;

frame_obj = PyObject_New (frame_object, &frame_object_type);
if (frame_obj == NULL)
@@ -285,23 +292,27 @@
return NULL;
}

- /* Try to get the previous frame, to determine if this is the last frame
- in a corrupt stack. If so, we need to store the frame_id of the next
- frame and not of this one (which is possibly invalid). */
- if (get_prev_frame (frame) == NULL
- && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
- && get_next_frame (frame) != NULL)
- {
- frame_obj->frame_id = get_frame_id (get_next_frame (frame));
- frame_obj->frame_id_is_next = 1;
- }
- else
+ TRY_CATCH (except, RETURN_MASK_ALL)
{
- frame_obj->frame_id = get_frame_id (frame);
- frame_obj->frame_id_is_next = 0;
- }

- frame_obj->gdbarch = get_frame_arch (frame);
+ /* Try to get the previous frame, to determine if this is the last frame
+ in a corrupt stack. If so, we need to store the frame_id of the next
+ frame and not of this one (which is possibly invalid). */
+ if (get_prev_frame (frame) == NULL
+ && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
+ && get_next_frame (frame) != NULL)
+ {
+ frame_obj->frame_id = get_frame_id (get_next_frame (frame));
+ frame_obj->frame_id_is_next = 1;
+ }
+ else
+ {
+ frame_obj->frame_id = get_frame_id (frame);
+ frame_obj->frame_id_is_next = 0;
+ }
+ frame_obj->gdbarch = get_frame_arch (frame);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);

return (PyObject *) frame_obj;
}
Index: gdb/python/py-lazy-string.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-lazy-string.c,v
retrieving revision 1.9
diff -u -r1.9 py-lazy-string.c
--- gdb/python/py-lazy-string.c 26 Jan 2011 20:53:45 -0000 1.9
+++ gdb/python/py-lazy-string.c 24 Oct 2011 13:27:35 -0000
@@ -96,7 +96,8 @@
stpy_convert_to_value (PyObject *self, PyObject *args)
{
lazy_string_object *self_string = (lazy_string_object *) self;
- struct value *val;
+ struct value *val = NULL;
+ volatile struct gdb_exception except;

if (self_string->address == 0)
{
@@ -105,7 +106,12 @@
return NULL;
}

- val = value_at_lazy (self_string->type, self_string->address);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ val = value_at_lazy (self_string->type, self_string->address);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
return value_to_value_object (val);
}

Index: gdb/python/py-symbol.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-symbol.c,v
retrieving revision 1.9
diff -u -r1.9 py-symbol.c
--- gdb/python/py-symbol.c 20 Oct 2011 12:31:30 -0000 1.9
+++ gdb/python/py-symbol.c 24 Oct 2011 13:27:35 -0000
@@ -274,9 +274,10 @@
int domain = VAR_DOMAIN, is_a_field_of_this = 0;
const char *name;
static char *keywords[] = { "name", "block", "domain", NULL };
- struct symbol *symbol;
+ struct symbol *symbol = NULL;
PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
const struct block *block = NULL;
+ volatile struct gdb_exception except;

if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
&block_object_type, &block_obj, &domain))
@@ -297,7 +298,11 @@
GDB_PY_HANDLE_EXCEPTION (except);
}

- symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);

ret_tuple = PyTuple_New (2);
if (!ret_tuple)
@@ -335,14 +340,19 @@
int domain = VAR_DOMAIN;
const char *name;
static char *keywords[] = { "name", "domain", NULL };
- struct symbol *symbol;
+ struct symbol *symbol = NULL;
PyObject *sym_obj;
+ volatile struct gdb_exception except;

if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
&domain))
return NULL;

- symbol = lookup_symbol_global (name, NULL, domain);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ symbol = lookup_symbol_global (name, NULL, domain);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);

if (symbol)
{
Index: gdb/python/py-type.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-type.c,v
retrieving revision 1.26
diff -u -r1.26 py-type.c
--- gdb/python/py-type.c 20 Oct 2011 12:31:30 -0000 1.26
+++ gdb/python/py-type.c 24 Oct 2011 13:27:35 -0000
@@ -595,9 +595,7 @@
}
if (except.reason < 0)
{
- PyErr_Format (except.reason == RETURN_QUIT
- ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
- "%s", except.message);
+ gdbpy_convert_exception (except);
return NULL;
}

@@ -609,8 +607,9 @@
const struct block *block)
{
struct type *type;
- char *type_name;
+ 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. */
@@ -625,20 +624,29 @@
if (! type)
return NULL;

- switch (demangled_type)
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ switch (demangled_type)
+ {
+ case DEMANGLE_COMPONENT_REFERENCE:
+ return lookup_reference_type (type);
+ case DEMANGLE_COMPONENT_POINTER:
+ return lookup_pointer_type (type);
+ case DEMANGLE_COMPONENT_CONST:
+ return make_cv_type (1, 0, type, NULL);
+ case DEMANGLE_COMPONENT_VOLATILE:
+ return make_cv_type (0, 1, type, NULL);
+ }
+
+ type_name = cp_comp_to_string (demangled, 10);
+ }
+ if (except.reason < 0)
{
- case DEMANGLE_COMPONENT_REFERENCE:
- return lookup_reference_type (type);
- case DEMANGLE_COMPONENT_POINTER:
- return lookup_pointer_type (type);
- case DEMANGLE_COMPONENT_CONST:
- return make_cv_type (1, 0, type, NULL);
- case DEMANGLE_COMPONENT_VOLATILE:
- return make_cv_type (0, 1, type, NULL);
+ gdbpy_convert_exception (except);
+ return NULL;
}
}

- type_name = cp_comp_to_string (demangled, 10);
type = typy_lookup_typename (type_name, block);
xfree (type_name);

@@ -1007,11 +1015,13 @@
{
result = check_types_worklist (&worklist, cache);
}
- if (except.reason < 0)
- result = Py_NE;
-
+ /* check_types_worklist calls several nested Python helper
+ functions, some of which can raise a GDB Exception, so we
+ just check and convert here. If there is a GDB exception, a
+ comparison is not capable (or trusted), so exit. */
bcache_xfree (cache);
VEC_free (type_equality_entry_d, worklist);
+ GDB_PY_HANDLE_EXCEPTION (except);
}

if (op == result)
@@ -1112,7 +1122,8 @@
struct type *type = ((type_object *) self)->type;
char *field;
int i;
-
+ volatile struct gdb_exception except;
+
field = python_string_to_host_string (key);
if (field == NULL)
return NULL;
@@ -1123,7 +1134,12 @@

for (;;)
{
- CHECK_TYPEDEF (type);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ CHECK_TYPEDEF (type);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
if (TYPE_CODE (type) != TYPE_CODE_PTR
&& TYPE_CODE (type) != TYPE_CODE_REF)
break;
@@ -1178,7 +1194,8 @@
struct type *type = ((type_object *) self)->type;
const char *field;
int i;
-
+ volatile struct gdb_exception except;
+
if (!PyArg_ParseTuple (args, "s", &field))
return NULL;

@@ -1188,7 +1205,11 @@

for (;;)
{
- CHECK_TYPEDEF (type);
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ CHECK_TYPEDEF (type);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
if (TYPE_CODE (type) != TYPE_CODE_PTR
&& TYPE_CODE (type) != TYPE_CODE_REF)
break;