Document the GDB 10.2 release in gdb/ChangeLog
[binutils-gdb.git] / gdb / python / py-frame.c
blob8e32ba55de4909c25326aed7fc39c7e672a43093
1 /* Python interface to stack frames
3 Copyright (C) 2008-2021 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/>. */
20 #include "defs.h"
21 #include "charset.h"
22 #include "block.h"
23 #include "frame.h"
24 #include "symtab.h"
25 #include "stack.h"
26 #include "value.h"
27 #include "python-internal.h"
28 #include "symfile.h"
29 #include "objfiles.h"
31 struct frame_object {
32 PyObject_HEAD
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. */
44 int frame_id_is_next;
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) \
50 do { \
51 frame = frame_object_to_frame_info (frame_obj); \
52 if (frame == NULL) \
53 error (_("Frame is invalid.")); \
54 } while (0)
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. */
60 struct frame_info *
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);
67 if (frame == NULL)
68 return NULL;
70 if (frame_obj->frame_id_is_next)
71 frame = get_prev_frame (frame);
73 return frame;
76 /* Called by the Python interpreter to obtain string representation
77 of the object. */
79 static PyObject *
80 frapy_str (PyObject *self)
82 string_file strfile;
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. */
92 static PyObject *
93 frapy_is_valid (PyObject *self, PyObject *args)
95 struct frame_info *frame = NULL;
97 try
99 frame = frame_object_to_frame_info (self);
101 catch (const gdb_exception &except)
103 GDB_PY_HANDLE_EXCEPTION (except);
106 if (frame == NULL)
107 Py_RETURN_FALSE;
109 Py_RETURN_TRUE;
112 /* Implementation of gdb.Frame.name (self) -> String.
113 Returns the name of the function corresponding to this frame. */
115 static PyObject *
116 frapy_name (PyObject *self, PyObject *args)
118 struct frame_info *frame;
119 gdb::unique_xmalloc_ptr<char> name;
120 enum language lang;
121 PyObject *result;
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);
134 if (name)
136 result = PyUnicode_Decode (name.get (), strlen (name.get ()),
137 host_charset (), NULL);
139 else
141 result = Py_None;
142 Py_INCREF (Py_None);
145 return result;
148 /* Implementation of gdb.Frame.type (self) -> Integer.
149 Returns the frame type, namely one of the gdb.*_FRAME constants. */
151 static PyObject *
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. */
174 static PyObject *
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. */
195 static PyObject *
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. */
218 static PyObject *
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. */
241 static PyObject *
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))
248 return NULL;
251 struct frame_info *frame;
252 int regnum;
254 FRAPY_REQUIRE_VALID (self, frame);
256 if (!gdbpy_parse_register_id (get_frame_arch (frame), pyo_reg_id,
257 &regnum))
259 PyErr_SetString (PyExc_ValueError, "Bad register");
260 return NULL;
263 gdb_assert (regnum >= 0);
264 val = value_of_register (regnum, frame);
266 if (val == NULL)
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. */
280 static PyObject *
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."));
305 return NULL;
308 if (block)
310 return block_to_block_object
311 (block, symbol_objfile (BLOCK_FUNCTION (fn_block)));
314 Py_RETURN_NONE;
318 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
319 Returns the symbol for the function corresponding to this frame. */
321 static PyObject *
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);
341 if (sym)
342 return symbol_to_symbol_object (sym);
344 Py_RETURN_NONE;
347 /* Convert a frame_info struct to a Python Frame object.
348 Sets a Python exception and returns NULL on error. */
350 PyObject *
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)
356 return 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;
371 else
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);
381 return NULL;
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
389 there isn't one. */
391 static PyObject *
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);
408 if (prev)
409 prev_obj = frame_info_to_frame_object (prev);
410 else
412 Py_INCREF (Py_None);
413 prev_obj = Py_None;
416 return prev_obj;
419 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
420 Returns the frame immediately newer (inner) to this frame, or None if
421 there isn't one. */
423 static PyObject *
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);
440 if (next)
441 next_obj = frame_info_to_frame_object (next);
442 else
444 Py_INCREF (Py_None);
445 next_obj = Py_None;
448 return next_obj;
451 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
452 Returns the frame's symtab and line. */
454 static PyObject *
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);
472 return sal_obj;
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. */
482 static PyObject *
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))
492 return NULL;
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));
501 if (!var_name)
502 return NULL;
504 if (block_obj)
506 block = block_object_to_block (block_obj);
507 if (!block)
509 PyErr_SetString (PyExc_RuntimeError,
510 _("Second argument must be block."));
511 return NULL;
517 struct block_symbol lookup_sym;
518 FRAPY_REQUIRE_VALID (self, frame);
520 if (!block)
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);
529 return NULL;
532 if (!var)
534 PyErr_Format (PyExc_ValueError,
535 _("Variable '%s' not found."), var_name.get ());
537 return NULL;
540 else
542 PyErr_SetString (PyExc_TypeError,
543 _("Argument must be a symbol or string."));
544 return NULL;
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. */
563 static PyObject *
564 frapy_select (PyObject *self, PyObject *args)
566 struct frame_info *fi;
570 FRAPY_REQUIRE_VALID (self, fi);
572 select_frame (fi);
574 catch (const gdb_exception &except)
576 GDB_PY_HANDLE_EXCEPTION (except);
579 Py_RETURN_NONE;
582 /* Implementation of gdb.newest_frame () -> gdb.Frame.
583 Returns the newest frame object. */
585 PyObject *
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. */
605 PyObject *
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. */
625 PyObject *
626 gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
628 int reason;
629 const char *str;
631 if (!PyArg_ParseTuple (args, "i", &reason))
632 return NULL;
634 if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
636 PyErr_SetString (PyExc_ValueError,
637 _("Invalid frame stop reason."));
638 return NULL;
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. */
649 static PyObject *
650 frapy_richcompare (PyObject *self, PyObject *other, int op)
652 int result;
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 frame_object *self_frame = (frame_object *) self;
662 frame_object *other_frame = (frame_object *) other;
664 if (self_frame->frame_id_is_next == other_frame->frame_id_is_next
665 && frame_id_eq (self_frame->frame_id, other_frame->frame_id))
666 result = Py_EQ;
667 else
668 result = Py_NE;
670 if (op == result)
671 Py_RETURN_TRUE;
672 Py_RETURN_FALSE;
675 /* Sets up the Frame API in the gdb module. */
678 gdbpy_initialize_frames (void)
680 frame_object_type.tp_new = PyType_GenericNew;
681 if (PyType_Ready (&frame_object_type) < 0)
682 return -1;
684 /* Note: These would probably be best exposed as class attributes of
685 Frame, but I don't know how to do it except by messing with the
686 type's dictionary. That seems too messy. */
687 if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0
688 || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0
689 || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0
690 || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME",
691 TAILCALL_FRAME) < 0
692 || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME",
693 SIGTRAMP_FRAME) < 0
694 || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0
695 || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME",
696 SENTINEL_FRAME) < 0)
697 return -1;
699 #define SET(name, description) \
700 if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
701 return -1;
702 #include "unwind_stop_reasons.def"
703 #undef SET
705 return gdb_pymodule_addobject (gdb_module, "Frame",
706 (PyObject *) &frame_object_type);
711 static PyMethodDef frame_object_methods[] = {
712 { "is_valid", frapy_is_valid, METH_NOARGS,
713 "is_valid () -> Boolean.\n\
714 Return true if this frame is valid, false if not." },
715 { "name", frapy_name, METH_NOARGS,
716 "name () -> String.\n\
717 Return the function name of the frame, or None if it can't be determined." },
718 { "type", frapy_type, METH_NOARGS,
719 "type () -> Integer.\n\
720 Return the type of the frame." },
721 { "architecture", frapy_arch, METH_NOARGS,
722 "architecture () -> gdb.Architecture.\n\
723 Return the architecture of the frame." },
724 { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
725 "unwind_stop_reason () -> Integer.\n\
726 Return the reason why it's not possible to find frames older than this." },
727 { "pc", frapy_pc, METH_NOARGS,
728 "pc () -> Long.\n\
729 Return the frame's resume address." },
730 { "read_register", frapy_read_register, METH_VARARGS,
731 "read_register (register_name) -> gdb.Value\n\
732 Return the value of the register in the frame." },
733 { "block", frapy_block, METH_NOARGS,
734 "block () -> gdb.Block.\n\
735 Return the frame's code block." },
736 { "function", frapy_function, METH_NOARGS,
737 "function () -> gdb.Symbol.\n\
738 Returns the symbol for the function corresponding to this frame." },
739 { "older", frapy_older, METH_NOARGS,
740 "older () -> gdb.Frame.\n\
741 Return the frame that called this frame." },
742 { "newer", frapy_newer, METH_NOARGS,
743 "newer () -> gdb.Frame.\n\
744 Return the frame called by this frame." },
745 { "find_sal", frapy_find_sal, METH_NOARGS,
746 "find_sal () -> gdb.Symtab_and_line.\n\
747 Return the frame's symtab and line." },
748 { "read_var", frapy_read_var, METH_VARARGS,
749 "read_var (variable) -> gdb.Value.\n\
750 Return the value of the variable in this frame." },
751 { "select", frapy_select, METH_NOARGS,
752 "Select this frame as the user's current frame." },
753 {NULL} /* Sentinel */
756 PyTypeObject frame_object_type = {
757 PyVarObject_HEAD_INIT (NULL, 0)
758 "gdb.Frame", /* tp_name */
759 sizeof (frame_object), /* tp_basicsize */
760 0, /* tp_itemsize */
761 0, /* tp_dealloc */
762 0, /* tp_print */
763 0, /* tp_getattr */
764 0, /* tp_setattr */
765 0, /* tp_compare */
766 0, /* tp_repr */
767 0, /* tp_as_number */
768 0, /* tp_as_sequence */
769 0, /* tp_as_mapping */
770 0, /* tp_hash */
771 0, /* tp_call */
772 frapy_str, /* tp_str */
773 0, /* tp_getattro */
774 0, /* tp_setattro */
775 0, /* tp_as_buffer */
776 Py_TPFLAGS_DEFAULT, /* tp_flags */
777 "GDB frame object", /* tp_doc */
778 0, /* tp_traverse */
779 0, /* tp_clear */
780 frapy_richcompare, /* tp_richcompare */
781 0, /* tp_weaklistoffset */
782 0, /* tp_iter */
783 0, /* tp_iternext */
784 frame_object_methods, /* tp_methods */
785 0, /* tp_members */
786 0, /* tp_getset */
787 0, /* tp_base */
788 0, /* tp_dict */
789 0, /* tp_descr_get */
790 0, /* tp_descr_set */
791 0, /* tp_dictoffset */
792 0, /* tp_init */
793 0, /* tp_alloc */