1 /* Python interface to finish breakpoints
3 Copyright (C) 2011-2015 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"
33 /* Function that is called when a Python finish bp is found out of scope. */
34 static char * const outofscope_func
= "out_of_scope";
36 /* struct implementing the gdb.FinishBreakpoint object by extending
37 the gdb.Breakpoint class. */
38 struct finish_breakpoint_object
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
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
;
55 extern PyTypeObject finish_breakpoint_object_type
56 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
58 /* Python function to get the 'return_value' attribute of
62 bpfinishpy_get_returnvalue (PyObject
*self
, void *closure
)
64 struct finish_breakpoint_object
*self_finishbp
=
65 (struct finish_breakpoint_object
*) self
;
67 if (!self_finishbp
->return_value
)
70 Py_INCREF (self_finishbp
->return_value
);
71 return self_finishbp
->return_value
;
74 /* Deallocate FinishBreakpoint object. */
77 bpfinishpy_dealloc (PyObject
*self
)
79 struct finish_breakpoint_object
*self_bpfinish
=
80 (struct finish_breakpoint_object
*) self
;
82 Py_XDECREF (self_bpfinish
->function_value
);
83 Py_XDECREF (self_bpfinish
->return_type
);
84 Py_XDECREF (self_bpfinish
->return_value
);
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. */
92 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object
*bp_obj
)
94 struct finish_breakpoint_object
*self_finishbp
=
95 (struct finish_breakpoint_object
*) bp_obj
;
97 /* Can compute return_value only once. */
98 gdb_assert (!self_finishbp
->return_value
);
100 if (!self_finishbp
->return_type
)
105 struct value
*function
=
106 value_object_to_value (self_finishbp
->function_value
);
107 struct type
*value_type
=
108 type_object_to_type (self_finishbp
->return_type
);
110 /* bpfinishpy_init cannot finish into DUMMY_FRAME (throws an error
111 in such case) so it is OK to always pass CTX_SAVER as NULL. */
112 struct value
*ret
= get_return_value (function
, value_type
, NULL
);
116 self_finishbp
->return_value
= value_to_value_object (ret
);
117 if (!self_finishbp
->return_value
)
118 gdbpy_print_stack ();
123 self_finishbp
->return_value
= Py_None
;
126 CATCH (except
, RETURN_MASK_ALL
)
128 gdbpy_convert_exception (except
);
129 gdbpy_print_stack ();
134 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
135 of the gdb.FinishBreakpoint object BP_OBJ. */
138 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object
*bp_obj
)
143 /* Can't delete it here, but it will be removed at the next stop. */
144 disable_breakpoint (bp_obj
->bp
);
145 gdb_assert (bp_obj
->bp
->disposition
== disp_del
);
147 CATCH (except
, RETURN_MASK_ALL
)
149 gdbpy_convert_exception (except
);
150 gdbpy_print_stack ();
155 /* Python function to create a new breakpoint. */
158 bpfinishpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
160 static char *keywords
[] = { "frame", "internal", NULL
};
161 struct finish_breakpoint_object
*self_bpfinish
=
162 (struct finish_breakpoint_object
*) self
;
163 int type
= bp_breakpoint
;
164 PyObject
*frame_obj
= NULL
;
166 struct frame_info
*frame
= NULL
; /* init for gcc -Wall */
167 struct frame_info
*prev_frame
= NULL
;
168 struct frame_id frame_id
;
169 PyObject
*internal
= NULL
;
171 CORE_ADDR finish_pc
, pc
;
172 char *addr_str
, small_buf
[100];
173 struct symbol
*function
;
175 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "|OO", keywords
,
176 &frame_obj
, &internal
))
181 /* Default frame to newest frame if necessary. */
182 if (frame_obj
== NULL
)
183 frame
= get_current_frame ();
185 frame
= frame_object_to_frame_info (frame_obj
);
189 PyErr_SetString (PyExc_ValueError
,
190 _("Invalid ID for the `frame' object."));
194 prev_frame
= get_prev_frame (frame
);
197 PyErr_SetString (PyExc_ValueError
,
198 _("\"FinishBreakpoint\" not "
199 "meaningful in the outermost "
202 else if (get_frame_type (prev_frame
) == DUMMY_FRAME
)
204 PyErr_SetString (PyExc_ValueError
,
205 _("\"FinishBreakpoint\" cannot "
206 "be set on a dummy frame."));
210 frame_id
= get_frame_id (prev_frame
);
211 if (frame_id_eq (frame_id
, null_frame_id
))
212 PyErr_SetString (PyExc_ValueError
,
213 _("Invalid ID for the `frame' object."));
217 CATCH (except
, RETURN_MASK_ALL
)
219 gdbpy_convert_exception (except
);
224 if (PyErr_Occurred ())
227 thread
= pid_to_thread_id (inferior_ptid
);
230 PyErr_SetString (PyExc_ValueError
,
231 _("No thread currently selected."));
237 internal_bp
= PyObject_IsTrue (internal
);
238 if (internal_bp
== -1)
240 PyErr_SetString (PyExc_ValueError
,
241 _("The value of `internal' must be a boolean."));
246 /* Find the function we will return from. */
247 self_bpfinish
->return_type
= NULL
;
248 self_bpfinish
->function_value
= NULL
;
252 if (get_frame_pc_if_available (frame
, &pc
))
254 function
= find_pc_function (pc
);
255 if (function
!= NULL
)
257 struct type
*ret_type
=
258 TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
260 /* Remember only non-void return types. */
261 if (TYPE_CODE (ret_type
) != TYPE_CODE_VOID
)
263 struct value
*func_value
;
265 /* Ignore Python errors at this stage. */
266 self_bpfinish
->return_type
= type_to_type_object (ret_type
);
268 func_value
= read_var_value (function
, frame
);
269 self_bpfinish
->function_value
=
270 value_to_value_object (func_value
);
276 CATCH (except
, RETURN_MASK_ALL
)
278 /* Just swallow. Either the return type or the function value
283 if (self_bpfinish
->return_type
== NULL
|| self_bpfinish
->function_value
== NULL
)
285 /* Won't be able to compute return value. */
286 Py_XDECREF (self_bpfinish
->return_type
);
287 Py_XDECREF (self_bpfinish
->function_value
);
289 self_bpfinish
->return_type
= NULL
;
290 self_bpfinish
->function_value
= NULL
;
293 bppy_pending_object
= &self_bpfinish
->py_bp
;
294 bppy_pending_object
->number
= -1;
295 bppy_pending_object
->bp
= NULL
;
299 /* Set a breakpoint on the return address. */
300 finish_pc
= get_frame_pc (prev_frame
);
301 xsnprintf (small_buf
, sizeof (small_buf
), "*%s", hex_string (finish_pc
));
302 addr_str
= small_buf
;
304 create_breakpoint (python_gdbarch
,
305 addr_str
, NULL
, thread
, NULL
,
311 &bkpt_breakpoint_ops
,
312 0, 1, internal_bp
, 0);
314 CATCH (except
, RETURN_MASK_ALL
)
316 GDB_PY_SET_HANDLE_EXCEPTION (except
);
320 self_bpfinish
->py_bp
.bp
->frame_id
= frame_id
;
321 self_bpfinish
->py_bp
.is_finish_bp
= 1;
323 /* Bind the breakpoint with the current program space. */
324 self_bpfinish
->py_bp
.bp
->pspace
= current_program_space
;
329 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
330 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
331 then delete the breakpoint. */
334 bpfinishpy_out_of_scope (struct finish_breakpoint_object
*bpfinish_obj
)
336 gdbpy_breakpoint_object
*bp_obj
= (gdbpy_breakpoint_object
*) bpfinish_obj
;
337 PyObject
*py_obj
= (PyObject
*) bp_obj
;
339 if (bpfinish_obj
->py_bp
.bp
->enable_state
== bp_enabled
340 && PyObject_HasAttrString (py_obj
, outofscope_func
))
342 PyObject
*meth_result
;
344 meth_result
= PyObject_CallMethod (py_obj
, outofscope_func
, NULL
);
345 if (meth_result
== NULL
)
346 gdbpy_print_stack ();
347 Py_XDECREF (meth_result
);
350 delete_breakpoint (bpfinish_obj
->py_bp
.bp
);
353 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
354 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
357 bpfinishpy_detect_out_scope_cb (struct breakpoint
*b
, void *args
)
359 struct breakpoint
*bp_stopped
= (struct breakpoint
*) args
;
360 PyObject
*py_bp
= (PyObject
*) b
->py_bp_object
;
361 struct gdbarch
*garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
363 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
364 not anymore in the current callstack. */
365 if (py_bp
!= NULL
&& b
->py_bp_object
->is_finish_bp
)
367 struct finish_breakpoint_object
*finish_bp
=
368 (struct finish_breakpoint_object
*) py_bp
;
370 /* Check scope if not currently stopped at the FinishBreakpoint. */
375 if (b
->pspace
== current_inferior ()->pspace
376 && (!target_has_registers
377 || frame_find_by_id (b
->frame_id
) == NULL
))
378 bpfinishpy_out_of_scope (finish_bp
);
380 CATCH (except
, RETURN_MASK_ALL
)
382 gdbpy_convert_exception (except
);
383 gdbpy_print_stack ();
392 /* Attached to `stop' notifications, check if the execution has run
393 out of the scope of any FinishBreakpoint before it has been hit. */
396 bpfinishpy_handle_stop (struct bpstats
*bs
, int print_frame
)
398 struct cleanup
*cleanup
= ensure_python_env (get_current_arch (),
401 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
,
402 bs
== NULL
? NULL
: bs
->breakpoint_at
);
404 do_cleanups (cleanup
);
407 /* Attached to `exit' notifications, triggers all the necessary out of
408 scope notifications. */
411 bpfinishpy_handle_exit (struct inferior
*inf
)
413 struct cleanup
*cleanup
= ensure_python_env (target_gdbarch (),
416 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
, NULL
);
418 do_cleanups (cleanup
);
421 /* Initialize the Python finish breakpoint code. */
424 gdbpy_initialize_finishbreakpoints (void)
426 if (PyType_Ready (&finish_breakpoint_object_type
) < 0)
429 if (gdb_pymodule_addobject (gdb_module
, "FinishBreakpoint",
430 (PyObject
*) &finish_breakpoint_object_type
) < 0)
433 observer_attach_normal_stop (bpfinishpy_handle_stop
);
434 observer_attach_inferior_exit (bpfinishpy_handle_exit
);
439 static PyGetSetDef finish_breakpoint_object_getset
[] = {
440 { "return_value", bpfinishpy_get_returnvalue
, NULL
,
441 "gdb.Value object representing the return value, if any. \
442 None otherwise.", NULL
},
443 { NULL
} /* Sentinel. */
446 PyTypeObject finish_breakpoint_object_type
=
448 PyVarObject_HEAD_INIT (NULL
, 0)
449 "gdb.FinishBreakpoint", /*tp_name*/
450 sizeof (struct finish_breakpoint_object
), /*tp_basicsize*/
452 bpfinishpy_dealloc
, /*tp_dealloc*/
459 0, /*tp_as_sequence*/
467 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
468 "GDB finish breakpoint object", /* tp_doc */
471 0, /* tp_richcompare */
472 0, /* tp_weaklistoffset */
477 finish_breakpoint_object_getset
,/* tp_getset */
478 &breakpoint_object_type
, /* tp_base */
480 0, /* tp_descr_get */
481 0, /* tp_descr_set */
482 0, /* tp_dictoffset */
483 bpfinishpy_init
, /* tp_init */