From patchwork Mon May 20 14:14:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 90488 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E329385F014 for ; Mon, 20 May 2024 14:15:05 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 72ECE3858C5F for ; Mon, 20 May 2024 14:14:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 72ECE3858C5F 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 72ECE3858C5F 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=1716214474; cv=none; b=QV2PhNTtIiA6dyFERnDlZ8ao64kjcf49FFITV+40QgrY2pn7ZeMVA1/sljl3CrglC5V4RjFjyHraeNhLLWQ4j5ia8CtioEU4LYCfVk4eDPWvxMQh8jnTMU7+ZohCgiPchK58BJSNFTYkpzF8X9NKhK9dIaVj1u6GkDnnAZ1Ym0E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716214474; c=relaxed/simple; bh=zsMdA8Dl+v6rN77RvMAVEXOMkoHX9mvMsvv69bgvds0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=isJ5aKG0onZRZZM7A7hdJsIxsravo1MHJx8exkG0g9TIZmTSTynHqqWfHewK7ank+JGm6cE/HznlbU0Jmx2r1TGwTz3QORJ95iQMgxSZuRBngEmu6EQ42XlpP0bp5TrR0QaOAOY6JO7OzGbqPo3gtB1QC4e4u/VvViftKpWoEZU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716214472; 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=nq8bc6FsRyafjWPiGe4po2ZkdNCiMjm3xP5AAl7aP4s=; b=AtYMsgDJvH1sapiITdA3XxYZDWoTnlGS0rLQW113LgukqgbhOuAjxmRB0dnk3bGkxogYij ZJMooFlCggTL9cIC3oaq7nJfh3/tMhWvnI9LW+Xyr2oqK0l9D9RmO6wHhypRPC/gI+cI0w /JRP6Kkn9sxAVm0f0e1+Q7FGwBJMeBA= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-310-OfAygJBzMCe4dn-sVa1_lw-1; Mon, 20 May 2024 10:14:31 -0400 X-MC-Unique: OfAygJBzMCe4dn-sVa1_lw-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a59a5b06802so602058966b.1 for ; Mon, 20 May 2024 07:14:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716214469; x=1716819269; 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=nq8bc6FsRyafjWPiGe4po2ZkdNCiMjm3xP5AAl7aP4s=; b=BIFrcF2/ln3VHn3MrfbD2tlR/lP4G+XSQWmHggFeXoGK3+S9aTKbV0qZUqbu5T6qSB PNHlTvABcEjVtlUBFtbeoJe7vovRwK0wu02tLkHfmngUIqcWzqt6WqNcECPEj6tY7MrY OZI+eSDHuA61GyfGS6Q4LS0Qw1cgJ1pqzdACuAkMPokiBvBFLLz7IDtxOMFwPg+HTwSE UkTOUS6O2RW9XmlMAnNyFeyh74XZza9ETY7lCuQkfFPrXWTAOblWmnZLE4P2seOkjmwP bc1DPezjmJMvIZD14n372Ti6sSTUVfctb9PAU9QeVZKeEAP2b6lc5HuXBNFQl8yrl0+C 7RhA== X-Gm-Message-State: AOJu0Ywmze9LHQ4dUqatWN+2UJCxAXpPx9MU0+Ghj7iIkEQYTbFNQOrj JPmLp/0mOx8GuoGAX365+ZRw4kuztRDARqY8I0d1Erdr1h21karZobnt8X+d3zBZr10qDOChHvz ldjCkCSBE9m5JTOutQJjdlxxQ/S/iJhwmSfAnswRAvV7v5sIGAEbYcg23wBallMjobgMdPQJc+H Tvutz0ly3czhAKYDdqVlYr7wPheG9UT0VzZQI1qJiU5fI= X-Received: by 2002:a17:906:ca8a:b0:a5c:df23:c9c6 with SMTP id a640c23a62f3a-a5cdf23d59cmr982127766b.47.1716214468455; Mon, 20 May 2024 07:14:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLn/+9qt5gby7LH+Y+CiQOEycU+17ZTflHRcG4nU+mRXiz2BfBzgGzne7L1u+esZpXE51oFA== X-Received: by 2002:a17:906:ca8a:b0:a5c:df23:c9c6 with SMTP id a640c23a62f3a-a5cdf23d59cmr982125366b.47.1716214467732; Mon, 20 May 2024 07:14:27 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a17b01948sm1478885966b.175.2024.05.20.07.14.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 07:14:26 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdb/testsuite: tests for debug lookup within the sysroot Date: Mon, 20 May 2024 15:14:23 +0100 Message-Id: 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 X-Spam-Status: No, score=-11.8 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Add tests for looking up debug information within the sysroot via both build-id and gnu_debuglink. I wanted to ensure that the gnu_debuglink test couldn't make use of build-ids, so I added the 'no-build-id' flag to gdb_compile. As these tests rely on setting the sysroot, if I'm running a dynamically linked executable, GDB will try to find all shared libraries within the sysroot. This would mean I'd have to figure out and copy all shared libraries the executable uses, certainly possible, but a bit of a pain. So instead, I've just compiled the test executable as a static binary. Now there are no shared library dependencies. I can now split the debug information out from the test binary, and place it within the sysroot. When GDB is started and the executable loaded, we can check that GDB is finding the debug information within the sysroot. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30871 --- gdb/testsuite/gdb.base/sysroot-debug-lookup.c | 22 +++ .../gdb.base/sysroot-debug-lookup.exp | 162 ++++++++++++++++++ gdb/testsuite/lib/gdb.exp | 13 ++ 3 files changed, 197 insertions(+) create mode 100644 gdb/testsuite/gdb.base/sysroot-debug-lookup.c create mode 100644 gdb/testsuite/gdb.base/sysroot-debug-lookup.exp diff --git a/gdb/testsuite/gdb.base/sysroot-debug-lookup.c b/gdb/testsuite/gdb.base/sysroot-debug-lookup.c new file mode 100644 index 00000000000..e67331d3150 --- /dev/null +++ b/gdb/testsuite/gdb.base/sysroot-debug-lookup.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 1992-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 () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.base/sysroot-debug-lookup.exp b/gdb/testsuite/gdb.base/sysroot-debug-lookup.exp new file mode 100644 index 00000000000..e0377df0fa7 --- /dev/null +++ b/gdb/testsuite/gdb.base/sysroot-debug-lookup.exp @@ -0,0 +1,162 @@ +# 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 . + +# Test GDB's ability to find debug information by looking within the +# sysroot. +# +# We compile a static binary (to reduce what we need to copy into the +# sysroot), split the debug information from the binary, and setup a +# sysroot. +# +# The debug-file-directory is set to just '/debug', but we're +# expecting GDB to actually look in '$SYSROOT/debug'. +# +# There's a test for using .build-id based lookup, and a test for +# gnu_debuglink based lookup. + +require {!is_remote host} + +standard_testfile + +# Create a copy of BINFILE, split out the debug information, and then +# setup a sysroot. Hide (by moving) the actual debug information file +# and create a symlink to the hidden debug information from within the +# sysroot. +# +# Start GDB, set the sysroot, and then load the executable, ensure GDB +# finds the debug information, which must have happened by lookin in +# the sysroot. +proc_with_prefix lookup_via_build_id {} { + set filename ${::binfile}_1 + if { [build_executable "build exec" ${filename} $::srcfile \ + {additional_flags=-static debug build-id}] } { + return + } + + # Split debug information into a .debug file, remove debug + # information from FILENAME. Don't add a .gnu_debuglink to + # FILENAME, we rely on the build-id. + if {[gdb_gnu_strip_debug $filename { no-debuglink }] != 0} { + unsupported "cannot split debug information from executable" + return + } + + set sysroot [standard_output_file "sysroot1"] + set debug_dir "/debug" + + set debug_symlink \ + $sysroot/$debug_dir/[build_id_debug_filename_get $filename] + + set build_id_dir [file dirname $debug_symlink] + + set debug_filename ${filename}_hidden_debug + + remote_exec build "mkdir -p $build_id_dir" + remote_exec build "mv $filename.debug $debug_filename" + remote_exec build "ln -sf $debug_filename $debug_symlink" + + clean_restart + + gdb_test_no_output "set sysroot $sysroot" "set sysroot" + gdb_test_no_output "set debug-file-directory $debug_dir" + + gdb_file_cmd $filename + + gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } + + set re [string_to_regexp "Reading symbols from $debug_filename..."] + gdb_assert {[regexp $re $::gdb_file_cmd_msg]} +} + +# Create a copy of BINFILE, split out the debug information, and then +# setup a sysroot. Hide (by moving) the actual debug information file +# and create a symlink to the hidden debug information from within the +# sysroot. +# +# Copy the executable into the sysroot and then start GDB, set the +# sysroot, and load the executable. Check that GDB finds the debug +# information, which must have happened by lookin in the sysroot. +proc_with_prefix lookup_via_debuglink {} { + set filename ${::binfile}_2 + if { [build_executable "build exec" ${filename} $::srcfile \ + {additional_flags=-static debug no-build-id}] } { + return + } + + # Split debug information into a .debug file, remove debug + # information from FILENAME. + if {[gdb_gnu_strip_debug $filename] != 0} { + unsupported "cannot split debug information from executable" + return + } + + # We're going to setup the sysroot like this: + # + # sysroot2/ + # bin/ + # $FILENAME + # debug/ + # bin/ + # $FILENAME.debug + # + # When looking up debug information via the debuglink, GDB will + # only search in the sysroot if the original objfile was in the + # sysroot. And GDB will resolve symlinks, so if the objfile is + # symlinked to outside the sysroot, GDB will not search in the + # sysroot for the debug information. + # + # So we have to copy the executable into the sysroot. + # + # We are OK to symlink the debug information to a file outside the + # sysroot though. + + set sysroot [standard_output_file "sysroot2"] + + foreach path { bin debug/bin } { + remote_exec build "mkdir -p $sysroot/$path" + } + + # Copy the executable into the sysroot. + set file_basename [file tail $filename] + set exec_in_sysroot ${sysroot}/bin/${file_basename} + remote_exec build "cp $filename $exec_in_sysroot" + + # Rename the debug file outside of the sysroot, this should stop + # GDB finding this file "by accident". + set debug_filename ${filename}_hidden_debug + remote_exec build "mv $filename.debug $debug_filename" + + # Symlink the debug information into the sysroot. + set debug_symlink \ + ${sysroot}/debug/bin/${file_basename}.debug + remote_exec build "ln -sf $debug_filename $debug_symlink" + + # Restart GDB and setup the sysroot and debug directory. + clean_restart + gdb_test_no_output "set sysroot $sysroot" "set sysroot" + gdb_test_no_output "set debug-file-directory /debug" + + # Load the executable, we expect GDB to find the debug information + # in the sysroot. + gdb_file_cmd $exec_in_sysroot + + gdb_assert { $::gdb_file_cmd_debug_info eq "debug" } + + set re [string_to_regexp "Reading symbols from $debug_symlink..."] + gdb_assert {[regexp $re $::gdb_file_cmd_msg]} +} + +lookup_via_build_id +lookup_via_debuglink diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index a63c13f9cbc..a8e8b69d6ef 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -5211,6 +5211,7 @@ proc quote_for_host { args } { # debug information # - text_segment=addr: Tell the linker to place the text segment at ADDR. # - build-id: Ensure the final binary includes a build-id. +# - no-build-id: Ensure the final binary does not include a build-id. # - column-info/no-column-info: Enable/Disable generation of column table # information. # @@ -5316,6 +5317,18 @@ proc gdb_compile {source dest type options} { lappend new_options "additional_flags=-Wl,--build-id" } + # If the 'no-build-id' option is used then disable the build-id. + if {[lsearch -exact $options no-build-id] > 0} { + lappend new_options "additional_flags=-Wl,--build-id=none" + } + + # Sanity check. If both 'build-id' and 'no-build-id' are used + # then what is expected from us! + if {[lsearch -exact $options build-id] > 0 + && [lsearch -exact $options no-build-id] > 0} { + error "cannot use build-id and no-build-id options" + } + # Treating .c input files as C++ is deprecated in Clang, so # explicitly force C++ language. if { !$getting_compiler_info