1 /* Python interface to symbols.
3 Copyright (C) 2008-2015 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/>. */
24 #include "python-internal.h"
27 typedef struct sympy_symbol_object
{
29 /* The GDB symbol structure this object is wrapping. */
30 struct symbol
*symbol
;
31 /* A symbol object is associated with an objfile, so keep track with
32 doubly-linked list, rooted in the objfile. This lets us
33 invalidate the underlying struct symbol when the objfile is
35 struct sympy_symbol_object
*prev
;
36 struct sympy_symbol_object
*next
;
39 /* Require a valid symbol. All access to symbol_object->symbol should be
40 gated by this call. */
41 #define SYMPY_REQUIRE_VALID(symbol_obj, symbol) \
43 symbol = symbol_object_to_symbol (symbol_obj); \
46 PyErr_SetString (PyExc_RuntimeError, \
47 _("Symbol is invalid.")); \
52 static const struct objfile_data
*sympy_objfile_data_key
;
55 sympy_str (PyObject
*self
)
58 struct symbol
*symbol
= NULL
;
60 SYMPY_REQUIRE_VALID (self
, symbol
);
62 result
= PyString_FromString (SYMBOL_PRINT_NAME (symbol
));
68 sympy_get_type (PyObject
*self
, void *closure
)
70 struct symbol
*symbol
= NULL
;
72 SYMPY_REQUIRE_VALID (self
, symbol
);
74 if (SYMBOL_TYPE (symbol
) == NULL
)
80 return type_to_type_object (SYMBOL_TYPE (symbol
));
84 sympy_get_symtab (PyObject
*self
, void *closure
)
86 struct symbol
*symbol
= NULL
;
88 SYMPY_REQUIRE_VALID (self
, symbol
);
90 if (!SYMBOL_OBJFILE_OWNED (symbol
))
93 return symtab_to_symtab_object (symbol_symtab (symbol
));
97 sympy_get_name (PyObject
*self
, void *closure
)
99 struct symbol
*symbol
= NULL
;
101 SYMPY_REQUIRE_VALID (self
, symbol
);
103 return PyString_FromString (SYMBOL_NATURAL_NAME (symbol
));
107 sympy_get_linkage_name (PyObject
*self
, void *closure
)
109 struct symbol
*symbol
= NULL
;
111 SYMPY_REQUIRE_VALID (self
, symbol
);
113 return PyString_FromString (SYMBOL_LINKAGE_NAME (symbol
));
117 sympy_get_print_name (PyObject
*self
, void *closure
)
119 struct symbol
*symbol
= NULL
;
121 SYMPY_REQUIRE_VALID (self
, symbol
);
123 return sympy_str (self
);
127 sympy_get_addr_class (PyObject
*self
, void *closure
)
129 struct symbol
*symbol
= NULL
;
131 SYMPY_REQUIRE_VALID (self
, symbol
);
133 return PyInt_FromLong (SYMBOL_CLASS (symbol
));
137 sympy_is_argument (PyObject
*self
, void *closure
)
139 struct symbol
*symbol
= NULL
;
141 SYMPY_REQUIRE_VALID (self
, symbol
);
143 return PyBool_FromLong (SYMBOL_IS_ARGUMENT (symbol
));
147 sympy_is_constant (PyObject
*self
, void *closure
)
149 struct symbol
*symbol
= NULL
;
150 enum address_class theclass
;
152 SYMPY_REQUIRE_VALID (self
, symbol
);
154 theclass
= SYMBOL_CLASS (symbol
);
156 return PyBool_FromLong (theclass
== LOC_CONST
|| theclass
== LOC_CONST_BYTES
);
160 sympy_is_function (PyObject
*self
, void *closure
)
162 struct symbol
*symbol
= NULL
;
163 enum address_class theclass
;
165 SYMPY_REQUIRE_VALID (self
, symbol
);
167 theclass
= SYMBOL_CLASS (symbol
);
169 return PyBool_FromLong (theclass
== LOC_BLOCK
);
173 sympy_is_variable (PyObject
*self
, void *closure
)
175 struct symbol
*symbol
= NULL
;
176 enum address_class theclass
;
178 SYMPY_REQUIRE_VALID (self
, symbol
);
180 theclass
= SYMBOL_CLASS (symbol
);
182 return PyBool_FromLong (!SYMBOL_IS_ARGUMENT (symbol
)
183 && (theclass
== LOC_LOCAL
|| theclass
== LOC_REGISTER
184 || theclass
== LOC_STATIC
|| theclass
== LOC_COMPUTED
185 || theclass
== LOC_OPTIMIZED_OUT
));
188 /* Implementation of gdb.Symbol.needs_frame -> Boolean.
189 Returns true iff the symbol needs a frame for evaluation. */
192 sympy_needs_frame (PyObject
*self
, void *closure
)
194 struct symbol
*symbol
= NULL
;
197 SYMPY_REQUIRE_VALID (self
, symbol
);
201 result
= symbol_read_needs_frame (symbol
);
203 CATCH (except
, RETURN_MASK_ALL
)
205 GDB_PY_HANDLE_EXCEPTION (except
);
214 /* Implementation of gdb.Symbol.line -> int.
215 Returns the line number at which the symbol was defined. */
218 sympy_line (PyObject
*self
, void *closure
)
220 struct symbol
*symbol
= NULL
;
222 SYMPY_REQUIRE_VALID (self
, symbol
);
224 return PyInt_FromLong (SYMBOL_LINE (symbol
));
227 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
228 Returns True if this Symbol still exists in GDB. */
231 sympy_is_valid (PyObject
*self
, PyObject
*args
)
233 struct symbol
*symbol
= NULL
;
235 symbol
= symbol_object_to_symbol (self
);
242 /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
243 the value of the symbol, or an error in various circumstances. */
246 sympy_value (PyObject
*self
, PyObject
*args
)
248 struct symbol
*symbol
= NULL
;
249 struct frame_info
*frame_info
= NULL
;
250 PyObject
*frame_obj
= NULL
;
251 struct value
*value
= NULL
;
253 if (!PyArg_ParseTuple (args
, "|O", &frame_obj
))
256 if (frame_obj
!= NULL
&& !PyObject_TypeCheck (frame_obj
, &frame_object_type
))
258 PyErr_SetString (PyExc_TypeError
, "argument is not a frame");
262 SYMPY_REQUIRE_VALID (self
, symbol
);
263 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
265 PyErr_SetString (PyExc_TypeError
, "cannot get the value of a typedef");
271 if (frame_obj
!= NULL
)
273 frame_info
= frame_object_to_frame_info (frame_obj
);
274 if (frame_info
== NULL
)
275 error (_("invalid frame"));
278 if (symbol_read_needs_frame (symbol
) && frame_info
== NULL
)
279 error (_("symbol requires a frame to compute its value"));
281 value
= read_var_value (symbol
, frame_info
);
283 CATCH (except
, RETURN_MASK_ALL
)
285 GDB_PY_HANDLE_EXCEPTION (except
);
289 return value_to_value_object (value
);
292 /* Given a symbol, and a symbol_object that has previously been
293 allocated and initialized, populate the symbol_object with the
294 struct symbol data. Also, register the symbol_object life-cycle
295 with the life-cycle of the object file associated with this
296 symbol, if needed. */
298 set_symbol (symbol_object
*obj
, struct symbol
*symbol
)
300 obj
->symbol
= symbol
;
302 if (SYMBOL_OBJFILE_OWNED (symbol
)
303 && symbol_symtab (symbol
) != NULL
)
305 struct objfile
*objfile
= symbol_objfile (symbol
);
307 obj
->next
= objfile_data (objfile
, sympy_objfile_data_key
);
309 obj
->next
->prev
= obj
;
310 set_objfile_data (objfile
, sympy_objfile_data_key
, obj
);
316 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
317 symbol object from GDB. */
319 symbol_to_symbol_object (struct symbol
*sym
)
321 symbol_object
*sym_obj
;
323 sym_obj
= PyObject_New (symbol_object
, &symbol_object_type
);
325 set_symbol (sym_obj
, sym
);
327 return (PyObject
*) sym_obj
;
330 /* Return the symbol that is wrapped by this symbol object. */
332 symbol_object_to_symbol (PyObject
*obj
)
334 if (! PyObject_TypeCheck (obj
, &symbol_object_type
))
336 return ((symbol_object
*) obj
)->symbol
;
340 sympy_dealloc (PyObject
*obj
)
342 symbol_object
*sym_obj
= (symbol_object
*) obj
;
345 sym_obj
->prev
->next
= sym_obj
->next
;
346 else if (sym_obj
->symbol
!= NULL
347 && SYMBOL_OBJFILE_OWNED (sym_obj
->symbol
)
348 && symbol_symtab (sym_obj
->symbol
) != NULL
)
350 set_objfile_data (symbol_objfile (sym_obj
->symbol
),
351 sympy_objfile_data_key
, sym_obj
->next
);
354 sym_obj
->next
->prev
= sym_obj
->prev
;
355 sym_obj
->symbol
= NULL
;
359 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
360 A tuple with 2 elements is always returned. The first is the symbol
361 object or None, the second is a boolean with the value of
362 is_a_field_of_this (see comment in lookup_symbol_in_language). */
365 gdbpy_lookup_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
367 int domain
= VAR_DOMAIN
;
368 struct field_of_this_result is_a_field_of_this
;
370 static char *keywords
[] = { "name", "block", "domain", NULL
};
371 struct symbol
*symbol
= NULL
;
372 PyObject
*block_obj
= NULL
, *ret_tuple
, *sym_obj
, *bool_obj
;
373 const struct block
*block
= NULL
;
375 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!i", keywords
, &name
,
376 &block_object_type
, &block_obj
, &domain
))
380 block
= block_object_to_block (block_obj
);
383 struct frame_info
*selected_frame
;
387 selected_frame
= get_selected_frame (_("No frame selected."));
388 block
= get_frame_block (selected_frame
, NULL
);
390 CATCH (except
, RETURN_MASK_ALL
)
392 GDB_PY_HANDLE_EXCEPTION (except
);
399 symbol
= lookup_symbol (name
, block
, (domain_enum
) domain
,
400 &is_a_field_of_this
).symbol
;
402 CATCH (except
, RETURN_MASK_ALL
)
404 GDB_PY_HANDLE_EXCEPTION (except
);
408 ret_tuple
= PyTuple_New (2);
414 sym_obj
= symbol_to_symbol_object (symbol
);
417 Py_DECREF (ret_tuple
);
426 PyTuple_SET_ITEM (ret_tuple
, 0, sym_obj
);
428 bool_obj
= (is_a_field_of_this
.type
!= NULL
) ? Py_True
: Py_False
;
429 Py_INCREF (bool_obj
);
430 PyTuple_SET_ITEM (ret_tuple
, 1, bool_obj
);
436 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
439 gdbpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
441 int domain
= VAR_DOMAIN
;
443 static char *keywords
[] = { "name", "domain", NULL
};
444 struct symbol
*symbol
= NULL
;
447 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
453 symbol
= lookup_global_symbol (name
, NULL
, (domain_enum
) domain
).symbol
;
455 CATCH (except
, RETURN_MASK_ALL
)
457 GDB_PY_HANDLE_EXCEPTION (except
);
463 sym_obj
= symbol_to_symbol_object (symbol
);
476 /* This function is called when an objfile is about to be freed.
477 Invalidate the symbol as further actions on the symbol would result
478 in bad data. All access to obj->symbol should be gated by
479 SYMPY_REQUIRE_VALID which will raise an exception on invalid
482 del_objfile_symbols (struct objfile
*objfile
, void *datum
)
484 symbol_object
*obj
= datum
;
487 symbol_object
*next
= obj
->next
;
498 gdbpy_initialize_symbols (void)
500 if (PyType_Ready (&symbol_object_type
) < 0)
503 /* Register an objfile "free" callback so we can properly
504 invalidate symbol when an object file that is about to be
506 sympy_objfile_data_key
507 = register_objfile_data_with_cleanup (NULL
, del_objfile_symbols
);
509 if (PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNDEF", LOC_UNDEF
) < 0
510 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST",
512 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_STATIC",
514 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGISTER",
516 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_ARG",
518 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REF_ARG",
520 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LOCAL",
522 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_TYPEDEF",
524 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LABEL",
526 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_BLOCK",
528 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST_BYTES",
530 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNRESOLVED",
532 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_OPTIMIZED_OUT",
533 LOC_OPTIMIZED_OUT
) < 0
534 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_COMPUTED",
536 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGPARM_ADDR",
537 LOC_REGPARM_ADDR
) < 0
538 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_UNDEF_DOMAIN",
540 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_VAR_DOMAIN",
542 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_STRUCT_DOMAIN",
544 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LABEL_DOMAIN",
546 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_VARIABLES_DOMAIN",
547 VARIABLES_DOMAIN
) < 0
548 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_FUNCTIONS_DOMAIN",
549 FUNCTIONS_DOMAIN
) < 0
550 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_TYPES_DOMAIN",
554 return gdb_pymodule_addobject (gdb_module
, "Symbol",
555 (PyObject
*) &symbol_object_type
);
560 static PyGetSetDef symbol_object_getset
[] = {
561 { "type", sympy_get_type
, NULL
,
562 "Type of the symbol.", NULL
},
563 { "symtab", sympy_get_symtab
, NULL
,
564 "Symbol table in which the symbol appears.", NULL
},
565 { "name", sympy_get_name
, NULL
,
566 "Name of the symbol, as it appears in the source code.", NULL
},
567 { "linkage_name", sympy_get_linkage_name
, NULL
,
568 "Name of the symbol, as used by the linker (i.e., may be mangled).",
570 { "print_name", sympy_get_print_name
, NULL
,
571 "Name of the symbol in a form suitable for output.\n\
572 This is either name or linkage_name, depending on whether the user asked GDB\n\
573 to display demangled or mangled names.", NULL
},
574 { "addr_class", sympy_get_addr_class
, NULL
, "Address class of the symbol." },
575 { "is_argument", sympy_is_argument
, NULL
,
576 "True if the symbol is an argument of a function." },
577 { "is_constant", sympy_is_constant
, NULL
,
578 "True if the symbol is a constant." },
579 { "is_function", sympy_is_function
, NULL
,
580 "True if the symbol is a function or method." },
581 { "is_variable", sympy_is_variable
, NULL
,
582 "True if the symbol is a variable." },
583 { "needs_frame", sympy_needs_frame
, NULL
,
584 "True if the symbol requires a frame for evaluation." },
585 { "line", sympy_line
, NULL
,
586 "The source line number at which the symbol was defined." },
587 { NULL
} /* Sentinel */
590 static PyMethodDef symbol_object_methods
[] = {
591 { "is_valid", sympy_is_valid
, METH_NOARGS
,
592 "is_valid () -> Boolean.\n\
593 Return true if this symbol is valid, false if not." },
594 { "value", sympy_value
, METH_VARARGS
,
595 "value ([frame]) -> gdb.Value\n\
596 Return the value of the symbol." },
597 {NULL
} /* Sentinel */
600 PyTypeObject symbol_object_type
= {
601 PyVarObject_HEAD_INIT (NULL
, 0)
602 "gdb.Symbol", /*tp_name*/
603 sizeof (symbol_object
), /*tp_basicsize*/
605 sympy_dealloc
, /*tp_dealloc*/
612 0, /*tp_as_sequence*/
616 sympy_str
, /*tp_str*/
620 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
621 "GDB symbol object", /*tp_doc */
624 0, /*tp_richcompare */
625 0, /*tp_weaklistoffset */
628 symbol_object_methods
, /*tp_methods */
630 symbol_object_getset
/*tp_getset */