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