1 /* Python interface to stack frames
3 Copyright (C) 2008-2012 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/>. */
24 #include "exceptions.h"
28 #include "python-internal.h"
34 struct frame_id frame_id
;
35 struct gdbarch
*gdbarch
;
37 /* Marks that the FRAME_ID member actually holds the ID of the frame next
38 to this, and not this frames' ID itself. This is a hack to permit Python
39 frame objects which represent invalid frames (i.e., the last frame_info
40 in a corrupt stack). The problem arises from the fact that this code
41 relies on FRAME_ID to uniquely identify a frame, which is not always true
42 for the last "frame" in a corrupt stack (it can have a null ID, or the same
43 ID as the previous frame). Whenever get_prev_frame returns NULL, we
44 record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */
48 /* Require a valid frame. This must be called inside a TRY_CATCH, or
49 another context in which a gdb exception is allowed. */
50 #define FRAPY_REQUIRE_VALID(frame_obj, frame) \
52 frame = frame_object_to_frame_info (frame_obj); \
54 error (_("Frame is invalid.")); \
57 /* Returns the frame_info object corresponding to the given Python Frame
58 object. If the frame doesn't exist anymore (the frame id doesn't
59 correspond to any frame in the inferior), returns NULL. */
62 frame_object_to_frame_info (PyObject
*obj
)
64 frame_object
*frame_obj
= (frame_object
*) obj
;
65 struct frame_info
*frame
;
67 frame
= frame_find_by_id (frame_obj
->frame_id
);
71 if (frame_obj
->frame_id_is_next
)
72 frame
= get_prev_frame (frame
);
77 /* Called by the Python interpreter to obtain string representation
81 frapy_str (PyObject
*self
)
85 struct ui_file
*strfile
;
87 strfile
= mem_fileopen ();
88 fprint_frame_id (strfile
, ((frame_object
*) self
)->frame_id
);
89 s
= ui_file_xstrdup (strfile
, NULL
);
90 result
= PyString_FromString (s
);
96 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
97 Returns True if the frame corresponding to the frame_id of this
98 object still exists in the inferior. */
101 frapy_is_valid (PyObject
*self
, PyObject
*args
)
103 struct frame_info
*frame
= NULL
;
104 volatile struct gdb_exception except
;
106 TRY_CATCH (except
, RETURN_MASK_ALL
)
108 frame
= frame_object_to_frame_info (self
);
110 GDB_PY_HANDLE_EXCEPTION (except
);
118 /* Implementation of gdb.Frame.name (self) -> String.
119 Returns the name of the function corresponding to this frame. */
122 frapy_name (PyObject
*self
, PyObject
*args
)
124 struct frame_info
*frame
;
128 volatile struct gdb_exception except
;
130 TRY_CATCH (except
, RETURN_MASK_ALL
)
132 FRAPY_REQUIRE_VALID (self
, frame
);
134 find_frame_funname (frame
, &name
, &lang
, NULL
);
136 GDB_PY_HANDLE_EXCEPTION (except
);
139 result
= PyUnicode_Decode (name
, strlen (name
), host_charset (), NULL
);
149 /* Implementation of gdb.Frame.type (self) -> Integer.
150 Returns the frame type, namely one of the gdb.*_FRAME constants. */
153 frapy_type (PyObject
*self
, PyObject
*args
)
155 struct frame_info
*frame
;
156 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
157 volatile struct gdb_exception except
;
159 TRY_CATCH (except
, RETURN_MASK_ALL
)
161 FRAPY_REQUIRE_VALID (self
, frame
);
163 type
= get_frame_type (frame
);
165 GDB_PY_HANDLE_EXCEPTION (except
);
167 return PyInt_FromLong (type
);
170 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
171 Returns one of the gdb.FRAME_UNWIND_* constants. */
174 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
176 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
177 volatile struct gdb_exception except
;
178 enum unwind_stop_reason stop_reason
;
180 TRY_CATCH (except
, RETURN_MASK_ALL
)
182 FRAPY_REQUIRE_VALID (self
, frame
);
184 GDB_PY_HANDLE_EXCEPTION (except
);
186 stop_reason
= get_frame_unwind_stop_reason (frame
);
188 return PyInt_FromLong (stop_reason
);
191 /* Implementation of gdb.Frame.pc (self) -> Long.
192 Returns the frame's resume address. */
195 frapy_pc (PyObject
*self
, PyObject
*args
)
197 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
198 struct frame_info
*frame
;
199 volatile struct gdb_exception except
;
201 TRY_CATCH (except
, RETURN_MASK_ALL
)
203 FRAPY_REQUIRE_VALID (self
, frame
);
205 pc
= get_frame_pc (frame
);
207 GDB_PY_HANDLE_EXCEPTION (except
);
209 return gdb_py_long_from_ulongest (pc
);
212 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
213 Returns the frame's code block. */
216 frapy_block (PyObject
*self
, PyObject
*args
)
218 struct frame_info
*frame
;
219 struct block
*block
= NULL
, *fn_block
;
220 volatile struct gdb_exception except
;
222 TRY_CATCH (except
, RETURN_MASK_ALL
)
224 FRAPY_REQUIRE_VALID (self
, frame
);
225 block
= get_frame_block (frame
, NULL
);
227 GDB_PY_HANDLE_EXCEPTION (except
);
229 for (fn_block
= block
;
230 fn_block
!= NULL
&& BLOCK_FUNCTION (fn_block
) == NULL
;
231 fn_block
= BLOCK_SUPERBLOCK (fn_block
))
234 if (block
== NULL
|| fn_block
== NULL
|| BLOCK_FUNCTION (fn_block
) == NULL
)
236 PyErr_SetString (PyExc_RuntimeError
,
237 _("Cannot locate object file for block."));
245 symt
= SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block
));
246 return block_to_block_object (block
, symt
->objfile
);
253 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
254 Returns the symbol for the function corresponding to this frame. */
257 frapy_function (PyObject
*self
, PyObject
*args
)
259 struct symbol
*sym
= NULL
;
260 struct frame_info
*frame
;
261 volatile struct gdb_exception except
;
263 TRY_CATCH (except
, RETURN_MASK_ALL
)
265 FRAPY_REQUIRE_VALID (self
, frame
);
267 sym
= find_pc_function (get_frame_address_in_block (frame
));
269 GDB_PY_HANDLE_EXCEPTION (except
);
272 return symbol_to_symbol_object (sym
);
277 /* Convert a frame_info struct to a Python Frame object.
278 Sets a Python exception and returns NULL on error. */
281 frame_info_to_frame_object (struct frame_info
*frame
)
283 frame_object
*frame_obj
;
284 volatile struct gdb_exception except
;
286 frame_obj
= PyObject_New (frame_object
, &frame_object_type
);
287 if (frame_obj
== NULL
)
289 PyErr_SetString (PyExc_MemoryError
,
290 _("Could not allocate frame object."));
294 TRY_CATCH (except
, RETURN_MASK_ALL
)
297 /* Try to get the previous frame, to determine if this is the last frame
298 in a corrupt stack. If so, we need to store the frame_id of the next
299 frame and not of this one (which is possibly invalid). */
300 if (get_prev_frame (frame
) == NULL
301 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
302 && get_next_frame (frame
) != NULL
)
304 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
305 frame_obj
->frame_id_is_next
= 1;
309 frame_obj
->frame_id
= get_frame_id (frame
);
310 frame_obj
->frame_id_is_next
= 0;
312 frame_obj
->gdbarch
= get_frame_arch (frame
);
314 GDB_PY_HANDLE_EXCEPTION (except
);
316 return (PyObject
*) frame_obj
;
319 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
320 Returns the frame immediately older (outer) to this frame, or None if
324 frapy_older (PyObject
*self
, PyObject
*args
)
326 struct frame_info
*frame
, *prev
;
327 volatile struct gdb_exception except
;
328 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
330 TRY_CATCH (except
, RETURN_MASK_ALL
)
332 FRAPY_REQUIRE_VALID (self
, frame
);
334 prev
= get_prev_frame (frame
);
336 prev_obj
= (PyObject
*) frame_info_to_frame_object (prev
);
343 GDB_PY_HANDLE_EXCEPTION (except
);
348 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
349 Returns the frame immediately newer (inner) to this frame, or None if
353 frapy_newer (PyObject
*self
, PyObject
*args
)
355 struct frame_info
*frame
, *next
;
356 volatile struct gdb_exception except
;
357 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
359 TRY_CATCH (except
, RETURN_MASK_ALL
)
361 FRAPY_REQUIRE_VALID (self
, frame
);
363 next
= get_next_frame (frame
);
365 next_obj
= (PyObject
*) frame_info_to_frame_object (next
);
372 GDB_PY_HANDLE_EXCEPTION (except
);
377 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
378 Returns the frame's symtab and line. */
381 frapy_find_sal (PyObject
*self
, PyObject
*args
)
383 struct frame_info
*frame
;
384 struct symtab_and_line sal
;
385 volatile struct gdb_exception except
;
386 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
388 TRY_CATCH (except
, RETURN_MASK_ALL
)
390 FRAPY_REQUIRE_VALID (self
, frame
);
392 find_frame_sal (frame
, &sal
);
393 sal_obj
= symtab_and_line_to_sal_object (sal
);
395 GDB_PY_HANDLE_EXCEPTION (except
);
400 /* Implementation of gdb.Frame.read_var_value (self, variable,
401 [block]) -> gdb.Value. If the optional block argument is provided
402 start the search from that block, otherwise search from the frame's
403 current block (determined by examining the resume address of the
404 frame). The variable argument must be a string or an instance of a
405 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
406 NULL on error, with a python exception set. */
408 frapy_read_var (PyObject
*self
, PyObject
*args
)
410 struct frame_info
*frame
;
411 PyObject
*sym_obj
, *block_obj
= NULL
;
412 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
413 struct value
*val
= NULL
;
414 volatile struct gdb_exception except
;
416 if (!PyArg_ParseTuple (args
, "O|O", &sym_obj
, &block_obj
))
419 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
420 var
= symbol_object_to_symbol (sym_obj
);
421 else if (gdbpy_is_string (sym_obj
))
424 const struct block
*block
= NULL
;
425 struct cleanup
*cleanup
;
426 volatile struct gdb_exception except
;
428 var_name
= python_string_to_target_string (sym_obj
);
431 cleanup
= make_cleanup (xfree
, var_name
);
435 block
= block_object_to_block (block_obj
);
438 PyErr_SetString (PyExc_RuntimeError
,
439 _("Second argument must be block."));
444 TRY_CATCH (except
, RETURN_MASK_ALL
)
446 FRAPY_REQUIRE_VALID (self
, frame
);
449 block
= get_frame_block (frame
, NULL
);
450 var
= lookup_symbol (var_name
, block
, VAR_DOMAIN
, NULL
);
452 GDB_PY_HANDLE_EXCEPTION (except
);
456 PyErr_Format (PyExc_ValueError
,
457 _("Variable '%s' not found."), var_name
);
458 do_cleanups (cleanup
);
463 do_cleanups (cleanup
);
467 PyErr_SetString (PyExc_TypeError
,
468 _("Argument must be a symbol or string."));
472 TRY_CATCH (except
, RETURN_MASK_ALL
)
474 FRAPY_REQUIRE_VALID (self
, frame
);
476 val
= read_var_value (var
, frame
);
478 GDB_PY_HANDLE_EXCEPTION (except
);
480 return value_to_value_object (val
);
483 /* Select this frame. */
486 frapy_select (PyObject
*self
, PyObject
*args
)
488 struct frame_info
*fi
;
489 volatile struct gdb_exception except
;
491 TRY_CATCH (except
, RETURN_MASK_ALL
)
493 FRAPY_REQUIRE_VALID (self
, fi
);
497 GDB_PY_HANDLE_EXCEPTION (except
);
502 /* Implementation of gdb.newest_frame () -> gdb.Frame.
503 Returns the newest frame object. */
506 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
508 struct frame_info
*frame
;
509 PyObject
*frame_obj
= NULL
; /* Initialize to appease gcc warning. */
510 volatile struct gdb_exception except
;
512 TRY_CATCH (except
, RETURN_MASK_ALL
)
514 frame
= get_current_frame ();
515 frame_obj
= frame_info_to_frame_object (frame
);
517 GDB_PY_HANDLE_EXCEPTION (except
);
522 /* Implementation of gdb.selected_frame () -> gdb.Frame.
523 Returns the selected frame object. */
526 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
528 struct frame_info
*frame
;
529 PyObject
*frame_obj
= NULL
; /* Initialize to appease gcc warning. */
530 volatile struct gdb_exception except
;
532 TRY_CATCH (except
, RETURN_MASK_ALL
)
534 frame
= get_selected_frame ("No frame is currently selected.");
535 frame_obj
= frame_info_to_frame_object (frame
);
537 GDB_PY_HANDLE_EXCEPTION (except
);
542 /* Implementation of gdb.stop_reason_string (Integer) -> String.
543 Return a string explaining the unwind stop reason. */
546 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
551 if (!PyArg_ParseTuple (args
, "i", &reason
))
554 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
556 PyErr_SetString (PyExc_ValueError
,
557 _("Invalid frame stop reason."));
561 str
= frame_stop_reason_string (reason
);
562 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
565 /* Implements the equality comparison for Frame objects.
566 All other comparison operators will throw a TypeError Python exception,
567 as they aren't valid for frames. */
570 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
574 if (!PyObject_TypeCheck (other
, &frame_object_type
)
575 || (op
!= Py_EQ
&& op
!= Py_NE
))
577 Py_INCREF (Py_NotImplemented
);
578 return Py_NotImplemented
;
581 if (frame_id_eq (((frame_object
*) self
)->frame_id
,
582 ((frame_object
*) other
)->frame_id
))
592 /* Sets up the Frame API in the gdb module. */
595 gdbpy_initialize_frames (void)
597 frame_object_type
.tp_new
= PyType_GenericNew
;
598 if (PyType_Ready (&frame_object_type
) < 0)
601 /* Note: These would probably be best exposed as class attributes of
602 Frame, but I don't know how to do it except by messing with the
603 type's dictionary. That seems too messy. */
604 PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
);
605 PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
);
606 PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
);
607 PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME", TAILCALL_FRAME
);
608 PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME", SIGTRAMP_FRAME
);
609 PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
);
610 PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME", SENTINEL_FRAME
);
612 #define SET(name, description) \
613 PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
614 #define FIRST_ERROR(name) \
615 PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
616 #include "unwind_stop_reasons.def"
619 Py_INCREF (&frame_object_type
);
620 PyModule_AddObject (gdb_module
, "Frame", (PyObject
*) &frame_object_type
);
625 static PyMethodDef frame_object_methods
[] = {
626 { "is_valid", frapy_is_valid
, METH_NOARGS
,
627 "is_valid () -> Boolean.\n\
628 Return true if this frame is valid, false if not." },
629 { "name", frapy_name
, METH_NOARGS
,
630 "name () -> String.\n\
631 Return the function name of the frame, or None if it can't be determined." },
632 { "type", frapy_type
, METH_NOARGS
,
633 "type () -> Integer.\n\
634 Return the type of the frame." },
635 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
636 "unwind_stop_reason () -> Integer.\n\
637 Return the reason why it's not possible to find frames older than this." },
638 { "pc", frapy_pc
, METH_NOARGS
,
640 Return the frame's resume address." },
641 { "block", frapy_block
, METH_NOARGS
,
642 "block () -> gdb.Block.\n\
643 Return the frame's code block." },
644 { "function", frapy_function
, METH_NOARGS
,
645 "function () -> gdb.Symbol.\n\
646 Returns the symbol for the function corresponding to this frame." },
647 { "older", frapy_older
, METH_NOARGS
,
648 "older () -> gdb.Frame.\n\
649 Return the frame that called this frame." },
650 { "newer", frapy_newer
, METH_NOARGS
,
651 "newer () -> gdb.Frame.\n\
652 Return the frame called by this frame." },
653 { "find_sal", frapy_find_sal
, METH_NOARGS
,
654 "find_sal () -> gdb.Symtab_and_line.\n\
655 Return the frame's symtab and line." },
656 { "read_var", frapy_read_var
, METH_VARARGS
,
657 "read_var (variable) -> gdb.Value.\n\
658 Return the value of the variable in this frame." },
659 { "select", frapy_select
, METH_NOARGS
,
660 "Select this frame as the user's current frame." },
661 {NULL
} /* Sentinel */
664 PyTypeObject frame_object_type
= {
665 PyObject_HEAD_INIT (NULL
)
667 "gdb.Frame", /* tp_name */
668 sizeof (frame_object
), /* tp_basicsize */
676 0, /* tp_as_number */
677 0, /* tp_as_sequence */
678 0, /* tp_as_mapping */
681 frapy_str
, /* tp_str */
684 0, /* tp_as_buffer */
685 Py_TPFLAGS_DEFAULT
, /* tp_flags */
686 "GDB frame object", /* tp_doc */
689 frapy_richcompare
, /* tp_richcompare */
690 0, /* tp_weaklistoffset */
693 frame_object_methods
, /* tp_methods */
698 0, /* tp_descr_get */
699 0, /* tp_descr_set */
700 0, /* tp_dictoffset */