xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-finishbreakpoint.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* Python interface to finish breakpoints
2 
3    Copyright (C) 2011-2016 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 
21 
22 #include "defs.h"
23 #include "python-internal.h"
24 #include "breakpoint.h"
25 #include "frame.h"
26 #include "gdbthread.h"
27 #include "arch-utils.h"
28 #include "language.h"
29 #include "observer.h"
30 #include "inferior.h"
31 #include "block.h"
32 #include "location.h"
33 
34 /* Function that is called when a Python finish bp is found out of scope.  */
35 static char * const outofscope_func = "out_of_scope";
36 
37 /* struct implementing the gdb.FinishBreakpoint object by extending
38    the gdb.Breakpoint class.  */
39 struct finish_breakpoint_object
40 {
41   /* gdb.Breakpoint base class.  */
42   gdbpy_breakpoint_object py_bp;
43   /* gdb.Type object of the value return by the breakpointed function.
44      May be NULL if no debug information was available or return type
45      was VOID.  */
46   PyObject *return_type;
47   /* gdb.Value object of the function finished by this breakpoint.  Will be
48      NULL if return_type is NULL.  */
49   PyObject *function_value;
50   /* When stopped at this FinishBreakpoint, gdb.Value object returned by
51      the function; Py_None if the value is not computable; NULL if GDB is
52      not stopped at a FinishBreakpoint.  */
53   PyObject *return_value;
54 };
55 
56 extern PyTypeObject finish_breakpoint_object_type
57     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
58 
59 /* Python function to get the 'return_value' attribute of
60    FinishBreakpoint.  */
61 
62 static PyObject *
63 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
64 {
65   struct finish_breakpoint_object *self_finishbp =
66       (struct finish_breakpoint_object *) self;
67 
68   if (!self_finishbp->return_value)
69     Py_RETURN_NONE;
70 
71   Py_INCREF (self_finishbp->return_value);
72   return self_finishbp->return_value;
73 }
74 
75 /* Deallocate FinishBreakpoint object.  */
76 
77 static void
78 bpfinishpy_dealloc (PyObject *self)
79 {
80   struct finish_breakpoint_object *self_bpfinish =
81         (struct finish_breakpoint_object *) self;
82 
83   Py_XDECREF (self_bpfinish->function_value);
84   Py_XDECREF (self_bpfinish->return_type);
85   Py_XDECREF (self_bpfinish->return_value);
86 }
87 
88 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
89    of the gdb.FinishBreakpoint object BP_OBJ.  Will compute and cache the
90    `return_value', if possible.  */
91 
92 void
93 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
94 {
95   struct finish_breakpoint_object *self_finishbp =
96         (struct finish_breakpoint_object *) bp_obj;
97 
98   /* Can compute return_value only once.  */
99   gdb_assert (!self_finishbp->return_value);
100 
101   if (!self_finishbp->return_type)
102     return;
103 
104   TRY
105     {
106       struct value *function =
107         value_object_to_value (self_finishbp->function_value);
108       struct type *value_type =
109         type_object_to_type (self_finishbp->return_type);
110       struct value *ret = get_return_value (function, value_type);
111 
112       if (ret)
113         {
114           self_finishbp->return_value = value_to_value_object (ret);
115           if (!self_finishbp->return_value)
116               gdbpy_print_stack ();
117         }
118       else
119         {
120           Py_INCREF (Py_None);
121           self_finishbp->return_value = Py_None;
122         }
123     }
124   CATCH (except, RETURN_MASK_ALL)
125     {
126       gdbpy_convert_exception (except);
127       gdbpy_print_stack ();
128     }
129   END_CATCH
130 }
131 
132 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
133    of the gdb.FinishBreakpoint object BP_OBJ.  */
134 
135 void
136 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
137 {
138 
139   TRY
140     {
141       /* Can't delete it here, but it will be removed at the next stop.  */
142       disable_breakpoint (bp_obj->bp);
143       gdb_assert (bp_obj->bp->disposition == disp_del);
144     }
145   CATCH (except, RETURN_MASK_ALL)
146     {
147       gdbpy_convert_exception (except);
148       gdbpy_print_stack ();
149     }
150   END_CATCH
151 }
152 
153 /* Python function to create a new breakpoint.  */
154 
155 static int
156 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
157 {
158   static char *keywords[] = { "frame", "internal", NULL };
159   struct finish_breakpoint_object *self_bpfinish =
160       (struct finish_breakpoint_object *) self;
161   PyObject *frame_obj = NULL;
162   int thread;
163   struct frame_info *frame = NULL; /* init for gcc -Wall */
164   struct frame_info *prev_frame = NULL;
165   struct frame_id frame_id;
166   PyObject *internal = NULL;
167   int internal_bp = 0;
168   CORE_ADDR pc;
169   struct symbol *function;
170 
171   if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
172                                     &frame_obj, &internal))
173     return -1;
174 
175   TRY
176     {
177       /* Default frame to newest frame if necessary.  */
178       if (frame_obj == NULL)
179 	frame = get_current_frame ();
180       else
181 	frame = frame_object_to_frame_info (frame_obj);
182 
183       if (frame == NULL)
184 	{
185 	  PyErr_SetString (PyExc_ValueError,
186 			   _("Invalid ID for the `frame' object."));
187 	}
188       else
189 	{
190 	  prev_frame = get_prev_frame (frame);
191 	  if (prev_frame == 0)
192 	    {
193 	      PyErr_SetString (PyExc_ValueError,
194 			       _("\"FinishBreakpoint\" not "
195 				 "meaningful in the outermost "
196 				 "frame."));
197 	    }
198 	  else if (get_frame_type (prev_frame) == DUMMY_FRAME)
199 	    {
200 	      PyErr_SetString (PyExc_ValueError,
201 			       _("\"FinishBreakpoint\" cannot "
202 				 "be set on a dummy frame."));
203 	    }
204 	  else
205 	    {
206 	      frame_id = get_frame_id (prev_frame);
207 	      if (frame_id_eq (frame_id, null_frame_id))
208 		PyErr_SetString (PyExc_ValueError,
209 				 _("Invalid ID for the `frame' object."));
210 	    }
211 	}
212     }
213   CATCH (except, RETURN_MASK_ALL)
214     {
215       gdbpy_convert_exception (except);
216       return -1;
217     }
218   END_CATCH
219 
220   if (PyErr_Occurred ())
221     return -1;
222 
223   thread = ptid_to_global_thread_id (inferior_ptid);
224   if (thread == 0)
225     {
226       PyErr_SetString (PyExc_ValueError,
227                        _("No thread currently selected."));
228       return -1;
229     }
230 
231   if (internal)
232     {
233       internal_bp = PyObject_IsTrue (internal);
234       if (internal_bp == -1)
235         {
236           PyErr_SetString (PyExc_ValueError,
237                            _("The value of `internal' must be a boolean."));
238           return -1;
239         }
240     }
241 
242   /* Find the function we will return from.  */
243   self_bpfinish->return_type = NULL;
244   self_bpfinish->function_value = NULL;
245 
246   TRY
247     {
248       if (get_frame_pc_if_available (frame, &pc))
249         {
250           function = find_pc_function (pc);
251           if (function != NULL)
252             {
253               struct type *ret_type =
254                   TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
255 
256               /* Remember only non-void return types.  */
257               if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
258                 {
259                   struct value *func_value;
260 
261                   /* Ignore Python errors at this stage.  */
262                   self_bpfinish->return_type = type_to_type_object (ret_type);
263                   PyErr_Clear ();
264                   func_value = read_var_value (function, NULL, frame);
265                   self_bpfinish->function_value =
266                       value_to_value_object (func_value);
267                   PyErr_Clear ();
268                 }
269             }
270         }
271     }
272   CATCH (except, RETURN_MASK_ALL)
273     {
274       /* Just swallow.  Either the return type or the function value
275 	 remain NULL.  */
276     }
277   END_CATCH
278 
279   if (self_bpfinish->return_type == NULL || self_bpfinish->function_value == NULL)
280     {
281       /* Won't be able to compute return value.  */
282       Py_XDECREF (self_bpfinish->return_type);
283       Py_XDECREF (self_bpfinish->function_value);
284 
285       self_bpfinish->return_type = NULL;
286       self_bpfinish->function_value = NULL;
287     }
288 
289   bppy_pending_object = &self_bpfinish->py_bp;
290   bppy_pending_object->number = -1;
291   bppy_pending_object->bp = NULL;
292 
293   TRY
294     {
295       struct event_location *location;
296       struct cleanup *back_to;
297 
298       /* Set a breakpoint on the return address.  */
299       location = new_address_location (get_frame_pc (prev_frame), NULL, 0);
300       back_to = make_cleanup_delete_event_location (location);
301       create_breakpoint (python_gdbarch,
302                          location, NULL, thread, NULL,
303                          0,
304                          1 /*temp_flag*/,
305                          bp_breakpoint,
306                          0,
307                          AUTO_BOOLEAN_TRUE,
308                          &bkpt_breakpoint_ops,
309                          0, 1, internal_bp, 0);
310       do_cleanups (back_to);
311     }
312   CATCH (except, RETURN_MASK_ALL)
313     {
314       GDB_PY_SET_HANDLE_EXCEPTION (except);
315     }
316   END_CATCH
317 
318   self_bpfinish->py_bp.bp->frame_id = frame_id;
319   self_bpfinish->py_bp.is_finish_bp = 1;
320 
321   /* Bind the breakpoint with the current program space.  */
322   self_bpfinish->py_bp.bp->pspace = current_program_space;
323 
324   return 0;
325 }
326 
327 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
328    the current callstack.  Triggers the method OUT_OF_SCOPE if implemented,
329    then delete the breakpoint.  */
330 
331 static void
332 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
333 {
334   gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
335   PyObject *py_obj = (PyObject *) bp_obj;
336 
337   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
338       && PyObject_HasAttrString (py_obj, outofscope_func))
339     {
340       PyObject *meth_result;
341 
342       meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
343       if (meth_result == NULL)
344 	gdbpy_print_stack ();
345       Py_XDECREF (meth_result);
346     }
347 
348   delete_breakpoint (bpfinish_obj->py_bp.bp);
349 }
350 
351 /* Callback for `bpfinishpy_detect_out_scope'.  Triggers Python's
352    `B->out_of_scope' function if B is a FinishBreakpoint out of its scope.  */
353 
354 static int
355 bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
356 {
357   struct breakpoint *bp_stopped = (struct breakpoint *) args;
358   PyObject *py_bp = (PyObject *) b->py_bp_object;
359 
360   /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
361      not anymore in the current callstack.  */
362   if (py_bp != NULL && b->py_bp_object->is_finish_bp)
363     {
364       struct finish_breakpoint_object *finish_bp =
365           (struct finish_breakpoint_object *) py_bp;
366 
367       /* Check scope if not currently stopped at the FinishBreakpoint.  */
368       if (b != bp_stopped)
369         {
370           TRY
371             {
372               if (b->pspace == current_inferior ()->pspace
373                   && (!target_has_registers
374                       || frame_find_by_id (b->frame_id) == NULL))
375                 bpfinishpy_out_of_scope (finish_bp);
376             }
377           CATCH (except, RETURN_MASK_ALL)
378             {
379               gdbpy_convert_exception (except);
380               gdbpy_print_stack ();
381             }
382 	  END_CATCH
383         }
384     }
385 
386   return 0;
387 }
388 
389 /* Attached to `stop' notifications, check if the execution has run
390    out of the scope of any FinishBreakpoint before it has been hit.  */
391 
392 static void
393 bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
394 {
395   struct cleanup *cleanup = ensure_python_env (get_current_arch (),
396                                                current_language);
397 
398   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
399                             bs == NULL ? NULL : bs->breakpoint_at);
400 
401   do_cleanups (cleanup);
402 }
403 
404 /* Attached to `exit' notifications, triggers all the necessary out of
405    scope notifications.  */
406 
407 static void
408 bpfinishpy_handle_exit (struct inferior *inf)
409 {
410   struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
411                                                current_language);
412 
413   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
414 
415   do_cleanups (cleanup);
416 }
417 
418 /* Initialize the Python finish breakpoint code.  */
419 
420 int
421 gdbpy_initialize_finishbreakpoints (void)
422 {
423   if (PyType_Ready (&finish_breakpoint_object_type) < 0)
424     return -1;
425 
426   if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
427 			      (PyObject *) &finish_breakpoint_object_type) < 0)
428     return -1;
429 
430   observer_attach_normal_stop (bpfinishpy_handle_stop);
431   observer_attach_inferior_exit (bpfinishpy_handle_exit);
432 
433   return 0;
434 }
435 
436 static PyGetSetDef finish_breakpoint_object_getset[] = {
437   { "return_value", bpfinishpy_get_returnvalue, NULL,
438   "gdb.Value object representing the return value, if any. \
439 None otherwise.", NULL },
440     { NULL }  /* Sentinel.  */
441 };
442 
443 PyTypeObject finish_breakpoint_object_type =
444 {
445   PyVarObject_HEAD_INIT (NULL, 0)
446   "gdb.FinishBreakpoint",         /*tp_name*/
447   sizeof (struct finish_breakpoint_object),  /*tp_basicsize*/
448   0,                              /*tp_itemsize*/
449   bpfinishpy_dealloc,             /*tp_dealloc*/
450   0,                              /*tp_print*/
451   0,                              /*tp_getattr*/
452   0,                              /*tp_setattr*/
453   0,                              /*tp_compare*/
454   0,                              /*tp_repr*/
455   0,                              /*tp_as_number*/
456   0,                              /*tp_as_sequence*/
457   0,                              /*tp_as_mapping*/
458   0,                              /*tp_hash */
459   0,                              /*tp_call*/
460   0,                              /*tp_str*/
461   0,                              /*tp_getattro*/
462   0,                              /*tp_setattro */
463   0,                              /*tp_as_buffer*/
464   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
465   "GDB finish breakpoint object", /* tp_doc */
466   0,                              /* tp_traverse */
467   0,                              /* tp_clear */
468   0,                              /* tp_richcompare */
469   0,                              /* tp_weaklistoffset */
470   0,                              /* tp_iter */
471   0,                              /* tp_iternext */
472   0,                              /* tp_methods */
473   0,                              /* tp_members */
474   finish_breakpoint_object_getset,/* tp_getset */
475   &breakpoint_object_type,        /* tp_base */
476   0,                              /* tp_dict */
477   0,                              /* tp_descr_get */
478   0,                              /* tp_descr_set */
479   0,                              /* tp_dictoffset */
480   bpfinishpy_init,                /* tp_init */
481   0,                              /* tp_alloc */
482   0                               /* tp_new */
483 };
484