Home | History | Annotate | Line # | Download | only in python
py-symbol.c revision 1.1.1.5
      1      1.1  christos /* Python interface to symbols.
      2      1.1  christos 
      3  1.1.1.5  christos    Copyright (C) 2008-2017 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.1.1.5  christos #include "py-ref.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.1  christos   result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
     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.1.1.2  christos   if (!SYMBOL_OBJFILE_OWNED (symbol))
     92  1.1.1.2  christos     Py_RETURN_NONE;
     93  1.1.1.2  christos 
     94  1.1.1.2  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.1  christos   return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
    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.1  christos   return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
    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.1.1.3  christos   enum address_class theclass;
    152      1.1  christos 
    153      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    154      1.1  christos 
    155  1.1.1.3  christos   theclass = SYMBOL_CLASS (symbol);
    156      1.1  christos 
    157  1.1.1.3  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.1.1.3  christos   enum address_class theclass;
    165      1.1  christos 
    166      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    167      1.1  christos 
    168  1.1.1.3  christos   theclass = SYMBOL_CLASS (symbol);
    169      1.1  christos 
    170  1.1.1.3  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.1.1.3  christos   enum address_class theclass;
    178      1.1  christos 
    179      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    180      1.1  christos 
    181  1.1.1.3  christos   theclass = SYMBOL_CLASS (symbol);
    182      1.1  christos 
    183      1.1  christos   return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
    184  1.1.1.3  christos 			  && (theclass == LOC_LOCAL || theclass == LOC_REGISTER
    185  1.1.1.3  christos 			      || theclass == LOC_STATIC || theclass == LOC_COMPUTED
    186  1.1.1.3  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.1.1.3  christos   TRY
    201      1.1  christos     {
    202      1.1  christos       result = symbol_read_needs_frame (symbol);
    203      1.1  christos     }
    204  1.1.1.3  christos   CATCH (except, RETURN_MASK_ALL)
    205  1.1.1.3  christos     {
    206  1.1.1.3  christos       GDB_PY_HANDLE_EXCEPTION (except);
    207  1.1.1.3  christos     }
    208  1.1.1.3  christos   END_CATCH
    209      1.1  christos 
    210      1.1  christos   if (result)
    211      1.1  christos     Py_RETURN_TRUE;
    212      1.1  christos   Py_RETURN_FALSE;
    213      1.1  christos }
    214      1.1  christos 
    215      1.1  christos /* Implementation of gdb.Symbol.line -> int.
    216      1.1  christos    Returns the line number at which the symbol was defined.  */
    217      1.1  christos 
    218      1.1  christos static PyObject *
    219      1.1  christos sympy_line (PyObject *self, void *closure)
    220      1.1  christos {
    221      1.1  christos   struct symbol *symbol = NULL;
    222      1.1  christos 
    223      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    224      1.1  christos 
    225      1.1  christos   return PyInt_FromLong (SYMBOL_LINE (symbol));
    226      1.1  christos }
    227      1.1  christos 
    228      1.1  christos /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
    229      1.1  christos    Returns True if this Symbol still exists in GDB.  */
    230      1.1  christos 
    231      1.1  christos static PyObject *
    232      1.1  christos sympy_is_valid (PyObject *self, PyObject *args)
    233      1.1  christos {
    234      1.1  christos   struct symbol *symbol = NULL;
    235      1.1  christos 
    236      1.1  christos   symbol = symbol_object_to_symbol (self);
    237      1.1  christos   if (symbol == NULL)
    238      1.1  christos     Py_RETURN_FALSE;
    239      1.1  christos 
    240      1.1  christos   Py_RETURN_TRUE;
    241      1.1  christos }
    242      1.1  christos 
    243      1.1  christos /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value.  Returns
    244      1.1  christos    the value of the symbol, or an error in various circumstances.  */
    245      1.1  christos 
    246      1.1  christos static PyObject *
    247      1.1  christos sympy_value (PyObject *self, PyObject *args)
    248      1.1  christos {
    249      1.1  christos   struct symbol *symbol = NULL;
    250      1.1  christos   struct frame_info *frame_info = NULL;
    251      1.1  christos   PyObject *frame_obj = NULL;
    252      1.1  christos   struct value *value = NULL;
    253      1.1  christos 
    254      1.1  christos   if (!PyArg_ParseTuple (args, "|O", &frame_obj))
    255      1.1  christos     return NULL;
    256      1.1  christos 
    257      1.1  christos   if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
    258      1.1  christos     {
    259      1.1  christos       PyErr_SetString (PyExc_TypeError, "argument is not a frame");
    260      1.1  christos       return NULL;
    261      1.1  christos     }
    262      1.1  christos 
    263      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    264      1.1  christos   if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
    265      1.1  christos     {
    266      1.1  christos       PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
    267      1.1  christos       return NULL;
    268      1.1  christos     }
    269      1.1  christos 
    270  1.1.1.3  christos   TRY
    271      1.1  christos     {
    272      1.1  christos       if (frame_obj != NULL)
    273      1.1  christos 	{
    274      1.1  christos 	  frame_info = frame_object_to_frame_info (frame_obj);
    275      1.1  christos 	  if (frame_info == NULL)
    276      1.1  christos 	    error (_("invalid frame"));
    277      1.1  christos 	}
    278      1.1  christos 
    279      1.1  christos       if (symbol_read_needs_frame (symbol) && frame_info == NULL)
    280      1.1  christos 	error (_("symbol requires a frame to compute its value"));
    281      1.1  christos 
    282  1.1.1.4  christos       /* TODO: currently, we have no way to recover the block in which SYMBOL
    283  1.1.1.4  christos 	 was found, so we have no block to pass to read_var_value.  This will
    284  1.1.1.4  christos 	 yield an incorrect value when symbol is not local to FRAME_INFO (this
    285  1.1.1.4  christos 	 can happen with nested functions).  */
    286  1.1.1.4  christos       value = read_var_value (symbol, NULL, frame_info);
    287      1.1  christos     }
    288  1.1.1.3  christos   CATCH (except, RETURN_MASK_ALL)
    289  1.1.1.3  christos     {
    290  1.1.1.3  christos       GDB_PY_HANDLE_EXCEPTION (except);
    291  1.1.1.3  christos     }
    292  1.1.1.3  christos   END_CATCH
    293      1.1  christos 
    294      1.1  christos   return value_to_value_object (value);
    295      1.1  christos }
    296      1.1  christos 
    297      1.1  christos /* Given a symbol, and a symbol_object that has previously been
    298      1.1  christos    allocated and initialized, populate the symbol_object with the
    299      1.1  christos    struct symbol data.  Also, register the symbol_object life-cycle
    300      1.1  christos    with the life-cycle of the object file associated with this
    301      1.1  christos    symbol, if needed.  */
    302      1.1  christos static void
    303      1.1  christos set_symbol (symbol_object *obj, struct symbol *symbol)
    304      1.1  christos {
    305      1.1  christos   obj->symbol = symbol;
    306      1.1  christos   obj->prev = NULL;
    307  1.1.1.2  christos   if (SYMBOL_OBJFILE_OWNED (symbol)
    308  1.1.1.2  christos       && symbol_symtab (symbol) != NULL)
    309      1.1  christos     {
    310  1.1.1.2  christos       struct objfile *objfile = symbol_objfile (symbol);
    311      1.1  christos 
    312  1.1.1.4  christos       obj->next = ((struct sympy_symbol_object *)
    313  1.1.1.4  christos 		   objfile_data (objfile, sympy_objfile_data_key));
    314      1.1  christos       if (obj->next)
    315      1.1  christos 	obj->next->prev = obj;
    316  1.1.1.2  christos       set_objfile_data (objfile, sympy_objfile_data_key, obj);
    317      1.1  christos     }
    318      1.1  christos   else
    319      1.1  christos     obj->next = NULL;
    320      1.1  christos }
    321      1.1  christos 
    322      1.1  christos /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
    323      1.1  christos    symbol object from GDB.  */
    324      1.1  christos PyObject *
    325      1.1  christos symbol_to_symbol_object (struct symbol *sym)
    326      1.1  christos {
    327      1.1  christos   symbol_object *sym_obj;
    328      1.1  christos 
    329      1.1  christos   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
    330      1.1  christos   if (sym_obj)
    331      1.1  christos     set_symbol (sym_obj, sym);
    332      1.1  christos 
    333      1.1  christos   return (PyObject *) sym_obj;
    334      1.1  christos }
    335      1.1  christos 
    336      1.1  christos /* Return the symbol that is wrapped by this symbol object.  */
    337      1.1  christos struct symbol *
    338      1.1  christos symbol_object_to_symbol (PyObject *obj)
    339      1.1  christos {
    340      1.1  christos   if (! PyObject_TypeCheck (obj, &symbol_object_type))
    341      1.1  christos     return NULL;
    342      1.1  christos   return ((symbol_object *) obj)->symbol;
    343      1.1  christos }
    344      1.1  christos 
    345      1.1  christos static void
    346      1.1  christos sympy_dealloc (PyObject *obj)
    347      1.1  christos {
    348      1.1  christos   symbol_object *sym_obj = (symbol_object *) obj;
    349      1.1  christos 
    350      1.1  christos   if (sym_obj->prev)
    351      1.1  christos     sym_obj->prev->next = sym_obj->next;
    352  1.1.1.2  christos   else if (sym_obj->symbol != NULL
    353  1.1.1.2  christos 	   && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
    354  1.1.1.2  christos 	   && symbol_symtab (sym_obj->symbol) != NULL)
    355      1.1  christos     {
    356  1.1.1.2  christos       set_objfile_data (symbol_objfile (sym_obj->symbol),
    357      1.1  christos 			sympy_objfile_data_key, sym_obj->next);
    358      1.1  christos     }
    359      1.1  christos   if (sym_obj->next)
    360      1.1  christos     sym_obj->next->prev = sym_obj->prev;
    361      1.1  christos   sym_obj->symbol = NULL;
    362      1.1  christos }
    363      1.1  christos 
    364      1.1  christos /* Implementation of
    365      1.1  christos    gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
    366      1.1  christos    A tuple with 2 elements is always returned.  The first is the symbol
    367      1.1  christos    object or None, the second is a boolean with the value of
    368      1.1  christos    is_a_field_of_this (see comment in lookup_symbol_in_language).  */
    369      1.1  christos 
    370      1.1  christos PyObject *
    371      1.1  christos gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
    372      1.1  christos {
    373      1.1  christos   int domain = VAR_DOMAIN;
    374      1.1  christos   struct field_of_this_result is_a_field_of_this;
    375      1.1  christos   const char *name;
    376  1.1.1.5  christos   static const char *keywords[] = { "name", "block", "domain", NULL };
    377      1.1  christos   struct symbol *symbol = NULL;
    378  1.1.1.5  christos   PyObject *block_obj = NULL, *sym_obj, *bool_obj;
    379      1.1  christos   const struct block *block = NULL;
    380      1.1  christos 
    381  1.1.1.5  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
    382  1.1.1.5  christos 					&block_object_type, &block_obj,
    383  1.1.1.5  christos 					&domain))
    384      1.1  christos     return NULL;
    385      1.1  christos 
    386      1.1  christos   if (block_obj)
    387      1.1  christos     block = block_object_to_block (block_obj);
    388      1.1  christos   else
    389      1.1  christos     {
    390      1.1  christos       struct frame_info *selected_frame;
    391      1.1  christos 
    392  1.1.1.3  christos       TRY
    393      1.1  christos 	{
    394      1.1  christos 	  selected_frame = get_selected_frame (_("No frame selected."));
    395      1.1  christos 	  block = get_frame_block (selected_frame, NULL);
    396      1.1  christos 	}
    397  1.1.1.3  christos       CATCH (except, RETURN_MASK_ALL)
    398  1.1.1.3  christos 	{
    399  1.1.1.3  christos 	  GDB_PY_HANDLE_EXCEPTION (except);
    400  1.1.1.3  christos 	}
    401  1.1.1.3  christos       END_CATCH
    402      1.1  christos     }
    403      1.1  christos 
    404  1.1.1.3  christos   TRY
    405      1.1  christos     {
    406  1.1.1.4  christos       symbol = lookup_symbol (name, block, (domain_enum) domain,
    407  1.1.1.4  christos 			      &is_a_field_of_this).symbol;
    408      1.1  christos     }
    409  1.1.1.3  christos   CATCH (except, RETURN_MASK_ALL)
    410  1.1.1.3  christos     {
    411  1.1.1.3  christos       GDB_PY_HANDLE_EXCEPTION (except);
    412  1.1.1.3  christos     }
    413  1.1.1.3  christos   END_CATCH
    414      1.1  christos 
    415  1.1.1.5  christos   gdbpy_ref<> ret_tuple (PyTuple_New (2));
    416  1.1.1.5  christos   if (ret_tuple == NULL)
    417      1.1  christos     return NULL;
    418      1.1  christos 
    419      1.1  christos   if (symbol)
    420      1.1  christos     {
    421      1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    422      1.1  christos       if (!sym_obj)
    423  1.1.1.5  christos 	return NULL;
    424      1.1  christos     }
    425      1.1  christos   else
    426      1.1  christos     {
    427      1.1  christos       sym_obj = Py_None;
    428      1.1  christos       Py_INCREF (Py_None);
    429      1.1  christos     }
    430  1.1.1.5  christos   PyTuple_SET_ITEM (ret_tuple.get (), 0, sym_obj);
    431      1.1  christos 
    432      1.1  christos   bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
    433      1.1  christos   Py_INCREF (bool_obj);
    434  1.1.1.5  christos   PyTuple_SET_ITEM (ret_tuple.get (), 1, bool_obj);
    435      1.1  christos 
    436  1.1.1.5  christos   return ret_tuple.release ();
    437      1.1  christos }
    438      1.1  christos 
    439      1.1  christos /* Implementation of
    440      1.1  christos    gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */
    441      1.1  christos 
    442      1.1  christos PyObject *
    443      1.1  christos gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
    444      1.1  christos {
    445      1.1  christos   int domain = VAR_DOMAIN;
    446      1.1  christos   const char *name;
    447  1.1.1.5  christos   static const char *keywords[] = { "name", "domain", NULL };
    448      1.1  christos   struct symbol *symbol = NULL;
    449      1.1  christos   PyObject *sym_obj;
    450      1.1  christos 
    451  1.1.1.5  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
    452  1.1.1.5  christos 					&domain))
    453      1.1  christos     return NULL;
    454      1.1  christos 
    455  1.1.1.3  christos   TRY
    456      1.1  christos     {
    457  1.1.1.4  christos       symbol = lookup_global_symbol (name, NULL, (domain_enum) domain).symbol;
    458      1.1  christos     }
    459  1.1.1.3  christos   CATCH (except, RETURN_MASK_ALL)
    460  1.1.1.3  christos     {
    461  1.1.1.3  christos       GDB_PY_HANDLE_EXCEPTION (except);
    462  1.1.1.3  christos     }
    463  1.1.1.3  christos   END_CATCH
    464      1.1  christos 
    465      1.1  christos   if (symbol)
    466      1.1  christos     {
    467      1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    468      1.1  christos       if (!sym_obj)
    469      1.1  christos 	return NULL;
    470      1.1  christos     }
    471      1.1  christos   else
    472      1.1  christos     {
    473      1.1  christos       sym_obj = Py_None;
    474      1.1  christos       Py_INCREF (Py_None);
    475      1.1  christos     }
    476      1.1  christos 
    477      1.1  christos   return sym_obj;
    478      1.1  christos }
    479      1.1  christos 
    480      1.1  christos /* This function is called when an objfile is about to be freed.
    481      1.1  christos    Invalidate the symbol as further actions on the symbol would result
    482      1.1  christos    in bad data.  All access to obj->symbol should be gated by
    483      1.1  christos    SYMPY_REQUIRE_VALID which will raise an exception on invalid
    484      1.1  christos    symbols.  */
    485      1.1  christos static void
    486      1.1  christos del_objfile_symbols (struct objfile *objfile, void *datum)
    487      1.1  christos {
    488  1.1.1.4  christos   symbol_object *obj = (symbol_object *) datum;
    489      1.1  christos   while (obj)
    490      1.1  christos     {
    491      1.1  christos       symbol_object *next = obj->next;
    492      1.1  christos 
    493      1.1  christos       obj->symbol = NULL;
    494      1.1  christos       obj->next = NULL;
    495      1.1  christos       obj->prev = NULL;
    496      1.1  christos 
    497      1.1  christos       obj = next;
    498      1.1  christos     }
    499      1.1  christos }
    500      1.1  christos 
    501      1.1  christos int
    502      1.1  christos gdbpy_initialize_symbols (void)
    503      1.1  christos {
    504      1.1  christos   if (PyType_Ready (&symbol_object_type) < 0)
    505      1.1  christos     return -1;
    506      1.1  christos 
    507      1.1  christos   /* Register an objfile "free" callback so we can properly
    508      1.1  christos      invalidate symbol when an object file that is about to be
    509      1.1  christos      deleted.  */
    510      1.1  christos   sympy_objfile_data_key
    511      1.1  christos     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
    512      1.1  christos 
    513      1.1  christos   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
    514      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
    515      1.1  christos 				  LOC_CONST) < 0
    516      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
    517      1.1  christos 				  LOC_STATIC) < 0
    518      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
    519      1.1  christos 				  LOC_REGISTER) < 0
    520      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
    521      1.1  christos 				  LOC_ARG) < 0
    522      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
    523      1.1  christos 				  LOC_REF_ARG) < 0
    524      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
    525      1.1  christos 				  LOC_LOCAL) < 0
    526      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
    527      1.1  christos 				  LOC_TYPEDEF) < 0
    528      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
    529      1.1  christos 				  LOC_LABEL) < 0
    530      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
    531      1.1  christos 				  LOC_BLOCK) < 0
    532      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
    533      1.1  christos 				  LOC_CONST_BYTES) < 0
    534      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
    535      1.1  christos 				  LOC_UNRESOLVED) < 0
    536      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
    537      1.1  christos 				  LOC_OPTIMIZED_OUT) < 0
    538      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
    539      1.1  christos 				  LOC_COMPUTED) < 0
    540      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
    541      1.1  christos 				  LOC_REGPARM_ADDR) < 0
    542      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
    543      1.1  christos 				  UNDEF_DOMAIN) < 0
    544      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
    545      1.1  christos 				  VAR_DOMAIN) < 0
    546      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
    547      1.1  christos 				  STRUCT_DOMAIN) < 0
    548      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN",
    549      1.1  christos 				  LABEL_DOMAIN) < 0
    550      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
    551      1.1  christos 				  VARIABLES_DOMAIN) < 0
    552      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
    553      1.1  christos 				  FUNCTIONS_DOMAIN) < 0
    554      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
    555      1.1  christos 				  TYPES_DOMAIN) < 0)
    556      1.1  christos     return -1;
    557      1.1  christos 
    558      1.1  christos   return gdb_pymodule_addobject (gdb_module, "Symbol",
    559      1.1  christos 				 (PyObject *) &symbol_object_type);
    560      1.1  christos }
    561      1.1  christos 
    562      1.1  christos 
    563      1.1  christos 
    565      1.1  christos static gdb_PyGetSetDef symbol_object_getset[] = {
    566      1.1  christos   { "type", sympy_get_type, NULL,
    567      1.1  christos     "Type of the symbol.", NULL },
    568      1.1  christos   { "symtab", sympy_get_symtab, NULL,
    569      1.1  christos     "Symbol table in which the symbol appears.", NULL },
    570      1.1  christos   { "name", sympy_get_name, NULL,
    571      1.1  christos     "Name of the symbol, as it appears in the source code.", NULL },
    572      1.1  christos   { "linkage_name", sympy_get_linkage_name, NULL,
    573      1.1  christos     "Name of the symbol, as used by the linker (i.e., may be mangled).",
    574      1.1  christos     NULL },
    575      1.1  christos   { "print_name", sympy_get_print_name, NULL,
    576      1.1  christos     "Name of the symbol in a form suitable for output.\n\
    577      1.1  christos This is either name or linkage_name, depending on whether the user asked GDB\n\
    578      1.1  christos to display demangled or mangled names.", NULL },
    579      1.1  christos   { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
    580      1.1  christos   { "is_argument", sympy_is_argument, NULL,
    581      1.1  christos     "True if the symbol is an argument of a function." },
    582      1.1  christos   { "is_constant", sympy_is_constant, NULL,
    583      1.1  christos     "True if the symbol is a constant." },
    584      1.1  christos   { "is_function", sympy_is_function, NULL,
    585      1.1  christos     "True if the symbol is a function or method." },
    586      1.1  christos   { "is_variable", sympy_is_variable, NULL,
    587      1.1  christos     "True if the symbol is a variable." },
    588      1.1  christos   { "needs_frame", sympy_needs_frame, NULL,
    589      1.1  christos     "True if the symbol requires a frame for evaluation." },
    590      1.1  christos   { "line", sympy_line, NULL,
    591      1.1  christos     "The source line number at which the symbol was defined." },
    592      1.1  christos   { NULL }  /* Sentinel */
    593      1.1  christos };
    594      1.1  christos 
    595      1.1  christos static PyMethodDef symbol_object_methods[] = {
    596      1.1  christos   { "is_valid", sympy_is_valid, METH_NOARGS,
    597      1.1  christos     "is_valid () -> Boolean.\n\
    598      1.1  christos Return true if this symbol is valid, false if not." },
    599      1.1  christos   { "value", sympy_value, METH_VARARGS,
    600      1.1  christos     "value ([frame]) -> gdb.Value\n\
    601      1.1  christos Return the value of the symbol." },
    602      1.1  christos   {NULL}  /* Sentinel */
    603      1.1  christos };
    604      1.1  christos 
    605      1.1  christos PyTypeObject symbol_object_type = {
    606      1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    607      1.1  christos   "gdb.Symbol",			  /*tp_name*/
    608      1.1  christos   sizeof (symbol_object),	  /*tp_basicsize*/
    609      1.1  christos   0,				  /*tp_itemsize*/
    610      1.1  christos   sympy_dealloc,		  /*tp_dealloc*/
    611      1.1  christos   0,				  /*tp_print*/
    612      1.1  christos   0,				  /*tp_getattr*/
    613      1.1  christos   0,				  /*tp_setattr*/
    614      1.1  christos   0,				  /*tp_compare*/
    615      1.1  christos   0,				  /*tp_repr*/
    616      1.1  christos   0,				  /*tp_as_number*/
    617      1.1  christos   0,				  /*tp_as_sequence*/
    618      1.1  christos   0,				  /*tp_as_mapping*/
    619      1.1  christos   0,				  /*tp_hash */
    620      1.1  christos   0,				  /*tp_call*/
    621      1.1  christos   sympy_str,			  /*tp_str*/
    622      1.1  christos   0,				  /*tp_getattro*/
    623      1.1  christos   0,				  /*tp_setattro*/
    624      1.1  christos   0,				  /*tp_as_buffer*/
    625      1.1  christos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
    626      1.1  christos   "GDB symbol object",		  /*tp_doc */
    627      1.1  christos   0,				  /*tp_traverse */
    628      1.1  christos   0,				  /*tp_clear */
    629      1.1  christos   0,				  /*tp_richcompare */
    630      1.1  christos   0,				  /*tp_weaklistoffset */
    631      1.1  christos   0,				  /*tp_iter */
    632      1.1  christos   0,				  /*tp_iternext */
    633      1.1  christos   symbol_object_methods,	  /*tp_methods */
    634      1.1  christos   0,				  /*tp_members */
    635      1.1  christos   symbol_object_getset		  /*tp_getset */
    636                    };
    637