1 /* Python interface to stack frames
3 Copyright (C) 2008-2022 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
;
64 struct frame_info
*frame
;
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 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
87 Returns True if the frame corresponding to the frame_id of this
88 object still exists in the inferior. */
91 frapy_is_valid (PyObject
*self
, PyObject
*args
)
93 struct frame_info
*frame
= NULL
;
97 frame
= frame_object_to_frame_info (self
);
99 catch (const gdb_exception
&except
)
101 GDB_PY_HANDLE_EXCEPTION (except
);
110 /* Implementation of gdb.Frame.name (self) -> String.
111 Returns the name of the function corresponding to this frame. */
114 frapy_name (PyObject
*self
, PyObject
*args
)
116 struct frame_info
*frame
;
117 gdb::unique_xmalloc_ptr
<char> name
;
123 FRAPY_REQUIRE_VALID (self
, frame
);
125 name
= find_frame_funname (frame
, &lang
, NULL
);
127 catch (const gdb_exception
&except
)
129 GDB_PY_HANDLE_EXCEPTION (except
);
134 result
= PyUnicode_Decode (name
.get (), strlen (name
.get ()),
135 host_charset (), NULL
);
146 /* Implementation of gdb.Frame.type (self) -> Integer.
147 Returns the frame type, namely one of the gdb.*_FRAME constants. */
150 frapy_type (PyObject
*self
, PyObject
*args
)
152 struct frame_info
*frame
;
153 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
157 FRAPY_REQUIRE_VALID (self
, frame
);
159 type
= get_frame_type (frame
);
161 catch (const gdb_exception
&except
)
163 GDB_PY_HANDLE_EXCEPTION (except
);
166 return gdb_py_object_from_longest (type
).release ();
169 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
170 Returns the frame's architecture as a gdb.Architecture object. */
173 frapy_arch (PyObject
*self
, PyObject
*args
)
175 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
176 frame_object
*obj
= (frame_object
*) self
;
180 FRAPY_REQUIRE_VALID (self
, frame
);
182 catch (const gdb_exception
&except
)
184 GDB_PY_HANDLE_EXCEPTION (except
);
187 return gdbarch_to_arch_object (obj
->gdbarch
);
190 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
191 Returns one of the gdb.FRAME_UNWIND_* constants. */
194 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
196 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
197 enum unwind_stop_reason stop_reason
;
201 FRAPY_REQUIRE_VALID (self
, frame
);
203 catch (const gdb_exception
&except
)
205 GDB_PY_HANDLE_EXCEPTION (except
);
208 stop_reason
= get_frame_unwind_stop_reason (frame
);
210 return gdb_py_object_from_longest (stop_reason
).release ();
213 /* Implementation of gdb.Frame.pc (self) -> Long.
214 Returns the frame's resume address. */
217 frapy_pc (PyObject
*self
, PyObject
*args
)
219 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
220 struct frame_info
*frame
;
224 FRAPY_REQUIRE_VALID (self
, frame
);
226 pc
= get_frame_pc (frame
);
228 catch (const gdb_exception
&except
)
230 GDB_PY_HANDLE_EXCEPTION (except
);
233 return gdb_py_object_from_ulongest (pc
).release ();
236 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
237 Returns the value of a register in this frame. */
240 frapy_read_register (PyObject
*self
, PyObject
*args
)
242 PyObject
*pyo_reg_id
;
243 struct value
*val
= NULL
;
245 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
249 struct frame_info
*frame
;
252 FRAPY_REQUIRE_VALID (self
, frame
);
254 if (!gdbpy_parse_register_id (get_frame_arch (frame
), pyo_reg_id
,
257 PyErr_SetString (PyExc_ValueError
, "Bad register");
261 gdb_assert (regnum
>= 0);
262 val
= value_of_register (regnum
, frame
);
265 PyErr_SetString (PyExc_ValueError
, _("Can't read register."));
267 catch (const gdb_exception
&except
)
269 GDB_PY_HANDLE_EXCEPTION (except
);
272 return val
== NULL
? NULL
: value_to_value_object (val
);
275 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
276 Returns the frame's code block. */
279 frapy_block (PyObject
*self
, PyObject
*args
)
281 struct frame_info
*frame
;
282 const struct block
*block
= NULL
, *fn_block
;
286 FRAPY_REQUIRE_VALID (self
, frame
);
287 block
= get_frame_block (frame
, NULL
);
289 catch (const gdb_exception
&except
)
291 GDB_PY_HANDLE_EXCEPTION (except
);
294 for (fn_block
= block
;
295 fn_block
!= NULL
&& BLOCK_FUNCTION (fn_block
) == NULL
;
296 fn_block
= BLOCK_SUPERBLOCK (fn_block
))
299 if (block
== NULL
|| fn_block
== NULL
|| BLOCK_FUNCTION (fn_block
) == NULL
)
301 PyErr_SetString (PyExc_RuntimeError
,
302 _("Cannot locate block for frame."));
308 return block_to_block_object
309 (block
, BLOCK_FUNCTION (fn_block
)->objfile ());
316 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
317 Returns the symbol for the function corresponding to this frame. */
320 frapy_function (PyObject
*self
, PyObject
*args
)
322 struct symbol
*sym
= NULL
;
323 struct frame_info
*frame
;
327 enum language funlang
;
329 FRAPY_REQUIRE_VALID (self
, frame
);
331 gdb::unique_xmalloc_ptr
<char> funname
332 = find_frame_funname (frame
, &funlang
, &sym
);
334 catch (const gdb_exception
&except
)
336 GDB_PY_HANDLE_EXCEPTION (except
);
340 return symbol_to_symbol_object (sym
);
345 /* Convert a frame_info struct to a Python Frame object.
346 Sets a Python exception and returns NULL on error. */
349 frame_info_to_frame_object (struct frame_info
*frame
)
351 gdbpy_ref
<frame_object
> frame_obj (PyObject_New (frame_object
,
352 &frame_object_type
));
353 if (frame_obj
== NULL
)
359 /* Try to get the previous frame, to determine if this is the last frame
360 in a corrupt stack. If so, we need to store the frame_id of the next
361 frame and not of this one (which is possibly invalid). */
362 if (get_prev_frame (frame
) == NULL
363 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
364 && get_next_frame (frame
) != NULL
)
366 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
367 frame_obj
->frame_id_is_next
= 1;
371 frame_obj
->frame_id
= get_frame_id (frame
);
372 frame_obj
->frame_id_is_next
= 0;
374 frame_obj
->gdbarch
= get_frame_arch (frame
);
376 catch (const gdb_exception
&except
)
378 gdbpy_convert_exception (except
);
382 return (PyObject
*) frame_obj
.release ();
385 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
386 Returns the frame immediately older (outer) to this frame, or None if
390 frapy_older (PyObject
*self
, PyObject
*args
)
392 struct frame_info
*frame
, *prev
= NULL
;
393 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
397 FRAPY_REQUIRE_VALID (self
, frame
);
399 prev
= get_prev_frame (frame
);
401 catch (const gdb_exception
&except
)
403 GDB_PY_HANDLE_EXCEPTION (except
);
407 prev_obj
= frame_info_to_frame_object (prev
);
417 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
418 Returns the frame immediately newer (inner) to this frame, or None if
422 frapy_newer (PyObject
*self
, PyObject
*args
)
424 struct frame_info
*frame
, *next
= NULL
;
425 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
429 FRAPY_REQUIRE_VALID (self
, frame
);
431 next
= get_next_frame (frame
);
433 catch (const gdb_exception
&except
)
435 GDB_PY_HANDLE_EXCEPTION (except
);
439 next_obj
= frame_info_to_frame_object (next
);
449 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
450 Returns the frame's symtab and line. */
453 frapy_find_sal (PyObject
*self
, PyObject
*args
)
455 struct frame_info
*frame
;
456 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
460 FRAPY_REQUIRE_VALID (self
, frame
);
462 symtab_and_line sal
= find_frame_sal (frame
);
463 sal_obj
= symtab_and_line_to_sal_object (sal
);
465 catch (const gdb_exception
&except
)
467 GDB_PY_HANDLE_EXCEPTION (except
);
473 /* Implementation of gdb.Frame.read_var_value (self, variable,
474 [block]) -> gdb.Value. If the optional block argument is provided
475 start the search from that block, otherwise search from the frame's
476 current block (determined by examining the resume address of the
477 frame). The variable argument must be a string or an instance of a
478 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
479 NULL on error, with a python exception set. */
481 frapy_read_var (PyObject
*self
, PyObject
*args
)
483 struct frame_info
*frame
;
484 PyObject
*sym_obj
, *block_obj
= NULL
;
485 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
486 const struct block
*block
= NULL
;
487 struct value
*val
= NULL
;
489 if (!PyArg_ParseTuple (args
, "O|O", &sym_obj
, &block_obj
))
492 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
493 var
= symbol_object_to_symbol (sym_obj
);
494 else if (gdbpy_is_string (sym_obj
))
496 gdb::unique_xmalloc_ptr
<char>
497 var_name (python_string_to_target_string (sym_obj
));
504 block
= block_object_to_block (block_obj
);
507 PyErr_SetString (PyExc_RuntimeError
,
508 _("Second argument must be block."));
515 struct block_symbol lookup_sym
;
516 FRAPY_REQUIRE_VALID (self
, frame
);
519 block
= get_frame_block (frame
, NULL
);
520 lookup_sym
= lookup_symbol (var_name
.get (), block
, VAR_DOMAIN
, NULL
);
521 var
= lookup_sym
.symbol
;
522 block
= lookup_sym
.block
;
524 catch (const gdb_exception
&except
)
526 gdbpy_convert_exception (except
);
532 PyErr_Format (PyExc_ValueError
,
533 _("Variable '%s' not found."), var_name
.get ());
540 PyErr_SetString (PyExc_TypeError
,
541 _("Argument must be a symbol or string."));
547 FRAPY_REQUIRE_VALID (self
, frame
);
549 val
= read_var_value (var
, block
, frame
);
551 catch (const gdb_exception
&except
)
553 GDB_PY_HANDLE_EXCEPTION (except
);
556 return value_to_value_object (val
);
559 /* Select this frame. */
562 frapy_select (PyObject
*self
, PyObject
*args
)
564 struct frame_info
*fi
;
568 FRAPY_REQUIRE_VALID (self
, fi
);
572 catch (const gdb_exception
&except
)
574 GDB_PY_HANDLE_EXCEPTION (except
);
580 /* The stack frame level for this frame. */
583 frapy_level (PyObject
*self
, PyObject
*args
)
585 struct frame_info
*fi
;
589 FRAPY_REQUIRE_VALID (self
, fi
);
591 return gdb_py_object_from_longest (frame_relative_level (fi
)).release ();
593 catch (const gdb_exception
&except
)
595 GDB_PY_HANDLE_EXCEPTION (except
);
601 /* Implementation of gdb.newest_frame () -> gdb.Frame.
602 Returns the newest frame object. */
605 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
607 struct frame_info
*frame
= NULL
;
611 frame
= get_current_frame ();
613 catch (const gdb_exception
&except
)
615 GDB_PY_HANDLE_EXCEPTION (except
);
618 return frame_info_to_frame_object (frame
);
621 /* Implementation of gdb.selected_frame () -> gdb.Frame.
622 Returns the selected frame object. */
625 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
627 struct frame_info
*frame
= NULL
;
631 frame
= get_selected_frame ("No frame is currently selected.");
633 catch (const gdb_exception
&except
)
635 GDB_PY_HANDLE_EXCEPTION (except
);
638 return frame_info_to_frame_object (frame
);
641 /* Implementation of gdb.stop_reason_string (Integer) -> String.
642 Return a string explaining the unwind stop reason. */
645 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
650 if (!PyArg_ParseTuple (args
, "i", &reason
))
653 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
655 PyErr_SetString (PyExc_ValueError
,
656 _("Invalid frame stop reason."));
660 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
661 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
664 /* Implements the equality comparison for Frame objects.
665 All other comparison operators will throw a TypeError Python exception,
666 as they aren't valid for frames. */
669 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
673 if (!PyObject_TypeCheck (other
, &frame_object_type
)
674 || (op
!= Py_EQ
&& op
!= Py_NE
))
676 Py_INCREF (Py_NotImplemented
);
677 return Py_NotImplemented
;
680 frame_object
*self_frame
= (frame_object
*) self
;
681 frame_object
*other_frame
= (frame_object
*) other
;
683 if (self_frame
->frame_id_is_next
== other_frame
->frame_id_is_next
684 && frame_id_eq (self_frame
->frame_id
, other_frame
->frame_id
))
694 /* Sets up the Frame API in the gdb module. */
697 gdbpy_initialize_frames (void)
699 frame_object_type
.tp_new
= PyType_GenericNew
;
700 if (PyType_Ready (&frame_object_type
) < 0)
703 /* Note: These would probably be best exposed as class attributes of
704 Frame, but I don't know how to do it except by messing with the
705 type's dictionary. That seems too messy. */
706 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
707 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
708 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
709 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
711 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
713 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
714 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
718 #define SET(name, description) \
719 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
721 #include "unwind_stop_reasons.def"
724 return gdb_pymodule_addobject (gdb_module
, "Frame",
725 (PyObject
*) &frame_object_type
);
730 static PyMethodDef frame_object_methods
[] = {
731 { "is_valid", frapy_is_valid
, METH_NOARGS
,
732 "is_valid () -> Boolean.\n\
733 Return true if this frame is valid, false if not." },
734 { "name", frapy_name
, METH_NOARGS
,
735 "name () -> String.\n\
736 Return the function name of the frame, or None if it can't be determined." },
737 { "type", frapy_type
, METH_NOARGS
,
738 "type () -> Integer.\n\
739 Return the type of the frame." },
740 { "architecture", frapy_arch
, METH_NOARGS
,
741 "architecture () -> gdb.Architecture.\n\
742 Return the architecture of the frame." },
743 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
744 "unwind_stop_reason () -> Integer.\n\
745 Return the reason why it's not possible to find frames older than this." },
746 { "pc", frapy_pc
, METH_NOARGS
,
748 Return the frame's resume address." },
749 { "read_register", frapy_read_register
, METH_VARARGS
,
750 "read_register (register_name) -> gdb.Value\n\
751 Return the value of the register in the frame." },
752 { "block", frapy_block
, METH_NOARGS
,
753 "block () -> gdb.Block.\n\
754 Return the frame's code block." },
755 { "function", frapy_function
, METH_NOARGS
,
756 "function () -> gdb.Symbol.\n\
757 Returns the symbol for the function corresponding to this frame." },
758 { "older", frapy_older
, METH_NOARGS
,
759 "older () -> gdb.Frame.\n\
760 Return the frame that called this frame." },
761 { "newer", frapy_newer
, METH_NOARGS
,
762 "newer () -> gdb.Frame.\n\
763 Return the frame called by this frame." },
764 { "find_sal", frapy_find_sal
, METH_NOARGS
,
765 "find_sal () -> gdb.Symtab_and_line.\n\
766 Return the frame's symtab and line." },
767 { "read_var", frapy_read_var
, METH_VARARGS
,
768 "read_var (variable) -> gdb.Value.\n\
769 Return the value of the variable in this frame." },
770 { "select", frapy_select
, METH_NOARGS
,
771 "Select this frame as the user's current frame." },
772 { "level", frapy_level
, METH_NOARGS
,
773 "The stack level of this frame." },
774 {NULL
} /* Sentinel */
777 PyTypeObject frame_object_type
= {
778 PyVarObject_HEAD_INIT (NULL
, 0)
779 "gdb.Frame", /* tp_name */
780 sizeof (frame_object
), /* tp_basicsize */
788 0, /* tp_as_number */
789 0, /* tp_as_sequence */
790 0, /* tp_as_mapping */
793 frapy_str
, /* tp_str */
796 0, /* tp_as_buffer */
797 Py_TPFLAGS_DEFAULT
, /* tp_flags */
798 "GDB frame object", /* tp_doc */
801 frapy_richcompare
, /* tp_richcompare */
802 0, /* tp_weaklistoffset */
805 frame_object_methods
, /* tp_methods */
810 0, /* tp_descr_get */
811 0, /* tp_descr_set */
812 0, /* tp_dictoffset */