[10/28] Have expand_symtabs_matching work for already-expanded CUs

Message ID 20250311-search-in-psyms-v1-10-d73d9be20983@tromey.com
State New
Headers
Series Search symbols via quick API |

Commit Message

Tom Tromey March 11, 2025, 2:12 p.m. UTC
  Currently, gdb will search the already-expanded symtabs in one loop,
and then also expand matching symtabs in another loop.  However, this
is somewhat inefficient -- when searching the already-expanded
symtabs, all such symtabs are examined.  However, the various "quick"
implementations already know which subset of symtabs might have a
match.

This changes the contract of expand_symtabs_matching to also call the
callback for an already-expanded symtab.  With this change, the number
of searched symtabs should sometimes be reduced.  This also cuts down
on the amount of redundant code.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
---
 gdb/dwarf2/read.c | 42 ++++++++++++++++++++++++++++--------------
 gdb/psymtab.c     |  3 ---
 2 files changed, 28 insertions(+), 17 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index bd0e524668f7f8966c1166533601961e3e2dbb86..78e4a3446e16a381b22311ce2bebb0d948771029 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1575,6 +1575,23 @@  struct readnow_functions : public dwarf2_base_index_functions
      domain_search_flags domain,
      expand_symtabs_lang_matcher lang_matcher) override
   {
+    dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
+    auto_bool_vector marked;
+    dw_expand_symtabs_matching_file_matcher (per_objfile, marked,
+					     file_matcher);
+
+    for (const auto &per_cu : per_objfile->per_bfd->all_units)
+      {
+	QUIT;
+
+	if (per_cu->is_debug_types)
+	  continue;
+	if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
+					      marked, file_matcher,
+					      expansion_notify,
+					      lang_matcher))
+	  return false;
+      }
     return true;
   }
 };
@@ -2002,13 +2019,15 @@  dw2_expand_symtabs_matching_one
 	return true;
     }
 
-  bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
   compunit_symtab *symtab
     = dw2_instantiate_symtab (per_cu, per_objfile, false);
   gdb_assert (symtab != nullptr);
 
-  if (expansion_notify != NULL && symtab_was_null)
-    return expansion_notify (symtab);
+  if (expansion_notify != nullptr)
+    {
+      marked.set (per_cu->index, true);
+      return expansion_notify (symtab);
+    }
 
   return true;
 }
@@ -2040,13 +2059,6 @@  dw_expand_symtabs_matching_file_matcher
 	  continue;
 	}
 
-      /* We only need to look at symtabs not already expanded.  */
-      if (per_objfile->symtab_set_p (per_cu.get ()))
-	{
-	  marked.set (per_cu->index, true);
-	  continue;
-	}
-
       if (per_cu->fnd != nullptr)
 	{
 	  file_and_directory *fnd = per_cu->fnd.get ();
@@ -4414,6 +4426,12 @@  load_full_comp_unit (dwarf2_per_cu *this_cu,
   if (reader.is_dummy ())
     return;
 
+  /* We always need the file names filled in so that
+     expand_symtabs_matching can match filenames.  It's convenient to
+     do this here.  */
+  if (!this_cu->files_read)
+    dw2_get_file_names_reader (reader.cu (), reader.top_level_die ());
+
   struct dwarf2_cu *cu = reader.cu ();
   const gdb_byte *info_ptr = reader.info_ptr ();
 
@@ -14865,10 +14883,6 @@  cooked_index_functions::expand_symtabs_matching
 	{
 	  QUIT;
 
-	  /* No need to consider symbols from expanded CUs.  */
-	  if (per_objfile->symtab_set_p (entry->per_cu))
-	    continue;
-
 	  /* We don't need to consider symbols from some CUs.  */
 	  if (marked.is_set (entry->per_cu->index))
 	    continue;
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 28455ba0bc58990a515f0899a873c9995fb4cca8..d12f8f1c5c18018fe96b0d647f3957dfd0f4d594 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -914,9 +914,6 @@  psymbol_functions::expand_symtabs_matching
     {
       QUIT;
 
-      if (ps->readin_p (objfile))
-	continue;
-
       if (file_matcher)
 	{
 	  bool match;