1 /* Python interface to stack frames
3 Copyright (C) 2008-2014 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/>. */
24 #include "exceptions.h"
28 #include "python-internal.h"
31 #include "user-regs.h"
35 struct frame_id frame_id
;
36 struct gdbarch
*gdbarch
;
38 /* Marks that the FRAME_ID member actually holds the ID of the frame next
39 to this, and not this frames' ID itself. This is a hack to permit Python
40 frame objects which represent invalid frames (i.e., the last frame_info
41 in a corrupt stack). The problem arises from the fact that this code
42 relies on FRAME_ID to uniquely identify a frame, which is not always true
43 for the last "frame" in a corrupt stack (it can have a null ID, or the same
44 ID as the previous frame). Whenever get_prev_frame returns NULL, we
45 record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */
49 /* Require a valid frame. This must be called inside a TRY_CATCH, or
50 another context in which a gdb exception is allowed. */
51 #define FRAPY_REQUIRE_VALID(frame_obj, frame) \
53 frame = frame_object_to_frame_info (frame_obj); \
55 error (_("Frame is invalid.")); \
58 /* Returns the frame_info object corresponding to the given Python Frame
59 object. If the frame doesn't exist anymore (the frame id doesn't
60 correspond to any frame in the inferior), returns NULL. */
63 frame_object_to_frame_info (PyObject
*obj
)
65 frame_object
*frame_obj
= (frame_object
*) obj
;
66 struct frame_info
*frame
;
68 frame
= frame_find_by_id (frame_obj
->frame_id
);
72 if (frame_obj
->frame_id_is_next
)
73 frame
= get_prev_frame (frame
);
78 /* Called by the Python interpreter to obtain string representation
82 frapy_str (PyObject
*self
)
86 struct ui_file
*strfile
;
88 strfile
= mem_fileopen ();
89 fprint_frame_id (strfile
, ((frame_object
*) self
)->frame_id
);
90 s
= ui_file_xstrdup (strfile
, NULL
);
91 result
= PyString_FromString (s
);
97 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
98 Returns True if the frame corresponding to the frame_id of this
99 object still exists in the inferior. */
102 frapy_is_valid (PyObject
*self
, PyObject
*args
)
104 struct frame_info
*frame
= NULL
;
105 volatile struct gdb_exception except
;
107 TRY_CATCH (except
, RETURN_MASK_ALL
)
109 frame
= frame_object_to_frame_info (self
);
111 GDB_PY_HANDLE_EXCEPTION (except
);
119 /* Implementation of gdb.Frame.name (self) -> String.
120 Returns the name of the function corresponding to this frame. */
123 frapy_name (PyObject
*self
, PyObject
*args
)
125 struct frame_info
*frame
;
129 volatile struct gdb_exception except
;
131 TRY_CATCH (except
, RETURN_MASK_ALL
)
133 FRAPY_REQUIRE_VALID (self
, frame
);
135 find_frame_funname (frame
, &name
, &lang
, NULL
);
138 if (except
.reason
< 0)
141 GDB_PY_HANDLE_EXCEPTION (except
);
145 result
= PyUnicode_Decode (name
, strlen (name
), host_charset (), NULL
);
157 /* Implementation of gdb.Frame.type (self) -> Integer.
158 Returns the frame type, namely one of the gdb.*_FRAME constants. */
161 frapy_type (PyObject
*self
, PyObject
*args
)
163 struct frame_info
*frame
;
164 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
165 volatile struct gdb_exception except
;
167 TRY_CATCH (except
, RETURN_MASK_ALL
)
169 FRAPY_REQUIRE_VALID (self
, frame
);
171 type
= get_frame_type (frame
);
173 GDB_PY_HANDLE_EXCEPTION (except
);
175 return PyInt_FromLong (type
);
178 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
179 Returns the frame's architecture as a gdb.Architecture object. */
182 frapy_arch (PyObject
*self
, PyObject
*args
)
184 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
185 frame_object
*obj
= (frame_object
*) self
;
186 volatile struct gdb_exception except
;
188 TRY_CATCH (except
, RETURN_MASK_ALL
)
190 FRAPY_REQUIRE_VALID (self
, frame
);
192 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 volatile struct gdb_exception except
;
205 enum unwind_stop_reason stop_reason
;
207 TRY_CATCH (except
, RETURN_MASK_ALL
)
209 FRAPY_REQUIRE_VALID (self
, frame
);
211 GDB_PY_HANDLE_EXCEPTION (except
);
213 stop_reason
= get_frame_unwind_stop_reason (frame
);
215 return PyInt_FromLong (stop_reason
);
218 /* Implementation of gdb.Frame.pc (self) -> Long.
219 Returns the frame's resume address. */
222 frapy_pc (PyObject
*self
, PyObject
*args
)
224 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
225 struct frame_info
*frame
;
226 volatile struct gdb_exception except
;
228 TRY_CATCH (except
, RETURN_MASK_ALL
)
230 FRAPY_REQUIRE_VALID (self
, frame
);
232 pc
= get_frame_pc (frame
);
234 GDB_PY_HANDLE_EXCEPTION (except
);
236 return gdb_py_long_from_ulongest (pc
);
239 /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
240 Returns the value of a register in this frame. */
243 frapy_read_register (PyObject
*self
, PyObject
*args
)
245 volatile struct gdb_exception except
;
246 const char *regnum_str
;
247 struct value
*val
= NULL
;
249 if (!PyArg_ParseTuple (args
, "s", ®num_str
))
252 TRY_CATCH (except
, RETURN_MASK_ALL
)
254 struct frame_info
*frame
;
257 FRAPY_REQUIRE_VALID (self
, frame
);
259 regnum
= user_reg_map_name_to_regnum (get_frame_arch (frame
),
261 strlen (regnum_str
));
263 val
= value_of_register (regnum
, frame
);
266 PyErr_SetString (PyExc_ValueError
, _("Unknown register."));
268 GDB_PY_HANDLE_EXCEPTION (except
);
270 return val
== NULL
? NULL
: value_to_value_object (val
);
273 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
274 Returns the frame's code block. */
277 frapy_block (PyObject
*self
, PyObject
*args
)
279 struct frame_info
*frame
;
280 const struct block
*block
= NULL
, *fn_block
;
281 volatile struct gdb_exception except
;
283 TRY_CATCH (except
, RETURN_MASK_ALL
)
285 FRAPY_REQUIRE_VALID (self
, frame
);
286 block
= get_frame_block (frame
, NULL
);
288 GDB_PY_HANDLE_EXCEPTION (except
);
290 for (fn_block
= block
;
291 fn_block
!= NULL
&& BLOCK_FUNCTION (fn_block
) == NULL
;
292 fn_block
= BLOCK_SUPERBLOCK (fn_block
))
295 if (block
== NULL
|| fn_block
== NULL
|| BLOCK_FUNCTION (fn_block
) == NULL
)
297 PyErr_SetString (PyExc_RuntimeError
,
298 _("Cannot locate block for frame."));
306 symt
= SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block
));
307 return block_to_block_object (block
, symt
->objfile
);
314 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
315 Returns the symbol for the function corresponding to this frame. */
318 frapy_function (PyObject
*self
, PyObject
*args
)
320 struct symbol
*sym
= NULL
;
321 struct frame_info
*frame
;
322 volatile struct gdb_exception except
;
324 TRY_CATCH (except
, RETURN_MASK_ALL
)
326 FRAPY_REQUIRE_VALID (self
, frame
);
328 sym
= find_pc_function (get_frame_address_in_block (frame
));
330 GDB_PY_HANDLE_EXCEPTION (except
);
333 return symbol_to_symbol_object (sym
);
338 /* Convert a frame_info struct to a Python Frame object.
339 Sets a Python exception and returns NULL on error. */
342 frame_info_to_frame_object (struct frame_info
*frame
)
344 frame_object
*frame_obj
;
345 volatile struct gdb_exception except
;
347 frame_obj
= PyObject_New (frame_object
, &frame_object_type
);
348 if (frame_obj
== NULL
)
351 TRY_CATCH (except
, RETURN_MASK_ALL
)
354 /* Try to get the previous frame, to determine if this is the last frame
355 in a corrupt stack. If so, we need to store the frame_id of the next
356 frame and not of this one (which is possibly invalid). */
357 if (get_prev_frame (frame
) == NULL
358 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
359 && get_next_frame (frame
) != NULL
)
361 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
362 frame_obj
->frame_id_is_next
= 1;
366 frame_obj
->frame_id
= get_frame_id (frame
);
367 frame_obj
->frame_id_is_next
= 0;
369 frame_obj
->gdbarch
= get_frame_arch (frame
);
371 if (except
.reason
< 0)
373 Py_DECREF (frame_obj
);
374 gdbpy_convert_exception (except
);
377 return (PyObject
*) frame_obj
;
380 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
381 Returns the frame immediately older (outer) to this frame, or None if
385 frapy_older (PyObject
*self
, PyObject
*args
)
387 struct frame_info
*frame
, *prev
= NULL
;
388 volatile struct gdb_exception except
;
389 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
391 TRY_CATCH (except
, RETURN_MASK_ALL
)
393 FRAPY_REQUIRE_VALID (self
, frame
);
395 prev
= get_prev_frame (frame
);
397 GDB_PY_HANDLE_EXCEPTION (except
);
400 prev_obj
= (PyObject
*) frame_info_to_frame_object (prev
);
410 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
411 Returns the frame immediately newer (inner) to this frame, or None if
415 frapy_newer (PyObject
*self
, PyObject
*args
)
417 struct frame_info
*frame
, *next
= NULL
;
418 volatile struct gdb_exception except
;
419 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
421 TRY_CATCH (except
, RETURN_MASK_ALL
)
423 FRAPY_REQUIRE_VALID (self
, frame
);
425 next
= get_next_frame (frame
);
427 GDB_PY_HANDLE_EXCEPTION (except
);
430 next_obj
= (PyObject
*) frame_info_to_frame_object (next
);
440 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
441 Returns the frame's symtab and line. */
444 frapy_find_sal (PyObject
*self
, PyObject
*args
)
446 struct frame_info
*frame
;
447 struct symtab_and_line sal
;
448 volatile struct gdb_exception except
;
449 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
451 TRY_CATCH (except
, RETURN_MASK_ALL
)
453 FRAPY_REQUIRE_VALID (self
, frame
);
455 find_frame_sal (frame
, &sal
);
456 sal_obj
= symtab_and_line_to_sal_object (sal
);
458 GDB_PY_HANDLE_EXCEPTION (except
);
463 /* Implementation of gdb.Frame.read_var_value (self, variable,
464 [block]) -> gdb.Value. If the optional block argument is provided
465 start the search from that block, otherwise search from the frame's
466 current block (determined by examining the resume address of the
467 frame). The variable argument must be a string or an instance of a
468 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
469 NULL on error, with a python exception set. */
471 frapy_read_var (PyObject
*self
, PyObject
*args
)
473 struct frame_info
*frame
;
474 PyObject
*sym_obj
, *block_obj
= NULL
;
475 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
476 struct value
*val
= NULL
;
477 volatile struct gdb_exception except
;
479 if (!PyArg_ParseTuple (args
, "O|O", &sym_obj
, &block_obj
))
482 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
483 var
= symbol_object_to_symbol (sym_obj
);
484 else if (gdbpy_is_string (sym_obj
))
487 const struct block
*block
= NULL
;
488 struct cleanup
*cleanup
;
489 volatile struct gdb_exception except
;
491 var_name
= python_string_to_target_string (sym_obj
);
494 cleanup
= make_cleanup (xfree
, var_name
);
498 block
= block_object_to_block (block_obj
);
501 PyErr_SetString (PyExc_RuntimeError
,
502 _("Second argument must be block."));
503 do_cleanups (cleanup
);
508 TRY_CATCH (except
, RETURN_MASK_ALL
)
510 FRAPY_REQUIRE_VALID (self
, frame
);
513 block
= get_frame_block (frame
, NULL
);
514 var
= lookup_symbol (var_name
, block
, VAR_DOMAIN
, NULL
);
516 if (except
.reason
< 0)
518 do_cleanups (cleanup
);
519 gdbpy_convert_exception (except
);
525 PyErr_Format (PyExc_ValueError
,
526 _("Variable '%s' not found."), var_name
);
527 do_cleanups (cleanup
);
532 do_cleanups (cleanup
);
536 PyErr_SetString (PyExc_TypeError
,
537 _("Argument must be a symbol or string."));
541 TRY_CATCH (except
, RETURN_MASK_ALL
)
543 FRAPY_REQUIRE_VALID (self
, frame
);
545 val
= read_var_value (var
, frame
);
547 GDB_PY_HANDLE_EXCEPTION (except
);
549 return value_to_value_object (val
);
552 /* Select this frame. */
555 frapy_select (PyObject
*self
, PyObject
*args
)
557 struct frame_info
*fi
;
558 volatile struct gdb_exception except
;
560 TRY_CATCH (except
, RETURN_MASK_ALL
)
562 FRAPY_REQUIRE_VALID (self
, fi
);
566 GDB_PY_HANDLE_EXCEPTION (except
);
571 /* Implementation of gdb.newest_frame () -> gdb.Frame.
572 Returns the newest frame object. */
575 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
577 struct frame_info
*frame
= NULL
;
578 volatile struct gdb_exception except
;
580 TRY_CATCH (except
, RETURN_MASK_ALL
)
582 frame
= get_current_frame ();
584 GDB_PY_HANDLE_EXCEPTION (except
);
586 return frame_info_to_frame_object (frame
);
589 /* Implementation of gdb.selected_frame () -> gdb.Frame.
590 Returns the selected frame object. */
593 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
595 struct frame_info
*frame
= NULL
;
596 volatile struct gdb_exception except
;
598 TRY_CATCH (except
, RETURN_MASK_ALL
)
600 frame
= get_selected_frame ("No frame is currently selected.");
602 GDB_PY_HANDLE_EXCEPTION (except
);
604 return frame_info_to_frame_object (frame
);
607 /* Implementation of gdb.stop_reason_string (Integer) -> String.
608 Return a string explaining the unwind stop reason. */
611 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
616 if (!PyArg_ParseTuple (args
, "i", &reason
))
619 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
621 PyErr_SetString (PyExc_ValueError
,
622 _("Invalid frame stop reason."));
626 str
= unwind_stop_reason_to_string (reason
);
627 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
630 /* Implements the equality comparison for Frame objects.
631 All other comparison operators will throw a TypeError Python exception,
632 as they aren't valid for frames. */
635 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
639 if (!PyObject_TypeCheck (other
, &frame_object_type
)
640 || (op
!= Py_EQ
&& op
!= Py_NE
))
642 Py_INCREF (Py_NotImplemented
);
643 return Py_NotImplemented
;
646 if (frame_id_eq (((frame_object
*) self
)->frame_id
,
647 ((frame_object
*) other
)->frame_id
))
657 /* Sets up the Frame API in the gdb module. */
660 gdbpy_initialize_frames (void)
662 frame_object_type
.tp_new
= PyType_GenericNew
;
663 if (PyType_Ready (&frame_object_type
) < 0)
666 /* Note: These would probably be best exposed as class attributes of
667 Frame, but I don't know how to do it except by messing with the
668 type's dictionary. That seems too messy. */
669 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
670 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
671 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
672 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
674 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
676 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
677 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
681 #define SET(name, description) \
682 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
684 #include "unwind_stop_reasons.def"
687 return gdb_pymodule_addobject (gdb_module
, "Frame",
688 (PyObject
*) &frame_object_type
);
693 static PyMethodDef frame_object_methods
[] = {
694 { "is_valid", frapy_is_valid
, METH_NOARGS
,
695 "is_valid () -> Boolean.\n\
696 Return true if this frame is valid, false if not." },
697 { "name", frapy_name
, METH_NOARGS
,
698 "name () -> String.\n\
699 Return the function name of the frame, or None if it can't be determined." },
700 { "type", frapy_type
, METH_NOARGS
,
701 "type () -> Integer.\n\
702 Return the type of the frame." },
703 { "architecture", frapy_arch
, METH_NOARGS
,
704 "architecture () -> gdb.Architecture.\n\
705 Return the architecture of the frame." },
706 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
707 "unwind_stop_reason () -> Integer.\n\
708 Return the reason why it's not possible to find frames older than this." },
709 { "pc", frapy_pc
, METH_NOARGS
,
711 Return the frame's resume address." },
712 { "read_register", frapy_read_register
, METH_VARARGS
,
713 "read_register (register_name) -> gdb.Value\n\
714 Return the value of the register in the frame." },
715 { "block", frapy_block
, METH_NOARGS
,
716 "block () -> gdb.Block.\n\
717 Return the frame's code block." },
718 { "function", frapy_function
, METH_NOARGS
,
719 "function () -> gdb.Symbol.\n\
720 Returns the symbol for the function corresponding to this frame." },
721 { "older", frapy_older
, METH_NOARGS
,
722 "older () -> gdb.Frame.\n\
723 Return the frame that called this frame." },
724 { "newer", frapy_newer
, METH_NOARGS
,
725 "newer () -> gdb.Frame.\n\
726 Return the frame called by this frame." },
727 { "find_sal", frapy_find_sal
, METH_NOARGS
,
728 "find_sal () -> gdb.Symtab_and_line.\n\
729 Return the frame's symtab and line." },
730 { "read_var", frapy_read_var
, METH_VARARGS
,
731 "read_var (variable) -> gdb.Value.\n\
732 Return the value of the variable in this frame." },
733 { "select", frapy_select
, METH_NOARGS
,
734 "Select this frame as the user's current frame." },
735 {NULL
} /* Sentinel */
738 PyTypeObject frame_object_type
= {
739 PyVarObject_HEAD_INIT (NULL
, 0)
740 "gdb.Frame", /* tp_name */
741 sizeof (frame_object
), /* tp_basicsize */
749 0, /* tp_as_number */
750 0, /* tp_as_sequence */
751 0, /* tp_as_mapping */
754 frapy_str
, /* tp_str */
757 0, /* tp_as_buffer */
758 Py_TPFLAGS_DEFAULT
, /* tp_flags */
759 "GDB frame object", /* tp_doc */
762 frapy_richcompare
, /* tp_richcompare */
763 0, /* tp_weaklistoffset */
766 frame_object_methods
, /* tp_methods */
771 0, /* tp_descr_get */
772 0, /* tp_descr_set */
773 0, /* tp_dictoffset */