Commit | Line | Data |
---|---|---|
cc72b2a2 KP |
1 | /* Python interface to finish breakpoints |
2 | ||
e2882c85 | 3 | Copyright (C) 2011-2018 Free Software Foundation, Inc. |
cc72b2a2 KP |
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" | |
cc72b2a2 KP |
23 | #include "python-internal.h" |
24 | #include "breakpoint.h" | |
25 | #include "frame.h" | |
26 | #include "gdbthread.h" | |
27 | #include "arch-utils.h" | |
28 | #include "language.h" | |
76727919 | 29 | #include "observable.h" |
cc72b2a2 | 30 | #include "inferior.h" |
6a3a010b | 31 | #include "block.h" |
f00aae0f | 32 | #include "location.h" |
16361ffb | 33 | #include "py-ref.h" |
cc72b2a2 | 34 | |
cc72b2a2 | 35 | /* Function that is called when a Python finish bp is found out of scope. */ |
a121b7c1 | 36 | static const char outofscope_func[] = "out_of_scope"; |
cc72b2a2 KP |
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. */ | |
4cb0213d | 43 | gdbpy_breakpoint_object py_bp; |
cc72b2a2 KP |
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; | |
6a3a010b | 48 | /* gdb.Value object of the function finished by this breakpoint. Will be |
cc72b2a2 | 49 | NULL if return_type is NULL. */ |
6a3a010b | 50 | PyObject *function_value; |
cc72b2a2 KP |
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 | ||
e36122e9 | 57 | extern PyTypeObject finish_breakpoint_object_type |
62eec1a5 TT |
58 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object"); |
59 | ||
cc72b2a2 KP |
60 | /* Python function to get the 'return_value' attribute of |
61 | FinishBreakpoint. */ | |
62 | ||
63 | static PyObject * | |
64 | bpfinishpy_get_returnvalue (PyObject *self, void *closure) | |
65 | { | |
66 | struct finish_breakpoint_object *self_finishbp = | |
67 | (struct finish_breakpoint_object *) self; | |
68 | ||
69 | if (!self_finishbp->return_value) | |
70 | Py_RETURN_NONE; | |
71 | ||
72 | Py_INCREF (self_finishbp->return_value); | |
73 | return self_finishbp->return_value; | |
74 | } | |
75 | ||
76 | /* Deallocate FinishBreakpoint object. */ | |
77 | ||
78 | static void | |
79 | bpfinishpy_dealloc (PyObject *self) | |
80 | { | |
81 | struct finish_breakpoint_object *self_bpfinish = | |
82 | (struct finish_breakpoint_object *) self; | |
83 | ||
6a3a010b | 84 | Py_XDECREF (self_bpfinish->function_value); |
cc72b2a2 KP |
85 | Py_XDECREF (self_bpfinish->return_type); |
86 | Py_XDECREF (self_bpfinish->return_value); | |
87 | } | |
88 | ||
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. */ | |
92 | ||
93 | void | |
4cb0213d | 94 | bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj) |
cc72b2a2 KP |
95 | { |
96 | struct finish_breakpoint_object *self_finishbp = | |
97 | (struct finish_breakpoint_object *) bp_obj; | |
cc72b2a2 KP |
98 | |
99 | /* Can compute return_value only once. */ | |
100 | gdb_assert (!self_finishbp->return_value); | |
101 | ||
102 | if (!self_finishbp->return_type) | |
103 | return; | |
104 | ||
492d29ea | 105 | TRY |
cc72b2a2 | 106 | { |
6a3a010b MR |
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); | |
0700e23e | 111 | struct value *ret = get_return_value (function, value_type); |
cc72b2a2 KP |
112 | |
113 | if (ret) | |
114 | { | |
115 | self_finishbp->return_value = value_to_value_object (ret); | |
116 | if (!self_finishbp->return_value) | |
117 | gdbpy_print_stack (); | |
118 | } | |
119 | else | |
120 | { | |
121 | Py_INCREF (Py_None); | |
122 | self_finishbp->return_value = Py_None; | |
123 | } | |
124 | } | |
492d29ea | 125 | CATCH (except, RETURN_MASK_ALL) |
cc72b2a2 KP |
126 | { |
127 | gdbpy_convert_exception (except); | |
128 | gdbpy_print_stack (); | |
129 | } | |
492d29ea | 130 | END_CATCH |
cc72b2a2 KP |
131 | } |
132 | ||
133 | /* Triggered when gdbpy_should_stop has triggered the `stop' callback | |
134 | of the gdb.FinishBreakpoint object BP_OBJ. */ | |
135 | ||
136 | void | |
4cb0213d | 137 | bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj) |
cc72b2a2 | 138 | { |
cc72b2a2 | 139 | |
492d29ea | 140 | TRY |
cc72b2a2 KP |
141 | { |
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); | |
145 | } | |
492d29ea | 146 | CATCH (except, RETURN_MASK_ALL) |
cc72b2a2 KP |
147 | { |
148 | gdbpy_convert_exception (except); | |
149 | gdbpy_print_stack (); | |
150 | } | |
492d29ea | 151 | END_CATCH |
cc72b2a2 KP |
152 | } |
153 | ||
154 | /* Python function to create a new breakpoint. */ | |
155 | ||
156 | static int | |
157 | bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs) | |
158 | { | |
2adadf51 | 159 | static const char *keywords[] = { "frame", "internal", NULL }; |
cc72b2a2 KP |
160 | struct finish_breakpoint_object *self_bpfinish = |
161 | (struct finish_breakpoint_object *) self; | |
cc72b2a2 KP |
162 | PyObject *frame_obj = NULL; |
163 | int thread; | |
62d7fb51 DE |
164 | struct frame_info *frame = NULL; /* init for gcc -Wall */ |
165 | struct frame_info *prev_frame = NULL; | |
cc72b2a2 KP |
166 | struct frame_id frame_id; |
167 | PyObject *internal = NULL; | |
168 | int internal_bp = 0; | |
a06efdd6 | 169 | CORE_ADDR pc; |
cc72b2a2 KP |
170 | struct symbol *function; |
171 | ||
2adadf51 PA |
172 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords, |
173 | &frame_obj, &internal)) | |
cc72b2a2 KP |
174 | return -1; |
175 | ||
492d29ea | 176 | TRY |
cc72b2a2 | 177 | { |
dd5fa3e7 TT |
178 | /* Default frame to newest frame if necessary. */ |
179 | if (frame_obj == NULL) | |
180 | frame = get_current_frame (); | |
cc72b2a2 | 181 | else |
dd5fa3e7 TT |
182 | frame = frame_object_to_frame_info (frame_obj); |
183 | ||
184 | if (frame == NULL) | |
185 | { | |
256458bc | 186 | PyErr_SetString (PyExc_ValueError, |
dd5fa3e7 TT |
187 | _("Invalid ID for the `frame' object.")); |
188 | } | |
189 | else | |
190 | { | |
191 | prev_frame = get_prev_frame (frame); | |
192 | if (prev_frame == 0) | |
193 | { | |
194 | PyErr_SetString (PyExc_ValueError, | |
195 | _("\"FinishBreakpoint\" not " | |
196 | "meaningful in the outermost " | |
197 | "frame.")); | |
198 | } | |
199 | else if (get_frame_type (prev_frame) == DUMMY_FRAME) | |
200 | { | |
201 | PyErr_SetString (PyExc_ValueError, | |
202 | _("\"FinishBreakpoint\" cannot " | |
203 | "be set on a dummy frame.")); | |
204 | } | |
205 | else | |
206 | { | |
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.")); | |
211 | } | |
212 | } | |
cc72b2a2 | 213 | } |
492d29ea | 214 | CATCH (except, RETURN_MASK_ALL) |
cc72b2a2 KP |
215 | { |
216 | gdbpy_convert_exception (except); | |
217 | return -1; | |
218 | } | |
492d29ea PA |
219 | END_CATCH |
220 | ||
221 | if (PyErr_Occurred ()) | |
cc72b2a2 KP |
222 | return -1; |
223 | ||
00431a78 | 224 | if (inferior_ptid == null_ptid) |
cc72b2a2 KP |
225 | { |
226 | PyErr_SetString (PyExc_ValueError, | |
227 | _("No thread currently selected.")); | |
228 | return -1; | |
229 | } | |
230 | ||
00431a78 PA |
231 | thread = inferior_thread ()->global_num; |
232 | ||
cc72b2a2 KP |
233 | if (internal) |
234 | { | |
235 | internal_bp = PyObject_IsTrue (internal); | |
256458bc | 236 | if (internal_bp == -1) |
cc72b2a2 | 237 | { |
256458bc | 238 | PyErr_SetString (PyExc_ValueError, |
cc72b2a2 KP |
239 | _("The value of `internal' must be a boolean.")); |
240 | return -1; | |
241 | } | |
242 | } | |
243 | ||
244 | /* Find the function we will return from. */ | |
245 | self_bpfinish->return_type = NULL; | |
6a3a010b | 246 | self_bpfinish->function_value = NULL; |
cc72b2a2 | 247 | |
492d29ea | 248 | TRY |
cc72b2a2 KP |
249 | { |
250 | if (get_frame_pc_if_available (frame, &pc)) | |
251 | { | |
252 | function = find_pc_function (pc); | |
253 | if (function != NULL) | |
254 | { | |
255 | struct type *ret_type = | |
256 | TYPE_TARGET_TYPE (SYMBOL_TYPE (function)); | |
257 | ||
258 | /* Remember only non-void return types. */ | |
259 | if (TYPE_CODE (ret_type) != TYPE_CODE_VOID) | |
260 | { | |
6a3a010b MR |
261 | struct value *func_value; |
262 | ||
cc72b2a2 KP |
263 | /* Ignore Python errors at this stage. */ |
264 | self_bpfinish->return_type = type_to_type_object (ret_type); | |
265 | PyErr_Clear (); | |
63e43d3a | 266 | func_value = read_var_value (function, NULL, frame); |
6a3a010b MR |
267 | self_bpfinish->function_value = |
268 | value_to_value_object (func_value); | |
cc72b2a2 KP |
269 | PyErr_Clear (); |
270 | } | |
271 | } | |
272 | } | |
273 | } | |
492d29ea | 274 | CATCH (except, RETURN_MASK_ALL) |
7556d4a4 PA |
275 | { |
276 | /* Just swallow. Either the return type or the function value | |
277 | remain NULL. */ | |
278 | } | |
492d29ea | 279 | END_CATCH |
7556d4a4 PA |
280 | |
281 | if (self_bpfinish->return_type == NULL || self_bpfinish->function_value == NULL) | |
cc72b2a2 KP |
282 | { |
283 | /* Won't be able to compute return value. */ | |
284 | Py_XDECREF (self_bpfinish->return_type); | |
6a3a010b | 285 | Py_XDECREF (self_bpfinish->function_value); |
cc72b2a2 KP |
286 | |
287 | self_bpfinish->return_type = NULL; | |
6a3a010b | 288 | self_bpfinish->function_value = NULL; |
cc72b2a2 KP |
289 | } |
290 | ||
291 | bppy_pending_object = &self_bpfinish->py_bp; | |
292 | bppy_pending_object->number = -1; | |
293 | bppy_pending_object->bp = NULL; | |
294 | ||
492d29ea | 295 | TRY |
cc72b2a2 KP |
296 | { |
297 | /* Set a breakpoint on the return address. */ | |
ffc2605c TT |
298 | event_location_up location |
299 | = new_address_location (get_frame_pc (prev_frame), NULL, 0); | |
cc72b2a2 | 300 | create_breakpoint (python_gdbarch, |
ffc2605c | 301 | location.get (), NULL, thread, NULL, |
cc72b2a2 KP |
302 | 0, |
303 | 1 /*temp_flag*/, | |
304 | bp_breakpoint, | |
305 | 0, | |
306 | AUTO_BOOLEAN_TRUE, | |
307 | &bkpt_breakpoint_ops, | |
44f238bb | 308 | 0, 1, internal_bp, 0); |
cc72b2a2 | 309 | } |
492d29ea PA |
310 | CATCH (except, RETURN_MASK_ALL) |
311 | { | |
312 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
313 | } | |
314 | END_CATCH | |
256458bc | 315 | |
cc72b2a2 KP |
316 | self_bpfinish->py_bp.bp->frame_id = frame_id; |
317 | self_bpfinish->py_bp.is_finish_bp = 1; | |
256458bc | 318 | |
cc72b2a2 KP |
319 | /* Bind the breakpoint with the current program space. */ |
320 | self_bpfinish->py_bp.bp->pspace = current_program_space; | |
321 | ||
322 | return 0; | |
cc72b2a2 KP |
323 | } |
324 | ||
325 | /* Called when GDB notices that the finish breakpoint BP_OBJ is out of | |
326 | the current callstack. Triggers the method OUT_OF_SCOPE if implemented, | |
327 | then delete the breakpoint. */ | |
328 | ||
329 | static void | |
330 | bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj) | |
331 | { | |
4cb0213d | 332 | gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj; |
cc72b2a2 KP |
333 | PyObject *py_obj = (PyObject *) bp_obj; |
334 | ||
335 | if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled | |
336 | && PyObject_HasAttrString (py_obj, outofscope_func)) | |
337 | { | |
7780f186 TT |
338 | gdbpy_ref<> meth_result (PyObject_CallMethod (py_obj, outofscope_func, |
339 | NULL)); | |
18868860 TT |
340 | if (meth_result == NULL) |
341 | gdbpy_print_stack (); | |
cc72b2a2 KP |
342 | } |
343 | ||
344 | delete_breakpoint (bpfinish_obj->py_bp.bp); | |
345 | } | |
346 | ||
347 | /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's | |
348 | `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */ | |
349 | ||
350 | static int | |
351 | bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args) | |
352 | { | |
cc72b2a2 KP |
353 | struct breakpoint *bp_stopped = (struct breakpoint *) args; |
354 | PyObject *py_bp = (PyObject *) b->py_bp_object; | |
256458bc | 355 | |
cc72b2a2 KP |
356 | /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is |
357 | not anymore in the current callstack. */ | |
358 | if (py_bp != NULL && b->py_bp_object->is_finish_bp) | |
359 | { | |
360 | struct finish_breakpoint_object *finish_bp = | |
361 | (struct finish_breakpoint_object *) py_bp; | |
362 | ||
363 | /* Check scope if not currently stopped at the FinishBreakpoint. */ | |
364 | if (b != bp_stopped) | |
365 | { | |
492d29ea | 366 | TRY |
cc72b2a2 KP |
367 | { |
368 | if (b->pspace == current_inferior ()->pspace | |
369 | && (!target_has_registers | |
370 | || frame_find_by_id (b->frame_id) == NULL)) | |
371 | bpfinishpy_out_of_scope (finish_bp); | |
372 | } | |
492d29ea | 373 | CATCH (except, RETURN_MASK_ALL) |
cc72b2a2 KP |
374 | { |
375 | gdbpy_convert_exception (except); | |
376 | gdbpy_print_stack (); | |
377 | } | |
492d29ea | 378 | END_CATCH |
cc72b2a2 KP |
379 | } |
380 | } | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | /* Attached to `stop' notifications, check if the execution has run | |
386 | out of the scope of any FinishBreakpoint before it has been hit. */ | |
387 | ||
388 | static void | |
389 | bpfinishpy_handle_stop (struct bpstats *bs, int print_frame) | |
390 | { | |
6e7c365e | 391 | gdbpy_enter enter_py (get_current_arch (), current_language); |
cc72b2a2 KP |
392 | |
393 | iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, | |
394 | bs == NULL ? NULL : bs->breakpoint_at); | |
cc72b2a2 KP |
395 | } |
396 | ||
397 | /* Attached to `exit' notifications, triggers all the necessary out of | |
398 | scope notifications. */ | |
399 | ||
400 | static void | |
401 | bpfinishpy_handle_exit (struct inferior *inf) | |
402 | { | |
6e7c365e | 403 | gdbpy_enter enter_py (target_gdbarch (), current_language); |
cc72b2a2 KP |
404 | |
405 | iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL); | |
cc72b2a2 KP |
406 | } |
407 | ||
408 | /* Initialize the Python finish breakpoint code. */ | |
409 | ||
999633ed | 410 | int |
cc72b2a2 KP |
411 | gdbpy_initialize_finishbreakpoints (void) |
412 | { | |
413 | if (PyType_Ready (&finish_breakpoint_object_type) < 0) | |
999633ed | 414 | return -1; |
256458bc | 415 | |
aa36459a TT |
416 | if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint", |
417 | (PyObject *) &finish_breakpoint_object_type) < 0) | |
999633ed | 418 | return -1; |
256458bc | 419 | |
76727919 TT |
420 | gdb::observers::normal_stop.attach (bpfinishpy_handle_stop); |
421 | gdb::observers::inferior_exit.attach (bpfinishpy_handle_exit); | |
999633ed TT |
422 | |
423 | return 0; | |
cc72b2a2 KP |
424 | } |
425 | ||
0d1f4ceb | 426 | static gdb_PyGetSetDef finish_breakpoint_object_getset[] = { |
cc72b2a2 KP |
427 | { "return_value", bpfinishpy_get_returnvalue, NULL, |
428 | "gdb.Value object representing the return value, if any. \ | |
429 | None otherwise.", NULL }, | |
430 | { NULL } /* Sentinel. */ | |
431 | }; | |
432 | ||
e36122e9 | 433 | PyTypeObject finish_breakpoint_object_type = |
cc72b2a2 | 434 | { |
9a27f2c6 | 435 | PyVarObject_HEAD_INIT (NULL, 0) |
cc72b2a2 KP |
436 | "gdb.FinishBreakpoint", /*tp_name*/ |
437 | sizeof (struct finish_breakpoint_object), /*tp_basicsize*/ | |
438 | 0, /*tp_itemsize*/ | |
439 | bpfinishpy_dealloc, /*tp_dealloc*/ | |
440 | 0, /*tp_print*/ | |
441 | 0, /*tp_getattr*/ | |
442 | 0, /*tp_setattr*/ | |
443 | 0, /*tp_compare*/ | |
444 | 0, /*tp_repr*/ | |
445 | 0, /*tp_as_number*/ | |
446 | 0, /*tp_as_sequence*/ | |
447 | 0, /*tp_as_mapping*/ | |
448 | 0, /*tp_hash */ | |
449 | 0, /*tp_call*/ | |
450 | 0, /*tp_str*/ | |
451 | 0, /*tp_getattro*/ | |
452 | 0, /*tp_setattro */ | |
453 | 0, /*tp_as_buffer*/ | |
454 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
455 | "GDB finish breakpoint object", /* tp_doc */ | |
456 | 0, /* tp_traverse */ | |
457 | 0, /* tp_clear */ | |
458 | 0, /* tp_richcompare */ | |
459 | 0, /* tp_weaklistoffset */ | |
460 | 0, /* tp_iter */ | |
461 | 0, /* tp_iternext */ | |
462 | 0, /* tp_methods */ | |
463 | 0, /* tp_members */ | |
464 | finish_breakpoint_object_getset,/* tp_getset */ | |
465 | &breakpoint_object_type, /* tp_base */ | |
466 | 0, /* tp_dict */ | |
467 | 0, /* tp_descr_get */ | |
468 | 0, /* tp_descr_set */ | |
469 | 0, /* tp_dictoffset */ | |
470 | bpfinishpy_init, /* tp_init */ | |
471 | 0, /* tp_alloc */ | |
472 | 0 /* tp_new */ | |
473 | }; |