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