gdb/python/py-finishbreakpoint.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Python interface to finish breakpoints

  2.    Copyright (C) 2011-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */



  14. #include "defs.h"
  15. #include "python-internal.h"
  16. #include "breakpoint.h"
  17. #include "frame.h"
  18. #include "gdbthread.h"
  19. #include "arch-utils.h"
  20. #include "language.h"
  21. #include "observer.h"
  22. #include "inferior.h"
  23. #include "block.h"

  24. /* Function that is called when a Python finish bp is found out of scope.  */
  25. static char * const outofscope_func = "out_of_scope";

  26. /* struct implementing the gdb.FinishBreakpoint object by extending
  27.    the gdb.Breakpoint class.  */
  28. struct finish_breakpoint_object
  29. {
  30.   /* gdb.Breakpoint base class.  */
  31.   gdbpy_breakpoint_object py_bp;
  32.   /* gdb.Type object of the value return by the breakpointed function.
  33.      May be NULL if no debug information was available or return type
  34.      was VOID.  */
  35.   PyObject *return_type;
  36.   /* gdb.Value object of the function finished by this breakpoint.  Will be
  37.      NULL if return_type is NULL.  */
  38.   PyObject *function_value;
  39.   /* When stopped at this FinishBreakpoint, gdb.Value object returned by
  40.      the function; Py_None if the value is not computable; NULL if GDB is
  41.      not stopped at a FinishBreakpoint.  */
  42.   PyObject *return_value;
  43. };

  44. static PyTypeObject finish_breakpoint_object_type
  45.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");

  46. /* Python function to get the 'return_value' attribute of
  47.    FinishBreakpoint.  */

  48. static PyObject *
  49. bpfinishpy_get_returnvalue (PyObject *self, void *closure)
  50. {
  51.   struct finish_breakpoint_object *self_finishbp =
  52.       (struct finish_breakpoint_object *) self;

  53.   if (!self_finishbp->return_value)
  54.     Py_RETURN_NONE;

  55.   Py_INCREF (self_finishbp->return_value);
  56.   return self_finishbp->return_value;
  57. }

  58. /* Deallocate FinishBreakpoint object.  */

  59. static void
  60. bpfinishpy_dealloc (PyObject *self)
  61. {
  62.   struct finish_breakpoint_object *self_bpfinish =
  63.         (struct finish_breakpoint_object *) self;

  64.   Py_XDECREF (self_bpfinish->function_value);
  65.   Py_XDECREF (self_bpfinish->return_type);
  66.   Py_XDECREF (self_bpfinish->return_value);
  67. }

  68. /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
  69.    of the gdb.FinishBreakpoint object BP_OBJ.  Will compute and cache the
  70.    `return_value', if possible.  */

  71. void
  72. bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
  73. {
  74.   struct finish_breakpoint_object *self_finishbp =
  75.         (struct finish_breakpoint_object *) bp_obj;
  76.   volatile struct gdb_exception except;

  77.   /* Can compute return_value only once.  */
  78.   gdb_assert (!self_finishbp->return_value);

  79.   if (!self_finishbp->return_type)
  80.     return;

  81.   TRY_CATCH (except, RETURN_MASK_ALL)
  82.     {
  83.       struct value *function =
  84.         value_object_to_value (self_finishbp->function_value);
  85.       struct type *value_type =
  86.         type_object_to_type (self_finishbp->return_type);
  87.       struct value *ret = get_return_value (function, value_type);

  88.       if (ret)
  89.         {
  90.           self_finishbp->return_value = value_to_value_object (ret);
  91.           if (!self_finishbp->return_value)
  92.               gdbpy_print_stack ();
  93.         }
  94.       else
  95.         {
  96.           Py_INCREF (Py_None);
  97.           self_finishbp->return_value = Py_None;
  98.         }
  99.     }
  100.   if (except.reason < 0)
  101.     {
  102.       gdbpy_convert_exception (except);
  103.       gdbpy_print_stack ();
  104.     }
  105. }

  106. /* Triggered when gdbpy_should_stop has triggered the `stop' callback
  107.    of the gdb.FinishBreakpoint object BP_OBJ.  */

  108. void
  109. bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
  110. {
  111.   volatile struct gdb_exception except;

  112.   TRY_CATCH (except, RETURN_MASK_ALL)
  113.     {
  114.       /* Can't delete it here, but it will be removed at the next stop.  */
  115.       disable_breakpoint (bp_obj->bp);
  116.       gdb_assert (bp_obj->bp->disposition == disp_del);
  117.     }
  118.   if (except.reason < 0)
  119.     {
  120.       gdbpy_convert_exception (except);
  121.       gdbpy_print_stack ();
  122.     }
  123. }

  124. /* Python function to create a new breakpoint.  */

  125. static int
  126. bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
  127. {
  128.   static char *keywords[] = { "frame", "internal", NULL };
  129.   struct finish_breakpoint_object *self_bpfinish =
  130.       (struct finish_breakpoint_object *) self;
  131.   int type = bp_breakpoint;
  132.   PyObject *frame_obj = NULL;
  133.   int thread;
  134.   struct frame_info *frame = NULL; /* init for gcc -Wall */
  135.   struct frame_info *prev_frame = NULL;
  136.   struct frame_id frame_id;
  137.   PyObject *internal = NULL;
  138.   int internal_bp = 0;
  139.   CORE_ADDR finish_pc, pc;
  140.   volatile struct gdb_exception except;
  141.   char *addr_str, small_buf[100];
  142.   struct symbol *function;

  143.   if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
  144.                                     &frame_obj, &internal))
  145.     return -1;

  146.   TRY_CATCH (except, RETURN_MASK_ALL)
  147.     {
  148.       /* Default frame to newest frame if necessary.  */
  149.       if (frame_obj == NULL)
  150.         frame = get_current_frame ();
  151.       else
  152.         frame = frame_object_to_frame_info (frame_obj);

  153.       if (frame == NULL)
  154.         {
  155.           PyErr_SetString (PyExc_ValueError,
  156.                            _("Invalid ID for the `frame' object."));
  157.         }
  158.       else
  159.         {
  160.           prev_frame = get_prev_frame (frame);
  161.           if (prev_frame == 0)
  162.             {
  163.               PyErr_SetString (PyExc_ValueError,
  164.                                _("\"FinishBreakpoint\" not "
  165.                                  "meaningful in the outermost "
  166.                                  "frame."));
  167.             }
  168.           else if (get_frame_type (prev_frame) == DUMMY_FRAME)
  169.             {
  170.               PyErr_SetString (PyExc_ValueError,
  171.                                _("\"FinishBreakpoint\" cannot "
  172.                                  "be set on a dummy frame."));
  173.             }
  174.           else
  175.             {
  176.               frame_id = get_frame_id (prev_frame);
  177.               if (frame_id_eq (frame_id, null_frame_id))
  178.                 PyErr_SetString (PyExc_ValueError,
  179.                                  _("Invalid ID for the `frame' object."));
  180.             }
  181.         }
  182.     }
  183.   if (except.reason < 0)
  184.     {
  185.       gdbpy_convert_exception (except);
  186.       return -1;
  187.     }
  188.   else if (PyErr_Occurred ())
  189.     return -1;

  190.   thread = pid_to_thread_id (inferior_ptid);
  191.   if (thread == 0)
  192.     {
  193.       PyErr_SetString (PyExc_ValueError,
  194.                        _("No thread currently selected."));
  195.       return -1;
  196.     }

  197.   if (internal)
  198.     {
  199.       internal_bp = PyObject_IsTrue (internal);
  200.       if (internal_bp == -1)
  201.         {
  202.           PyErr_SetString (PyExc_ValueError,
  203.                            _("The value of `internal' must be a boolean."));
  204.           return -1;
  205.         }
  206.     }

  207.   /* Find the function we will return from.  */
  208.   self_bpfinish->return_type = NULL;
  209.   self_bpfinish->function_value = NULL;

  210.   TRY_CATCH (except, RETURN_MASK_ALL)
  211.     {
  212.       if (get_frame_pc_if_available (frame, &pc))
  213.         {
  214.           function = find_pc_function (pc);
  215.           if (function != NULL)
  216.             {
  217.               struct type *ret_type =
  218.                   TYPE_TARGET_TYPE (SYMBOL_TYPE (function));

  219.               /* Remember only non-void return types.  */
  220.               if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
  221.                 {
  222.                   struct value *func_value;

  223.                   /* Ignore Python errors at this stage.  */
  224.                   self_bpfinish->return_type = type_to_type_object (ret_type);
  225.                   PyErr_Clear ();
  226.                   func_value = read_var_value (function, frame);
  227.                   self_bpfinish->function_value =
  228.                       value_to_value_object (func_value);
  229.                   PyErr_Clear ();
  230.                 }
  231.             }
  232.         }
  233.     }
  234.   if (except.reason < 0
  235.       || !self_bpfinish->return_type || !self_bpfinish->function_value)
  236.     {
  237.       /* Won't be able to compute return value.  */
  238.       Py_XDECREF (self_bpfinish->return_type);
  239.       Py_XDECREF (self_bpfinish->function_value);

  240.       self_bpfinish->return_type = NULL;
  241.       self_bpfinish->function_value = NULL;
  242.     }

  243.   bppy_pending_object = &self_bpfinish->py_bp;
  244.   bppy_pending_object->number = -1;
  245.   bppy_pending_object->bp = NULL;

  246.   TRY_CATCH (except, RETURN_MASK_ALL)
  247.     {
  248.       /* Set a breakpoint on the return address.  */
  249.       finish_pc = get_frame_pc (prev_frame);
  250.       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
  251.       addr_str = small_buf;

  252.       create_breakpoint (python_gdbarch,
  253.                          addr_str, NULL, thread, NULL,
  254.                          0,
  255.                          1 /*temp_flag*/,
  256.                          bp_breakpoint,
  257.                          0,
  258.                          AUTO_BOOLEAN_TRUE,
  259.                          &bkpt_breakpoint_ops,
  260.                          0, 1, internal_bp, 0);
  261.     }
  262.   GDB_PY_SET_HANDLE_EXCEPTION (except);

  263.   self_bpfinish->py_bp.bp->frame_id = frame_id;
  264.   self_bpfinish->py_bp.is_finish_bp = 1;

  265.   /* Bind the breakpoint with the current program space.  */
  266.   self_bpfinish->py_bp.bp->pspace = current_program_space;

  267.   return 0;
  268. }

  269. /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
  270.    the current callstack.  Triggers the method OUT_OF_SCOPE if implemented,
  271.    then delete the breakpoint.  */

  272. static void
  273. bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
  274. {
  275.   gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
  276.   PyObject *py_obj = (PyObject *) bp_obj;

  277.   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
  278.       && PyObject_HasAttrString (py_obj, outofscope_func))
  279.     {
  280.       PyObject *meth_result;

  281.       meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
  282.       if (meth_result == NULL)
  283.         gdbpy_print_stack ();
  284.       Py_XDECREF (meth_result);
  285.     }

  286.   delete_breakpoint (bpfinish_obj->py_bp.bp);
  287. }

  288. /* Callback for `bpfinishpy_detect_out_scope'.  Triggers Python's
  289.    `B->out_of_scope' function if B is a FinishBreakpoint out of its scope.  */

  290. static int
  291. bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
  292. {
  293.   volatile struct gdb_exception except;
  294.   struct breakpoint *bp_stopped = (struct breakpoint *) args;
  295.   PyObject *py_bp = (PyObject *) b->py_bp_object;
  296.   struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();

  297.   /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
  298.      not anymore in the current callstack.  */
  299.   if (py_bp != NULL && b->py_bp_object->is_finish_bp)
  300.     {
  301.       struct finish_breakpoint_object *finish_bp =
  302.           (struct finish_breakpoint_object *) py_bp;

  303.       /* Check scope if not currently stopped at the FinishBreakpoint.  */
  304.       if (b != bp_stopped)
  305.         {
  306.           TRY_CATCH (except, RETURN_MASK_ALL)
  307.             {
  308.               if (b->pspace == current_inferior ()->pspace
  309.                   && (!target_has_registers
  310.                       || frame_find_by_id (b->frame_id) == NULL))
  311.                 bpfinishpy_out_of_scope (finish_bp);
  312.             }
  313.           if (except.reason < 0)
  314.             {
  315.               gdbpy_convert_exception (except);
  316.               gdbpy_print_stack ();
  317.             }
  318.         }
  319.     }

  320.   return 0;
  321. }

  322. /* Attached to `stop' notifications, check if the execution has run
  323.    out of the scope of any FinishBreakpoint before it has been hit.  */

  324. static void
  325. bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
  326. {
  327.   struct cleanup *cleanup = ensure_python_env (get_current_arch (),
  328.                                                current_language);

  329.   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
  330.                             bs == NULL ? NULL : bs->breakpoint_at);

  331.   do_cleanups (cleanup);
  332. }

  333. /* Attached to `exit' notifications, triggers all the necessary out of
  334.    scope notifications.  */

  335. static void
  336. bpfinishpy_handle_exit (struct inferior *inf)
  337. {
  338.   struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
  339.                                                current_language);

  340.   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);

  341.   do_cleanups (cleanup);
  342. }

  343. /* Initialize the Python finish breakpoint code.  */

  344. int
  345. gdbpy_initialize_finishbreakpoints (void)
  346. {
  347.   if (PyType_Ready (&finish_breakpoint_object_type) < 0)
  348.     return -1;

  349.   if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
  350.                               (PyObject *) &finish_breakpoint_object_type) < 0)
  351.     return -1;

  352.   observer_attach_normal_stop (bpfinishpy_handle_stop);
  353.   observer_attach_inferior_exit (bpfinishpy_handle_exit);

  354.   return 0;
  355. }

  356. static PyGetSetDef finish_breakpoint_object_getset[] = {
  357.   { "return_value", bpfinishpy_get_returnvalue, NULL,
  358.   "gdb.Value object representing the return value, if any. \
  359. None otherwise.", NULL },
  360.     { NULL/* Sentinel.  */
  361. };

  362. static PyTypeObject finish_breakpoint_object_type =
  363. {
  364.   PyVarObject_HEAD_INIT (NULL, 0)
  365.   "gdb.FinishBreakpoint",         /*tp_name*/
  366.   sizeof (struct finish_breakpoint_object),  /*tp_basicsize*/
  367.   0,                              /*tp_itemsize*/
  368.   bpfinishpy_dealloc,             /*tp_dealloc*/
  369.   0,                              /*tp_print*/
  370.   0,                              /*tp_getattr*/
  371.   0,                              /*tp_setattr*/
  372.   0,                              /*tp_compare*/
  373.   0,                              /*tp_repr*/
  374.   0,                              /*tp_as_number*/
  375.   0,                              /*tp_as_sequence*/
  376.   0,                              /*tp_as_mapping*/
  377.   0,                              /*tp_hash */
  378.   0,                              /*tp_call*/
  379.   0,                              /*tp_str*/
  380.   0,                              /*tp_getattro*/
  381.   0,                              /*tp_setattro */
  382.   0,                              /*tp_as_buffer*/
  383.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
  384.   "GDB finish breakpoint object", /* tp_doc */
  385.   0,                              /* tp_traverse */
  386.   0,                              /* tp_clear */
  387.   0,                              /* tp_richcompare */
  388.   0,                              /* tp_weaklistoffset */
  389.   0,                              /* tp_iter */
  390.   0,                              /* tp_iternext */
  391.   0,                              /* tp_methods */
  392.   0,                              /* tp_members */
  393.   finish_breakpoint_object_getset,/* tp_getset */
  394.   &breakpoint_object_type,        /* tp_base */
  395.   0,                              /* tp_dict */
  396.   0,                              /* tp_descr_get */
  397.   0,                              /* tp_descr_set */
  398.   0,                              /* tp_dictoffset */
  399.   bpfinishpy_init,                /* tp_init */
  400.   0,                              /* tp_alloc */
  401.   0                               /* tp_new */
  402. };