From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id A693B3870847 for ; Sun, 17 May 2020 18:04:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A693B3870847 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-47-s2rK9_EjM4-NBi2B3EZYtA-1; Sun, 17 May 2020 14:04:55 -0400 X-MC-Unique: s2rK9_EjM4-NBi2B3EZYtA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 932837BAE for ; Sun, 17 May 2020 18:04:54 +0000 (UTC) Received: from cascais.Home (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1221A82ED0 for ; Sun, 17 May 2020 18:04:53 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 3/3] Make exec-file-mismatch compare build IDs Date: Sun, 17 May 2020 19:04:50 +0100 Message-Id: <20200517180450.14925-4-palves@redhat.com> In-Reply-To: <20200517180450.14925-1-palves@redhat.com> References: <20200517180450.14925-1-palves@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-16.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 May 2020 18:05:00 -0000 The patch makes GDB first try exec-file-mismatch validation via build IDs, and then if that isn't possible, fallback to validating using the old method of comparing filenames. I'd argue that we should remove the filename validation for causing too many false positives, though. Currently, the exec-file-mismatch feature simply compares filenames to decide whether the exec file loaded in gdb and the exec file the target reports is running match. This causes false positives when remote debugging, because it'll often be the case that the paths in the host and the target won't match. And of course misses the case of the files having the same name but being actually different files (e.g., different builds). This also broke many testcases when running against gdbserver, causing tests to be skipped like (here native-extended-gdbserver): (gdb) run Starting program: /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink-filelink warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink-filelink and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink exec-file-mismatch handling is currently "ask" Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink"? (y or n) UNTESTED: gdb.base/argv0-symlink.exp: could not run to main or to fail like (here native-gdbserver): (gdb) spawn /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/../../gdbserver/gdbserver --once localhost:2346 /home/pedro/gdb/binutils-gdb/build/gdb/te stsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x Process /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x created; pid = 20040 Listening on port 2346 target remote localhost:2346 Remote debugging using localhost:2346 warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/temp/19968/skip_btrace_tests-19968.x and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x exec-file-mismatch handling is currently "ask" Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x"? (y or n) Quit (gdb) UNSUPPORTED: gdb.btrace/buffer-size.exp: target does not support record-btrace The former case is about GDB not realizing the two files are the same, because one of the them is a symlink to the other. The latter case is about GDB realizing that one file is a copy of the other. Over the years, the toolchain has settled on build ID matching being the canonical method to match core dumps to executables, and executables with no debug info to their debug info. This patch makes us use build IDs to match the running image of a binary with its version loaded in gdb, which may or may not have debug info. This is very much like the core dump/executable matching. The change to gdb_bfd_open is necessary to get rid of the "transfers from remote targets can be slow" warning when we open the remote file to read its build ID: (gdb) r Starting program: /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/break/break Reading /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink from remote target... warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/break/break and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink exec-file-mismatch handling is currently "ask" Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink"? (y or n) While trying this out, I was worried that bfd would read a lot of stuff from the binary in order to extract the build ID, making it potentially slow, but turns out we don't read all that much. Maybe a couple hundred bytes, and most of it seemingly is the read-ahead cache. So I'm not worried about that. Otherwise I'd consider whether a new qXfer:buildid:read would be better. But I'm happy that we seemingly don't need to worry about it. gdb/ChangeLog: yyyy-mm-dd Pedro Alves * NEWS (set exec-file-mismatch): Adjust entry. * exec.c: Include "build-id.h". (validate_exec_file): Try to match build IDs. * gdb_bfd.c (struct gdb_bfd_open_closure): New. (gdb_bfd_iovec_fileio_open): Adjust to use gdb_bfd_open_closure and pass down 'warn_if_slow'. (gdb_bfd_open): Add 'warn_if_slow' parameter. Use gdb_bfd_open_closure to pass it down. * gdb_bfd.h (gdb_bfd_open): Add 'warn_if_slow' parameter. gdb/doc/ChangeLog: yyyy-mm-dd Pedro Alves * gdb.texinfo (Attach): Update exec-file-mismatch description to mention build IDs. (Separate Debug Files): Add "build id" anchor. --- gdb/doc/gdb.texinfo | 23 +++++++++++++---------- gdb/NEWS | 15 +++++++-------- gdb/exec.c | 41 +++++++++++++++++++++++++++++++++++++++-- gdb/gdb_bfd.c | 23 ++++++++++++++++------- gdb/gdb_bfd.h | 6 ++++-- 5 files changed, 79 insertions(+), 29 deletions(-) diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 8f3301259af..aaddfa9b885 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -2909,22 +2909,24 @@ the @code{file} command to load the program. @xref{Files, ,Commands to Specify Files}. @anchor{set exec-file-mismatch} -If the debugger can determine the name of the executable file running -in the process it is attaching to, and this file name does not match -the name of the current exec-file loaded by @value{GDBN}, the option -@code{exec-file-mismatch} specifies how to handle the mismatch. +If the debugger can determine that the executable file running in the +process it is attaching to does not match the current exec-file loaded +by @value{GDBN}, the option @code{exec-file-mismatch} specifies how to +handle the mismatch. @value{GDBN} tries to compare the files by +comparing their build IDs (@pxref{build ID}). If build IDs are not +present in either file, @value{GDBN} compares the file names. The +@code{exec-file-mismatch} option specifies how to handle a mismatch. @table @code @kindex exec-file-mismatch @cindex set exec-file-mismatch @item set exec-file-mismatch @samp{ask|warn|off} -Whether to detect mismatch between the name of the current executable -file loaded by @value{GDBN} and the name of the executable file used to -start the process. If @samp{ask}, the default, display a warning -and ask the user whether to load the process executable file; if -@samp{warn}, just display a warning; if @samp{off}, don't attempt to -detect a mismatch. +Whether to detect mismatch between the current executable file loaded +by @value{GDBN} and the executable file used to start the process. If +@samp{ask}, the default, display a warning and ask the user whether to +load the process executable file; if @samp{warn}, just display a +warning; if @samp{off}, don't attempt to detect a mismatch. @cindex show exec-file-mismatch @item show exec-file-mismatch @@ -20874,6 +20876,7 @@ checksum for the debug file, which @value{GDBN} uses to validate that the executable and the debug file came from the same build. @item +@anchor{build ID} The executable contains a @dfn{build ID}, a unique bit string that is also present in the corresponding debug info file. (This is supported only on some operating systems, when using the ELF or PE file formats diff --git a/gdb/NEWS b/gdb/NEWS index a059fc7aa0e..2a9c8b8ee19 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -54,14 +54,13 @@ set exec-file-mismatch -- Set exec-file-mismatch handling (ask|warn|off). show exec-file-mismatch -- Show exec-file-mismatch handling (ask|warn|off). - Set or show the option 'exec-file-mismatch'. When GDB attaches to - a running process and can determine the name of the executable file - the process runs, this new option indicates whether to detect mismatch - between the name of the current executable file loaded by GDB - and the name of the executable file used to start the process. - If 'ask', the default, display a warning and ask the user - whether to load the process executable file; if 'warn', just display - a warning; if 'off', don't attempt to detect a mismatch. + Set or show the option 'exec-file-mismatch'. When GDB attaches to a + running process, this new option indicates whether to detect + a mismatch between the current executable file loaded by GDB and the + executable file used to start the process. If 'ask', the default, + display a warning and ask the user whether to load the process + executable file; if 'warn', just display a warning; if 'off', don't + attempt to detect a mismatch. tui new-layout NAME WINDOW WEIGHT [WINDOW WEIGHT]... Define a new TUI layout, specifying its name and the windows that diff --git a/gdb/exec.c b/gdb/exec.c index a2added5e22..c05a1be1f1b 100644 --- a/gdb/exec.c +++ b/gdb/exec.c @@ -37,6 +37,7 @@ #include "gdb_bfd.h" #include "gcore.h" #include "source.h" +#include "build-id.h" #include #include "readline/tilde.h" @@ -247,11 +248,45 @@ validate_exec_file (int from_tty) struct inferior *inf = current_inferior (); /* Try to determine a filename from the process itself. */ const char *pid_exec_file = target_pid_to_exec_file (inf->pid); + bool build_id_mismatch = false; - /* If wee cannot validate the exec file, return. */ + /* If we cannot validate the exec file, return. */ if (current_exec_file == NULL || pid_exec_file == NULL) return; + /* Try validating via build-id, if available. This is the most + reliable check. */ + const bfd_build_id *exec_file_build_id = build_id_bfd_get (exec_bfd); + if (exec_file_build_id != nullptr) + { + /* Prepend the target prefix, to force gdb_bfd_open to open the + file on the remote file system (if indeed remote). */ + std::string target_pid_exec_file + = std::string (TARGET_SYSROOT_PREFIX) + pid_exec_file; + + gdb_bfd_ref_ptr abfd (gdb_bfd_open (target_pid_exec_file.c_str (), + gnutarget, -1, false)); + if (abfd != nullptr) + { + const bfd_build_id *target_exec_file_build_id + = build_id_bfd_get (abfd.get ()); + + if (target_exec_file_build_id != nullptr) + { + if (exec_file_build_id->size == target_exec_file_build_id->size + && memcmp (exec_file_build_id->data, + target_exec_file_build_id->data, + exec_file_build_id->size) == 0) + { + /* Match. */ + return; + } + else + build_id_mismatch = true; + } + } + } + std::string exec_file_target (pid_exec_file); /* In case the exec file is not local, exec_file_target has to point at @@ -259,7 +294,9 @@ validate_exec_file (int from_tty) if (is_target_filename (current_exec_file) && !target_filesystem_is_local ()) exec_file_target = TARGET_SYSROOT_PREFIX + exec_file_target; - if (exec_file_target != current_exec_file) + /* If build-id validation wasn't possible, fallback to validating by + filename. */ + if (build_id_mismatch || exec_file_target != current_exec_file) { warning (_("Mismatch between current exec-file %ps\n" diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index 61872a0bf3d..25e0178a8b8 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -271,22 +271,29 @@ fileio_errno_to_host (int errnum) return -1; } +/* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open. */ +struct gdb_bfd_open_closure +{ + inferior *inf; + bool warn_if_slow; +}; + /* Wrapper for target_fileio_open suitable for passing as the - OPEN_FUNC argument to gdb_bfd_openr_iovec. The supplied - OPEN_CLOSURE is unused. */ + OPEN_FUNC argument to gdb_bfd_openr_iovec. */ static void * -gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *inferior) +gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure) { const char *filename = bfd_get_filename (abfd); int fd, target_errno; int *stream; + gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure; gdb_assert (is_target_filename (filename)); - fd = target_fileio_open ((struct inferior *) inferior, + fd = target_fileio_open (oclosure->inf, filename + strlen (TARGET_SYSROOT_PREFIX), - FILEIO_O_RDONLY, 0, true, + FILEIO_O_RDONLY, 0, oclosure->warn_if_slow, &target_errno); if (fd == -1) { @@ -378,7 +385,8 @@ gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream, /* See gdb_bfd.h. */ gdb_bfd_ref_ptr -gdb_bfd_open (const char *name, const char *target, int fd) +gdb_bfd_open (const char *name, const char *target, int fd, + bool warn_if_slow) { hashval_t hash; void **slot; @@ -392,9 +400,10 @@ gdb_bfd_open (const char *name, const char *target, int fd) { gdb_assert (fd == -1); + gdb_bfd_open_closure open_closure { current_inferior (), warn_if_slow }; return gdb_bfd_openr_iovec (name, target, gdb_bfd_iovec_fileio_open, - current_inferior (), + &open_closure, gdb_bfd_iovec_fileio_pread, gdb_bfd_iovec_fileio_close, gdb_bfd_iovec_fileio_fstat); diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h index 532520e0f7d..a941b79fe73 100644 --- a/gdb/gdb_bfd.h +++ b/gdb/gdb_bfd.h @@ -78,10 +78,12 @@ typedef gdb::ref_ptr gdb_bfd_ref_ptr; If the BFD was not accessed using target fileio operations then the filename associated with the BFD and accessible with bfd_get_filename will not be exactly NAME but rather NAME with - TARGET_SYSROOT_PREFIX stripped. */ + TARGET_SYSROOT_PREFIX stripped. If WARN_IF_SLOW is true, print a + warning message if the file is being accessed over a link that may + be slow. */ gdb_bfd_ref_ptr gdb_bfd_open (const char *name, const char *target, - int fd = -1); + int fd = -1, bool warn_if_slow = true); /* Mark the CHILD BFD as being a member of PARENT. Also, increment the reference count of CHILD. Calling this function ensures that -- 2.14.5