public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v4 02/19] Pre-read DWZ section data
Date: Sun, 10 Dec 2023 14:41:10 -0700	[thread overview]
Message-ID: <20231210-t-bg-dwarf-reading-v4-2-b978c32fd12f@tromey.com> (raw)
In-Reply-To: <20231210-t-bg-dwarf-reading-v4-0-b978c32fd12f@tromey.com>

This changes the DWZ code to pre-read the section data and somewhat
simplify the DWZ API.  This makes it easier to add the bfd_cache_close
call to the new dwarf2_read_dwz_file function -- after this is done,
there shouldn't be a reason to keep the BFD's file descriptor open.
---
 gdb/dwarf2/dwz.c              | 90 ++++++++++++++++++++-----------------------
 gdb/dwarf2/dwz.h              | 13 +++++--
 gdb/dwarf2/macro.c            |  2 -
 gdb/dwarf2/read-debug-names.c |  2 +-
 gdb/dwarf2/read.c             | 13 +------
 gdb/dwarf2/read.h             |  3 +-
 6 files changed, 56 insertions(+), 67 deletions(-)

diff --git a/gdb/dwarf2/dwz.c b/gdb/dwarf2/dwz.c
index edd5b56d778..13f95801ebb 100644
--- a/gdb/dwarf2/dwz.c
+++ b/gdb/dwarf2/dwz.c
@@ -53,49 +53,35 @@ dwz_file::read_string (struct objfile *objfile, LONGEST str_offset)
 /* A helper function to find the sections for a .dwz file.  */
 
 static void
-locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
+locate_dwz_sections (struct objfile *objfile, bfd *abfd, asection *sectp,
+		     dwz_file *dwz_file)
 {
+  dwarf2_section_info *sect = nullptr;
+
   /* Note that we only support the standard ELF names, because .dwz
      is ELF-only (at the time of writing).  */
   if (dwarf2_elf_names.abbrev.matches (sectp->name))
-    {
-      dwz_file->abbrev.s.section = sectp;
-      dwz_file->abbrev.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->abbrev;
   else if (dwarf2_elf_names.info.matches (sectp->name))
-    {
-      dwz_file->info.s.section = sectp;
-      dwz_file->info.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->info;
   else if (dwarf2_elf_names.str.matches (sectp->name))
-    {
-      dwz_file->str.s.section = sectp;
-      dwz_file->str.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->str;
   else if (dwarf2_elf_names.line.matches (sectp->name))
-    {
-      dwz_file->line.s.section = sectp;
-      dwz_file->line.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->line;
   else if (dwarf2_elf_names.macro.matches (sectp->name))
-    {
-      dwz_file->macro.s.section = sectp;
-      dwz_file->macro.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->macro;
   else if (dwarf2_elf_names.gdb_index.matches (sectp->name))
-    {
-      dwz_file->gdb_index.s.section = sectp;
-      dwz_file->gdb_index.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->gdb_index;
   else if (dwarf2_elf_names.debug_names.matches (sectp->name))
-    {
-      dwz_file->debug_names.s.section = sectp;
-      dwz_file->debug_names.size = bfd_section_size (sectp);
-    }
+    sect = &dwz_file->debug_names;
   else if (dwarf2_elf_names.types.matches (sectp->name))
+    sect = &dwz_file->types;
+
+  if (sect != nullptr)
     {
-      dwz_file->types.s.section = sectp;
-      dwz_file->types.size = bfd_section_size (sectp);
+      sect->s.section = sectp;
+      sect->size = bfd_section_size (sectp);
+      sect->read (objfile);
     }
 }
 
@@ -190,20 +176,13 @@ dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
 
 /* See dwz.h.  */
 
-struct dwz_file *
-dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
+void
+dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
 {
   bfd_size_type buildid_len_arg;
   size_t buildid_len;
   bfd_byte *buildid;
-
-  if (per_bfd->dwz_file.has_value ())
-    {
-      dwz_file *result = per_bfd->dwz_file->get ();
-      if (require && result == nullptr)
-	error (_("could not read '.gnu_debugaltlink' section"));
-      return result;
-    }
+  dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
 
   /* This may query the user via the debuginfod support, so it may
      only be run in the main thread.  */
@@ -219,11 +198,7 @@ dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
   if (data == NULL)
     {
       if (bfd_get_error () == bfd_error_no_error)
-	{
-	  if (!require)
-	    return nullptr;
-	  error (_("could not read '.gnu_debugaltlink' section"));
-	}
+	return;
       error (_("could not read '.gnu_debugaltlink' section: %s"),
 	     bfd_errmsg (bfd_get_error ()));
     }
@@ -292,9 +267,28 @@ dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
     (new struct dwz_file (std::move (dwz_bfd)));
 
   for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
-    locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
+    locate_dwz_sections (per_objfile->objfile, result->dwz_bfd.get (),
+			 sec, result.get ());
 
   gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
+  bfd_cache_close (result->dwz_bfd.get ());
+
   per_bfd->dwz_file = std::move (result);
-  return per_bfd->dwz_file->get ();
+}
+
+/* See dwz.h.  */
+
+struct dwz_file *
+dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
+{
+  gdb_assert (!require || per_bfd->dwz_file.has_value ());
+
+  dwz_file *result = nullptr;
+  if (per_bfd->dwz_file.has_value ())
+    {
+      result = per_bfd->dwz_file->get ();
+      if (require && result == nullptr)
+	error (_("could not read '.gnu_debugaltlink' section"));
+    }
+  return result;
 }
diff --git a/gdb/dwarf2/dwz.h b/gdb/dwarf2/dwz.h
index 5f61b5e1216..a427cda902d 100644
--- a/gdb/dwarf2/dwz.h
+++ b/gdb/dwarf2/dwz.h
@@ -65,13 +65,20 @@ struct dwz_file
   const char *read_string (struct objfile *objfile, LONGEST str_offset);
 };
 
-/* Open the separate '.dwz' debug file, if needed.  If there is no
+/* Return the separate '.dwz' debug file.  If there is no
    .gnu_debugaltlink section in the file, then the result depends on
    REQUIRE: if REQUIRE is true, then error; if REQUIRE is false,
-   return NULL.  Always error if there is such a section but the file
-   cannot be found.  */
+   return NULL.  */
 
 extern dwz_file *dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd,
 				      bool require = false);
 
+/* Open the separate '.dwz' debug file, if needed.  This just sets the
+   appropriate field in the per-BFD structure.  If the DWZ file
+   exists, the relevant sections are read in as well.  Throws an error
+   if the .gnu_debugaltlink section exists but the file cannot be
+   found.  */
+
+extern void dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile);
+
 #endif /* GDB_DWARF2_DWZ_H */
diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index 7d86d16d0e3..2ee9f8c1009 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -735,8 +735,6 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 		dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd,
 						     true);
 
-		dwz->macro.read (objfile);
-
 		include_section = &dwz->macro;
 		include_bfd = include_section->get_bfd_owner ();
 		include_mac_end = dwz->macro.buffer + dwz->macro.size;
diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c
index 59c49da5553..e912c2a1114 100644
--- a/gdb/dwarf2/read-debug-names.c
+++ b/gdb/dwarf2/read-debug-names.c
@@ -467,7 +467,7 @@ dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
 	}
     }
 
