Simplify DWARF reader initialization

Message ID 20210327230318.582968-1-tom@tromey.com
State New
Headers show
Series
  • Simplify DWARF reader initialization
Related show

Commit Message

Tom Tromey March 27, 2021, 11:03 p.m.
Now that the quick functions are separate from the object file format,
there's no need to have elfread.c push a new entry on the objfile 'qf'
list.  Instead, this detail can be pushed into the DWARF reader.  That
is what this patch implements.

I wasn't sure whether lazy reading still makes sense or not.  It's
still only used by ELF, and only in certain situations (like vfork, I
think).  It may not be carrying its weight, so we may want to consider
removing this in the future.

Also, I'm unclear on why the various indices are only used for ELF.
This seems sub-optimal.  However, I haven't tried to address that
here.

gdb/ChangeLog
2021-03-27  Tom Tromey  <tom@tromey.com>

	* elfread.c (can_lazily_read_symbols): Move to dwarf2/read.c.
	(elf_symfile_read): Simplify.
	* dwarf2/read.c (struct lazy_dwarf_reader): Move from elfread.c.
	(make_lazy_dwarf_reader): New function.
	(make_dwarf_gdb_index, make_dwarf_debug_names): Now static.
	(dwarf2_initialize_objfile): Return void.  Remove index_kind
	parameter.  Push on 'qf' list.
	* dwarf2/public.h (dwarf2_initialize_objfile): Change return
	type.  Remove 'index_kind' parameter.
	(make_dwarf_gdb_index, make_dwarf_debug_names): Don't declare.
---
 gdb/ChangeLog       | 13 ++++++++
 gdb/dwarf2/public.h | 11 ++-----
 gdb/dwarf2/read.c   | 73 ++++++++++++++++++++++++++++++---------------
 gdb/elfread.c       | 37 +----------------------
 4 files changed, 66 insertions(+), 68 deletions(-)

-- 
2.26.2

Comments

Kumar N, Bhuvanendra via Gdb-patches March 28, 2021, 4:06 p.m. | #1
On 2021-03-27 7:03 p.m., Tom Tromey wrote:
> Now that the quick functions are separate from the object file format,

> there's no need to have elfread.c push a new entry on the objfile 'qf'

> list.  Instead, this detail can be pushed into the DWARF reader.  That

> is what this patch implements.

> 

> I wasn't sure whether lazy reading still makes sense or not.  It's

> still only used by ELF, and only in certain situations (like vfork, I

> think).  It may not be carrying its weight, so we may want to consider

> removing this in the future.

> 

> Also, I'm unclear on why the various indices are only used for ELF.

> This seems sub-optimal.  However, I haven't tried to address that

> here.


I think this makes sense, I always found weird that part of elfread.c that
knew too much about the dwarf reader.

Simon
Tom Tromey March 28, 2021, 4:43 p.m. | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:


Simon> I think this makes sense, I always found weird that part of elfread.c that
Simon> knew too much about the dwarf reader.

I'm going to check it in, then.  Thanks.

Tom

Patch

diff --git a/gdb/dwarf2/public.h b/gdb/dwarf2/public.h
index e6653f4f38d..33bb5d48217 100644
--- a/gdb/dwarf2/public.h
+++ b/gdb/dwarf2/public.h
@@ -34,18 +34,13 @@  enum class dw_index_kind
   DEBUG_NAMES,
 };
 
-/* Initialize for reading DWARF for OBJFILE.  Return false if this
-   file will use psymtabs, or true if using an index, in which case
-   *INDEX_KIND is set to the index variant in use.  */
-extern bool dwarf2_initialize_objfile (struct objfile *objfile,
-				       dw_index_kind *index_kind);
+/* Initialize for reading DWARF for OBJFILE, and push the appropriate
+   entry on the objfile's "qf" list.  */
+extern void dwarf2_initialize_objfile (struct objfile *objfile);
 
 struct psymbol_functions;
 extern void dwarf2_build_psymtabs (struct objfile *,
 				   psymbol_functions *psf = nullptr);
 extern void dwarf2_build_frame_info (struct objfile *);
 
