Explicit locations: use new location API
[deliverable/binutils-gdb.git] / gdb / python / py-finishbreakpoint.c
CommitLineData
cc72b2a2
KP
1/* Python interface to finish breakpoints
2
32d0add0 3 Copyright (C) 2011-2015 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"
29#include "observer.h"
30#include "inferior.h"
6a3a010b 31#include "block.h"
f00aae0f 32#include "location.h"
cc72b2a2 33
cc72b2a2
KP
34/* Function that is called when a Python finish bp is found out of scope. */
35static char * const outofscope_func = "out_of_scope";
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;
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 56extern 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
62static PyObject *
63bpfinishpy_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
77static void
78bpfinishpy_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
92void
4cb0213d 93bpfinishpy_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
492d29ea 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);
8a6c4031
JK
110
111 /* bpfinishpy_init cannot finish into DUMMY_FRAME (throws an error
112 in such case) so it is OK to always pass CTX_SAVER as NULL. */
113 struct value *ret = get_return_value (function, value_type, NULL);
cc72b2a2
KP
114
115 if (ret)
116 {
117 self_finishbp->return_value = value_to_value_object (ret);
118 if (!self_finishbp->return_value)
119 gdbpy_print_stack ();
120 }
121 else
122 {
123 Py_INCREF (Py_None);
124 self_finishbp->return_value = Py_None;
125 }
126 }
492d29ea 127 CATCH (except, RETURN_MASK_ALL)
cc72b2a2
KP
128 {
129 gdbpy_convert_exception (except);
130 gdbpy_print_stack ();
131 }
492d29ea 132 END_CATCH
cc72b2a2
KP
133}
134
135/* Triggered when gdbpy_should_stop has triggered the `stop' callback
136 of the gdb.FinishBreakpoint object BP_OBJ. */
137
138void
4cb0213d 139bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
cc72b2a2 140{
cc72b2a2 141
492d29ea 142 TRY
cc72b2a2
KP
143 {
144 /* Can't delete it here, but it will be removed at the next stop. */
145 disable_breakpoint (bp_obj->bp);
146 gdb_assert (bp_obj->bp->disposition == disp_del);
147 }
492d29ea 148 CATCH (except, RETURN_MASK_ALL)
cc72b2a2
KP
149 {
150 gdbpy_convert_exception (except);
151 gdbpy_print_stack ();
152 }
492d29ea 153 END_CATCH
cc72b2a2
KP
154}
155
156/* Python function to create a new breakpoint. */
157
158static int
159bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
160{
161 static char *keywords[] = { "frame", "internal", NULL };
162 struct finish_breakpoint_object *self_bpfinish =
163 (struct finish_breakpoint_object *) self;
164 int type = bp_breakpoint;
165 PyObject *frame_obj = NULL;
166 int thread;
62d7fb51
DE
167 struct frame_info *frame = NULL; /* init for gcc -Wall */
168 struct frame_info *prev_frame = NULL;
cc72b2a2
KP
169 struct frame_id frame_id;
170 PyObject *internal = NULL;
171 int internal_bp = 0;
172 CORE_ADDR finish_pc, pc;
f00aae0f 173 char small_buf[100], *p;
cc72b2a2
KP
174 struct symbol *function;
175
176 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
177 &frame_obj, &internal))
178 return -1;
179
492d29ea 180 TRY
cc72b2a2 181 {
dd5fa3e7
TT
182 /* Default frame to newest frame if necessary. */
183 if (frame_obj == NULL)
184 frame = get_current_frame ();
cc72b2a2 185 else
dd5fa3e7
TT
186 frame = frame_object_to_frame_info (frame_obj);
187
188 if (frame == NULL)
189 {
256458bc 190 PyErr_SetString (PyExc_ValueError,
dd5fa3e7
TT
191 _("Invalid ID for the `frame' object."));
192 }
193 else
194 {
195 prev_frame = get_prev_frame (frame);
196 if (prev_frame == 0)
197 {
198 PyErr_SetString (PyExc_ValueError,
199 _("\"FinishBreakpoint\" not "
200 "meaningful in the outermost "
201 "frame."));
202 }
203 else if (get_frame_type (prev_frame) == DUMMY_FRAME)
204 {
205 PyErr_SetString (PyExc_ValueError,
206 _("\"FinishBreakpoint\" cannot "
207 "be set on a dummy frame."));
208 }
209 else
210 {
211 frame_id = get_frame_id (prev_frame);
212 if (frame_id_eq (frame_id, null_frame_id))
213 PyErr_SetString (PyExc_ValueError,
214 _("Invalid ID for the `frame' object."));
215 }
216 }
cc72b2a2 217 }
492d29ea 218 CATCH (except, RETURN_MASK_ALL)
cc72b2a2
KP
219 {
220 gdbpy_convert_exception (except);
221 return -1;
222 }
492d29ea
PA
223 END_CATCH
224
225 if (PyErr_Occurred ())
cc72b2a2
KP
226 return -1;
227
228 thread = pid_to_thread_id (inferior_ptid);
229 if (thread == 0)
230 {
231 PyErr_SetString (PyExc_ValueError,
232 _("No thread currently selected."));
233 return -1;
234 }
235
236 if (internal)
237 {
238 internal_bp = PyObject_IsTrue (internal);
256458bc 239 if (internal_bp == -1)
cc72b2a2 240 {
256458bc 241 PyErr_SetString (PyExc_ValueError,
cc72b2a2
KP
242 _("The value of `internal' must be a boolean."));
243 return -1;
244 }
245 }
246
247 /* Find the function we will return from. */
248 self_bpfinish->return_type = NULL;
6a3a010b 249 self_bpfinish->function_value = NULL;
cc72b2a2 250
492d29ea 251 TRY
cc72b2a2
KP
252 {
253 if (get_frame_pc_if_available (frame, &pc))
254 {
255 function = find_pc_function (pc);
256 if (function != NULL)
257 {
258 struct type *ret_type =
259 TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
260
261 /* Remember only non-void return types. */
262 if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
263 {
6a3a010b
MR
264 struct value *func_value;
265
cc72b2a2
KP
266 /* Ignore Python errors at this stage. */
267 self_bpfinish->return_type = type_to_type_object (ret_type);
268 PyErr_Clear ();
6a3a010b
MR
269 func_value = read_var_value (function, frame);
270 self_bpfinish->function_value =
271 value_to_value_object (func_value);
cc72b2a2
KP
272 PyErr_Clear ();
273 }
274 }
275 }
276 }
492d29ea 277 CATCH (except, RETURN_MASK_ALL)
7556d4a4
PA
278 {
279 /* Just swallow. Either the return type or the function value
280 remain NULL. */
281 }
492d29ea 282 END_CATCH
7556d4a4
PA
283
284 if (self_bpfinish->return_type == NULL || self_bpfinish->function_value == NULL)
cc72b2a2
KP
285 {
286 /* Won't be able to compute return value. */
287 Py_XDECREF (self_bpfinish->return_type);
6a3a010b 288 Py_XDECREF (self_bpfinish->function_value);
cc72b2a2
KP
289
290 self_bpfinish->return_type = NULL;
6a3a010b 291 self_bpfinish->function_value = NULL;
cc72b2a2
KP
292 }
293
294 bppy_pending_object = &self_bpfinish->py_bp;
295 bppy_pending_object->number = -1;
296 bppy_pending_object->bp = NULL;
297
492d29ea 298 TRY
cc72b2a2 299 {
f00aae0f
KS
300 struct event_location *location;
301 struct cleanup *back_to;
302
cc72b2a2
KP
303 /* Set a breakpoint on the return address. */
304 finish_pc = get_frame_pc (prev_frame);
41843fe8 305 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
f00aae0f
KS
306 p = small_buf;
307 location = new_linespec_location (&p);
308 back_to = make_cleanup_delete_event_location (location);
cc72b2a2 309 create_breakpoint (python_gdbarch,
f00aae0f 310 location, NULL, thread, NULL,
cc72b2a2
KP
311 0,
312 1 /*temp_flag*/,
313 bp_breakpoint,
314 0,
315 AUTO_BOOLEAN_TRUE,
316 &bkpt_breakpoint_ops,
44f238bb 317 0, 1, internal_bp, 0);
f00aae0f 318 do_cleanups (back_to);
cc72b2a2 319 }
492d29ea
PA
320 CATCH (except, RETURN_MASK_ALL)
321 {
322 GDB_PY_SET_HANDLE_EXCEPTION (except);
323 }
324 END_CATCH
256458bc 325
cc72b2a2
KP
326 self_bpfinish->py_bp.bp->frame_id = frame_id;
327 self_bpfinish->py_bp.is_finish_bp = 1;
256458bc 328
cc72b2a2
KP
329 /* Bind the breakpoint with the current program space. */
330 self_bpfinish->py_bp.bp->pspace = current_program_space;
331
332 return 0;
cc72b2a2
KP
333}
334
335/* Called when GDB notices that the finish breakpoint BP_OBJ is out of
336 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
337 then delete the breakpoint. */
338
339static void
340bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
341{
4cb0213d 342 gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
cc72b2a2
KP
343 PyObject *py_obj = (PyObject *) bp_obj;
344
345 if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
346 && PyObject_HasAttrString (py_obj, outofscope_func))
347 {
18868860
TT
348 PyObject *meth_result;
349
350 meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
351 if (meth_result == NULL)
352 gdbpy_print_stack ();
353 Py_XDECREF (meth_result);
cc72b2a2
KP
354 }
355
356 delete_breakpoint (bpfinish_obj->py_bp.bp);
357}
358
359/* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
360 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
361
362static int
363bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
364{
cc72b2a2
KP
365 struct breakpoint *bp_stopped = (struct breakpoint *) args;
366 PyObject *py_bp = (PyObject *) b->py_bp_object;
367 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
256458bc 368
cc72b2a2
KP
369 /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
370 not anymore in the current callstack. */
371 if (py_bp != NULL && b->py_bp_object->is_finish_bp)
372 {
373 struct finish_breakpoint_object *finish_bp =
374 (struct finish_breakpoint_object *) py_bp;
375
376 /* Check scope if not currently stopped at the FinishBreakpoint. */
377 if (b != bp_stopped)
378 {
492d29ea 379 TRY
cc72b2a2
KP
380 {
381 if (b->pspace == current_inferior ()->pspace
382 && (!target_has_registers
383 || frame_find_by_id (b->frame_id) == NULL))
384 bpfinishpy_out_of_scope (finish_bp);
385 }
492d29ea 386 CATCH (except, RETURN_MASK_ALL)
cc72b2a2
KP
387 {
388 gdbpy_convert_exception (except);
389 gdbpy_print_stack ();
390 }
492d29ea 391 END_CATCH
cc72b2a2
KP
392 }
393 }
394
395 return 0;
396}
397
398/* Attached to `stop' notifications, check if the execution has run
399 out of the scope of any FinishBreakpoint before it has been hit. */
400
401static void
402bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
403{
404 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
405 current_language);
406
407 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
408 bs == NULL ? NULL : bs->breakpoint_at);
409
410 do_cleanups (cleanup);
411}
412
413/* Attached to `exit' notifications, triggers all the necessary out of
414 scope notifications. */
415
416static void
417bpfinishpy_handle_exit (struct inferior *inf)
418{
f5656ead 419 struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
cc72b2a2
KP
420 current_language);
421
422 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
423
424 do_cleanups (cleanup);
425}
426
427/* Initialize the Python finish breakpoint code. */
428
999633ed 429int
cc72b2a2
KP
430gdbpy_initialize_finishbreakpoints (void)
431{
432 if (PyType_Ready (&finish_breakpoint_object_type) < 0)
999633ed 433 return -1;
256458bc 434
aa36459a
TT
435 if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
436 (PyObject *) &finish_breakpoint_object_type) < 0)
999633ed 437 return -1;
256458bc 438
cc72b2a2
KP
439 observer_attach_normal_stop (bpfinishpy_handle_stop);
440 observer_attach_inferior_exit (bpfinishpy_handle_exit);
999633ed
TT
441
442 return 0;
cc72b2a2
KP
443}
444
445static PyGetSetDef finish_breakpoint_object_getset[] = {
446 { "return_value", bpfinishpy_get_returnvalue, NULL,
447 "gdb.Value object representing the return value, if any. \
448None otherwise.", NULL },
449 { NULL } /* Sentinel. */
450};
451
e36122e9 452PyTypeObject finish_breakpoint_object_type =
cc72b2a2 453{
9a27f2c6 454 PyVarObject_HEAD_INIT (NULL, 0)
cc72b2a2
KP
455 "gdb.FinishBreakpoint", /*tp_name*/
456 sizeof (struct finish_breakpoint_object), /*tp_basicsize*/
457 0, /*tp_itemsize*/
458 bpfinishpy_dealloc, /*tp_dealloc*/
459 0, /*tp_print*/
460 0, /*tp_getattr*/
461 0, /*tp_setattr*/
462 0, /*tp_compare*/
463 0, /*tp_repr*/
464 0, /*tp_as_number*/
465 0, /*tp_as_sequence*/
466 0, /*tp_as_mapping*/
467 0, /*tp_hash */
468 0, /*tp_call*/
469 0, /*tp_str*/
470 0, /*tp_getattro*/
471 0, /*tp_setattro */
472 0, /*tp_as_buffer*/
473 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
474 "GDB finish breakpoint object", /* tp_doc */
475 0, /* tp_traverse */
476 0, /* tp_clear */
477 0, /* tp_richcompare */
478 0, /* tp_weaklistoffset */
479 0, /* tp_iter */
480 0, /* tp_iternext */
481 0, /* tp_methods */
482 0, /* tp_members */
483 finish_breakpoint_object_getset,/* tp_getset */
484 &breakpoint_object_type, /* tp_base */
485 0, /* tp_dict */
486 0, /* tp_descr_get */
487 0, /* tp_descr_set */
488 0, /* tp_dictoffset */
489 bpfinishpy_init, /* tp_init */
490 0, /* tp_alloc */
491 0 /* tp_new */
492};
This page took 0.404582 seconds and 4 git commands to generate.