Home | History | Annotate | Line # | Download | only in python
py-symbol.c revision 1.1.1.2
      1      1.1  christos /* Python interface to symbols.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 2008-2015 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  christos 
     27      1.1  christos typedef struct sympy_symbol_object {
     28      1.1  christos   PyObject_HEAD
     29      1.1  christos   /* The GDB symbol structure this object is wrapping.  */
     30      1.1  christos   struct symbol *symbol;
     31      1.1  christos   /* A symbol object is associated with an objfile, so keep track with
     32      1.1  christos      doubly-linked list, rooted in the objfile.  This lets us
     33      1.1  christos      invalidate the underlying struct symbol when the objfile is
     34      1.1  christos      deleted.  */
     35      1.1  christos   struct sympy_symbol_object *prev;
     36      1.1  christos   struct sympy_symbol_object *next;
     37      1.1  christos } symbol_object;
     38      1.1  christos 
     39      1.1  christos /* Require a valid symbol.  All access to symbol_object->symbol should be
     40      1.1  christos    gated by this call.  */
     41      1.1  christos #define SYMPY_REQUIRE_VALID(symbol_obj, symbol)		\
     42      1.1  christos   do {							\
     43      1.1  christos     symbol = symbol_object_to_symbol (symbol_obj);	\
     44      1.1  christos     if (symbol == NULL)					\
     45      1.1  christos       {							\
     46      1.1  christos 	PyErr_SetString (PyExc_RuntimeError,		\
     47      1.1  christos 			 _("Symbol is invalid."));	\
     48      1.1  christos 	return NULL;					\
     49      1.1  christos       }							\
     50      1.1  christos   } while (0)
     51      1.1  christos 
     52      1.1  christos static const struct objfile_data *sympy_objfile_data_key;
     53      1.1  christos 
     54      1.1  christos static PyObject *
     55      1.1  christos sympy_str (PyObject *self)
     56      1.1  christos {
     57      1.1  christos   PyObject *result;
     58      1.1  christos   struct symbol *symbol = NULL;
     59      1.1  christos 
     60      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     61      1.1  christos 
     62      1.1  christos   result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
     63      1.1  christos 
     64      1.1  christos   return result;
     65      1.1  christos }
     66      1.1  christos 
     67      1.1  christos static PyObject *
     68      1.1  christos sympy_get_type (PyObject *self, void *closure)
     69      1.1  christos {
     70      1.1  christos   struct symbol *symbol = NULL;
     71      1.1  christos 
     72      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     73      1.1  christos 
     74      1.1  christos   if (SYMBOL_TYPE (symbol) == NULL)
     75      1.1  christos     {
     76      1.1  christos       Py_INCREF (Py_None);
     77      1.1  christos       return Py_None;
     78      1.1  christos     }
     79      1.1  christos 
     80      1.1  christos   return type_to_type_object (SYMBOL_TYPE (symbol));
     81      1.1  christos }
     82      1.1  christos 
     83      1.1  christos static PyObject *
     84      1.1  christos sympy_get_symtab (PyObject *self, void *closure)
     85      1.1  christos {
     86      1.1  christos   struct symbol *symbol = NULL;
     87      1.1  christos 
     88      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
     89      1.1  christos 
     90  1.1.1.2  christos   if (!SYMBOL_OBJFILE_OWNED (symbol))
     91  1.1.1.2  christos     Py_RETURN_NONE;
     92  1.1.1.2  christos 
     93  1.1.1.2  christos   return symtab_to_symtab_object (symbol_symtab (symbol));
     94      1.1  christos }
     95      1.1  christos 
     96      1.1  christos static PyObject *
     97      1.1  christos sympy_get_name (PyObject *self, void *closure)
     98      1.1  christos {
     99      1.1  christos   struct symbol *symbol = NULL;
    100      1.1  christos 
    101      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    102      1.1  christos 
    103      1.1  christos   return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
    104      1.1  christos }
    105      1.1  christos 
    106      1.1  christos static PyObject *
    107      1.1  christos sympy_get_linkage_name (PyObject *self, void *closure)
    108      1.1  christos {
    109      1.1  christos   struct symbol *symbol = NULL;
    110      1.1  christos 
    111      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    112      1.1  christos 
    113      1.1  christos   return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
    114      1.1  christos }
    115      1.1  christos 
    116      1.1  christos static PyObject *
    117      1.1  christos sympy_get_print_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.1  christos   return sympy_str (self);
    124      1.1  christos }
    125      1.1  christos 
    126      1.1  christos static PyObject *
    127      1.1  christos sympy_get_addr_class (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.1  christos   return PyInt_FromLong (SYMBOL_CLASS (symbol));
    134      1.1  christos }
    135      1.1  christos 
    136      1.1  christos static PyObject *
    137      1.1  christos sympy_is_argument (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 PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
    144      1.1  christos }
    145      1.1  christos 
    146      1.1  christos static PyObject *
    147      1.1  christos sympy_is_constant (PyObject *self, void *closure)
    148      1.1  christos {
    149      1.1  christos   struct symbol *symbol = NULL;
    150      1.1  christos   enum address_class class;
    151      1.1  christos 
    152      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    153      1.1  christos 
    154      1.1  christos   class = SYMBOL_CLASS (symbol);
    155      1.1  christos 
    156      1.1  christos   return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
    157      1.1  christos }
    158      1.1  christos 
    159      1.1  christos static PyObject *
    160      1.1  christos sympy_is_function (PyObject *self, void *closure)
    161      1.1  christos {
    162      1.1  christos   struct symbol *symbol = NULL;
    163      1.1  christos   enum address_class class;
    164      1.1  christos 
    165      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    166      1.1  christos 
    167      1.1  christos   class = SYMBOL_CLASS (symbol);
    168      1.1  christos 
    169      1.1  christos   return PyBool_FromLong (class == LOC_BLOCK);
    170      1.1  christos }
    171      1.1  christos 
    172      1.1  christos static PyObject *
    173      1.1  christos sympy_is_variable (PyObject *self, void *closure)
    174      1.1  christos {
    175      1.1  christos   struct symbol *symbol = NULL;
    176      1.1  christos   enum address_class class;
    177      1.1  christos 
    178      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    179      1.1  christos 
    180      1.1  christos   class = SYMBOL_CLASS (symbol);
    181      1.1  christos 
    182      1.1  christos   return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
    183      1.1  christos 			  && (class == LOC_LOCAL || class == LOC_REGISTER
    184      1.1  christos 			      || class == LOC_STATIC || class == LOC_COMPUTED
    185      1.1  christos 			      || class == LOC_OPTIMIZED_OUT));
    186      1.1  christos }
    187      1.1  christos 
    188      1.1  christos /* Implementation of gdb.Symbol.needs_frame -> Boolean.
    189      1.1  christos    Returns true iff the symbol needs a frame for evaluation.  */
    190      1.1  christos 
    191      1.1  christos static PyObject *
    192      1.1  christos sympy_needs_frame (PyObject *self, void *closure)
    193      1.1  christos {
    194      1.1  christos   struct symbol *symbol = NULL;
    195      1.1  christos   volatile struct gdb_exception except;
    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  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    201      1.1  christos     {
    202      1.1  christos       result = symbol_read_needs_frame (symbol);
    203      1.1  christos     }
    204      1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    205      1.1  christos 
    206      1.1  christos   if (result)
    207      1.1  christos     Py_RETURN_TRUE;
    208      1.1  christos   Py_RETURN_FALSE;
    209      1.1  christos }
    210      1.1  christos 
    211      1.1  christos /* Implementation of gdb.Symbol.line -> int.
    212      1.1  christos    Returns the line number at which the symbol was defined.  */
    213      1.1  christos 
    214      1.1  christos static PyObject *
    215      1.1  christos sympy_line (PyObject *self, void *closure)
    216      1.1  christos {
    217      1.1  christos   struct symbol *symbol = NULL;
    218      1.1  christos 
    219      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    220      1.1  christos 
    221      1.1  christos   return PyInt_FromLong (SYMBOL_LINE (symbol));
    222      1.1  christos }
    223      1.1  christos 
    224      1.1  christos /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
    225      1.1  christos    Returns True if this Symbol still exists in GDB.  */
    226      1.1  christos 
    227      1.1  christos static PyObject *
    228      1.1  christos sympy_is_valid (PyObject *self, PyObject *args)
    229      1.1  christos {
    230      1.1  christos   struct symbol *symbol = NULL;
    231      1.1  christos 
    232      1.1  christos   symbol = symbol_object_to_symbol (self);
    233      1.1  christos   if (symbol == NULL)
    234      1.1  christos     Py_RETURN_FALSE;
    235      1.1  christos 
    236      1.1  christos   Py_RETURN_TRUE;
    237      1.1  christos }
    238      1.1  christos 
    239      1.1  christos /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value.  Returns
    240      1.1  christos    the value of the symbol, or an error in various circumstances.  */
    241      1.1  christos 
    242      1.1  christos static PyObject *
    243      1.1  christos sympy_value (PyObject *self, PyObject *args)
    244      1.1  christos {
    245      1.1  christos   struct symbol *symbol = NULL;
    246      1.1  christos   struct frame_info *frame_info = NULL;
    247      1.1  christos   PyObject *frame_obj = NULL;
    248      1.1  christos   struct value *value = NULL;
    249      1.1  christos   volatile struct gdb_exception except;
    250      1.1  christos 
    251      1.1  christos   if (!PyArg_ParseTuple (args, "|O", &frame_obj))
    252      1.1  christos     return NULL;
    253      1.1  christos 
    254      1.1  christos   if (frame_obj != NULL && !PyObject_TypeCheck (frame_obj, &frame_object_type))
    255      1.1  christos     {
    256      1.1  christos       PyErr_SetString (PyExc_TypeError, "argument is not a frame");
    257      1.1  christos       return NULL;
    258      1.1  christos     }
    259      1.1  christos 
    260      1.1  christos   SYMPY_REQUIRE_VALID (self, symbol);
    261      1.1  christos   if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
    262      1.1  christos     {
    263      1.1  christos       PyErr_SetString (PyExc_TypeError, "cannot get the value of a typedef");
    264      1.1  christos       return NULL;
    265      1.1  christos     }
    266      1.1  christos 
    267      1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    268      1.1  christos     {
    269      1.1  christos       if (frame_obj != NULL)
    270      1.1  christos 	{
    271      1.1  christos 	  frame_info = frame_object_to_frame_info (frame_obj);
    272      1.1  christos 	  if (frame_info == NULL)
    273      1.1  christos 	    error (_("invalid frame"));
    274      1.1  christos 	}
    275      1.1  christos 
    276      1.1  christos       if (symbol_read_needs_frame (symbol) && frame_info == NULL)
    277      1.1  christos 	error (_("symbol requires a frame to compute its value"));
    278      1.1  christos 
    279      1.1  christos       value = read_var_value (symbol, frame_info);
    280      1.1  christos     }
    281      1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    282      1.1  christos 
    283      1.1  christos   return value_to_value_object (value);
    284      1.1  christos }
    285      1.1  christos 
    286      1.1  christos /* Given a symbol, and a symbol_object that has previously been
    287      1.1  christos    allocated and initialized, populate the symbol_object with the
    288      1.1  christos    struct symbol data.  Also, register the symbol_object life-cycle
    289      1.1  christos    with the life-cycle of the object file associated with this
    290      1.1  christos    symbol, if needed.  */
    291      1.1  christos static void
    292      1.1  christos set_symbol (symbol_object *obj, struct symbol *symbol)
    293      1.1  christos {
    294      1.1  christos   obj->symbol = symbol;
    295      1.1  christos   obj->prev = NULL;
    296  1.1.1.2  christos   if (SYMBOL_OBJFILE_OWNED (symbol)
    297  1.1.1.2  christos       && symbol_symtab (symbol) != NULL)
    298      1.1  christos     {
    299  1.1.1.2  christos       struct objfile *objfile = symbol_objfile (symbol);
    300      1.1  christos 
    301  1.1.1.2  christos       obj->next = objfile_data (objfile, sympy_objfile_data_key);
    302      1.1  christos       if (obj->next)
    303      1.1  christos 	obj->next->prev = obj;
    304  1.1.1.2  christos       set_objfile_data (objfile, sympy_objfile_data_key, obj);
    305      1.1  christos     }
    306      1.1  christos   else
    307      1.1  christos     obj->next = NULL;
    308      1.1  christos }
    309      1.1  christos 
    310      1.1  christos /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
    311      1.1  christos    symbol object from GDB.  */
    312      1.1  christos PyObject *
    313      1.1  christos symbol_to_symbol_object (struct symbol *sym)
    314      1.1  christos {
    315      1.1  christos   symbol_object *sym_obj;
    316      1.1  christos 
    317      1.1  christos   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
    318      1.1  christos   if (sym_obj)
    319      1.1  christos     set_symbol (sym_obj, sym);
    320      1.1  christos 
    321      1.1  christos   return (PyObject *) sym_obj;
    322      1.1  christos }
    323      1.1  christos 
    324      1.1  christos /* Return the symbol that is wrapped by this symbol object.  */
    325      1.1  christos struct symbol *
    326      1.1  christos symbol_object_to_symbol (PyObject *obj)
    327      1.1  christos {
    328      1.1  christos   if (! PyObject_TypeCheck (obj, &symbol_object_type))
    329      1.1  christos     return NULL;
    330      1.1  christos   return ((symbol_object *) obj)->symbol;
    331      1.1  christos }
    332      1.1  christos 
    333      1.1  christos static void
    334      1.1  christos sympy_dealloc (PyObject *obj)
    335      1.1  christos {
    336      1.1  christos   symbol_object *sym_obj = (symbol_object *) obj;
    337      1.1  christos 
    338      1.1  christos   if (sym_obj->prev)
    339      1.1  christos     sym_obj->prev->next = sym_obj->next;
    340  1.1.1.2  christos   else if (sym_obj->symbol != NULL
    341  1.1.1.2  christos 	   && SYMBOL_OBJFILE_OWNED (sym_obj->symbol)
    342  1.1.1.2  christos 	   && symbol_symtab (sym_obj->symbol) != NULL)
    343      1.1  christos     {
    344  1.1.1.2  christos       set_objfile_data (symbol_objfile (sym_obj->symbol),
    345      1.1  christos 			sympy_objfile_data_key, sym_obj->next);
    346      1.1  christos     }
    347      1.1  christos   if (sym_obj->next)
    348      1.1  christos     sym_obj->next->prev = sym_obj->prev;
    349      1.1  christos   sym_obj->symbol = NULL;
    350      1.1  christos }
    351      1.1  christos 
    352      1.1  christos /* Implementation of
    353      1.1  christos    gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
    354      1.1  christos    A tuple with 2 elements is always returned.  The first is the symbol
    355      1.1  christos    object or None, the second is a boolean with the value of
    356      1.1  christos    is_a_field_of_this (see comment in lookup_symbol_in_language).  */
    357      1.1  christos 
    358      1.1  christos PyObject *
    359      1.1  christos gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
    360      1.1  christos {
    361      1.1  christos   int domain = VAR_DOMAIN;
    362      1.1  christos   struct field_of_this_result is_a_field_of_this;
    363      1.1  christos   const char *name;
    364      1.1  christos   static char *keywords[] = { "name", "block", "domain", NULL };
    365      1.1  christos   struct symbol *symbol = NULL;
    366      1.1  christos   PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
    367      1.1  christos   const struct block *block = NULL;
    368      1.1  christos   volatile struct gdb_exception except;
    369      1.1  christos 
    370      1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
    371      1.1  christos 				     &block_object_type, &block_obj, &domain))
    372      1.1  christos     return NULL;
    373      1.1  christos 
    374      1.1  christos   if (block_obj)
    375      1.1  christos     block = block_object_to_block (block_obj);
    376      1.1  christos   else
    377      1.1  christos     {
    378      1.1  christos       struct frame_info *selected_frame;
    379      1.1  christos       volatile struct gdb_exception except;
    380      1.1  christos 
    381      1.1  christos       TRY_CATCH (except, RETURN_MASK_ALL)
    382      1.1  christos 	{
    383      1.1  christos 	  selected_frame = get_selected_frame (_("No frame selected."));
    384      1.1  christos 	  block = get_frame_block (selected_frame, NULL);
    385      1.1  christos 	}
    386      1.1  christos       GDB_PY_HANDLE_EXCEPTION (except);
    387      1.1  christos     }
    388      1.1  christos 
    389      1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    390      1.1  christos     {
    391      1.1  christos       symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
    392      1.1  christos     }
    393      1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    394      1.1  christos 
    395      1.1  christos   ret_tuple = PyTuple_New (2);
    396      1.1  christos   if (!ret_tuple)
    397      1.1  christos     return NULL;
    398      1.1  christos 
    399      1.1  christos   if (symbol)
    400      1.1  christos     {
    401      1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    402      1.1  christos       if (!sym_obj)
    403      1.1  christos 	{
    404      1.1  christos 	  Py_DECREF (ret_tuple);
    405      1.1  christos 	  return NULL;
    406      1.1  christos 	}
    407      1.1  christos     }
    408      1.1  christos   else
    409      1.1  christos     {
    410      1.1  christos       sym_obj = Py_None;
    411      1.1  christos       Py_INCREF (Py_None);
    412      1.1  christos     }
    413      1.1  christos   PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);
    414      1.1  christos 
    415      1.1  christos   bool_obj = (is_a_field_of_this.type != NULL) ? Py_True : Py_False;
    416      1.1  christos   Py_INCREF (bool_obj);
    417      1.1  christos   PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);
    418      1.1  christos 
    419      1.1  christos   return ret_tuple;
    420      1.1  christos }
    421      1.1  christos 
    422      1.1  christos /* Implementation of
    423      1.1  christos    gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */
    424      1.1  christos 
    425      1.1  christos PyObject *
    426      1.1  christos gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
    427      1.1  christos {
    428      1.1  christos   int domain = VAR_DOMAIN;
    429      1.1  christos   const char *name;
    430      1.1  christos   static char *keywords[] = { "name", "domain", NULL };
    431      1.1  christos   struct symbol *symbol = NULL;
    432      1.1  christos   PyObject *sym_obj;
    433      1.1  christos   volatile struct gdb_exception except;
    434      1.1  christos 
    435      1.1  christos   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
    436      1.1  christos 				     &domain))
    437      1.1  christos     return NULL;
    438      1.1  christos 
    439      1.1  christos   TRY_CATCH (except, RETURN_MASK_ALL)
    440      1.1  christos     {
    441  1.1.1.2  christos       symbol = lookup_global_symbol (name, NULL, domain);
    442      1.1  christos     }
    443      1.1  christos   GDB_PY_HANDLE_EXCEPTION (except);
    444      1.1  christos 
    445      1.1  christos   if (symbol)
    446      1.1  christos     {
    447      1.1  christos       sym_obj = symbol_to_symbol_object (symbol);
    448      1.1  christos       if (!sym_obj)
    449      1.1  christos 	return NULL;
    450      1.1  christos     }
    451      1.1  christos   else
    452      1.1  christos     {
    453      1.1  christos       sym_obj = Py_None;
    454      1.1  christos       Py_INCREF (Py_None);
    455      1.1  christos     }
    456      1.1  christos 
    457      1.1  christos   return sym_obj;
    458      1.1  christos }
    459      1.1  christos 
    460      1.1  christos /* This function is called when an objfile is about to be freed.
    461      1.1  christos    Invalidate the symbol as further actions on the symbol would result
    462      1.1  christos    in bad data.  All access to obj->symbol should be gated by
    463      1.1  christos    SYMPY_REQUIRE_VALID which will raise an exception on invalid
    464      1.1  christos    symbols.  */
    465      1.1  christos static void
    466      1.1  christos del_objfile_symbols (struct objfile *objfile, void *datum)
    467      1.1  christos {
    468      1.1  christos   symbol_object *obj = datum;
    469      1.1  christos   while (obj)
    470      1.1  christos     {
    471      1.1  christos       symbol_object *next = obj->next;
    472      1.1  christos 
    473      1.1  christos       obj->symbol = NULL;
    474      1.1  christos       obj->next = NULL;
    475      1.1  christos       obj->prev = NULL;
    476      1.1  christos 
    477      1.1  christos       obj = next;
    478      1.1  christos     }
    479      1.1  christos }
    480      1.1  christos 
    481      1.1  christos int
    482      1.1  christos gdbpy_initialize_symbols (void)
    483      1.1  christos {
    484      1.1  christos   if (PyType_Ready (&symbol_object_type) < 0)
    485      1.1  christos     return -1;
    486      1.1  christos 
    487      1.1  christos   /* Register an objfile "free" callback so we can properly
    488      1.1  christos      invalidate symbol when an object file that is about to be
    489      1.1  christos      deleted.  */
    490      1.1  christos   sympy_objfile_data_key
    491      1.1  christos     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
    492      1.1  christos 
    493      1.1  christos   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
    494      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
    495      1.1  christos 				  LOC_CONST) < 0
    496      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC",
    497      1.1  christos 				  LOC_STATIC) < 0
    498      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER",
    499      1.1  christos 				  LOC_REGISTER) < 0
    500      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG",
    501      1.1  christos 				  LOC_ARG) < 0
    502      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG",
    503      1.1  christos 				  LOC_REF_ARG) < 0
    504      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL",
    505      1.1  christos 				  LOC_LOCAL) < 0
    506      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF",
    507      1.1  christos 				  LOC_TYPEDEF) < 0
    508      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL",
    509      1.1  christos 				  LOC_LABEL) < 0
    510      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK",
    511      1.1  christos 				  LOC_BLOCK) < 0
    512      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
    513      1.1  christos 				  LOC_CONST_BYTES) < 0
    514      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
    515      1.1  christos 				  LOC_UNRESOLVED) < 0
    516      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
    517      1.1  christos 				  LOC_OPTIMIZED_OUT) < 0
    518      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED",
    519      1.1  christos 				  LOC_COMPUTED) < 0
    520      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
    521      1.1  christos 				  LOC_REGPARM_ADDR) < 0
    522      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN",
    523      1.1  christos 				  UNDEF_DOMAIN) < 0
    524      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN",
    525      1.1  christos 				  VAR_DOMAIN) < 0
    526      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN",
    527      1.1  christos 				  STRUCT_DOMAIN) < 0
    528      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN",
    529      1.1  christos 				  LABEL_DOMAIN) < 0
    530      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
    531      1.1  christos 				  VARIABLES_DOMAIN) < 0
    532      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
    533      1.1  christos 				  FUNCTIONS_DOMAIN) < 0
    534      1.1  christos       || PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN",
    535      1.1  christos 				  TYPES_DOMAIN) < 0)
    536      1.1  christos     return -1;
    537      1.1  christos 
    538      1.1  christos   return gdb_pymodule_addobject (gdb_module, "Symbol",
    539      1.1  christos 				 (PyObject *) &symbol_object_type);
    540      1.1  christos }
    541      1.1  christos 
    542      1.1  christos 
    543      1.1  christos 
    545      1.1  christos static PyGetSetDef symbol_object_getset[] = {
    546      1.1  christos   { "type", sympy_get_type, NULL,
    547      1.1  christos     "Type of the symbol.", NULL },
    548      1.1  christos   { "symtab", sympy_get_symtab, NULL,
    549      1.1  christos     "Symbol table in which the symbol appears.", NULL },
    550      1.1  christos   { "name", sympy_get_name, NULL,
    551      1.1  christos     "Name of the symbol, as it appears in the source code.", NULL },
    552      1.1  christos   { "linkage_name", sympy_get_linkage_name, NULL,
    553      1.1  christos     "Name of the symbol, as used by the linker (i.e., may be mangled).",
    554      1.1  christos     NULL },
    555      1.1  christos   { "print_name", sympy_get_print_name, NULL,
    556      1.1  christos     "Name of the symbol in a form suitable for output.\n\
    557      1.1  christos This is either name or linkage_name, depending on whether the user asked GDB\n\
    558      1.1  christos to display demangled or mangled names.", NULL },
    559      1.1  christos   { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
    560      1.1  christos   { "is_argument", sympy_is_argument, NULL,
    561      1.1  christos     "True if the symbol is an argument of a function." },
    562      1.1  christos   { "is_constant", sympy_is_constant, NULL,
    563      1.1  christos     "True if the symbol is a constant." },
    564      1.1  christos   { "is_function", sympy_is_function, NULL,
    565      1.1  christos     "True if the symbol is a function or method." },
    566      1.1  christos   { "is_variable", sympy_is_variable, NULL,
    567      1.1  christos     "True if the symbol is a variable." },
    568      1.1  christos   { "needs_frame", sympy_needs_frame, NULL,
    569      1.1  christos     "True if the symbol requires a frame for evaluation." },
    570      1.1  christos   { "line", sympy_line, NULL,
    571      1.1  christos     "The source line number at which the symbol was defined." },
    572      1.1  christos   { NULL }  /* Sentinel */
    573      1.1  christos };
    574      1.1  christos 
    575      1.1  christos static PyMethodDef symbol_object_methods[] = {
    576      1.1  christos   { "is_valid", sympy_is_valid, METH_NOARGS,
    577      1.1  christos     "is_valid () -> Boolean.\n\
    578      1.1  christos Return true if this symbol is valid, false if not." },
    579      1.1  christos   { "value", sympy_value, METH_VARARGS,
    580      1.1  christos     "value ([frame]) -> gdb.Value\n\
    581      1.1  christos Return the value of the symbol." },
    582      1.1  christos   {NULL}  /* Sentinel */
    583      1.1  christos };
    584      1.1  christos 
    585      1.1  christos PyTypeObject symbol_object_type = {
    586      1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
    587      1.1  christos   "gdb.Symbol",			  /*tp_name*/
    588      1.1  christos   sizeof (symbol_object),	  /*tp_basicsize*/
    589      1.1  christos   0,				  /*tp_itemsize*/
    590      1.1  christos   sympy_dealloc,		  /*tp_dealloc*/
    591      1.1  christos   0,				  /*tp_print*/
    592      1.1  christos   0,				  /*tp_getattr*/
    593      1.1  christos   0,				  /*tp_setattr*/
    594      1.1  christos   0,				  /*tp_compare*/
    595      1.1  christos   0,				  /*tp_repr*/
    596      1.1  christos   0,				  /*tp_as_number*/
    597      1.1  christos   0,				  /*tp_as_sequence*/
    598      1.1  christos   0,				  /*tp_as_mapping*/
    599      1.1  christos   0,				  /*tp_hash */
    600      1.1  christos   0,				  /*tp_call*/
    601      1.1  christos   sympy_str,			  /*tp_str*/
    602      1.1  christos   0,				  /*tp_getattro*/
    603      1.1  christos   0,				  /*tp_setattro*/
    604      1.1  christos   0,				  /*tp_as_buffer*/
    605      1.1  christos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
    606      1.1  christos   "GDB symbol object",		  /*tp_doc */
    607      1.1  christos   0,				  /*tp_traverse */
    608      1.1  christos   0,				  /*tp_clear */
    609      1.1  christos   0,				  /*tp_richcompare */
    610      1.1  christos   0,				  /*tp_weaklistoffset */
    611      1.1  christos   0,				  /*tp_iter */
    612      1.1  christos   0,				  /*tp_iternext */
    613      1.1  christos   symbol_object_methods,	  /*tp_methods */
    614      1.1  christos   0,				  /*tp_members */
    615      1.1  christos   symbol_object_getset		  /*tp_getset */
    616                    };
    617