]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-finishbreakpoint.c
7b2d319039e3815d3f9e2a0a1aeca60190409024
[thirdparty/binutils-gdb.git] / gdb / python / py-finishbreakpoint.c
1 /* Python interface to finish breakpoints
2
3 Copyright (C) 2011-2012 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 "defs.h"
23 #include "exceptions.h"
24 #include "python-internal.h"
25 #include "breakpoint.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "arch-utils.h"
29 #include "language.h"
30 #include "observer.h"
31 #include "inferior.h"
32 #include "block.h"
33
34 static PyTypeObject finish_breakpoint_object_type;
35
36 /* Function that is called when a Python finish bp is found out of scope. */
37 static char * const outofscope_func = "out_of_scope";
38
39 /* struct implementing the gdb.FinishBreakpoint object by extending
40 the gdb.Breakpoint class. */
41 struct finish_breakpoint_object
42 {
43 /* gdb.Breakpoint base class. */
44 breakpoint_object py_bp;
45 /* gdb.Type object of the value return by the breakpointed function.
46 May be NULL if no debug information was available or return type
47 was VOID. */
48 PyObject *return_type;
49 /* gdb.Value object of the function finished by this breakpoint. Will be
50 NULL if return_type is NULL. */
51 PyObject *function_value;
52 /* When stopped at this FinishBreakpoint, gdb.Value object returned by
53 the function; Py_None if the value is not computable; NULL if GDB is
54 not stopped at a FinishBreakpoint. */
55 PyObject *return_value;
56 };
57
58 /* Python function to get the 'return_value' attribute of
59 FinishBreakpoint. */
60
61 static PyObject *
62 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
63 {
64 struct finish_breakpoint_object *self_finishbp =
65 (struct finish_breakpoint_object *) self;
66
67 if (!self_finishbp->return_value)
68 Py_RETURN_NONE;
69
70 Py_INCREF (self_finishbp->return_value);
71 return self_finishbp->return_value;
72 }
73
74 /* Deallocate FinishBreakpoint object. */
75
76 static void
77 bpfinishpy_dealloc (PyObject *self)
78 {
79 struct finish_breakpoint_object *self_bpfinish =
80 (struct finish_breakpoint_object *) self;
81
82 Py_XDECREF (self_bpfinish->function_value);
83 Py_XDECREF (self_bpfinish->return_type);
84 Py_XDECREF (self_bpfinish->return_value);
85 }
86
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. */
90
91 void
92 bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj)
93 {
94 struct finish_breakpoint_object *self_finishbp =
95 (struct finish_breakpoint_object *) bp_obj;
96 volatile struct gdb_exception except;
97
98 /* Can compute return_value only once. */
99 gdb_assert (!self_finishbp->return_value);
100
101 if (!self_finishbp->return_type)
102 return;
103
104 TRY_CATCH (except, RETURN_MASK_ALL)
105 {
106 struct value *function =
107 value_object_to_value (self_finishbp->function_value);
108 struct type *value_type =
109 type_object_to_type (self_finishbp->return_type);
110 struct value *ret = get_return_value (function, value_type);
111
112 if (ret)
113 {
114 self_finishbp->return_value = value_to_value_object (ret);
115 if (!self_finishbp->return_value)
116 gdbpy_print_stack ();
117 }
118 else
119 {
120 Py_INCREF (Py_None);
121 self_finishbp->return_value = Py_None;
122 }
123 }
124 if (except.reason < 0)
125 {
126 gdbpy_convert_exception (except);
127 gdbpy_print_stack ();
128 }
129 }
130
131 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
132 of the gdb.FinishBreakpoint object BP_OBJ. */
133
134 void
135 bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj)
136 {
137 volatile struct gdb_exception except;
138
139 TRY_CATCH (except, RETURN_MASK_ALL)
140 {
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);
144 }
145 if (except.reason < 0)
146 {
147 gdbpy_convert_exception (except);
148 gdbpy_print_stack ();
149 }
150 }
151
152 /* Python function to create a new breakpoint. */
153
154 static int
155 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
156 {
157 static char *keywords[] = { "frame", "internal", NULL };
158 struct finish_breakpoint_object *self_bpfinish =
159 (struct finish_breakpoint_object *) self;
160 int type = bp_breakpoint;
161 PyObject *frame_obj = NULL;
162 int thread;
163 struct frame_info *frame, *prev_frame = NULL;
164 struct frame_id frame_id;
165 PyObject *internal = NULL;
166 int internal_bp = 0;
167 CORE_ADDR finish_pc, pc;
168 volatile struct gdb_exception except;
169 char *addr_str, small_buf[100];
170 struct symbol *function;
171
172 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
173 &frame_obj, &internal))
174 return -1;
175
176 /* Default frame to gdb.newest_frame if necessary. */
177 if (!frame_obj)
178 frame_obj = gdbpy_newest_frame (NULL, NULL);
179 else
180 Py_INCREF (frame_obj);
181
182 frame = frame_object_to_frame_info (frame_obj);
183 Py_DECREF (frame_obj);
184
185 if (frame == NULL)
186 goto invalid_frame;
187
188 TRY_CATCH (except, RETURN_MASK_ALL)
189 {
190 prev_frame = get_prev_frame (frame);
191 if (prev_frame == 0)
192 {
193 PyErr_SetString (PyExc_ValueError, _("\"FinishBreakpoint\" not " \
194 "meaningful in the outermost "\
195 "frame."));
196 }
197 else if (get_frame_type (prev_frame) == DUMMY_FRAME)
198 {
199 PyErr_SetString (PyExc_ValueError, _("\"FinishBreakpoint\" cannot "\
200 "be set on a dummy frame."));
201 }
202 else
203 {
204 frame_id = get_frame_id (prev_frame);
205 if (frame_id_eq (frame_id, null_frame_id))
206 PyErr_SetString (PyExc_ValueError,
207 _("Invalid ID for the `frame' object."));
208 }
209 }
210 if (except.reason < 0)
211 {
212 gdbpy_convert_exception (except);
213 return -1;
214 }
215 else if (PyErr_Occurred ())
216 return -1;
217
218 thread = pid_to_thread_id (inferior_ptid);
219 if (thread == 0)
220 {
221 PyErr_SetString (PyExc_ValueError,
222 _("No thread currently selected."));
223 return -1;
224 }
225
226 if (internal)
227 {
228 internal_bp = PyObject_IsTrue (internal);
229 if (internal_bp == -1)
230 {
231 PyErr_SetString (PyExc_ValueError,
232 _("The value of `internal' must be a boolean."));
233 return -1;
234 }
235 }
236
237 /* Find the function we will return from. */
238 self_bpfinish->return_type = NULL;
239 self_bpfinish->function_value = NULL;
240
241 TRY_CATCH (except, RETURN_MASK_ALL)
242 {
243 if (get_frame_pc_if_available (frame, &pc))
244 {
245 function = find_pc_function (pc);
246 if (function != NULL)
247 {
248 struct type *ret_type =
249 TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
250
251 /* Remember only non-void return types. */
252 if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
253 {
254 struct value *func_value;
255
256 /* Ignore Python errors at this stage. */
257 self_bpfinish->return_type = type_to_type_object (ret_type);
258 PyErr_Clear ();
259 func_value = read_var_value (function, frame);
260 self_bpfinish->function_value =
261 value_to_value_object (func_value);
262 PyErr_Clear ();
263 }
264 }
265 }
266 }
267 if (except.reason < 0
268 || !self_bpfinish->return_type || !self_bpfinish->function_value)
269 {
270 /* Won't be able to compute return value. */
271 Py_XDECREF (self_bpfinish->return_type);
272 Py_XDECREF (self_bpfinish->function_value);
273
274 self_bpfinish->return_type = NULL;
275 self_bpfinish->function_value = NULL;
276 }
277
278 bppy_pending_object = &self_bpfinish->py_bp;
279 bppy_pending_object->number = -1;
280 bppy_pending_object->bp = NULL;
281
282 TRY_CATCH (except, RETURN_MASK_ALL)
283 {
284 /* Set a breakpoint on the return address. */
285 finish_pc = get_frame_pc (prev_frame);
286 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
287 addr_str = small_buf;
288
289 create_breakpoint (python_gdbarch,
290 addr_str, NULL, thread, NULL,
291 0,
292 1 /*temp_flag*/,
293 bp_breakpoint,
294 0,
295 AUTO_BOOLEAN_TRUE,
296 &bkpt_breakpoint_ops,
297 0, 1, internal_bp, 0);
298 }
299 GDB_PY_SET_HANDLE_EXCEPTION (except);
300
301 self_bpfinish->py_bp.bp->frame_id = frame_id;
302 self_bpfinish->py_bp.is_finish_bp = 1;
303
304 /* Bind the breakpoint with the current program space. */
305 self_bpfinish->py_bp.bp->pspace = current_program_space;
306
307 return 0;
308
309 invalid_frame:
310 PyErr_SetString (PyExc_ValueError,
311 _("Invalid ID for the `frame' object."));
312 return -1;
313 }
314
315 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
316 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
317 then delete the breakpoint. */
318
319 static void
320 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
321 {
322 breakpoint_object *bp_obj = (breakpoint_object *) bpfinish_obj;
323 PyObject *py_obj = (PyObject *) bp_obj;
324
325 if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
326 && PyObject_HasAttrString (py_obj, outofscope_func))
327 {
328 if (!PyObject_CallMethod (py_obj, outofscope_func, NULL))
329 gdbpy_print_stack ();
330 }
331
332 delete_breakpoint (bpfinish_obj->py_bp.bp);
333 }
334
335 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
336 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
337
338 static int
339 bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
340 {
341 volatile struct gdb_exception except;
342 struct breakpoint *bp_stopped = (struct breakpoint *) args;
343 PyObject *py_bp = (PyObject *) b->py_bp_object;
344 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
345
346 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
347 not anymore in the current callstack. */
348 if (py_bp != NULL && b->py_bp_object->is_finish_bp)
349 {
350 struct finish_breakpoint_object *finish_bp =
351 (struct finish_breakpoint_object *) py_bp;
352
353 /* Check scope if not currently stopped at the FinishBreakpoint. */
354 if (b != bp_stopped)
355 {
356 TRY_CATCH (except, RETURN_MASK_ALL)
357 {
358 if (b->pspace == current_inferior ()->pspace
359 && (!target_has_registers
360 || frame_find_by_id (b->frame_id) == NULL))
361 bpfinishpy_out_of_scope (finish_bp);
362 }
363 if (except.reason < 0)
364 {
365 gdbpy_convert_exception (except);
366 gdbpy_print_stack ();
367 }
368 }
369 }
370
371 return 0;
372 }
373
374 /* Attached to `stop' notifications, check if the execution has run
375 out of the scope of any FinishBreakpoint before it has been hit. */
376
377 static void
378 bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
379 {
380 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
381 current_language);
382
383 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
384 bs == NULL ? NULL : bs->breakpoint_at);
385
386 do_cleanups (cleanup);
387 }
388
389 /* Attached to `exit' notifications, triggers all the necessary out of
390 scope notifications. */
391
392 static void
393 bpfinishpy_handle_exit (struct inferior *inf)
394 {
395 struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
396 current_language);
397
398 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
399
400 do_cleanups (cleanup);
401 }
402
403 /* Initialize the Python finish breakpoint code. */
404
405 void
406 gdbpy_initialize_finishbreakpoints (void)
407 {
408 if (PyType_Ready (&finish_breakpoint_object_type) < 0)
409 return;
410
411 Py_INCREF (&finish_breakpoint_object_type);
412 PyModule_AddObject (gdb_module, "FinishBreakpoint",
413 (PyObject *) &finish_breakpoint_object_type);
414
415 observer_attach_normal_stop (bpfinishpy_handle_stop);
416 observer_attach_inferior_exit (bpfinishpy_handle_exit);
417 }
418
419 static PyGetSetDef finish_breakpoint_object_getset[] = {
420 { "return_value", bpfinishpy_get_returnvalue, NULL,
421 "gdb.Value object representing the return value, if any. \
422 None otherwise.", NULL },
423 { NULL } /* Sentinel. */
424 };
425
426 static PyTypeObject finish_breakpoint_object_type =
427 {
428 PyObject_HEAD_INIT (NULL)
429 0, /*ob_size*/
430 "gdb.FinishBreakpoint", /*tp_name*/
431 sizeof (struct finish_breakpoint_object), /*tp_basicsize*/
432 0, /*tp_itemsize*/
433 bpfinishpy_dealloc, /*tp_dealloc*/
434 0, /*tp_print*/
435 0, /*tp_getattr*/
436 0, /*tp_setattr*/
437 0, /*tp_compare*/
438 0, /*tp_repr*/
439 0, /*tp_as_number*/
440 0, /*tp_as_sequence*/
441 0, /*tp_as_mapping*/
442 0, /*tp_hash */
443 0, /*tp_call*/
444 0, /*tp_str*/
445 0, /*tp_getattro*/
446 0, /*tp_setattro */
447 0, /*tp_as_buffer*/
448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
449 "GDB finish breakpoint object", /* tp_doc */
450 0, /* tp_traverse */
451 0, /* tp_clear */
452 0, /* tp_richcompare */
453 0, /* tp_weaklistoffset */
454 0, /* tp_iter */
455 0, /* tp_iternext */
456 0, /* tp_methods */
457 0, /* tp_members */
458 finish_breakpoint_object_getset,/* tp_getset */
459 &breakpoint_object_type, /* tp_base */
460 0, /* tp_dict */
461 0, /* tp_descr_get */
462 0, /* tp_descr_set */
463 0, /* tp_dictoffset */
464 bpfinishpy_init, /* tp_init */
465 0, /* tp_alloc */
466 0 /* tp_new */
467 };