1 /* Python interface to stack frames
3 Copyright (C) 2008-2013 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"
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
;
65 struct frame_info
*frame
;
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
)
85 struct ui_file
*strfile
;
87 strfile
= mem_fileopen ();
88 fprint_frame_id (strfile
, ((frame_object
*) self
)->frame_id
);
89 s
= ui_file_xstrdup (strfile
, NULL
);
90 result
= PyString_FromString (s
);
96 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
97 Returns True if the frame corresponding to the frame_id of this
98 object still exists in the inferior. */
101 frapy_is_valid (PyObject
*self
, PyObject
*args
)
103 struct frame_info
*frame
= NULL
;
104 volatile struct gdb_exception except
;
106 TRY_CATCH (except
, RETURN_MASK_ALL
)
108 frame
= frame_object_to_frame_info (self
);
110 GDB_PY_HANDLE_EXCEPTION (except
);
118 /* Implementation of gdb.Frame.name (self) -> String.
119 Returns the name of the function corresponding to this frame. */
122 frapy_name (PyObject
*self
, PyObject
*args
)
124 struct frame_info
*frame
;
128 volatile struct gdb_exception except
;
130 TRY_CATCH (except
, RETURN_MASK_ALL
)
132 FRAPY_REQUIRE_VALID (self
, frame
);
134 find_frame_funname (frame
, &name
, &lang
, NULL
);
137 if (except
.reason
< 0)
140 GDB_PY_HANDLE_EXCEPTION (except
);
144 result
= PyUnicode_Decode (name
, strlen (name
), host_charset (), NULL
);
156 /* Implementation of gdb.Frame.type (self) -> Integer.
157 Returns the frame type, namely one of the gdb.*_FRAME constants. */
160 frapy_type (PyObject
*self
, PyObject
*args
)
162 struct frame_info
*frame
;
163 enum frame_type type
= NORMAL_FRAME
;/* Initialize to appease gcc warning. */
164 volatile struct gdb_exception except
;
166 TRY_CATCH (except
, RETURN_MASK_ALL
)
168 FRAPY_REQUIRE_VALID (self
, frame
);
170 type
= get_frame_type (frame
);
172 GDB_PY_HANDLE_EXCEPTION (except
);
174 return PyInt_FromLong (type
);
177 /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
178 Returns the frame's architecture as a gdb.Architecture object. */
181 frapy_arch (PyObject
*self
, PyObject
*args
)
183 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
184 frame_object
*obj
= (frame_object
*) self
;
185 volatile struct gdb_exception except
;
187 TRY_CATCH (except
, RETURN_MASK_ALL
)
189 FRAPY_REQUIRE_VALID (self
, frame
);
191 GDB_PY_HANDLE_EXCEPTION (except
);
193 return gdbarch_to_arch_object (obj
->gdbarch
);
196 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
197 Returns one of the gdb.FRAME_UNWIND_* constants. */
200 frapy_unwind_stop_reason (PyObject
*self
, PyObject
*args
)
202 struct frame_info
*frame
= NULL
; /* Initialize to appease gcc warning. */
203 volatile struct gdb_exception except
;
204 enum unwind_stop_reason stop_reason
;
206 TRY_CATCH (except
, RETURN_MASK_ALL
)
208 FRAPY_REQUIRE_VALID (self
, frame
);
210 GDB_PY_HANDLE_EXCEPTION (except
);
212 stop_reason
= get_frame_unwind_stop_reason (frame
);
214 return PyInt_FromLong (stop_reason
);
217 /* Implementation of gdb.Frame.pc (self) -> Long.
218 Returns the frame's resume address. */
221 frapy_pc (PyObject
*self
, PyObject
*args
)
223 CORE_ADDR pc
= 0; /* Initialize to appease gcc warning. */
224 struct frame_info
*frame
;
225 volatile struct gdb_exception except
;
227 TRY_CATCH (except
, RETURN_MASK_ALL
)
229 FRAPY_REQUIRE_VALID (self
, frame
);
231 pc
= get_frame_pc (frame
);
233 GDB_PY_HANDLE_EXCEPTION (except
);
235 return gdb_py_long_from_ulongest (pc
);
238 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
239 Returns the frame's code block. */
242 frapy_block (PyObject
*self
, PyObject
*args
)
244 struct frame_info
*frame
;
245 struct block
*block
= NULL
, *fn_block
;
246 volatile struct gdb_exception except
;
248 TRY_CATCH (except
, RETURN_MASK_ALL
)
250 FRAPY_REQUIRE_VALID (self
, frame
);
251 block
= get_frame_block (frame
, NULL
);
253 GDB_PY_HANDLE_EXCEPTION (except
);
255 for (fn_block
= block
;
256 fn_block
!= NULL
&& BLOCK_FUNCTION (fn_block
) == NULL
;
257 fn_block
= BLOCK_SUPERBLOCK (fn_block
))
260 if (block
== NULL
|| fn_block
== NULL
|| BLOCK_FUNCTION (fn_block
) == NULL
)
262 PyErr_SetString (PyExc_RuntimeError
,
263 _("Cannot locate object file for block."));
271 symt
= SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block
));
272 return block_to_block_object (block
, symt
->objfile
);
279 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
280 Returns the symbol for the function corresponding to this frame. */
283 frapy_function (PyObject
*self
, PyObject
*args
)
285 struct symbol
*sym
= NULL
;
286 struct frame_info
*frame
;
287 volatile struct gdb_exception except
;
289 TRY_CATCH (except
, RETURN_MASK_ALL
)
291 FRAPY_REQUIRE_VALID (self
, frame
);
293 sym
= find_pc_function (get_frame_address_in_block (frame
));
295 GDB_PY_HANDLE_EXCEPTION (except
);
298 return symbol_to_symbol_object (sym
);
303 /* Convert a frame_info struct to a Python Frame object.
304 Sets a Python exception and returns NULL on error. */
307 frame_info_to_frame_object (struct frame_info
*frame
)
309 frame_object
*frame_obj
;
310 volatile struct gdb_exception except
;
312 frame_obj
= PyObject_New (frame_object
, &frame_object_type
);
313 if (frame_obj
== NULL
)
316 TRY_CATCH (except
, RETURN_MASK_ALL
)
319 /* Try to get the previous frame, to determine if this is the last frame
320 in a corrupt stack. If so, we need to store the frame_id of the next
321 frame and not of this one (which is possibly invalid). */
322 if (get_prev_frame (frame
) == NULL
323 && get_frame_unwind_stop_reason (frame
) != UNWIND_NO_REASON
324 && get_next_frame (frame
) != NULL
)
326 frame_obj
->frame_id
= get_frame_id (get_next_frame (frame
));
327 frame_obj
->frame_id_is_next
= 1;
331 frame_obj
->frame_id
= get_frame_id (frame
);
332 frame_obj
->frame_id_is_next
= 0;
334 frame_obj
->gdbarch
= get_frame_arch (frame
);
336 GDB_PY_HANDLE_EXCEPTION (except
);
338 return (PyObject
*) frame_obj
;
341 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
342 Returns the frame immediately older (outer) to this frame, or None if
346 frapy_older (PyObject
*self
, PyObject
*args
)
348 struct frame_info
*frame
, *prev
= NULL
;
349 volatile struct gdb_exception except
;
350 PyObject
*prev_obj
= NULL
; /* Initialize to appease gcc warning. */
352 TRY_CATCH (except
, RETURN_MASK_ALL
)
354 FRAPY_REQUIRE_VALID (self
, frame
);
356 prev
= get_prev_frame (frame
);
358 GDB_PY_HANDLE_EXCEPTION (except
);
361 prev_obj
= (PyObject
*) frame_info_to_frame_object (prev
);
371 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
372 Returns the frame immediately newer (inner) to this frame, or None if
376 frapy_newer (PyObject
*self
, PyObject
*args
)
378 struct frame_info
*frame
, *next
= NULL
;
379 volatile struct gdb_exception except
;
380 PyObject
*next_obj
= NULL
; /* Initialize to appease gcc warning. */
382 TRY_CATCH (except
, RETURN_MASK_ALL
)
384 FRAPY_REQUIRE_VALID (self
, frame
);
386 next
= get_next_frame (frame
);
388 GDB_PY_HANDLE_EXCEPTION (except
);
391 next_obj
= (PyObject
*) frame_info_to_frame_object (next
);
401 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
402 Returns the frame's symtab and line. */
405 frapy_find_sal (PyObject
*self
, PyObject
*args
)
407 struct frame_info
*frame
;
408 struct symtab_and_line sal
;
409 volatile struct gdb_exception except
;
410 PyObject
*sal_obj
= NULL
; /* Initialize to appease gcc warning. */
412 TRY_CATCH (except
, RETURN_MASK_ALL
)
414 FRAPY_REQUIRE_VALID (self
, frame
);
416 find_frame_sal (frame
, &sal
);
417 sal_obj
= symtab_and_line_to_sal_object (sal
);
419 GDB_PY_HANDLE_EXCEPTION (except
);
424 /* Implementation of gdb.Frame.read_var_value (self, variable,
425 [block]) -> gdb.Value. If the optional block argument is provided
426 start the search from that block, otherwise search from the frame's
427 current block (determined by examining the resume address of the
428 frame). The variable argument must be a string or an instance of a
429 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
430 NULL on error, with a python exception set. */
432 frapy_read_var (PyObject
*self
, PyObject
*args
)
434 struct frame_info
*frame
;
435 PyObject
*sym_obj
, *block_obj
= NULL
;
436 struct symbol
*var
= NULL
; /* gcc-4.3.2 false warning. */
437 struct value
*val
= NULL
;
438 volatile struct gdb_exception except
;
440 if (!PyArg_ParseTuple (args
, "O|O", &sym_obj
, &block_obj
))
443 if (PyObject_TypeCheck (sym_obj
, &symbol_object_type
))
444 var
= symbol_object_to_symbol (sym_obj
);
445 else if (gdbpy_is_string (sym_obj
))
448 const struct block
*block
= NULL
;
449 struct cleanup
*cleanup
;
450 volatile struct gdb_exception except
;
452 var_name
= python_string_to_target_string (sym_obj
);
455 cleanup
= make_cleanup (xfree
, var_name
);
459 block
= block_object_to_block (block_obj
);
462 PyErr_SetString (PyExc_RuntimeError
,
463 _("Second argument must be block."));
464 do_cleanups (cleanup
);
469 TRY_CATCH (except
, RETURN_MASK_ALL
)
471 FRAPY_REQUIRE_VALID (self
, frame
);
474 block
= get_frame_block (frame
, NULL
);
475 var
= lookup_symbol (var_name
, block
, VAR_DOMAIN
, NULL
);
477 if (except
.reason
< 0)
479 do_cleanups (cleanup
);
480 gdbpy_convert_exception (except
);
486 PyErr_Format (PyExc_ValueError
,
487 _("Variable '%s' not found."), var_name
);
488 do_cleanups (cleanup
);
493 do_cleanups (cleanup
);
497 PyErr_SetString (PyExc_TypeError
,
498 _("Argument must be a symbol or string."));
502 TRY_CATCH (except
, RETURN_MASK_ALL
)
504 FRAPY_REQUIRE_VALID (self
, frame
);
506 val
= read_var_value (var
, frame
);
508 GDB_PY_HANDLE_EXCEPTION (except
);
510 return value_to_value_object (val
);
513 /* Select this frame. */
516 frapy_select (PyObject
*self
, PyObject
*args
)
518 struct frame_info
*fi
;
519 volatile struct gdb_exception except
;
521 TRY_CATCH (except
, RETURN_MASK_ALL
)
523 FRAPY_REQUIRE_VALID (self
, fi
);
527 GDB_PY_HANDLE_EXCEPTION (except
);
532 /* Implementation of gdb.newest_frame () -> gdb.Frame.
533 Returns the newest frame object. */
536 gdbpy_newest_frame (PyObject
*self
, PyObject
*args
)
538 struct frame_info
*frame
= NULL
;
539 volatile struct gdb_exception except
;
541 TRY_CATCH (except
, RETURN_MASK_ALL
)
543 frame
= get_current_frame ();
545 GDB_PY_HANDLE_EXCEPTION (except
);
547 return frame_info_to_frame_object (frame
);
550 /* Implementation of gdb.selected_frame () -> gdb.Frame.
551 Returns the selected frame object. */
554 gdbpy_selected_frame (PyObject
*self
, PyObject
*args
)
556 struct frame_info
*frame
= NULL
;
557 volatile struct gdb_exception except
;
559 TRY_CATCH (except
, RETURN_MASK_ALL
)
561 frame
= get_selected_frame ("No frame is currently selected.");
563 GDB_PY_HANDLE_EXCEPTION (except
);
565 return frame_info_to_frame_object (frame
);
568 /* Implementation of gdb.stop_reason_string (Integer) -> String.
569 Return a string explaining the unwind stop reason. */
572 gdbpy_frame_stop_reason_string (PyObject
*self
, PyObject
*args
)
577 if (!PyArg_ParseTuple (args
, "i", &reason
))
580 if (reason
< UNWIND_FIRST
|| reason
> UNWIND_LAST
)
582 PyErr_SetString (PyExc_ValueError
,
583 _("Invalid frame stop reason."));
587 str
= frame_stop_reason_string (reason
);
588 return PyUnicode_Decode (str
, strlen (str
), host_charset (), NULL
);
591 /* Implements the equality comparison for Frame objects.
592 All other comparison operators will throw a TypeError Python exception,
593 as they aren't valid for frames. */
596 frapy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
600 if (!PyObject_TypeCheck (other
, &frame_object_type
)
601 || (op
!= Py_EQ
&& op
!= Py_NE
))
603 Py_INCREF (Py_NotImplemented
);
604 return Py_NotImplemented
;
607 if (frame_id_eq (((frame_object
*) self
)->frame_id
,
608 ((frame_object
*) other
)->frame_id
))
618 /* Sets up the Frame API in the gdb module. */
621 gdbpy_initialize_frames (void)
623 frame_object_type
.tp_new
= PyType_GenericNew
;
624 if (PyType_Ready (&frame_object_type
) < 0)
627 /* Note: These would probably be best exposed as class attributes of
628 Frame, but I don't know how to do it except by messing with the
629 type's dictionary. That seems too messy. */
630 if (PyModule_AddIntConstant (gdb_module
, "NORMAL_FRAME", NORMAL_FRAME
) < 0
631 || PyModule_AddIntConstant (gdb_module
, "DUMMY_FRAME", DUMMY_FRAME
) < 0
632 || PyModule_AddIntConstant (gdb_module
, "INLINE_FRAME", INLINE_FRAME
) < 0
633 || PyModule_AddIntConstant (gdb_module
, "TAILCALL_FRAME",
635 || PyModule_AddIntConstant (gdb_module
, "SIGTRAMP_FRAME",
637 || PyModule_AddIntConstant (gdb_module
, "ARCH_FRAME", ARCH_FRAME
) < 0
638 || PyModule_AddIntConstant (gdb_module
, "SENTINEL_FRAME",
642 #define SET(name, description) \
643 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
645 #define FIRST_ERROR(name) \
646 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
648 #include "unwind_stop_reasons.def"
652 return gdb_pymodule_addobject (gdb_module
, "Frame",
653 (PyObject
*) &frame_object_type
);
658 static PyMethodDef frame_object_methods
[] = {
659 { "is_valid", frapy_is_valid
, METH_NOARGS
,
660 "is_valid () -> Boolean.\n\
661 Return true if this frame is valid, false if not." },
662 { "name", frapy_name
, METH_NOARGS
,
663 "name () -> String.\n\
664 Return the function name of the frame, or None if it can't be determined." },
665 { "type", frapy_type
, METH_NOARGS
,
666 "type () -> Integer.\n\
667 Return the type of the frame." },
668 { "architecture", frapy_arch
, METH_NOARGS
,
669 "architecture () -> gdb.Architecture.\n\
670 Return the architecture of the frame." },
671 { "unwind_stop_reason", frapy_unwind_stop_reason
, METH_NOARGS
,
672 "unwind_stop_reason () -> Integer.\n\
673 Return the reason why it's not possible to find frames older than this." },
674 { "pc", frapy_pc
, METH_NOARGS
,
676 Return the frame's resume address." },
677 { "block", frapy_block
, METH_NOARGS
,
678 "block () -> gdb.Block.\n\
679 Return the frame's code block." },
680 { "function", frapy_function
, METH_NOARGS
,
681 "function () -> gdb.Symbol.\n\
682 Returns the symbol for the function corresponding to this frame." },
683 { "older", frapy_older
, METH_NOARGS
,
684 "older () -> gdb.Frame.\n\
685 Return the frame that called this frame." },
686 { "newer", frapy_newer
, METH_NOARGS
,
687 "newer () -> gdb.Frame.\n\
688 Return the frame called by this frame." },
689 { "find_sal", frapy_find_sal
, METH_NOARGS
,
690 "find_sal () -> gdb.Symtab_and_line.\n\
691 Return the frame's symtab and line." },
692 { "read_var", frapy_read_var
, METH_VARARGS
,
693 "read_var (variable) -> gdb.Value.\n\
694 Return the value of the variable in this frame." },
695 { "select", frapy_select
, METH_NOARGS
,
696 "Select this frame as the user's current frame." },
697 {NULL
} /* Sentinel */
700 PyTypeObject frame_object_type
= {
701 PyVarObject_HEAD_INIT (NULL
, 0)
702 "gdb.Frame", /* tp_name */
703 sizeof (frame_object
), /* tp_basicsize */
711 0, /* tp_as_number */
712 0, /* tp_as_sequence */
713 0, /* tp_as_mapping */
716 frapy_str
, /* tp_str */
719 0, /* tp_as_buffer */
720 Py_TPFLAGS_DEFAULT
, /* tp_flags */
721 "GDB frame object", /* tp_doc */
724 frapy_richcompare
, /* tp_richcompare */
725 0, /* tp_weaklistoffset */
728 frame_object_methods
, /* tp_methods */
733 0, /* tp_descr_get */
734 0, /* tp_descr_set */
735 0, /* tp_dictoffset */