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