Home | History | Annotate | Line # | Download | only in python
py-frame.c revision 1.1
      1  1.1  christos /* Python interface to stack frames
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2008-2014 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "charset.h"
     22  1.1  christos #include "block.h"
     23  1.1  christos #include "frame.h"
     24  1.1  christos #include "exceptions.h"
     25  1.1  christos #include "symtab.h"
     26  1.1  christos #include "stack.h"
     27  1.1  christos #include "value.h"
     28  1.1  christos #include "python-internal.h"
     29  1.1  christos #include "symfile.h"
     30  1.1  christos #include "objfiles.h"
     31  1.1  christos 
     32  1.1  christos typedef struct {
     33  1.1  christos   PyObject_HEAD
     34  1.1  christos   struct frame_id frame_id;
     35  1.1  christos   struct gdbarch *gdbarch;
     36  1.1  christos 
     37  1.1  christos   /* Marks that the FRAME_ID member actually holds the ID of the frame next
     38  1.1  christos      to this, and not this frames' ID itself.  This is a hack to permit Python
     39  1.1  christos      frame objects which represent invalid frames (i.e., the last frame_info
     40  1.1  christos      in a corrupt stack).  The problem arises from the fact that this code
     41  1.1  christos      relies on FRAME_ID to uniquely identify a frame, which is not always true
     42  1.1  christos      for the last "frame" in a corrupt stack (it can have a null ID, or the same
     43  1.1  christos      ID as the  previous frame).  Whenever get_prev_frame returns NULL, we
     44  1.1  christos      record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1.  */
     45  1.1  christos   int frame_id_is_next;
     46  1.1  christos } frame_object;
     47  1.1  christos 
     48  1.1  christos /* Require a valid frame.  This must be called inside a TRY_CATCH, or
     49  1.1  christos    another context in which a gdb exception is allowed.  */
     50  1.1  christos #define FRAPY_REQUIRE_VALID(frame_obj, frame)		\
     51  1.1  christos     do {						\
     52  1.1  christos       frame = frame_object_to_frame_info (frame_obj);	\
     53  1.1  christos       if (frame == NULL)				\
     54  1.1  christos 	error (_("Frame is invalid."));			\
     55  1.1  christos     } while (0)
     56  1.1  christos 
     57  1.1  christos /* Returns the frame_info object corresponding to the given Python Frame
     58  1.1  christos    object.  If the frame doesn't exist anymore (the frame id doesn't
     59  1.1  christos    correspond to any frame in the inferior), returns NULL.  */
     60  1.1  christos 
     61  1.1  christos struct frame_info *
     62  1.1  christos frame_object_to_frame_info (PyObject *obj)
     63  1.1  christos {
     64  1.1  christos   frame_object *frame_obj = (frame_object *) obj;
     65  1.1  christos   struct frame_info *frame;
     66  1.1  christos 
     67  1.1  christos   frame = frame_find_by_id (frame_obj->frame_id);
     68  1.1  christos   if (frame == NULL)
     69  1.1  christos     return NULL;
     70  1.1  christos 
     71  1.1  christos   if (frame_obj->frame_id_is_next)
     72  1.1  christos     frame = get_prev_frame (frame);
     73  1.1  christos 
     74  1.1  christos   return frame;
     75  1.1  christos }
     76  1.1  christos 
     77  1.1  christos /* Called by the Python interpreter to obtain string representation
     78  1.1  christos    of the object.  */
     79  1.1  christos 
     80  1.1  christos static PyObject *
     81  1.1  christos frapy_str (PyObject *self)
     82  1.1  christos {
     83  1.1  christos   char *s;
     84  1.1  christos   PyObject *result;
     85  1.1  christos   struct ui_file *strfile;
     86  1.1  christos 
     87  1.1  christos   strfile = mem_fileopen ();
     88  1.1  christos   fprint_frame_id (strfile, ((frame_object *) self)->frame_id);
     89  1.1  christos   s = ui_file_xstrdup (strfile, NULL);
     90  1.1  christos   result = PyString_FromString (s);
     91  1.1  christos   xfree (s);
     92  1.1  christos 
     93  1.1  christos   return result;
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
     97  1.1  christos    Returns True if the frame corresponding to the frame_id of this
     98  1.1  christos    object still exists in the inferior.  */
     99  1.1  christos 
    100  1.1  christos static PyObject *
    101  1.1  christos frapy_is_valid (PyObject *self, PyObject *args)
    102  1.1  christos {
    103  1.1  christos   struct frame_info *frame = NULL;
    104  1.1  christos   volatile struct gdb_exception except;
    105  1.1  christos 
    106  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    107  1.1  christos     {
    108  1.1  christos       frame = frame_object_to_frame_info (self);
    109  1.1  christos     }
    110  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    111  1.1  christos 
    112  1.1  christos   if (frame == NULL)
    113  1.1  christos     Py_RETURN_FALSE;
    114  1.1  christos 
    115  1.1  christos   Py_RETURN_TRUE;
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos /* Implementation of gdb.Frame.name (self) -> String.
    119  1.1  christos    Returns the name of the function corresponding to this frame.  */
    120  1.1  christos 
    121  1.1  christos static PyObject *
    122  1.1  christos frapy_name (PyObject *self, PyObject *args)
    123  1.1  christos {
    124  1.1  christos   struct frame_info *frame;
    125  1.1  christos   char *name = NULL;
    126  1.1  christos   enum language lang;
    127  1.1  christos   PyObject *result;
    128  1.1  christos   volatile struct gdb_exception except;
    129  1.1  christos 
    130  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    131  1.1  christos     {
    132  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    133  1.1  christos 
    134  1.1  christos       find_frame_funname (frame, &name, &lang, NULL);
    135  1.1  christos     }
    136  1.1  christos 
    137  1.1  christos   if (except.reason < 0)
    138  1.1  christos     xfree (name);
    139  1.1  christos 
    140  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    141  1.1  christos 
    142  1.1  christos   if (name)
    143  1.1  christos     {
    144  1.1  christos       result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL);
    145  1.1  christos       xfree (name);
    146  1.1  christos     }
    147  1.1  christos   else
    148  1.1  christos     {
    149  1.1  christos       result = Py_None;
    150  1.1  christos       Py_INCREF (Py_None);
    151  1.1  christos     }
    152  1.1  christos 
    153  1.1  christos   return result;
    154  1.1  christos }
    155  1.1  christos 
    156  1.1  christos /* Implementation of gdb.Frame.type (self) -> Integer.
    157  1.1  christos    Returns the frame type, namely one of the gdb.*_FRAME constants.  */
    158  1.1  christos 
    159  1.1  christos static PyObject *
    160  1.1  christos frapy_type (PyObject *self, PyObject *args)
    161  1.1  christos {
    162  1.1  christos   struct frame_info *frame;
    163  1.1  christos   enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning.  */
    164  1.1  christos   volatile struct gdb_exception except;
    165  1.1  christos 
    166  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    167  1.1  christos     {
    168  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    169  1.1  christos 
    170  1.1  christos       type = get_frame_type (frame);
    171  1.1  christos     }
    172  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    173  1.1  christos 
    174  1.1  christos   return PyInt_FromLong (type);
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
    178  1.1  christos    Returns the frame's architecture as a gdb.Architecture object.  */
    179  1.1  christos 
    180  1.1  christos static PyObject *
    181  1.1  christos frapy_arch (PyObject *self, PyObject *args)
    182  1.1  christos {
    183  1.1  christos   struct frame_info *frame = NULL;    /* Initialize to appease gcc warning.  */
    184  1.1  christos   frame_object *obj = (frame_object *) self;
    185  1.1  christos   volatile struct gdb_exception except;
    186  1.1  christos 
    187  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    188  1.1  christos     {
    189  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    190  1.1  christos     }
    191  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    192  1.1  christos 
    193  1.1  christos   return gdbarch_to_arch_object (obj->gdbarch);
    194  1.1  christos }
    195  1.1  christos 
    196  1.1  christos /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
    197  1.1  christos    Returns one of the gdb.FRAME_UNWIND_* constants.  */
    198  1.1  christos 
    199  1.1  christos static PyObject *
    200  1.1  christos frapy_unwind_stop_reason (PyObject *self, PyObject *args)
    201  1.1  christos {
    202  1.1  christos   struct frame_info *frame = NULL;    /* Initialize to appease gcc warning.  */
    203  1.1  christos   volatile struct gdb_exception except;
    204  1.1  christos   enum unwind_stop_reason stop_reason;
    205  1.1  christos 
    206  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    207  1.1  christos     {
    208  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    209  1.1  christos     }
    210  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    211  1.1  christos 
    212  1.1  christos   stop_reason = get_frame_unwind_stop_reason (frame);
    213  1.1  christos 
    214  1.1  christos   return PyInt_FromLong (stop_reason);
    215  1.1  christos }
    216  1.1  christos 
    217  1.1  christos /* Implementation of gdb.Frame.pc (self) -> Long.
    218  1.1  christos    Returns the frame's resume address.  */
    219  1.1  christos 
    220  1.1  christos static PyObject *
    221  1.1  christos frapy_pc (PyObject *self, PyObject *args)
    222  1.1  christos {
    223  1.1  christos   CORE_ADDR pc = 0;	      /* Initialize to appease gcc warning.  */
    224  1.1  christos   struct frame_info *frame;
    225  1.1  christos   volatile struct gdb_exception except;
    226  1.1  christos 
    227  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    228  1.1  christos     {
    229  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    230  1.1  christos 
    231  1.1  christos       pc = get_frame_pc (frame);
    232  1.1  christos     }
    233  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    234  1.1  christos 
    235  1.1  christos   return gdb_py_long_from_ulongest (pc);
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos /* Implementation of gdb.Frame.block (self) -> gdb.Block.
    239  1.1  christos    Returns the frame's code block.  */
    240  1.1  christos 
    241  1.1  christos static PyObject *
    242  1.1  christos frapy_block (PyObject *self, PyObject *args)
    243  1.1  christos {
    244  1.1  christos   struct frame_info *frame;
    245  1.1  christos   struct block *block = NULL, *fn_block;
    246  1.1  christos   volatile struct gdb_exception except;
    247  1.1  christos 
    248  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    249  1.1  christos     {
    250  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    251  1.1  christos       block = get_frame_block (frame, NULL);
    252  1.1  christos     }
    253  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    254  1.1  christos 
    255  1.1  christos   for (fn_block = block;
    256  1.1  christos        fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL;
    257  1.1  christos        fn_block = BLOCK_SUPERBLOCK (fn_block))
    258  1.1  christos     ;
    259  1.1  christos 
    260  1.1  christos   if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL)
    261  1.1  christos     {
    262  1.1  christos       PyErr_SetString (PyExc_RuntimeError,
    263  1.1  christos 		       _("Cannot locate block for frame."));
    264  1.1  christos       return NULL;
    265  1.1  christos     }
    266  1.1  christos 
    267  1.1  christos   if (block)
    268  1.1  christos     {
    269  1.1  christos       struct symtab *symt;
    270  1.1  christos 
    271  1.1  christos       symt = SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block));
    272  1.1  christos       return block_to_block_object (block, symt->objfile);
    273  1.1  christos     }
    274  1.1  christos 
    275  1.1  christos   Py_RETURN_NONE;
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos 
    279  1.1  christos /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
    280  1.1  christos    Returns the symbol for the function corresponding to this frame.  */
    281  1.1  christos 
    282  1.1  christos static PyObject *
    283  1.1  christos frapy_function (PyObject *self, PyObject *args)
    284  1.1  christos {
    285  1.1  christos   struct symbol *sym = NULL;
    286  1.1  christos   struct frame_info *frame;
    287  1.1  christos   volatile struct gdb_exception except;
    288  1.1  christos 
    289  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    290  1.1  christos     {
    291  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    292  1.1  christos 
    293  1.1  christos       sym = find_pc_function (get_frame_address_in_block (frame));
    294  1.1  christos     }
    295  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    296  1.1  christos 
    297  1.1  christos   if (sym)
    298  1.1  christos     return symbol_to_symbol_object (sym);
    299  1.1  christos 
    300  1.1  christos   Py_RETURN_NONE;
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos /* Convert a frame_info struct to a Python Frame object.
    304  1.1  christos    Sets a Python exception and returns NULL on error.  */
    305  1.1  christos 
    306  1.1  christos PyObject *
    307  1.1  christos frame_info_to_frame_object (struct frame_info *frame)
    308  1.1  christos {
    309  1.1  christos   frame_object *frame_obj;
    310  1.1  christos   volatile struct gdb_exception except;
    311  1.1  christos 
    312  1.1  christos   frame_obj = PyObject_New (frame_object, &frame_object_type);
    313  1.1  christos   if (frame_obj == NULL)
    314  1.1  christos     return NULL;
    315  1.1  christos 
    316  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    317  1.1  christos     {
    318  1.1  christos 
    319  1.1  christos       /* Try to get the previous frame, to determine if this is the last frame
    320  1.1  christos 	 in a corrupt stack.  If so, we need to store the frame_id of the next
    321  1.1  christos 	 frame and not of this one (which is possibly invalid).  */
    322  1.1  christos       if (get_prev_frame (frame) == NULL
    323  1.1  christos 	  && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
    324  1.1  christos 	  && get_next_frame (frame) != NULL)
    325  1.1  christos 	{
    326  1.1  christos 	  frame_obj->frame_id = get_frame_id (get_next_frame (frame));
    327  1.1  christos 	  frame_obj->frame_id_is_next = 1;
    328  1.1  christos 	}
    329  1.1  christos       else
    330  1.1  christos 	{
    331  1.1  christos 	  frame_obj->frame_id = get_frame_id (frame);
    332  1.1  christos 	  frame_obj->frame_id_is_next = 0;
    333  1.1  christos 	}
    334  1.1  christos       frame_obj->gdbarch = get_frame_arch (frame);
    335  1.1  christos     }
    336  1.1  christos   if (except.reason < 0)
    337  1.1  christos     {
    338  1.1  christos       Py_DECREF (frame_obj);
    339  1.1  christos       gdbpy_convert_exception (except);
    340  1.1  christos       return NULL;
    341  1.1  christos     }
    342  1.1  christos   return (PyObject *) frame_obj;
    343  1.1  christos }
    344  1.1  christos 
    345  1.1  christos /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
    346  1.1  christos    Returns the frame immediately older (outer) to this frame, or None if
    347  1.1  christos    there isn't one.  */
    348  1.1  christos 
    349  1.1  christos static PyObject *
    350  1.1  christos frapy_older (PyObject *self, PyObject *args)
    351  1.1  christos {
    352  1.1  christos   struct frame_info *frame, *prev = NULL;
    353  1.1  christos   volatile struct gdb_exception except;
    354  1.1  christos   PyObject *prev_obj = NULL;   /* Initialize to appease gcc warning.  */
    355  1.1  christos 
    356  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    357  1.1  christos     {
    358  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    359  1.1  christos 
    360  1.1  christos       prev = get_prev_frame (frame);
    361  1.1  christos     }
    362  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    363  1.1  christos 
    364  1.1  christos   if (prev)
    365  1.1  christos     prev_obj = (PyObject *) frame_info_to_frame_object (prev);
    366  1.1  christos   else
    367  1.1  christos     {
    368  1.1  christos       Py_INCREF (Py_None);
    369  1.1  christos       prev_obj = Py_None;
    370  1.1  christos     }
    371  1.1  christos 
    372  1.1  christos   return prev_obj;
    373  1.1  christos }
    374  1.1  christos 
    375  1.1  christos /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
    376  1.1  christos    Returns the frame immediately newer (inner) to this frame, or None if
    377  1.1  christos    there isn't one.  */
    378  1.1  christos 
    379  1.1  christos static PyObject *
    380  1.1  christos frapy_newer (PyObject *self, PyObject *args)
    381  1.1  christos {
    382  1.1  christos   struct frame_info *frame, *next = NULL;
    383  1.1  christos   volatile struct gdb_exception except;
    384  1.1  christos   PyObject *next_obj = NULL;   /* Initialize to appease gcc warning.  */
    385  1.1  christos 
    386  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    387  1.1  christos     {
    388  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    389  1.1  christos 
    390  1.1  christos       next = get_next_frame (frame);
    391  1.1  christos     }
    392  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    393  1.1  christos 
    394  1.1  christos   if (next)
    395  1.1  christos     next_obj = (PyObject *) frame_info_to_frame_object (next);
    396  1.1  christos   else
    397  1.1  christos     {
    398  1.1  christos       Py_INCREF (Py_None);
    399  1.1  christos       next_obj = Py_None;
    400  1.1  christos     }
    401  1.1  christos 
    402  1.1  christos   return next_obj;
    403  1.1  christos }
    404  1.1  christos 
    405  1.1  christos /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
    406  1.1  christos    Returns the frame's symtab and line.  */
    407  1.1  christos 
    408  1.1  christos static PyObject *
    409  1.1  christos frapy_find_sal (PyObject *self, PyObject *args)
    410  1.1  christos {
    411  1.1  christos   struct frame_info *frame;
    412  1.1  christos   struct symtab_and_line sal;
    413  1.1  christos   volatile struct gdb_exception except;
    414  1.1  christos   PyObject *sal_obj = NULL;   /* Initialize to appease gcc warning.  */
    415  1.1  christos 
    416  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    417  1.1  christos     {
    418  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    419  1.1  christos 
    420  1.1  christos       find_frame_sal (frame, &sal);
    421  1.1  christos       sal_obj = symtab_and_line_to_sal_object (sal);
    422  1.1  christos     }
    423  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    424  1.1  christos 
    425  1.1  christos   return sal_obj;
    426  1.1  christos }
    427  1.1  christos 
    428  1.1  christos /* Implementation of gdb.Frame.read_var_value (self, variable,
    429  1.1  christos    [block]) -> gdb.Value.  If the optional block argument is provided
    430  1.1  christos    start the search from that block, otherwise search from the frame's
    431  1.1  christos    current block (determined by examining the resume address of the
    432  1.1  christos    frame).  The variable argument must be a string or an instance of a
    433  1.1  christos    gdb.Symbol.  The block argument must be an instance of gdb.Block.  Returns
    434  1.1  christos    NULL on error, with a python exception set.  */
    435  1.1  christos static PyObject *
    436  1.1  christos frapy_read_var (PyObject *self, PyObject *args)
    437  1.1  christos {
    438  1.1  christos   struct frame_info *frame;
    439  1.1  christos   PyObject *sym_obj, *block_obj = NULL;
    440  1.1  christos   struct symbol *var = NULL;	/* gcc-4.3.2 false warning.  */
    441  1.1  christos   struct value *val = NULL;
    442  1.1  christos   volatile struct gdb_exception except;
    443  1.1  christos 
    444  1.1  christos   if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj))
    445  1.1  christos     return NULL;
    446  1.1  christos 
    447  1.1  christos   if (PyObject_TypeCheck (sym_obj, &symbol_object_type))
    448  1.1  christos     var = symbol_object_to_symbol (sym_obj);
    449  1.1  christos   else if (gdbpy_is_string (sym_obj))
    450  1.1  christos     {
    451  1.1  christos       char *var_name;
    452  1.1  christos       const struct block *block = NULL;
    453  1.1  christos       struct cleanup *cleanup;
    454  1.1  christos       volatile struct gdb_exception except;
    455  1.1  christos 
    456  1.1  christos       var_name = python_string_to_target_string (sym_obj);
    457  1.1  christos       if (!var_name)
    458  1.1  christos 	return NULL;
    459  1.1  christos       cleanup = make_cleanup (xfree, var_name);
    460  1.1  christos 
    461  1.1  christos       if (block_obj)
    462  1.1  christos 	{
    463  1.1  christos 	  block = block_object_to_block (block_obj);
    464  1.1  christos 	  if (!block)
    465  1.1  christos 	    {
    466  1.1  christos 	      PyErr_SetString (PyExc_RuntimeError,
    467  1.1  christos 			       _("Second argument must be block."));
    468  1.1  christos 	      do_cleanups (cleanup);
    469  1.1  christos 	      return NULL;
    470  1.1  christos 	    }
    471  1.1  christos 	}
    472  1.1  christos 
    473  1.1  christos       TRY_CATCH (except, RETURN_MASK_ALL)
    474  1.1  christos 	{
    475  1.1  christos 	  FRAPY_REQUIRE_VALID (self, frame);
    476  1.1  christos 
    477  1.1  christos 	  if (!block)
    478  1.1  christos 	    block = get_frame_block (frame, NULL);
    479  1.1  christos 	  var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL);
    480  1.1  christos 	}
    481  1.1  christos       if (except.reason < 0)
    482  1.1  christos 	{
    483  1.1  christos 	  do_cleanups (cleanup);
    484  1.1  christos 	  gdbpy_convert_exception (except);
    485  1.1  christos 	  return NULL;
    486  1.1  christos 	}
    487  1.1  christos 
    488  1.1  christos       if (!var)
    489  1.1  christos 	{
    490  1.1  christos 	  PyErr_Format (PyExc_ValueError,
    491  1.1  christos 			_("Variable '%s' not found."), var_name);
    492  1.1  christos 	  do_cleanups (cleanup);
    493  1.1  christos 
    494  1.1  christos 	  return NULL;
    495  1.1  christos 	}
    496  1.1  christos 
    497  1.1  christos       do_cleanups (cleanup);
    498  1.1  christos     }
    499  1.1  christos   else
    500  1.1  christos     {
    501  1.1  christos       PyErr_SetString (PyExc_TypeError,
    502  1.1  christos 		       _("Argument must be a symbol or string."));
    503  1.1  christos       return NULL;
    504  1.1  christos     }
    505  1.1  christos 
    506  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    507  1.1  christos     {
    508  1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    509  1.1  christos 
    510  1.1  christos       val = read_var_value (var, frame);
    511  1.1  christos     }
    512  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    513  1.1  christos 
    514  1.1  christos   return value_to_value_object (val);
    515  1.1  christos }
    516  1.1  christos 
    517  1.1  christos /* Select this frame.  */
    518  1.1  christos 
    519  1.1  christos static PyObject *
    520  1.1  christos frapy_select (PyObject *self, PyObject *args)
    521  1.1  christos {
    522  1.1  christos   struct frame_info *fi;
    523  1.1  christos   volatile struct gdb_exception except;
    524  1.1  christos 
    525  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    526  1.1  christos     {
    527  1.1  christos       FRAPY_REQUIRE_VALID (self, fi);
    528  1.1  christos 
    529  1.1  christos       select_frame (fi);
    530  1.1  christos     }
    531  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    532  1.1  christos 
    533  1.1  christos   Py_RETURN_NONE;
    534  1.1  christos }
    535  1.1  christos 
    536  1.1  christos /* Implementation of gdb.newest_frame () -> gdb.Frame.
    537  1.1  christos    Returns the newest frame object.  */
    538  1.1  christos 
    539  1.1  christos PyObject *
    540  1.1  christos gdbpy_newest_frame (PyObject *self, PyObject *args)
    541  1.1  christos {
    542  1.1  christos   struct frame_info *frame = NULL;
    543  1.1  christos   volatile struct gdb_exception except;
    544  1.1  christos 
    545  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    546  1.1  christos     {
    547  1.1  christos       frame = get_current_frame ();
    548  1.1  christos     }
    549  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    550  1.1  christos 
    551  1.1  christos   return frame_info_to_frame_object (frame);
    552  1.1  christos }
    553  1.1  christos 
    554  1.1  christos /* Implementation of gdb.selected_frame () -> gdb.Frame.
    555  1.1  christos    Returns the selected frame object.  */
    556  1.1  christos 
    557  1.1  christos PyObject *
    558  1.1  christos gdbpy_selected_frame (PyObject *self, PyObject *args)
    559  1.1  christos {
    560  1.1  christos   struct frame_info *frame = NULL;
    561  1.1  christos   volatile struct gdb_exception except;
    562  1.1  christos 
    563  1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    564  1.1  christos     {
    565  1.1  christos       frame = get_selected_frame ("No frame is currently selected.");
    566  1.1  christos     }
    567  1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    568  1.1  christos 
    569  1.1  christos   return frame_info_to_frame_object (frame);
    570  1.1  christos }
    571  1.1  christos 
    572  1.1  christos /* Implementation of gdb.stop_reason_string (Integer) -> String.
    573  1.1  christos    Return a string explaining the unwind stop reason.  */
    574  1.1  christos 
    575  1.1  christos PyObject *
    576  1.1  christos gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
    577  1.1  christos {
    578  1.1  christos   int reason;
    579  1.1  christos   const char *str;
    580  1.1  christos 
    581  1.1  christos   if (!PyArg_ParseTuple (args, "i", &reason))
    582  1.1  christos     return NULL;
    583  1.1  christos 
    584  1.1  christos   if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
    585  1.1  christos     {
    586  1.1  christos       PyErr_SetString (PyExc_ValueError,
    587  1.1  christos 		       _("Invalid frame stop reason."));
    588  1.1  christos       return NULL;
    589  1.1  christos     }
    590  1.1  christos 
    591  1.1  christos   str = frame_stop_reason_string (reason);
    592  1.1  christos   return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
    593  1.1  christos }
    594  1.1  christos 
    595  1.1  christos /* Implements the equality comparison for Frame objects.
    596  1.1  christos    All other comparison operators will throw a TypeError Python exception,
    597  1.1  christos    as they aren't valid for frames.  */
    598  1.1  christos 
    599  1.1  christos static PyObject *
    600  1.1  christos frapy_richcompare (PyObject *self, PyObject *other, int op)
    601  1.1  christos {
    602  1.1  christos   int result;
    603  1.1  christos 
    604  1.1  christos   if (!PyObject_TypeCheck (other, &frame_object_type)
    605  1.1  christos       || (op != Py_EQ && op != Py_NE))
    606  1.1  christos     {
    607  1.1  christos       Py_INCREF (Py_NotImplemented);
    608  1.1  christos       return Py_NotImplemented;
    609  1.1  christos     }
    610  1.1  christos 
    611  1.1  christos   if (frame_id_eq (((frame_object *) self)->frame_id,
    612  1.1  christos 		   ((frame_object *) other)->frame_id))
    613  1.1  christos     result = Py_EQ;
    614  1.1  christos   else
    615  1.1  christos     result = Py_NE;
    616  1.1  christos 
    617  1.1  christos   if (op == result)
    618  1.1  christos     Py_RETURN_TRUE;
    619  1.1  christos   Py_RETURN_FALSE;
    620  1.1  christos }
    621  1.1  christos 
    622  1.1  christos /* Sets up the Frame API in the gdb module.  */
    623  1.1  christos 
    624  1.1  christos int
    625  1.1  christos gdbpy_initialize_frames (void)
    626  1.1  christos {
    627  1.1  christos   frame_object_type.tp_new = PyType_GenericNew;
    628  1.1  christos   if (PyType_Ready (&frame_object_type) < 0)
    629  1.1  christos     return -1;
    630  1.1  christos 
    631  1.1  christos   /* Note: These would probably be best exposed as class attributes of
    632  1.1  christos      Frame, but I don't know how to do it except by messing with the
    633  1.1  christos      type's dictionary.  That seems too messy.  */
    634  1.1  christos   if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0
    635  1.1  christos       || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0
    636  1.1  christos       || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0
    637  1.1  christos       || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME",
    638  1.1  christos 				  TAILCALL_FRAME) < 0
    639  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME",
    640  1.1  christos 				  SIGTRAMP_FRAME) < 0
    641  1.1  christos       || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0
    642  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME",
    643  1.1  christos 				  SENTINEL_FRAME) < 0)
    644  1.1  christos     return -1;
    645  1.1  christos 
    646  1.1  christos #define SET(name, description) \
    647  1.1  christos   if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
    648  1.1  christos     return -1;
    649  1.1  christos #include "unwind_stop_reasons.def"
    650  1.1  christos #undef SET
    651  1.1  christos 
    652  1.1  christos   return gdb_pymodule_addobject (gdb_module, "Frame",
    653  1.1  christos 				 (PyObject *) &frame_object_type);
    654  1.1  christos }
    655  1.1  christos 
    656  1.1  christos 
    657  1.1  christos 
    659  1.1  christos static PyMethodDef frame_object_methods[] = {
    660  1.1  christos   { "is_valid", frapy_is_valid, METH_NOARGS,
    661  1.1  christos     "is_valid () -> Boolean.\n\
    662  1.1  christos Return true if this frame is valid, false if not." },
    663  1.1  christos   { "name", frapy_name, METH_NOARGS,
    664  1.1  christos     "name () -> String.\n\
    665  1.1  christos Return the function name of the frame, or None if it can't be determined." },
    666  1.1  christos   { "type", frapy_type, METH_NOARGS,
    667  1.1  christos     "type () -> Integer.\n\
    668  1.1  christos Return the type of the frame." },
    669  1.1  christos   { "architecture", frapy_arch, METH_NOARGS,
    670  1.1  christos     "architecture () -> gdb.Architecture.\n\
    671  1.1  christos Return the architecture of the frame." },
    672  1.1  christos   { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
    673  1.1  christos     "unwind_stop_reason () -> Integer.\n\
    674  1.1  christos Return the reason why it's not possible to find frames older than this." },
    675  1.1  christos   { "pc", frapy_pc, METH_NOARGS,
    676  1.1  christos     "pc () -> Long.\n\
    677  1.1  christos Return the frame's resume address." },
    678  1.1  christos   { "block", frapy_block, METH_NOARGS,
    679  1.1  christos     "block () -> gdb.Block.\n\
    680  1.1  christos Return the frame's code block." },
    681  1.1  christos   { "function", frapy_function, METH_NOARGS,
    682  1.1  christos     "function () -> gdb.Symbol.\n\
    683  1.1  christos Returns the symbol for the function corresponding to this frame." },
    684  1.1  christos   { "older", frapy_older, METH_NOARGS,
    685  1.1  christos     "older () -> gdb.Frame.\n\
    686  1.1  christos Return the frame that called this frame." },
    687  1.1  christos   { "newer", frapy_newer, METH_NOARGS,
    688  1.1  christos     "newer () -> gdb.Frame.\n\
    689  1.1  christos Return the frame called by this frame." },
    690  1.1  christos   { "find_sal", frapy_find_sal, METH_NOARGS,
    691  1.1  christos     "find_sal () -> gdb.Symtab_and_line.\n\
    692  1.1  christos Return the frame's symtab and line." },
    693  1.1  christos   { "read_var", frapy_read_var, METH_VARARGS,
    694  1.1  christos     "read_var (variable) -> gdb.Value.\n\
    695  1.1  christos Return the value of the variable in this frame." },
    696  1.1  christos   { "select", frapy_select, METH_NOARGS,
    697  1.1  christos     "Select this frame as the user's current frame." },
    698  1.1  christos   {NULL}  /* Sentinel */
    699  1.1  christos };
    700  1.1  christos 
    701  1.1  christos PyTypeObject frame_object_type = {
    702  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    703  1.1  christos   "gdb.Frame",			  /* tp_name */
    704  1.1  christos   sizeof (frame_object),	  /* tp_basicsize */
    705  1.1  christos   0,				  /* tp_itemsize */
    706  1.1  christos   0,				  /* tp_dealloc */
    707  1.1  christos   0,				  /* tp_print */
    708  1.1  christos   0,				  /* tp_getattr */
    709  1.1  christos   0,				  /* tp_setattr */
    710  1.1  christos   0,				  /* tp_compare */
    711  1.1  christos   0,				  /* tp_repr */
    712  1.1  christos   0,				  /* tp_as_number */
    713  1.1  christos   0,				  /* tp_as_sequence */
    714  1.1  christos   0,				  /* tp_as_mapping */
    715  1.1  christos   0,				  /* tp_hash  */
    716  1.1  christos   0,				  /* tp_call */
    717  1.1  christos   frapy_str,			  /* tp_str */
    718  1.1  christos   0,				  /* tp_getattro */
    719  1.1  christos   0,				  /* tp_setattro */
    720  1.1  christos   0,				  /* tp_as_buffer */
    721  1.1  christos   Py_TPFLAGS_DEFAULT,		  /* tp_flags */
    722  1.1  christos   "GDB frame object",		  /* tp_doc */
    723  1.1  christos   0,				  /* tp_traverse */
    724  1.1  christos   0,				  /* tp_clear */
    725  1.1  christos   frapy_richcompare,		  /* tp_richcompare */
    726  1.1  christos   0,				  /* tp_weaklistoffset */
    727  1.1  christos   0,				  /* tp_iter */
    728  1.1  christos   0,				  /* tp_iternext */
    729  1.1  christos   frame_object_methods,		  /* tp_methods */
    730  1.1  christos   0,				  /* tp_members */
    731  1.1  christos   0,				  /* tp_getset */
    732  1.1  christos   0,				  /* tp_base */
    733  1.1  christos   0,				  /* tp_dict */
    734  1.1  christos   0,				  /* tp_descr_get */
    735  1.1  christos   0,				  /* tp_descr_set */
    736  1.1  christos   0,				  /* tp_dictoffset */
    737  1.1  christos   0,				  /* tp_init */
    738  1.1  christos   0,				  /* tp_alloc */
    739                };
    740