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_type (PyObject
*self
, void *closure
)
71 struct symbol
*symbol
= NULL
;
73 SYMPY_REQUIRE_VALID (self
, symbol
);
75 if (SYMBOL_TYPE (symbol
) == NULL
)
81 return type_to_type_object (SYMBOL_TYPE (symbol
));
85 sympy_get_symtab (PyObject
*self
, void *closure
)
87 struct symbol
*symbol
= NULL
;
89 SYMPY_REQUIRE_VALID (self
, symbol
);
91 return symtab_to_symtab_object (SYMBOL_SYMTAB (symbol
));
95 sympy_get_name (PyObject
*self
, void *closure
)
97 struct symbol
*symbol
= NULL
;
99 SYMPY_REQUIRE_VALID (self
, symbol
);
101 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol
));
105 sympy_get_linkage_name (PyObject
*self
, void *closure
)
107 struct symbol
*symbol
= NULL
;
109 SYMPY_REQUIRE_VALID (self
, symbol
);
111 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol
));
115 sympy_get_print_name (PyObject
*self
, void *closure
)
117 struct symbol
*symbol
= NULL
;
119 SYMPY_REQUIRE_VALID (self
, symbol
);
121 return sympy_str (self
);
125 sympy_get_addr_class (PyObject
*self
, void *closure
)
127 struct symbol
*symbol
= NULL
;
129 SYMPY_REQUIRE_VALID (self
, symbol
);
131 return PyInt_FromLong (SYMBOL_CLASS (symbol
));
135 sympy_is_argument (PyObject
*self
, void *closure
)
137 struct symbol
*symbol
= NULL
;
139 SYMPY_REQUIRE_VALID (self
, symbol
);
141 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol
));
145 sympy_is_constant (PyObject
*self
, void *closure
)
147 struct symbol
*symbol
= NULL
;
148 enum address_class
class;
150 SYMPY_REQUIRE_VALID (self
, symbol
);
152 class = SYMBOL_CLASS (symbol
);
154 return PyBool_FromLong (class == LOC_CONST
|| class == LOC_CONST_BYTES
);
158 sympy_is_function (PyObject
*self
, void *closure
)
160 struct symbol
*symbol
= NULL
;
161 enum address_class
class;
163 SYMPY_REQUIRE_VALID (self
, symbol
);
165 class = SYMBOL_CLASS (symbol
);
167 return PyBool_FromLong (class == LOC_BLOCK
);
171 sympy_is_variable (PyObject
*self
, void *closure
)
173 struct symbol
*symbol
= NULL
;
174 enum address_class
class;
176 SYMPY_REQUIRE_VALID (self
, symbol
);
178 class = SYMBOL_CLASS (symbol
);
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
));
186 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
187 Returns True if this Symbol still exists in GDB. */
190 sympy_is_valid (PyObject
*self
, PyObject
*args
)
192 struct symbol
*symbol
= NULL
;
194 symbol
= symbol_object_to_symbol (self
);
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. */
207 set_symbol (symbol_object
*obj
, struct symbol
*symbol
)
209 obj
->symbol
= symbol
;
211 if (SYMBOL_SYMTAB (symbol
))
213 obj
->next
= objfile_data (SYMBOL_SYMTAB (symbol
)->objfile
,
214 sympy_objfile_data_key
);
217 obj
->next
->prev
= obj
;
218 set_objfile_data (SYMBOL_SYMTAB (symbol
)->objfile
,
219 sympy_objfile_data_key
, obj
);
225 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
226 symbol object from GDB. */
228 symbol_to_symbol_object (struct symbol
*sym
)
230 symbol_object
*sym_obj
;
232 sym_obj
= PyObject_New (symbol_object
, &symbol_object_type
);
234 set_symbol (sym_obj
, sym
);
236 return (PyObject
*) sym_obj
;
239 /* Return the symbol that is wrapped by this symbol object. */
241 symbol_object_to_symbol (PyObject
*obj
)
243 if (! PyObject_TypeCheck (obj
, &symbol_object_type
))
245 return ((symbol_object
*) obj
)->symbol
;
249 sympy_dealloc (PyObject
*obj
)
251 symbol_object
*sym_obj
= (symbol_object
*) obj
;
254 sym_obj
->prev
->next
= sym_obj
->next
;
255 else if (SYMBOL_SYMTAB (sym_obj
->symbol
))
257 set_objfile_data (SYMBOL_SYMTAB (sym_obj
->symbol
)->objfile
,
258 sympy_objfile_data_key
, sym_obj
->next
);
261 sym_obj
->next
->prev
= sym_obj
->prev
;
262 sym_obj
->symbol
= NULL
;
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). */
272 gdbpy_lookup_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
274 int domain
= VAR_DOMAIN
, is_a_field_of_this
= 0;
276 static char *keywords
[] = { "name", "block", "domain", NULL
};
277 struct symbol
*symbol
= NULL
;
278 PyObject
*block_obj
= NULL
, *ret_tuple
, *sym_obj
, *bool_obj
;
279 const struct block
*block
= NULL
;
280 volatile struct gdb_exception except
;
282 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!i", keywords
, &name
,
283 &block_object_type
, &block_obj
, &domain
))
287 block
= block_object_to_block (block_obj
);
290 struct frame_info
*selected_frame
;
291 volatile struct gdb_exception except
;
293 TRY_CATCH (except
, RETURN_MASK_ALL
)
295 selected_frame
= get_selected_frame (_("No frame selected."));
296 block
= get_frame_block (selected_frame
, NULL
);
298 GDB_PY_HANDLE_EXCEPTION (except
);
301 TRY_CATCH (except
, RETURN_MASK_ALL
)
303 symbol
= lookup_symbol (name
, block
, domain
, &is_a_field_of_this
);
305 GDB_PY_HANDLE_EXCEPTION (except
);
307 ret_tuple
= PyTuple_New (2);
313 sym_obj
= symbol_to_symbol_object (symbol
);
316 Py_DECREF (ret_tuple
);
325 PyTuple_SET_ITEM (ret_tuple
, 0, sym_obj
);
327 bool_obj
= is_a_field_of_this
? Py_True
: Py_False
;
328 Py_INCREF (bool_obj
);
329 PyTuple_SET_ITEM (ret_tuple
, 1, bool_obj
);
335 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
338 gdbpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
340 int domain
= VAR_DOMAIN
;
342 static char *keywords
[] = { "name", "domain", NULL
};
343 struct symbol
*symbol
= NULL
;
345 volatile struct gdb_exception except
;
347 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
351 TRY_CATCH (except
, RETURN_MASK_ALL
)
353 symbol
= lookup_symbol_global (name
, NULL
, domain
);
355 GDB_PY_HANDLE_EXCEPTION (except
);
359 sym_obj
= symbol_to_symbol_object (symbol
);
372 /* This function is called when an objfile is about to be freed.
373 Invalidate the symbol as further actions on the symbol would result
374 in bad data. All access to obj->symbol should be gated by
375 SYMPY_REQUIRE_VALID which will raise an exception on invalid
378 del_objfile_symbols (struct objfile
*objfile
, void *datum
)
380 symbol_object
*obj
= datum
;
383 symbol_object
*next
= obj
->next
;
394 gdbpy_initialize_symbols (void)
396 if (PyType_Ready (&symbol_object_type
) < 0)
399 /* Register an objfile "free" callback so we can properly
400 invalidate symbol when an object file that is about to be
402 sympy_objfile_data_key
403 = register_objfile_data_with_cleanup (NULL
, del_objfile_symbols
);
405 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNDEF", LOC_UNDEF
);
406 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST", LOC_CONST
);
407 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_STATIC", LOC_STATIC
);
408 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGISTER", LOC_REGISTER
);
409 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_ARG", LOC_ARG
);
410 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REF_ARG", LOC_REF_ARG
);
411 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LOCAL", LOC_LOCAL
);
412 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_TYPEDEF", LOC_TYPEDEF
);
413 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LABEL", LOC_LABEL
);
414 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_BLOCK", LOC_BLOCK
);
415 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST_BYTES",
417 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNRESOLVED",
419 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_OPTIMIZED_OUT",
421 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_COMPUTED", LOC_COMPUTED
);
422 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGPARM_ADDR",
424 PyModule_AddIntConstant (gdb_module
, "SYMBOL_UNDEF_DOMAIN", UNDEF_DOMAIN
);
425 PyModule_AddIntConstant (gdb_module
, "SYMBOL_VAR_DOMAIN", VAR_DOMAIN
);
426 PyModule_AddIntConstant (gdb_module
, "SYMBOL_STRUCT_DOMAIN", STRUCT_DOMAIN
);
427 PyModule_AddIntConstant (gdb_module
, "SYMBOL_LABEL_DOMAIN", LABEL_DOMAIN
);
428 PyModule_AddIntConstant (gdb_module
, "SYMBOL_VARIABLES_DOMAIN",
430 PyModule_AddIntConstant (gdb_module
, "SYMBOL_FUNCTIONS_DOMAIN",
432 PyModule_AddIntConstant (gdb_module
, "SYMBOL_TYPES_DOMAIN", TYPES_DOMAIN
);
434 Py_INCREF (&symbol_object_type
);
435 PyModule_AddObject (gdb_module
, "Symbol", (PyObject
*) &symbol_object_type
);
440 static PyGetSetDef symbol_object_getset
[] = {
441 { "type", sympy_get_type
, NULL
,
442 "Type of the symbol.", NULL
},
443 { "symtab", sympy_get_symtab
, NULL
,
444 "Symbol table in which the symbol appears.", NULL
},
445 { "name", sympy_get_name
, NULL
,
446 "Name of the symbol, as it appears in the source code.", NULL
},
447 { "linkage_name", sympy_get_linkage_name
, NULL
,
448 "Name of the symbol, as used by the linker (i.e., may be mangled).",
450 { "print_name", sympy_get_print_name
, NULL
,
451 "Name of the symbol in a form suitable for output.\n\
452 This is either name or linkage_name, depending on whether the user asked GDB\n\
453 to display demangled or mangled names.", NULL
},
454 { "addr_class", sympy_get_addr_class
, NULL
, "Address class of the symbol." },
455 { "is_argument", sympy_is_argument
, NULL
,
456 "True if the symbol is an argument of a function." },
457 { "is_constant", sympy_is_constant
, NULL
,
458 "True if the symbol is a constant." },
459 { "is_function", sympy_is_function
, NULL
,
460 "True if the symbol is a function or method." },
461 { "is_variable", sympy_is_variable
, NULL
,
462 "True if the symbol is a variable." },
463 { NULL
} /* Sentinel */
466 static PyMethodDef symbol_object_methods
[] = {
467 { "is_valid", sympy_is_valid
, METH_NOARGS
,
468 "is_valid () -> Boolean.\n\
469 Return true if this symbol is valid, false if not." },
470 {NULL
} /* Sentinel */
473 PyTypeObject symbol_object_type
= {
474 PyObject_HEAD_INIT (NULL
)
476 "gdb.Symbol", /*tp_name*/
477 sizeof (symbol_object
), /*tp_basicsize*/
479 sympy_dealloc
, /*tp_dealloc*/
486 0, /*tp_as_sequence*/
490 sympy_str
, /*tp_str*/
494 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
495 "GDB symbol object", /*tp_doc */
498 0, /*tp_richcompare */
499 0, /*tp_weaklistoffset */
502 symbol_object_methods
, /*tp_methods */
504 symbol_object_getset
/*tp_getset */