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