1 /* Python interface to stack frames
3 Copyright (C) 2008-2019 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/>. */
27 #include "python-internal.h"
30 #include "user-regs.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 fprint_frame_id (&strfile, ((frame_object *) self)->frame_id);
86 return PyString_FromString (strfile.c_str ());
89 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
90 Returns True if the frame corresponding to the frame_id of this
91 object still exists in the inferior. */
94 frapy_is_valid (PyObject *self, PyObject *args)
96 struct frame_info *frame = NULL;
100 frame = frame_object_to_frame_info (self);
102 CATCH (except, RETURN_MASK_ALL)
104 GDB_PY_HANDLE_EXCEPTION (except);
114 /* Implementation of gdb.Frame.name (self) -> String.
115 Returns the name of the function corresponding to this frame. */
118 frapy_name (PyObject *self, PyObject *args)
120 struct frame_info *frame;
121 gdb::unique_xmalloc_ptr<char> name;
127 FRAPY_REQUIRE_VALID (self, frame);
129 name = find_frame_funname (frame, &lang, NULL);
131 CATCH (except, RETURN_MASK_ALL)
133 GDB_PY_HANDLE_EXCEPTION (except);
139 result = PyUnicode_Decode (name.get (), strlen (name.get ()),
140 host_charset (), NULL);
151 /* Implementation of gdb.Frame.type (self) -> Integer.
152 Returns the frame type, namely one of the gdb.*_FRAME constants. */
155 frapy_type (PyObject *self, PyObject *args)
157 struct frame_info *frame;
158 enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning. */
162 FRAPY_REQUIRE_VALID (self, frame);
164 type = get_frame_type (frame);
166 CATCH (except, RETURN_MASK_ALL)
168 GDB_PY_HANDLE_EXCEPTION (except);
172 return PyInt_FromLong (type);
175 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
176 Returns the frame's architecture as a gdb.Architecture object. */
179 frapy_arch (PyObject *self, PyObject *args)
181 struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */
182 frame_object *obj = (frame_object *) self;
186 FRAPY_REQUIRE_VALID (self, frame);
188 CATCH (except, RETURN_MASK_ALL)
190 GDB_PY_HANDLE_EXCEPTION (except);
194 return gdbarch_to_arch_object (obj->gdbarch);
197 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
198 Returns one of the gdb.FRAME_UNWIND_* constants. */
201 frapy_unwind_stop_reason (PyObject *self, PyObject *args)
203 struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */
204 enum unwind_stop_reason stop_reason;
208 FRAPY_REQUIRE_VALID (self, frame);
210 CATCH (except, RETURN_MASK_ALL)
212 GDB_PY_HANDLE_EXCEPTION (except);
216 stop_reason = get_frame_unwind_stop_reason (frame);
218 return PyInt_FromLong (stop_reason);
221 /* Implementation of gdb.Frame.pc (self) -> Long.
222 Returns the frame's resume address. */
225 frapy_pc (PyObject *self, PyObject *args)
227 CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */
228 struct frame_info *frame;
232 FRAPY_REQUIRE_VALID (self, frame);
234 pc = get_frame_pc (frame);
236 CATCH (except, RETURN_MASK_ALL)
238 GDB_PY_HANDLE_EXCEPTION (except);
242 return gdb_py_long_from_ulongest (pc);
245 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
246 Returns the value of a register in this frame. */
249 frapy_read_register (PyObject *self, PyObject *args)
251 const char *regnum_str;
252 struct value *val = NULL;
254 if (!PyArg_ParseTuple (args, "s", ®num_str))
259 struct frame_info *frame;
262 FRAPY_REQUIRE_VALID (self, frame);
264 regnum = user_reg_map_name_to_regnum (get_frame_arch (frame),
266 strlen (regnum_str));
268 val = value_of_register (regnum, frame);
271 PyErr_SetString (PyExc_ValueError, _("Unknown register."));
273 CATCH (except, RETURN_MASK_ALL)
275 GDB_PY_HANDLE_EXCEPTION (except);
279 return val == NULL ? NULL : value_to_value_object (val);
282 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
283 Returns the frame's code block. */
286 frapy_block (PyObject *self, PyObject *args)
288 struct frame_info *frame;
289 const struct block *block = NULL, *fn_block;
293 FRAPY_REQUIRE_VALID (self, frame);
294 block = get_frame_block (frame, NULL);
296 CATCH (except, RETURN_MASK_ALL)
298 GDB_PY_HANDLE_EXCEPTION (except);
302 for (fn_block = block;
303 fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL;
304 fn_block = BLOCK_SUPERBLOCK (fn_block))
307 if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL)
309 PyErr_SetString (PyExc_RuntimeError,
310 _("Cannot locate block for frame."));
316 return block_to_block_object
317 (block, symbol_objfile (BLOCK_FUNCTION (fn_block)));
324 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
325 Returns the symbol for the function corresponding to this frame. */
328 frapy_function (PyObject *self, PyObject *args)
330 struct symbol *sym = NULL;
331 struct frame_info *frame;
335 enum language funlang;
337 FRAPY_REQUIRE_VALID (self, frame);
339 gdb::unique_xmalloc_ptr<char> funname
340 = find_frame_funname (frame, &funlang, &sym);
342 CATCH (except, RETURN_MASK_ALL)
344 GDB_PY_HANDLE_EXCEPTION (except);
349 return symbol_to_symbol_object (sym);
354 /* Convert a frame_info struct to a Python Frame object.
355 Sets a Python exception and returns NULL on error. */
358 frame_info_to_frame_object (struct frame_info *frame)
360 gdbpy_ref<frame_object> frame_obj (PyObject_New (frame_object,
361 &frame_object_type));
362 if (frame_obj == NULL)
368 /* Try to get the previous frame, to determine if this is the last frame
369 in a corrupt stack. If so, we need to store the frame_id of the next
370 frame and not of this one (which is possibly invalid). */
371 if (get_prev_frame (frame) == NULL
372 && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
373 && get_next_frame (frame) != NULL)
375 frame_obj->frame_id = get_frame_id (get_next_frame (frame));
376 frame_obj->frame_id_is_next = 1;
380 frame_obj->frame_id = get_frame_id (frame);
381 frame_obj->frame_id_is_next = 0;
383 frame_obj->gdbarch = get_frame_arch (frame);
385 CATCH (except, RETURN_MASK_ALL)
387 gdbpy_convert_exception (except);
392 return (PyObject *) frame_obj.release ();
395 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
396 Returns the frame immediately older (outer) to this frame, or None if
400 frapy_older (PyObject *self, PyObject *args)
402 struct frame_info *frame, *prev = NULL;
403 PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */
407 FRAPY_REQUIRE_VALID (self, frame);
409 prev = get_prev_frame (frame);
411 CATCH (except, RETURN_MASK_ALL)
413 GDB_PY_HANDLE_EXCEPTION (except);
418 prev_obj = frame_info_to_frame_object (prev);
428 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
429 Returns the frame immediately newer (inner) to this frame, or None if
433 frapy_newer (PyObject *self, PyObject *args)
435 struct frame_info *frame, *next = NULL;
436 PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */
440 FRAPY_REQUIRE_VALID (self, frame);
442 next = get_next_frame (frame);
444 CATCH (except, RETURN_MASK_ALL)
446 GDB_PY_HANDLE_EXCEPTION (except);
451 next_obj = frame_info_to_frame_object (next);
461 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
462 Returns the frame's symtab and line. */
465 frapy_find_sal (PyObject *self, PyObject *args)
467 struct frame_info *frame;
468 PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */
472 FRAPY_REQUIRE_VALID (self, frame);
474 symtab_and_line sal = find_frame_sal (frame);
475 sal_obj = symtab_and_line_to_sal_object (sal);
477 CATCH (except, RETURN_MASK_ALL)
479 GDB_PY_HANDLE_EXCEPTION (except);
486 /* Implementation of gdb.Frame.read_var_value (self, variable,
487 [block]) -> gdb.Value. If the optional block argument is provided
488 start the search from that block, otherwise search from the frame's
489 current block (determined by examining the resume address of the
490 frame). The variable argument must be a string or an instance of a
491 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
492 NULL on error, with a python exception set. */
494 frapy_read_var (PyObject *self, PyObject *args)
496 struct frame_info *frame;
497 PyObject *sym_obj, *block_obj = NULL;
498 struct symbol *var = NULL; /* gcc-4.3.2 false warning. */
499 const struct block *block = NULL;
500 struct value *val = NULL;
502 if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj))
505 if (PyObject_TypeCheck (sym_obj, &symbol_object_type))
506 var = symbol_object_to_symbol (sym_obj);
507 else if (gdbpy_is_string (sym_obj))
509 gdb::unique_xmalloc_ptr<char>
510 var_name (python_string_to_target_string (sym_obj));
517 block = block_object_to_block (block_obj);
520 PyErr_SetString (PyExc_RuntimeError,
521 _("Second argument must be block."));
528 struct block_symbol lookup_sym;
529 FRAPY_REQUIRE_VALID (self, frame);
532 block = get_frame_block (frame, NULL);
533 lookup_sym = lookup_symbol (var_name.get (), block, VAR_DOMAIN, NULL);
534 var = lookup_sym.symbol;
535 block = lookup_sym.block;
537 CATCH (except, RETURN_MASK_ALL)
539 gdbpy_convert_exception (except);
546 PyErr_Format (PyExc_ValueError,
547 _("Variable '%s' not found."), var_name.get ());
554 PyErr_SetString (PyExc_TypeError,
555 _("Argument must be a symbol or string."));
561 FRAPY_REQUIRE_VALID (self, frame);
563 val = read_var_value (var, block, frame);
565 CATCH (except, RETURN_MASK_ALL)
567 GDB_PY_HANDLE_EXCEPTION (except);
571 return value_to_value_object (val);
574 /* Select this frame. */
577 frapy_select (PyObject *self, PyObject *args)
579 struct frame_info *fi;
583 FRAPY_REQUIRE_VALID (self, fi);
587 CATCH (except, RETURN_MASK_ALL)
589 GDB_PY_HANDLE_EXCEPTION (except);
596 /* Implementation of gdb.newest_frame () -> gdb.Frame.
597 Returns the newest frame object. */
600 gdbpy_newest_frame (PyObject *self, PyObject *args)
602 struct frame_info *frame = NULL;
606 frame = get_current_frame ();
608 CATCH (except, RETURN_MASK_ALL)
610 GDB_PY_HANDLE_EXCEPTION (except);
614 return frame_info_to_frame_object (frame);
617 /* Implementation of gdb.selected_frame () -> gdb.Frame.
618 Returns the selected frame object. */
621 gdbpy_selected_frame (PyObject *self, PyObject *args)
623 struct frame_info *frame = NULL;
627 frame = get_selected_frame ("No frame is currently selected.");
629 CATCH (except, RETURN_MASK_ALL)
631 GDB_PY_HANDLE_EXCEPTION (except);
635 return frame_info_to_frame_object (frame);
638 /* Implementation of gdb.stop_reason_string (Integer) -> String.
639 Return a string explaining the unwind stop reason. */
642 gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
647 if (!PyArg_ParseTuple (args, "i", &reason))
650 if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
652 PyErr_SetString (PyExc_ValueError,
653 _("Invalid frame stop reason."));
657 str = unwind_stop_reason_to_string ((enum unwind_stop_reason) reason);
658 return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
661 /* Implements the equality comparison for Frame objects.
662 All other comparison operators will throw a TypeError Python exception,
663 as they aren't valid for frames. */
666 frapy_richcompare (PyObject *self, PyObject *other, int op)
670 if (!PyObject_TypeCheck (other, &frame_object_type)
671 || (op != Py_EQ && op != Py_NE))
673 Py_INCREF (Py_NotImplemented);
674 return Py_NotImplemented;
677 if (frame_id_eq (((frame_object *) self)->frame_id,
678 ((frame_object *) other)->frame_id))
688 /* Sets up the Frame API in the gdb module. */
691 gdbpy_initialize_frames (void)
693 frame_object_type.tp_new = PyType_GenericNew;
694 if (PyType_Ready (&frame_object_type) < 0)
697 /* Note: These would probably be best exposed as class attributes of
698 Frame, but I don't know how to do it except by messing with the
699 type's dictionary. That seems too messy. */
700 if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0
701 || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0
702 || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0
703 || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME",
705 || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME",
707 || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0
708 || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME",
712 #define SET(name, description) \
713 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
715 #include "unwind_stop_reasons.def"
718 return gdb_pymodule_addobject (gdb_module, "Frame",
719 (PyObject *) &frame_object_type);
724 static PyMethodDef frame_object_methods[] = {
725 { "is_valid", frapy_is_valid, METH_NOARGS,
726 "is_valid () -> Boolean.\n\
727 Return true if this frame is valid, false if not." },
728 { "name", frapy_name, METH_NOARGS,
729 "name () -> String.\n\
730 Return the function name of the frame, or None if it can't be determined." },
731 { "type", frapy_type, METH_NOARGS,
732 "type () -> Integer.\n\
733 Return the type of the frame." },
734 { "architecture", frapy_arch, METH_NOARGS,
735 "architecture () -> gdb.Architecture.\n\
736 Return the architecture of the frame." },
737 { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
738 "unwind_stop_reason () -> Integer.\n\
739 Return the reason why it's not possible to find frames older than this." },
740 { "pc", frapy_pc, METH_NOARGS,
742 Return the frame's resume address." },
743 { "read_register", frapy_read_register, METH_VARARGS,
744 "read_register (register_name) -> gdb.Value\n\
745 Return the value of the register in the frame." },
746 { "block", frapy_block, METH_NOARGS,
747 "block () -> gdb.Block.\n\
748 Return the frame's code block." },
749 { "function", frapy_function, METH_NOARGS,
750 "function () -> gdb.Symbol.\n\
751 Returns the symbol for the function corresponding to this frame." },
752 { "older", frapy_older, METH_NOARGS,
753 "older () -> gdb.Frame.\n\
754 Return the frame that called this frame." },
755 { "newer", frapy_newer, METH_NOARGS,
756 "newer () -> gdb.Frame.\n\
757 Return the frame called by this frame." },
758 { "find_sal", frapy_find_sal, METH_NOARGS,
759 "find_sal () -> gdb.Symtab_and_line.\n\
760 Return the frame's symtab and line." },
761 { "read_var", frapy_read_var, METH_VARARGS,
762 "read_var (variable) -> gdb.Value.\n\
763 Return the value of the variable in this frame." },
764 { "select", frapy_select, METH_NOARGS,
765 "Select this frame as the user's current frame." },
766 {NULL} /* Sentinel */
769 PyTypeObject frame_object_type = {
770 PyVarObject_HEAD_INIT (NULL, 0)
771 "gdb.Frame", /* tp_name */
772 sizeof (frame_object), /* tp_basicsize */
780 0, /* tp_as_number */
781 0, /* tp_as_sequence */
782 0, /* tp_as_mapping */
785 frapy_str, /* tp_str */
788 0, /* tp_as_buffer */
789 Py_TPFLAGS_DEFAULT, /* tp_flags */
790 "GDB frame object", /* tp_doc */
793 frapy_richcompare, /* tp_richcompare */
794 0, /* tp_weaklistoffset */
797 frame_object_methods, /* tp_methods */
802 0, /* tp_descr_get */
803 0, /* tp_descr_set */
804 0, /* tp_dictoffset */