1 /* Python interface to symbols.
3 Copyright (C) 2008-2023 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"
28 struct 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
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 /* A deleter that is used when an objfile is about to be freed. */
54 struct symbol_object_deleter
56 void operator() (symbol_object
*obj
)
60 symbol_object
*next
= obj
->next
;
71 static const registry
<objfile
>::key
<symbol_object
, symbol_object_deleter
>
72 sympy_objfile_data_key
;
75 sympy_str (PyObject
*self
)
78 struct symbol
*symbol
= NULL
;
80 SYMPY_REQUIRE_VALID (self
, symbol
);
82 result
= PyUnicode_FromString (symbol
->print_name ());
88 sympy_get_type (PyObject
*self
, void *closure
)
90 struct symbol
*symbol
= NULL
;
92 SYMPY_REQUIRE_VALID (self
, symbol
);
94 if (symbol
->type () == NULL
)
100 return type_to_type_object (symbol
->type ());
104 sympy_get_symtab (PyObject
*self
, void *closure
)
106 struct symbol
*symbol
= NULL
;
108 SYMPY_REQUIRE_VALID (self
, symbol
);
110 if (!symbol
->is_objfile_owned ())
113 return symtab_to_symtab_object (symbol
->symtab ());
117 sympy_get_name (PyObject
*self
, void *closure
)
119 struct symbol
*symbol
= NULL
;
121 SYMPY_REQUIRE_VALID (self
, symbol
);
123 return PyUnicode_FromString (symbol
->natural_name ());
127 sympy_get_linkage_name (PyObject
*self
, void *closure
)
129 struct symbol
*symbol
= NULL
;
131 SYMPY_REQUIRE_VALID (self
, symbol
);
133 return PyUnicode_FromString (symbol
->linkage_name ());
137 sympy_get_print_name (PyObject
*self
, void *closure
)
139 struct symbol
*symbol
= NULL
;
141 SYMPY_REQUIRE_VALID (self
, symbol
);
143 return sympy_str (self
);
147 sympy_get_addr_class (PyObject
*self
, void *closure
)
149 struct symbol
*symbol
= NULL
;
151 SYMPY_REQUIRE_VALID (self
, symbol
);
153 return gdb_py_object_from_longest (symbol
->aclass ()).release ();
157 sympy_is_argument (PyObject
*self
, void *closure
)
159 struct symbol
*symbol
= NULL
;
161 SYMPY_REQUIRE_VALID (self
, symbol
);
163 return PyBool_FromLong (symbol
->is_argument ());
167 sympy_is_constant (PyObject
*self
, void *closure
)
169 struct symbol
*symbol
= NULL
;
170 enum address_class theclass
;
172 SYMPY_REQUIRE_VALID (self
, symbol
);
174 theclass
= symbol
->aclass ();
176 return PyBool_FromLong (theclass
== LOC_CONST
|| theclass
== LOC_CONST_BYTES
);
180 sympy_is_function (PyObject
*self
, void *closure
)
182 struct symbol
*symbol
= NULL
;
183 enum address_class theclass
;
185 SYMPY_REQUIRE_VALID (self
, symbol
);
187 theclass
= symbol
->aclass ();
189 return PyBool_FromLong (theclass
== LOC_BLOCK
);
193 sympy_is_variable (PyObject
*self
, void *closure
)
195 struct symbol
*symbol
= NULL
;
196 enum address_class theclass
;
198 SYMPY_REQUIRE_VALID (self
, symbol
);
200 theclass
= symbol
->aclass ();
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
));
208 /* Implementation of gdb.Symbol.needs_frame -> Boolean.
209 Returns true iff the symbol needs a frame for evaluation. */
212 sympy_needs_frame (PyObject
*self
, void *closure
)
214 struct symbol
*symbol
= NULL
;
217 SYMPY_REQUIRE_VALID (self
, symbol
);
221 result
= symbol_read_needs_frame (symbol
);
223 catch (const gdb_exception
&except
)
225 GDB_PY_HANDLE_EXCEPTION (except
);
233 /* Implementation of gdb.Symbol.line -> int.
234 Returns the line number at which the symbol was defined. */
237 sympy_line (PyObject
*self
, void *closure
)
239 struct symbol
*symbol
= NULL
;
241 SYMPY_REQUIRE_VALID (self
, symbol
);
243 return gdb_py_object_from_longest (symbol
->line ()).release ();
246 /* Implementation of gdb.Symbol.is_valid (self) -> Boolean.
247 Returns True if this Symbol still exists in GDB. */
250 sympy_is_valid (PyObject
*self
, PyObject
*args
)
252 struct symbol
*symbol
= NULL
;
254 symbol
= symbol_object_to_symbol (self
);
261 /* Implementation of gdb.Symbol.value (self[, frame]) -> gdb.Value. Returns
262 the value of the symbol, or an error in various circumstances. */
265 sympy_value (PyObject
*self
, PyObject
*args
)
267 struct symbol
*symbol
= NULL
;
268 frame_info_ptr frame_info
= NULL
;
269 PyObject
*frame_obj
= NULL
;
270 struct value
*value
= NULL
;
272 if (!PyArg_ParseTuple (args
, "|O", &frame_obj
))
275 if (frame_obj
!= NULL
&& !PyObject_TypeCheck (frame_obj
, &frame_object_type
))
277 PyErr_SetString (PyExc_TypeError
, "argument is not a frame");
281 SYMPY_REQUIRE_VALID (self
, symbol
);
282 if (symbol
->aclass () == LOC_TYPEDEF
)
284 PyErr_SetString (PyExc_TypeError
, "cannot get the value of a typedef");
290 if (frame_obj
!= NULL
)
292 frame_info
= frame_object_to_frame_info (frame_obj
);
293 if (frame_info
== NULL
)
294 error (_("invalid frame"));
297 if (symbol_read_needs_frame (symbol
) && frame_info
== NULL
)
298 error (_("symbol requires a frame to compute its value"));
300 /* TODO: currently, we have no way to recover the block in which SYMBOL
301 was found, so we have no block to pass to read_var_value. This will
302 yield an incorrect value when symbol is not local to FRAME_INFO (this
303 can happen with nested functions). */
304 value
= read_var_value (symbol
, NULL
, frame_info
);
306 catch (const gdb_exception
&except
)
308 GDB_PY_HANDLE_EXCEPTION (except
);
311 return value_to_value_object (value
);
314 /* Given a symbol, and a symbol_object that has previously been
315 allocated and initialized, populate the symbol_object with the
316 struct symbol data. Also, register the symbol_object life-cycle
317 with the life-cycle of the object file associated with this
318 symbol, if needed. */
320 set_symbol (symbol_object
*obj
, struct symbol
*symbol
)
322 obj
->symbol
= symbol
;
324 if (symbol
->is_objfile_owned ()
325 && symbol
->symtab () != NULL
)
327 struct objfile
*objfile
= symbol
->objfile ();
329 obj
->next
= sympy_objfile_data_key
.get (objfile
);
331 obj
->next
->prev
= obj
;
332 sympy_objfile_data_key
.set (objfile
, obj
);
338 /* Create a new symbol object (gdb.Symbol) that encapsulates the struct
339 symbol object from GDB. */
341 symbol_to_symbol_object (struct symbol
*sym
)
343 symbol_object
*sym_obj
;
345 sym_obj
= PyObject_New (symbol_object
, &symbol_object_type
);
347 set_symbol (sym_obj
, sym
);
349 return (PyObject
*) sym_obj
;
352 /* Return the symbol that is wrapped by this symbol object. */
354 symbol_object_to_symbol (PyObject
*obj
)
356 if (! PyObject_TypeCheck (obj
, &symbol_object_type
))
358 return ((symbol_object
*) obj
)->symbol
;
362 sympy_dealloc (PyObject
*obj
)
364 symbol_object
*sym_obj
= (symbol_object
*) obj
;
367 sym_obj
->prev
->next
= sym_obj
->next
;
368 else if (sym_obj
->symbol
!= NULL
369 && sym_obj
->symbol
->is_objfile_owned ()
370 && sym_obj
->symbol
->symtab () != NULL
)
371 sympy_objfile_data_key
.set (sym_obj
->symbol
->objfile (), sym_obj
->next
);
373 sym_obj
->next
->prev
= sym_obj
->prev
;
374 sym_obj
->symbol
= NULL
;
375 Py_TYPE (obj
)->tp_free (obj
);
379 gdb.lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)
380 A tuple with 2 elements is always returned. The first is the symbol
381 object or None, the second is a boolean with the value of
382 is_a_field_of_this (see comment in lookup_symbol_in_language). */
385 gdbpy_lookup_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
387 int domain
= VAR_DOMAIN
;
388 struct field_of_this_result is_a_field_of_this
;
390 static const char *keywords
[] = { "name", "block", "domain", NULL
};
391 struct symbol
*symbol
= NULL
;
392 PyObject
*block_obj
= NULL
, *sym_obj
, *bool_obj
;
393 const struct block
*block
= NULL
;
395 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!i", keywords
, &name
,
396 &block_object_type
, &block_obj
,
401 block
= block_object_to_block (block_obj
);
404 frame_info_ptr selected_frame
;
408 selected_frame
= get_selected_frame (_("No frame selected."));
409 block
= get_frame_block (selected_frame
, NULL
);
411 catch (const gdb_exception
&except
)
413 GDB_PY_HANDLE_EXCEPTION (except
);
419 symbol
= lookup_symbol (name
, block
, (domain_enum
) domain
,
420 &is_a_field_of_this
).symbol
;
422 catch (const gdb_exception
&except
)
424 GDB_PY_HANDLE_EXCEPTION (except
);
427 gdbpy_ref
<> ret_tuple (PyTuple_New (2));
428 if (ret_tuple
== NULL
)
433 sym_obj
= symbol_to_symbol_object (symbol
);
442 PyTuple_SET_ITEM (ret_tuple
.get (), 0, sym_obj
);
444 bool_obj
= PyBool_FromLong (is_a_field_of_this
.type
!= NULL
);
445 PyTuple_SET_ITEM (ret_tuple
.get (), 1, bool_obj
);
447 return ret_tuple
.release ();
451 gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
454 gdbpy_lookup_global_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
456 int domain
= VAR_DOMAIN
;
458 static const char *keywords
[] = { "name", "domain", NULL
};
459 struct symbol
*symbol
= NULL
;
462 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
468 symbol
= lookup_global_symbol (name
, NULL
, (domain_enum
) domain
).symbol
;
470 catch (const gdb_exception
&except
)
472 GDB_PY_HANDLE_EXCEPTION (except
);
477 sym_obj
= symbol_to_symbol_object (symbol
);
491 gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */
494 gdbpy_lookup_static_symbol (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
497 int domain
= VAR_DOMAIN
;
498 static const char *keywords
[] = { "name", "domain", NULL
};
499 struct symbol
*symbol
= NULL
;
502 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
506 /* In order to find static symbols associated with the "current" object
507 file ahead of those from other object files, we first need to see if
508 we can acquire a current block. If this fails however, then we still
509 want to search all static symbols, so don't throw an exception just
511 const struct block
*block
= NULL
;
514 frame_info_ptr selected_frame
515 = get_selected_frame (_("No frame selected."));
516 block
= get_frame_block (selected_frame
, NULL
);
518 catch (const gdb_exception
&except
)
525 if (block
!= nullptr)
527 = lookup_symbol_in_static_block (name
, block
,
528 (domain_enum
) domain
).symbol
;
530 if (symbol
== nullptr)
531 symbol
= lookup_static_symbol (name
, (domain_enum
) domain
).symbol
;
533 catch (const gdb_exception
&except
)
535 GDB_PY_HANDLE_EXCEPTION (except
);
540 sym_obj
= symbol_to_symbol_object (symbol
);
554 gdb.lookup_static_symbols (name [, domain]) -> symbol list.
556 Returns a list of all static symbols matching NAME in DOMAIN. */
559 gdbpy_lookup_static_symbols (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
562 int domain
= VAR_DOMAIN
;
563 static const char *keywords
[] = { "name", "domain", NULL
};
565 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &name
,
569 gdbpy_ref
<> return_list (PyList_New (0));
570 if (return_list
== NULL
)
575 /* Expand any symtabs that contain potentially matching symbols. */
576 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
577 expand_symtabs_matching (NULL
, lookup_name
, NULL
, NULL
,
578 SEARCH_GLOBAL_BLOCK
| SEARCH_STATIC_BLOCK
,
581 for (objfile
*objfile
: current_program_space
->objfiles ())
583 for (compunit_symtab
*cust
: objfile
->compunits ())
585 const struct blockvector
*bv
;
587 bv
= cust
->blockvector ();
588 const struct block
*block
= bv
->static_block ();
590 if (block
!= nullptr)
592 symbol
*symbol
= lookup_symbol_in_static_block
593 (name
, block
, (domain_enum
) domain
).symbol
;
595 if (symbol
!= nullptr)
598 = symbol_to_symbol_object (symbol
);
599 if (PyList_Append (return_list
.get (), sym_obj
) == -1)
606 catch (const gdb_exception
&except
)
608 GDB_PY_HANDLE_EXCEPTION (except
);
611 return return_list
.release ();
615 gdbpy_initialize_symbols (void)
617 if (PyType_Ready (&symbol_object_type
) < 0)
620 if (PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNDEF", LOC_UNDEF
) < 0
621 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST",
623 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_STATIC",
625 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGISTER",
627 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_ARG",
629 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REF_ARG",
631 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LOCAL",
633 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_TYPEDEF",
635 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_LABEL",
637 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_BLOCK",
639 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_CONST_BYTES",
641 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_UNRESOLVED",
643 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_OPTIMIZED_OUT",
644 LOC_OPTIMIZED_OUT
) < 0
645 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_COMPUTED",
647 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_COMMON_BLOCK",
648 LOC_COMMON_BLOCK
) < 0
649 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LOC_REGPARM_ADDR",
650 LOC_REGPARM_ADDR
) < 0
651 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_UNDEF_DOMAIN",
653 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_VAR_DOMAIN",
655 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_STRUCT_DOMAIN",
657 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_LABEL_DOMAIN",
659 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_MODULE_DOMAIN",
661 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_COMMON_BLOCK_DOMAIN",
662 COMMON_BLOCK_DOMAIN
) < 0)
665 /* These remain defined for compatibility, but as they were never
666 correct, they are no longer documented. Eventually we can remove
667 them. These exist because at one time, enum search_domain and
668 enum domain_enum_tag were combined -- but different values were
669 used differently. Here we try to give them values that will make
670 sense if they are passed to gdb.lookup_symbol. */
671 if (PyModule_AddIntConstant (gdb_module
, "SYMBOL_VARIABLES_DOMAIN",
673 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_FUNCTIONS_DOMAIN",
675 || PyModule_AddIntConstant (gdb_module
, "SYMBOL_TYPES_DOMAIN",
679 return gdb_pymodule_addobject (gdb_module
, "Symbol",
680 (PyObject
*) &symbol_object_type
);
685 static gdb_PyGetSetDef symbol_object_getset
[] = {
686 { "type", sympy_get_type
, NULL
,
687 "Type of the symbol.", NULL
},
688 { "symtab", sympy_get_symtab
, NULL
,
689 "Symbol table in which the symbol appears.", NULL
},
690 { "name", sympy_get_name
, NULL
,
691 "Name of the symbol, as it appears in the source code.", NULL
},
692 { "linkage_name", sympy_get_linkage_name
, NULL
,
693 "Name of the symbol, as used by the linker (i.e., may be mangled).",
695 { "print_name", sympy_get_print_name
, NULL
,
696 "Name of the symbol in a form suitable for output.\n\
697 This is either name or linkage_name, depending on whether the user asked GDB\n\
698 to display demangled or mangled names.", NULL
},
699 { "addr_class", sympy_get_addr_class
, NULL
, "Address class of the symbol." },
700 { "is_argument", sympy_is_argument
, NULL
,
701 "True if the symbol is an argument of a function." },
702 { "is_constant", sympy_is_constant
, NULL
,
703 "True if the symbol is a constant." },
704 { "is_function", sympy_is_function
, NULL
,
705 "True if the symbol is a function or method." },
706 { "is_variable", sympy_is_variable
, NULL
,
707 "True if the symbol is a variable." },
708 { "needs_frame", sympy_needs_frame
, NULL
,
709 "True if the symbol requires a frame for evaluation." },
710 { "line", sympy_line
, NULL
,
711 "The source line number at which the symbol was defined." },
712 { NULL
} /* Sentinel */
715 static PyMethodDef symbol_object_methods
[] = {
716 { "is_valid", sympy_is_valid
, METH_NOARGS
,
717 "is_valid () -> Boolean.\n\
718 Return true if this symbol is valid, false if not." },
719 { "value", sympy_value
, METH_VARARGS
,
720 "value ([frame]) -> gdb.Value\n\
721 Return the value of the symbol." },
722 {NULL
} /* Sentinel */
725 PyTypeObject symbol_object_type
= {
726 PyVarObject_HEAD_INIT (NULL
, 0)
727 "gdb.Symbol", /*tp_name*/
728 sizeof (symbol_object
), /*tp_basicsize*/
730 sympy_dealloc
, /*tp_dealloc*/
737 0, /*tp_as_sequence*/
741 sympy_str
, /*tp_str*/
745 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
746 "GDB symbol object", /*tp_doc */
749 0, /*tp_richcompare */
750 0, /*tp_weaklistoffset */
753 symbol_object_methods
, /*tp_methods */
755 symbol_object_getset
/*tp_getset */