From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2009) id 8BF943889815; Mon, 21 Mar 2022 18:19:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8BF943889815 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Aaron Merey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] PR gdb/27570: missing support for debuginfod in core_target::build_file_mappings X-Act-Checkin: binutils-gdb X-Git-Author: Aaron Merey X-Git-Refname: refs/heads/master X-Git-Oldrev: 39f53acb410c3e303fb25ff823de57eb316515ca X-Git-Newrev: b91f93a02c9c32e55073c0f18b330a6a9c5cfa34 Message-Id: <20220321181915.8BF943889815@sourceware.org> Date: Mon, 21 Mar 2022 18:19:15 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Mar 2022 18:19:15 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Db91f93a02c9c= 32e55073c0f18b330a6a9c5cfa34 commit b91f93a02c9c32e55073c0f18b330a6a9c5cfa34 Author: Aaron Merey Date: Wed Mar 2 20:00:59 2022 -0500 PR gdb/27570: missing support for debuginfod in core_target::build_file= _mappings =20 Add debuginfod support to core_target::build_file_mappings and locate_exec_from_corefile_build_id to enable the downloading of missing executables and shared libraries referenced in core files. =20 Also add debuginfod support to solib_map_sections so that previously downloaded shared libraries can be retrieved from the local debuginfod cache. =20 When core file shared libraries are found locally, verify that their build-ids match the corresponding build-ids found in the core file. If there is a mismatch, attempt to query debuginfod for the correct build and print a warning if unsuccessful: =20 warning: Build-id of /lib64/libc.so.6 does not match core file. =20 Also disable debuginfod when gcore invokes gdb. Debuginfo is not needed for core file generation so debuginfod queries will slow down gcore unnecessarily. Diff: --- gdb/corelow.c | 28 ++++++++++++ gdb/debuginfod-support.c | 51 ++++++++++++++++++= ++++ gdb/debuginfod-support.h | 17 ++++++++ gdb/gcore.in | 2 +- gdb/solib.c | 32 ++++++++++++++ .../gdb.debuginfod/fetch_src_and_symbols.exp | 22 ++++++++++ 6 files changed, 151 insertions(+), 1 deletion(-) diff --git a/gdb/corelow.c b/gdb/corelow.c index c1148288932..42c43db5e2f 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -46,6 +46,8 @@ #include "gdbsupport/filestuff.h" #include "build-id.h" #include "gdbsupport/pathstuff.h" +#include "gdbsupport/scoped_fd.h" +#include "debuginfod-support.h" #include #include #include "gdbcmd.h" @@ -229,6 +231,11 @@ core_target::build_file_mappings () canonical) pathname will be provided. */ gdb::unique_xmalloc_ptr expanded_fname =3D exec_file_find (filename, NULL); + + if (expanded_fname =3D=3D nullptr && build_id !=3D nullptr) + debuginfod_exec_query (build_id->data, build_id->size, + filename, &expanded_fname); + if (expanded_fname =3D=3D nullptr) { m_core_unavailable_mappings.emplace_back (start, end - start); @@ -410,6 +417,27 @@ locate_exec_from_corefile_build_id (bfd *abfd, int fro= m_tty) gdb_bfd_ref_ptr execbfd =3D build_id_to_exec_bfd (build_id->size, build_id->data); =20 + if (execbfd =3D=3D nullptr) + { + /* Attempt to query debuginfod for the executable. */ + gdb::unique_xmalloc_ptr execpath; + scoped_fd fd =3D debuginfod_exec_query (build_id->data, build_id->si= ze, + abfd->filename, &execpath); + + if (fd.get () >=3D 0) + { + execbfd =3D gdb_bfd_open (execpath.get (), gnutarget); + + if (execbfd =3D=3D nullptr) + warning (_("\"%s\" from debuginfod cannot be opened as bfd: %s"), + execpath.get (), + gdb_bfd_errmsg (bfd_get_error (), nullptr).c_str ()); + else if (!build_id_verify (execbfd.get (), build_id->size, + build_id->data)) + execbfd.reset (nullptr); + } + } + if (execbfd !=3D nullptr) { exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty); diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c index e077e136614..3614ff23882 100644 --- a/gdb/debuginfod-support.c +++ b/gdb/debuginfod-support.c @@ -69,6 +69,15 @@ debuginfod_debuginfo_query (const unsigned char *build_i= d, return scoped_fd (-ENOSYS); } =20 +scoped_fd +debuginfod_exec_query (const unsigned char *build_id, + int build_id_len, + const char *filename, + gdb::unique_xmalloc_ptr *destname) +{ + return scoped_fd (-ENOSYS); +} + #define NO_IMPL _("Support for debuginfod is not compiled into GDB.") =20 #else @@ -272,6 +281,48 @@ debuginfod_debuginfo_query (const unsigned char *build= _id, =20 return fd; } + +/* See debuginfod-support.h */ + +scoped_fd +debuginfod_exec_query (const unsigned char *build_id, + int build_id_len, + const char *filename, + gdb::unique_xmalloc_ptr *destname) +{ + if (!debuginfod_is_enabled ()) + return scoped_fd (-ENOSYS); + + debuginfod_client *c =3D get_debuginfod_client (); + + if (c =3D=3D nullptr) + return scoped_fd (-ENOMEM); + + char *dname =3D nullptr; + user_data data ("executable for", filename); + + debuginfod_set_user_data (c, &data); + gdb::optional term_state; + if (target_supports_terminal_ours ()) + { + term_state.emplace (); + target_terminal::ours (); + } + + scoped_fd fd (debuginfod_find_executable (c, build_id, build_id_len, &dn= ame)); + debuginfod_set_user_data (c, nullptr); + + if (debuginfod_verbose > 0 && fd.get () < 0 && fd.get () !=3D -ENOENT) + printf_filtered (_("Download failed: %s. " \ + "Continuing without executable for %ps.\n"), + safe_strerror (-fd.get ()), + styled_string (file_name_style.style (), filename)); + + if (fd.get () >=3D 0) + destname->reset (dname); + + return fd; +} #endif =20 /* Set callback for "set debuginfod enabled". */ diff --git a/gdb/debuginfod-support.h b/gdb/debuginfod-support.h index 29e361bb76f..5b1c1cb91f4 100644 --- a/gdb/debuginfod-support.h +++ b/gdb/debuginfod-support.h @@ -61,4 +61,21 @@ debuginfod_debuginfo_query (const unsigned char *build_i= d, const char *filename, gdb::unique_xmalloc_ptr *destname); =20 +/* Query debuginfod servers for an executable file with BUILD_ID. + BUILD_ID can be given as a binary blob or a null-terminated string. + If given as a binary blob, BUILD_ID_LEN should be the number of bytes. + If given as a null-terminated string, BUILD_ID_LEN should be 0. + + FILENAME should be the name or path associated with the executable. + It is used for printing messages to the user. + + If the file is successfully retrieved, its path on the local machine + is stored in DESTNAME. If GDB is not built with debuginfod, this + function returns -ENOSYS. */ + +extern scoped_fd debuginfod_exec_query (const unsigned char *build_id, + int build_id_len, + const char *filename, + gdb::unique_xmalloc_ptr + *destname); #endif /* DEBUGINFOD_SUPPORT_H */ diff --git a/gdb/gcore.in b/gdb/gcore.in index 7038c0c394c..ed7abf2d29b 100644 --- a/gdb/gcore.in +++ b/gdb/gcore.in @@ -98,7 +98,7 @@ do # ` filename (tilde_expand (so->so_name)); gdb_bfd_ref_ptr abfd (ops->bfd_open (filename.get ())); + gdb::unique_xmalloc_ptr build_id_hexstr + =3D get_cbfd_soname_build_id (current_program_space->cbfd, so->so_name= ); + + /* If we already know the build-id of this solib from a core file, verify + it matches ABFD's build-id. If there is a mismatch or the solib wasn= 't + found, attempt to query debuginfod for the correct solib. */ + if (build_id_hexstr.get () !=3D nullptr) + { + bool mismatch =3D false; + + if (abfd !=3D nullptr && abfd->build_id !=3D nullptr) + { + std::string build_id =3D build_id_to_string (abfd->build_id); + + if (build_id !=3D build_id_hexstr.get ()) + mismatch =3D true; + } + if (abfd =3D=3D nullptr || mismatch) + { + scoped_fd fd =3D debuginfod_exec_query ((const unsigned char*) + build_id_hexstr.get (), + 0, so->so_name, &filename); + + if (fd.get () >=3D 0) + abfd =3D ops->bfd_open (filename.get ()); + else if (mismatch) + warning (_("Build-id of %ps does not match core file."), + styled_string (file_name_style.style (), filename.get ())); + } + } =20 if (abfd =3D=3D NULL) return 0; diff --git a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp b/gdb/t= estsuite/gdb.debuginfod/fetch_src_and_symbols.exp index 5912c38c266..f12ed7d486c 100644 --- a/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp +++ b/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp @@ -63,6 +63,11 @@ if { [gdb_compile "$sourcetmp" "$binfile" executable {de= bug}] !=3D "" } { return -1 } =20 +if { [gdb_compile "$sourcetmp" "${binfile}2" executable {debug}] !=3D "" }= { + fail "compile" + return -1 +} + # Write some assembly that just has a .gnu_debugaltlink section. # Copied from testsuite/gdb.dwarf2/dwzbuildid.exp. proc write_just_debugaltlink {filename dwzname buildid} { @@ -114,6 +119,8 @@ proc write_dwarf_file {filename buildid {value 99}} { } } =20 +set corefile [standard_output_file "corefile"] + proc no_url { } { global binfile outputdir debugdir =20 @@ -167,6 +174,16 @@ proc no_url { } { gdb_test "file ${binfile}_alt.o" \ ".*could not find '.gnu_debugaltlink'.*" \ "file [file tail ${binfile}_alt.o]" + + # Generate a core file and test that gdb cannot find the executable + clean_restart ${binfile}2 + gdb_test "start" "Temporary breakpoint.*" + gdb_test "generate-core-file $::corefile" "Saved corefile $::corefile"= \ + "file [file tail $::corefile] gen" + file rename -force ${binfile}2 $debugdir + + clean_restart + gdb_test "core $::corefile" ".*in ?? ().*" "file [file tail $::corefil= e]" } =20 proc local_url { } { @@ -234,6 +251,11 @@ proc local_url { } { gdb_test "br main" "Breakpoint 1 at.*file.*" gdb_test "l" ".*This program is distributed in the hope.*" =20 + # gdb should now find the executable file + clean_restart + gdb_test "core $::corefile" ".*return 0.*" "file [file tail $::corefil= e]" \ + "Enable debuginfod?.*" "y" + # gdb should now find the debugaltlink file clean_restart gdb_test "file ${binfile}_alt.o" \