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