1 /* Python interface to finish breakpoints
3 Copyright (C) 2011-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "python-internal.h"
24 #include "breakpoint.h"
26 #include "gdbthread.h"
27 #include "arch-utils.h"
29 #include "observable.h"
34 /* Function that is called when a Python finish bp is found out of scope. */
35 static const char outofscope_func
[] = "out_of_scope";
37 /* struct implementing the gdb.FinishBreakpoint object by extending
38 the gdb.Breakpoint class. */
39 struct finish_breakpoint_object
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
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
;
56 extern PyTypeObject finish_breakpoint_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
59 /* Python function to get the 'return_value' attribute of
63 bpfinishpy_get_returnvalue (PyObject
*self
, void *closure
)
65 struct finish_breakpoint_object
*self_finishbp
=
66 (struct finish_breakpoint_object
*) self
;
68 if (!self_finishbp
->return_value
)
71 Py_INCREF (self_finishbp
->return_value
);
72 return self_finishbp
->return_value
;
75 /* Deallocate FinishBreakpoint object. */
78 bpfinishpy_dealloc (PyObject
*self
)
80 struct finish_breakpoint_object
*self_bpfinish
=
81 (struct finish_breakpoint_object
*) self
;
83 Py_XDECREF (self_bpfinish
->function_value
);
84 Py_XDECREF (self_bpfinish
->return_type
);
85 Py_XDECREF (self_bpfinish
->return_value
);
86 Py_TYPE (self
)->tp_free (self
);
89 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
90 of the gdb.FinishBreakpoint object BP_OBJ. Will compute and cache the
91 `return_value', if possible. */
94 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object
*bp_obj
)
96 struct finish_breakpoint_object
*self_finishbp
=
97 (struct finish_breakpoint_object
*) bp_obj
;
99 /* Can compute return_value only once. */
100 gdb_assert (!self_finishbp
->return_value
);
102 if (!self_finishbp
->return_type
)
107 struct value
*function
=
108 value_object_to_value (self_finishbp
->function_value
);
109 struct type
*value_type
=
110 type_object_to_type (self_finishbp
->return_type
);
111 struct value
*ret
= get_return_value (function
, value_type
);
115 self_finishbp
->return_value
= value_to_value_object (ret
);
116 if (!self_finishbp
->return_value
)
117 gdbpy_print_stack ();
122 self_finishbp
->return_value
= Py_None
;
125 catch (const gdb_exception
&except
)
127 gdbpy_convert_exception (except
);
128 gdbpy_print_stack ();
132 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
133 of the gdb.FinishBreakpoint object BP_OBJ. */
136 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object
*bp_obj
)
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
);
145 catch (const gdb_exception
&except
)
147 gdbpy_convert_exception (except
);
148 gdbpy_print_stack ();
152 /* Python function to create a new breakpoint. */
155 bpfinishpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
157 static const char *keywords
[] = { "frame", "internal", NULL
};
158 struct finish_breakpoint_object
*self_bpfinish
=
159 (struct finish_breakpoint_object
*) self
;
160 PyObject
*frame_obj
= NULL
;
162 struct frame_info
*frame
= NULL
; /* init for gcc -Wall */
163 struct frame_info
*prev_frame
= NULL
;
164 struct frame_id frame_id
;
165 PyObject
*internal
= NULL
;
168 struct symbol
*function
;
170 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "|OO", keywords
,
171 &frame_obj
, &internal
))
176 /* Default frame to newest frame if necessary. */
177 if (frame_obj
== NULL
)
178 frame
= get_current_frame ();
180 frame
= frame_object_to_frame_info (frame_obj
);
184 PyErr_SetString (PyExc_ValueError
,
185 _("Invalid ID for the `frame' object."));
189 prev_frame
= get_prev_frame (frame
);
192 PyErr_SetString (PyExc_ValueError
,
193 _("\"FinishBreakpoint\" not "
194 "meaningful in the outermost "
197 else if (get_frame_type (prev_frame
) == DUMMY_FRAME
)
199 PyErr_SetString (PyExc_ValueError
,
200 _("\"FinishBreakpoint\" cannot "
201 "be set on a dummy frame."));
205 frame_id
= get_frame_id (prev_frame
);
206 if (frame_id_eq (frame_id
, null_frame_id
))
207 PyErr_SetString (PyExc_ValueError
,
208 _("Invalid ID for the `frame' object."));
212 catch (const gdb_exception
&except
)
214 gdbpy_convert_exception (except
);
218 if (PyErr_Occurred ())
221 if (inferior_ptid
== null_ptid
)
223 PyErr_SetString (PyExc_ValueError
,
224 _("No thread currently selected."));
228 thread
= inferior_thread ()->global_num
;
232 internal_bp
= PyObject_IsTrue (internal
);
233 if (internal_bp
== -1)
235 PyErr_SetString (PyExc_ValueError
,
236 _("The value of `internal' must be a boolean."));
241 /* Find the function we will return from. */
242 self_bpfinish
->return_type
= NULL
;
243 self_bpfinish
->function_value
= NULL
;
247 if (get_frame_pc_if_available (frame
, &pc
))
249 function
= find_pc_function (pc
);
250 if (function
!= NULL
)
252 struct type
*ret_type
=
253 check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (function
)));
255 /* Remember only non-void return types. */
256 if (TYPE_CODE (ret_type
) != TYPE_CODE_VOID
)
258 struct value
*func_value
;
260 /* Ignore Python errors at this stage. */
261 self_bpfinish
->return_type
= type_to_type_object (ret_type
);
263 func_value
= read_var_value (function
, NULL
, frame
);
264 self_bpfinish
->function_value
=
265 value_to_value_object (func_value
);
271 catch (const gdb_exception
&except
)
273 /* Just swallow. Either the return type or the function value
277 if (self_bpfinish
->return_type
== NULL
|| self_bpfinish
->function_value
== NULL
)
279 /* Won't be able to compute return value. */
280 Py_XDECREF (self_bpfinish
->return_type
);
281 Py_XDECREF (self_bpfinish
->function_value
);
283 self_bpfinish
->return_type
= NULL
;
284 self_bpfinish
->function_value
= NULL
;
287 bppy_pending_object
= &self_bpfinish
->py_bp
;
288 bppy_pending_object
->number
= -1;
289 bppy_pending_object
->bp
= NULL
;
293 /* Set a breakpoint on the return address. */
294 event_location_up location
295 = new_address_location (get_frame_pc (prev_frame
), NULL
, 0);
296 create_breakpoint (python_gdbarch
,
297 location
.get (), NULL
, thread
, NULL
,
303 &bkpt_breakpoint_ops
,
304 0, 1, internal_bp
, 0);
306 catch (const gdb_exception
&except
)
308 GDB_PY_SET_HANDLE_EXCEPTION (except
);
311 self_bpfinish
->py_bp
.bp
->frame_id
= frame_id
;
312 self_bpfinish
->py_bp
.is_finish_bp
= 1;
314 /* Bind the breakpoint with the current program space. */
315 self_bpfinish
->py_bp
.bp
->pspace
= current_program_space
;
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. */
325 bpfinishpy_out_of_scope (struct finish_breakpoint_object
*bpfinish_obj
)
327 gdbpy_breakpoint_object
*bp_obj
= (gdbpy_breakpoint_object
*) bpfinish_obj
;
328 PyObject
*py_obj
= (PyObject
*) bp_obj
;
330 if (bpfinish_obj
->py_bp
.bp
->enable_state
== bp_enabled
331 && PyObject_HasAttrString (py_obj
, outofscope_func
))
333 gdbpy_ref
<> meth_result (PyObject_CallMethod (py_obj
, outofscope_func
,
335 if (meth_result
== NULL
)
336 gdbpy_print_stack ();
339 delete_breakpoint (bpfinish_obj
->py_bp
.bp
);
342 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
343 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
346 bpfinishpy_detect_out_scope_cb (struct breakpoint
*b
,
347 struct breakpoint
*bp_stopped
)
349 PyObject
*py_bp
= (PyObject
*) b
->py_bp_object
;
351 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
352 not anymore in the current callstack. */
353 if (py_bp
!= NULL
&& b
->py_bp_object
->is_finish_bp
)
355 struct finish_breakpoint_object
*finish_bp
=
356 (struct finish_breakpoint_object
*) py_bp
;
358 /* Check scope if not currently stopped at the FinishBreakpoint. */
363 if (b
->pspace
== current_inferior ()->pspace
364 && (!target_has_registers
365 || frame_find_by_id (b
->frame_id
) == NULL
))
366 bpfinishpy_out_of_scope (finish_bp
);
368 catch (const gdb_exception
&except
)
370 gdbpy_convert_exception (except
);
371 gdbpy_print_stack ();
379 /* Attached to `stop' notifications, check if the execution has run
380 out of the scope of any FinishBreakpoint before it has been hit. */
383 bpfinishpy_handle_stop (struct bpstats
*bs
, int print_frame
)
385 gdbpy_enter
enter_py (get_current_arch (), current_language
);
387 iterate_over_breakpoints ([&] (breakpoint
*bp
)
389 return bpfinishpy_detect_out_scope_cb
390 (bp
, bs
== NULL
? NULL
: bs
->breakpoint_at
);
394 /* Attached to `exit' notifications, triggers all the necessary out of
395 scope notifications. */
398 bpfinishpy_handle_exit (struct inferior
*inf
)
400 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
402 iterate_over_breakpoints ([&] (breakpoint
*bp
)
404 return bpfinishpy_detect_out_scope_cb (bp
, nullptr);
408 /* Initialize the Python finish breakpoint code. */
411 gdbpy_initialize_finishbreakpoints (void)
413 if (PyType_Ready (&finish_breakpoint_object_type
) < 0)
416 if (gdb_pymodule_addobject (gdb_module
, "FinishBreakpoint",
417 (PyObject
*) &finish_breakpoint_object_type
) < 0)
420 gdb::observers::normal_stop
.attach (bpfinishpy_handle_stop
);
421 gdb::observers::inferior_exit
.attach (bpfinishpy_handle_exit
);
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. */
433 PyTypeObject finish_breakpoint_object_type
=
435 PyVarObject_HEAD_INIT (NULL
, 0)
436 "gdb.FinishBreakpoint", /*tp_name*/
437 sizeof (struct finish_breakpoint_object
), /*tp_basicsize*/
439 bpfinishpy_dealloc
, /*tp_dealloc*/
446 0, /*tp_as_sequence*/
454 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
455 "GDB finish breakpoint object", /* tp_doc */
458 0, /* tp_richcompare */
459 0, /* tp_weaklistoffset */
464 finish_breakpoint_object_getset
,/* tp_getset */
465 &breakpoint_object_type
, /* tp_base */
467 0, /* tp_descr_get */
468 0, /* tp_descr_set */
469 0, /* tp_dictoffset */
470 bpfinishpy_init
, /* tp_init */