* ppc-opc.c (powerpc_opcodes) <"lswx">: Use RAX for the second and
[binutils-gdb.git] / gdb / python / py-frame.c
blob12a54e8cb2d0a420c47bc70b34d33601f5b39b5f
1 /* Python interface to stack frames
3 Copyright (C) 2008-2012 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 "exceptions.h"
25 #include "symtab.h"
26 #include "stack.h"
27 #include "value.h"
28 #include "python-internal.h"
29 #include "symfile.h"
30 #include "objfiles.h"
32 typedef struct {
33 PyObject_HEAD
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. */
45 int frame_id_is_next;
46 } frame_object;
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) \
51 do { \
52 frame = frame_object_to_frame_info (frame_obj); \
53 if (frame == NULL) \
54 error (_("Frame is invalid.")); \
55 } while (0)
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. */
61 struct frame_info *
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);
68 if (frame == NULL)
69 return NULL;
71 if (frame_obj->frame_id_is_next)
72 frame = get_prev_frame (frame);
74 return frame;
77 /* Called by the Python interpreter to obtain string representation
78 of the object. */
80 static PyObject *
81 frapy_str (PyObject *self)
83 char *s;
84 PyObject *result;
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);
91 xfree (s);
93 return result;
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. */
100 static PyObject *
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);
112 if (frame == NULL)
113 Py_RETURN_FALSE;
115 Py_RETURN_TRUE;
118 /* Implementation of gdb.Frame.name (self) -> String.
119 Returns the name of the function corresponding to this frame. */
121 static PyObject *
122 frapy_name (PyObject *self, PyObject *args)
124 struct frame_info *frame;
125 const char *name;
126 enum language lang;
127 PyObject *result;
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);
136 GDB_PY_HANDLE_EXCEPTION (except);
138 if (name)
139 result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL);
140 else
142 result = Py_None;
143 Py_INCREF (Py_None);
146 return result;
149 /* Implementation of gdb.Frame.type (self) -> Integer.
150 Returns the frame type, namely one of the gdb.*_FRAME constants. */
152 static PyObject *
153 frapy_type (PyObject *self, PyObject *args)
155 struct frame_info *frame;
156 enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning. */
157 volatile struct gdb_exception except;
159 TRY_CATCH (except, RETURN_MASK_ALL)
161 FRAPY_REQUIRE_VALID (self, frame);
163 type = get_frame_type (frame);
165 GDB_PY_HANDLE_EXCEPTION (except);
167 return PyInt_FromLong (type);
170 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
171 Returns one of the gdb.FRAME_UNWIND_* constants. */
173 static PyObject *
174 frapy_unwind_stop_reason (PyObject *self, PyObject *args)
176 struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */
177 volatile struct gdb_exception except;
178 enum unwind_stop_reason stop_reason;
180 TRY_CATCH (except, RETURN_MASK_ALL)
182 FRAPY_REQUIRE_VALID (self, frame);
184 GDB_PY_HANDLE_EXCEPTION (except);
186 stop_reason = get_frame_unwind_stop_reason (frame);
188 return PyInt_FromLong (stop_reason);
191 /* Implementation of gdb.Frame.pc (self) -> Long.
192 Returns the frame's resume address. */
194 static PyObject *
195 frapy_pc (PyObject *self, PyObject *args)
197 CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */
198 struct frame_info *frame;
199 volatile struct gdb_exception except;
201 TRY_CATCH (except, RETURN_MASK_ALL)
203 FRAPY_REQUIRE_VALID (self, frame);
205 pc = get_frame_pc (frame);
207 GDB_PY_HANDLE_EXCEPTION (except);
209 return gdb_py_long_from_ulongest (pc);
212 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
213 Returns the frame's code block. */
215 static PyObject *
216 frapy_block (PyObject *self, PyObject *args)
218 struct frame_info *frame;
219 struct block *block = NULL, *fn_block;
220 volatile struct gdb_exception except;
222 TRY_CATCH (except, RETURN_MASK_ALL)
224 FRAPY_REQUIRE_VALID (self, frame);
225 block = get_frame_block (frame, NULL);
227 GDB_PY_HANDLE_EXCEPTION (except);
229 for (fn_block = block;
230 fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL;
231 fn_block = BLOCK_SUPERBLOCK (fn_block))
234 if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL)
236 PyErr_SetString (PyExc_RuntimeError,
237 _("Cannot locate object file for block."));
238 return NULL;
241 if (block)
243 struct symtab *symt;
245 symt = SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block));
246 return block_to_block_object (block, symt->objfile);
249 Py_RETURN_NONE;
253 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
254 Returns the symbol for the function corresponding to this frame. */
256 static PyObject *
257 frapy_function (PyObject *self, PyObject *args)
259 struct symbol *sym = NULL;
260 struct frame_info *frame;
261 volatile struct gdb_exception except;
263 TRY_CATCH (except, RETURN_MASK_ALL)
265 FRAPY_REQUIRE_VALID (self, frame);
267 sym = find_pc_function (get_frame_address_in_block (frame));
269 GDB_PY_HANDLE_EXCEPTION (except);
271 if (sym)
272 return symbol_to_symbol_object (sym);
274 Py_RETURN_NONE;
277 /* Convert a frame_info struct to a Python Frame object.
278 Sets a Python exception and returns NULL on error. */
280 PyObject *
281 frame_info_to_frame_object (struct frame_info *frame)
283 frame_object *frame_obj;
284 volatile struct gdb_exception except;
286 frame_obj = PyObject_New (frame_object, &frame_object_type);
287 if (frame_obj == NULL)
289 PyErr_SetString (PyExc_MemoryError,
290 _("Could not allocate frame object."));
291 return NULL;
294 TRY_CATCH (except, RETURN_MASK_ALL)
297 /* Try to get the previous frame, to determine if this is the last frame
298 in a corrupt stack. If so, we need to store the frame_id of the next
299 frame and not of this one (which is possibly invalid). */
300 if (get_prev_frame (frame) == NULL
301 && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
302 && get_next_frame (frame) != NULL)
304 frame_obj->frame_id = get_frame_id (get_next_frame (frame));
305 frame_obj->frame_id_is_next = 1;
307 else
309 frame_obj->frame_id = get_frame_id (frame);
310 frame_obj->frame_id_is_next = 0;
312 frame_obj->gdbarch = get_frame_arch (frame);
314 GDB_PY_HANDLE_EXCEPTION (except);
316 return (PyObject *) frame_obj;
319 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
320 Returns the frame immediately older (outer) to this frame, or None if
321 there isn't one. */
323 static PyObject *
324 frapy_older (PyObject *self, PyObject *args)
326 struct frame_info *frame, *prev;
327 volatile struct gdb_exception except;
328 PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */
330 TRY_CATCH (except, RETURN_MASK_ALL)
332 FRAPY_REQUIRE_VALID (self, frame);
334 prev = get_prev_frame (frame);
335 if (prev)
336 prev_obj = (PyObject *) frame_info_to_frame_object (prev);
337 else
339 Py_INCREF (Py_None);
340 prev_obj = Py_None;
343 GDB_PY_HANDLE_EXCEPTION (except);
345 return prev_obj;
348 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
349 Returns the frame immediately newer (inner) to this frame, or None if
350 there isn't one. */
352 static PyObject *
353 frapy_newer (PyObject *self, PyObject *args)
355 struct frame_info *frame, *next;
356 volatile struct gdb_exception except;
357 PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */
359 TRY_CATCH (except, RETURN_MASK_ALL)
361 FRAPY_REQUIRE_VALID (self, frame);
363 next = get_next_frame (frame);
364 if (next)
365 next_obj = (PyObject *) frame_info_to_frame_object (next);
366 else
368 Py_INCREF (Py_None);
369 next_obj = Py_None;
372 GDB_PY_HANDLE_EXCEPTION (except);
374 return next_obj;
377 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
378 Returns the frame's symtab and line. */
380 static PyObject *
381 frapy_find_sal (PyObject *self, PyObject *args)
383 struct frame_info *frame;
384 struct symtab_and_line sal;
385 volatile struct gdb_exception except;
386 PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */
388 TRY_CATCH (except, RETURN_MASK_ALL)
390 FRAPY_REQUIRE_VALID (self, frame);
392 find_frame_sal (frame, &sal);
393 sal_obj = symtab_and_line_to_sal_object (sal);
395 GDB_PY_HANDLE_EXCEPTION (except);
397 return sal_obj;
400 /* Implementation of gdb.Frame.read_var_value (self, variable,
401 [block]) -> gdb.Value. If the optional block argument is provided
402 start the search from that block, otherwise search from the frame's
403 current block (determined by examining the resume address of the
404 frame). The variable argument must be a string or an instance of a
405 gdb.Symbol. The block argument must be an instance of gdb.Block. Returns
406 NULL on error, with a python exception set. */
407 static PyObject *
408 frapy_read_var (PyObject *self, PyObject *args)
410 struct frame_info *frame;
411 PyObject *sym_obj, *block_obj = NULL;
412 struct symbol *var = NULL; /* gcc-4.3.2 false warning. */
413 struct value *val = NULL;
414 volatile struct gdb_exception except;
416 if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj))
417 return NULL;
419 if (PyObject_TypeCheck (sym_obj, &symbol_object_type))
420 var = symbol_object_to_symbol (sym_obj);
421 else if (gdbpy_is_string (sym_obj))
423 char *var_name;
424 const struct block *block = NULL;
425 struct cleanup *cleanup;
426 volatile struct gdb_exception except;
428 var_name = python_string_to_target_string (sym_obj);
429 if (!var_name)
430 return NULL;
431 cleanup = make_cleanup (xfree, var_name);
433 if (block_obj)
435 block = block_object_to_block (block_obj);
436 if (!block)
438 PyErr_SetString (PyExc_RuntimeError,
439 _("Second argument must be block."));
440 return NULL;
444 TRY_CATCH (except, RETURN_MASK_ALL)
446 FRAPY_REQUIRE_VALID (self, frame);
448 if (!block)
449 block = get_frame_block (frame, NULL);
450 var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL);
452 GDB_PY_HANDLE_EXCEPTION (except);
454 if (!var)
456 PyErr_Format (PyExc_ValueError,
457 _("Variable '%s' not found."), var_name);
458 do_cleanups (cleanup);
460 return NULL;
463 do_cleanups (cleanup);
465 else
467 PyErr_SetString (PyExc_TypeError,
468 _("Argument must be a symbol or string."));
469 return NULL;
472 TRY_CATCH (except, RETURN_MASK_ALL)
474 FRAPY_REQUIRE_VALID (self, frame);
476 val = read_var_value (var, frame);
478 GDB_PY_HANDLE_EXCEPTION (except);
480 return value_to_value_object (val);
483 /* Select this frame. */
485 static PyObject *
486 frapy_select (PyObject *self, PyObject *args)
488 struct frame_info *fi;
489 volatile struct gdb_exception except;
491 TRY_CATCH (except, RETURN_MASK_ALL)
493 FRAPY_REQUIRE_VALID (self, fi);
495 select_frame (fi);
497 GDB_PY_HANDLE_EXCEPTION (except);
499 Py_RETURN_NONE;
502 /* Implementation of gdb.newest_frame () -> gdb.Frame.
503 Returns the newest frame object. */
505 PyObject *
506 gdbpy_newest_frame (PyObject *self, PyObject *args)
508 struct frame_info *frame;
509 PyObject *frame_obj = NULL; /* Initialize to appease gcc warning. */
510 volatile struct gdb_exception except;
512 TRY_CATCH (except, RETURN_MASK_ALL)
514 frame = get_current_frame ();
515 frame_obj = frame_info_to_frame_object (frame);
517 GDB_PY_HANDLE_EXCEPTION (except);
519 return frame_obj;
522 /* Implementation of gdb.selected_frame () -> gdb.Frame.
523 Returns the selected frame object. */
525 PyObject *
526 gdbpy_selected_frame (PyObject *self, PyObject *args)
528 struct frame_info *frame;
529 PyObject *frame_obj = NULL; /* Initialize to appease gcc warning. */
530 volatile struct gdb_exception except;
532 TRY_CATCH (except, RETURN_MASK_ALL)
534 frame = get_selected_frame ("No frame is currently selected.");
535 frame_obj = frame_info_to_frame_object (frame);
537 GDB_PY_HANDLE_EXCEPTION (except);
539 return frame_obj;
542 /* Implementation of gdb.stop_reason_string (Integer) -> String.
543 Return a string explaining the unwind stop reason. */
545 PyObject *
546 gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
548 int reason;
549 const char *str;
551 if (!PyArg_ParseTuple (args, "i", &reason))
552 return NULL;
554 if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
556 PyErr_SetString (PyExc_ValueError,
557 _("Invalid frame stop reason."));
558 return NULL;
561 str = frame_stop_reason_string (reason);
562 return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
565 /* Implements the equality comparison for Frame objects.
566 All other comparison operators will throw a TypeError Python exception,
567 as they aren't valid for frames. */
569 static PyObject *
570 frapy_richcompare (PyObject *self, PyObject *other, int op)
572 int result;
574 if (!PyObject_TypeCheck (other, &frame_object_type)
575 || (op != Py_EQ && op != Py_NE))
577 Py_INCREF (Py_NotImplemented);
578 return Py_NotImplemented;
581 if (frame_id_eq (((frame_object *) self)->frame_id,
582 ((frame_object *) other)->frame_id))
583 result = Py_EQ;
584 else
585 result = Py_NE;
587 if (op == result)
588 Py_RETURN_TRUE;
589 Py_RETURN_FALSE;
592 /* Sets up the Frame API in the gdb module. */
594 void
595 gdbpy_initialize_frames (void)
597 frame_object_type.tp_new = PyType_GenericNew;
598 if (PyType_Ready (&frame_object_type) < 0)
599 return;
601 /* Note: These would probably be best exposed as class attributes of
602 Frame, but I don't know how to do it except by messing with the
603 type's dictionary. That seems too messy. */
604 PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME);
605 PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME);
606 PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME);
607 PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", TAILCALL_FRAME);
608 PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", SIGTRAMP_FRAME);
609 PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME);
610 PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", SENTINEL_FRAME);
612 #define SET(name, description) \
613 PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
614 #define FIRST_ERROR(name) \
615 PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
616 #include "unwind_stop_reasons.def"
617 #undef SET
619 Py_INCREF (&frame_object_type);
620 PyModule_AddObject (gdb_module, "Frame", (PyObject *) &frame_object_type);
625 static PyMethodDef frame_object_methods[] = {
626 { "is_valid", frapy_is_valid, METH_NOARGS,
627 "is_valid () -> Boolean.\n\
628 Return true if this frame is valid, false if not." },
629 { "name", frapy_name, METH_NOARGS,
630 "name () -> String.\n\
631 Return the function name of the frame, or None if it can't be determined." },
632 { "type", frapy_type, METH_NOARGS,
633 "type () -> Integer.\n\
634 Return the type of the frame." },
635 { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
636 "unwind_stop_reason () -> Integer.\n\
637 Return the reason why it's not possible to find frames older than this." },
638 { "pc", frapy_pc, METH_NOARGS,
639 "pc () -> Long.\n\
640 Return the frame's resume address." },
641 { "block", frapy_block, METH_NOARGS,
642 "block () -> gdb.Block.\n\
643 Return the frame's code block." },
644 { "function", frapy_function, METH_NOARGS,
645 "function () -> gdb.Symbol.\n\
646 Returns the symbol for the function corresponding to this frame." },
647 { "older", frapy_older, METH_NOARGS,
648 "older () -> gdb.Frame.\n\
649 Return the frame that called this frame." },
650 { "newer", frapy_newer, METH_NOARGS,
651 "newer () -> gdb.Frame.\n\
652 Return the frame called by this frame." },
653 { "find_sal", frapy_find_sal, METH_NOARGS,
654 "find_sal () -> gdb.Symtab_and_line.\n\
655 Return the frame's symtab and line." },
656 { "read_var", frapy_read_var, METH_VARARGS,
657 "read_var (variable) -> gdb.Value.\n\
658 Return the value of the variable in this frame." },
659 { "select", frapy_select, METH_NOARGS,
660 "Select this frame as the user's current frame." },
661 {NULL} /* Sentinel */
664 PyTypeObject frame_object_type = {
665 PyObject_HEAD_INIT (NULL)
666 0, /* ob_size */
667 "gdb.Frame", /* tp_name */
668 sizeof (frame_object), /* tp_basicsize */
669 0, /* tp_itemsize */
670 0, /* tp_dealloc */
671 0, /* tp_print */
672 0, /* tp_getattr */
673 0, /* tp_setattr */
674 0, /* tp_compare */
675 0, /* tp_repr */
676 0, /* tp_as_number */
677 0, /* tp_as_sequence */
678 0, /* tp_as_mapping */
679 0, /* tp_hash */
680 0, /* tp_call */
681 frapy_str, /* tp_str */
682 0, /* tp_getattro */
683 0, /* tp_setattro */
684 0, /* tp_as_buffer */
685 Py_TPFLAGS_DEFAULT, /* tp_flags */
686 "GDB frame object", /* tp_doc */
687 0, /* tp_traverse */
688 0, /* tp_clear */
689 frapy_richcompare, /* tp_richcompare */
690 0, /* tp_weaklistoffset */
691 0, /* tp_iter */
692 0, /* tp_iternext */
693 frame_object_methods, /* tp_methods */
694 0, /* tp_members */
695 0, /* tp_getset */
696 0, /* tp_base */
697 0, /* tp_dict */
698 0, /* tp_descr_get */
699 0, /* tp_descr_set */
700 0, /* tp_dictoffset */
701 0, /* tp_init */
702 0, /* tp_alloc */