public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 3/5] gdb: refactor objfile::find_and_add_separate_symbol_file
Date: Wed, 18 Oct 2023 11:53:21 +0100	[thread overview]
Message-ID: <108c51107d11eae41fd77d54d73613569875c4c7.1697626088.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1697626088.git.aburgess@redhat.com>

This is purely a refactoring commit.

This commit splits objfile::find_and_add_separate_symbol_file into
some separate helper functions.  My hope is that the steps for looking
up separate debug information are now clearer.

In a later commit I'm going to extend
objfile::find_and_add_separate_symbol_file, with some additional
logic, so starting with a simpler function will make the following
changes easier.

There should be no user visible changes after this commit.
---
 gdb/symfile-debug.c | 127 ++++++++++++++++++++++++++++----------------
 1 file changed, 82 insertions(+), 45 deletions(-)

diff --git a/gdb/symfile-debug.c b/gdb/symfile-debug.c
index 961ae2327f7..0a4499320c7 100644
--- a/gdb/symfile-debug.c
+++ b/gdb/symfile-debug.c
@@ -560,65 +560,102 @@ objfile::require_partial_symbols (bool verbose)
     }
 }
 
+/* Call LOOKUP_FUNC to find the filename of a file containing the separate
+   debug information matching OBJFILE.  If LOOKUP_FUNC does return a
+   filename then open this file and return a std::pair containing the
+   gdb_bfd_ref_ptr of the open file and the filename returned by
+   LOOKUP_FUNC, otherwise this function returns an empty pair; the first
+   item will be nullptr, and the second will be an empty string.
+
+   Any warnings generated by this function, or by calling LOOKUP_FUNC are
+   placed into WARNINGS, these warnings are only displayed to the user if
+   GDB is unable to find the separate debug information via any route.  */
+static std::pair<gdb_bfd_ref_ptr, std::string>
+simple_find_and_open_separate_symbol_file
+  (struct objfile *objfile,
+   std::string (*lookup_func) (struct objfile *, deferred_warnings *),
+   deferred_warnings *warnings)
+{
+  std::string filename = lookup_func (objfile, warnings);
+
+  if (!filename.empty ())
+    {
+      gdb_bfd_ref_ptr symfile_bfd
+	= symfile_bfd_open_no_error (filename.c_str ());
+      if (symfile_bfd != nullptr)
+	return { symfile_bfd, filename };
+    }
+
+  return {};
+}
+
+/* Lookup separate debug information for OBJFILE via debuginfod.  If
+   successful the debug information will be have been downloaded into the
+   debuginfod cache and this function will return a std::pair containing a
+   gdb_bfd_ref_ptr of the open debug information file and the filename for
+   the file within the debuginfod cache.  If no debug information could be
+   found then this function returns an empty pair; the first item will be
+   nullptr, and the second will be an empty string.  */
+
+static std::pair<gdb_bfd_ref_ptr, std::string>
+debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
+{
+  const struct bfd_build_id *build_id
+    = build_id_bfd_get (objfile->obfd.get ());
+  const char *filename = bfd_get_filename (objfile->obfd.get ());
+
+  if (build_id != nullptr)
+    {
+      gdb::unique_xmalloc_ptr<char> symfile_path;
+      scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size,
+						filename, &symfile_path));
+
+      if (fd.get () >= 0)
+	{
+	  /* File successfully retrieved from server.  */
+	  gdb_bfd_ref_ptr debug_bfd
+	    (symfile_bfd_open_no_error (symfile_path.get ()));
+
+	  if (debug_bfd != nullptr
+	      && build_id_verify (debug_bfd.get (),
+				  build_id->size, build_id->data))
+	    return { debug_bfd, std::string (symfile_path.get ()) };
+	}
+    }
+
+  return {};
+}
+
 /* See objfiles.h.  */
 
 bool
 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
 {
-  bool has_dwarf2 = true;
-
+  bool has_dwarf2 = false;
   deferred_warnings warnings;
 
-  std::string debugfile
-    = find_separate_debug_file_by_buildid (this, &warnings);
+  std::pair<gdb_bfd_ref_ptr, std::string> result;
 
-  if (debugfile.empty ())
-    debugfile = find_separate_debug_file_by_debuglink (this, &warnings);
-
-  if (!debugfile.empty ())
-    {
-      gdb_bfd_ref_ptr debug_bfd
-	(symfile_bfd_open_no_error (debugfile.c_str ()));
+  result = simple_find_and_open_separate_symbol_file
+    (this, find_separate_debug_file_by_buildid, &warnings);
 
-      if (debug_bfd != nullptr)
-	symbol_file_add_separate (debug_bfd, debugfile.c_str (),
-				  symfile_flags, this);
-    }
-  else
-    {
-      has_dwarf2 = false;
-      const struct bfd_build_id *build_id
-	= build_id_bfd_get (this->obfd.get ());
-      const char *filename = bfd_get_filename (this->obfd.get ());
+  if (result.first == nullptr)
+    result = simple_find_and_open_separate_symbol_file
+      (this, find_separate_debug_file_by_debuglink, &warnings);
 
-      if (build_id != nullptr)
-	{
-	  gdb::unique_xmalloc_ptr<char> symfile_path;
-	  scoped_fd fd (debuginfod_debuginfo_query (build_id->data,
-						    build_id->size,
-						    filename,
-						    &symfile_path));
+  if (result.first == nullptr)
+    result = debuginfod_find_and_open_separate_symbol_file (this);
 
-	  if (fd.get () >= 0)
-	    {
-	      /* File successfully retrieved from server.  */
-	      gdb_bfd_ref_ptr debug_bfd
-		(symfile_bfd_open_no_error (symfile_path.get ()));
-
-	      if (debug_bfd != nullptr
-		  && build_id_verify (debug_bfd.get (), build_id->size,
-				      build_id->data))
-		{
-		  symbol_file_add_separate (debug_bfd, symfile_path.get (),
-					    symfile_flags, this);
-		  has_dwarf2 = true;
-		}
-	    }
-	}
+  if (result.first != nullptr)
+    {
+      symbol_file_add_separate (result.first, result.second.c_str (),
+				symfile_flags, this);
+      has_dwarf2 = true;
     }
+
   /* If all the methods to collect the debuginfo failed, print the
      warnings, this is a no-op if there are no warnings.  */
-  if (debugfile.empty () && !has_dwarf2)
+  if (!has_dwarf2)
     warnings.emit ();
 
   return has_dwarf2;
-- 
2.25.4


  parent reply	other threads:[~2023-10-18 10:53 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-18 10:53 [PATCH 0/5] New Python hook for missing debug information Andrew Burgess
2023-10-18 10:53 ` [PATCH 1/5] gdb/coffread: bring separate debug file logic into line with elfread.c Andrew Burgess
2023-10-20 17:35   ` Tom Tromey
2023-10-24 11:59     ` Andrew Burgess
2023-10-18 10:53 ` [PATCH 2/5] gdb: merge debug symbol file lookup code from coffread & elfread paths Andrew Burgess
2023-10-18 13:18   ` Jon Turney
2023-10-18 17:25     ` Andrew Burgess
2023-10-18 10:53 ` Andrew Burgess [this message]
2023-10-20 17:50   ` [PATCH 3/5] gdb: refactor objfile::find_and_add_separate_symbol_file Tom Tromey
2023-10-24 12:03     ` Andrew Burgess
2023-11-08 15:22     ` Andrew Burgess
2023-10-18 10:53 ` [PATCH 4/5] gdb: add an extension language hook for missing debug info Andrew Burgess
2023-10-18 10:53 ` [PATCH 5/5] gdb: implement missing debug handler hook for Python Andrew Burgess
2023-10-18 12:08   ` Eli Zaretskii
2023-11-08 15:48 ` [PATCHv2 0/5] New Python hook for missing debug information Andrew Burgess
2023-11-08 15:48   ` [PATCHv2 1/5] gdb/coffread: bring separate debug file logic into line with elfread.c Andrew Burgess
2023-11-08 15:48   ` [PATCHv2 2/5] gdb: merge debug symbol file lookup code from coffread & elfread paths Andrew Burgess
2023-11-08 15:48   ` [PATCHv2 3/5] gdb: refactor objfile::find_and_add_separate_symbol_file Andrew Burgess
2023-11-08 15:48   ` [PATCHv2 4/5] gdb: add an extension language hook for missing debug info Andrew Burgess
2023-11-08 15:48   ` [PATCHv2 5/5] gdb: implement missing debug handler hook for Python Andrew Burgess
2023-11-12 22:38     ` Tom Tromey
2023-11-15 12:36     ` Tom de Vries
2023-11-16 10:59       ` Andrew Burgess
2023-11-16 11:16         ` Tom de Vries
2023-11-16 17:21           ` Andrew Burgess
2023-11-16 15:26         ` Tom Tromey
2023-11-12 22:39   ` [PATCHv2 0/5] New Python hook for missing debug information Tom Tromey
2023-11-13 16:04     ` Andrew Burgess
2023-11-13 17:18       ` Tom Tromey
2023-11-12 22:40   ` 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=108c51107d11eae41fd77d54d73613569875c4c7.1697626088.git.aburgess@redhat.com \
    --to=aburgess@redhat.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).