1 /* Python interface to stack frames
3 Copyright (C) 2008-2024 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/>. */
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
;
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
)
83 const frame_id
&fid
= ((frame_object
*) self
)->frame_id
;
84 return PyUnicode_FromString (fid
.to_string ().c_str ());
87 /* Implement repr() for gdb.Frame. */
90 frapy_repr (PyObject
*self
)
92 frame_object
*frame_obj
= (frame_object
*) self
;
93 frame_info_ptr f_info
= frame_find_by_id (frame_obj
->frame_id
);
94 if (f_info
== nullptr)
95 return gdb_py_invalid_object_repr (self
);
97 const frame_id
&fid
= frame_obj
->frame_id
;
98 return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>",
99 Py_TYPE (self
)->tp_name
,
100 frame_relative_level (f_info
),
101 fid
.to_string ().c_str ());
104 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
105 Returns True if the frame corresponding to the frame_id of this
106 object still exists in the inferior. */
109 frapy_is_valid (PyObject
*self
, PyObject
*args
)
111 frame_info_ptr frame
= NULL
;
115 frame
= frame_object_to_frame_info (self
);
117 catch (const gdb_exception
&except
)
119 GDB_PY_HANDLE_EXCEPTION (except
);
128 /* Implementation of gdb.Frame.name (self) -> String.
129 Returns the name of the function corresponding to this frame. */
132 frapy_name (PyObject
*self
, PyObject
*args
)
134 frame_info_ptr frame
;
135 gdb::unique_xmalloc_ptr
<char> name
;
141 FRAPY_REQUIRE_VALID (self
, frame
);
143 name
= find_frame_funname (frame
, &lang
, NULL
);
145 catch (const gdb_exception
&except
)
147 GDB_PY_HANDLE_EXCEPTION (except
);
152 result
= PyUnicode_Decode (name
.get (), strlen (name
.get ()),
153 host_charset (), NULL
);
164 /* Implementation of gdb.Frame.type (self) -> Integer.
165 Returns the frame type, namely one of the gdb.*_FRAME constants. */
168 frapy_type (PyObject
*self
, PyObject
*args
)
170 frame_info_ptr frame
;
171 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
175 FRAPY_REQUIRE_VALID (self
, frame
);
177 type
= get_frame_type (frame
);
179 catch (const gdb_exception
&except
)
181 GDB_PY_HANDLE_EXCEPTION (except
);
184 return gdb_py_object_from_longest (type
).release ();
187 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
188 Returns the frame's architecture as a gdb.Architecture object. */
191 frapy_arch (PyObject
*self
, PyObject
*args
)
193 frame_info_ptr frame
= NULL
; /* Initialize to appease gcc warning. */
194 frame_object
*obj
= (frame_object
*) self
;
198 FRAPY_REQUIRE_VALID (self
, frame
);
200 catch (const gdb_exception
&except
)
202 GDB_PY_HANDLE_EXCEPTION (except
);
205 return gdbarch_to_arch_object (obj
->gdbarch
);
208 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
209 Returns one of the gdb.FRAME_UNWIND_* constants. */
212 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
214 frame_info_ptr frame
= NULL
; /* Initialize to appease gcc warning. */
215 enum unwind_stop_reason stop_reason
;
219 FRAPY_REQUIRE_VALID (self
, frame
);
221 catch (const gdb_exception
&except
)
223 GDB_PY_HANDLE_EXCEPTION (except
);
226 stop_reason
= get_frame_unwind_stop_reason (frame
);
228 return gdb_py_object_from_longest (stop_reason
).release ();
231 /* Implementation of gdb.Frame.pc (self) -> Long.
232 Returns the frame's resume address. */
235 frapy_pc (PyObject
*self
, PyObject
*args
)
237 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
238 frame_info_ptr frame
;
242 FRAPY_REQUIRE_VALID (self
, frame
);
244 pc
= get_frame_pc (frame
);
246 catch (const gdb_exception
&except
)
248 GDB_PY_HANDLE_EXCEPTION (except
);
251 return gdb_py_object_from_ulongest (pc
).release ();
254 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
255 Returns the value of a register in this frame. */
258 frapy_read_register (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
260 PyObject
*pyo_reg_id
;
261 PyObject
*result
= nullptr;
263 static const char *keywords
[] = { "register", nullptr };
264 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "O", keywords
, &pyo_reg_id
))
269 scoped_value_mark free_values
;
270 frame_info_ptr frame
;
273 FRAPY_REQUIRE_VALID (self
, frame
);
275 if (!gdbpy_parse_register_id (get_frame_arch (frame
), pyo_reg_id
,
279 gdb_assert (regnum
>= 0);
281 = value_of_register (regnum
, get_next_frame_sentinel_okay (frame
));
284 PyErr_SetString (PyExc_ValueError
, _("Can't read register."));
286 result
= value_to_value_object (val
);
288 catch (const gdb_exception
&except
)
290 GDB_PY_HANDLE_EXCEPTION (except
);
296 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
297 Returns the frame's code block. */
300 frapy_block (PyObject
*self
, PyObject
*args
)
302 frame_info_ptr frame
;
303 const struct block
*block
= NULL
, *fn_block
;
307 FRAPY_REQUIRE_VALID (self
, frame
);
308 block
= get_frame_block (frame
, NULL
);
310 catch (const gdb_exception
&except
)
312 GDB_PY_HANDLE_EXCEPTION (except
);
315 for (fn_block
= block
;
316 fn_block
!= NULL
&& fn_block
->function () == NULL
;
317 fn_block
= fn_block
->superblock ())
320 if (block
== NULL
|| fn_block
== NULL
|| fn_block
->function () == NULL
)
322 PyErr_SetString (PyExc_RuntimeError
,
323 _("Cannot locate block for frame."));
327 return block_to_block_object (block
, fn_block
->function ()->objfile ());
331 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
332 Returns the symbol for the function corresponding to this frame. */
335 frapy_function (PyObject
*self
, PyObject
*args
)
337 struct symbol
*sym
= NULL
;
338 frame_info_ptr frame
;
342 enum language funlang
;
344 FRAPY_REQUIRE_VALID (self
, frame
);
346 gdb::unique_xmalloc_ptr
<char> funname
347 = find_frame_funname (frame
, &funlang
, &sym
);
349 catch (const gdb_exception
&except
)
351 GDB_PY_HANDLE_EXCEPTION (except
);
355 return symbol_to_symbol_object (sym
);
360 /* Convert a frame_info struct to a Python Frame object.
361 Sets a Python exception and returns NULL on error. */
364 frame_info_to_frame_object (frame_info_ptr frame
)
366 gdbpy_ref
<frame_object
> frame_obj (PyObject_New (frame_object
,
367 &frame_object_type
));
368 if (frame_obj
== NULL
)
374 /* Try to get the previous frame, to determine if this is the last frame
375 in a corrupt stack. If so, we need to store the frame_id of the next
376 frame and not of this one (which is possibly invalid). */
377 if (get_prev_frame (frame
) == NULL
378 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
379 && get_next_frame (frame
) != NULL
)
381 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
382 frame_obj
->frame_id_is_next
= 1;
386 frame_obj
->frame_id
= get_frame_id (frame
);
387 frame_obj
->frame_id_is_next
= 0;
389 frame_obj
->gdbarch
= get_frame_arch (frame
);
391 catch (const gdb_exception
&except
)
393 gdbpy_convert_exception (except
);
397 return (PyObject
*) frame_obj
.release ();
400 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
401 Returns the frame immediately older (outer) to this frame, or None if
405 frapy_older (PyObject
*self
, PyObject
*args
)
407 frame_info_ptr frame
, prev
= NULL
;
408 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
412 FRAPY_REQUIRE_VALID (self
, frame
);
414 prev
= get_prev_frame (frame
);
416 catch (const gdb_exception
&except
)
418 GDB_PY_HANDLE_EXCEPTION (except
);
422 prev_obj
= frame_info_to_frame_object (prev
);
432 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
433 Returns the frame immediately newer (inner) to this frame, or None if
437 frapy_newer (PyObject
*self
, PyObject
*args
)
439 frame_info_ptr frame
, next
= NULL
;
440 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
444 FRAPY_REQUIRE_VALID (self
, frame
);
446 next
= get_next_frame (frame
);
448 catch (const gdb_exception
&except
)
450 GDB_PY_HANDLE_EXCEPTION (except
);
454 next_obj
= frame_info_to_frame_object (next
);
464 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
465 Returns the frame's symtab and line. */
468 frapy_find_sal (PyObject
*self
, PyObject
*args
)
470 frame_info_ptr frame
;
471 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
475 FRAPY_REQUIRE_VALID (self
, frame
);
477 symtab_and_line sal
= find_frame_sal (frame
);
478 sal_obj
= symtab_and_line_to_sal_object (sal
);
480 catch (const gdb_exception
&except
)
482 GDB_PY_HANDLE_EXCEPTION (except
);
488 /* Implementation of gdb.Frame.read_var_value (self, variable,
489 [block]) -> gdb.Value. If the optional block argument is provided
490 start the search from that block, otherwise search from the frame's
491 current block (determined by examining the resume address of the
492 frame). The variable argument must be a string or an instance of a
493 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
494 NULL on error, with a python exception set. */
496 frapy_read_var (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
498 frame_info_ptr frame
;
499 PyObject
*sym_obj
, *block_obj
= NULL
;
500 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
501 const struct block
*block
= NULL
;
503 static const char *keywords
[] = { "variable", "block", nullptr };
504 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "O|O!", keywords
,
505 &sym_obj
, &block_object_type
,
509 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
510 var
= symbol_object_to_symbol (sym_obj
);
511 else if (gdbpy_is_string (sym_obj
))
513 gdb::unique_xmalloc_ptr
<char>
514 var_name (python_string_to_target_string (sym_obj
));
519 if (block_obj
!= nullptr)
521 /* This call should only fail if the type of BLOCK_OBJ is wrong,
522 and we ensure the type is correct when we parse the arguments,
523 so we can just assert the return value is not nullptr. */
524 block
= block_object_to_block (block_obj
);
525 gdb_assert (block
!= nullptr);
530 struct block_symbol lookup_sym
;
531 FRAPY_REQUIRE_VALID (self
, frame
);
534 block
= get_frame_block (frame
, NULL
);
535 lookup_sym
= lookup_symbol (var_name
.get (), block
, VAR_DOMAIN
, NULL
);
536 var
= lookup_sym
.symbol
;
537 block
= lookup_sym
.block
;
539 catch (const gdb_exception
&except
)
541 gdbpy_convert_exception (except
);
547 PyErr_Format (PyExc_ValueError
,
548 _("Variable '%s' not found."), var_name
.get ());
555 PyErr_Format (PyExc_TypeError
,
556 _("argument 1 must be gdb.Symbol or str, not %s"),
557 Py_TYPE (sym_obj
)->tp_name
);
561 PyObject
*result
= nullptr;
564 FRAPY_REQUIRE_VALID (self
, frame
);
566 scoped_value_mark free_values
;
567 struct value
*val
= read_var_value (var
, block
, frame
);
568 result
= value_to_value_object (val
);
570 catch (const gdb_exception
&except
)
572 GDB_PY_HANDLE_EXCEPTION (except
);
578 /* Select this frame. */
581 frapy_select (PyObject
*self
, PyObject
*args
)
587 FRAPY_REQUIRE_VALID (self
, fi
);
591 catch (const gdb_exception
&except
)
593 GDB_PY_HANDLE_EXCEPTION (except
);
599 /* The stack frame level for this frame. */
602 frapy_level (PyObject
*self
, PyObject
*args
)
608 FRAPY_REQUIRE_VALID (self
, fi
);
610 return gdb_py_object_from_longest (frame_relative_level (fi
)).release ();
612 catch (const gdb_exception
&except
)
614 GDB_PY_HANDLE_EXCEPTION (except
);
620 /* The language for this frame. */
623 frapy_language (PyObject
*self
, PyObject
*args
)
628 FRAPY_REQUIRE_VALID (self
, fi
);
630 enum language lang
= get_frame_language (fi
);
631 const language_defn
*lang_def
= language_def (lang
);
633 return host_string_to_python_string (lang_def
->name ()).release ();
635 catch (const gdb_exception
&except
)
637 GDB_PY_HANDLE_EXCEPTION (except
);
643 /* The static link for this frame. */
646 frapy_static_link (PyObject
*self
, PyObject
*args
)
652 FRAPY_REQUIRE_VALID (self
, link
);
654 link
= frame_follow_static_link (link
);
656 catch (const gdb_exception
&except
)
658 GDB_PY_HANDLE_EXCEPTION (except
);
664 return frame_info_to_frame_object (link
);
667 /* Implementation of gdb.newest_frame () -> gdb.Frame.
668 Returns the newest frame object. */
671 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
673 frame_info_ptr frame
= NULL
;
677 frame
= get_current_frame ();
679 catch (const gdb_exception
&except
)
681 GDB_PY_HANDLE_EXCEPTION (except
);
684 return frame_info_to_frame_object (frame
);
687 /* Implementation of gdb.selected_frame () -> gdb.Frame.
688 Returns the selected frame object. */
691 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
693 frame_info_ptr frame
= NULL
;
697 frame
= get_selected_frame ("No frame is currently selected.");
699 catch (const gdb_exception
&except
)
701 GDB_PY_HANDLE_EXCEPTION (except
);
704 return frame_info_to_frame_object (frame
);
707 /* Implementation of gdb.stop_reason_string (Integer) -> String.
708 Return a string explaining the unwind stop reason. */
711 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
716 if (!PyArg_ParseTuple (args
, "i", &reason
))
719 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
721 PyErr_SetString (PyExc_ValueError
,
722 _("Invalid frame stop reason."));
726 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
727 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
730 /* Implements the equality comparison for Frame objects.
731 All other comparison operators will throw a TypeError Python exception,
732 as they aren't valid for frames. */
735 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
739 if (!PyObject_TypeCheck (other
, &frame_object_type
)
740 || (op
!= Py_EQ
&& op
!= Py_NE
))
742 Py_INCREF (Py_NotImplemented
);
743 return Py_NotImplemented
;
746 frame_object
*self_frame
= (frame_object
*) self
;
747 frame_object
*other_frame
= (frame_object
*) other
;
749 if (self_frame
->frame_id_is_next
== other_frame
->frame_id_is_next
750 && self_frame
->frame_id
== other_frame
->frame_id
)
760 /* Sets up the Frame API in the gdb module. */
762 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
763 gdbpy_initialize_frames (void)
765 frame_object_type
.tp_new
= PyType_GenericNew
;
766 if (PyType_Ready (&frame_object_type
) < 0)
769 /* Note: These would probably be best exposed as class attributes of
770 Frame, but I don't know how to do it except by messing with the
771 type's dictionary. That seems too messy. */
772 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
773 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
774 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
775 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
777 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
779 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
780 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
784 #define SET(name, description) \
785 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
787 #include "unwind_stop_reasons.def"
790 return gdb_pymodule_addobject (gdb_module
, "Frame",
791 (PyObject
*) &frame_object_type
);
794 GDBPY_INITIALIZE_FILE (gdbpy_initialize_frames
);
798 static PyMethodDef frame_object_methods
[] = {
799 { "is_valid", frapy_is_valid
, METH_NOARGS
,
800 "is_valid () -> Boolean.\n\
801 Return true if this frame is valid, false if not." },
802 { "name", frapy_name
, METH_NOARGS
,
803 "name () -> String.\n\
804 Return the function name of the frame, or None if it can't be determined." },
805 { "type", frapy_type
, METH_NOARGS
,
806 "type () -> Integer.\n\
807 Return the type of the frame." },
808 { "architecture", frapy_arch
, METH_NOARGS
,
809 "architecture () -> gdb.Architecture.\n\
810 Return the architecture of the frame." },
811 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
812 "unwind_stop_reason () -> Integer.\n\
813 Return the reason why it's not possible to find frames older than this." },
814 { "pc", frapy_pc
, METH_NOARGS
,
816 Return the frame's resume address." },
817 { "read_register", (PyCFunction
) frapy_read_register
,
818 METH_VARARGS
| METH_KEYWORDS
,
819 "read_register (register_name) -> gdb.Value\n\
820 Return the value of the register in the frame." },
821 { "block", frapy_block
, METH_NOARGS
,
822 "block () -> gdb.Block.\n\
823 Return the frame's code block." },
824 { "function", frapy_function
, METH_NOARGS
,
825 "function () -> gdb.Symbol.\n\
826 Returns the symbol for the function corresponding to this frame." },
827 { "older", frapy_older
, METH_NOARGS
,
828 "older () -> gdb.Frame.\n\
829 Return the frame that called this frame." },
830 { "newer", frapy_newer
, METH_NOARGS
,
831 "newer () -> gdb.Frame.\n\
832 Return the frame called by this frame." },
833 { "find_sal", frapy_find_sal
, METH_NOARGS
,
834 "find_sal () -> gdb.Symtab_and_line.\n\
835 Return the frame's symtab and line." },
836 { "read_var", (PyCFunction
) frapy_read_var
, METH_VARARGS
| METH_KEYWORDS
,
837 "read_var (variable) -> gdb.Value.\n\
838 Return the value of the variable in this frame." },
839 { "select", frapy_select
, METH_NOARGS
,
840 "Select this frame as the user's current frame." },
841 { "level", frapy_level
, METH_NOARGS
,
842 "The stack level of this frame." },
843 { "language", frapy_language
, METH_NOARGS
,
844 "The language of this frame." },
845 { "static_link", frapy_static_link
, METH_NOARGS
,
846 "The static link of this frame, or None." },
847 {NULL
} /* Sentinel */
850 PyTypeObject frame_object_type
= {
851 PyVarObject_HEAD_INIT (NULL
, 0)
852 "gdb.Frame", /* tp_name */
853 sizeof (frame_object
), /* tp_basicsize */
860 frapy_repr
, /* tp_repr */
861 0, /* tp_as_number */
862 0, /* tp_as_sequence */
863 0, /* tp_as_mapping */
866 frapy_str
, /* tp_str */
869 0, /* tp_as_buffer */
870 Py_TPFLAGS_DEFAULT
, /* tp_flags */
871 "GDB frame object", /* tp_doc */
874 frapy_richcompare
, /* tp_richcompare */
875 0, /* tp_weaklistoffset */
878 frame_object_methods
, /* tp_methods */
883 0, /* tp_descr_get */
884 0, /* tp_descr_set */
885 0, /* tp_dictoffset */