1 /* Python interface to finish breakpoints
3 Copyright (C) 2011-2017 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"
35 /* Function that is called when a Python finish bp is found out of scope. */
36 static char * const outofscope_func
= "out_of_scope";
38 /* struct implementing the gdb.FinishBreakpoint object by extending
39 the gdb.Breakpoint class. */
40 struct finish_breakpoint_object
42 /* gdb.Breakpoint base class. */
43 gdbpy_breakpoint_object py_bp
;
44 /* gdb.Type object of the value return by the breakpointed function.
45 May be NULL if no debug information was available or return type
47 PyObject
*return_type
;
48 /* gdb.Value object of the function finished by this breakpoint. Will be
49 NULL if return_type is NULL. */
50 PyObject
*function_value
;
51 /* When stopped at this FinishBreakpoint, gdb.Value object returned by
52 the function; Py_None if the value is not computable; NULL if GDB is
53 not stopped at a FinishBreakpoint. */
54 PyObject
*return_value
;
57 extern PyTypeObject finish_breakpoint_object_type
58 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
60 /* Python function to get the 'return_value' attribute of
64 bpfinishpy_get_returnvalue (PyObject
*self
, void *closure
)
66 struct finish_breakpoint_object
*self_finishbp
=
67 (struct finish_breakpoint_object
*) self
;
69 if (!self_finishbp
->return_value
)
72 Py_INCREF (self_finishbp
->return_value
);
73 return self_finishbp
->return_value
;
76 /* Deallocate FinishBreakpoint object. */
79 bpfinishpy_dealloc (PyObject
*self
)
81 struct finish_breakpoint_object
*self_bpfinish
=
82 (struct finish_breakpoint_object
*) self
;
84 Py_XDECREF (self_bpfinish
->function_value
);
85 Py_XDECREF (self_bpfinish
->return_type
);
86 Py_XDECREF (self_bpfinish
->return_value
);
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 (except
, RETURN_MASK_ALL
)
127 gdbpy_convert_exception (except
);
128 gdbpy_print_stack ();
133 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
134 of the gdb.FinishBreakpoint object BP_OBJ. */
137 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object
*bp_obj
)
142 /* Can't delete it here, but it will be removed at the next stop. */
143 disable_breakpoint (bp_obj
->bp
);
144 gdb_assert (bp_obj
->bp
->disposition
== disp_del
);
146 CATCH (except
, RETURN_MASK_ALL
)
148 gdbpy_convert_exception (except
);
149 gdbpy_print_stack ();
154 /* Python function to create a new breakpoint. */
157 bpfinishpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
159 static char *keywords
[] = { "frame", "internal", NULL
};
160 struct finish_breakpoint_object
*self_bpfinish
=
161 (struct finish_breakpoint_object
*) self
;
162 PyObject
*frame_obj
= NULL
;
164 struct frame_info
*frame
= NULL
; /* init for gcc -Wall */
165 struct frame_info
*prev_frame
= NULL
;
166 struct frame_id frame_id
;
167 PyObject
*internal
= NULL
;
170 struct symbol
*function
;
172 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "|OO", keywords
,
173 &frame_obj
, &internal
))
178 /* Default frame to newest frame if necessary. */
179 if (frame_obj
== NULL
)
180 frame
= get_current_frame ();
182 frame
= frame_object_to_frame_info (frame_obj
);
186 PyErr_SetString (PyExc_ValueError
,
187 _("Invalid ID for the `frame' object."));
191 prev_frame
= get_prev_frame (frame
);
194 PyErr_SetString (PyExc_ValueError
,
195 _("\"FinishBreakpoint\" not "
196 "meaningful in the outermost "
199 else if (get_frame_type (prev_frame
) == DUMMY_FRAME
)
201 PyErr_SetString (PyExc_ValueError
,
202 _("\"FinishBreakpoint\" cannot "
203 "be set on a dummy frame."));
207 frame_id
= get_frame_id (prev_frame
);
208 if (frame_id_eq (frame_id
, null_frame_id
))
209 PyErr_SetString (PyExc_ValueError
,
210 _("Invalid ID for the `frame' object."));
214 CATCH (except
, RETURN_MASK_ALL
)
216 gdbpy_convert_exception (except
);
221 if (PyErr_Occurred ())
224 thread
= ptid_to_global_thread_id (inferior_ptid
);
227 PyErr_SetString (PyExc_ValueError
,
228 _("No thread currently selected."));
234 internal_bp
= PyObject_IsTrue (internal
);
235 if (internal_bp
== -1)
237 PyErr_SetString (PyExc_ValueError
,
238 _("The value of `internal' must be a boolean."));
243 /* Find the function we will return from. */
244 self_bpfinish
->return_type
= NULL
;
245 self_bpfinish
->function_value
= NULL
;
249 if (get_frame_pc_if_available (frame
, &pc
))
251 function
= find_pc_function (pc
);
252 if (function
!= NULL
)
254 struct type
*ret_type
=
255 TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
257 /* Remember only non-void return types. */
258 if (TYPE_CODE (ret_type
) != TYPE_CODE_VOID
)
260 struct value
*func_value
;
262 /* Ignore Python errors at this stage. */
263 self_bpfinish
->return_type
= type_to_type_object (ret_type
);
265 func_value
= read_var_value (function
, NULL
, frame
);
266 self_bpfinish
->function_value
=
267 value_to_value_object (func_value
);
273 CATCH (except
, RETURN_MASK_ALL
)
275 /* Just swallow. Either the return type or the function value
280 if (self_bpfinish
->return_type
== NULL
|| self_bpfinish
->function_value
== NULL
)
282 /* Won't be able to compute return value. */
283 Py_XDECREF (self_bpfinish
->return_type
);
284 Py_XDECREF (self_bpfinish
->function_value
);
286 self_bpfinish
->return_type
= NULL
;
287 self_bpfinish
->function_value
= NULL
;
290 bppy_pending_object
= &self_bpfinish
->py_bp
;
291 bppy_pending_object
->number
= -1;
292 bppy_pending_object
->bp
= NULL
;
296 struct event_location
*location
;
297 struct cleanup
*back_to
;
299 /* Set a breakpoint on the return address. */
300 location
= new_address_location (get_frame_pc (prev_frame
), NULL
, 0);
301 back_to
= make_cleanup_delete_event_location (location
);
302 create_breakpoint (python_gdbarch
,
303 location
, NULL
, thread
, NULL
,
309 &bkpt_breakpoint_ops
,
310 0, 1, internal_bp
, 0);
311 do_cleanups (back_to
);
313 CATCH (except
, RETURN_MASK_ALL
)
315 GDB_PY_SET_HANDLE_EXCEPTION (except
);
319 self_bpfinish
->py_bp
.bp
->frame_id
= frame_id
;
320 self_bpfinish
->py_bp
.is_finish_bp
= 1;
322 /* Bind the breakpoint with the current program space. */
323 self_bpfinish
->py_bp
.bp
->pspace
= current_program_space
;
328 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
329 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
330 then delete the breakpoint. */
333 bpfinishpy_out_of_scope (struct finish_breakpoint_object
*bpfinish_obj
)
335 gdbpy_breakpoint_object
*bp_obj
= (gdbpy_breakpoint_object
*) bpfinish_obj
;
336 PyObject
*py_obj
= (PyObject
*) bp_obj
;
338 if (bpfinish_obj
->py_bp
.bp
->enable_state
== bp_enabled
339 && PyObject_HasAttrString (py_obj
, outofscope_func
))
341 gdbpy_ref
<> meth_result (PyObject_CallMethod (py_obj
, outofscope_func
,
343 if (meth_result
== NULL
)
344 gdbpy_print_stack ();
347 delete_breakpoint (bpfinish_obj
->py_bp
.bp
);
350 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
351 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
354 bpfinishpy_detect_out_scope_cb (struct breakpoint
*b
, void *args
)
356 struct breakpoint
*bp_stopped
= (struct breakpoint
*) args
;
357 PyObject
*py_bp
= (PyObject
*) b
->py_bp_object
;
359 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
360 not anymore in the current callstack. */
361 if (py_bp
!= NULL
&& b
->py_bp_object
->is_finish_bp
)
363 struct finish_breakpoint_object
*finish_bp
=
364 (struct finish_breakpoint_object
*) py_bp
;
366 /* Check scope if not currently stopped at the FinishBreakpoint. */
371 if (b
->pspace
== current_inferior ()->pspace
372 && (!target_has_registers
373 || frame_find_by_id (b
->frame_id
) == NULL
))
374 bpfinishpy_out_of_scope (finish_bp
);
376 CATCH (except
, RETURN_MASK_ALL
)
378 gdbpy_convert_exception (except
);
379 gdbpy_print_stack ();
388 /* Attached to `stop' notifications, check if the execution has run
389 out of the scope of any FinishBreakpoint before it has been hit. */
392 bpfinishpy_handle_stop (struct bpstats
*bs
, int print_frame
)
394 gdbpy_enter
enter_py (get_current_arch (), current_language
);
396 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
,
397 bs
== NULL
? NULL
: bs
->breakpoint_at
);
400 /* Attached to `exit' notifications, triggers all the necessary out of
401 scope notifications. */
404 bpfinishpy_handle_exit (struct inferior
*inf
)
406 gdbpy_enter
enter_py (target_gdbarch (), current_language
);
408 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
, NULL
);
411 /* Initialize the Python finish breakpoint code. */
414 gdbpy_initialize_finishbreakpoints (void)
416 if (PyType_Ready (&finish_breakpoint_object_type
) < 0)
419 if (gdb_pymodule_addobject (gdb_module
, "FinishBreakpoint",
420 (PyObject
*) &finish_breakpoint_object_type
) < 0)
423 observer_attach_normal_stop (bpfinishpy_handle_stop
);
424 observer_attach_inferior_exit (bpfinishpy_handle_exit
);
429 static PyGetSetDef finish_breakpoint_object_getset
[] = {
430 { "return_value", bpfinishpy_get_returnvalue
, NULL
,
431 "gdb.Value object representing the return value, if any. \
432 None otherwise.", NULL
},
433 { NULL
} /* Sentinel. */
436 PyTypeObject finish_breakpoint_object_type
=
438 PyVarObject_HEAD_INIT (NULL
, 0)
439 "gdb.FinishBreakpoint", /*tp_name*/
440 sizeof (struct finish_breakpoint_object
), /*tp_basicsize*/
442 bpfinishpy_dealloc
, /*tp_dealloc*/
449 0, /*tp_as_sequence*/
457 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
458 "GDB finish breakpoint object", /* tp_doc */
461 0, /* tp_richcompare */
462 0, /* tp_weaklistoffset */
467 finish_breakpoint_object_getset
,/* tp_getset */
468 &breakpoint_object_type
, /* tp_base */
470 0, /* tp_descr_get */
471 0, /* tp_descr_set */
472 0, /* tp_dictoffset */
473 bpfinishpy_init
, /* tp_init */