[PATCH v2 01/13] gdb: reimplement readnow_functions::search

Jan Vrany
2025-11-24T19:58:04Z

This commit reimplements readnow_functions::search without using DWARF
data structures. This will allow readnow_functions to be used in other
cases, for example in JIT reader.

I was bit surprised to realize that original code did not use lookup_name
to match symbols at all, and this calling listener with more CUs than
needed. I did try to use it as described in the comment but run into
issues (test failures) when completing symbols so I decided not to.

With this change, running all tests with -readnow shown no regressions
(on my Debian, x86_64 machine).

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33554
---
gdb/dwarf2/read.c | 49 +++++++++++++++++++++++++++++++----------------
1 file changed, 32 insertions(+), 17 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index b34c3463774..561e2dc2add 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1546,26 +1546,41 @@ struct readnow_functions : public dwarf2_base_index_functions
domain_search_flags domain,
search_symtabs_lang_matcher lang_matcher) override
{
- dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
- auto_bool_vector cus_to_skip;
- dw_search_file_matcher (per_objfile, cus_to_skip, file_matcher);
+ /* This invariant is documented in quick-functions.h. */
+ gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);

- for (const auto &per_cu : per_objfile->per_bfd->all_units)
+ for (compunit_symtab &cu : objfile->compunits ())
{
- QUIT;
-
- /* Skip various types of unit that should not be searched
- directly: partial units and dummy units. */
- if (/* Note that we request the non-strict unit type here. If
- there was an error while reading, like in
- dw-form-strx-out-of-bounds.exp, then the unit type may
- not be set. */
- per_cu->unit_type (false) == DW_UT_partial
- || per_cu->unit_type (false) == 0
- || per_objfile->get_symtab (per_cu.get ()) == nullptr)
+ if (lang_matcher != nullptr && !lang_matcher (cu.language ()))
continue;
- if (!dw2_search_one (per_cu.get (), per_objfile, cus_to_skip,
- file_matcher, listener, lang_matcher))
+
+ if (file_matcher != nullptr)
+ {
+ bool matched = false;
+ for (auto st : cu.filetabs ())
+ {
+ if (file_matcher (st->filename (), false))
+ {
+ matched = true;
+ break;
+ }
+ if ((basenames_may_differ
+ || file_matcher (lbasename (st->filename ()), true))
+ && file_matcher (symtab_to_fullname (st), false))
+ {
+ matched = true;
+ break;
+ }
+ }
+ if (!matched)
+ continue;
+ }
+
+ /* Here we simply call the listener (if any) without bothering to
+ consult lookup_name and symbol_matcher (if any). This should be
+ okay since i) all symtabs are already expanded and ii) listeners
+ iterate over matching symbols themselves. */
+ if (listener != nullptr && !listener (&cu))
return false;
}
return true;
--
2.51.0