GDB (API)
/home/stan/gdb/src/gdb/python/py-finishbreakpoint.c
Go to the documentation of this file.
00001 /* Python interface to finish breakpoints
00002 
00003    Copyright (C) 2011-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 
00021 
00022 #include "defs.h"
00023 #include "exceptions.h"
00024 #include "python-internal.h"
00025 #include "breakpoint.h"
00026 #include "frame.h"
00027 #include "gdbthread.h"
00028 #include "arch-utils.h"
00029 #include "language.h"
00030 #include "observer.h"
00031 #include "inferior.h"
00032 #include "block.h"
00033 
00034 /* Function that is called when a Python finish bp is found out of scope.  */
00035 static char * const outofscope_func = "out_of_scope";
00036 
00037 /* struct implementing the gdb.FinishBreakpoint object by extending
00038    the gdb.Breakpoint class.  */
00039 struct finish_breakpoint_object
00040 {
00041   /* gdb.Breakpoint base class.  */
00042   breakpoint_object py_bp;
00043   /* gdb.Type object of the value return by the breakpointed function.
00044      May be NULL if no debug information was available or return type
00045      was VOID.  */
00046   PyObject *return_type;
00047   /* gdb.Value object of the function finished by this breakpoint.  Will be
00048      NULL if return_type is NULL.  */
00049   PyObject *function_value;
00050   /* When stopped at this FinishBreakpoint, gdb.Value object returned by
00051      the function; Py_None if the value is not computable; NULL if GDB is
00052      not stopped at a FinishBreakpoint.  */
00053   PyObject *return_value;
00054 };
00055 
00056 static PyTypeObject finish_breakpoint_object_type
00057     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
00058 
00059 /* Python function to get the 'return_value' attribute of
00060    FinishBreakpoint.  */
00061 
00062 static PyObject *
00063 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
00064 {
00065   struct finish_breakpoint_object *self_finishbp =
00066       (struct finish_breakpoint_object *) self;
00067 
00068   if (!self_finishbp->return_value)
00069     Py_RETURN_NONE;
00070 
00071   Py_INCREF (self_finishbp->return_value);
00072   return self_finishbp->return_value;
00073 }
00074 
00075 /* Deallocate FinishBreakpoint object.  */
00076 
00077 static void
00078 bpfinishpy_dealloc (PyObject *self)
00079 {
00080   struct finish_breakpoint_object *self_bpfinish =
00081         (struct finish_breakpoint_object *) self;
00082 
00083   Py_XDECREF (self_bpfinish->function_value);
00084   Py_XDECREF (self_bpfinish->return_type);
00085   Py_XDECREF (self_bpfinish->return_value);
00086 }
00087 
00088 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
00089    of the gdb.FinishBreakpoint object BP_OBJ.  Will compute and cache the
00090    `return_value', if possible.  */
00091 
00092 void
00093 bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj)
00094 {
00095   struct finish_breakpoint_object *self_finishbp =
00096         (struct finish_breakpoint_object *) bp_obj;
00097   volatile struct gdb_exception except;
00098 
00099   /* Can compute return_value only once.  */
00100   gdb_assert (!self_finishbp->return_value);
00101 
00102   if (!self_finishbp->return_type)
00103     return;
00104 
00105   TRY_CATCH (except, RETURN_MASK_ALL)
00106     {
00107       struct value *function =
00108         value_object_to_value (self_finishbp->function_value);
00109       struct type *value_type =
00110         type_object_to_type (self_finishbp->return_type);
00111       struct value *ret = get_return_value (function, value_type);
00112 
00113       if (ret)
00114         {
00115           self_finishbp->return_value = value_to_value_object (ret);
00116           if (!self_finishbp->return_value)
00117               gdbpy_print_stack ();
00118         }
00119       else
00120         {
00121           Py_INCREF (Py_None);
00122           self_finishbp->return_value = Py_None;
00123         }
00124     }
00125   if (except.reason < 0)
00126     {
00127       gdbpy_convert_exception (except);
00128       gdbpy_print_stack ();
00129     }
00130 }
00131 
00132 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
00133    of the gdb.FinishBreakpoint object BP_OBJ.  */
00134 
00135 void
00136 bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj)
00137 {
00138   volatile struct gdb_exception except;
00139 
00140   TRY_CATCH (except, RETURN_MASK_ALL)
00141     {
00142       /* Can't delete it here, but it will be removed at the next stop.  */
00143       disable_breakpoint (bp_obj->bp);
00144       gdb_assert (bp_obj->bp->disposition == disp_del);
00145     }
00146   if (except.reason < 0)
00147     {
00148       gdbpy_convert_exception (except);
00149       gdbpy_print_stack ();
00150     }
00151 }
00152 
00153 /* Python function to create a new breakpoint.  */
00154 
00155 static int
00156 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
00157 {
00158   static char *keywords[] = { "frame", "internal", NULL };
00159   struct finish_breakpoint_object *self_bpfinish =
00160       (struct finish_breakpoint_object *) self;
00161   int type = bp_breakpoint;
00162   PyObject *frame_obj = NULL;
00163   int thread;
00164   struct frame_info *frame = NULL; /* init for gcc -Wall */
00165   struct frame_info *prev_frame = NULL;
00166   struct frame_id frame_id;
00167   PyObject *internal = NULL;
00168   int internal_bp = 0;
00169   CORE_ADDR finish_pc, pc;
00170   volatile struct gdb_exception except;
00171   char *addr_str, small_buf[100];
00172   struct symbol *function;
00173 
00174   if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
00175                                     &frame_obj, &internal))
00176     return -1;
00177 
00178   TRY_CATCH (except, RETURN_MASK_ALL)
00179     {
00180       /* Default frame to newest frame if necessary.  */
00181       if (frame_obj == NULL)
00182         frame = get_current_frame ();
00183       else
00184         frame = frame_object_to_frame_info (frame_obj);
00185 
00186       if (frame == NULL)
00187         {
00188           PyErr_SetString (PyExc_ValueError, 
00189                            _("Invalid ID for the `frame' object."));
00190         }
00191       else
00192         {
00193           prev_frame = get_prev_frame (frame);
00194           if (prev_frame == 0)
00195             {
00196               PyErr_SetString (PyExc_ValueError,
00197                                _("\"FinishBreakpoint\" not "
00198                                  "meaningful in the outermost "
00199                                  "frame."));
00200             }
00201           else if (get_frame_type (prev_frame) == DUMMY_FRAME)
00202             {
00203               PyErr_SetString (PyExc_ValueError,
00204                                _("\"FinishBreakpoint\" cannot "
00205                                  "be set on a dummy frame."));
00206             }
00207           else
00208             {
00209               frame_id = get_frame_id (prev_frame);
00210               if (frame_id_eq (frame_id, null_frame_id))
00211                 PyErr_SetString (PyExc_ValueError,
00212                                  _("Invalid ID for the `frame' object."));
00213             }
00214         }
00215     }
00216   if (except.reason < 0)
00217     {
00218       gdbpy_convert_exception (except);
00219       return -1;
00220     }
00221   else if (PyErr_Occurred ())
00222     return -1;
00223 
00224   thread = pid_to_thread_id (inferior_ptid);
00225   if (thread == 0)
00226     {
00227       PyErr_SetString (PyExc_ValueError,
00228                        _("No thread currently selected."));
00229       return -1;
00230     }
00231 
00232   if (internal)
00233     {
00234       internal_bp = PyObject_IsTrue (internal);
00235       if (internal_bp == -1) 
00236         {
00237           PyErr_SetString (PyExc_ValueError, 
00238                            _("The value of `internal' must be a boolean."));
00239           return -1;
00240         }
00241     }
00242 
00243   /* Find the function we will return from.  */
00244   self_bpfinish->return_type = NULL;
00245   self_bpfinish->function_value = NULL;
00246 
00247   TRY_CATCH (except, RETURN_MASK_ALL)
00248     {
00249       if (get_frame_pc_if_available (frame, &pc))
00250         {
00251           function = find_pc_function (pc);
00252           if (function != NULL)
00253             {
00254               struct type *ret_type =
00255                   TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
00256 
00257               /* Remember only non-void return types.  */
00258               if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
00259                 {
00260                   struct value *func_value;
00261 
00262                   /* Ignore Python errors at this stage.  */
00263                   self_bpfinish->return_type = type_to_type_object (ret_type);
00264                   PyErr_Clear ();
00265                   func_value = read_var_value (function, frame);
00266                   self_bpfinish->function_value =
00267                       value_to_value_object (func_value);
00268                   PyErr_Clear ();
00269                 }
00270             }
00271         }
00272     }
00273   if (except.reason < 0
00274       || !self_bpfinish->return_type || !self_bpfinish->function_value)
00275     {
00276       /* Won't be able to compute return value.  */
00277       Py_XDECREF (self_bpfinish->return_type);
00278       Py_XDECREF (self_bpfinish->function_value);
00279 
00280       self_bpfinish->return_type = NULL;
00281       self_bpfinish->function_value = NULL;
00282     }
00283 
00284   bppy_pending_object = &self_bpfinish->py_bp;
00285   bppy_pending_object->number = -1;
00286   bppy_pending_object->bp = NULL;
00287 
00288   TRY_CATCH (except, RETURN_MASK_ALL)
00289     {
00290       /* Set a breakpoint on the return address.  */
00291       finish_pc = get_frame_pc (prev_frame);
00292       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
00293       addr_str = small_buf;
00294 
00295       create_breakpoint (python_gdbarch,
00296                          addr_str, NULL, thread, NULL,
00297                          0,
00298                          1 /*temp_flag*/,
00299                          bp_breakpoint,
00300                          0,
00301                          AUTO_BOOLEAN_TRUE,
00302                          &bkpt_breakpoint_ops,
00303                          0, 1, internal_bp, 0);
00304     }
00305   GDB_PY_SET_HANDLE_EXCEPTION (except);
00306   
00307   self_bpfinish->py_bp.bp->frame_id = frame_id;
00308   self_bpfinish->py_bp.is_finish_bp = 1;
00309   
00310   /* Bind the breakpoint with the current program space.  */
00311   self_bpfinish->py_bp.bp->pspace = current_program_space;
00312 
00313   return 0;
00314 }
00315 
00316 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
00317    the current callstack.  Triggers the method OUT_OF_SCOPE if implemented,
00318    then delete the breakpoint.  */
00319 
00320 static void
00321 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
00322 {
00323   breakpoint_object *bp_obj = (breakpoint_object *) bpfinish_obj;
00324   PyObject *py_obj = (PyObject *) bp_obj;
00325 
00326   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
00327       && PyObject_HasAttrString (py_obj, outofscope_func))
00328     {
00329       PyObject *meth_result;
00330 
00331       meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
00332       if (meth_result == NULL)
00333         gdbpy_print_stack ();
00334       Py_XDECREF (meth_result);
00335     }
00336 
00337   delete_breakpoint (bpfinish_obj->py_bp.bp);
00338 }
00339 
00340 /* Callback for `bpfinishpy_detect_out_scope'.  Triggers Python's
00341    `B->out_of_scope' function if B is a FinishBreakpoint out of its scope.  */
00342 
00343 static int
00344 bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
00345 {
00346   volatile struct gdb_exception except;
00347   struct breakpoint *bp_stopped = (struct breakpoint *) args;
00348   PyObject *py_bp = (PyObject *) b->py_bp_object;
00349   struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
00350   
00351   /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
00352      not anymore in the current callstack.  */
00353   if (py_bp != NULL && b->py_bp_object->is_finish_bp)
00354     {
00355       struct finish_breakpoint_object *finish_bp =
00356           (struct finish_breakpoint_object *) py_bp;
00357 
00358       /* Check scope if not currently stopped at the FinishBreakpoint.  */
00359       if (b != bp_stopped)
00360         {
00361           TRY_CATCH (except, RETURN_MASK_ALL)
00362             {
00363               if (b->pspace == current_inferior ()->pspace
00364                   && (!target_has_registers
00365                       || frame_find_by_id (b->frame_id) == NULL))
00366                 bpfinishpy_out_of_scope (finish_bp);
00367             }
00368           if (except.reason < 0)
00369             {
00370               gdbpy_convert_exception (except);
00371               gdbpy_print_stack ();
00372             }
00373         }
00374     }
00375 
00376   return 0;
00377 }
00378 
00379 /* Attached to `stop' notifications, check if the execution has run
00380    out of the scope of any FinishBreakpoint before it has been hit.  */
00381 
00382 static void
00383 bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
00384 {
00385   struct cleanup *cleanup = ensure_python_env (get_current_arch (),
00386                                                current_language);
00387 
00388   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
00389                             bs == NULL ? NULL : bs->breakpoint_at);
00390 
00391   do_cleanups (cleanup);
00392 }
00393 
00394 /* Attached to `exit' notifications, triggers all the necessary out of
00395    scope notifications.  */
00396 
00397 static void
00398 bpfinishpy_handle_exit (struct inferior *inf)
00399 {
00400   struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
00401                                                current_language);
00402 
00403   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
00404 
00405   do_cleanups (cleanup);
00406 }
00407 
00408 /* Initialize the Python finish breakpoint code.  */
00409 
00410 int
00411 gdbpy_initialize_finishbreakpoints (void)
00412 {
00413   if (PyType_Ready (&finish_breakpoint_object_type) < 0)
00414     return -1;
00415   
00416   if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
00417                               (PyObject *) &finish_breakpoint_object_type) < 0)
00418     return -1;
00419     
00420   observer_attach_normal_stop (bpfinishpy_handle_stop);
00421   observer_attach_inferior_exit (bpfinishpy_handle_exit);
00422 
00423   return 0;
00424 }
00425 
00426 static PyGetSetDef finish_breakpoint_object_getset[] = {
00427   { "return_value", bpfinishpy_get_returnvalue, NULL,
00428   "gdb.Value object representing the return value, if any. \
00429 None otherwise.", NULL },
00430     { NULL }  /* Sentinel.  */
00431 };
00432 
00433 static PyTypeObject finish_breakpoint_object_type =
00434 {
00435   PyVarObject_HEAD_INIT (NULL, 0)
00436   "gdb.FinishBreakpoint",         /*tp_name*/
00437   sizeof (struct finish_breakpoint_object),  /*tp_basicsize*/
00438   0,                              /*tp_itemsize*/
00439   bpfinishpy_dealloc,             /*tp_dealloc*/
00440   0,                              /*tp_print*/
00441   0,                              /*tp_getattr*/
00442   0,                              /*tp_setattr*/
00443   0,                              /*tp_compare*/
00444   0,                              /*tp_repr*/
00445   0,                              /*tp_as_number*/
00446   0,                              /*tp_as_sequence*/
00447   0,                              /*tp_as_mapping*/
00448   0,                              /*tp_hash */
00449   0,                              /*tp_call*/
00450   0,                              /*tp_str*/
00451   0,                              /*tp_getattro*/
00452   0,                              /*tp_setattro */
00453   0,                              /*tp_as_buffer*/
00454   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
00455   "GDB finish breakpoint object", /* tp_doc */
00456   0,                              /* tp_traverse */
00457   0,                              /* tp_clear */
00458   0,                              /* tp_richcompare */
00459   0,                              /* tp_weaklistoffset */
00460   0,                              /* tp_iter */
00461   0,                              /* tp_iternext */
00462   0,                              /* tp_methods */
00463   0,                              /* tp_members */
00464   finish_breakpoint_object_getset,/* tp_getset */
00465   &breakpoint_object_type,        /* tp_base */
00466   0,                              /* tp_dict */
00467   0,                              /* tp_descr_get */
00468   0,                              /* tp_descr_set */
00469   0,                              /* tp_dictoffset */
00470   bpfinishpy_init,                /* tp_init */
00471   0,                              /* tp_alloc */
00472   0                               /* tp_new */
00473 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines