Fix dw2-ifort-parameter.exp on PPC64
[binutils-gdb.git] / gdb / python / py-finishbreakpoint.c
blob712a9ee5a98a89722b4739100b17342d37dbafed
1 /* Python interface to finish breakpoints
3 Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "defs.h"
23 #include "exceptions.h"
24 #include "python-internal.h"
25 #include "breakpoint.h"
26 #include "frame.h"
27 #include "gdbthread.h"
28 #include "arch-utils.h"
29 #include "language.h"
30 #include "observer.h"
31 #include "inferior.h"
32 #include "block.h"
34 /* Function that is called when a Python finish bp is found out of scope. */
35 static char * const outofscope_func = "out_of_scope";
37 /* struct implementing the gdb.FinishBreakpoint object by extending
38 the gdb.Breakpoint class. */
39 struct finish_breakpoint_object
41 /* gdb.Breakpoint base class. */
42 gdbpy_breakpoint_object py_bp;
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;
47 /* gdb.Value object of the function finished by this breakpoint. Will be
48 NULL if return_type is NULL. */
49 PyObject *function_value;
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;
56 static PyTypeObject finish_breakpoint_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("finish_breakpoint_object");
59 /* Python function to get the 'return_value' attribute of
60 FinishBreakpoint. */
62 static PyObject *
63 bpfinishpy_get_returnvalue (PyObject *self, void *closure)
65 struct finish_breakpoint_object *self_finishbp =
66 (struct finish_breakpoint_object *) self;
68 if (!self_finishbp->return_value)
69 Py_RETURN_NONE;
71 Py_INCREF (self_finishbp->return_value);
72 return self_finishbp->return_value;
75 /* Deallocate FinishBreakpoint object. */
77 static void
78 bpfinishpy_dealloc (PyObject *self)
80 struct finish_breakpoint_object *self_bpfinish =
81 (struct finish_breakpoint_object *) self;
83 Py_XDECREF (self_bpfinish->function_value);
84 Py_XDECREF (self_bpfinish->return_type);
85 Py_XDECREF (self_bpfinish->return_value);
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. */
92 void
93 bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
95 struct finish_breakpoint_object *self_finishbp =
96 (struct finish_breakpoint_object *) bp_obj;
97 volatile struct gdb_exception except;
99 /* Can compute return_value only once. */
100 gdb_assert (!self_finishbp->return_value);
102 if (!self_finishbp->return_type)
103 return;
105 TRY_CATCH (except, RETURN_MASK_ALL)
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);
111 struct value *ret = get_return_value (function, value_type);
113 if (ret)
115 self_finishbp->return_value = value_to_value_object (ret);
116 if (!self_finishbp->return_value)
117 gdbpy_print_stack ();
119 else
121 Py_INCREF (Py_None);
122 self_finishbp->return_value = Py_None;
125 if (except.reason < 0)
127 gdbpy_convert_exception (except);
128 gdbpy_print_stack ();
132 /* Triggered when gdbpy_should_stop has triggered the `stop' callback
133 of the gdb.FinishBreakpoint object BP_OBJ. */
135 void
136 bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj)
138 volatile struct gdb_exception except;
140 TRY_CATCH (except, RETURN_MASK_ALL)
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);
146 if (except.reason < 0)
148 gdbpy_convert_exception (except);
149 gdbpy_print_stack ();
153 /* Python function to create a new breakpoint. */
155 static int
156 bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
158 static char *keywords[] = { "frame", "internal", NULL };
159 struct finish_breakpoint_object *self_bpfinish =
160 (struct finish_breakpoint_object *) self;
161 int type = bp_breakpoint;
162 PyObject *frame_obj = NULL;
163 int thread;
164 struct frame_info *frame = NULL; /* init for gcc -Wall */
165 struct frame_info *prev_frame = NULL;
166 struct frame_id frame_id;
167 PyObject *internal = NULL;
168 int internal_bp = 0;
169 CORE_ADDR finish_pc, pc;
170 volatile struct gdb_exception except;
171 char *addr_str, small_buf[100];
172 struct symbol *function;
174 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "|OO", keywords,
175 &frame_obj, &internal))
176 return -1;
178 TRY_CATCH (except, RETURN_MASK_ALL)
180 /* Default frame to newest frame if necessary. */
181 if (frame_obj == NULL)
182 frame = get_current_frame ();
183 else
184 frame = frame_object_to_frame_info (frame_obj);
186 if (frame == NULL)
188 PyErr_SetString (PyExc_ValueError,
189 _("Invalid ID for the `frame' object."));
191 else
193 prev_frame = get_prev_frame (frame);
194 if (prev_frame == 0)
196 PyErr_SetString (PyExc_ValueError,
197 _("\"FinishBreakpoint\" not "
198 "meaningful in the outermost "
199 "frame."));
201 else if (get_frame_type (prev_frame) == DUMMY_FRAME)
203 PyErr_SetString (PyExc_ValueError,
204 _("\"FinishBreakpoint\" cannot "
205 "be set on a dummy frame."));
207 else
209 frame_id = get_frame_id (prev_frame);
210 if (frame_id_eq (frame_id, null_frame_id))
211 PyErr_SetString (PyExc_ValueError,
212 _("Invalid ID for the `frame' object."));
216 if (except.reason < 0)
218 gdbpy_convert_exception (except);
219 return -1;
221 else if (PyErr_Occurred ())
222 return -1;
224 thread = pid_to_thread_id (inferior_ptid);
225 if (thread == 0)
227 PyErr_SetString (PyExc_ValueError,
228 _("No thread currently selected."));
229 return -1;
232 if (internal)
234 internal_bp = PyObject_IsTrue (internal);
235 if (internal_bp == -1)
237 PyErr_SetString (PyExc_ValueError,
238 _("The value of `internal' must be a boolean."));
239 return -1;
243 /* Find the function we will return from. */
244 self_bpfinish->return_type = NULL;
245 self_bpfinish->function_value = NULL;
247 TRY_CATCH (except, RETURN_MASK_ALL)
249 if (get_frame_pc_if_available (frame, &pc))
251 function = find_pc_function (pc);
252 if (function != NULL)
254 struct type *ret_type =
255 TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
257 /* Remember only non-void return types. */
258 if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
260 struct value *func_value;
262 /* Ignore Python errors at this stage. */
263 self_bpfinish->return_type = type_to_type_object (ret_type);
264 PyErr_Clear ();
265 func_value = read_var_value (function, frame);
266 self_bpfinish->function_value =
267 value_to_value_object (func_value);
268 PyErr_Clear ();
273 if (except.reason < 0
274 || !self_bpfinish->return_type || !self_bpfinish->function_value)
276 /* Won't be able to compute return value. */
277 Py_XDECREF (self_bpfinish->return_type);
278 Py_XDECREF (self_bpfinish->function_value);
280 self_bpfinish->return_type = NULL;
281 self_bpfinish->function_value = NULL;
284 bppy_pending_object = &self_bpfinish->py_bp;
285 bppy_pending_object->number = -1;
286 bppy_pending_object->bp = NULL;
288 TRY_CATCH (except, RETURN_MASK_ALL)
290 /* Set a breakpoint on the return address. */
291 finish_pc = get_frame_pc (prev_frame);
292 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (finish_pc));
293 addr_str = small_buf;
295 create_breakpoint (python_gdbarch,
296 addr_str, NULL, thread, NULL,
298 1 /*temp_flag*/,
299 bp_breakpoint,
301 AUTO_BOOLEAN_TRUE,
302 &bkpt_breakpoint_ops,
303 0, 1, internal_bp, 0);
305 GDB_PY_SET_HANDLE_EXCEPTION (except);
307 self_bpfinish->py_bp.bp->frame_id = frame_id;
308 self_bpfinish->py_bp.is_finish_bp = 1;
310 /* Bind the breakpoint with the current program space. */
311 self_bpfinish->py_bp.bp->pspace = current_program_space;
313 return 0;
316 /* Called when GDB notices that the finish breakpoint BP_OBJ is out of
317 the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
318 then delete the breakpoint. */
320 static void
321 bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
323 gdbpy_breakpoint_object *bp_obj = (gdbpy_breakpoint_object *) bpfinish_obj;
324 PyObject *py_obj = (PyObject *) bp_obj;
326 if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
327 && PyObject_HasAttrString (py_obj, outofscope_func))
329 PyObject *meth_result;
331 meth_result = PyObject_CallMethod (py_obj, outofscope_func, NULL);
332 if (meth_result == NULL)
333 gdbpy_print_stack ();
334 Py_XDECREF (meth_result);
337 delete_breakpoint (bpfinish_obj->py_bp.bp);
340 /* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
341 `B->out_of_scope' function if B is a FinishBreakpoint out of its scope. */
343 static int
344 bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
346 volatile struct gdb_exception except;
347 struct breakpoint *bp_stopped = (struct breakpoint *) args;
348 PyObject *py_bp = (PyObject *) b->py_bp_object;
349 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
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)
355 struct finish_breakpoint_object *finish_bp =
356 (struct finish_breakpoint_object *) py_bp;
358 /* Check scope if not currently stopped at the FinishBreakpoint. */
359 if (b != bp_stopped)
361 TRY_CATCH (except, RETURN_MASK_ALL)
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);
368 if (except.reason < 0)
370 gdbpy_convert_exception (except);
371 gdbpy_print_stack ();
376 return 0;
379 /* Attached to `stop' notifications, check if the execution has run
380 out of the scope of any FinishBreakpoint before it has been hit. */
382 static void
383 bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
385 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
386 current_language);
388 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
389 bs == NULL ? NULL : bs->breakpoint_at);
391 do_cleanups (cleanup);
394 /* Attached to `exit' notifications, triggers all the necessary out of
395 scope notifications. */
397 static void
398 bpfinishpy_handle_exit (struct inferior *inf)
400 struct cleanup *cleanup = ensure_python_env (target_gdbarch (),
401 current_language);
403 iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
405 do_cleanups (cleanup);
408 /* Initialize the Python finish breakpoint code. */
411 gdbpy_initialize_finishbreakpoints (void)
413 if (PyType_Ready (&finish_breakpoint_object_type) < 0)
414 return -1;
416 if (gdb_pymodule_addobject (gdb_module, "FinishBreakpoint",
417 (PyObject *) &finish_breakpoint_object_type) < 0)
418 return -1;
420 observer_attach_normal_stop (bpfinishpy_handle_stop);
421 observer_attach_inferior_exit (bpfinishpy_handle_exit);
423 return 0;
426 static PyGetSetDef finish_breakpoint_object_getset[] = {
427 { "return_value", bpfinishpy_get_returnvalue, NULL,
428 "gdb.Value object representing the return value, if any. \
429 None otherwise.", NULL },
430 { NULL } /* Sentinel. */
433 static PyTypeObject finish_breakpoint_object_type =
435 PyVarObject_HEAD_INIT (NULL, 0)
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 */