]> Git Repo - binutils.git/blob - gdb/python/py-finishbreakpoint.c
gdb: remove TYPE_TARGET_TYPE
[binutils.git] / gdb / python / py-finishbreakpoint.c
1 /* Python interface to finish breakpoints
2
3    Copyright (C) 2011-2022 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 "observable.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 const char 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
44   /* gdb.Symbol object of the function finished by this breakpoint.
45
46      nullptr if no debug information was available or return type was VOID.  */
47   PyObject *func_symbol;
48
49   /* gdb.Value object of the function finished by this breakpoint.
50
51      nullptr if no debug information was available or return type was VOID.  */
52   PyObject *function_value;
53
54   /* When stopped at this FinishBreakpoint, gdb.Value object returned by
55      the function; Py_None if the value is not computable; NULL if GDB is
56      not stopped at a FinishBreakpoint.  */
57   PyObject *return_value;
58 };
59
60 extern PyTypeObject finish_breakpoint_object_type
61     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
62
63 /* Python function to get the 'return_value' attribute of
64    FinishBreakpoint.  */
65
66 static PyObject *
67 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
68 {
69   struct finish_breakpoint_object *self_finishbp =
70       (struct finish_breakpoint_object *) self;
71
72   if (!self_finishbp->return_value)
73     Py_RETURN_NONE;
74
75   Py_INCREF (self_finishbp->return_value);
76   return self_finishbp->return_value;
77 }
78
79 /* Deallocate FinishBreakpoint object.  */
80
81 static void
82 bpfinishpy_dealloc (PyObject *self)
83 {
84   struct finish_breakpoint_object *self_bpfinish =
85         (struct finish_breakpoint_object *) self;
86
87   Py_XDECREF (self_bpfinish->func_symbol);
88   Py_XDECREF (self_bpfinish->function_value);
89   Py_XDECREF (self_bpfinish->return_value);
90   Py_TYPE (self)->tp_free (self);
91 }
92
93 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
94    of the gdb.FinishBreakpoint object BP_OBJ.  Will compute and cache the
95    `return_value', if possible.  */
96
97 void
98 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
99 {
100   struct finish_breakpoint_object *self_finishbp =
101         (struct finish_breakpoint_object *) bp_obj;
102
103   /* Can compute return_value only once.  */
104   gdb_assert (!self_finishbp->return_value);
105
106   if (self_finishbp->func_symbol == nullptr)
107     return;
108
109   try
110     {
111       struct symbol *func_symbol =
112         symbol_object_to_symbol (self_finishbp->func_symbol);
113       struct value *function =
114         value_object_to_value (self_finishbp->function_value);
115       struct value *ret =
116         get_return_value (func_symbol, function);
117
118       if (ret)
119         {
120           self_finishbp->return_value = value_to_value_object (ret);
121           if (!self_finishbp->return_value)
122               gdbpy_print_stack ();
123         }
124       else
125         {
126           Py_INCREF (Py_None);
127           self_finishbp->return_value = Py_None;
128         }
129     }
130   catch (const gdb_exception &except)
131     {
132       gdbpy_convert_exception (except);
133       gdbpy_print_stack ();
134     }
135 }
136
137 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
138    of the gdb.FinishBreakpoint object BP_OBJ.  */
139
140 void
141 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
142 {
143
144   try
145     {
146       /* Can't delete it here, but it will be removed at the next stop.  */
147       disable_breakpoint (bp_obj->bp);
148       gdb_assert (bp_obj->bp->disposition == disp_del);
149     }
150   catch (const gdb_exception &except)
151     {
152       gdbpy_convert_exception (except);
153       gdbpy_print_stack ();
154     }
155 }
156
157 /* Python function to create a new breakpoint.  */
158
159 static int
160 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
161 {
162   static const char *keywords[] = { "frame", "internal", NULL };
163   struct finish_breakpoint_object *self_bpfinish =
164       (struct finish_breakpoint_object *) self;
165   PyObject *frame_obj = NULL;
166   int thread;
167   struct frame_info *frame = NULL; /* init for gcc -Wall */
168   struct frame_info *prev_frame = NULL;
169   struct frame_id frame_id;
170   PyObject *internal = NULL;
171   int internal_bp = 0;
172   CORE_ADDR pc;
173
174   if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
175                                         &frame_obj, &internal))
176     return -1;
177
178   try
179     {
180       /* Default frame to newest frame if necessary.  */
181       if (frame_obj == NULL)
182         frame = get_current_frame ();
183       else
184         frame = frame_object_to_frame_info (frame_obj);
185
186       if (frame == NULL)
187         {
188           PyErr_SetString (PyExc_ValueError,
189                            _("Invalid ID for the `frame' object."));
190         }
191       else
192         {
193           prev_frame = get_prev_frame (frame);
194           if (prev_frame == 0)
195             {
196               PyErr_SetString (PyExc_ValueError,
197                                _("\"FinishBreakpoint\" not "
198                                  "meaningful in the outermost "
199                                  "frame."));
200             }
201           else if (get_frame_type (prev_frame) == DUMMY_FRAME)
202             {
203               PyErr_SetString (PyExc_ValueError,
204                                _("\"FinishBreakpoint\" cannot "
205                                  "be set on a dummy frame."));
206             }
207           else
208             {
209               frame_id = get_frame_id (prev_frame);
210               if (frame_id_eq (frame_id, null_frame_id))
211                 PyErr_SetString (PyExc_ValueError,
212                                  _("Invalid ID for the `frame' object."));
213             }
214         }
215     }
216   catch (const gdb_exception &except)
217     {
218       gdbpy_convert_exception (except);
219       return -1;
220     }
221
222   if (PyErr_Occurred ())
223     return -1;
224
225   if (inferior_ptid == null_ptid)
226     {
227       PyErr_SetString (PyExc_ValueError,
228                        _("No thread currently selected."));
229       return -1;
230     }
231
232   thread = inferior_thread ()->global_num;
233
234   if (internal)
235     {
236       internal_bp = PyObject_IsTrue (internal);
237       if (internal_bp == -1)
238         {
239           PyErr_SetString (PyExc_ValueError,
240                            _("The value of `internal' must be a boolean."));
241           return -1;
242         }
243     }
244
245   /* Find the function we will return from.  */
246   self_bpfinish->func_symbol = nullptr;
247   self_bpfinish->function_value = nullptr;
248
249   try
250     {
251       if (get_frame_pc_if_available (frame, &pc))
252         {
253           struct symbol *function = find_pc_function (pc);
254           if (function != nullptr)
255             {
256               struct type *ret_type =
257                 check_typedef (function->type ()->target_type ());
258
259               /* Remember only non-void return types.  */
260               if (ret_type->code () != TYPE_CODE_VOID)
261                 {
262                   /* Ignore Python errors at this stage.  */
263                   value *func_value = read_var_value (function, NULL, frame);
264                   self_bpfinish->function_value
265                     = value_to_value_object (func_value);
266                   PyErr_Clear ();
267
268                   self_bpfinish->func_symbol
269                     = symbol_to_symbol_object (function);
270                   PyErr_Clear ();
271                 }
272             }
273         }
274     }
275   catch (const gdb_exception &except)
276     {
277       /* Just swallow.  Either the return type or the function value
278          remain NULL.  */
279     }
280
281   if (self_bpfinish->func_symbol == nullptr
282       || self_bpfinish->function_value == nullptr)
283     {
284       /* Won't be able to compute return value.  */
285       Py_XDECREF (self_bpfinish->func_symbol);
286       Py_XDECREF (self_bpfinish->function_value);
287
288       self_bpfinish->func_symbol = nullptr;
289       self_bpfinish->function_value = nullptr;
290     }
291
292   bppy_pending_object = &self_bpfinish->py_bp;
293   bppy_pending_object->number = -1;
294   bppy_pending_object->bp = NULL;
295
296   try
297     {
298       /* Set a breakpoint on the return address.  */
299       location_spec_up locspec
300         = new_address_location_spec (get_frame_pc (prev_frame), NULL, 0);
301       create_breakpoint (gdbpy_enter::get_gdbarch (),
302                          locspec.get (), NULL, thread, NULL, false,
303                          0,
304                          1 /*temp_flag*/,
305                          bp_breakpoint,
306                          0,
307                          AUTO_BOOLEAN_TRUE,
308                          &code_breakpoint_ops,
309                          0, 1, internal_bp, 0);
310     }
311   catch (const gdb_exception &except)
312     {
313       GDB_PY_SET_HANDLE_EXCEPTION (except);
314     }
315
316   self_bpfinish->py_bp.bp->frame_id = frame_id;
317   self_bpfinish->py_bp.is_finish_bp = 1;
318
319   /* Bind the breakpoint with the current program space.  */
320   self_bpfinish->py_bp.bp->pspace = current_program_space;
321
322   return 0;
323 }
324
325 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
326    the current callstack.  Triggers the method OUT_OF_SCOPE if implemented,
327    then delete the breakpoint.  */
328
329 static void
330 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
331 {
332   gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
333   PyObject *py_obj = (PyObject *) bp_obj;
334
335   if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
336       && PyObject_HasAttrString (py_obj, outofscope_func))
337     {
338       gdbpy_ref<> meth_result (PyObject_CallMethod (py_obj, outofscope_func,
339                                                     NULL));
340       if (meth_result == NULL)
341         gdbpy_print_stack ();
342     }
343
344   delete_breakpoint (bpfinish_obj->py_bp.bp);
345 }
346
347 /* Callback for `bpfinishpy_detect_out_scope'.  Triggers Python's
348    `B->out_of_scope' function if B is a FinishBreakpoint out of its scope.  */
349
350 static void
351 bpfinishpy_detect_out_scope_cb (struct breakpoint *b,
352                                 struct breakpoint *bp_stopped)
353 {
354   PyObject *py_bp = (PyObject *) b->py_bp_object;
355
356   /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
357      not anymore in the current callstack.  */
358   if (py_bp != NULL && b->py_bp_object->is_finish_bp)
359     {
360       struct finish_breakpoint_object *finish_bp =
361           (struct finish_breakpoint_object *) py_bp;
362
363       /* Check scope if not currently stopped at the FinishBreakpoint.  */
364       if (b != bp_stopped)
365         {
366           try
367             {
368               if (b->pspace == current_inferior ()->pspace
369                   && (!target_has_registers ()
370                       || frame_find_by_id (b->frame_id) == NULL))
371                 bpfinishpy_out_of_scope (finish_bp);
372             }
373           catch (const gdb_exception &except)
374             {
375               gdbpy_convert_exception (except);
376               gdbpy_print_stack ();
377             }
378         }
379     }
380 }
381
382 /* Attached to `stop' notifications, check if the execution has run
383    out of the scope of any FinishBreakpoint before it has been hit.  */
384
385 static void
386 bpfinishpy_handle_stop (struct bpstat *bs, int print_frame)
387 {
388   gdbpy_enter enter_py;
389
390   for (breakpoint *bp : all_breakpoints_safe ())
391     bpfinishpy_detect_out_scope_cb (bp, bs == NULL ? NULL : bs->breakpoint_at);
392 }
393
394 /* Attached to `exit' notifications, triggers all the necessary out of
395    scope notifications.  */
396
397 static void
398 bpfinishpy_handle_exit (struct inferior *inf)
399 {
400   gdbpy_enter enter_py (target_gdbarch ());
401
402   for (breakpoint *bp : all_breakpoints_safe ())
403     bpfinishpy_detect_out_scope_cb (bp, nullptr);
404 }
405
406 /* Initialize the Python finish breakpoint code.  */
407
408 int
409 gdbpy_initialize_finishbreakpoints (void)
410 {
411   if (PyType_Ready (&finish_breakpoint_object_type) < 0)
412     return -1;
413
414   if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
415                               (PyObject *) &finish_breakpoint_object_type) < 0)
416     return -1;
417
418   gdb::observers::normal_stop.attach (bpfinishpy_handle_stop,
419                                       "py-finishbreakpoint");
420   gdb::observers::inferior_exit.attach (bpfinishpy_handle_exit,
421                                         "py-finishbreakpoint");
422
423   return 0;
424 }
425
426 static gdb_PyGetSetDef finish_breakpoint_object_getset[] = {
427   { "return_value", bpfinishpy_get_returnvalue, NULL,
428   "gdb.Value object representing the return value, if any. \
429 None otherwise.", NULL },
430     { NULL }  /* Sentinel.  */
431 };
432
433 PyTypeObject finish_breakpoint_object_type =
434 {
435   PyVarObject_HEAD_INIT (NULL, 0)
436   "gdb.FinishBreakpoint",         /*tp_name*/
437   sizeof (struct finish_breakpoint_object),  /*tp_basicsize*/
438   0,                              /*tp_itemsize*/
439   bpfinishpy_dealloc,             /*tp_dealloc*/
440   0,                              /*tp_print*/
441   0,                              /*tp_getattr*/
442   0,                              /*tp_setattr*/
443   0,                              /*tp_compare*/
444   0,                              /*tp_repr*/
445   0,                              /*tp_as_number*/
446   0,                              /*tp_as_sequence*/
447   0,                              /*tp_as_mapping*/
448   0,                              /*tp_hash */
449   0,                              /*tp_call*/
450   0,                              /*tp_str*/
451   0,                              /*tp_getattro*/
452   0,                              /*tp_setattro */
453   0,                              /*tp_as_buffer*/
454   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
455   "GDB finish breakpoint object", /* tp_doc */
456   0,                              /* tp_traverse */
457   0,                              /* tp_clear */
458   0,                              /* tp_richcompare */
459   0,                              /* tp_weaklistoffset */
460   0,                              /* tp_iter */
461   0,                              /* tp_iternext */
462   0,                              /* tp_methods */
463   0,                              /* tp_members */
464   finish_breakpoint_object_getset,/* tp_getset */
465   &breakpoint_object_type,        /* tp_base */
466   0,                              /* tp_dict */
467   0,                              /* tp_descr_get */
468   0,                              /* tp_descr_set */
469   0,                              /* tp_dictoffset */
470   bpfinishpy_init,                /* tp_init */
471   0,                              /* tp_alloc */
472   0                               /* tp_new */
473 };
This page took 0.053157 seconds and 4 git commands to generate.