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