-extern quick_symbol_functions_up make_dwarf_gdb_index ();
-extern quick_symbol_functions_up make_dwarf_debug_names ();
-
 #endif /* DWARF2_PUBLIC_H */
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index b324541ee9f..1b98b758c35 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -2191,6 +2191,30 @@  struct dwarf2_per_cu_quick_data
   unsigned int no_file_data : 1;
 };
 
+/* A subclass of psymbol_functions that arranges to read the DWARF
+   partial symbols when needed.  */
+struct lazy_dwarf_reader : public psymbol_functions
+{
+  using psymbol_functions::psymbol_functions;
+
+  bool can_lazily_read_symbols () override
+  {
+    return true;
+  }
+
+  void read_partial_symbols (struct objfile *objfile) override
+  {
+    if (dwarf2_has_info (objfile, nullptr))
+      dwarf2_build_psymtabs (objfile, this);
+  }
+};
+
+static quick_symbol_functions_up
+make_lazy_dwarf_reader ()
+{
+  return quick_symbol_functions_up (new lazy_dwarf_reader);
+}
+
 struct dwarf2_base_index_functions : public quick_symbol_functions
 {
   bool has_symbols (struct objfile *objfile) override;
@@ -2292,13 +2316,13 @@  struct dwarf2_debug_names_index : public dwarf2_base_index_functions
      enum search_domain kind) override;
 };
 
-quick_symbol_functions_up
+static quick_symbol_functions_up
 make_dwarf_gdb_index ()
 {
   return quick_symbol_functions_up (new dwarf2_gdb_index);
 }
 
-quick_symbol_functions_up
+static quick_symbol_functions_up
 make_dwarf_debug_names ()
 {
   return quick_symbol_functions_up (new dwarf2_debug_names_index);
@@ -5993,10 +6017,10 @@  get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
   return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
 }
 
-/* See symfile.h.  */
+/* See dwarf2/public.h.  */
 
-bool
-dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
+void
+dwarf2_initialize_objfile (struct objfile *objfile)
 {
   dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
@@ -6016,9 +6040,9 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
       if (per_bfd->using_index)
 	{
 	  dwarf_read_debug_printf ("using_index already set");
-	  *index_kind = dw_index_kind::GDB_INDEX;
 	  per_objfile->resize_symtabs ();
-	  return true;
+	  objfile->qf.push_front (make_dwarf_gdb_index ());
+	  return;
 	}
 
       per_bfd->using_index = 1;
@@ -6037,11 +6061,11 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
 					    struct dwarf2_per_cu_quick_data);
 	}
 
