]> Git Repo - binutils.git/blob - gdb/python/py-symbol.c
2011-10-20 Phil Muldoon <[email protected]>
[binutils.git] / gdb / python / py-symbol.c
1 /* Python interface to symbols.
2
3    Copyright (C) 2008, 2009, 2010, 2011 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 "defs.h"
21 #include "block.h"
22 #include "exceptions.h"
23 #include "frame.h"
24 #include "symtab.h"
25 #include "python-internal.h"
26 #include "objfiles.h"
27
28 typedef struct sympy_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   struct sympy_symbol_object *prev;
37   struct sympy_symbol_object *next;
38 } symbol_object;
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 static const struct objfile_data *sympy_objfile_data_key;
54
55 static PyObject *
56 sympy_str (PyObject *self)
57 {
58   PyObject *result;
59   struct symbol *symbol = NULL;
60
61   SYMPY_REQUIRE_VALID (self, symbol);
62
63   result = PyString_FromString (SYMBOL_PRINT_NAME (symbol));
64
65   return result;
66 }
67
68 static PyObject *
69 sympy_get_type (PyObject *self, void *closure)
70 {
71   struct symbol *symbol = NULL;
72
73   SYMPY_REQUIRE_VALID (self, symbol);
74
75   if (SYMBOL_TYPE (symbol) == NULL)
76     {
77       Py_INCREF (Py_None);
78       return Py_None;
79     }
80
81   return type_to_type_object (SYMBOL_TYPE (symbol));
82 }
83
84 static PyObject *
85 sympy_get_symtab (PyObject *self, void *closure)
86 {
87   struct symbol *symbol = NULL;
88
89   SYMPY_REQUIRE_VALID (self, symbol);
90
91   return symtab_to_symtab_object (SYMBOL_SYMTAB (symbol));
92 }
93
94 static PyObject *
95 sympy_get_name (PyObject *self, void *closure)
96 {
97   struct symbol *symbol = NULL;
98
99   SYMPY_REQUIRE_VALID (self, symbol);
100
101   return PyString_FromString (SYMBOL_NATURAL_NAME (symbol));
102 }
103
104 static PyObject *
105 sympy_get_linkage_name (PyObject *self, void *closure)
106 {
107   struct symbol *symbol = NULL;
108
109   SYMPY_REQUIRE_VALID (self, symbol);
110
111   return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol));
112 }
113
114 static PyObject *
115 sympy_get_print_name (PyObject *self, void *closure)
116 {
117   struct symbol *symbol = NULL;
118
119   SYMPY_REQUIRE_VALID (self, symbol);
120
121   return sympy_str (self);
122 }
123
124 static PyObject *
125 sympy_get_addr_class (PyObject *self, void *closure)
126 {
127   struct symbol *symbol = NULL;
128
129   SYMPY_REQUIRE_VALID (self, symbol);
130
131   return PyInt_FromLong (SYMBOL_CLASS (symbol));
132 }
133
134 static PyObject *
135 sympy_is_argument (PyObject *self, void *closure)
136 {
137   struct symbol *symbol = NULL;
138
139   SYMPY_REQUIRE_VALID (self, symbol);
140
141   return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol));
142 }
143
144 static PyObject *
145 sympy_is_constant (PyObject *self, void *closure)
146 {
147   struct symbol *symbol = NULL;
148   enum address_class class;
149
150   SYMPY_REQUIRE_VALID (self, symbol);
151
152   class = SYMBOL_CLASS (symbol);
153
154   return PyBool_FromLong (class == LOC_CONST || class == LOC_CONST_BYTES);
155 }
156
157 static PyObject *
158 sympy_is_function (PyObject *self, void *closure)
159 {
160   struct symbol *symbol = NULL;
161   enum address_class class;
162
163   SYMPY_REQUIRE_VALID (self, symbol);
164
165   class = SYMBOL_CLASS (symbol);
166
167   return PyBool_FromLong (class == LOC_BLOCK);
168 }
169
170 static PyObject *
171 sympy_is_variable (PyObject *self, void *closure)
172 {
173   struct symbol *symbol = NULL;
174   enum address_class class;
175
176   SYMPY_REQUIRE_VALID (self, symbol);
177
178   class = SYMBOL_CLASS (symbol);
179
180   return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol)
181                           && (class == LOC_LOCAL || class == LOC_REGISTER
182                               || class == LOC_STATIC || class == LOC_COMPUTED
183                               || class == LOC_OPTIMIZED_OUT));
184 }
185
186 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
187    Returns True if this Symbol still exists in GDB.  */
188
189 static PyObject *
190 sympy_is_valid (PyObject *self, PyObject *args)
191 {
192   struct symbol *symbol = NULL;
193
194   symbol = symbol_object_to_symbol (self);
195   if (symbol == NULL)
196     Py_RETURN_FALSE;
197
198   Py_RETURN_TRUE;
199 }
200
201 /* Given a symbol, and a symbol_object that has previously been
202    allocated and initialized, populate the symbol_object with the
203    struct symbol data.  Also, register the symbol_object life-cycle
204    with the life-cycle of the object file associated with this
205    symbol, if needed.  */
206 static void
207 set_symbol (symbol_object *obj, struct symbol *symbol)
208 {
209   obj->symbol = symbol;
210   obj->prev = NULL;
211   if (SYMBOL_SYMTAB (symbol))
212     {
213       obj->next = objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
214                                 sympy_objfile_data_key);
215
216       if (obj->next)
217         obj->next->prev = obj;
218       set_objfile_data (SYMBOL_SYMTAB (symbol)->objfile,
219                         sympy_objfile_data_key, obj);
220     }
221   else
222     obj->next = NULL;
223 }
224
225 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
226    symbol object from GDB.  */
227 PyObject *
228 symbol_to_symbol_object (struct symbol *sym)
229 {
230   symbol_object *sym_obj;
231
232   sym_obj = PyObject_New (symbol_object, &symbol_object_type);
233   if (sym_obj)
234     set_symbol (sym_obj, sym);
235
236   return (PyObject *) sym_obj;
237 }
238
239 /* Return the symbol that is wrapped by this symbol object.  */
240 struct symbol *
241 symbol_object_to_symbol (PyObject *obj)
242 {
243   if (! PyObject_TypeCheck (obj, &symbol_object_type))
244     return NULL;
245   return ((symbol_object *) obj)->symbol;
246 }
247
248 static void
249 sympy_dealloc (PyObject *obj)
250 {
251   symbol_object *sym_obj = (symbol_object *) obj;
252
253   if (sym_obj->prev)
254     sym_obj->prev->next = sym_obj->next;
255   else if (SYMBOL_SYMTAB (sym_obj->symbol))
256     {
257       set_objfile_data (SYMBOL_SYMTAB (sym_obj->symbol)->objfile,
258                         sympy_objfile_data_key, sym_obj->next);
259     }
260   if (sym_obj->next)
261     sym_obj->next->prev = sym_obj->prev;
262   sym_obj->symbol = NULL;
263 }
264
265 /* Implementation of
266    gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
267    A tuple with 2 elements is always returned.  The first is the symbol
268    object or None, the second is a boolean with the value of
269    is_a_field_of_this (see comment in lookup_symbol_in_language).  */
270
271 PyObject *
272 gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
273 {
274   int domain = VAR_DOMAIN, is_a_field_of_this = 0;
275   const char *name;
276   static char *keywords[] = { "name", "block", "domain", NULL };
277   struct symbol *symbol;
278   PyObject *block_obj = NULL, *ret_tuple, *sym_obj, *bool_obj;
279   const struct block *block = NULL;
280
281   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!i", keywords, &name,
282                                      &block_object_type, &block_obj, &domain))
283     return NULL;
284
285   if (block_obj)
286     block = block_object_to_block (block_obj);
287   else
288     {
289       struct frame_info *selected_frame;
290       volatile struct gdb_exception except;
291
292       TRY_CATCH (except, RETURN_MASK_ALL)
293         {
294           selected_frame = get_selected_frame (_("No frame selected."));
295           block = get_frame_block (selected_frame, NULL);
296         }
297       GDB_PY_HANDLE_EXCEPTION (except);
298     }
299
300   symbol = lookup_symbol (name, block, domain, &is_a_field_of_this);
301
302   ret_tuple = PyTuple_New (2);
303   if (!ret_tuple)
304     return NULL;
305
306   if (symbol)
307     {
308       sym_obj = symbol_to_symbol_object (symbol);
309       if (!sym_obj)
310         {
311           Py_DECREF (ret_tuple);
312           return NULL;
313         }
314     }
315   else
316     {
317       sym_obj = Py_None;
318       Py_INCREF (Py_None);
319     }
320   PyTuple_SET_ITEM (ret_tuple, 0, sym_obj);
321
322   bool_obj = is_a_field_of_this? Py_True : Py_False;
323   Py_INCREF (bool_obj);
324   PyTuple_SET_ITEM (ret_tuple, 1, bool_obj);
325
326   return ret_tuple;
327 }
328
329 /* Implementation of
330    gdb.lookup_global_symbol (name [, domain]) -> symbol or None.  */
331
332 PyObject *
333 gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
334 {
335   int domain = VAR_DOMAIN;
336   const char *name;
337   static char *keywords[] = { "name", "domain", NULL };
338   struct symbol *symbol;
339   PyObject *sym_obj;
340
341   if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name,
342                                      &domain))
343     return NULL;
344
345   symbol = lookup_symbol_global (name, NULL, domain);
346
347   if (symbol)
348     {
349       sym_obj = symbol_to_symbol_object (symbol);
350       if (!sym_obj)
351         return NULL;
352     }
353   else
354     {
355       sym_obj = Py_None;
356       Py_INCREF (Py_None);
357     }
358
359   return sym_obj;
360 }
361
362 /* This function is called when an objfile is about to be freed.
363    Invalidate the symbol as further actions on the symbol would result
364    in bad data.  All access to obj->symbol should be gated by
365    SYMPY_REQUIRE_VALID which will raise an exception on invalid
366    symbols.  */
367 static void
368 del_objfile_symbols (struct objfile *objfile, void *datum)
369 {
370   symbol_object *obj = datum;
371   while (obj)
372     {
373       symbol_object *next = obj->next;
374
375       obj->symbol = NULL;
376       obj->next = NULL;
377       obj->prev = NULL;
378
379       obj = next;
380     }
381 }
382
383 void
384 gdbpy_initialize_symbols (void)
385 {
386   if (PyType_Ready (&symbol_object_type) < 0)
387     return;
388
389   /* Register an objfile "free" callback so we can properly
390      invalidate symbol when an object file that is about to be
391      deleted.  */
392   sympy_objfile_data_key
393     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
394
395   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF);
396   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST", LOC_CONST);
397   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_STATIC", LOC_STATIC);
398   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGISTER", LOC_REGISTER);
399   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_ARG", LOC_ARG);
400   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG);
401   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LOCAL", LOC_LOCAL);
402   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF);
403   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_LABEL", LOC_LABEL);
404   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_BLOCK", LOC_BLOCK);
405   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST_BYTES",
406                            LOC_CONST_BYTES);
407   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNRESOLVED",
408                            LOC_UNRESOLVED);
409   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_OPTIMIZED_OUT",
410                            LOC_OPTIMIZED_OUT);
411   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED);
412   PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_REGPARM_ADDR",
413                            LOC_REGPARM_ADDR);
414   PyModule_AddIntConstant (gdb_module, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN);
415   PyModule_AddIntConstant (gdb_module, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN);
416   PyModule_AddIntConstant (gdb_module, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN);
417   PyModule_AddIntConstant (gdb_module, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN);
418   PyModule_AddIntConstant (gdb_module, "SYMBOL_VARIABLES_DOMAIN",
419                            VARIABLES_DOMAIN);
420   PyModule_AddIntConstant (gdb_module, "SYMBOL_FUNCTIONS_DOMAIN",
421                            FUNCTIONS_DOMAIN);
422   PyModule_AddIntConstant (gdb_module, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN);
423
424   Py_INCREF (&symbol_object_type);
425   PyModule_AddObject (gdb_module, "Symbol", (PyObject *) &symbol_object_type);
426 }
427
428 \f
429
430 static PyGetSetDef symbol_object_getset[] = {
431   { "type", sympy_get_type, NULL,
432     "Type of the symbol.", NULL },
433   { "symtab", sympy_get_symtab, NULL,
434     "Symbol table in which the symbol appears.", NULL },
435   { "name", sympy_get_name, NULL,
436     "Name of the symbol, as it appears in the source code.", NULL },
437   { "linkage_name", sympy_get_linkage_name, NULL,
438     "Name of the symbol, as used by the linker (i.e., may be mangled).",
439     NULL },
440   { "print_name", sympy_get_print_name, NULL,
441     "Name of the symbol in a form suitable for output.\n\
442 This is either name or linkage_name, depending on whether the user asked GDB\n\
443 to display demangled or mangled names.", NULL },
444   { "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
445   { "is_argument", sympy_is_argument, NULL,
446     "True if the symbol is an argument of a function." },
447   { "is_constant", sympy_is_constant, NULL,
448     "True if the symbol is a constant." },
449   { "is_function", sympy_is_function, NULL,
450     "True if the symbol is a function or method." },
451   { "is_variable", sympy_is_variable, NULL,
452     "True if the symbol is a variable." },
453   { NULL }  /* Sentinel */
454 };
455
456 static PyMethodDef symbol_object_methods[] = {
457   { "is_valid", sympy_is_valid, METH_NOARGS,
458     "is_valid () -> Boolean.\n\
459 Return true if this symbol is valid, false if not." },
460   {NULL}  /* Sentinel */
461 };
462
463 PyTypeObject symbol_object_type = {
464   PyObject_HEAD_INIT (NULL)
465   0,                              /*ob_size*/
466   "gdb.Symbol",                   /*tp_name*/
467   sizeof (symbol_object),         /*tp_basicsize*/
468   0,                              /*tp_itemsize*/
469   sympy_dealloc,                  /*tp_dealloc*/
470   0,                              /*tp_print*/
471   0,                              /*tp_getattr*/
472   0,                              /*tp_setattr*/
473   0,                              /*tp_compare*/
474   0,                              /*tp_repr*/
475   0,                              /*tp_as_number*/
476   0,                              /*tp_as_sequence*/
477   0,                              /*tp_as_mapping*/
478   0,                              /*tp_hash */
479   0,                              /*tp_call*/
480   sympy_str,                      /*tp_str*/
481   0,                              /*tp_getattro*/
482   0,                              /*tp_setattro*/
483   0,                              /*tp_as_buffer*/
484   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
485   "GDB symbol object",            /*tp_doc */
486   0,                              /*tp_traverse */
487   0,                              /*tp_clear */
488   0,                              /*tp_richcompare */
489   0,                              /*tp_weaklistoffset */
490   0,                              /*tp_iter */
491   0,                              /*tp_iternext */
492   symbol_object_methods,          /*tp_methods */
493   0,                              /*tp_members */
494   symbol_object_getset            /*tp_getset */
495 };
This page took 0.053221 seconds and 4 git commands to generate.