Home | History | Annotate | Line # | Download | only in python
py-frame.c revision 1.12
      1   1.1  christos /* Python interface to stack frames
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 2008-2024 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.11  christos #include "language.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 "symtab.h"
     25   1.1  christos #include "stack.h"
     26   1.1  christos #include "value.h"
     27   1.1  christos #include "python-internal.h"
     28   1.1  christos #include "symfile.h"
     29   1.1  christos #include "objfiles.h"
     30   1.1  christos 
     31  1.10  christos struct frame_object {
     32   1.1  christos   PyObject_HEAD
     33   1.1  christos   struct frame_id frame_id;
     34   1.1  christos   struct gdbarch *gdbarch;
     35   1.1  christos 
     36   1.1  christos   /* Marks that the FRAME_ID member actually holds the ID of the frame next
     37   1.1  christos      to this, and not this frames' ID itself.  This is a hack to permit Python
     38   1.1  christos      frame objects which represent invalid frames (i.e., the last frame_info
     39   1.1  christos      in a corrupt stack).  The problem arises from the fact that this code
     40   1.1  christos      relies on FRAME_ID to uniquely identify a frame, which is not always true
     41   1.1  christos      for the last "frame" in a corrupt stack (it can have a null ID, or the same
     42   1.1  christos      ID as the  previous frame).  Whenever get_prev_frame returns NULL, we
     43   1.1  christos      record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1.  */
     44   1.1  christos   int frame_id_is_next;
     45  1.10  christos };
     46   1.1  christos 
     47   1.1  christos /* Require a valid frame.  This must be called inside a TRY_CATCH, or
     48   1.1  christos    another context in which a gdb exception is allowed.  */
     49   1.1  christos #define FRAPY_REQUIRE_VALID(frame_obj, frame)		\
     50   1.1  christos     do {						\
     51   1.1  christos       frame = frame_object_to_frame_info (frame_obj);	\
     52   1.1  christos       if (frame == NULL)				\
     53   1.1  christos 	error (_("Frame is invalid."));			\
     54   1.1  christos     } while (0)
     55   1.1  christos 
     56   1.1  christos /* Returns the frame_info object corresponding to the given Python Frame
     57   1.1  christos    object.  If the frame doesn't exist anymore (the frame id doesn't
     58   1.1  christos    correspond to any frame in the inferior), returns NULL.  */
     59   1.1  christos 
     60  1.10  christos frame_info_ptr
     61   1.1  christos frame_object_to_frame_info (PyObject *obj)
     62   1.1  christos {
     63   1.1  christos   frame_object *frame_obj = (frame_object *) obj;
     64  1.10  christos   frame_info_ptr frame;
     65   1.1  christos 
     66   1.1  christos   frame = frame_find_by_id (frame_obj->frame_id);
     67   1.1  christos   if (frame == NULL)
     68   1.1  christos     return NULL;
     69   1.1  christos 
     70   1.1  christos   if (frame_obj->frame_id_is_next)
     71   1.1  christos     frame = get_prev_frame (frame);
     72   1.1  christos 
     73   1.1  christos   return frame;
     74   1.1  christos }
     75   1.1  christos 
     76   1.1  christos /* Called by the Python interpreter to obtain string representation
     77   1.1  christos    of the object.  */
     78   1.1  christos 
     79   1.1  christos static PyObject *
     80   1.1  christos frapy_str (PyObject *self)
     81   1.1  christos {
     82  1.10  christos   const frame_id &fid = ((frame_object *) self)->frame_id;
     83  1.10  christos   return PyUnicode_FromString (fid.to_string ().c_str ());
     84   1.1  christos }
     85   1.1  christos 
     86  1.11  christos /* Implement repr() for gdb.Frame.  */
     87  1.11  christos 
     88  1.11  christos static PyObject *
     89  1.11  christos frapy_repr (PyObject *self)
     90  1.11  christos {
     91  1.11  christos   frame_object *frame_obj = (frame_object *) self;
     92  1.11  christos   frame_info_ptr f_info = frame_find_by_id (frame_obj->frame_id);
     93  1.11  christos   if (f_info == nullptr)
     94  1.11  christos     return gdb_py_invalid_object_repr (self);
     95  1.11  christos 
     96  1.11  christos   const frame_id &fid = frame_obj->frame_id;
     97  1.11  christos   return PyUnicode_FromFormat ("<%s level=%d frame-id=%s>",
     98  1.11  christos 			       Py_TYPE (self)->tp_name,
     99  1.11  christos 			       frame_relative_level (f_info),
    100  1.11  christos 			       fid.to_string ().c_str ());
    101  1.11  christos }
    102  1.11  christos 
    103   1.1  christos /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
    104   1.1  christos    Returns True if the frame corresponding to the frame_id of this
    105   1.1  christos    object still exists in the inferior.  */
    106   1.1  christos 
    107   1.1  christos static PyObject *
    108   1.1  christos frapy_is_valid (PyObject *self, PyObject *args)
    109   1.1  christos {
    110  1.10  christos   frame_info_ptr frame = NULL;
    111   1.1  christos 
    112   1.9  christos   try
    113   1.1  christos     {
    114   1.1  christos       frame = frame_object_to_frame_info (self);
    115   1.1  christos     }
    116   1.9  christos   catch (const gdb_exception &except)
    117   1.5  christos     {
    118  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    119   1.5  christos     }
    120   1.1  christos 
    121   1.1  christos   if (frame == NULL)
    122   1.1  christos     Py_RETURN_FALSE;
    123   1.1  christos 
    124   1.1  christos   Py_RETURN_TRUE;
    125   1.1  christos }
    126   1.1  christos 
    127   1.1  christos /* Implementation of gdb.Frame.name (self) -> String.
    128   1.1  christos    Returns the name of the function corresponding to this frame.  */
    129   1.1  christos 
    130   1.1  christos static PyObject *
    131   1.1  christos frapy_name (PyObject *self, PyObject *args)
    132   1.1  christos {
    133  1.10  christos   frame_info_ptr frame;
    134   1.8  christos   gdb::unique_xmalloc_ptr<char> name;
    135   1.1  christos   enum language lang;
    136   1.1  christos   PyObject *result;
    137   1.1  christos 
    138   1.9  christos   try
    139   1.1  christos     {
    140   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    141   1.1  christos 
    142   1.8  christos       name = find_frame_funname (frame, &lang, NULL);
    143   1.1  christos     }
    144   1.9  christos   catch (const gdb_exception &except)
    145   1.5  christos     {
    146  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    147   1.5  christos     }
    148   1.1  christos 
    149   1.1  christos   if (name)
    150   1.1  christos     {
    151   1.8  christos       result = PyUnicode_Decode (name.get (), strlen (name.get ()),
    152   1.8  christos 				 host_charset (), NULL);
    153   1.1  christos     }
    154   1.1  christos   else
    155   1.1  christos     {
    156   1.1  christos       result = Py_None;
    157   1.1  christos       Py_INCREF (Py_None);
    158   1.1  christos     }
    159   1.1  christos 
    160   1.1  christos   return result;
    161   1.1  christos }
    162   1.1  christos 
    163   1.1  christos /* Implementation of gdb.Frame.type (self) -> Integer.
    164   1.1  christos    Returns the frame type, namely one of the gdb.*_FRAME constants.  */
    165   1.1  christos 
    166   1.1  christos static PyObject *
    167   1.1  christos frapy_type (PyObject *self, PyObject *args)
    168   1.1  christos {
    169  1.10  christos   frame_info_ptr frame;
    170   1.1  christos   enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning.  */
    171   1.1  christos 
    172   1.9  christos   try
    173   1.1  christos     {
    174   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    175   1.1  christos 
    176   1.1  christos       type = get_frame_type (frame);
    177   1.1  christos     }
    178   1.9  christos   catch (const gdb_exception &except)
    179   1.5  christos     {
    180  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    181   1.5  christos     }
    182   1.1  christos 
    183  1.10  christos   return gdb_py_object_from_longest (type).release ();
    184   1.1  christos }
    185   1.1  christos 
    186   1.1  christos /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture.
    187   1.1  christos    Returns the frame's architecture as a gdb.Architecture object.  */
    188   1.1  christos 
    189   1.1  christos static PyObject *
    190   1.1  christos frapy_arch (PyObject *self, PyObject *args)
    191   1.1  christos {
    192  1.10  christos   frame_info_ptr frame = NULL;    /* Initialize to appease gcc warning.  */
    193   1.1  christos   frame_object *obj = (frame_object *) self;
    194   1.1  christos 
    195   1.9  christos   try
    196   1.1  christos     {
    197   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    198   1.1  christos     }
    199   1.9  christos   catch (const gdb_exception &except)
    200   1.5  christos     {
    201  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    202   1.5  christos     }
    203   1.1  christos 
    204   1.1  christos   return gdbarch_to_arch_object (obj->gdbarch);
    205   1.1  christos }
    206   1.1  christos 
    207   1.1  christos /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
    208   1.1  christos    Returns one of the gdb.FRAME_UNWIND_* constants.  */
    209   1.1  christos 
    210   1.1  christos static PyObject *
    211   1.1  christos frapy_unwind_stop_reason (PyObject *self, PyObject *args)
    212   1.1  christos {
    213  1.10  christos   frame_info_ptr frame = NULL;    /* Initialize to appease gcc warning.  */
    214   1.1  christos   enum unwind_stop_reason stop_reason;
    215   1.1  christos 
    216   1.9  christos   try
    217   1.1  christos     {
    218   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    219   1.1  christos     }
    220   1.9  christos   catch (const gdb_exception &except)
    221   1.5  christos     {
    222  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    223   1.5  christos     }
    224   1.1  christos 
    225   1.1  christos   stop_reason = get_frame_unwind_stop_reason (frame);
    226   1.1  christos 
    227  1.10  christos   return gdb_py_object_from_longest (stop_reason).release ();
    228   1.1  christos }
    229   1.1  christos 
    230   1.1  christos /* Implementation of gdb.Frame.pc (self) -> Long.
    231   1.1  christos    Returns the frame's resume address.  */
    232   1.1  christos 
    233   1.1  christos static PyObject *
    234   1.1  christos frapy_pc (PyObject *self, PyObject *args)
    235   1.1  christos {
    236   1.1  christos   CORE_ADDR pc = 0;	      /* Initialize to appease gcc warning.  */
    237  1.10  christos   frame_info_ptr frame;
    238   1.1  christos 
    239   1.9  christos   try
    240   1.1  christos     {
    241   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    242   1.1  christos 
    243   1.1  christos       pc = get_frame_pc (frame);
    244   1.1  christos     }
    245   1.9  christos   catch (const gdb_exception &except)
    246   1.5  christos     {
    247  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    248   1.5  christos     }
    249   1.1  christos 
    250  1.10  christos   return gdb_py_object_from_ulongest (pc).release ();
    251   1.1  christos }
    252   1.1  christos 
    253   1.3  christos /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
    254   1.3  christos    Returns the value of a register in this frame.  */
    255   1.3  christos 
    256   1.3  christos static PyObject *
    257  1.11  christos frapy_read_register (PyObject *self, PyObject *args, PyObject *kw)
    258   1.3  christos {
    259   1.9  christos   PyObject *pyo_reg_id;
    260  1.11  christos   PyObject *result = nullptr;
    261  1.11  christos 
    262  1.11  christos   static const char *keywords[] = { "register", nullptr };
    263  1.11  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, &pyo_reg_id))
    264  1.11  christos     return nullptr;
    265   1.3  christos 
    266   1.9  christos   try
    267   1.3  christos     {
    268  1.11  christos       scoped_value_mark free_values;
    269  1.10  christos       frame_info_ptr frame;
    270   1.3  christos       int regnum;
    271   1.3  christos 
    272   1.3  christos       FRAPY_REQUIRE_VALID (self, frame);
    273   1.3  christos 
    274   1.9  christos       if (!gdbpy_parse_register_id (get_frame_arch (frame), pyo_reg_id,
    275   1.9  christos 				    &regnum))
    276  1.10  christos 	return nullptr;
    277   1.9  christos 
    278   1.9  christos       gdb_assert (regnum >= 0);
    279  1.11  christos       value *val
    280  1.11  christos 	= value_of_register (regnum, get_next_frame_sentinel_okay (frame));
    281   1.3  christos 
    282   1.3  christos       if (val == NULL)
    283  1.10  christos 	PyErr_SetString (PyExc_ValueError, _("Can't read register."));
    284  1.11  christos       else
    285  1.11  christos 	result = value_to_value_object (val);
    286   1.3  christos     }
    287   1.9  christos   catch (const gdb_exception &except)
    288   1.5  christos     {
    289  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    290   1.5  christos     }
    291   1.3  christos 
    292  1.11  christos   return result;
    293   1.3  christos }
    294   1.3  christos 
    295   1.1  christos /* Implementation of gdb.Frame.block (self) -> gdb.Block.
    296   1.1  christos    Returns the frame's code block.  */
    297   1.1  christos 
    298   1.1  christos static PyObject *
    299   1.1  christos frapy_block (PyObject *self, PyObject *args)
    300   1.1  christos {
    301  1.10  christos   frame_info_ptr frame;
    302   1.3  christos   const struct block *block = NULL, *fn_block;
    303   1.1  christos 
    304   1.9  christos   try
    305   1.1  christos     {
    306   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    307   1.1  christos       block = get_frame_block (frame, NULL);
    308   1.1  christos     }
    309   1.9  christos   catch (const gdb_exception &except)
    310   1.5  christos     {
    311  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    312   1.5  christos     }
    313   1.1  christos 
    314   1.1  christos   for (fn_block = block;
    315  1.10  christos        fn_block != NULL && fn_block->function () == NULL;
    316  1.10  christos        fn_block = fn_block->superblock ())
    317   1.1  christos     ;
    318   1.1  christos 
    319  1.10  christos   if (block == NULL || fn_block == NULL || fn_block->function () == NULL)
    320   1.1  christos     {
    321   1.1  christos       PyErr_SetString (PyExc_RuntimeError,
    322   1.1  christos 		       _("Cannot locate block for frame."));
    323   1.1  christos       return NULL;
    324   1.1  christos     }
    325   1.1  christos 
    326  1.11  christos   return block_to_block_object (block, fn_block->function ()->objfile ());
    327   1.1  christos }
    328   1.1  christos 
    329   1.1  christos 
    330   1.1  christos /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
    331   1.1  christos    Returns the symbol for the function corresponding to this frame.  */
    332   1.1  christos 
    333   1.1  christos static PyObject *
    334   1.1  christos frapy_function (PyObject *self, PyObject *args)
    335   1.1  christos {
    336   1.1  christos   struct symbol *sym = NULL;
    337  1.10  christos   frame_info_ptr frame;
    338   1.1  christos 
    339   1.9  christos   try
    340   1.1  christos     {
    341   1.6  christos       enum language funlang;
    342   1.6  christos 
    343   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    344   1.1  christos 
    345   1.8  christos       gdb::unique_xmalloc_ptr<char> funname
    346   1.8  christos 	= find_frame_funname (frame, &funlang, &sym);
    347   1.1  christos     }
    348   1.9  christos   catch (const gdb_exception &except)
    349   1.5  christos     {
    350  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    351   1.5  christos     }
    352   1.1  christos 
    353   1.1  christos   if (sym)
    354   1.1  christos     return symbol_to_symbol_object (sym);
    355   1.1  christos 
    356   1.1  christos   Py_RETURN_NONE;
    357   1.1  christos }
    358   1.1  christos 
    359   1.1  christos /* Convert a frame_info struct to a Python Frame object.
    360   1.1  christos    Sets a Python exception and returns NULL on error.  */
    361   1.1  christos 
    362   1.1  christos PyObject *
    363  1.11  christos frame_info_to_frame_object (const frame_info_ptr &frame)
    364   1.1  christos {
    365   1.7  christos   gdbpy_ref<frame_object> frame_obj (PyObject_New (frame_object,
    366   1.7  christos 						   &frame_object_type));
    367   1.1  christos   if (frame_obj == NULL)
    368   1.1  christos     return NULL;
    369   1.1  christos 
    370   1.9  christos   try
    371   1.1  christos     {
    372   1.1  christos 
    373   1.1  christos       /* Try to get the previous frame, to determine if this is the last frame
    374   1.1  christos 	 in a corrupt stack.  If so, we need to store the frame_id of the next
    375   1.1  christos 	 frame and not of this one (which is possibly invalid).  */
    376   1.1  christos       if (get_prev_frame (frame) == NULL
    377   1.1  christos 	  && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
    378   1.1  christos 	  && get_next_frame (frame) != NULL)
    379   1.1  christos 	{
    380   1.1  christos 	  frame_obj->frame_id = get_frame_id (get_next_frame (frame));
    381   1.1  christos 	  frame_obj->frame_id_is_next = 1;
    382   1.1  christos 	}
    383   1.1  christos       else
    384   1.1  christos 	{
    385   1.1  christos 	  frame_obj->frame_id = get_frame_id (frame);
    386   1.1  christos 	  frame_obj->frame_id_is_next = 0;
    387   1.1  christos 	}
    388   1.1  christos       frame_obj->gdbarch = get_frame_arch (frame);
    389   1.1  christos     }
    390   1.9  christos   catch (const gdb_exception &except)
    391   1.1  christos     {
    392  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    393   1.1  christos     }
    394   1.5  christos 
    395   1.7  christos   return (PyObject *) frame_obj.release ();
    396   1.1  christos }
    397   1.1  christos 
    398   1.1  christos /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
    399   1.1  christos    Returns the frame immediately older (outer) to this frame, or None if
    400   1.1  christos    there isn't one.  */
    401   1.1  christos 
    402   1.1  christos static PyObject *
    403   1.1  christos frapy_older (PyObject *self, PyObject *args)
    404   1.1  christos {
    405  1.10  christos   frame_info_ptr frame, prev = NULL;
    406   1.1  christos   PyObject *prev_obj = NULL;   /* Initialize to appease gcc warning.  */
    407   1.1  christos 
    408   1.9  christos   try
    409   1.1  christos     {
    410   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    411   1.1  christos 
    412   1.1  christos       prev = get_prev_frame (frame);
    413   1.1  christos     }
    414   1.9  christos   catch (const gdb_exception &except)
    415   1.5  christos     {
    416  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    417   1.5  christos     }
    418   1.1  christos 
    419   1.1  christos   if (prev)
    420   1.8  christos     prev_obj = frame_info_to_frame_object (prev);
    421   1.1  christos   else
    422   1.1  christos     {
    423   1.1  christos       Py_INCREF (Py_None);
    424   1.1  christos       prev_obj = Py_None;
    425   1.1  christos     }
    426   1.1  christos 
    427   1.1  christos   return prev_obj;
    428   1.1  christos }
    429   1.1  christos 
    430   1.1  christos /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
    431   1.1  christos    Returns the frame immediately newer (inner) to this frame, or None if
    432   1.1  christos    there isn't one.  */
    433   1.1  christos 
    434   1.1  christos static PyObject *
    435   1.1  christos frapy_newer (PyObject *self, PyObject *args)
    436   1.1  christos {
    437  1.10  christos   frame_info_ptr frame, next = NULL;
    438   1.1  christos   PyObject *next_obj = NULL;   /* Initialize to appease gcc warning.  */
    439   1.1  christos 
    440   1.9  christos   try
    441   1.1  christos     {
    442   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    443   1.1  christos 
    444   1.1  christos       next = get_next_frame (frame);
    445   1.1  christos     }
    446   1.9  christos   catch (const gdb_exception &except)
    447   1.5  christos     {
    448  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    449   1.5  christos     }
    450   1.1  christos 
    451   1.1  christos   if (next)
    452   1.8  christos     next_obj = frame_info_to_frame_object (next);
    453   1.1  christos   else
    454   1.1  christos     {
    455   1.1  christos       Py_INCREF (Py_None);
    456   1.1  christos       next_obj = Py_None;
    457   1.1  christos     }
    458   1.1  christos 
    459   1.1  christos   return next_obj;
    460   1.1  christos }
    461   1.1  christos 
    462   1.1  christos /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
    463   1.1  christos    Returns the frame's symtab and line.  */
    464   1.1  christos 
    465   1.1  christos static PyObject *
    466   1.1  christos frapy_find_sal (PyObject *self, PyObject *args)
    467   1.1  christos {
    468  1.10  christos   frame_info_ptr frame;
    469   1.1  christos   PyObject *sal_obj = NULL;   /* Initialize to appease gcc warning.  */
    470   1.1  christos 
    471   1.9  christos   try
    472   1.1  christos     {
    473   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    474   1.1  christos 
    475   1.8  christos       symtab_and_line sal = find_frame_sal (frame);
    476   1.1  christos       sal_obj = symtab_and_line_to_sal_object (sal);
    477   1.1  christos     }
    478   1.9  christos   catch (const gdb_exception &except)
    479   1.5  christos     {
    480  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    481   1.5  christos     }
    482   1.1  christos 
    483   1.1  christos   return sal_obj;
    484   1.1  christos }
    485   1.1  christos 
    486   1.1  christos /* Implementation of gdb.Frame.read_var_value (self, variable,
    487   1.1  christos    [block]) -> gdb.Value.  If the optional block argument is provided
    488   1.1  christos    start the search from that block, otherwise search from the frame's
    489   1.1  christos    current block (determined by examining the resume address of the
    490   1.1  christos    frame).  The variable argument must be a string or an instance of a
    491   1.1  christos    gdb.Symbol.  The block argument must be an instance of gdb.Block.  Returns
    492   1.1  christos    NULL on error, with a python exception set.  */
    493   1.1  christos static PyObject *
    494  1.11  christos frapy_read_var (PyObject *self, PyObject *args, PyObject *kw)
    495   1.1  christos {
    496  1.10  christos   frame_info_ptr frame;
    497   1.1  christos   PyObject *sym_obj, *block_obj = NULL;
    498   1.1  christos   struct symbol *var = NULL;	/* gcc-4.3.2 false warning.  */
    499   1.6  christos   const struct block *block = NULL;
    500   1.1  christos 
    501  1.11  christos   static const char *keywords[] = { "variable", "block", nullptr };
    502  1.11  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O|O!", keywords,
    503  1.11  christos 					&sym_obj, &block_object_type,
    504  1.11  christos 					&block_obj))
    505  1.11  christos     return nullptr;
    506   1.1  christos 
    507   1.1  christos   if (PyObject_TypeCheck (sym_obj, &symbol_object_type))
    508   1.1  christos     var = symbol_object_to_symbol (sym_obj);
    509   1.1  christos   else if (gdbpy_is_string (sym_obj))
    510   1.1  christos     {
    511   1.7  christos       gdb::unique_xmalloc_ptr<char>
    512   1.7  christos 	var_name (python_string_to_target_string (sym_obj));
    513   1.1  christos 
    514   1.1  christos       if (!var_name)
    515   1.1  christos 	return NULL;
    516   1.1  christos 
    517  1.11  christos       if (block_obj != nullptr)
    518   1.1  christos 	{
    519  1.11  christos 	  /* This call should only fail if the type of BLOCK_OBJ is wrong,
    520  1.11  christos 	     and we ensure the type is correct when we parse the arguments,
    521  1.11  christos 	     so we can just assert the return value is not nullptr.  */
    522   1.1  christos 	  block = block_object_to_block (block_obj);
    523  1.11  christos 	  gdb_assert (block != nullptr);
    524   1.1  christos 	}
    525   1.1  christos 
    526   1.9  christos       try
    527   1.1  christos 	{
    528   1.6  christos 	  struct block_symbol lookup_sym;
    529   1.1  christos 	  FRAPY_REQUIRE_VALID (self, frame);
    530   1.1  christos 
    531   1.1  christos 	  if (!block)
    532   1.1  christos 	    block = get_frame_block (frame, NULL);
    533  1.11  christos 	  lookup_sym = lookup_symbol (var_name.get (), block,
    534  1.11  christos 				      SEARCH_VFT, nullptr);
    535   1.6  christos 	  var = lookup_sym.symbol;
    536   1.6  christos 	  block = lookup_sym.block;
    537   1.1  christos 	}
    538   1.9  christos       catch (const gdb_exception &except)
    539   1.1  christos 	{
    540  1.12  christos 	  return gdbpy_handle_gdb_exception (nullptr, except);
    541   1.1  christos 	}
    542   1.1  christos 
    543   1.1  christos       if (!var)
    544   1.1  christos 	{
    545   1.1  christos 	  PyErr_Format (PyExc_ValueError,
    546   1.7  christos 			_("Variable '%s' not found."), var_name.get ());
    547   1.1  christos 
    548   1.1  christos 	  return NULL;
    549   1.1  christos 	}
    550   1.1  christos     }
    551   1.1  christos   else
    552   1.1  christos     {
    553  1.11  christos       PyErr_Format (PyExc_TypeError,
    554  1.11  christos 		    _("argument 1 must be gdb.Symbol or str, not %s"),
    555  1.11  christos 		    Py_TYPE (sym_obj)->tp_name);
    556   1.1  christos       return NULL;
    557   1.1  christos     }
    558   1.1  christos 
    559  1.11  christos   PyObject *result = nullptr;
    560   1.9  christos   try
    561   1.1  christos     {
    562   1.1  christos       FRAPY_REQUIRE_VALID (self, frame);
    563   1.1  christos 
    564  1.11  christos       scoped_value_mark free_values;
    565  1.11  christos       struct value *val = read_var_value (var, block, frame);
    566  1.11  christos       result = value_to_value_object (val);
    567   1.1  christos     }
    568   1.9  christos   catch (const gdb_exception &except)
    569   1.5  christos     {
    570  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    571   1.5  christos     }
    572   1.1  christos 
    573  1.11  christos   return result;
    574   1.1  christos }
    575   1.1  christos 
    576   1.1  christos /* Select this frame.  */
    577   1.1  christos 
    578   1.1  christos static PyObject *
    579   1.1  christos frapy_select (PyObject *self, PyObject *args)
    580   1.1  christos {
    581  1.10  christos   frame_info_ptr fi;
    582   1.1  christos 
    583   1.9  christos   try
    584   1.1  christos     {
    585   1.1  christos       FRAPY_REQUIRE_VALID (self, fi);
    586   1.1  christos 
    587   1.1  christos       select_frame (fi);
    588   1.1  christos     }
    589   1.9  christos   catch (const gdb_exception &except)
    590   1.5  christos     {
    591  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    592   1.5  christos     }
    593   1.1  christos 
    594   1.1  christos   Py_RETURN_NONE;
    595   1.1  christos }
    596   1.1  christos 
    597  1.10  christos /* The stack frame level for this frame.  */
    598  1.10  christos 
    599  1.10  christos static PyObject *
    600  1.10  christos frapy_level (PyObject *self, PyObject *args)
    601  1.10  christos {
    602  1.10  christos   frame_info_ptr fi;
    603  1.10  christos 
    604  1.10  christos   try
    605  1.10  christos     {
    606  1.10  christos       FRAPY_REQUIRE_VALID (self, fi);
    607  1.10  christos 
    608  1.10  christos       return gdb_py_object_from_longest (frame_relative_level (fi)).release ();
    609  1.10  christos     }
    610  1.10  christos   catch (const gdb_exception &except)
    611  1.10  christos     {
    612  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    613  1.10  christos     }
    614  1.10  christos 
    615  1.10  christos   Py_RETURN_NONE;
    616  1.10  christos }
    617  1.10  christos 
    618  1.10  christos /* The language for this frame.  */
    619  1.10  christos 
    620  1.10  christos static PyObject *
    621  1.10  christos frapy_language (PyObject *self, PyObject *args)
    622  1.10  christos {
    623  1.10  christos   try
    624  1.10  christos     {
    625  1.10  christos       frame_info_ptr fi;
    626  1.10  christos       FRAPY_REQUIRE_VALID (self, fi);
    627  1.10  christos 
    628  1.10  christos       enum language lang = get_frame_language (fi);
    629  1.10  christos       const language_defn *lang_def = language_def (lang);
    630  1.10  christos 
    631  1.10  christos       return host_string_to_python_string (lang_def->name ()).release ();
    632  1.10  christos     }
    633  1.10  christos   catch (const gdb_exception &except)
    634  1.10  christos     {
    635  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    636  1.10  christos     }
    637  1.10  christos 
    638  1.10  christos   Py_RETURN_NONE;
    639  1.10  christos }
    640  1.10  christos 
    641  1.11  christos /* The static link for this frame.  */
    642  1.11  christos 
    643  1.11  christos static PyObject *
    644  1.11  christos frapy_static_link (PyObject *self, PyObject *args)
    645  1.11  christos {
    646  1.11  christos   frame_info_ptr link;
    647  1.11  christos 
    648  1.11  christos   try
    649  1.11  christos     {
    650  1.11  christos       FRAPY_REQUIRE_VALID (self, link);
    651  1.11  christos 
    652  1.11  christos       link = frame_follow_static_link (link);
    653  1.11  christos     }
    654  1.11  christos   catch (const gdb_exception &except)
    655  1.11  christos     {
    656  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    657  1.11  christos     }
    658  1.11  christos 
    659  1.11  christos   if (link == nullptr)
    660  1.11  christos     Py_RETURN_NONE;
    661  1.11  christos 
    662  1.11  christos   return frame_info_to_frame_object (link);
    663  1.11  christos }
    664  1.11  christos 
    665   1.1  christos /* Implementation of gdb.newest_frame () -> gdb.Frame.
    666   1.1  christos    Returns the newest frame object.  */
    667   1.1  christos 
    668   1.1  christos PyObject *
    669   1.1  christos gdbpy_newest_frame (PyObject *self, PyObject *args)
    670   1.1  christos {
    671  1.10  christos   frame_info_ptr frame = NULL;
    672   1.1  christos 
    673   1.9  christos   try
    674   1.1  christos     {
    675   1.1  christos       frame = get_current_frame ();
    676   1.1  christos     }
    677   1.9  christos   catch (const gdb_exception &except)
    678   1.5  christos     {
    679  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    680   1.5  christos     }
    681   1.1  christos 
    682   1.1  christos   return frame_info_to_frame_object (frame);
    683   1.1  christos }
    684   1.1  christos 
    685   1.1  christos /* Implementation of gdb.selected_frame () -> gdb.Frame.
    686   1.1  christos    Returns the selected frame object.  */
    687   1.1  christos 
    688   1.1  christos PyObject *
    689   1.1  christos gdbpy_selected_frame (PyObject *self, PyObject *args)
    690   1.1  christos {
    691  1.10  christos   frame_info_ptr frame = NULL;
    692   1.1  christos 
    693   1.9  christos   try
    694   1.1  christos     {
    695   1.1  christos       frame = get_selected_frame ("No frame is currently selected.");
    696   1.1  christos     }
    697   1.9  christos   catch (const gdb_exception &except)
    698   1.5  christos     {
    699  1.12  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    700   1.5  christos     }
    701   1.1  christos 
    702   1.1  christos   return frame_info_to_frame_object (frame);
    703   1.1  christos }
    704   1.1  christos 
    705   1.1  christos /* Implementation of gdb.stop_reason_string (Integer) -> String.
    706   1.1  christos    Return a string explaining the unwind stop reason.  */
    707   1.1  christos 
    708   1.1  christos PyObject *
    709   1.1  christos gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
    710   1.1  christos {
    711   1.1  christos   int reason;
    712   1.1  christos   const char *str;
    713   1.1  christos 
    714   1.1  christos   if (!PyArg_ParseTuple (args, "i", &reason))
    715   1.1  christos     return NULL;
    716   1.1  christos 
    717   1.1  christos   if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
    718   1.1  christos     {
    719   1.1  christos       PyErr_SetString (PyExc_ValueError,
    720   1.1  christos 		       _("Invalid frame stop reason."));
    721   1.1  christos       return NULL;
    722   1.1  christos     }
    723   1.1  christos 
    724   1.6  christos   str = unwind_stop_reason_to_string ((enum unwind_stop_reason) reason);
    725   1.1  christos   return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
    726   1.1  christos }
    727   1.1  christos 
    728   1.1  christos /* Implements the equality comparison for Frame objects.
    729   1.1  christos    All other comparison operators will throw a TypeError Python exception,
    730   1.1  christos    as they aren't valid for frames.  */
    731   1.1  christos 
    732   1.1  christos static PyObject *
    733   1.1  christos frapy_richcompare (PyObject *self, PyObject *other, int op)
    734   1.1  christos {
    735   1.1  christos   int result;
    736   1.1  christos 
    737   1.1  christos   if (!PyObject_TypeCheck (other, &frame_object_type)
    738   1.1  christos       || (op != Py_EQ && op != Py_NE))
    739   1.1  christos     {
    740   1.1  christos       Py_INCREF (Py_NotImplemented);
    741   1.1  christos       return Py_NotImplemented;
    742   1.1  christos     }
    743   1.1  christos 
    744  1.10  christos   frame_object *self_frame = (frame_object *) self;
    745  1.10  christos   frame_object *other_frame = (frame_object *) other;
    746  1.10  christos 
    747  1.10  christos   if (self_frame->frame_id_is_next == other_frame->frame_id_is_next
    748  1.10  christos       && self_frame->frame_id == other_frame->frame_id)
    749   1.1  christos     result = Py_EQ;
    750   1.1  christos   else
    751   1.1  christos     result = Py_NE;
    752   1.1  christos 
    753   1.1  christos   if (op == result)
    754   1.1  christos     Py_RETURN_TRUE;
    755   1.1  christos   Py_RETURN_FALSE;
    756   1.1  christos }
    757   1.1  christos 
    758   1.1  christos /* Sets up the Frame API in the gdb module.  */
    759   1.1  christos 
    760  1.11  christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
    761   1.1  christos gdbpy_initialize_frames (void)
    762   1.1  christos {
    763   1.1  christos   frame_object_type.tp_new = PyType_GenericNew;
    764  1.12  christos   if (gdbpy_type_ready (&frame_object_type) < 0)
    765   1.1  christos     return -1;
    766   1.1  christos 
    767   1.1  christos   /* Note: These would probably be best exposed as class attributes of
    768   1.1  christos      Frame, but I don't know how to do it except by messing with the
    769   1.1  christos      type's dictionary.  That seems too messy.  */
    770   1.1  christos   if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0
    771   1.1  christos       || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0
    772   1.1  christos       || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0
    773   1.1  christos       || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME",
    774   1.1  christos 				  TAILCALL_FRAME) < 0
    775   1.1  christos       || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME",
    776   1.1  christos 				  SIGTRAMP_FRAME) < 0
    777   1.1  christos       || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0
    778   1.1  christos       || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME",
    779   1.1  christos 				  SENTINEL_FRAME) < 0)
    780   1.1  christos     return -1;
    781   1.1  christos 
    782   1.1  christos #define SET(name, description) \
    783   1.1  christos   if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \
    784   1.1  christos     return -1;
    785   1.1  christos #include "unwind_stop_reasons.def"
    786   1.1  christos #undef SET
    787   1.1  christos 
    788  1.12  christos   return 0;
    789   1.1  christos }
    790   1.1  christos 
    791  1.11  christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_frames);
    792  1.11  christos 
    793   1.1  christos 
    794   1.1  christos 
    796   1.1  christos static PyMethodDef frame_object_methods[] = {
    797   1.1  christos   { "is_valid", frapy_is_valid, METH_NOARGS,
    798   1.1  christos     "is_valid () -> Boolean.\n\
    799   1.1  christos Return true if this frame is valid, false if not." },
    800   1.1  christos   { "name", frapy_name, METH_NOARGS,
    801   1.1  christos     "name () -> String.\n\
    802   1.1  christos Return the function name of the frame, or None if it can't be determined." },
    803   1.1  christos   { "type", frapy_type, METH_NOARGS,
    804   1.1  christos     "type () -> Integer.\n\
    805   1.1  christos Return the type of the frame." },
    806   1.1  christos   { "architecture", frapy_arch, METH_NOARGS,
    807   1.1  christos     "architecture () -> gdb.Architecture.\n\
    808   1.1  christos Return the architecture of the frame." },
    809   1.1  christos   { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
    810   1.1  christos     "unwind_stop_reason () -> Integer.\n\
    811   1.1  christos Return the reason why it's not possible to find frames older than this." },
    812   1.1  christos   { "pc", frapy_pc, METH_NOARGS,
    813   1.1  christos     "pc () -> Long.\n\
    814  1.11  christos Return the frame's resume address." },
    815  1.11  christos   { "read_register", (PyCFunction) frapy_read_register,
    816   1.3  christos     METH_VARARGS | METH_KEYWORDS,
    817   1.3  christos     "read_register (register_name) -> gdb.Value\n\
    818   1.1  christos Return the value of the register in the frame." },
    819   1.1  christos   { "block", frapy_block, METH_NOARGS,
    820   1.1  christos     "block () -> gdb.Block.\n\
    821   1.1  christos Return the frame's code block." },
    822   1.1  christos   { "function", frapy_function, METH_NOARGS,
    823   1.1  christos     "function () -> gdb.Symbol.\n\
    824   1.1  christos Returns the symbol for the function corresponding to this frame." },
    825   1.1  christos   { "older", frapy_older, METH_NOARGS,
    826   1.1  christos     "older () -> gdb.Frame.\n\
    827   1.1  christos Return the frame that called this frame." },
    828   1.1  christos   { "newer", frapy_newer, METH_NOARGS,
    829   1.1  christos     "newer () -> gdb.Frame.\n\
    830   1.1  christos Return the frame called by this frame." },
    831   1.1  christos   { "find_sal", frapy_find_sal, METH_NOARGS,
    832   1.1  christos     "find_sal () -> gdb.Symtab_and_line.\n\
    833  1.11  christos Return the frame's symtab and line." },
    834   1.1  christos   { "read_var", (PyCFunction) frapy_read_var, METH_VARARGS | METH_KEYWORDS,
    835   1.1  christos     "read_var (variable) -> gdb.Value.\n\
    836   1.1  christos Return the value of the variable in this frame." },
    837   1.1  christos   { "select", frapy_select, METH_NOARGS,
    838  1.10  christos     "Select this frame as the user's current frame." },
    839  1.10  christos   { "level", frapy_level, METH_NOARGS,
    840  1.10  christos     "The stack level of this frame." },
    841  1.10  christos   { "language", frapy_language, METH_NOARGS,
    842  1.11  christos     "The language of this frame." },
    843  1.11  christos   { "static_link", frapy_static_link, METH_NOARGS,
    844   1.1  christos     "The static link of this frame, or None." },
    845   1.1  christos   {NULL}  /* Sentinel */
    846   1.1  christos };
    847   1.1  christos 
    848   1.1  christos PyTypeObject frame_object_type = {
    849   1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    850   1.1  christos   "gdb.Frame",			  /* tp_name */
    851   1.1  christos   sizeof (frame_object),	  /* tp_basicsize */
    852   1.1  christos   0,				  /* tp_itemsize */
    853   1.1  christos   0,				  /* tp_dealloc */
    854   1.1  christos   0,				  /* tp_print */
    855   1.1  christos   0,				  /* tp_getattr */
    856   1.1  christos   0,				  /* tp_setattr */
    857  1.11  christos   0,				  /* tp_compare */
    858   1.1  christos   frapy_repr,			  /* tp_repr */
    859   1.1  christos   0,				  /* tp_as_number */
    860   1.1  christos   0,				  /* tp_as_sequence */
    861   1.1  christos   0,				  /* tp_as_mapping */
    862   1.1  christos   0,				  /* tp_hash  */
    863   1.1  christos   0,				  /* tp_call */
    864   1.1  christos   frapy_str,			  /* tp_str */
    865   1.1  christos   0,				  /* tp_getattro */
    866   1.1  christos   0,				  /* tp_setattro */
    867   1.1  christos   0,				  /* tp_as_buffer */
    868   1.1  christos   Py_TPFLAGS_DEFAULT,		  /* tp_flags */
    869   1.1  christos   "GDB frame object",		  /* tp_doc */
    870   1.1  christos   0,				  /* tp_traverse */
    871   1.1  christos   0,				  /* tp_clear */
    872   1.1  christos   frapy_richcompare,		  /* tp_richcompare */
    873   1.1  christos   0,				  /* tp_weaklistoffset */
    874   1.1  christos   0,				  /* tp_iter */
    875   1.1  christos   0,				  /* tp_iternext */
    876   1.1  christos   frame_object_methods,		  /* tp_methods */
    877   1.1  christos   0,				  /* tp_members */
    878   1.1  christos   0,				  /* tp_getset */
    879   1.1  christos   0,				  /* tp_base */
    880   1.1  christos   0,				  /* tp_dict */
    881   1.1  christos   0,				  /* tp_descr_get */
    882   1.1  christos   0,				  /* tp_descr_set */
    883   1.1  christos   0,				  /* tp_dictoffset */
    884   1.1  christos   0,				  /* tp_init */
    885   1.1  christos   0,				  /* tp_alloc */
    886                 };
    887