-      /* Return 1 so that gdb sees the "quick" functions.  However,
-	 these functions will be no-ops because we will have expanded
-	 all symtabs.  */
-      *index_kind = dw_index_kind::GDB_INDEX;
-      return true;
+      /* Arrange for gdb to see the "quick" functions.  However, these
+	 functions will be no-ops because we will have expanded all
+	 symtabs.  */
+      objfile->qf.push_front (make_dwarf_gdb_index ());
+      return;
     }
 
   /* Was a debug names index already read when we processed an objfile sharing
@@ -6049,9 +6073,9 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
   if (per_bfd->debug_names_table != nullptr)
     {
       dwarf_read_debug_printf ("re-using shared debug names table");
-      *index_kind = dw_index_kind::DEBUG_NAMES;
       per_objfile->resize_symtabs ();
-      return true;
+      objfile->qf.push_front (make_dwarf_debug_names ());
+      return;
     }
 
   /* Was a GDB index already read when we processed an objfile sharing
@@ -6059,9 +6083,9 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
   if (per_bfd->index_table != nullptr)
     {
       dwarf_read_debug_printf ("re-using shared index table");
-      *index_kind = dw_index_kind::GDB_INDEX;
       per_objfile->resize_symtabs ();
-      return true;
+      objfile->qf.push_front (make_dwarf_gdb_index ());
+      return;
     }
 
   /* There might already be partial symtabs built for this BFD.  This happens
@@ -6072,15 +6096,16 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
   if (per_bfd->partial_symtabs != nullptr)
     {
       dwarf_read_debug_printf ("re-using shared partial symtabs");
-      return false;
+      objfile->qf.push_front (make_lazy_dwarf_reader ());
+      return;
     }
 
   if (dwarf2_read_debug_names (per_objfile))
     {
       dwarf_read_debug_printf ("found debug names");
-      *index_kind = dw_index_kind::DEBUG_NAMES;
       per_objfile->resize_symtabs ();
-      return true;
+      objfile->qf.push_front (make_dwarf_debug_names ());
+      return;
     }
 
   if (dwarf2_read_gdb_index (per_objfile,
@@ -6088,9 +6113,9 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
 			     get_gdb_index_contents_from_section<dwz_file>))
     {
       dwarf_read_debug_printf ("found gdb index from file");
-      *index_kind = dw_index_kind::GDB_INDEX;
       per_objfile->resize_symtabs ();
-      return true;
+      objfile->qf.push_front (make_dwarf_gdb_index ());
+      return;
     }
 
   /* ... otherwise, try to find the index in the index cache.  */
@@ -6100,13 +6125,13 @@  dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
     {
       dwarf_read_debug_printf ("found gdb index from cache");
       global_index_cache.hit ();
-      *index_kind = dw_index_kind::GDB_INDEX;
       per_objfile->resize_symtabs ();
-      return true;
+      objfile->qf.push_front (make_dwarf_gdb_index ());
+      return;
     }
 
   global_index_cache.miss ();
-  return false;
+  objfile->qf.push_front (make_lazy_dwarf_reader ());
 }
 
 
diff --git a/gdb/elfread.c b/gdb/elfread.c
index f5de913a5cc..1dea226242d 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -53,24 +53,6 @@ 
 #include "debuginfod-support.h"
 #include "dwarf2/public.h"
 
-/* A subclass of psymbol_functions that arranges to read the DWARF
-   partial symbols when needed.  */
-struct lazy_dwarf_reader : public psymbol_functions
-{
-  using psymbol_functions::psymbol_functions;
-
-  bool can_lazily_read_symbols () override
-  {
-    return true;
-  }
-
-  void read_partial_symbols (struct objfile *objfile) override
-  {
-    if (dwarf2_has_info (objfile, nullptr))
-      dwarf2_build_psymtabs (objfile, this);
-  }
-};
-
 /* The struct elfinfo is available only during ELF symbol table and
    psymtab reading.  It is destroyed at the completion of psymtab-reading.
    It's local to elf_symfile_read.  */
@@ -1273,24 +1255,7 @@  elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
     }
 
   if (dwarf2_has_info (objfile, NULL, true))
-    {
-      dw_index_kind index_kind;
-
-      if (dwarf2_initialize_objfile (objfile, &index_kind))
-	{
-	  switch (index_kind)
-	    {
-	    case dw_index_kind::GDB_INDEX:
-	      objfile->qf.push_front (make_dwarf_gdb_index ());
-	      break;
-	    case dw_index_kind::DEBUG_NAMES:
-	      objfile->qf.push_front (make_dwarf_debug_names ());
-	      break;
-	    }
-	}
-      else
-	objfile->qf.emplace_front (new lazy_dwarf_reader);
-    }
+    dwarf2_initialize_objfile (objfile);
   /* If the file has its own symbol tables it has no separate debug
      info.  `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to
      SYMTABS/PSYMTABS.  `.gnu_debuglink' may no longer be present with