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