1 /* Python interface to stack frames
3 Copyright (C) 2008-2020 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
)
84 fprint_frame_id (&strfile
, ((frame_object
*) self
)->frame_id
);
85 return PyString_FromString (strfile
.c_str ());
88 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
89 Returns True if the frame corresponding to the frame_id of this
90 object still exists in the inferior. */
93 frapy_is_valid (PyObject
*self
, PyObject
*args
)
95 struct frame_info
*frame
= NULL
;
99 frame
= frame_object_to_frame_info (self
);
101 catch (const gdb_exception
&except
)
103 GDB_PY_HANDLE_EXCEPTION (except
);
112 /* Implementation of gdb.Frame.name (self) -> String.
113 Returns the name of the function corresponding to this frame. */
116 frapy_name (PyObject
*self
, PyObject
*args
)
118 struct frame_info
*frame
;
119 gdb::unique_xmalloc_ptr
<char> name
;
125 FRAPY_REQUIRE_VALID (self
, frame
);
127 name
= find_frame_funname (frame
, &lang
, NULL
);
129 catch (const gdb_exception
&except
)
131 GDB_PY_HANDLE_EXCEPTION (except
);
136 result
= PyUnicode_Decode (name
.get (), strlen (name
.get ()),
137 host_charset (), NULL
);
148 /* Implementation of gdb.Frame.type (self) -> Integer.
149 Returns the frame type, namely one of the gdb.*_FRAME constants. */
152 frapy_type (PyObject
*self
, PyObject
*args
)
154 struct frame_info
*frame
;
155 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
159 FRAPY_REQUIRE_VALID (self
, frame
);
161 type
= get_frame_type (frame
);
163 catch (const gdb_exception
&except
)
165 GDB_PY_HANDLE_EXCEPTION (except
);
168 return gdb_py_object_from_longest (type
).release ();
171 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
172 Returns the frame's architecture as a gdb.Architecture object. */
175 frapy_arch (PyObject
*self
, PyObject
*args
)
177 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
178 frame_object
*obj
= (frame_object
*) self
;
182 FRAPY_REQUIRE_VALID (self
, frame
);
184 catch (const gdb_exception
&except
)
186 GDB_PY_HANDLE_EXCEPTION (except
);
189 return gdbarch_to_arch_object (obj
->gdbarch
);
192 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
193 Returns one of the gdb.FRAME_UNWIND_* constants. */
196 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
198 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
199 enum unwind_stop_reason stop_reason
;
203 FRAPY_REQUIRE_VALID (self
, frame
);
205 catch (const gdb_exception
&except
)
207 GDB_PY_HANDLE_EXCEPTION (except
);
210 stop_reason
= get_frame_unwind_stop_reason (frame
);
212 return gdb_py_object_from_longest (stop_reason
).release ();
215 /* Implementation of gdb.Frame.pc (self) -> Long.
216 Returns the frame's resume address. */
219 frapy_pc (PyObject
*self
, PyObject
*args
)
221 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
222 struct frame_info
*frame
;
226 FRAPY_REQUIRE_VALID (self
, frame
);
228 pc
= get_frame_pc (frame
);
230 catch (const gdb_exception
&except
)
232 GDB_PY_HANDLE_EXCEPTION (except
);
235 return gdb_py_object_from_ulongest (pc
).release ();
238 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
239 Returns the value of a register in this frame. */
242 frapy_read_register (PyObject
*self
, PyObject
*args
)
244 PyObject
*pyo_reg_id
;
245 struct value
*val
= NULL
;
247 if (!PyArg_UnpackTuple (args
, "read_register", 1, 1, &pyo_reg_id
))
251 struct frame_info
*frame
;
254 FRAPY_REQUIRE_VALID (self
, frame
);
256 if (!gdbpy_parse_register_id (get_frame_arch (frame
), pyo_reg_id
,
259 PyErr_SetString (PyExc_ValueError
, "Bad register");
263 gdb_assert (regnum
>= 0);
264 val
= value_of_register (regnum
, frame
);
267 PyErr_SetString (PyExc_ValueError
, _("Can't read register."));
269 catch (const gdb_exception
&except
)
271 GDB_PY_HANDLE_EXCEPTION (except
);
274 return val
== NULL
? NULL
: value_to_value_object (val
);
277 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
278 Returns the frame's code block. */
281 frapy_block (PyObject
*self
, PyObject
*args
)
283 struct frame_info
*frame
;
284 const struct block
*block
= NULL
, *fn_block
;
288 FRAPY_REQUIRE_VALID (self
, frame
);
289 block
= get_frame_block (frame
, NULL
);
291 catch (const gdb_exception
&except
)
293 GDB_PY_HANDLE_EXCEPTION (except
);
296 for (fn_block
= block
;
297 fn_block
!= NULL
&& BLOCK_FUNCTION (fn_block
) == NULL
;
298 fn_block
= BLOCK_SUPERBLOCK (fn_block
))
301 if (block
== NULL
|| fn_block
== NULL
|| BLOCK_FUNCTION (fn_block
) == NULL
)
303 PyErr_SetString (PyExc_RuntimeError
,
304 _("Cannot locate block for frame."));
310 return block_to_block_object
311 (block
, symbol_objfile (BLOCK_FUNCTION (fn_block
)));
318 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
319 Returns the symbol for the function corresponding to this frame. */
322 frapy_function (PyObject
*self
, PyObject
*args
)
324 struct symbol
*sym
= NULL
;
325 struct frame_info
*frame
;
329 enum language funlang
;
331 FRAPY_REQUIRE_VALID (self
, frame
);
333 gdb::unique_xmalloc_ptr
<char> funname
334 = find_frame_funname (frame
, &funlang
, &sym
);
336 catch (const gdb_exception
&except
)
338 GDB_PY_HANDLE_EXCEPTION (except
);
342 return symbol_to_symbol_object (sym
);
347 /* Convert a frame_info struct to a Python Frame object.
348 Sets a Python exception and returns NULL on error. */
351 frame_info_to_frame_object (struct frame_info
*frame
)
353 gdbpy_ref
<frame_object
> frame_obj (PyObject_New (frame_object
,
354 &frame_object_type
));
355 if (frame_obj
== NULL
)
361 /* Try to get the previous frame, to determine if this is the last frame
362 in a corrupt stack. If so, we need to store the frame_id of the next
363 frame and not of this one (which is possibly invalid). */
364 if (get_prev_frame (frame
) == NULL
365 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
366 && get_next_frame (frame
) != NULL
)
368 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
369 frame_obj
->frame_id_is_next
= 1;
373 frame_obj
->frame_id
= get_frame_id (frame
);
374 frame_obj
->frame_id_is_next
= 0;
376 frame_obj
->gdbarch
= get_frame_arch (frame
);
378 catch (const gdb_exception
&except
)
380 gdbpy_convert_exception (except
);
384 return (PyObject
*) frame_obj
.release ();
387 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
388 Returns the frame immediately older (outer) to this frame, or None if
392 frapy_older (PyObject
*self
, PyObject
*args
)
394 struct frame_info
*frame
, *prev
= NULL
;
395 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
399 FRAPY_REQUIRE_VALID (self
, frame
);
401 prev
= get_prev_frame (frame
);
403 catch (const gdb_exception
&except
)
405 GDB_PY_HANDLE_EXCEPTION (except
);
409 prev_obj
= frame_info_to_frame_object (prev
);
419 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
420 Returns the frame immediately newer (inner) to this frame, or None if
424 frapy_newer (PyObject
*self
, PyObject
*args
)
426 struct frame_info
*frame
, *next
= NULL
;
427 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
431 FRAPY_REQUIRE_VALID (self
, frame
);
433 next
= get_next_frame (frame
);
435 catch (const gdb_exception
&except
)
437 GDB_PY_HANDLE_EXCEPTION (except
);
441 next_obj
= frame_info_to_frame_object (next
);
451 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
452 Returns the frame's symtab and line. */
455 frapy_find_sal (PyObject
*self
, PyObject
*args
)
457 struct frame_info
*frame
;
458 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
462 FRAPY_REQUIRE_VALID (self
, frame
);
464 symtab_and_line sal
= find_frame_sal (frame
);
465 sal_obj
= symtab_and_line_to_sal_object (sal
);
467 catch (const gdb_exception
&except
)
469 GDB_PY_HANDLE_EXCEPTION (except
);
475 /* Implementation of gdb.Frame.read_var_value (self, variable,
476 [block]) -> gdb.Value. If the optional block argument is provided
477 start the search from that block, otherwise search from the frame's
478 current block (determined by examining the resume address of the
479 frame). The variable argument must be a string or an instance of a
480 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
481 NULL on error, with a python exception set. */
483 frapy_read_var (PyObject
*self
, PyObject
*args
)
485 struct frame_info
*frame
;
486 PyObject
*sym_obj
, *block_obj
= NULL
;
487 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
488 const struct block
*block
= NULL
;
489 struct value
*val
= NULL
;
491 if (!PyArg_ParseTuple (args
, "O|O", &sym_obj
, &block_obj
))
494 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
495 var
= symbol_object_to_symbol (sym_obj
);
496 else if (gdbpy_is_string (sym_obj
))
498 gdb::unique_xmalloc_ptr
<char>
499 var_name (python_string_to_target_string (sym_obj
));
506 block
= block_object_to_block (block_obj
);
509 PyErr_SetString (PyExc_RuntimeError
,
510 _("Second argument must be block."));
517 struct block_symbol lookup_sym
;
518 FRAPY_REQUIRE_VALID (self
, frame
);
521 block
= get_frame_block (frame
, NULL
);
522 lookup_sym
= lookup_symbol (var_name
.get (), block
, VAR_DOMAIN
, NULL
);
523 var
= lookup_sym
.symbol
;
524 block
= lookup_sym
.block
;
526 catch (const gdb_exception
&except
)
528 gdbpy_convert_exception (except
);
534 PyErr_Format (PyExc_ValueError
,
535 _("Variable '%s' not found."), var_name
.get ());
542 PyErr_SetString (PyExc_TypeError
,
543 _("Argument must be a symbol or string."));
549 FRAPY_REQUIRE_VALID (self
, frame
);
551 val
= read_var_value (var
, block
, frame
);
553 catch (const gdb_exception
&except
)
555 GDB_PY_HANDLE_EXCEPTION (except
);
558 return value_to_value_object (val
);
561 /* Select this frame. */
564 frapy_select (PyObject
*self
, PyObject
*args
)
566 struct frame_info
*fi
;
570 FRAPY_REQUIRE_VALID (self
, fi
);
574 catch (const gdb_exception
&except
)
576 GDB_PY_HANDLE_EXCEPTION (except
);
582 /* Implementation of gdb.newest_frame () -> gdb.Frame.
583 Returns the newest frame object. */
586 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
588 struct frame_info
*frame
= NULL
;
592 frame
= get_current_frame ();
594 catch (const gdb_exception
&except
)
596 GDB_PY_HANDLE_EXCEPTION (except
);
599 return frame_info_to_frame_object (frame
);
602 /* Implementation of gdb.selected_frame () -> gdb.Frame.
603 Returns the selected frame object. */
606 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
608 struct frame_info
*frame
= NULL
;
612 frame
= get_selected_frame ("No frame is currently selected.");
614 catch (const gdb_exception
&except
)
616 GDB_PY_HANDLE_EXCEPTION (except
);
619 return frame_info_to_frame_object (frame
);
622 /* Implementation of gdb.stop_reason_string (Integer) -> String.
623 Return a string explaining the unwind stop reason. */
626 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
631 if (!PyArg_ParseTuple (args
, "i", &reason
))
634 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
636 PyErr_SetString (PyExc_ValueError
,
637 _("Invalid frame stop reason."));
641 str
= unwind_stop_reason_to_string ((enum unwind_stop_reason
) reason
);
642 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
645 /* Implements the equality comparison for Frame objects.
646 All other comparison operators will throw a TypeError Python exception,
647 as they aren't valid for frames. */
650 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
654 if (!PyObject_TypeCheck (other
, &frame_object_type
)
655 || (op
!= Py_EQ
&& op
!= Py_NE
))
657 Py_INCREF (Py_NotImplemented
);
658 return Py_NotImplemented
;
661 if (frame_id_eq (((frame_object
*) self
)->frame_id
,
662 ((frame_object
*) other
)->frame_id
))
672 /* Sets up the Frame API in the gdb module. */
675 gdbpy_initialize_frames (void)
677 frame_object_type
.tp_new
= PyType_GenericNew
;
678 if (PyType_Ready (&frame_object_type
) < 0)
681 /* Note: These would probably be best exposed as class attributes of
682 Frame, but I don't know how to do it except by messing with the
683 type's dictionary. That seems too messy. */
684 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
685 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
686 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
687 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
689 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
691 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
692 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
696 #define SET(name, description) \
697 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
699 #include "unwind_stop_reasons.def"
702 return gdb_pymodule_addobject (gdb_module
, "Frame",
703 (PyObject
*) &frame_object_type
);
708 static PyMethodDef frame_object_methods
[] = {
709 { "is_valid", frapy_is_valid
, METH_NOARGS
,
710 "is_valid () -> Boolean.\n\
711 Return true if this frame is valid, false if not." },
712 { "name", frapy_name
, METH_NOARGS
,
713 "name () -> String.\n\
714 Return the function name of the frame, or None if it can't be determined." },
715 { "type", frapy_type
, METH_NOARGS
,
716 "type () -> Integer.\n\
717 Return the type of the frame." },
718 { "architecture", frapy_arch
, METH_NOARGS
,
719 "architecture () -> gdb.Architecture.\n\
720 Return the architecture of the frame." },
721 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
722 "unwind_stop_reason () -> Integer.\n\
723 Return the reason why it's not possible to find frames older than this." },
724 { "pc", frapy_pc
, METH_NOARGS
,
726 Return the frame's resume address." },
727 { "read_register", frapy_read_register
, METH_VARARGS
,
728 "read_register (register_name) -> gdb.Value\n\
729 Return the value of the register in the frame." },
730 { "block", frapy_block
, METH_NOARGS
,
731 "block () -> gdb.Block.\n\
732 Return the frame's code block." },
733 { "function", frapy_function
, METH_NOARGS
,
734 "function () -> gdb.Symbol.\n\
735 Returns the symbol for the function corresponding to this frame." },
736 { "older", frapy_older
, METH_NOARGS
,
737 "older () -> gdb.Frame.\n\
738 Return the frame that called this frame." },
739 { "newer", frapy_newer
, METH_NOARGS
,
740 "newer () -> gdb.Frame.\n\
741 Return the frame called by this frame." },
742 { "find_sal", frapy_find_sal
, METH_NOARGS
,
743 "find_sal () -> gdb.Symtab_and_line.\n\
744 Return the frame's symtab and line." },
745 { "read_var", frapy_read_var
, METH_VARARGS
,
746 "read_var (variable) -> gdb.Value.\n\
747 Return the value of the variable in this frame." },
748 { "select", frapy_select
, METH_NOARGS
,
749 "Select this frame as the user's current frame." },
750 {NULL
} /* Sentinel */
753 PyTypeObject frame_object_type
= {
754 PyVarObject_HEAD_INIT (NULL
, 0)
755 "gdb.Frame", /* tp_name */
756 sizeof (frame_object
), /* tp_basicsize */
764 0, /* tp_as_number */
765 0, /* tp_as_sequence */
766 0, /* tp_as_mapping */
769 frapy_str
, /* tp_str */
772 0, /* tp_as_buffer */
773 Py_TPFLAGS_DEFAULT
, /* tp_flags */
774 "GDB frame object", /* tp_doc */
777 frapy_richcompare
, /* tp_richcompare */
778 0, /* tp_weaklistoffset */
781 frame_object_methods
, /* tp_methods */
786 0, /* tp_descr_get */
787 0, /* tp_descr_set */
788 0, /* tp_dictoffset */