]> Git Repo - binutils.git/blame - gdb/python/py-finishbreakpoint.c
Automatic date update in version.in
[binutils.git] / gdb / python / py-finishbreakpoint.c
CommitLineData
cc72b2a2
KP
1/* Python interface to finish breakpoints
2
4a94e368 3 Copyright (C) 2011-2022 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 35static const char outofscope_func[] = "out_of_scope";
cc72b2a2
KP
36
37/* struct implementing the gdb.FinishBreakpoint object by extending
38 the gdb.Breakpoint class. */
39struct finish_breakpoint_object
40{
41 /* gdb.Breakpoint base class. */
4cb0213d 42 gdbpy_breakpoint_object py_bp;
e6b36367
LS
43
44 /* gdb.Symbol object of the function finished by this breakpoint.
45
46 nullptr if no debug information was available or return type was VOID. */
47 PyObject *func_symbol;
48
49 /* gdb.Value object of the function finished by this breakpoint.
50
51 nullptr if no debug information was available or return type was VOID. */
6a3a010b 52 PyObject *function_value;
e6b36367 53
cc72b2a2
KP
54 /* When stopped at this FinishBreakpoint, gdb.Value object returned by
55 the function; Py_None if the value is not computable; NULL if GDB is
56 not stopped at a FinishBreakpoint. */
57 PyObject *return_value;
58};
59
e36122e9 60extern PyTypeObject finish_breakpoint_object_type
62eec1a5
TT
61 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
62
cc72b2a2
KP
63/* Python function to get the 'return_value' attribute of
64 FinishBreakpoint. */
65
66static PyObject *
67bpfinishpy_get_returnvalue (PyObject *self, void *closure)
68{
69 struct finish_breakpoint_object *self_finishbp =
70 (struct finish_breakpoint_object *) self;
71
72 if (!self_finishbp->return_value)
73 Py_RETURN_NONE;
74
75 Py_INCREF (self_finishbp->return_value);
76 return self_finishbp->return_value;
77}
78
79/* Deallocate FinishBreakpoint object. */
80
81static void
82bpfinishpy_dealloc (PyObject *self)
83{
84 struct finish_breakpoint_object *self_bpfinish =
dda83cd7 85 (struct finish_breakpoint_object *) self;
cc72b2a2 86
e6b36367 87 Py_XDECREF (self_bpfinish->func_symbol);
6a3a010b 88 Py_XDECREF (self_bpfinish->function_value);
cc72b2a2 89 Py_XDECREF (self_bpfinish->return_value);
2e953aca 90 Py_TYPE (self)->tp_free (self);
cc72b2a2
KP
91}
92
93/* Triggered when gdbpy_should_stop is about to execute the `stop' callback
94 of the gdb.FinishBreakpoint object BP_OBJ. Will compute and cache the
95 `return_value', if possible. */
96
97void
4cb0213d 98bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
cc72b2a2
KP
99{
100 struct finish_breakpoint_object *self_finishbp =
dda83cd7 101 (struct finish_breakpoint_object *) bp_obj;
cc72b2a2
KP
102
103 /* Can compute return_value only once. */
104 gdb_assert (!self_finishbp->return_value);
105
e6b36367 106 if (self_finishbp->func_symbol == nullptr)
cc72b2a2
KP
107 return;
108
a70b8144 109 try
cc72b2a2 110 {
e6b36367
LS
111 struct symbol *func_symbol =
112 symbol_object_to_symbol (self_finishbp->func_symbol);
6a3a010b 113 struct value *function =
dda83cd7 114 value_object_to_value (self_finishbp->function_value);
e6b36367
LS
115 struct value *ret =
116 get_return_value (func_symbol, function);
cc72b2a2
KP
117
118 if (ret)
dda83cd7
SM
119 {
120 self_finishbp->return_value = value_to_value_object (ret);
121 if (!self_finishbp->return_value)
122 gdbpy_print_stack ();
123 }
cc72b2a2 124 else
dda83cd7
SM
125 {
126 Py_INCREF (Py_None);
127 self_finishbp->return_value = Py_None;
128 }
cc72b2a2 129 }
230d2906 130 catch (const gdb_exception &except)
cc72b2a2
KP
131 {
132 gdbpy_convert_exception (except);
133 gdbpy_print_stack ();
134 }
135}
136
137/* Triggered when gdbpy_should_stop has triggered the `stop' callback
138 of the gdb.FinishBreakpoint object BP_OBJ. */
139
140void
4cb0213d 141bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
cc72b2a2 142{
cc72b2a2 143
a70b8144 144 try
cc72b2a2
KP
145 {
146 /* Can't delete it here, but it will be removed at the next stop. */
147 disable_breakpoint (bp_obj->bp);
6533cbee 148 bp_obj->bp->disposition = disp_del_at_next_stop;
cc72b2a2 149 }
230d2906 150 catch (const gdb_exception &except)
cc72b2a2
KP
151 {
152 gdbpy_convert_exception (except);
153 gdbpy_print_stack ();
154 }
155}
156
157/* Python function to create a new breakpoint. */
158
159static int
160bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
161{
2adadf51 162 static const char *keywords[] = { "frame", "internal", NULL };
cc72b2a2
KP
163 struct finish_breakpoint_object *self_bpfinish =
164 (struct finish_breakpoint_object *) self;
cc72b2a2
KP
165 PyObject *frame_obj = NULL;
166 int thread;
bd2b40ac
TT
167 frame_info_ptr frame = NULL; /* init for gcc -Wall */
168 frame_info_ptr prev_frame = NULL;
cc72b2a2
KP
169 struct frame_id frame_id;
170 PyObject *internal = NULL;
171 int internal_bp = 0;
a06efdd6 172 CORE_ADDR pc;
cc72b2a2 173
2adadf51
PA
174 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
175 &frame_obj, &internal))
cc72b2a2
KP
176 return -1;
177
a70b8144 178 try
cc72b2a2 179 {
dd5fa3e7
TT
180 /* Default frame to newest frame if necessary. */
181 if (frame_obj == NULL)
182 frame = get_current_frame ();
cc72b2a2 183 else
dd5fa3e7
TT
184 frame = frame_object_to_frame_info (frame_obj);
185
186 if (frame == NULL)
187 {
256458bc 188 PyErr_SetString (PyExc_ValueError,
dd5fa3e7
TT
189 _("Invalid ID for the `frame' object."));
190 }
191 else
192 {
193 prev_frame = get_prev_frame (frame);
194 if (prev_frame == 0)
195 {
196 PyErr_SetString (PyExc_ValueError,
197 _("\"FinishBreakpoint\" not "
198 "meaningful in the outermost "
199 "frame."));
200 }
201 else if (get_frame_type (prev_frame) == DUMMY_FRAME)
202 {
203 PyErr_SetString (PyExc_ValueError,
204 _("\"FinishBreakpoint\" cannot "
205 "be set on a dummy frame."));
206 }
207 else
04ea6b63 208 frame_id = get_frame_id (prev_frame);
dd5fa3e7 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,
dda83cd7 223 _("No thread currently selected."));
cc72b2a2
KP
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)
dda83cd7
SM
233 {
234 PyErr_SetString (PyExc_ValueError,
235 _("The value of `internal' must be a boolean."));
236 return -1;
237 }
cc72b2a2
KP
238 }
239
240 /* Find the function we will return from. */
e6b36367
LS
241 self_bpfinish->func_symbol = nullptr;
242 self_bpfinish->function_value = nullptr;
cc72b2a2 243
a70b8144 244 try
cc72b2a2
KP
245 {
246 if (get_frame_pc_if_available (frame, &pc))
dda83cd7 247 {
e6b36367
LS
248 struct symbol *function = find_pc_function (pc);
249 if (function != nullptr)
dda83cd7
SM
250 {
251 struct type *ret_type =
27710edb 252 check_typedef (function->type ()->target_type ());
cc72b2a2 253
dda83cd7
SM
254 /* Remember only non-void return types. */
255 if (ret_type->code () != TYPE_CODE_VOID)
256 {
dda83cd7 257 /* Ignore Python errors at this stage. */
e6b36367
LS
258 value *func_value = read_var_value (function, NULL, frame);
259 self_bpfinish->function_value
260 = value_to_value_object (func_value);
dda83cd7 261 PyErr_Clear ();
e6b36367
LS
262
263 self_bpfinish->func_symbol
264 = symbol_to_symbol_object (function);
dda83cd7
SM
265 PyErr_Clear ();
266 }
267 }
268 }
cc72b2a2 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
e6b36367
LS
276 if (self_bpfinish->func_symbol == nullptr
277 || self_bpfinish->function_value == nullptr)
cc72b2a2
KP
278 {
279 /* Won't be able to compute return value. */
e6b36367 280 Py_XDECREF (self_bpfinish->func_symbol);
6a3a010b 281 Py_XDECREF (self_bpfinish->function_value);
cc72b2a2 282
e6b36367
LS
283 self_bpfinish->func_symbol = nullptr;
284 self_bpfinish->function_value = nullptr;
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. */
264f9890
PA
294 location_spec_up locspec
295 = new_address_location_spec (get_frame_pc (prev_frame), NULL, 0);
1da5d0e6 296 create_breakpoint (gdbpy_enter::get_gdbarch (),
264f9890 297 locspec.get (), NULL, thread, NULL, false,
dda83cd7
SM
298 0,
299 1 /*temp_flag*/,
300 bp_breakpoint,
301 0,
302 AUTO_BOOLEAN_TRUE,
74421c0b 303 &code_breakpoint_ops,
dda83cd7 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
324static void
325bpfinishpy_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 345static void
95da600f
CB
346bpfinishpy_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
380static void
313f3b21 381bpfinishpy_handle_stop (struct bpstat *bs, int print_frame)
cc72b2a2 382{
1da5d0e6 383 gdbpy_enter enter_py;
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
392static void
393bpfinishpy_handle_exit (struct inferior *inf)
394{
1da5d0e6 395 gdbpy_enter enter_py (target_gdbarch ());
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 403int
cc72b2a2
KP
404gdbpy_initialize_finishbreakpoints (void)
405{
8a3b1706
AB
406 if (!gdbpy_breakpoint_init_breakpoint_type ())
407 return -1;
408
cc72b2a2 409 if (PyType_Ready (&finish_breakpoint_object_type) < 0)
999633ed 410 return -1;
256458bc 411
aa36459a
TT
412 if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
413 (PyObject *) &finish_breakpoint_object_type) < 0)
999633ed 414 return -1;
256458bc 415
c90e7d63
SM
416 gdb::observers::normal_stop.attach (bpfinishpy_handle_stop,
417 "py-finishbreakpoint");
418 gdb::observers::inferior_exit.attach (bpfinishpy_handle_exit,
419 "py-finishbreakpoint");
999633ed
TT
420
421 return 0;
cc72b2a2
KP
422}
423
0d1f4ceb 424static gdb_PyGetSetDef finish_breakpoint_object_getset[] = {
cc72b2a2
KP
425 { "return_value", bpfinishpy_get_returnvalue, NULL,
426 "gdb.Value object representing the return value, if any. \
427None otherwise.", NULL },
428 { NULL } /* Sentinel. */
429};
430
e36122e9 431PyTypeObject finish_breakpoint_object_type =
cc72b2a2 432{
9a27f2c6 433 PyVarObject_HEAD_INIT (NULL, 0)
cc72b2a2
KP
434 "gdb.FinishBreakpoint", /*tp_name*/
435 sizeof (struct finish_breakpoint_object), /*tp_basicsize*/
436 0, /*tp_itemsize*/
437 bpfinishpy_dealloc, /*tp_dealloc*/
438 0, /*tp_print*/
439 0, /*tp_getattr*/
440 0, /*tp_setattr*/
441 0, /*tp_compare*/
442 0, /*tp_repr*/
443 0, /*tp_as_number*/
444 0, /*tp_as_sequence*/
445 0, /*tp_as_mapping*/
446 0, /*tp_hash */
447 0, /*tp_call*/
448 0, /*tp_str*/
449 0, /*tp_getattro*/
450 0, /*tp_setattro */
451 0, /*tp_as_buffer*/
452 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
453 "GDB finish breakpoint object", /* tp_doc */
454 0, /* tp_traverse */
455 0, /* tp_clear */
456 0, /* tp_richcompare */
457 0, /* tp_weaklistoffset */
458 0, /* tp_iter */
459 0, /* tp_iternext */
460 0, /* tp_methods */
461 0, /* tp_members */
462 finish_breakpoint_object_getset,/* tp_getset */
463 &breakpoint_object_type, /* tp_base */
464 0, /* tp_dict */
465 0, /* tp_descr_get */
466 0, /* tp_descr_set */
467 0, /* tp_dictoffset */
468 bpfinishpy_init, /* tp_init */
469 0, /* tp_alloc */
470 0 /* tp_new */
471};
This page took 2.923706 seconds and 4 git commands to generate.