]> Git Repo - binutils.git/blob - gdb/python/py-finishbreakpoint.c
43f68072996e51b108578972856ab2b922f0bffc
[binutils.git] / gdb / python / py-finishbreakpoint.c
1 /* Python interface to finish breakpoints
2
3    Copyright (C) 2011-2015 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
33 /* Function that is called when a Python finish bp is found out of scope.  */
34 static char * const outofscope_func = "out_of_scope";
35
36 /* struct implementing the gdb.FinishBreakpoint object by extending
37    the gdb.Breakpoint class.  */
38 struct finish_breakpoint_object
39 {
40   /* gdb.Breakpoint base class.  */
41   gdbpy_breakpoint_object py_bp;
42   /* gdb.Type object of the value return by the breakpointed function.
43      May be NULL if no debug information was available or return type
44      was VOID.  */
45   PyObject *return_type;
46   /* gdb.Value object of the function finished by this breakpoint.  Will be
47      NULL if return_type is NULL.  */
48   PyObject *function_value;
49   /* When stopped at this FinishBreakpoint, gdb.Value object returned by
50      the function; Py_None if the value is not computable; NULL if GDB is
51      not stopped at a FinishBreakpoint.  */
52   PyObject *return_value;
53 };
54
55 extern PyTypeObject finish_breakpoint_object_type
56     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
57
58 /* Python function to get the 'return_value' attribute of
59    FinishBreakpoint.  */
60
61 static PyObject *
62 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
63 {
64   struct finish_breakpoint_object *self_finishbp =
65       (struct finish_breakpoint_object *) self;
66
67   if (!self_finishbp->return_value)
68     Py_RETURN_NONE;
69
70   Py_INCREF (self_finishbp->return_value);
71   return self_finishbp->return_value;
72 }
73
74 /* Deallocate FinishBreakpoint object.  */
75
76 static void
77 bpfinishpy_dealloc (PyObject *self)
78 {
79   struct finish_breakpoint_object *self_bpfinish =
80         (struct finish_breakpoint_object *) self;
81
82   Py_XDECREF (self_bpfinish->function_value);
83   Py_XDECREF (self_bpfinish->return_type);
84   Py_XDECREF (self_bpfinish->return_value);
85 }
86
87 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
88    of the gdb.FinishBreakpoint object BP_OBJ.  Will compute and cache the
89    `return_value', if possible.  */
90
91 void
92 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
93 {
94   struct finish_breakpoint_object *self_finishbp =
95         (struct finish_breakpoint_object *) bp_obj;
96   volatile struct gdb_exception except;
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_CATCH (except, RETURN_MASK_ALL)
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   if (except.reason < 0)
125     {
126       gdbpy_convert_exception (except);
127       gdbpy_print_stack ();
128     }
129 }
130
131 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
132    of the gdb.FinishBreakpoint object BP_OBJ.  */
133
134 void
135 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
136 {
137   volatile struct gdb_exception except;
138
139   TRY_CATCH (except, RETURN_MASK_ALL)
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   if (except.reason < 0)
146     {
147       gdbpy_convert_exception (except);
148       gdbpy_print_stack ();
149     }
150 }
151
152 /* Python function to create a new breakpoint.  */
153
154 static int
155 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
156 {
157   static char *keywords[] = { "frame", "internal", NULL };
158   struct finish_breakpoint_object *self_bpfinish =
159       (struct finish_breakpoint_object *) self;
160   int type = bp_breakpoint;
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 finish_pc, pc;
169   volatile struct gdb_exception except;
170   char *addr_str, small_buf[100];
171   struct symbol *function;
172
173   if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
174                                     &frame_obj, &internal))
175     return -1;
176
177   TRY_CATCH (except, RETURN_MASK_ALL)
178     {
179       /* Default frame to newest frame if necessary.  */
180       if (frame_obj == NULL)
181         frame = get_current_frame ();
182       else
183         frame = frame_object_to_frame_info (frame_obj);
184
185       if (frame == NULL)
186         {
187           PyErr_SetString (PyExc_ValueError,
188                            _("Invalid ID for the `frame' object."));
189         }
190       else
191         {
192           prev_frame = get_prev_frame (frame);
193           if (prev_frame == 0)
194             {
195               PyErr_SetString (PyExc_ValueError,
196                                _("\"FinishBreakpoint\" not "
197                                  "meaningful in the outermost "
198                                  "frame."));
199             }
200           else if (get_frame_type (prev_frame) == DUMMY_FRAME)
201             {
202               PyErr_SetString (PyExc_ValueError,
203                                _("\"FinishBreakpoint\" cannot "
204                                  "be set on a dummy frame."));
205             }
206           else
207             {
208               frame_id = get_frame_id (prev_frame);
209               if (frame_id_eq (frame_id, null_frame_id))
210                 PyErr_SetString (PyExc_ValueError,
211                                  _("Invalid ID for the `frame' object."));
212             }
213         }
214     }
215   if (except.reason < 0)
216     {
217       gdbpy_convert_exception (except);
218       return -1;
219     }
220   else if (PyErr_Occurred ())
221     return -1;
222
223   thread = pid_to_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_CATCH (except, RETURN_MASK_ALL)
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, frame);
265                   self_bpfinish->function_value =
266                       value_to_value_object (func_value);
267                   PyErr_Clear ();
268                 }
269             }
270         }
271     }
272   if (except.reason < 0)
273     {
274       /* Just swallow.  Either the return type or the function value
275          remain NULL.  */
276     }
277
278   if (self_bpfinish->return_type == NULL || self_bpfinish->function_value == NULL)
279     {
280       /* Won't be able to compute return value.  */
281       Py_XDECREF (self_bpfinish->return_type);
282       Py_XDECREF (self_bpfinish->function_value);
283
284       self_bpfinish->return_type = NULL;
285       self_bpfinish->function_value = NULL;
286     }
287
288   bppy_pending_object = &self_bpfinish->py_bp;
289   bppy_pending_object->number = -1;
290   bppy_pending_object->bp = NULL;
291
292   TRY_CATCH (except, RETURN_MASK_ALL)
293     {
294       /* Set a breakpoint on the return address.  */
295       finish_pc = get_frame_pc (prev_frame);
296       xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
297       addr_str = small_buf;
298
299       create_breakpoint (python_gdbarch,
300                          addr_str, NULL, thread, NULL,
301                          0,
302                          1 /*temp_flag*/,
303                          bp_breakpoint,
304                          0,
305                          AUTO_BOOLEAN_TRUE,
306                          &bkpt_breakpoint_ops,
307                          0, 1, internal_bp, 0);
308     }
309   GDB_PY_SET_HANDLE_EXCEPTION (except);
310
311   self_bpfinish->py_bp.bp->frame_id = frame_id;
312   self_bpfinish->py_bp.is_finish_bp = 1;
313
314   /* Bind the breakpoint with the current program space.  */
315   self_bpfinish->py_bp.bp->pspace = current_program_space;
316
317   return 0;
318 }
319
320 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
321    the current callstack.  Triggers the method OUT_OF_SCOPE if implemented,
322    then delete the breakpoint.  */
323
324 static void
325 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
326 {
327   gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
328   PyObject *py_obj = (PyObject *) bp_obj;
329
330   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
331       && PyObject_HasAttrString (py_obj, outofscope_func))
332     {
333       PyObject *meth_result;
334
335       meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
336       if (meth_result == NULL)
337         gdbpy_print_stack ();
338       Py_XDECREF (meth_result);
339     }
340
341   delete_breakpoint (bpfinish_obj->py_bp.bp);
342 }
343
344 /* Callback for `bpfinishpy_detect_out_scope'.  Triggers Python's
345    `B->out_of_scope' function if B is a FinishBreakpoint out of its scope.  */
346
347 static int
348 bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
349 {
350   volatile struct gdb_exception except;
351   struct breakpoint *bp_stopped = (struct breakpoint *) args;
352   PyObject *py_bp = (PyObject *) b->py_bp_object;
353   struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
354
355   /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
356      not anymore in the current callstack.  */
357   if (py_bp != NULL && b->py_bp_object->is_finish_bp)
358     {
359       struct finish_breakpoint_object *finish_bp =
360           (struct finish_breakpoint_object *) py_bp;
361
362       /* Check scope if not currently stopped at the FinishBreakpoint.  */
363       if (b != bp_stopped)
364         {
365           TRY_CATCH (except, RETURN_MASK_ALL)
366             {
367               if (b->pspace == current_inferior ()->pspace
368                   && (!target_has_registers
369                       || frame_find_by_id (b->frame_id) == NULL))
370                 bpfinishpy_out_of_scope (finish_bp);
371             }
372           if (except.reason < 0)
373             {
374               gdbpy_convert_exception (except);
375               gdbpy_print_stack ();
376             }
377         }
378     }
379
380   return 0;
381 }
382
383 /* Attached to `stop' notifications, check if the execution has run
384    out of the scope of any FinishBreakpoint before it has been hit.  */
385
386 static void
387 bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
388 {
389   struct cleanup *cleanup = ensure_python_env (get_current_arch (),
390                                                current_language);
391
392   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
393                             bs == NULL ? NULL : bs->breakpoint_at);
394
395   do_cleanups (cleanup);
396 }
397
398 /* Attached to `exit' notifications, triggers all the necessary out of
399    scope notifications.  */
400
401 static void
402 bpfinishpy_handle_exit (struct inferior *inf)
403 {
404   struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
405                                                current_language);
406
407   iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
408
409   do_cleanups (cleanup);
410 }
411
412 /* Initialize the Python finish breakpoint code.  */
413
414 int
415 gdbpy_initialize_finishbreakpoints (void)
416 {
417   if (PyType_Ready (&finish_breakpoint_object_type) < 0)
418     return -1;
419
420   if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
421                               (PyObject *) &finish_breakpoint_object_type) < 0)
422     return -1;
423
424   observer_attach_normal_stop (bpfinishpy_handle_stop);
425   observer_attach_inferior_exit (bpfinishpy_handle_exit);
426
427   return 0;
428 }
429
430 static PyGetSetDef finish_breakpoint_object_getset[] = {
431   { "return_value", bpfinishpy_get_returnvalue, NULL,
432   "gdb.Value object representing the return value, if any. \
433 None otherwise.", NULL },
434     { NULL }  /* Sentinel.  */
435 };
436
437 PyTypeObject finish_breakpoint_object_type =
438 {
439   PyVarObject_HEAD_INIT (NULL, 0)
440   "gdb.FinishBreakpoint",         /*tp_name*/
441   sizeof (struct finish_breakpoint_object),  /*tp_basicsize*/
442   0,                              /*tp_itemsize*/
443   bpfinishpy_dealloc,             /*tp_dealloc*/
444   0,                              /*tp_print*/
445   0,                              /*tp_getattr*/
446   0,                              /*tp_setattr*/
447   0,                              /*tp_compare*/
448   0,                              /*tp_repr*/
449   0,                              /*tp_as_number*/
450   0,                              /*tp_as_sequence*/
451   0,                              /*tp_as_mapping*/
452   0,                              /*tp_hash */
453   0,                              /*tp_call*/
454   0,                              /*tp_str*/
455   0,                              /*tp_getattro*/
456   0,                              /*tp_setattro */
457   0,                              /*tp_as_buffer*/
458   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
459   "GDB finish breakpoint object", /* tp_doc */
460   0,                              /* tp_traverse */
461   0,                              /* tp_clear */
462   0,                              /* tp_richcompare */
463   0,                              /* tp_weaklistoffset */
464   0,                              /* tp_iter */
465   0,                              /* tp_iternext */
466   0,                              /* tp_methods */
467   0,                              /* tp_members */
468   finish_breakpoint_object_getset,/* tp_getset */
469   &breakpoint_object_type,        /* tp_base */
470   0,                              /* tp_dict */
471   0,                              /* tp_descr_get */
472   0,                              /* tp_descr_set */
473   0,                              /* tp_dictoffset */
474   bpfinishpy_init,                /* tp_init */
475   0,                              /* tp_alloc */
476   0                               /* tp_new */
477 };
This page took 0.043959 seconds and 2 git commands to generate.