1 /* Python interface to finish breakpoints
3 Copyright (C) 2011-2012 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 "exceptions.h"
24 #include "python-internal.h"
25 #include "breakpoint.h"
27 #include "gdbthread.h"
28 #include "arch-utils.h"
33 static PyTypeObject finish_breakpoint_object_type
;
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 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.Type object of the function finished by this breakpoint. Will be
49 NULL if return_type is NULL. */
50 PyObject
*function_type
;
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 /* Python function to get the 'return_value' attribute of
61 bpfinishpy_get_returnvalue (PyObject
*self
, void *closure
)
63 struct finish_breakpoint_object
*self_finishbp
=
64 (struct finish_breakpoint_object
*) self
;
66 if (!self_finishbp
->return_value
)
69 Py_INCREF (self_finishbp
->return_value
);
70 return self_finishbp
->return_value
;
73 /* Deallocate FinishBreakpoint object. */
76 bpfinishpy_dealloc (PyObject
*self
)
78 struct finish_breakpoint_object
*self_bpfinish
=
79 (struct finish_breakpoint_object
*) self
;
81 Py_XDECREF (self_bpfinish
->function_type
);
82 Py_XDECREF (self_bpfinish
->return_type
);
83 Py_XDECREF (self_bpfinish
->return_value
);
86 /* Triggered when gdbpy_should_stop is about to execute the `stop' callback
87 of the gdb.FinishBreakpoint object BP_OBJ. Will compute and cache the
88 `return_value', if possible. */
91 bpfinishpy_pre_stop_hook (struct breakpoint_object
*bp_obj
)
93 struct finish_breakpoint_object
*self_finishbp
=
94 (struct finish_breakpoint_object
*) bp_obj
;
95 volatile struct gdb_exception except
;
97 /* Can compute return_value only once. */
98 gdb_assert (!self_finishbp
->return_value
);
100 if (!self_finishbp
->return_type
)
103 TRY_CATCH (except
, RETURN_MASK_ALL
)
106 get_return_value (type_object_to_type (self_finishbp
->function_type
),
107 type_object_to_type (self_finishbp
->return_type
));
111 self_finishbp
->return_value
= value_to_value_object (ret
);
112 if (!self_finishbp
->return_value
)
113 gdbpy_print_stack ();
118 self_finishbp
->return_value
= Py_None
;
121 if (except
.reason
< 0)
123 gdbpy_convert_exception (except
);
124 gdbpy_print_stack ();
128 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
129 of the gdb.FinishBreakpoint object BP_OBJ. */
132 bpfinishpy_post_stop_hook (struct breakpoint_object
*bp_obj
)
134 volatile struct gdb_exception except
;
136 TRY_CATCH (except
, RETURN_MASK_ALL
)
138 /* Can't delete it here, but it will be removed at the next stop. */
139 disable_breakpoint (bp_obj
->bp
);
140 gdb_assert (bp_obj
->bp
->disposition
== disp_del
);
142 if (except
.reason
< 0)
144 gdbpy_convert_exception (except
);
145 gdbpy_print_stack ();
149 /* Python function to create a new breakpoint. */
152 bpfinishpy_init (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
154 static char *keywords
[] = { "frame", "internal", NULL
};
155 struct finish_breakpoint_object
*self_bpfinish
=
156 (struct finish_breakpoint_object
*) self
;
157 int type
= bp_breakpoint
;
158 PyObject
*frame_obj
= NULL
;
160 struct frame_info
*frame
, *prev_frame
= NULL
;
161 struct frame_id frame_id
;
162 PyObject
*internal
= NULL
;
164 CORE_ADDR finish_pc
, pc
;
165 volatile struct gdb_exception except
;
166 char *addr_str
, small_buf
[100];
167 struct symbol
*function
;
169 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "|OO", keywords
,
170 &frame_obj
, &internal
))
173 /* Default frame to gdb.newest_frame if necessary. */
175 frame_obj
= gdbpy_newest_frame (NULL
, NULL
);
177 Py_INCREF (frame_obj
);
179 frame
= frame_object_to_frame_info (frame_obj
);
180 Py_DECREF (frame_obj
);
185 TRY_CATCH (except
, RETURN_MASK_ALL
)
187 prev_frame
= get_prev_frame (frame
);
190 PyErr_SetString (PyExc_ValueError
, _("\"FinishBreakpoint\" not " \
191 "meaningful in the outermost "\
194 else if (get_frame_type (prev_frame
) == DUMMY_FRAME
)
196 PyErr_SetString (PyExc_ValueError
, _("\"FinishBreakpoint\" cannot "\
197 "be set on a dummy frame."));
201 frame_id
= get_frame_id (prev_frame
);
202 if (frame_id_eq (frame_id
, null_frame_id
))
203 PyErr_SetString (PyExc_ValueError
,
204 _("Invalid ID for the `frame' object."));
207 if (except
.reason
< 0)
209 gdbpy_convert_exception (except
);
212 else if (PyErr_Occurred ())
215 thread
= pid_to_thread_id (inferior_ptid
);
218 PyErr_SetString (PyExc_ValueError
,
219 _("No thread currently selected."));
225 internal_bp
= PyObject_IsTrue (internal
);
226 if (internal_bp
== -1)
228 PyErr_SetString (PyExc_ValueError
,
229 _("The value of `internal' must be a boolean."));
234 /* Find the function we will return from. */
235 self_bpfinish
->return_type
= NULL
;
236 self_bpfinish
->function_type
= NULL
;
238 TRY_CATCH (except
, RETURN_MASK_ALL
)
240 if (get_frame_pc_if_available (frame
, &pc
))
242 function
= find_pc_function (pc
);
243 if (function
!= NULL
)
245 struct type
*ret_type
=
246 TYPE_TARGET_TYPE (SYMBOL_TYPE (function
));
248 /* Remember only non-void return types. */
249 if (TYPE_CODE (ret_type
) != TYPE_CODE_VOID
)
251 /* Ignore Python errors at this stage. */
252 self_bpfinish
->return_type
= type_to_type_object (ret_type
);
254 self_bpfinish
->function_type
=
255 type_to_type_object (SYMBOL_TYPE (function
));
261 if (except
.reason
< 0
262 || !self_bpfinish
->return_type
|| !self_bpfinish
->function_type
)
264 /* Won't be able to compute return value. */
265 Py_XDECREF (self_bpfinish
->return_type
);
266 Py_XDECREF (self_bpfinish
->function_type
);
268 self_bpfinish
->return_type
= NULL
;
269 self_bpfinish
->function_type
= NULL
;
272 bppy_pending_object
= &self_bpfinish
->py_bp
;
273 bppy_pending_object
->number
= -1;
274 bppy_pending_object
->bp
= NULL
;
276 TRY_CATCH (except
, RETURN_MASK_ALL
)
278 /* Set a breakpoint on the return address. */
279 finish_pc
= get_frame_pc (prev_frame
);
280 xsnprintf (small_buf
, sizeof (small_buf
), "*%s", hex_string (finish_pc
));
281 addr_str
= small_buf
;
283 create_breakpoint (python_gdbarch
,
284 addr_str
, NULL
, thread
,
290 &bkpt_breakpoint_ops
,
293 GDB_PY_SET_HANDLE_EXCEPTION (except
);
295 self_bpfinish
->py_bp
.bp
->frame_id
= frame_id
;
296 self_bpfinish
->py_bp
.is_finish_bp
= 1;
298 /* Bind the breakpoint with the current program space. */
299 self_bpfinish
->py_bp
.bp
->pspace
= current_program_space
;
304 PyErr_SetString (PyExc_ValueError
,
305 _("Invalid ID for the `frame' object."));
309 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
310 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
311 then delete the breakpoint. */
314 bpfinishpy_out_of_scope (struct finish_breakpoint_object
*bpfinish_obj
)
316 volatile struct gdb_exception except
;
317 breakpoint_object
*bp_obj
= (breakpoint_object
*) bpfinish_obj
;
318 PyObject
*py_obj
= (PyObject
*) bp_obj
;
320 if (bpfinish_obj
->py_bp
.bp
->enable_state
== bp_enabled
321 && PyObject_HasAttrString (py_obj
, outofscope_func
))
323 if (!PyObject_CallMethod (py_obj
, outofscope_func
, NULL
))
324 gdbpy_print_stack ();
327 delete_breakpoint (bpfinish_obj
->py_bp
.bp
);
330 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
331 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
334 bpfinishpy_detect_out_scope_cb (struct breakpoint
*b
, void *args
)
336 volatile struct gdb_exception except
;
337 struct breakpoint
*bp_stopped
= (struct breakpoint
*) args
;
338 PyObject
*py_bp
= (PyObject
*) b
->py_bp_object
;
339 struct gdbarch
*garch
= b
->gdbarch
? b
->gdbarch
: get_current_arch ();
341 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
342 not anymore in the current callstack. */
343 if (py_bp
!= NULL
&& b
->py_bp_object
->is_finish_bp
)
345 struct finish_breakpoint_object
*finish_bp
=
346 (struct finish_breakpoint_object
*) py_bp
;
348 /* Check scope if not currently stopped at the FinishBreakpoint. */
351 TRY_CATCH (except
, RETURN_MASK_ALL
)
353 if (b
->pspace
== current_inferior ()->pspace
354 && (!target_has_registers
355 || frame_find_by_id (b
->frame_id
) == NULL
))
356 bpfinishpy_out_of_scope (finish_bp
);
358 if (except
.reason
< 0)
360 gdbpy_convert_exception (except
);
361 gdbpy_print_stack ();
369 /* Attached to `stop' notifications, check if the execution has run
370 out of the scope of any FinishBreakpoint before it has been hit. */
373 bpfinishpy_handle_stop (struct bpstats
*bs
, int print_frame
)
375 struct cleanup
*cleanup
= ensure_python_env (get_current_arch (),
378 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
,
379 bs
== NULL
? NULL
: bs
->breakpoint_at
);
381 do_cleanups (cleanup
);
384 /* Attached to `exit' notifications, triggers all the necessary out of
385 scope notifications. */
388 bpfinishpy_handle_exit (struct inferior
*inf
)
390 struct cleanup
*cleanup
= ensure_python_env (target_gdbarch
,
393 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb
, NULL
);
395 do_cleanups (cleanup
);
398 /* Initialize the Python finish breakpoint code. */
401 gdbpy_initialize_finishbreakpoints (void)
403 if (PyType_Ready (&finish_breakpoint_object_type
) < 0)
406 Py_INCREF (&finish_breakpoint_object_type
);
407 PyModule_AddObject (gdb_module
, "FinishBreakpoint",
408 (PyObject
*) &finish_breakpoint_object_type
);
410 observer_attach_normal_stop (bpfinishpy_handle_stop
);
411 observer_attach_inferior_exit (bpfinishpy_handle_exit
);
414 static PyGetSetDef finish_breakpoint_object_getset
[] = {
415 { "return_value", bpfinishpy_get_returnvalue
, NULL
,
416 "gdb.Value object representing the return value, if any. \
417 None otherwise.", NULL
},
418 { NULL
} /* Sentinel. */
421 static PyTypeObject finish_breakpoint_object_type
=
423 PyObject_HEAD_INIT (NULL
)
425 "gdb.FinishBreakpoint", /*tp_name*/
426 sizeof (struct finish_breakpoint_object
), /*tp_basicsize*/
428 bpfinishpy_dealloc
, /*tp_dealloc*/
435 0, /*tp_as_sequence*/
443 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
444 "GDB finish breakpoint object", /* tp_doc */
447 0, /* tp_richcompare */
448 0, /* tp_weaklistoffset */
453 finish_breakpoint_object_getset
,/* tp_getset */
454 &breakpoint_object_type
, /* tp_base */
456 0, /* tp_descr_get */
457 0, /* tp_descr_set */
458 0, /* tp_dictoffset */
459 bpfinishpy_init
, /* tp_init */