aboutsummaryrefslogtreecommitdiff
path: root/gdb/build-id.c
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2024-07-31 15:54:52 +0100
committerAndrew Burgess <aburgess@redhat.com>2024-11-10 10:18:22 +0000
commitef1a41f20b7ea4799e09b37312dc051245435fd1 (patch)
tree22c04a38ed29a6dbc56bd67e0e3d51ddbe682cfd /gdb/build-id.c
parent629bcc68d799ff77a5f174e2f5300f9a26a83521 (diff)
downloadbinutils-ef1a41f20b7ea4799e09b37312dc051245435fd1.zip
binutils-ef1a41f20b7ea4799e09b37312dc051245435fd1.tar.gz
binutils-ef1a41f20b7ea4799e09b37312dc051245435fd1.tar.bz2
gdb: add extension hook ext_lang_find_objfile_from_buildid
Add a new ext_lang_find_objfile_from_buildid function which is called from find_objfile_by_build_id and gives extension languages a chance to find missing objfiles. This commit adds the ext_lang_find_objfile_from_buildid function and the extension_language_ops::find_objfile_from_buildid() hook, but does not implement the hook for any extension languages, that will come in the next commit. This commit does rewrite find_objfile_by_build_id (build-id.c) to call the new hook though. The basic steps of find_objfile_by_build_id are now this: 1. Try to find the missing objfile using the build-id by looking in the debug-file-directory's .build-id/ sub-directory. If we find the file then we're done. 2. Ask debuginfod to download the missing file for us. If we download the file successfully then we're done. 3. Ask the extension language hook to find the file for us. If the extension language asks us to try again then we repeat step (1) only and if we still don't have the file, we move to step (4). If the extension language told us where the file is then we use that file and we're done. 4. We didn't find the file. Carry on without it. Only step (3) is new in this logic, everything else was already done. There are no tests added here as we can't currently write an extension language callback. The next commit will add the tests. Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/build-id.c')
-rw-r--r--gdb/build-id.c88
1 files changed, 65 insertions, 23 deletions
diff --git a/gdb/build-id.c b/gdb/build-id.c
index 6266be1..9d4b005 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -28,6 +28,7 @@
#include "cli/cli-style.h"
#include "gdbsupport/scoped_fd.h"
#include "debuginfod-support.h"
+#include "extension.h"
/* See build-id.h. */
@@ -343,32 +344,73 @@ find_separate_debug_file_by_buildid (struct objfile *objfile,
/* See build-id.h. */
gdb_bfd_ref_ptr
-find_objfile_by_build_id (const bfd_build_id *build_id,
+find_objfile_by_build_id (program_space *pspace,
+ const bfd_build_id *build_id,
const char *expected_filename)
{
- /* Try to find the executable (or shared object) by looking for a
- (sym)link on disk from the build-id to the object file. */
- gdb_bfd_ref_ptr abfd = build_id_to_exec_bfd (build_id->size,
- build_id->data);
-
- if (abfd != nullptr)
- return abfd;
-
- /* Attempt to query debuginfod for the executable. */
- gdb::unique_xmalloc_ptr<char> path;
- scoped_fd fd = debuginfod_exec_query (build_id->data, build_id->size,
- expected_filename, &path);
- if (fd.get () >= 0)
+ gdb_bfd_ref_ptr abfd;
+
+ for (unsigned attempt = 0, max_attempts = 1;
+ attempt < max_attempts && abfd == nullptr;
+ ++attempt)
{
- abfd = gdb_bfd_open (path.get (), gnutarget);
-
- if (abfd == nullptr)
- warning (_("\"%ps\" from debuginfod cannot be opened as bfd: %s"),
- styled_string (file_name_style.style (), path.get ()),
- gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
- else if (!build_id_verify (abfd.get (), build_id->size,
- build_id->data))
- abfd = nullptr;
+ /* Try to find the executable (or shared object) by looking for a
+ (sym)link on disk from the build-id to the object file. */
+ abfd = build_id_to_exec_bfd (build_id->size, build_id->data);
+
+ if (abfd != nullptr || attempt > 0)
+ break;
+
+ /* Attempt to query debuginfod for the executable. This will only
+ get run during the first attempt, if an extension language hook
+ (see below) asked for a second attempt then we will have already
+ broken out of the loop above. */
+ gdb::unique_xmalloc_ptr<char> path;
+ scoped_fd fd = debuginfod_exec_query (build_id->data, build_id->size,
+ expected_filename, &path);
+ if (fd.get () >= 0)
+ {
+ abfd = gdb_bfd_open (path.get (), gnutarget);
+
+ if (abfd == nullptr)
+ warning (_("\"%ps\" from debuginfod cannot be opened as bfd: %s"),
+ styled_string (file_name_style.style (), path.get ()),
+ gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
+ else if (!build_id_verify (abfd.get (), build_id->size,
+ build_id->data))
+ abfd = nullptr;
+ }
+
+ if (abfd != nullptr)
+ break;
+
+ ext_lang_missing_file_result ext_result
+ = ext_lang_find_objfile_from_buildid (pspace, build_id,
+ expected_filename);
+ if (!ext_result.filename ().empty ())
+ {
+ /* The extension identified the file for us. */
+ abfd = gdb_bfd_open (ext_result.filename ().c_str (), gnutarget);
+ if (abfd == nullptr)
+ {
+ warning (_("\"%ps\" from extension cannot be opened as bfd: %s"),
+ styled_string (file_name_style.style (),
+ ext_result.filename ().c_str ()),
+ gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ());
+ break;
+ }
+
+ /* If the extension gave us a path to a file then we always
+ assume that it is the correct file, we do no additional check
+ of its build-id. */
+ }
+ else if (ext_result.try_again ())
+ {
+ /* The extension might have installed the file in the expected
+ location, we should try again. */
+ max_attempts = 2;
+ continue;
+ }
}
return abfd;