aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/py-symbol.c
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/python/py-symbol.c')
-rw-r--r--gdb/python/py-symbol.c132
1 files changed, 71 insertions, 61 deletions
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 754420f..3028a30 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -1,6 +1,6 @@
/* Python interface to symbols.
- Copyright (C) 2008-2024 Free Software Foundation, Inc.
+ Copyright (C) 2008-2025 Free Software Foundation, Inc.
This file is part of GDB.
@@ -29,12 +29,6 @@ struct symbol_object {
PyObject_HEAD
/* The GDB symbol structure this object is wrapping. */
struct symbol *symbol;
- /* A symbol object is associated with an objfile, so keep track with
- doubly-linked list, rooted in the objfile. This lets us
- invalidate the underlying struct symbol when the objfile is
- deleted. */
- symbol_object *prev;
- symbol_object *next;
};
/* Require a valid symbol. All access to symbol_object->symbol should be
@@ -50,26 +44,8 @@ struct symbol_object {
} \
} while (0)
-/* A deleter that is used when an objfile is about to be freed. */
-struct symbol_object_deleter
-{
- void operator() (symbol_object *obj)
- {
- while (obj)
- {
- symbol_object *next = obj->next;
-
- obj->symbol = NULL;
- obj->next = NULL;
- obj->prev = NULL;
-
- obj = next;
- }
- }
-};
-
-static const registry<objfile>::key<symbol_object, symbol_object_deleter>
- sympy_objfile_data_key;
+static const gdbpy_registry<gdbpy_memoizing_registry_storage<symbol_object,
+ symbol, &symbol_object::symbol>> sympy_registry;
static PyObject *
sympy_str (PyObject *self)
@@ -153,6 +129,19 @@ sympy_get_addr_class (PyObject *self, void *closure)
return gdb_py_object_from_longest (symbol->aclass ()).release ();
}
+/* Implement gdb.Symbol.domain attribute. Return the domain as an
+ integer. */
+
+static PyObject *
+sympy_get_domain (PyObject *self, void *closure)
+{
+ struct symbol *symbol = nullptr;
+
+ SYMPY_REQUIRE_VALID (self, symbol);
+
+ return gdb_py_object_from_longest (symbol->domain ()).release ();
+}
+
static PyObject *
sympy_is_argument (PyObject *self, void *closure)
{
@@ -205,6 +194,18 @@ sympy_is_variable (PyObject *self, void *closure)
|| theclass == LOC_OPTIMIZED_OUT));
}
+/* Implementation of Symbol.is_artificial. */
+
+static PyObject *
+sympy_is_artificial (PyObject *self, void *closure)
+{
+ struct symbol *symbol = nullptr;
+
+ SYMPY_REQUIRE_VALID (self, symbol);
+
+ return PyBool_FromLong (symbol->is_artificial ());
+}
+
/* Implementation of gdb.Symbol.needs_frame -> Boolean.
Returns true iff the symbol needs a frame for evaluation. */
@@ -222,7 +223,7 @@ sympy_needs_frame (PyObject *self, void *closure)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
if (result)
@@ -307,7 +308,7 @@ sympy_value (PyObject *self, PyObject *args)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
return result;
@@ -322,19 +323,18 @@ static void
set_symbol (symbol_object *obj, struct symbol *symbol)
{
obj->symbol = symbol;
- obj->prev = NULL;
- if (symbol->is_objfile_owned ()
- && symbol->symtab () != NULL)
+ if (symbol->is_objfile_owned ())
{
- struct objfile *objfile = symbol->objfile ();
-
- obj->next = sympy_objfile_data_key.get (objfile);
- if (obj->next)
- obj->next->prev = obj;
- sympy_objfile_data_key.set (objfile, obj);
+ /* Can it really happen that symbol->symtab () is NULL? */
+ if (symbol->symtab () != nullptr)
+ {
+ sympy_registry.add (symbol->objfile (), obj);
+ }
}
else
- obj->next = NULL;
+ {
+ sympy_registry.add (symbol->arch (), obj);
+ }
}
/* Create a new symbol object (gdb.Symbol) that encapsulates the struct
@@ -344,6 +344,15 @@ symbol_to_symbol_object (struct symbol *sym)
{
symbol_object *sym_obj;
+ /* Look if there's already a gdb.Symbol object for given SYMBOL
+ and if so, return it. */
+ if (sym->is_objfile_owned ())
+ sym_obj = sympy_registry.lookup (sym->objfile (), sym);
+ else
+ sym_obj = sympy_registry.lookup (sym->arch (), sym);
+ if (sym_obj != nullptr)
+ return (PyObject*)sym_obj;
+
sym_obj = PyObject_New (symbol_object, &symbol_object_type);
if (sym_obj)
set_symbol (sym_obj, sym);
@@ -365,15 +374,14 @@ sympy_dealloc (PyObject *obj)
{
symbol_object *sym_obj = (symbol_object *) obj;
- if (sym_obj->prev)
- sym_obj->prev->next = sym_obj->next;
- else if (sym_obj->symbol != NULL
- && sym_obj->symbol->is_objfile_owned ()
- && sym_obj->symbol->symtab () != NULL)
- sympy_objfile_data_key.set (sym_obj->symbol->objfile (), sym_obj->next);
- if (sym_obj->next)
- sym_obj->next->prev = sym_obj->prev;
- sym_obj->symbol = NULL;
+ if (sym_obj->symbol != nullptr)
+ {
+ if (sym_obj->symbol->is_objfile_owned ())
+ sympy_registry.remove (sym_obj->symbol->objfile (), sym_obj);
+ else
+ sympy_registry.remove (sym_obj->symbol->arch (), sym_obj);
+ }
+
Py_TYPE (obj)->tp_free (obj);
}
@@ -425,7 +433,7 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
}
@@ -436,7 +444,7 @@ gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
gdbpy_ref<> ret_tuple (PyTuple_New (2));
@@ -485,7 +493,7 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
if (symbol)
@@ -553,7 +561,7 @@ gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
if (symbol)
@@ -598,8 +606,7 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
/* Expand any symtabs that contain potentially matching symbols. */
lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
expand_symtabs_matching (NULL, lookup_name, NULL, NULL,
- SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
- SEARCH_ALL_DOMAINS);
+ SEARCH_STATIC_BLOCK, flags);
for (objfile *objfile : current_program_space->objfiles ())
{
@@ -620,10 +627,11 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
if (symbol != nullptr)
{
- PyObject *sym_obj
- = symbol_to_symbol_object (symbol);
+ PyObject *sym_obj = symbol_to_symbol_object (symbol);
+ if (sym_obj == nullptr)
+ return nullptr;
if (PyList_Append (return_list.get (), sym_obj) == -1)
- return NULL;
+ return nullptr;
}
}
}
@@ -631,7 +639,7 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
}
catch (const gdb_exception &except)
{
- GDB_PY_HANDLE_EXCEPTION (except);
+ return gdbpy_handle_gdb_exception (nullptr, except);
}
return return_list.release ();
@@ -640,7 +648,7 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
gdbpy_initialize_symbols (void)
{
- if (PyType_Ready (&symbol_object_type) < 0)
+ if (gdbpy_type_ready (&symbol_object_type) < 0)
return -1;
if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
@@ -685,8 +693,7 @@ gdbpy_initialize_symbols (void)
#include "sym-domains.def"
#undef SYM_DOMAIN
- return gdb_pymodule_addobject (gdb_module, "Symbol",
- (PyObject *) &symbol_object_type);
+ return 0;
}
GDBPY_INITIALIZE_FILE (gdbpy_initialize_symbols);
@@ -708,8 +715,11 @@ static gdb_PyGetSetDef symbol_object_getset[] = {
This is either name or linkage_name, depending on whether the user asked GDB\n\
to display demangled or mangled names.", NULL },
{ "addr_class", sympy_get_addr_class, NULL, "Address class of the symbol." },
+ { "domain", sympy_get_domain, nullptr, "Domain of the symbol." },
{ "is_argument", sympy_is_argument, NULL,
"True if the symbol is an argument of a function." },
+ { "is_artificial", sympy_is_artificial, nullptr,
+ "True if the symbol is marked artificial." },
{ "is_constant", sympy_is_constant, NULL,
"True if the symbol is a constant." },
{ "is_function", sympy_is_function, NULL,