Home | History | Annotate | Line # | Download | only in python
py-symbol.c revision 1.9
      1  1.1  christos /* Python interface to symbols.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 2008-2020 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 "block.h"
     22  1.1  christos #include "frame.h"
     23  1.1  christos #include "symtab.h"
     24  1.1  christos #include "python-internal.h"
     25  1.1  christos #include "objfiles.h"
     26  1.9  christos #include "symfile.h"
     27  1.1  christos 
     28  1.1  christos typedef struct sympy_symbol_object {
     29  1.1  christos   PyObject_HEAD
     30  1.1  christos   /* The GDB symbol structure this object is wrapping.  */
     31  1.1  christos   struct symbol *symbol;
     32  1.1  christos   /* A symbol object is associated with an objfile, so keep track with
     33  1.1  christos      doubly-linked list, rooted in the objfile.  This lets us
     34  1.1  christos      invalidate the underlying struct symbol when the objfile is
     35  1.1  christos      deleted.  */
     36  1.1  christos   struct sympy_symbol_object *prev;
     37  1.1  christos   struct sympy_symbol_object *next;
     38  1.1  christos } symbol_object;
     39  1.1  christos 
     40  1.1  christos /* Require a valid symbol.  All access to symbol_object->symbol should be
     41  1.1  christos    gated by this call.  */
     42  1.1  christos #define SYMPY_REQUIRE_VALID(symbol_obj, symbol)		\
     43  1.1  christos   do {							\
     44  1.1  christos     symbol = symbol_object_to_symbol (symbol_obj);	\
     45  1.1  christos     if (symbol == NULL)					\
     46  1.1  christos       {							\
     47  1.1  christos 	PyErr_SetString (PyExc_RuntimeError,		\
     48  1.1  christos 			 _("Symbol is invalid."));	\
     49  1.1  christos 	return NULL;					\
     50  1.1  christos       }							\
     51  1.1  christos   } while (0)
     52  1.1  christos 
     53  1.1  christos static const struct objfile_data *sympy_objfile_data_key;
     54  1.1  christos 
     55  1.1  christos static PyObject *
     56  1.1  christos sympy_str (PyObject *self)
     57  1.1  christos {
     58  1.1  christos   PyObject *result;
     59  1.1  christos   struct symbol *symbol = NULL;
     60  1.1  christos 
     61  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     62  1.1  christos 
     63  1.9  christos   result = PyString_FromString (symbol->print_name ());
     64  1.1  christos 
     65  1.1  christos   return result;
     66  1.1  christos }
     67  1.1  christos 
     68  1.1  christos static PyObject *
     69  1.1  christos sympy_get_type (PyObject *self, void *closure)
     70  1.1  christos {
     71  1.1  christos   struct symbol *symbol = NULL;
     72  1.1  christos 
     73  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     74  1.1  christos 
     75  1.1  christos   if (SYMBOL_TYPE (symbol) == NULL)
     76  1.1  christos     {
     77  1.1  christos       Py_INCREF (Py_None);
     78  1.1  christos       return Py_None;
     79  1.1  christos     }
     80  1.1  christos 
     81  1.1  christos   return type_to_type_object (SYMBOL_TYPE (symbol));
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos static PyObject *
     85  1.1  christos sympy_get_symtab (PyObject *self, void *closure)
     86  1.1  christos {
     87  1.1  christos   struct symbol *symbol = NULL;
     88  1.1  christos 
     89  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     90  1.1  christos 
     91  1.3  christos   if (!SYMBOL_OBJFILE_OWNED (symbol))
     92  1.3  christos     Py_RETURN_NONE;
     93  1.3  christos 
     94  1.3  christos   return symtab_to_symtab_object (symbol_symtab (symbol));
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos static PyObject *
     98  1.1  christos sympy_get_name (PyObject *self, void *closure)
     99  1.1  christos {
    100  1.1  christos   struct symbol *symbol = NULL;
    101  1.1  christos 
    102  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    103  1.1  christos 
    104  1.9  christos   return PyString_FromString (symbol->natural_name ());
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos static PyObject *
    108  1.1  christos sympy_get_linkage_name (PyObject *self, void *closure)
    109  1.1  christos {
    110  1.1  christos   struct symbol *symbol = NULL;
    111  1.1  christos 
    112  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    113  1.1  christos 
    114  1.9  christos   return PyString_FromString (symbol->linkage_name ());
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos static PyObject *
    118  1.1  christos sympy_get_print_name (PyObject *self, void *closure)
    119  1.1  christos {
    120  1.1  christos   struct symbol *symbol = NULL;
    121  1.1  christos 
    122  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    123  1.1  christos 
    124  1.1  christos   return sympy_str (self);
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos static PyObject *
    128  1.1  christos sympy_get_addr_class (PyObject *self, void *closure)
    129  1.1  christos {
    130  1.1  christos   struct symbol *symbol = NULL;
    131  1.1  christos 
    132  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    133  1.1  christos 
    134  1.1  christos   return PyInt_FromLong (SYMBOL_CLASS (symbol));
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos static PyObject *
    138  1.1  christos sympy_is_argument (PyObject *self, void *closure)
    139  1.1  christos {
    140  1.1  christos   struct symbol *symbol = NULL;
    141  1.1  christos 
    142  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    143  1.1  christos 
    144  1.1  christos   return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
    145  1.1  christos }
    146  1.1  christos 
    147  1.1  christos static PyObject *
    148  1.1  christos sympy_is_constant (PyObject *self, void *closure)
    149  1.1  christos {
    150  1.1  christos   struct symbol *symbol = NULL;
    151  1.5  christos   enum address_class theclass;
    152  1.1  christos 
    153  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    154  1.1  christos 
    155  1.5  christos   theclass = SYMBOL_CLASS (symbol);
    156  1.1  christos 
    157  1.5  christos   return PyBool_FromLong (theclass == LOC_CONST || theclass == LOC_CONST_BYTES);
    158  1.1  christos }
    159  1.1  christos 
    160  1.1  christos static PyObject *
    161  1.1  christos sympy_is_function (PyObject *self, void *closure)
    162  1.1  christos {
    163  1.1  christos   struct symbol *symbol = NULL;
    164  1.5  christos   enum address_class theclass;
    165  1.1  christos 
    166  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    167  1.1  christos 
    168  1.5  christos   theclass = SYMBOL_CLASS (symbol);
    169  1.1  christos 
    170  1.5  christos   return PyBool_FromLong (theclass == LOC_BLOCK);
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos static PyObject *
    174  1.1  christos sympy_is_variable (PyObject *self, void *closure)
    175  1.1  christos {
    176  1.1  christos   struct symbol *symbol = NULL;
    177  1.5  christos   enum address_class theclass;
    178  1.1  christos 
    179  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    180  1.1  christos 
    181  1.5  christos   theclass = SYMBOL_CLASS (symbol);
    182  1.1  christos 
    183  1.1  christos   return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
    184  1.5  christos 			  && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
    185  1.5  christos 			      || theclass == LOC_STATIC || theclass == LOC_COMPUTED
    186  1.5  christos 			      || theclass == LOC_OPTIMIZED_OUT));
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos /* Implementation of gdb.Symbol.needs_frame -> Boolean.
    190  1.1  christos    Returns true iff the symbol needs a frame for evaluation.  */
    191  1.1  christos 
    192  1.1  christos static PyObject *
    193  1.1  christos sympy_needs_frame (PyObject *self, void *closure)
    194  1.1  christos {
    195  1.1  christos   struct symbol *symbol = NULL;
    196  1.1  christos   int result = 0;
    197  1.1  christos 
    198  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    199  1.1  christos 
    200  1.9  christos   try
    201  1.1  christos     {
    202  1.1  christos       result = symbol_read_needs_frame (symbol);
    203  1.1  christos     }
    204  1.9  christos   catch (const gdb_exception &except)
    205  1.5  christos     {
    206  1.5  christos       GDB_PY_HANDLE_EXCEPTION (except);
    207  1.5  christos     }
    208  1.1  christos 
    209  1.1  christos   if (result)
    210  1.1  christos     Py_RETURN_TRUE;
    211  1.1  christos   Py_RETURN_FALSE;
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos /* Implementation of gdb.Symbol.line -> int.
    215  1.1  christos    Returns the line number at which the symbol was defined.  */
    216  1.1  christos 
    217  1.1  christos static PyObject *
    218  1.1  christos sympy_line (PyObject *self, void *closure)
    219  1.1  christos {
    220  1.1  christos   struct symbol *symbol = NULL;
    221  1.1  christos 
    222  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    223  1.1  christos 
    224  1.1  christos   return PyInt_FromLong (SYMBOL_LINE (symbol));
    225  1.1  christos }
    226  1.1  christos 
    227  1.1  christos /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
    228  1.1  christos    Returns True if this Symbol still exists in GDB.  */
    229  1.1  christos 
    230  1.1  christos static PyObject *
    231  1.1  christos sympy_is_valid (PyObject *self, PyObject *args)
    232  1.1  christos {
    233  1.1  christos   struct symbol *symbol = NULL;
    234  1.1  christos 
    235  1.1  christos   symbol = symbol_object_to_symbol (self);
    236  1.1  christos   if (symbol == NULL)
    237  1.1  christos     Py_RETURN_FALSE;
    238  1.1  christos 
    239  1.1  christos   Py_RETURN_TRUE;
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value.  Returns
    243  1.1  christos    the value of the symbol, or an error in various circumstances.  */
    244  1.1  christos 
    245  1.1  christos static PyObject *
    246  1.1  christos sympy_value (PyObject *self, PyObject *args)
    247  1.1  christos {
    248  1.1  christos   struct symbol *symbol = NULL;
    249  1.1  christos   struct frame_info *frame_info = NULL;
    250  1.1  christos   PyObject *frame_obj = NULL;
    251  1.1  christos   struct value *value = NULL;
    252  1.1  christos 
    253  1.1  christos   if (!PyArg_ParseTuple (args, "|O", &frame_obj))
    254  1.1  christos     return NULL;
    255  1.1  christos 
    256  1.1  christos   if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
    257  1.1  christos     {
    258  1.1  christos       PyErr_SetString (PyExc_TypeError, "argument is not a frame");
    259  1.1  christos       return NULL;
    260  1.1  christos     }
    261  1.1  christos 
    262  1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    263  1.1  christos   if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
    264  1.1  christos     {
    265  1.1  christos       PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
    266  1.1  christos       return NULL;
    267  1.1  christos     }
    268  1.1  christos 
    269  1.9  christos   try
    270  1.1  christos     {
    271  1.1  christos       if (frame_obj != NULL)
    272  1.1  christos 	{
    273  1.1  christos 	  frame_info = frame_object_to_frame_info (frame_obj);
    274  1.1  christos 	  if (frame_info == NULL)
    275  1.1  christos 	    error (_("invalid frame"));
    276  1.1  christos 	}
    277  1.1  christos 
    278  1.1  christos       if (symbol_read_needs_frame (symbol) && frame_info == NULL)
    279  1.1  christos 	error (_("symbol requires a frame to compute its value"));
    280  1.1  christos 
    281  1.6  christos       /* TODO: currently, we have no way to recover the block in which SYMBOL
    282  1.6  christos 	 was found, so we have no block to pass to read_var_value.  This will
    283  1.6  christos 	 yield an incorrect value when symbol is not local to FRAME_INFO (this
    284  1.6  christos 	 can happen with nested functions).  */
    285  1.6  christos       value = read_var_value (symbol, NULL, frame_info);
    286  1.1  christos     }
    287  1.9  christos   catch (const gdb_exception &except)
    288  1.5  christos     {
    289  1.5  christos       GDB_PY_HANDLE_EXCEPTION (except);
    290  1.5  christos     }
    291  1.1  christos 
    292  1.1  christos   return value_to_value_object (value);
    293  1.1  christos }
    294  1.1  christos 
    295  1.1  christos /* Given a symbol, and a symbol_object that has previously been
    296  1.1  christos    allocated and initialized, populate the symbol_object with the
    297  1.1  christos    struct symbol data.  Also, register the symbol_object life-cycle
    298  1.1  christos    with the life-cycle of the object file associated with this
    299  1.1  christos    symbol, if needed.  */
    300  1.1  christos static void
    301  1.1  christos set_symbol (symbol_object *obj, struct symbol *symbol)
    302  1.1  christos {
    303  1.1  christos   obj->symbol = symbol;
    304  1.1  christos   obj->prev = NULL;
    305  1.3  christos   if (SYMBOL_OBJFILE_OWNED (symbol)
    306  1.3  christos       && symbol_symtab (symbol) != NULL)
    307  1.1  christos     {
    308  1.3  christos       struct objfile *objfile = symbol_objfile (symbol);
    309  1.1  christos 
    310  1.6  christos       obj->next = ((struct sympy_symbol_object *)
    311  1.6  christos 		   objfile_data (objfile, sympy_objfile_data_key));
    312  1.1  christos       if (obj->next)
    313  1.1  christos 	obj->next->prev = obj;
    314  1.3  christos       set_objfile_data (objfile, sympy_objfile_data_key, obj);
    315  1.1  christos     }
    316  1.1  christos   else
    317  1.1  christos     obj->next = NULL;
    318  1.1  christos }
    319  1.1  christos 
    320  1.1  christos /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
    321  1.1  christos    symbol object from GDB.  */
    322  1.1  christos PyObject *
    323  1.1  christos symbol_to_symbol_object (struct symbol *sym)
    324  1.1  christos {
    325  1.1  christos   symbol_object *sym_obj;
    326  1.1  christos 
    327  1.1  christos   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
    328  1.1  christos   if (sym_obj)
    329  1.1  christos     set_symbol (sym_obj, sym);
    330  1.1  christos 
    331  1.1  christos   return (PyObject *) sym_obj;
    332  1.1  christos }
    333  1.1  christos 
    334  1.1  christos /* Return the symbol that is wrapped by this symbol object.  */
    335  1.1  christos struct symbol *
    336  1.1  christos symbol_object_to_symbol (PyObject *obj)
    337  1.1  christos {
    338  1.1  christos   if (! PyObject_TypeCheck (obj, &symbol_object_type))
    339  1.1  christos     return NULL;
    340  1.1  christos   return ((symbol_object *) obj)->symbol;
    341  1.1  christos }
    342  1.1  christos 
    343  1.1  christos static void
    344  1.1  christos sympy_dealloc (PyObject *obj)
    345  1.1  christos {
    346  1.1  christos   symbol_object *sym_obj = (symbol_object *) obj;
    347  1.1  christos 
    348  1.1  christos   if (sym_obj->prev)
    349  1.1  christos     sym_obj->prev->next = sym_obj->next;
    350  1.3  christos   else if (sym_obj->symbol != NULL
    351  1.3  christos 	   && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
    352  1.3  christos 	   && symbol_symtab (sym_obj->symbol) != NULL)
    353  1.1  christos     {
    354  1.3  christos       set_objfile_data (symbol_objfile (sym_obj->symbol),
    355  1.1  christos 			sympy_objfile_data_key, sym_obj->next);
    356  1.1  christos     }
    357  1.1  christos   if (sym_obj->next)
    358  1.1  christos     sym_obj->next->prev = sym_obj->prev;
    359  1.1  christos   sym_obj->symbol = NULL;
    360  1.9  christos   Py_TYPE (obj)->tp_free (obj);
    361  1.1  christos }
    362  1.1  christos 
    363  1.1  christos /* Implementation of
    364  1.1  christos    gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
    365  1.1  christos    A tuple with 2 elements is always returned.  The first is the symbol
    366  1.1  christos    object or None, the second is a boolean with the value of
    367  1.1  christos    is_a_field_of_this (see comment in lookup_symbol_in_language).  */
    368  1.1  christos 
    369  1.1  christos PyObject *
    370  1.1  christos gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
    371  1.1  christos {
    372  1.1  christos   int domain = VAR_DOMAIN;
    373  1.1  christos   struct field_of_this_result is_a_field_of_this;
    374  1.1  christos   const char *name;
    375  1.7  christos   static const char *keywords[] = { "name", "block", "domain", NULL };
    376  1.1  christos   struct symbol *symbol = NULL;
    377  1.7  christos   PyObject *block_obj = NULL, *sym_obj, *bool_obj;
    378  1.1  christos   const struct block *block = NULL;
    379  1.1  christos 
    380  1.7  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
    381  1.7  christos 					&block_object_type, &block_obj,
    382  1.7  christos 					&domain))
    383  1.1  christos     return NULL;
    384  1.1  christos 
    385  1.1  christos   if (block_obj)
    386  1.1  christos     block = block_object_to_block (block_obj);
    387  1.1  christos   else
    388  1.1  christos     {
    389  1.1  christos       struct frame_info *selected_frame;
    390  1.1  christos 
    391  1.9  christos       try
    392  1.1  christos 	{
    393  1.1  christos 	  selected_frame = get_selected_frame (_("No frame selected."));
    394  1.1  christos 	  block = get_frame_block (selected_frame, NULL);
    395  1.1  christos 	}
    396  1.9  christos       catch (const gdb_exception &except)
    397  1.5  christos 	{
    398  1.5  christos 	  GDB_PY_HANDLE_EXCEPTION (except);
    399  1.5  christos 	}
    400  1.1  christos     }
    401  1.1  christos 
    402  1.9  christos   try
    403  1.1  christos     {
    404  1.6  christos       symbol = lookup_symbol (name, block, (domain_enum) domain,
    405  1.6  christos 			      &is_a_field_of_this).symbol;
    406  1.1  christos     }
    407  1.9  christos   catch (const gdb_exception &except)
    408  1.5  christos     {
    409  1.5  christos       GDB_PY_HANDLE_EXCEPTION (except);
    410  1.5  christos     }
    411  1.1  christos 
    412  1.7  christos   gdbpy_ref<> ret_tuple (PyTuple_New (2));
    413  1.7  christos   if (ret_tuple == NULL)
    414  1.1  christos     return NULL;
    415  1.1  christos 
    416  1.1  christos   if (symbol)
    417  1.1  christos     {
    418  1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    419  1.1  christos       if (!sym_obj)
    420  1.7  christos 	return NULL;
    421  1.1  christos     }
    422  1.1  christos   else
    423  1.1  christos     {
    424  1.1  christos       sym_obj = Py_None;
    425  1.1  christos       Py_INCREF (Py_None);
    426  1.1  christos     }
    427  1.7  christos   PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj);
    428  1.1  christos 
    429  1.1  christos   bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
    430  1.1  christos   Py_INCREF (bool_obj);
    431  1.7  christos   PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
    432  1.1  christos 
    433  1.7  christos   return ret_tuple.release ();
    434  1.1  christos }
    435  1.1  christos 
    436  1.1  christos /* Implementation of
    437  1.1  christos    gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */
    438  1.1  christos 
    439  1.1  christos PyObject *
    440  1.1  christos gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
    441  1.1  christos {
    442  1.1  christos   int domain = VAR_DOMAIN;
    443  1.1  christos   const char *name;
    444  1.7  christos   static const char *keywords[] = { "name", "domain", NULL };
    445  1.1  christos   struct symbol *symbol = NULL;
    446  1.1  christos   PyObject *sym_obj;
    447  1.1  christos 
    448  1.7  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
    449  1.7  christos 					&domain))
    450  1.1  christos     return NULL;
    451  1.1  christos 
    452  1.9  christos   try
    453  1.1  christos     {
    454  1.6  christos       symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol;
    455  1.1  christos     }
    456  1.9  christos   catch (const gdb_exception &except)
    457  1.5  christos     {
    458  1.5  christos       GDB_PY_HANDLE_EXCEPTION (except);
    459  1.5  christos     }
    460  1.1  christos 
    461  1.1  christos   if (symbol)
    462  1.1  christos     {
    463  1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    464  1.1  christos       if (!sym_obj)
    465  1.1  christos 	return NULL;
    466  1.1  christos     }
    467  1.1  christos   else
    468  1.1  christos     {
    469  1.1  christos       sym_obj = Py_None;
    470  1.1  christos       Py_INCREF (Py_None);
    471  1.1  christos     }
    472  1.1  christos 
    473  1.1  christos   return sym_obj;
    474  1.1  christos }
    475  1.1  christos 
    476  1.9  christos /* Implementation of
    477  1.9  christos    gdb.lookup_static_symbol (name [, domain]) -> symbol or None.  */
    478  1.9  christos 
    479  1.9  christos PyObject *
    480  1.9  christos gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
    481  1.9  christos {
    482  1.9  christos   const char *name;
    483  1.9  christos   int domain = VAR_DOMAIN;
    484  1.9  christos   static const char *keywords[] = { "name", "domain", NULL };
    485  1.9  christos   struct symbol *symbol = NULL;
    486  1.9  christos   PyObject *sym_obj;
    487  1.9  christos 
    488  1.9  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
    489  1.9  christos 					&domain))
    490  1.9  christos     return NULL;
    491  1.9  christos 
    492  1.9  christos   /* In order to find static symbols associated with the "current" object
    493  1.9  christos      file ahead of those from other object files, we first need to see if
    494  1.9  christos      we can acquire a current block.  If this fails however, then we still
    495  1.9  christos      want to search all static symbols, so don't throw an exception just
    496  1.9  christos      yet.  */
    497  1.9  christos   const struct block *block = NULL;
    498  1.9  christos   try
    499  1.9  christos     {
    500  1.9  christos       struct frame_info *selected_frame
    501  1.9  christos 	= get_selected_frame (_("No frame selected."));
    502  1.9  christos       block = get_frame_block (selected_frame, NULL);
    503  1.9  christos     }
    504  1.9  christos   catch (const gdb_exception &except)
    505  1.9  christos     {
    506  1.9  christos       /* Nothing.  */
    507  1.9  christos     }
    508  1.9  christos 
    509  1.9  christos   try
    510  1.9  christos     {
    511  1.9  christos       if (block != nullptr)
    512  1.9  christos 	symbol
    513  1.9  christos 	  = lookup_symbol_in_static_block (name, block,
    514  1.9  christos 					   (domain_enum) domain).symbol;
    515  1.9  christos 
    516  1.9  christos       if (symbol == nullptr)
    517  1.9  christos 	symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
    518  1.9  christos     }
    519  1.9  christos   catch (const gdb_exception &except)
    520  1.9  christos     {
    521  1.9  christos       GDB_PY_HANDLE_EXCEPTION (except);
    522  1.9  christos     }
    523  1.9  christos 
    524  1.9  christos   if (symbol)
    525  1.9  christos     {
    526  1.9  christos       sym_obj = symbol_to_symbol_object (symbol);
    527  1.9  christos       if (!sym_obj)
    528  1.9  christos 	return NULL;
    529  1.9  christos     }
    530  1.9  christos   else
    531  1.9  christos     {
    532  1.9  christos       sym_obj = Py_None;
    533  1.9  christos       Py_INCREF (Py_None);
    534  1.9  christos     }
    535  1.9  christos 
    536  1.9  christos   return sym_obj;
    537  1.9  christos }
    538  1.9  christos 
    539  1.9  christos /* Implementation of
    540  1.9  christos    gdb.lookup_static_symbols (name [, domain]) -> symbol list.
    541  1.9  christos 
    542  1.9  christos    Returns a list of all static symbols matching NAME in DOMAIN.  */
    543  1.9  christos 
    544  1.9  christos PyObject *
    545  1.9  christos gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
    546  1.9  christos {
    547  1.9  christos   const char *name;
    548  1.9  christos   int domain = VAR_DOMAIN;
    549  1.9  christos   static const char *keywords[] = { "name", "domain", NULL };
    550  1.9  christos 
    551  1.9  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
    552  1.9  christos 					&domain))
    553  1.9  christos     return NULL;
    554  1.9  christos 
    555  1.9  christos   gdbpy_ref<> return_list (PyList_New (0));
    556  1.9  christos   if (return_list == NULL)
    557  1.9  christos     return NULL;
    558  1.9  christos 
    559  1.9  christos   try
    560  1.9  christos     {
    561  1.9  christos       /* Expand any symtabs that contain potentially matching symbols.  */
    562  1.9  christos       lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
    563  1.9  christos       expand_symtabs_matching (NULL, lookup_name, NULL, NULL, ALL_DOMAIN);
    564  1.9  christos 
    565  1.9  christos       for (objfile *objfile : current_program_space->objfiles ())
    566  1.9  christos 	{
    567  1.9  christos 	  for (compunit_symtab *cust : objfile->compunits ())
    568  1.9  christos 	    {
    569  1.9  christos 	      const struct blockvector *bv;
    570  1.9  christos 	      const struct block *block;
    571  1.9  christos 
    572  1.9  christos 	      bv = COMPUNIT_BLOCKVECTOR (cust);
    573  1.9  christos 	      block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
    574  1.9  christos 
    575  1.9  christos 	      if (block != nullptr)
    576  1.9  christos 		{
    577  1.9  christos 		  symbol *symbol = lookup_symbol_in_static_block
    578  1.9  christos 		    (name, block, (domain_enum) domain).symbol;
    579  1.9  christos 
    580  1.9  christos 		  if (symbol != nullptr)
    581  1.9  christos 		    {
    582  1.9  christos 		      PyObject *sym_obj
    583  1.9  christos 			= symbol_to_symbol_object (symbol);
    584  1.9  christos 		      if (PyList_Append (return_list.get (), sym_obj) == -1)
    585  1.9  christos 			return NULL;
    586  1.9  christos 		    }
    587  1.9  christos 		}
    588  1.9  christos 	    }
    589  1.9  christos 	}
    590  1.9  christos     }
    591  1.9  christos   catch (const gdb_exception &except)
    592  1.9  christos     {
    593  1.9  christos       GDB_PY_HANDLE_EXCEPTION (except);
    594  1.9  christos     }
    595  1.9  christos 
    596  1.9  christos   return return_list.release ();
    597  1.9  christos }
    598  1.9  christos 
    599  1.1  christos /* This function is called when an objfile is about to be freed.
    600  1.1  christos    Invalidate the symbol as further actions on the symbol would result
    601  1.1  christos    in bad data.  All access to obj->symbol should be gated by
    602  1.1  christos    SYMPY_REQUIRE_VALID which will raise an exception on invalid
    603  1.1  christos    symbols.  */
    604  1.1  christos static void
    605  1.1  christos del_objfile_symbols (struct objfile *objfile, void *datum)
    606  1.1  christos {
    607  1.6  christos   symbol_object *obj = (symbol_object *) datum;
    608  1.1  christos   while (obj)
    609  1.1  christos     {
    610  1.1  christos       symbol_object *next = obj->next;
    611  1.1  christos 
    612  1.1  christos       obj->symbol = NULL;
    613  1.1  christos       obj->next = NULL;
    614  1.1  christos       obj->prev = NULL;
    615  1.1  christos 
    616  1.1  christos       obj = next;
    617  1.1  christos     }
    618  1.1  christos }
    619  1.1  christos 
    620  1.1  christos int
    621  1.1  christos gdbpy_initialize_symbols (void)
    622  1.1  christos {
    623  1.1  christos   if (PyType_Ready (&symbol_object_type) < 0)
    624  1.1  christos     return -1;
    625  1.1  christos 
    626  1.1  christos   /* Register an objfile "free" callback so we can properly
    627  1.1  christos      invalidate symbol when an object file that is about to be
    628  1.1  christos      deleted.  */
    629  1.1  christos   sympy_objfile_data_key
    630  1.1  christos     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
    631  1.1  christos 
    632  1.1  christos   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
    633  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
    634  1.1  christos 				  LOC_CONST) < 0
    635  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
    636  1.1  christos 				  LOC_STATIC) < 0
    637  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
    638  1.1  christos 				  LOC_REGISTER) < 0
    639  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
    640  1.1  christos 				  LOC_ARG) < 0
    641  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
    642  1.1  christos 				  LOC_REF_ARG) < 0
    643  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
    644  1.1  christos 				  LOC_LOCAL) < 0
    645  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
    646  1.1  christos 				  LOC_TYPEDEF) < 0
    647  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
    648  1.1  christos 				  LOC_LABEL) < 0
    649  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
    650  1.1  christos 				  LOC_BLOCK) < 0
    651  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
    652  1.1  christos 				  LOC_CONST_BYTES) < 0
    653  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
    654  1.1  christos 				  LOC_UNRESOLVED) < 0
    655  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
    656  1.1  christos 				  LOC_OPTIMIZED_OUT) < 0
    657  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
    658  1.1  christos 				  LOC_COMPUTED) < 0
    659  1.8  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMMON_BLOCK",
    660  1.8  christos 				  LOC_COMMON_BLOCK) < 0
    661  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
    662  1.1  christos 				  LOC_REGPARM_ADDR) < 0
    663  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
    664  1.1  christos 				  UNDEF_DOMAIN) < 0
    665  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
    666  1.1  christos 				  VAR_DOMAIN) < 0
    667  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
    668  1.1  christos 				  STRUCT_DOMAIN) < 0
    669  1.8  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_MODULE_DOMAIN",
    670  1.8  christos 				  MODULE_DOMAIN) < 0
    671  1.8  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_COMMON_BLOCK_DOMAIN",
    672  1.8  christos 				  COMMON_BLOCK_DOMAIN) < 0)
    673  1.8  christos     return -1;
    674  1.8  christos 
    675  1.8  christos   /* These remain defined for compatibility, but as they were never
    676  1.8  christos      correct, they are no longer documented.  Eventually we can remove
    677  1.8  christos      them.  These exist because at one time, enum search_domain and
    678  1.8  christos      enum domain_enum_tag were combined -- but different values were
    679  1.8  christos      used differently.  Here we try to give them values that will make
    680  1.8  christos      sense if they are passed to gdb.lookup_symbol.  */
    681  1.8  christos   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
    682  1.8  christos 			       VAR_DOMAIN) < 0
    683  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
    684  1.8  christos 				  VAR_DOMAIN) < 0
    685  1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
    686  1.8  christos 				  VAR_DOMAIN) < 0)
    687  1.1  christos     return -1;
    688  1.1  christos 
    689  1.1  christos   return gdb_pymodule_addobject (gdb_module, "Symbol",
    690  1.1  christos 				 (PyObject *) &symbol_object_type);
    691  1.1  christos }
    692  1.1  christos 
    693  1.1  christos 
    694  1.1  christos 
    696  1.1  christos static gdb_PyGetSetDef symbol_object_getset[] = {
    697  1.1  christos   { "type", sympy_get_type, NULL,
    698  1.1  christos     "Type of the symbol.", NULL },
    699  1.1  christos   { "symtab", sympy_get_symtab, NULL,
    700  1.1  christos     "Symbol table in which the symbol appears.", NULL },
    701  1.1  christos   { "name", sympy_get_name, NULL,
    702  1.1  christos     "Name of the symbol, as it appears in the source code.", NULL },
    703  1.1  christos   { "linkage_name", sympy_get_linkage_name, NULL,
    704  1.1  christos     "Name of the symbol, as used by the linker (i.e., may be mangled).",
    705  1.1  christos     NULL },
    706  1.1  christos   { "print_name", sympy_get_print_name, NULL,
    707  1.1  christos     "Name of the symbol in a form suitable for output.\n\
    708  1.1  christos This is either name or linkage_name, depending on whether the user asked GDB\n\
    709  1.1  christos to display demangled or mangled names.", NULL },
    710  1.1  christos   { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
    711  1.1  christos   { "is_argument", sympy_is_argument, NULL,
    712  1.1  christos     "True if the symbol is an argument of a function." },
    713  1.1  christos   { "is_constant", sympy_is_constant, NULL,
    714  1.1  christos     "True if the symbol is a constant." },
    715  1.1  christos   { "is_function", sympy_is_function, NULL,
    716  1.1  christos     "True if the symbol is a function or method." },
    717  1.1  christos   { "is_variable", sympy_is_variable, NULL,
    718  1.1  christos     "True if the symbol is a variable." },
    719  1.1  christos   { "needs_frame", sympy_needs_frame, NULL,
    720  1.1  christos     "True if the symbol requires a frame for evaluation." },
    721  1.1  christos   { "line", sympy_line, NULL,
    722  1.1  christos     "The source line number at which the symbol was defined." },
    723  1.1  christos   { NULL }  /* Sentinel */
    724  1.1  christos };
    725  1.1  christos 
    726  1.1  christos static PyMethodDef symbol_object_methods[] = {
    727  1.1  christos   { "is_valid", sympy_is_valid, METH_NOARGS,
    728  1.1  christos     "is_valid () -> Boolean.\n\
    729  1.1  christos Return true if this symbol is valid, false if not." },
    730  1.1  christos   { "value", sympy_value, METH_VARARGS,
    731  1.1  christos     "value ([frame]) -> gdb.Value\n\
    732  1.1  christos Return the value of the symbol." },
    733  1.1  christos   {NULL}  /* Sentinel */
    734  1.1  christos };
    735  1.1  christos 
    736  1.1  christos PyTypeObject symbol_object_type = {
    737  1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    738  1.1  christos   "gdb.Symbol",			  /*tp_name*/
    739  1.1  christos   sizeof (symbol_object),	  /*tp_basicsize*/
    740  1.1  christos   0,				  /*tp_itemsize*/
    741  1.1  christos   sympy_dealloc,		  /*tp_dealloc*/
    742  1.1  christos   0,				  /*tp_print*/
    743  1.1  christos   0,				  /*tp_getattr*/
    744  1.1  christos   0,				  /*tp_setattr*/
    745  1.1  christos   0,				  /*tp_compare*/
    746  1.1  christos   0,				  /*tp_repr*/
    747  1.1  christos   0,				  /*tp_as_number*/
    748  1.1  christos   0,				  /*tp_as_sequence*/
    749  1.1  christos   0,				  /*tp_as_mapping*/
    750  1.1  christos   0,				  /*tp_hash */
    751  1.1  christos   0,				  /*tp_call*/
    752  1.1  christos   sympy_str,			  /*tp_str*/
    753  1.1  christos   0,				  /*tp_getattro*/
    754  1.1  christos   0,				  /*tp_setattro*/
    755  1.1  christos   0,				  /*tp_as_buffer*/
    756  1.1  christos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
    757  1.1  christos   "GDB symbol object",		  /*tp_doc */
    758  1.1  christos   0,				  /*tp_traverse */
    759  1.1  christos   0,				  /*tp_clear */
    760  1.1  christos   0,				  /*tp_richcompare */
    761  1.1  christos   0,				  /*tp_weaklistoffset */
    762  1.1  christos   0,				  /*tp_iter */
    763  1.1  christos   0,				  /*tp_iternext */
    764  1.1  christos   symbol_object_methods,	  /*tp_methods */
    765  1.1  christos   0,				  /*tp_members */
    766  1.1  christos   symbol_object_getset		  /*tp_getset */
    767                };
    768