This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


After fixing the bugs related to py-value.c not using TRY_CATCH in some
cases, I decided to take a look at the rest of the Python files.  I
caught a few others too.

I also changed the behavior of typy_richcompare to not return Py_NE on
error (And to build an exception instead).  Not sure what the original
reason for this, but in terms of comparison it seems odd to return a
result on an error.

Cheers,

Phil
--

2011-10-11  Phil Muldoon  <pmuldoon@redhat.com>

	* python/py-breakpoint.c (bppy_set_enabled): Use TRY_CATCH.
	(bppy_set_task): Ditto.
	(bppy_delete_breakpoint): Ditto.
	* python/py-symbol.c (gdbpy_lookup_symbol): Ditto.
	(gdbpy_lookup_global_symbol): Ditto.
	* python/py-lazy-string.c (stpy_convert_to_value): Ditto.
	* python/py-frame.c (frapy_is_valid): Ditto.
	(frame_info_to_frame_object): Ditto.
	* python/py-type.c (typy_lookup_type): Ditto.
	(typy_getitem): Ditto.
	(typy_has_key): Ditto.
	(typy_richcompare): Use TRY_CATCH.  Do not return Py_NE on error.

--

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 3dc0fca..6e46e88 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -150,6 +150,7 @@ bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
 {
   breakpoint_object *self_bp = (breakpoint_object *) self;
   int cmp;
+  volatile struct gdb_exception except;
 
   BPPY_SET_REQUIRE_VALID (self_bp);
 
@@ -170,10 +171,16 @@ bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
   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 @@ bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
 {
   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 @@ bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
       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 @@ static PyObject *
 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;
 }
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 9143367..53f8e69 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -101,9 +101,15 @@ frapy_str (PyObject *self)
 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 @@ PyObject *
 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 @@ frame_info_to_frame_object (struct frame_info *frame)
       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;
 }
diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 940ce88..45ba41e 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -96,7 +96,8 @@ static PyObject *
 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 @@ stpy_convert_to_value  (PyObject *self, PyObject *args)
       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);
 }
 
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 8a8510e..fd365a7 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -274,9 +274,10 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
   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;
   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 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
       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 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
   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)
     {
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index c7fd25b..f4746aa 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -608,8 +608,9 @@ typy_lookup_type (struct demangle_component *demangled,
 		  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.  */
@@ -624,20 +625,31 @@ typy_lookup_type (struct demangle_component *demangled,
       if (! type)
 	return NULL;
 
-      switch (demangled_type)
+      TRY_CATCH (except, RETURN_MASK_ALL)
 	{
-	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);
+	  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)
+	{
+	  PyErr_Format (except.reason == RETURN_QUIT
+			? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
+			"%s", except.message);
+	  return NULL;
 	}
     }
 
-  type_name = cp_comp_to_string (demangled, 10);
   type = typy_lookup_typename (type_name, block);
   xfree (type_name);
 
@@ -990,8 +1002,16 @@ typy_richcompare (PyObject *self, PyObject *other, int op)
 	{
 	  result = check_types_worklist (&worklist, cache);
 	}
+      /* 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.  */
       if (except.reason < 0)
-	result = Py_NE;
+	{
+	  bcache_xfree (cache);
+	  VEC_free (type_equality_entry_d, worklist);
+	  GDB_PY_HANDLE_EXCEPTION (except);
+	}
 
       bcache_xfree (cache);
       VEC_free (type_equality_entry_d, worklist);
@@ -1095,7 +1115,8 @@ typy_getitem (PyObject *self, PyObject *key)
   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;
@@ -1106,7 +1127,12 @@ typy_getitem (PyObject *self, PyObject *key)
 
   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;
@@ -1161,7 +1187,8 @@ typy_has_key (PyObject *self, PyObject *args)
   struct type *type = ((type_object *) self)->type;
   char *field;
   int i;
-  
+  volatile struct gdb_exception except;
+
   if (!PyArg_ParseTuple (args, "s", &field))
     return NULL;
 
@@ -1171,7 +1198,11 @@ typy_has_key (PyObject *self, PyObject *args)
 
   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;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]