From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id ABEBB38185E2 for ; Wed, 5 Jun 2024 13:15:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ABEBB38185E2 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ABEBB38185E2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717593345; cv=none; b=ppvX3iXv/rZxBSgna4irxWW9RMlZeyhZ3KgDTfMPc1oKMtv5XywN1yDMJxoEf4IhtzPER1BRQPBz97b+uFT+8idVAJrEwUExS4urfxXKejhP6o2N6oJ/hjBZ/qXnyrJ1UG0N473A74gCSc850MWGGjNEyJgPTqBZfOvMnHDmFo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717593345; c=relaxed/simple; bh=KtExCZsWeJ6Cf9wbhkavtDQ8RaUdmdWNukUjEXO3xUU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=m2rb+EQCHgIyA1xU6I9SsxYuZe4R4FYrzSu1kNYiBOF/FaIMaeV2jTjjEku7IH12alzcbgZg2hyEtnL2kELB79BWl+jPLAUAGhauiG6qkEAm7ZIU0qYVcDnnxmcsg8YDJLHCbXsolENMzh+hnP0LAozx0GJQ/WEG2J7KRHgEOxI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717593340; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=K0OoFHA0TOBG6ulARNZFRVbjwhQomhydCPaZZ/cqT64=; b=ft7EtRgNHQQ57dUCwK6brjhvXXEN1aScihOLSqYuQIWqdbRGtu6AJ691a1L6m7tKWVXoxE 7zokSbPFrmGMY9DtrSJ/g3URsMH2UEk0zPzXrNz6XdHRasMPglGcw4owqhr121PoSokCs/ ksKBtZWqQKYh/xt0V7R8k7RF0ZV8uiU= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-314-M-NdQpYIPVqev5e0uxLS7Q-1; Wed, 05 Jun 2024 09:15:37 -0400 X-MC-Unique: M-NdQpYIPVqev5e0uxLS7Q-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42135f6012fso31848185e9.3 for ; Wed, 05 Jun 2024 06:15:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717593335; x=1718198135; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K0OoFHA0TOBG6ulARNZFRVbjwhQomhydCPaZZ/cqT64=; b=rX001mD+C2w0NyUOhFnmQP4p1XznN9GfMYT3BEYgJnJMHppvZPPKz1+R6B0KxorYv6 LMW898i+HJJB99/CpvDa+eWS1x6JqTgXkQNXxu0Hajgt1JxJnJ9J5uKd+NbXIkUvQ8cD pU0XecxqP+dzAcI8upI4vuIQmwNd4suZo7twpuRBb9ZmmtPhLqJmCTXGcOQ1KNku4aDn 0dPSpecPnRQvwjcESe4oGH+Z7N6vCwmNaLsXvO/0BnT4KLeaW71zWqy5Vh+YopuNXbmF +/FZ7a7lJclXTdw3EdLqXh74YsS8Pj2mWn0MKwOQLKDPh3UJ9/a+KJrLwDwAK4R1AbQQ RGNg== X-Gm-Message-State: AOJu0Yxsc4kPPUWisVerx6oHueWZBkRS5+2zE20llzJkPMXT0PgvOK0/ PjiERi/rrg4zKApXHPvRXfA9ekoUpOpc2NZ4DkjpfZTqVIMJTYDtHk7YQq4aNcliC3OvIZr+XoM q24+kHitbZ5JxcXhKszTnZCjt46lYmFD/ZjKWVECqeFz8bAQQLaTksh+Yi6mmqLkdiuOPE4wO4z /Zpd/E4onCb3z/WTRr8vo9XDUJwmJn1c8PBxYZKsc36xI= X-Received: by 2002:a05:600c:46c3:b0:421:2cb3:143 with SMTP id 5b1f17b1804b1-421562cf604mr18347525e9.3.1717593334818; Wed, 05 Jun 2024 06:15:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3Wmm14YfgQ2VCCYud4V8oJCiQ4EW1eAw+7BARUYOJby8PLIVy9sXIPbbVCAKTrZUvTs5uCg== X-Received: by 2002:a05:600c:46c3:b0:421:2cb3:143 with SMTP id 5b1f17b1804b1-421562cf604mr18347235e9.3.1717593334135; Wed, 05 Jun 2024 06:15:34 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4215a61fe1dsm2061025e9.12.2024.06.05.06.15.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 06:15:33 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 8/8] gdb: check for multiple matching build-id files Date: Wed, 5 Jun 2024 14:15:15 +0100 Message-Id: <1fa8c902cb7841a4affc625d854fccd871a19d6f.1717592684.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Within the debug-file-directory GDB looks for the existence of a .build-id directory. Within the .build-id directory GDB looks for files with the form: .build-id/ff/4b4142d62b399499844924d53e33d4028380db.debug which contain the debug information for the objfile with the build-id ff4b4142d62b399499844924d53e33d4028380db. There appear to be two strategies for populating the .build-id directory. Ubuntu takes the approach of placing the actual debug information in this directory, so 4b4142d62b399499844924d53e33d4028380db.debug is an actual file containing the debug information. Fedora, RHEL, and SUSE take a slightly different approach, placing the debug information elsewhere, and then creating symlinks in the .build-id directory back to the original debug information file. The actual debug information is arranged in a mirror of the filesystem within the debug directory, as an example, if the debug-file-directory is /usr/lib/debug, then the debug information for /bin/foo can be found in /usr/lib/debug/bin/foo.debug. Where this gets interesting is that in some cases a package will install a single binary with multiple names, in this case a single binary will be install with either hard-links, or symlinks providing the alternative names. The debug information for these multiple binaries will then be placed into the /usr/lib/debug/ tree, and again, links are created so a single file can provide debug information for each of the names that binary presents as. In the .build-id tree though we have a problem. Do we have a single entry that links to one of the binary names? This would work; a user debugging any of the binaries will find the debug information based on the build-id, and will get the correct information, after all the binaries are identical (same file linked together). But there is one problem with this approach. Sometimes, for *reasons* it's possible that one or more the linked binaries might get removed. I'm honestly not 100% certain under what circumstances this can happen, but what I observe is that sometime a single name for a binary, and its corresponding .debug entry, can be removed. If this happens to be the entry that the .build-id link is pointing at, then we have a problem. The user can no longer find the debug information based on the .build-id. The solution that Fedora, RHEL, & SUSE have adopted is to add multiple entries in the .build-id tree, with each entry pointing to a different name within the debug/ tree, a sequence number is added to the build-id to distinguish the multiple entries. Thus, we might end up with a layout like this: /bin/ binary_name_1 binary_name_2 binary_name_3 /usr/ lib/ debug/ bin/ binary_name_1.debug binary_name_2.debug -> binary_name_1.debug [ HARDLINK ] binary_name_3.debug -> binary_name_1.debug [ HARDLINK ] .build-id/ a3/ 4b4142d62b399499844924d53e33d4028380db.debug -> ../../../debug/bin/binary_name_1.debug [ SYMLINK ] 4b4142d62b399499844924d53e33d4028380db.1.debug -> ../../../debug/bin/binary_name_2.debug [ SYMLINK ] 4b4142d62b399499844924d53e33d4028380db.2.debug -> ../../../debug/bin/binary_name_3.debug [ SYMLINK ] With current master GDB, debug information will only ever be looked up via the 4b4142d62b399499844924d53e33d4028380db.debug link. But if binary_name_1 and its corresponding binary_name_1.debug are ever removed, then master GDB will fail to find the debug information. Ubuntu seems to have a much better approach for debug information handling; they place the debug information directly into the .build-id tree, so there only ever needs to be a single entry for any one build-id. It's not clear if/how they handle the case where multiple names might share a single .debug file, if one of those names is then uninstalled, how do they know the .debug file should be retained or not ... but I assume that problem either doesn't exist or has been solved. Anyway, for a while Fedora has carried a patch that handles the build-id sequence number logic, and I'd like to try and get this patch upstream. I'm aware that this is a patch that applies to only some (probably a minority) of distros. However, the logic is contained to only a single function in build-id.c, and isn't that complex, so I'm hoping that there wont be too many objections. For distros that don't have build-id sequence numbers then they should see no change. The unnumbered file is checked for first, and only if that exists, but isn't readable, do we check for numbered files instead. Tests are included. --- gdb/build-id.c | 175 +++++++++++++---- gdb/testsuite/gdb.base/build-id-seqno.c | 22 +++ gdb/testsuite/gdb.base/build-id-seqno.exp | 133 +++++++++++++ gdb/testsuite/gdb.server/build-id-seqno.c | 22 +++ gdb/testsuite/gdb.server/build-id-seqno.exp | 198 ++++++++++++++++++++ 5 files changed, 512 insertions(+), 38 deletions(-) create mode 100644 gdb/testsuite/gdb.base/build-id-seqno.c create mode 100644 gdb/testsuite/gdb.base/build-id-seqno.exp create mode 100644 gdb/testsuite/gdb.server/build-id-seqno.c create mode 100644 gdb/testsuite/gdb.server/build-id-seqno.exp diff --git a/gdb/build-id.c b/gdb/build-id.c index 8ce94f1e7a0..1469222ca49 100644 --- a/gdb/build-id.c +++ b/gdb/build-id.c @@ -73,50 +73,150 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check) return retval; } -/* Helper for build_id_to_debug_bfd. LINK is a path to a potential - build-id-based separate debug file, potentially a symlink to the real file. - If the file exists and matches BUILD_ID, return a BFD reference to it. */ +/* Helper for build_id_to_debug_bfd. ORIGINAL_LINK with SUFFIX appended is + a path to a potential build-id-based separate debug file, potentially a + symlink to the real file. If the file exists and matches BUILD_ID, + return a BFD reference to it. */ static gdb_bfd_ref_ptr -build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len, - const bfd_byte *build_id) +build_id_to_debug_bfd_1 (const std::string &original_link, + size_t build_id_len, const bfd_byte *build_id, + const char *suffix) { - separate_debug_file_debug_printf ("Trying %s...", link.c_str ()); - - /* lrealpath() is expensive even for the usually non-existent files. */ - gdb::unique_xmalloc_ptr filename_holder; - const char *filename = nullptr; - if (is_target_filename (link)) - filename = link.c_str (); - else if (access (link.c_str (), F_OK) == 0) + tribool supports_target_stat = TRIBOOL_UNKNOWN; + + /* Drop the 'target:' prefix if the target filesystem is local. */ + std::string_view original_link_view (original_link); + if (is_target_filename (original_link) && target_filesystem_is_local ()) + original_link_view + = original_link_view.substr (strlen (TARGET_SYSROOT_PREFIX)); + + /* The upper bound of '10' here is completely arbitrary. The loop should + terminate via 'break' when either (a) a readable symlink is found, or + (b) a non-existing entry is found. + + However, for remote targets, we rely on the remote returning sane + error codes. If a remote sends back the wrong error code then it + might trick GDB into thinking that the symlink exists, but points to a + missing file, in which case GDB will try the next seqno. We don't + want a broken remote to cause GDB to spin here forever, hence a fixed + upper bound. */ + + for (unsigned seqno = 0; seqno < 10; seqno++) { - filename_holder.reset (lrealpath (link.c_str ())); - filename = filename_holder.get (); - } + std::string link (original_link_view); - if (filename == NULL) - { - separate_debug_file_debug_printf ("unable to compute real path"); - return {}; - } + if (seqno > 0) + string_appendf (link, ".%u", seqno); - /* We expect to be silent on the non-existing files. */ - gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget); + link += suffix; - if (debug_bfd == NULL) - { - separate_debug_file_debug_printf ("unable to open."); - return {}; - } + separate_debug_file_debug_printf ("Trying %s...", link.c_str ()); - if (!build_id_verify (debug_bfd.get(), build_id_len, build_id)) - { - separate_debug_file_debug_printf ("build-id does not match."); - return {}; + gdb::unique_xmalloc_ptr filename_holder; + const char *filename = nullptr; + if (is_target_filename (link)) + { + gdb_assert (link.length () >= strlen (TARGET_SYSROOT_PREFIX)); + const char *link_on_target + = link.c_str () + strlen (TARGET_SYSROOT_PREFIX); + + fileio_error target_errno; + if (supports_target_stat != TRIBOOL_FALSE) + { + struct stat sb; + int res = target_fileio_stat (nullptr, link_on_target, &sb, + &target_errno); + + if (res != 0 && target_errno != FILEIO_ENOSYS) + { + supports_target_stat = TRIBOOL_TRUE; + separate_debug_file_debug_printf ("path doesn't exist"); + break; + } + else if (res != 0 && target_errno == FILEIO_ENOSYS) + supports_target_stat = TRIBOOL_FALSE; + else + { + supports_target_stat = TRIBOOL_TRUE; + filename = link.c_str (); + } + } + + if (supports_target_stat == TRIBOOL_FALSE) + { + gdb_assert (filename == nullptr); + + /* Connecting to a target that doesn't support 'stat'. Try + 'readlink' as an alternative. This isn't ideal, but is + maybe better than nothing. Returns EINVAL if the path + isn't a symbolic link, which hints that the path is + available -- there are other errors e.g. ENOENT for when + the path doesn't exist, but we just assume that anything + other than EINVAL indicates the path doesn't exist. */ + std::optional link_target + = target_fileio_readlink (nullptr, link_on_target, + &target_errno); + if (link_target.has_value () + || target_errno == FILEIO_EINVAL) + filename = link.c_str (); + else + { + separate_debug_file_debug_printf ("path doesn't exist"); + break; + } + } + } + else + { + struct stat buf; + + /* The `access' call below automatically dereferences LINK, but + we want to stop incrementing SEQNO once we find a symlink + that doesn't exist. */ + if (lstat (link.c_str (), &buf) != 0) + { + separate_debug_file_debug_printf ("path doesn't exist"); + break; + } + + /* Can LINK be accessed, or if LINK is a symlink, can the file + pointed too be accessed? Do this as lrealpath() is + expensive, even for the usually non-existent files. */ + if (access (link.c_str (), F_OK) == 0) + { + filename_holder.reset (lrealpath (link.c_str ())); + filename = filename_holder.get (); + } + } + + if (filename == nullptr) + { + separate_debug_file_debug_printf ("unable to compute real path"); + continue; + } + + /* We expect to be silent on the non-existing files. */ + gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget); + + if (debug_bfd == NULL) + { + separate_debug_file_debug_printf ("unable to open `%s`", filename); + continue; + } + + if (!build_id_verify (debug_bfd.get(), build_id_len, build_id)) + { + separate_debug_file_debug_printf ("build-id does not match"); + continue; + } + + separate_debug_file_debug_printf ("found a match"); + return debug_bfd; } - separate_debug_file_debug_printf ("found a match"); - return debug_bfd; + separate_debug_file_debug_printf ("no suitable file found"); + return {}; } /* Common code for finding BFDs of a given build-id. This function @@ -156,10 +256,8 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id, while (size-- > 0) string_appendf (link, "%02x", (unsigned) *data++); - link += suffix; - gdb_bfd_ref_ptr debug_bfd - = build_id_to_debug_bfd_1 (link, build_id_len, build_id); + = build_id_to_debug_bfd_1 (link, build_id_len, build_id, suffix); if (debug_bfd != NULL) return debug_bfd; @@ -176,7 +274,8 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id, || !target_filesystem_is_local ())) { link = gdb_sysroot + link; - debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id); + debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id, + suffix); if (debug_bfd != NULL) return debug_bfd; } diff --git a/gdb/testsuite/gdb.base/build-id-seqno.c b/gdb/testsuite/gdb.base/build-id-seqno.c new file mode 100644 index 00000000000..e2119ba7d92 --- /dev/null +++ b/gdb/testsuite/gdb.base/build-id-seqno.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main (void) +{ + return 0; +} diff --git a/gdb/testsuite/gdb.base/build-id-seqno.exp b/gdb/testsuite/gdb.base/build-id-seqno.exp new file mode 100644 index 00000000000..b13ac8edd86 --- /dev/null +++ b/gdb/testsuite/gdb.base/build-id-seqno.exp @@ -0,0 +1,133 @@ +# This testcase is part of GDB, the GNU debugger. +# +# Copyright 2024 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Setup a .build-id/ based debug directory containing multiple entries +# for the same build-id, with each entry given a different sequence +# number. +# +# Ensure that GDB will scan over broken symlinks for the same build-id +# (but different sequence number) to find later working symlinks. +# +# This test only places debug information on the host, so it is always +# local to GDB. + +require {!is_remote host} + +standard_testfile + +if {[build_executable "failed to prepare" $testfile $srcfile] == -1} { + return -1 +} + +# Split out BINFILE.debug. Remove debug from BINFILE. +if {[gdb_gnu_strip_debug $binfile] != 0} { + return -1 +} + +# Get the '.build-id/xx/xxx...xxx' part of the filename. +set build_id_filename [build_id_debug_filename_get $binfile] + +# Hide (rename) BINFILE.debug, this should ensure GDB can't find it +# directly but needs to look for the build-id based file in the debug +# directory. +set hidden_debuginfo [standard_output_file "hidden_$testfile.debug"] +remote_exec build "mv ${binfile}.debug $hidden_debuginfo" + +# A filename that doesn't exist. Some symlinks will point at this +# file. +set missing_debuginfo [host_standard_output_file "missing_debuginfo"] + +# Create the debug directory, and the .build-id directory structure +# within it. +set debugdir [host_standard_output_file "debug"] +remote_exec host "mkdir -p $debugdir/[file dirname $build_id_filename]" + +set host_hidden_debuginfo [gdb_remote_download host $hidden_debuginfo] +remote_exec host "ln -fs $host_hidden_debuginfo $debugdir/$build_id_filename" + +# Start GDB and load global BINFILE. If FIND_DEBUGINFO is true then +# we expect GDB to find the debug information matching BINFILE, +# otherwise, we expect GDB not to find the debug information. +proc load_binfile_check_debug_is_found { find_debuginfo testname } { + with_test_prefix "$testname" { + clean_restart + + gdb_test_no_output "set debug-file-directory $::debugdir" \ + "set debug-file-directory" + + gdb_file_cmd $::binfile + + if { $find_debuginfo } { + gdb_assert { [regexp [string_to_regexp \ + "Reading symbols from $::hidden_debuginfo..."] \ + $::gdb_file_cmd_msg] } \ + "debuginfo was read via build-id" + } else { + gdb_assert { [regexp "\\(No debugging symbols found in \[^\r\n\]+/$::testfile\\)" \ + $::gdb_file_cmd_msg] } \ + } + } +} + +# Return a copy of FILENAME, which should end '.debug', with NUMBER +# added, e.g. add_seqno 1 "foo.debug" --> "foo.1.debug". +proc add_seqno { number filename } { + return [regsub "\.debug\$" $filename ".${number}.debug"] +} + +load_binfile_check_debug_is_found true \ + "find debuginfo with a single build-id file" + +remote_exec host "ln -fs $host_hidden_debuginfo \ + $debugdir/[add_seqno 1 $build_id_filename]" +remote_exec host "ln -fs $host_hidden_debuginfo \ + $debugdir/[add_seqno 2 $build_id_filename]" +remote_exec host "ln -fs $host_hidden_debuginfo \ + $debugdir/[add_seqno 3 $build_id_filename]" + +load_binfile_check_debug_is_found true \ + "find debuginfo with 4 build-id files" + +remote_exec host "ln -fs $missing_debuginfo $debugdir/$build_id_filename" + +load_binfile_check_debug_is_found true \ + "find debuginfo, first build-id file is bad" + +remote_exec host "ln -fs $missing_debuginfo \ + $debugdir/[add_seqno 1 $build_id_filename]" +remote_exec host "ln -fs $missing_debuginfo \ + $debugdir/[add_seqno 3 $build_id_filename]" + +load_binfile_check_debug_is_found true \ + "find debuginfo, first 2 build-id files are bad" + +remote_exec host "ln -fs $missing_debuginfo \ + $debugdir/[add_seqno 2 $build_id_filename]" + +load_binfile_check_debug_is_found false \ + "cannot find debuginfo, all build-id files are bad" + +remote_exec host "ln -fs $host_hidden_debuginfo \ + $debugdir/[add_seqno 3 $build_id_filename]" + +load_binfile_check_debug_is_found true \ + "find debuginfo, last build-id file is good" + +remote_exec host "rm -f $debugdir/[add_seqno 1 $build_id_filename]" + +load_binfile_check_debug_is_found false \ + "cannot find debuginfo, file with seqno 1 is missing" diff --git a/gdb/testsuite/gdb.server/build-id-seqno.c b/gdb/testsuite/gdb.server/build-id-seqno.c new file mode 100644 index 00000000000..e2119ba7d92 --- /dev/null +++ b/gdb/testsuite/gdb.server/build-id-seqno.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main (void) +{ + return 0; +} diff --git a/gdb/testsuite/gdb.server/build-id-seqno.exp b/gdb/testsuite/gdb.server/build-id-seqno.exp new file mode 100644 index 00000000000..c565d5a4348 --- /dev/null +++ b/gdb/testsuite/gdb.server/build-id-seqno.exp @@ -0,0 +1,198 @@ +# This testcase is part of GDB, the GNU debugger. +# +# Copyright 2024 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Setup a .build-id/ based debug directory containing multiple entries +# for the same build-id, with each entry given a different sequence +# number. +# +# Ensure that GDB will scan over broken symlinks for the same build-id +# (but different sequence number) to find later working symlinks. +# +# This test places the build-id files within a directory next to where +# gdbserver is started, and places a relative address in the +# debug-file-directory, in this way we require GDB to find the debug +# information via gdbserver. + +require {!is_remote host} + +load_lib gdbserver-support.exp + +standard_testfile + +if {[build_executable "failed to prepare" $testfile $srcfile] == -1} { + return -1 +} + +# Split out BINFILE.debug. Remove debug from BINFILE. +if {[gdb_gnu_strip_debug $binfile] != 0} { + return -1 +} + +# Get the '.build-id/xx/xxx...xxx' part of the filename. +set build_id_filename [build_id_debug_filename_get $binfile] + +# Hide (rename) BINFILE.debug, this should ensure GDB can't find it +# directly but needs to look for the build-id based file in the debug +# directory. +set hidden_debuginfo [standard_output_file "hidden_$testfile.debug"] +remote_exec build "mv ${binfile}.debug $hidden_debuginfo" + +# A filename that doesn't exist. Some symlinks will point at this +# file. +set missing_debuginfo "missing_debuginfo" + +# Helper called from gdb_finish when the 'target' is remote. Ensure the +# debug directory we create is deleted. +proc cleanup_remote_target {} { + remote_exec target "rm -fr debug/" +} + +if { ![is_remote target] } { + set gdbserver_dir [standard_output_file "gdbserver-dir"]/ +} else { + lappend gdb_finish_hooks cleanup_remote_target + set gdbserver_dir "" +} + +# Copy files to the target (if needed). +set target_binfile [gdb_remote_download target $binfile] +set target_debuginfo [gdb_remote_download target $hidden_debuginfo] + +# Setup the debug information on the target. +set debugdir "${gdbserver_dir}debug" +remote_exec target \ + "mkdir -p $debugdir/[file dirname $build_id_filename]" +remote_exec target \ + "ln -sf $target_debuginfo $debugdir/$build_id_filename" + +# Start GDB and load global BINFILE. If DEBUGINFO_FILE is not the +# empty string then this contains the '.build-id/xx/xxx....xxxx' part +# of the filename which we expect GDB to read from the remote target. +# If DEBUGINFO_FILE is the empty string then we don't expect GDB to +# find any debug information. +proc load_binfile_check_debug_is_found { debuginfo_file testname } { + with_test_prefix "$testname" { + with_timeout_factor 5 { + # Probing for .build-id based debug files on remote + # targets uses the vFile:stat packet by default, though + # there is a work around that avoids this which can be + # used if GDB is connected to an older gdbserver without + # 'stat' support. + # + # Check the work around works by disabling use of the + # vFile:stat packet. + foreach_with_prefix stat_pkt {auto off} { + clean_restart + + gdb_test_no_output "set debug-file-directory debug" \ + "set debug-file-directory" + + gdb_test_no_output "set sysroot target:" + + gdb_test "set remote hostio-stat-packet $stat_pkt" + + # Make sure we're disconnected, in case we're testing with an + # extended-remote board, therefore already connected. + gdb_test "disconnect" ".*" + + # Start gdbserver. This needs to be done after starting GDB. When + # gdbserver is running local to GDB, start gdbserver in a sub-directory, + # this prevents GDB from finding the debug information itself. + if { ![is_remote target] } { + with_cwd $::gdbserver_dir { + set res [gdbserver_start "" $::target_binfile] + } + } else { + set res [gdbserver_start "" $::target_binfile] + } + set gdbserver_protocol [lindex $res 0] + set gdbserver_gdbport [lindex $res 1] + + # Connect to gdbserver. The output will be placed into the global + # GDB_TARGET_REMOTE_CMD_MSG, and we'll match against this below. + gdb_assert {[gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0} \ + "connect to gdbserver" + + if { $debuginfo_file ne "" } { + gdb_assert { [regexp "Reading symbols from target:debug/[string_to_regexp $debuginfo_file]\\.\\.\\." \ + $::gdb_target_remote_cmd_msg] } \ + "debuginfo was read via build-id" + gdb_assert { [regexp "Reading debug/[string_to_regexp $debuginfo_file] from remote target\\.\\.\\." \ + $::gdb_target_remote_cmd_msg] } \ + "debuginfo was read from remote target" + } else { + gdb_assert { [regexp "\\(No debugging symbols found in \[^\r\n\]+/$::testfile\\)" \ + $::gdb_target_remote_cmd_msg] } + } + } + } + } +} + +# Return a copy of FILENAME, which should end '.debug', with NUMBER +# added, e.g. add_seqno 1 "foo.debug" --> "foo.1.debug". +proc add_seqno { number filename } { + return [regsub "\.debug\$" $filename ".${number}.debug"] +} + +# Precompute sequence numbered build-id filenames. +set build_id_1_filename [add_seqno 1 $build_id_filename] +set build_id_2_filename [add_seqno 2 $build_id_filename] +set build_id_3_filename [add_seqno 3 $build_id_filename] + +load_binfile_check_debug_is_found $build_id_filename \ + "find debuginfo with a single build-id file" + +remote_exec target "ln -fs $target_debuginfo \ + $debugdir/$build_id_1_filename" +remote_exec target "ln -fs $target_debuginfo \ + $debugdir/$build_id_2_filename" +remote_exec target "ln -fs $target_debuginfo \ + $debugdir/$build_id_3_filename" + +load_binfile_check_debug_is_found $build_id_filename \ + "find debuginfo with 4 build-id files" + +remote_exec target "ln -fs $missing_debuginfo $debugdir/$build_id_filename" + +load_binfile_check_debug_is_found $build_id_1_filename \ + "find debuginfo, first build-id file is bad" + +remote_exec target "ln -fs $missing_debuginfo \ + $debugdir/$build_id_1_filename" +remote_exec target "ln -fs $missing_debuginfo \ + $debugdir/$build_id_3_filename" + +load_binfile_check_debug_is_found $build_id_2_filename \ + "find debuginfo, first 2 build-id files are bad" + +remote_exec target "ln -fs $missing_debuginfo \ + $debugdir/$build_id_2_filename" + +load_binfile_check_debug_is_found "" \ + "cannot find debuginfo, all build-id files are bad" + +remote_exec target "ln -fs $target_debuginfo \ + $debugdir/$build_id_3_filename" + +load_binfile_check_debug_is_found $build_id_3_filename \ + "find debuginfo, last build-id file is good" + +remote_exec target "rm -f $debugdir/$build_id_1_filename" + +load_binfile_check_debug_is_found "" \ + "cannot find debuginfo, file with seqno 1 is missing" -- 2.25.4