1 /* Python interface to stack frames
3 Copyright (C) 2008-2025 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"
33 struct frame_id frame_id
;
34 struct gdbarch
*gdbarch
;
36 /* Marks that the FRAME_ID member actually holds the ID of the frame next
37 to this, and not this frames' ID itself. This is a hack to permit Python
38 frame objects which represent invalid frames (i.e., the last frame_info
39 in a corrupt stack). The problem arises from the fact that this code
40 relies on FRAME_ID to uniquely identify a frame, which is not always true
41 for the last "frame" in a corrupt stack (it can have a null ID, or the same
42 ID as the previous frame). Whenever get_prev_frame returns NULL, we
43 record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */
47 /* Require a valid frame. This must be called inside a TRY_CATCH, or
48 another context in which a gdb exception is allowed. */
49 #define FRAPY_REQUIRE_VALID(frame_obj, frame) \
51 frame = frame_object_to_frame_info (frame_obj); \
53 error (_("Frame is invalid.")); \
56 /* Returns the frame_info object corresponding to the given Python Frame
57 object. If the frame doesn't exist anymore (the frame id doesn't
58 correspond to any frame in the inferior), returns NULL. */
61 frame_object_to_frame_info (PyObject
*obj
)
63 frame_object
*frame_obj
= (frame_object
*) obj
;
66 frame
= frame_find_by_id (frame_obj
->frame_id
);
70 if (frame_obj
->frame_id_is_next
)
71 frame
= get_prev_frame (frame
);
76 /* Called by the Python interpreter to obtain string representation
80 frapy_str (PyObject
*self
)
82 const frame_id
&fid
= ((frame_object
*) self
)->frame_id
;
83 return PyUnicode_FromString (fid
.to_string ().c_str ());
86 /* Implement repr() for gdb.Frame. */
89 frapy_repr (PyObject
*self
)
91 frame_object
*frame_obj
= (frame_object
*) self
;
92 frame_info_ptr f_info
= frame_find_by_id (frame_obj
->frame_id
);
93 if (f_info
== nullptr)
94 return gdb_py_invalid_object_repr (self
);
96 const frame_id
&fid
= frame_obj
->frame_id
;
97 return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>",
98 Py_TYPE (self
)->tp_name
,
99 frame_relative_level (f_info
),
100 fid
.to_string ().c_str ());
103 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
104 Returns True if the frame corresponding to the frame_id of this
105 object still exists in the inferior. */
108 frapy_is_valid (PyObject
*self
, PyObject
*args
)
110 frame_info_ptr frame
= NULL
;
114 frame
= frame_object_to_frame_info (self
);
116 catch (const gdb_exception
&except
)
118 return gdbpy_handle_gdb_exception (nullptr, except
);
127 /* Implementation of gdb.Frame.name (self) -> String.
128 Returns the name of the function corresponding to this frame. */
131 frapy_name (PyObject
*self
, PyObject
*args
)
133 frame_info_ptr frame
;
134 gdb::unique_xmalloc_ptr
<char> name
;
140 FRAPY_REQUIRE_VALID (self
, frame
);
142 name
= find_frame_funname (frame
, &lang
, NULL
);
144 catch (const gdb_exception
&except
)
146 return gdbpy_handle_gdb_exception (nullptr, except
);
151 result
= PyUnicode_Decode (name
.get (), strlen (name
.get ()),
152 host_charset (), NULL
);
163 /* Implementation of gdb.Frame.type (self) -> Integer.
164 Returns the frame type, namely one of the gdb.*_FRAME constants. */
167 frapy_type (PyObject
*self
, PyObject
*args
)
169 frame_info_ptr frame
;
170 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
174 FRAPY_REQUIRE_VALID (self
, frame
);
176 type
= get_frame_type (frame
);
178 catch (const gdb_exception
&except
)
180 return gdbpy_handle_gdb_exception (nullptr, except
);
183 return gdb_py_object_from_longest (type
).release ();
186 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
187 Returns the frame's architecture as a gdb.Architecture object. */
190 frapy_arch (PyObject
*self
, PyObject
*args
)
192 frame_info_ptr frame
= NULL
; /* Initialize to appease gcc warning. */
193 frame_object
*obj
= (frame_object
*) self
;
197 FRAPY_REQUIRE_VALID (self
, frame
);
199 catch (const gdb_exception
&except
)
201 return gdbpy_handle_gdb_exception (nullptr, except
);
204 return gdbarch_to_arch_object (obj
->gdbarch
);
207 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
208 Returns one of the gdb.FRAME_UNWIND_* constants. */
211 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
213 frame_info_ptr frame
= NULL
; /* Initialize to appease gcc warning. */
214 enum unwind_stop_reason stop_reason
;
218 FRAPY_REQUIRE_VALID (self
, frame
);
220 catch (const gdb_exception
&except
)
222 return gdbpy_handle_gdb_exception (nullptr, except
);
225 stop_reason
= get_frame_unwind_stop_reason (frame
);
227 return gdb_py_object_from_longest (stop_reason
).release ();
230 /* Implementation of gdb.Frame.pc (self) -> Long.
231 Returns the frame's resume address. */
234 frapy_pc (PyObject
*self
, PyObject
*args
)
236 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
237 frame_info_ptr frame
;
241 FRAPY_REQUIRE_VALID (self
, frame
);
243 pc
= get_frame_pc (frame
);
245 catch (const gdb_exception
&except
)
247 return gdbpy_handle_gdb_exception (nullptr, except
);
250 return gdb_py_object_from_ulongest (pc
).release ();
253 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
254 Returns the value of a register in this frame. */
257 frapy_read_register (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
259 PyObject
*pyo_reg_id
;
260 PyObject
*result
= nullptr;
262 static const char *keywords
[] = { "register", nullptr };
263 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "O", keywords
, &pyo_reg_id
))
268 scoped_value_mark free_values
;
269 frame_info_ptr frame
;
272 FRAPY_REQUIRE_VALID (self
, frame
);
274 if (!gdbpy_parse_register_id (get_frame_arch (frame
), pyo_reg_id
,
278 gdb_assert (regnum
>= 0);
280 = value_of_register (regnum
, get_next_frame_sentinel_okay (frame
));
283 PyErr_SetString (PyExc_ValueError
, _("Can't read register."));
285 result
= value_to_value_object (val
);
287 catch (const gdb_exception
&except
)
289 return gdbpy_handle_gdb_exception (nullptr, except
);
295 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
296 Returns the frame's code block. */
299 frapy_block (PyObject
*self
, PyObject
*args
)
301 frame_info_ptr frame
;
302 const struct block
*block
= NULL
, *fn_block
;
306 FRAPY_REQUIRE_VALID (self
, frame
);
307 block
= get_frame_block (frame
, NULL
);
309 catch (const gdb_exception
&except
)
311 return gdbpy_handle_gdb_exception (nullptr, except
);
314 for (fn_block
= block
;
315 fn_block
!= NULL
&& fn_block
->function () == NULL
;
316 fn_block
= fn_block
->superblock ())
319 if (block
== NULL
|| fn_block
== NULL
|| fn_block
->function () == NULL
)
321 PyErr_SetString (PyExc_RuntimeError
,
322 _("Cannot locate block for frame."));
326 return block_to_block_object (block
, fn_block
->function ()->objfile ());
330 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
331 Returns the symbol for the function corresponding to this frame. */
334 frapy_function (PyObject
*self
, PyObject
*args
)
336 struct symbol
*sym
= NULL
;
337 frame_info_ptr frame
;
341 enum language funlang
;
343 FRAPY_REQUIRE_VALID (self
, frame
);
345 gdb::unique_xmalloc_ptr
<char> funname
346 = find_frame_funname (frame
, &funlang
, &sym
);
348 catch (const gdb_exception
&except
)
350 return gdbpy_handle_gdb_exception (nullptr, except
);
354 return symbol_to_symbol_object (sym
);
359 /* Convert a frame_info struct to a Python Frame object.
360 Sets a Python exception and returns NULL on error. */
363 frame_info_to_frame_object (const frame_info_ptr
&frame
)
365 gdbpy_ref
<frame_object
> frame_obj (PyObject_New (frame_object
,
366 &frame_object_type
));
367 if (frame_obj
== NULL
)
373 /* Try to get the previous frame, to determine if this is the last frame
374 in a corrupt stack. If so, we need to store the frame_id of the next
375 frame and not of this one (which is possibly invalid). */
376 if (get_prev_frame (frame
) == NULL
377 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
378 && get_next_frame (frame
) != NULL
)
380 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
381 frame_obj
->frame_id_is_next
= 1;
385 frame_obj
->frame_id
= get_frame_id (frame
);
386 frame_obj
->frame_id_is_next
= 0;
388 frame_obj
->gdbarch
= get_frame_arch (frame
);
390 catch (const gdb_exception
&except
)
392 return gdbpy_handle_gdb_exception (nullptr, except
);
395 return (PyObject
*) frame_obj
.release ();
398 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
399 Returns the frame immediately older (outer) to this frame, or None if
403 frapy_older (PyObject
*self
, PyObject
*args
)
405 frame_info_ptr frame
, prev
= NULL
;
406 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
410 FRAPY_REQUIRE_VALID (self
, frame
);
412 prev
= get_prev_frame (frame
);
414 catch (const gdb_exception
&except
)
416 return gdbpy_handle_gdb_exception (nullptr, except
);
420 prev_obj
= frame_info_to_frame_object (prev
);
430 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
431 Returns the frame immediately newer (inner) to this frame, or None if
435 frapy_newer (PyObject
*self
, PyObject
*args
)
437 frame_info_ptr frame
, next
= NULL
;
438 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
442 FRAPY_REQUIRE_VALID (self
, frame
);
444 next
= get_next_frame (frame
);
446 catch (const gdb_exception
&except
)
448 return gdbpy_handle_gdb_exception (nullptr, except
);
452 next_obj
= frame_info_to_frame_object (next
);
462 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
463 Returns the frame's symtab and line. */
466 frapy_find_sal (PyObject
*self
, PyObject
*args
)
468 frame_info_ptr frame
;
469 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
473 FRAPY_REQUIRE_VALID (self
, frame
);
475 symtab_and_line sal
= find_frame_sal (frame
);
476 sal_obj
= symtab_and_line_to_sal_object (sal
);
478 catch (const gdb_exception
&except
)
480 return gdbpy_handle_gdb_exception (nullptr, 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
, PyObject
*kw
)
496 frame_info_ptr 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
;
501 static const char *keywords
[] = { "variable", "block", nullptr };
502 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "O|O!", keywords
,
503 &sym_obj
, &block_object_type
,
507 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
508 var
= symbol_object_to_symbol (sym_obj
);
509 else if (gdbpy_is_string (sym_obj
))
511 gdb::unique_xmalloc_ptr
<char>
512 var_name (python_string_to_target_string (sym_obj
));
517 if (block_obj
!= nullptr)
519 /* This call should only fail if the type of BLOCK_OBJ is wrong,
520 and we ensure the type is correct when we parse the arguments,
521 so we can just assert the return value is not nullptr. */
522 block
= block_object_to_block (block_obj
);
523 gdb_assert (block
!= nullptr);
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
,
534 SEARCH_VFT
, nullptr);
535 var
= lookup_sym
.symbol
;
536 block
= lookup_sym
.block
;
538 catch (const gdb_exception
&except
)
540 return gdbpy_handle_gdb_exception (nullptr, except
);
545 PyErr_Format (PyExc_ValueError
,
546 _("Variable '%s' not found."), var_name
.get ());
553 PyErr_Format (PyExc_TypeError
,
554 _("argument 1 must be gdb.Symbol or str, not %s"),
555 Py_TYPE (sym_obj
)->tp_name
);
559 PyObject
*result
= nullptr;
562 FRAPY_REQUIRE_VALID (self
, frame
);
564 scoped_value_mark free_values
;
565 struct value
*val
= read_var_value (var
, block
, frame
);
566 result
= value_to_value_object (val
);
568 catch (const gdb_exception
&except
)
570 return gdbpy_handle_gdb_exception (nullptr, except
);
576 /* Select this frame. */
579 frapy_select (PyObject
*self
, PyObject
*args
)
585 FRAPY_REQUIRE_VALID (self
, fi
);
589 catch (const gdb_exception
&except
)
591 return gdbpy_handle_gdb_exception (nullptr, except
);
597 /* The stack frame level for this frame. */
600 frapy_level (PyObject
*self
, PyObject
*args
)
606 FRAPY_REQUIRE_VALID (self
, fi
);
608 return gdb_py_object_from_longest (frame_relative_level (fi
)).release ();
610 catch (const gdb_exception
&except
)
612 return gdbpy_handle_gdb_exception (nullptr, except
);
618 /* The language for this frame. */
621 frapy_language (PyObject
*self
, PyObject
*args
)
626 FRAPY_REQUIRE_VALID (self
, fi
);
628 enum language lang
= get_frame_language (fi
);
629 const language_defn
*lang_def
= language_def (lang
);
631 return host_string_to_python_string (lang_def
->name ()).release ();
633 catch (const gdb_exception
&except
)
635 return gdbpy_handle_gdb_exception (nullptr, except
);
641 /* The static link for this frame. */
644 frapy_static_link (PyObject
*self
, PyObject
*args
)
650 FRAPY_REQUIRE_VALID (self
, link
);
652 link
= frame_follow_static_link (link
);
654 catch (const gdb_exception
&except
)
656 return gdbpy_handle_gdb_exception (nullptr, except
);
662 return frame_info_to_frame_object (link
);
665 /* Implementation of gdb.newest_frame () -> gdb.Frame.
666 Returns the newest frame object. */
669 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
671 frame_info_ptr frame
= NULL
;
675 frame
= get_current_frame ();
677 catch (const gdb_exception
&except
)
679 return gdbpy_handle_gdb_exception (nullptr, except
);
682 return frame_info_to_frame_object (frame
);
685 /* Implementation of gdb.selected_frame () -> gdb.Frame.
686 Returns the selected frame object. */
689 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
691 frame_info_ptr frame
= NULL
;
695 frame
= get_selected_frame ("No frame is currently selected.");
697 catch (const gdb_exception
&except
)
699 return gdbpy_handle_gdb_exception (nullptr, except
);
702 return frame_info_to_frame_object (frame
);
705 /* Implementation of gdb.stop_reason_string (Integer) -> String.
706 Return a string explaining the unwind stop reason. */
709 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
714 if (!PyArg_ParseTuple (args
, "i", &reason
))
717 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
719 PyErr_SetString (PyExc_ValueError
,
720 _("Invalid frame stop reason."));
724 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
725 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
728 /* Implements the equality comparison for Frame objects.
729 All other comparison operators will throw a TypeError Python exception,
730 as they aren't valid for frames. */
733 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
737 if (!PyObject_TypeCheck (other
, &frame_object_type
)
738 || (op
!= Py_EQ
&& op
!= Py_NE
))
740 Py_INCREF (Py_NotImplemented
);
741 return Py_NotImplemented
;
744 frame_object
*self_frame
= (frame_object
*) self
;
745 frame_object
*other_frame
= (frame_object
*) other
;
747 if (self_frame
->frame_id_is_next
== other_frame
->frame_id_is_next
748 && self_frame
->frame_id
== other_frame
->frame_id
)
758 /* Sets up the Frame API in the gdb module. */
760 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
761 gdbpy_initialize_frames (void)
763 frame_object_type
.tp_new
= PyType_GenericNew
;
764 if (gdbpy_type_ready (&frame_object_type
) < 0)
767 /* Note: These would probably be best exposed as class attributes of
768 Frame, but I don't know how to do it except by messing with the
769 type's dictionary. That seems too messy. */
770 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
771 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
772 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
773 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
775 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
777 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
778 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
782 #define SET(name, description) \
783 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
785 #include "unwind_stop_reasons.def"
791 GDBPY_INITIALIZE_FILE (gdbpy_initialize_frames
);
795 static PyMethodDef frame_object_methods
[] = {
796 { "is_valid", frapy_is_valid
, METH_NOARGS
,
797 "is_valid () -> Boolean.\n\
798 Return true if this frame is valid, false if not." },
799 { "name", frapy_name
, METH_NOARGS
,
800 "name () -> String.\n\
801 Return the function name of the frame, or None if it can't be determined." },
802 { "type", frapy_type
, METH_NOARGS
,
803 "type () -> Integer.\n\
804 Return the type of the frame." },
805 { "architecture", frapy_arch
, METH_NOARGS
,
806 "architecture () -> gdb.Architecture.\n\
807 Return the architecture of the frame." },
808 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
809 "unwind_stop_reason () -> Integer.\n\
810 Return the reason why it's not possible to find frames older than this." },
811 { "pc", frapy_pc
, METH_NOARGS
,
813 Return the frame's resume address." },
814 { "read_register", (PyCFunction
) frapy_read_register
,
815 METH_VARARGS
| METH_KEYWORDS
,
816 "read_register (register_name) -> gdb.Value\n\
817 Return the value of the register in the frame." },
818 { "block", frapy_block
, METH_NOARGS
,
819 "block () -> gdb.Block.\n\
820 Return the frame's code block." },
821 { "function", frapy_function
, METH_NOARGS
,
822 "function () -> gdb.Symbol.\n\
823 Returns the symbol for the function corresponding to this frame." },
824 { "older", frapy_older
, METH_NOARGS
,
825 "older () -> gdb.Frame.\n\
826 Return the frame that called this frame." },
827 { "newer", frapy_newer
, METH_NOARGS
,
828 "newer () -> gdb.Frame.\n\
829 Return the frame called by this frame." },
830 { "find_sal", frapy_find_sal
, METH_NOARGS
,
831 "find_sal () -> gdb.Symtab_and_line.\n\
832 Return the frame's symtab and line." },
833 { "read_var", (PyCFunction
) frapy_read_var
, METH_VARARGS
| METH_KEYWORDS
,
834 "read_var (variable) -> gdb.Value.\n\
835 Return the value of the variable in this frame." },
836 { "select", frapy_select
, METH_NOARGS
,
837 "Select this frame as the user's current frame." },
838 { "level", frapy_level
, METH_NOARGS
,
839 "The stack level of this frame." },
840 { "language", frapy_language
, METH_NOARGS
,
841 "The language of this frame." },
842 { "static_link", frapy_static_link
, METH_NOARGS
,
843 "The static link of this frame, or None." },
844 {NULL
} /* Sentinel */
847 PyTypeObject frame_object_type
= {
848 PyVarObject_HEAD_INIT (NULL
, 0)
849 "gdb.Frame", /* tp_name */
850 sizeof (frame_object
), /* tp_basicsize */
857 frapy_repr
, /* tp_repr */
858 0, /* tp_as_number */
859 0, /* tp_as_sequence */
860 0, /* tp_as_mapping */
863 frapy_str
, /* tp_str */
866 0, /* tp_as_buffer */
867 Py_TPFLAGS_DEFAULT
, /* tp_flags */
868 "GDB frame object", /* tp_doc */
871 frapy_richcompare
, /* tp_richcompare */
872 0, /* tp_weaklistoffset */
875 frame_object_methods
, /* tp_methods */
880 0, /* tp_descr_get */
881 0, /* tp_descr_set */
882 0, /* tp_dictoffset */