1 /* Python interface to symbols.
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "exceptions.h"
25 #include "python-internal.h"
28 typedef struct sympy_symbol_object
{
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
36 struct sympy_symbol_object
*prev
;
37 struct sympy_symbol_object
*next
;
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) \
44 symbol = symbol_object_to_symbol (symbol_obj); \
47 PyErr_SetString (PyExc_RuntimeError, \
48 _("Symbol is invalid.")); \
53 static const struct objfile_data
*sympy_objfile_data_key
;
56 sympy_str (PyObject
*self
)
59 struct symbol
*symbol
= NULL
;
61 SYMPY_REQUIRE_VALID (self
, symbol
);
63 result
= PyString_FromString (SYMBOL_PRINT_NAME (symbol
));
69 sympy_get_symtab (PyObject
*self
, void *closure
)
71 struct symbol
*symbol
= NULL
;
73 SYMPY_REQUIRE_VALID (self
, symbol
);
75 return symtab_to_symtab_object (SYMBOL_SYMTAB (symbol
));
79 sympy_get_name (PyObject
*self
, void *closure
)
81 struct symbol
*symbol
= NULL
;
83 SYMPY_REQUIRE_VALID (self
, symbol
);
85 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol
));
89 sympy_get_linkage_name (PyObject
*self
, void *closure
)
91 struct symbol
*symbol
= NULL
;
93 SYMPY_REQUIRE_VALID (self
, symbol
);
95 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol
));
99 sympy_get_print_name (PyObject
*self
, void *closure
)
101 struct symbol
*symbol
= NULL
;
103 SYMPY_REQUIRE_VALID (self
, symbol
);
105 return sympy_str (self
);
109 sympy_get_addr_class (PyObject
*self
, void *closure
)
111 struct symbol
*symbol
= NULL
;
113 SYMPY_REQUIRE_VALID (self
, symbol
);
115 return PyInt_FromLong (SYMBOL_CLASS (symbol
));
119 sympy_is_argument (PyObject
*self
, void *closure
)
121 struct symbol
*symbol
= NULL
;
123 SYMPY_REQUIRE_VALID (self
, symbol
);
125 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol
));
129 sympy_is_constant (PyObject
*self
, void *closure
)
131 struct symbol
*symbol
= NULL
;
132 enum address_class
class;
134 SYMPY_REQUIRE_VALID (self
, symbol
);
136 class = SYMBOL_CLASS (symbol
);
138 return PyBool_FromLong (class == LOC_CONST
|| class == LOC_CONST_BYTES
);
142 sympy_is_function (PyObject
*self
, void *closure
)
144 struct symbol
*symbol
= NULL
;
145 enum address_class
class;
147 SYMPY_REQUIRE_VALID (self
, symbol
);
149 class = SYMBOL_CLASS (symbol
);
151 return PyBool_FromLong (class == LOC_BLOCK
);
155 sympy_is_variable (PyObject
*self
, void *closure
)
157 struct symbol
*symbol
= NULL
;
158 enum address_class
class;
160 SYMPY_REQUIRE_VALID (self
, symbol
);
162 class = SYMBOL_CLASS (symbol
);
164 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol
)
165 && (class == LOC_LOCAL
|| class == LOC_REGISTER
166 || class == LOC_STATIC
|| class == LOC_COMPUTED
167 || class == LOC_OPTIMIZED_OUT
));
170 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
171 Returns True if this Symbol still exists in GDB. */
174 sympy_is_valid (PyObject
*self
, PyObject
*args
)
176 struct symbol
*symbol
= NULL
;
178 symbol
= symbol_object_to_symbol (self
);
185 /* Given a symbol, and a symbol_object that has previously been
186 allocated and initialized, populate the symbol_object with the
187 struct symbol data. Also, register the symbol_object life-cycle
188 with the life-cycle of the object file associated with this
189 symbol, if needed. */
191 set_symbol (symbol_object
*obj
, struct symbol
*symbol
)
193 obj
->symbol
= symbol
;
195 if (SYMBOL_SYMTAB (symbol
))
197 obj
->next
= objfile_data (SYMBOL_SYMTAB (symbol
)->objfile
,
198 sympy_objfile_data_key
);
201 obj
->next
->prev
= obj
;
202 set_objfile_data (SYMBOL_SYMTAB (symbol
)->objfile
,
203 sympy_objfile_data_key
, obj
);
209 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
210 symbol object from GDB. */
212 symbol_to_symbol_object (struct symbol
*sym
)
214 symbol_object
*sym_obj
;
216 sym_obj
= PyObject_New (symbol_object
, &symbol_object_type
);
218 set_symbol (sym_obj
, sym
);
220 return (PyObject
*) sym_obj
;
223 /* Return the symbol that is wrapped by this symbol object. */
225 symbol_object_to_symbol (PyObject
*obj
)
227 if (! PyObject_TypeCheck (obj
, &symbol_object_type
))
229 return ((symbol_object
*) obj
)->symbol
;
233 sympy_dealloc (PyObject
*obj
)
235 symbol_object
*sym_obj
= (symbol_object
*) obj
;
238 sym_obj
->prev
->next
= sym_obj
->next
;
239 else if (SYMBOL_SYMTAB (sym_obj
->symbol
))
241 set_objfile_data (SYMBOL_SYMTAB (sym_obj
->symbol
)->objfile
,
242 sympy_objfile_data_key
, sym_obj
->next
);
245 sym_obj
->next
->prev
= sym_obj
->prev
;
246 sym_obj
->symbol
= NULL
;
250 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
251 A tuple with 2 elements is always returned. The first is the symbol
252 object or None, the second is a boolean with the value of
253 is_a_field_of_this (see comment in lookup_symbol_in_language). */
256 gdbpy_lookup_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
258 int domain
= VAR_DOMAIN
, is_a_field_of_this
= 0;
260 static char *keywords
[] = { "name", "block", "domain", NULL
};
261 struct symbol
*symbol
;
262 PyObject
*block_obj
= NULL
, *ret_tuple
, *sym_obj
, *bool_obj
;
263 struct block
*block
= NULL
;
265 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!i", keywords
, &name
,
266 &block_object_type
, &block_obj
, &domain
))
270 block
= block_object_to_block (block_obj
);
273 struct frame_info
*selected_frame
;
274 volatile struct gdb_exception except
;
276 TRY_CATCH (except
, RETURN_MASK_ALL
)
278 selected_frame
= get_selected_frame (_("No frame selected."));
279 block
= get_frame_block (selected_frame
, NULL
);
281 GDB_PY_HANDLE_EXCEPTION (except
);
284 symbol
= lookup_symbol (name
, block
, domain
, &is_a_field_of_this
);
286 ret_tuple
= PyTuple_New (2);
292 sym_obj
= symbol_to_symbol_object (symbol
);
295 Py_DECREF (ret_tuple
);
304 PyTuple_SET_ITEM (ret_tuple
, 0, sym_obj
);
306 bool_obj
= is_a_field_of_this
? Py_True
: Py_False
;
307 Py_INCREF (bool_obj
);
308 PyTuple_SET_ITEM (ret_tuple
, 1, bool_obj
);
314 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
317 gdbpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
319 int domain
= VAR_DOMAIN
;
321 static char *keywords
[] = { "name", "domain", NULL
};
322 struct symbol
*symbol
;
325 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
329 symbol
= lookup_symbol_global (name
, NULL
, domain
);
333 sym_obj
= symbol_to_symbol_object (symbol
);
346 /* This function is called when an objfile is about to be freed.
347 Invalidate the symbol as further actions on the symbol would result
348 in bad data. All access to obj->symbol should be gated by
349 SYMPY_REQUIRE_VALID which will raise an exception on invalid
352 del_objfile_symbols (struct objfile
*objfile
, void *datum
)
354 symbol_object
*obj
= datum
;
357 symbol_object
*next
= obj
->next
;
368 gdbpy_initialize_symbols (void)
370 if (PyType_Ready (&symbol_object_type
) < 0)
373 /* Register an objfile "free" callback so we can properly
374 invalidate symbol when an object file that is about to be
376 sympy_objfile_data_key
377 = register_objfile_data_with_cleanup (NULL
, del_objfile_symbols
);
379 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNDEF", LOC_UNDEF
);
380 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST", LOC_CONST
);
381 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_STATIC", LOC_STATIC
);
382 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGISTER", LOC_REGISTER
);
383 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_ARG", LOC_ARG
);
384 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG
);
385 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LOCAL", LOC_LOCAL
);
386 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF
);
387 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LABEL", LOC_LABEL
);
388 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_BLOCK", LOC_BLOCK
);
389 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST_BYTES",
391 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNRESOLVED",
393 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_OPTIMIZED_OUT",
395 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED
);
396 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGPARM_ADDR",
398 PyModule_AddIntConstant (gdb_module
, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN
);
399 PyModule_AddIntConstant (gdb_module
, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN
);
400 PyModule_AddIntConstant (gdb_module
, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN
);
401 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN
);
402 PyModule_AddIntConstant (gdb_module
, "SYMBOL_VARIABLES_DOMAIN",
404 PyModule_AddIntConstant (gdb_module
, "SYMBOL_FUNCTIONS_DOMAIN",
406 PyModule_AddIntConstant (gdb_module
, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN
);
408 Py_INCREF (&symbol_object_type
);
409 PyModule_AddObject (gdb_module
, "Symbol", (PyObject
*) &symbol_object_type
);
414 static PyGetSetDef symbol_object_getset
[] = {
415 { "symtab", sympy_get_symtab
, NULL
,
416 "Symbol table in which the symbol appears.", NULL
},
417 { "name", sympy_get_name
, NULL
,
418 "Name of the symbol, as it appears in the source code.", NULL
},
419 { "linkage_name", sympy_get_linkage_name
, NULL
,
420 "Name of the symbol, as used by the linker (i.e., may be mangled).",
422 { "print_name", sympy_get_print_name
, NULL
,
423 "Name of the symbol in a form suitable for output.\n\
424 This is either name or linkage_name, depending on whether the user asked GDB\n\
425 to display demangled or mangled names.", NULL
},
426 { "addr_class", sympy_get_addr_class
, NULL
, "Address class of the symbol." },
427 { "is_argument", sympy_is_argument
, NULL
,
428 "True if the symbol is an argument of a function." },
429 { "is_constant", sympy_is_constant
, NULL
,
430 "True if the symbol is a constant." },
431 { "is_function", sympy_is_function
, NULL
,
432 "True if the symbol is a function or method." },
433 { "is_variable", sympy_is_variable
, NULL
,
434 "True if the symbol is a variable." },
435 { NULL
} /* Sentinel */
438 static PyMethodDef symbol_object_methods
[] = {
439 { "is_valid", sympy_is_valid
, METH_NOARGS
,
440 "is_valid () -> Boolean.\n\
441 Return true if this symbol is valid, false if not." },
442 {NULL
} /* Sentinel */
445 PyTypeObject symbol_object_type
= {
446 PyObject_HEAD_INIT (NULL
)
448 "gdb.Symbol", /*tp_name*/
449 sizeof (symbol_object
), /*tp_basicsize*/
451 sympy_dealloc
, /*tp_dealloc*/
458 0, /*tp_as_sequence*/
462 sympy_str
, /*tp_str*/
466 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
467 "GDB symbol object", /*tp_doc */
470 0, /*tp_richcompare */
471 0, /*tp_weaklistoffset */
474 symbol_object_methods
, /*tp_methods */
476 symbol_object_getset
/*tp_getset */