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