From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id uddTK0edHWk0xBIAWB0awg (envelope-from ) for ; Wed, 19 Nov 2025 05:34:47 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JA3ll312; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id AAF921E08D; Wed, 19 Nov 2025 05:34:47 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id BA3261E08D for ; Wed, 19 Nov 2025 05:34:46 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2DB95384D17A for ; Wed, 19 Nov 2025 10:34:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2DB95384D17A Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JA3ll312 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 9D16B384D169 for ; Wed, 19 Nov 2025 10:32:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D16B384D169 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 9D16B384D169 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=1763548369; cv=none; b=WOh+0HZPDyJ0EFaGIHwzh0ZWIaKI8kmmmC8YMmW/d04rG8fZRbv4M0BozFAg3ZliMq7fVJcpql2oIJqfL0MEBj1+aPWNH3rCPHapzBAuWnzWzrU57lOg9dVaEsylVeB3D+MoTOuRQZgS2I7s65Nawei4/SSlWlGDNGWFEcHBC/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1763548369; c=relaxed/simple; bh=TOPbMlvKePL7nNdGoUUD5VKTmykur00RwtbiHXDjQfE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ACWKb48Y48EfQQwZhZo4/QVOAPSMohg6Oev1aUPJibccW9ApeS9nz/v/DwThiIRYWspXIn5Phuoz9Fm6wqFoPZ4T2knjIzxbqdTKsPB8/0GVLji1hkQoIzcxoTMQ1ISRcHxR4vpIp1ahk1tWmGWakwwuOkSFyGqCrU6WgX9MNXE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D16B384D169 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763548369; 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=A/FXILbU154bcE6g2MRWqqdC8p/ZfwKKYe6GX2ufRl4=; b=JA3ll312E/zpLHjlOKy4J6klvpzYn7cHHCEXo3vEY+u/qrmL648ZfY/UajaQqcdJemTOXj Sd7H6F4HrNILSXevYxNSg9aQH4sf7wuTGzSSAiisrJH3bvNx8Nrdm9EeDQOPWyY1sAUClR KZkuRhccpPixdON+dNDcaC4DNqf9Jq8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-684-YdyV0bOiMSuhbmJNdPVYcg-1; Wed, 19 Nov 2025 05:32:48 -0500 X-MC-Unique: YdyV0bOiMSuhbmJNdPVYcg-1 X-Mimecast-MFC-AGG-ID: YdyV0bOiMSuhbmJNdPVYcg_1763548367 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4776079ada3so62976665e9.1 for ; Wed, 19 Nov 2025 02:32:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763548367; x=1764153167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=A/FXILbU154bcE6g2MRWqqdC8p/ZfwKKYe6GX2ufRl4=; b=Yp7VZ9/vKNWuMVoY+E+pr4JXdl7hcpC4pXt3lJBDWAyvPWEEOoSD6wVlNC/K4W5x5Y 4hx7j+4jQkR95+I0y7exHdz0UO+aWOqy8bctf1kPipGh/Kq+YAizWqCdiHQHl7DK38hO 6FVCli6clMaYnU/5Y3LFJFCdRNLIoW+h+sIkU5kSPTJaN0QAXhlcqti8svqvTj8sWNFn NjNavnRJpKGUzFxV+DumpSy35KFOrVV7XnoiRqTkoVgSTnJy09Rb+bFJgKW/Ymbp+Eky qx5v0A0MVUOJxNTpV8O6p84SFJ8sAVpGxn/5btLOCbnscUK/EzMM/pUF+MlMFQtWWVib Kvfw== X-Gm-Message-State: AOJu0Yw5gAG0sL0Sr+7mQ5owmlxc+/AoxA5WxCCZIb2QdWtPlxQVOPX0 K8ThoDg4eIkE0pn8cyQXFOYoBy3eT5gakE+ZAtwCL39MN+7VTyHt3CfNiJ6BZBGyvkpGV0FB1Fm iC6lpjJPCvWLPExsNRDkSf0TMFTYfqtw0rnIpymWAhkSR7mpNPDB90g94NolVtg4QF/aV/tTr7k K8pyaQqPbtDiYaMio/9kV1nmxbW67Zl071dI/3+1EcmpdrXB8= X-Gm-Gg: ASbGncu348nfwA1XtkInquOIMNJ7Cnsaf7IIlPEJvaJEH+uCFNrCkmrZw0N+vw7JvS7 qgozcOYL2ljsdwuwUxcDSNuQWXqH5ggwaxaKTHm33PeDGjTdE2RNCjzGEz8JuDF58JjFIAVgtKE MNkYHguKXp3CAnIbleTfXbCfd+yWpDhwMIccFNtEATAfliRKw43+thQ3Mm8xHHFXNGyQAO4CvHZ exQ1OTpNanw60gCNk4oqiogX3zSiiTwsIQ4u/kecbek0LX8kku7MynQsoS1k6m0m1g9ZkAq4ZCm 4nwrGnFDe0ONbT/rz5cvzGznuZ5/GgIzZPiczcyj9Xz80QVg0pX3KZkZ0Gc3LYS0t2lJViCdaOj KpEbi X-Received: by 2002:a05:600c:5246:b0:477:a977:b8c2 with SMTP id 5b1f17b1804b1-477a977b995mr49609065e9.13.1763548366572; Wed, 19 Nov 2025 02:32:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHe+p94fk8nW9uHW95SJyJYh4qo+Pf6fvcKja+cOtbBemXDoT2iIVcxp40mn11ll1BvVtxfEg== X-Received: by 2002:a05:600c:5246:b0:477:a977:b8c2 with SMTP id 5b1f17b1804b1-477a977b995mr49608735e9.13.1763548366025; Wed, 19 Nov 2025 02:32:46 -0800 (PST) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-477b1025707sm39824775e9.6.2025.11.19.02.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 02:32:45 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 1/2] gdb: fix 'list' for multiple source file results Date: Wed, 19 Nov 2025 10:32:37 +0000 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hHo8udTUJh6mM-0dMOWogV4iqYmLDS8orez2CVjVt68_1763548367 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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~public-inbox=simark.ca@sourceware.org This commit: commit c7a45b98a61451f05ff654c4fb72a9c9cb2fba36 Date: Thu Jun 12 15:37:50 2025 +0000 gdb, linespec: avoid multiple locations with same PC broke GDB's ability to list multiple source files using a 'list' command. In GDB 16 and earlier something like 'list foo.c:10' could print multiple results if there were multiple 'foo.c' files compiled into the executable. The above commit added a filter to add_sal_to_sals (linespec.c) such that multiple sals in the same program space, but with the same pc value, could not be added, only the first sal would actually be recorded. The problem with this is that add_sal_to_sals is used from decode_digits_list_mode (also linespec.c) where the pc value is forced to zero. This force to zero makes sense I think as there might not be any compiled code for the requested line (this is for 'list' after all), so there might not be a valid pc to use. I'm not a fan of using '0' as a special pc value, there are embedded targets where 0 is a valid pc value, but given we're already using 0 here, I propose to just roll with it. So, my proposal is that, if the pc is 0, add_sal_to_sals should always add the sal. This fixes the decode_digits_list_mode, but should keep the fix that c7a45b98a614 introduced. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33647 --- gdb/linespec.c | 15 ++- gdb/testsuite/gdb.base/list-multi-source.c | 60 +++++++++ gdb/testsuite/gdb.base/list-multi-source.exp | 129 +++++++++++++++++++ 3 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 gdb/testsuite/gdb.base/list-multi-source.c create mode 100644 gdb/testsuite/gdb.base/list-multi-source.exp diff --git a/gdb/linespec.c b/gdb/linespec.c index 277b45b4b7f..2cbfe2fcc20 100644 --- a/gdb/linespec.c +++ b/gdb/linespec.c @@ -1071,11 +1071,16 @@ add_sal_to_sals (struct linespec_state *self, const symtab_and_line &sal, const char *symname, bool literal_canonical) { - /* We don't want two SALs with the same PC from the - same program space. */ - for (const auto &s : sals) - if (sal.pc == s.pc && sal.pspace == s.pspace) - return; + /* We don't want two SALs with the same PC from the same program space. + However, for the 'list' command we force the pc value to be 0, and in + this case we do want to see all SALs. See decode_digits_list_mode + for where the 0 originates from. */ + if (sal.pc != 0) + { + for (const auto &s : sals) + if (sal.pc == s.pc && sal.pspace == s.pspace) + return; + } sals.push_back (sal); diff --git a/gdb/testsuite/gdb.base/list-multi-source.c b/gdb/testsuite/gdb.base/list-multi-source.c new file mode 100644 index 00000000000..ab2b6a01b36 --- /dev/null +++ b/gdb/testsuite/gdb.base/list-multi-source.c @@ -0,0 +1,60 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2025 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 . */ + +extern int function_a (void); +extern int function_b (void); + +#ifdef MAIN +int +main (void) +{ + int res; + + res = function_a (); + + res += function_b (); + + return res; +} +#endif + +#if defined FILE_A || defined FILE_B +static int +get_value_common (void) +{ + /* NOTE: When reading this file in the source tree, the variable used in + the return statement below will be replaced by a constant value when + the file is copied into the source tree. */ + return value_to_return; /* List this line. */ +} +#endif + +#ifdef FILE_A +int +function_a (void) +{ + return get_value_common (); +} +#endif + +#ifdef FILE_B +int +function_b (void) +{ + return get_value_common (); +} +#endif diff --git a/gdb/testsuite/gdb.base/list-multi-source.exp b/gdb/testsuite/gdb.base/list-multi-source.exp new file mode 100644 index 00000000000..66f6582fdff --- /dev/null +++ b/gdb/testsuite/gdb.base/list-multi-source.exp @@ -0,0 +1,129 @@ +# Copyright 2025 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 that 'list FILE:LINE' can print multiple results if FILE +# matches multiple files from the source tree. +# +# Then test that we can use 'list DIR/FILE:LINE' to restrict the +# results to a single source file. + +# With a remote host, source files are automatically copied to the +# host by dejagnu, and this drops the directory structure that is +# needed for this test to work, i.e. we need a/foo.c and b/foo.c, but +# dejagnu's automatic copying just gives us a single foo.c. Instead +# of trying to fix this, for now at least, just skip remote host +# testing. +require {!is_remote host} + +# This test uses a single source file that is copied into the build +# tree 3 times. The three copies are then copied with different +# defines set so that we see different functions in each copy. +standard_testfile .c + +# Create the source tree within the build directory. +set src_root [standard_output_file "src"] +set src_a "$src_root/a" +set src_b "$src_root/b" +set file_a "$src_a/foo.c" +set file_b "$src_b/foo.c" +set file_main "$src_root/main.c" +file mkdir "$src_root" +file mkdir "$src_a" +file mkdir "$src_b" + +# Helper proc. Copy global SRCFILE to DEST, replacing +# 'value_to_return' with VALUE during the copy. +proc copy_and_update_source_file { dest value } { + # Open the source file for reading. + set in [open "$::srcdir/$::subdir/$::srcfile" r] + + # Read in the entire contents of the file. This should be fine as + # the input file is not large. + set file_content [read $in] + + # Close the input file. + close $in + + # Perform the replacement over the entire file contents. + set updated_content [string map \ + [list "value_to_return" $value] \ + $file_content] + + # Open the destination file for writing. + set out [open $dest w] + + # Write the modified content to the destination file. + puts -nonewline $out $updated_content + + # Close the output file. + close $out +} + +# Make three copies of the single source file in the build directory +# based source tree. Two of the source files are modified slighly to +# make the output of 'list' unique for each copy. +file copy "$srcdir/$subdir/$srcfile" "$file_main" +copy_and_update_source_file $file_a "3" +copy_and_update_source_file $file_b "-3" + +# Build the executable. Use defines to make the source files +# different. +if { [prepare_for_testing_full "failed to prepare" \ + [list $testfile debug \ + $file_main [list debug additional_flags=-DMAIN] \ + $file_a [list debug additional_flags=-DFILE_A] \ + $file_b [list debug additional_flags=-DFILE_B]]]} { + return +} + +# The LINENUM we should list, and the first and last lines that should +# appear in the list output. +set linenum [gdb_get_line_number "List this line"] +set first_linenum [expr {$linenum - 5}] +set last_linenum [expr {$linenum + 4}] + +# List using FILE:LINE for a filename that is ambiguous. +gdb_test "list foo.c:$linenum" \ + [multi_line \ + "file: \"\[^\r\n\]+/a/foo.c\", line number: $linenum, symbol: \"\[^\r\n\]+\"" \ + "$first_linenum\\s+\[^\r\n\]+" \ + ".*" \ + "$linenum\\s+[string_to_regexp {return 3; /* List this line. */}]" \ + ".*" \ + "$last_linenum\\s+\[^\r\n\]+" \ + "file: \"\[^\r\n\]+/b/foo.c\", line number: $linenum, symbol: \"\[^\r\n\]+\"" \ + "$first_linenum\\s+\[^\r\n\]+" \ + ".*" \ + "$linenum\\s+[string_to_regexp {return -3; /* List this line. */}]" \ + ".*" \ + "$last_linenum\\s+\[^\r\n\]+"] + +# Now list using a more acurate filename, we should only get a single +# result. +gdb_test "list a/foo.c:$linenum" \ + [multi_line \ + "^$first_linenum\\s+\[^\r\n\]+" \ + ".*" \ + "$linenum\\s+[string_to_regexp {return 3; /* List this line. */}]" \ + ".*" \ + "$last_linenum\\s+\[^\r\n\]+"] + +gdb_test "list b/foo.c:$linenum" \ + [multi_line \ + "^$first_linenum\\s+\[^\r\n\]+" \ + ".*" \ + "$linenum\\s+[string_to_regexp {return -3; /* List this line. */}]" \ + ".*" \ + "$last_linenum\\s+\[^\r\n\]+"] -- 2.47.1