-  create_all_units (per_objfile, false);
+  create_all_units (per_objfile);
   if (!check_cus_from_debug_names (per_bfd, *map, dwz_map))
     {
       per_bfd->all_units.clear ();
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 9a5f52d0f7e..9fd61eb0777 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -3201,7 +3201,7 @@ dwarf2_initialize_objfile (struct objfile *objfile)
      main thread.  */
   try
     {
-      dwarf2_get_dwz_file (per_bfd);
+      dwarf2_read_dwz_file (per_objfile);
     }
   catch (const gdb_exception_error &err)
     {
@@ -5101,7 +5101,7 @@ finalize_all_units (dwarf2_per_bfd *per_bfd)
 /* See read.h.  */
 
 void
-create_all_units (dwarf2_per_objfile *per_objfile, bool pre_read_p)
+create_all_units (dwarf2_per_objfile *per_objfile)
 {
   htab_up types_htab;
   gdb_assert (per_objfile->per_bfd->all_units.empty ());
@@ -5117,15 +5117,6 @@ create_all_units (dwarf2_per_objfile *per_objfile, bool pre_read_p)
   dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
   if (dwz != NULL)
     {
-      if (pre_read_p)
-	{
-	  /* Pre-read the sections we'll need to construct an index.  */
-	  struct objfile *objfile = per_objfile->objfile;
-	  dwz->abbrev.read (objfile);
-	  dwz->info.read (objfile);
-	  dwz->str.read (objfile);
-	  dwz->line.read (objfile);
-	}
       read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
 				    types_htab, rcuh_kind::COMPILE);
 
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index 6f97f641c8e..4ab869cdd29 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -966,8 +966,7 @@ extern void finalize_all_units (dwarf2_per_bfd *per_bfd);
 
 /* Create a list of all compilation units in OBJFILE.  */
 
-extern void create_all_units (dwarf2_per_objfile *per_objfile,
-			      bool pre_read_p = true);
+extern void create_all_units (dwarf2_per_objfile *per_objfile);
 
 /* Create a quick_file_names hash table.  */
 

-- 
2.43.0


  parent reply	other threads:[~2023-12-10 21:41 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-10 21:41 [PATCH v4 00/19] Index DWARF in the background Tom Tromey
2023-12-10 21:41 ` [PATCH v4 01/19] Don't use objfile::intern in DWO code Tom Tromey
2024-01-09 18:44   ` Simon Marchi
2024-01-09 19:12     ` Tom Tromey
2024-01-09 19:51       ` Tom Tromey
2023-12-10 21:41 ` Tom Tromey [this message]
2023-12-10 21:41 ` [PATCH v4 03/19] Add a couple of bfd_cache_close calls Tom Tromey
2023-12-10 21:41 ` [PATCH v4 04/19] Add thread-safety to gdb's BFD wrappers Tom Tromey
2023-12-10 21:41 ` [PATCH v4 05/19] Refactor complaint thread-safety approach Tom Tromey
2023-12-10 21:41 ` [PATCH v4 06/19] Add deferred_warnings parameter to read_addrmap_from_aranges Tom Tromey
2023-12-10 21:41 ` [PATCH v4 07/19] Add quick_symbol_functions::compute_main_name Tom Tromey
2023-12-10 21:41 ` [PATCH v4 08/19] Add gdb::task_group Tom Tromey
2023-12-18 14:55   ` Tom Tromey
2023-12-10 21:41 ` [PATCH v4 09/19] Move cooked_index_storage to cooked-index.h Tom Tromey
2023-12-10 21:41 ` [PATCH v4 10/19] Add "maint set dwarf synchronous" Tom Tromey
2023-12-10 21:41 ` [PATCH v4 11/19] Change how cooked index waits for threads Tom Tromey
2023-12-10 21:41 ` [PATCH v4 12/19] Do more DWARF reading in the background Tom Tromey
2023-12-10 21:41 ` [PATCH v4 13/19] Simplify the public DWARF API Tom Tromey
2023-12-10 21:41 ` [PATCH v4 14/19] Remove two quick_symbol_functions methods Tom Tromey
2023-12-10 21:41 ` [PATCH v4 15/19] Change current_language to be a macro Tom Tromey
2023-12-10 21:41 ` [PATCH v4 16/19] Lazy language setting Tom Tromey
2023-12-10 21:41 ` [PATCH v4 17/19] Optimize lookup_minimal_symbol_text Tom Tromey
2023-12-10 21:41 ` [PATCH v4 18/19] Avoid language-based lookups in startup path Tom Tromey
2023-12-10 21:41 ` [PATCH v4 19/19] Back out some parallel_for_each features Tom Tromey
2024-01-09  1:08 ` [PATCH v4 00/19] Index DWARF in the background Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231210-t-bg-dwarf-reading-v4-2-b978c32fd12f@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).