[PATCH v2 13/28] Convert gdbpy_lookup_static_symbols

Tom Tromey
2025-04-02T23:56:43Z

This changes gdbpy_lookup_static_symbols to the callback approach,
merging the search loop and the call to expand_symtabs_matching.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
---
gdb/python/py-symbol.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 3028a307205eb9ea70dd0c5f7fcbeda01d89b96d..5268b5a242c4cab801e342b2580fa8d99fdbbd1d 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -605,17 +605,17 @@ 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_STATIC_BLOCK, flags);

for (objfile *objfile : current_program_space->objfiles ())
{
- for (compunit_symtab *cust : objfile->compunits ())
+ bool error_found = false;
+
+ auto callback = [&] (compunit_symtab *cust)
{
/* Skip included compunits to prevent including compunits from
being searched twice. */
if (cust->user != nullptr)
- continue;
+ return true;

const struct blockvector *bv = cust->blockvector ();
const struct block *block = bv->static_block ();
@@ -628,13 +628,24 @@ gdbpy_lookup_static_symbols (PyObject *self, PyObject *args, PyObject *kw)
if (symbol != nullptr)
{
PyObject *sym_obj = symbol_to_symbol_object (symbol);
- if (sym_obj == nullptr)
- return nullptr;
- if (PyList_Append (return_list.get (), sym_obj) == -1)
- return nullptr;
+ if (sym_obj == nullptr
+ || PyList_Append (return_list.get (), sym_obj) == -1)
+ {
+ error_found = true;
+ return false;
+ }
}
}
- }
+
+ return true;
+ };
+
+ objfile->expand_symtabs_matching
+ (nullptr, &lookup_name, nullptr, callback,
+ SEARCH_STATIC_BLOCK, flags);
+
+ if (error_found)
+ return nullptr;
}
}
catch (const gdb_exception &except)

--
2.46.1