From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id QWd2FC0wK2raMgIAWB0awg (envelope-from ) for ; Thu, 11 Jun 2026 18:01:17 -0400 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=LBYuTDcf; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 4E4071E098; Thu, 11 Jun 2026 18:01:17 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.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 autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (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 37D7D1E070 for ; Thu, 11 Jun 2026 18:01:16 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E2544B99F49 for ; Thu, 11 Jun 2026 22:01:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E2544B99F49 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=LBYuTDcf 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 73C254B99F41 for ; Thu, 11 Jun 2026 21:59:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73C254B99F41 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 73C254B99F41 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1781215157; cv=none; b=jGrr5Skoe5NnaL4YdVw8R9Xe2wCZ4x+qOeHQDvxLVRSjnMjjXD1oLQLnH8lR9AFJ3mMiDbj+JXazbM+PBvq6NRvRQnLjYY0U2Aa+8QYzqingjdmQdbdJQtEv/iaC6efsK38SRnrYNu5fhjkvIIqBv8HQY7CGKGpQ56ho/GHWLYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1781215157; c=relaxed/simple; bh=Qlfk4yX3BHHWqtTcFCzRgddg6/jkhAz/OH4OM95OE24=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=NWUq74DTqzE2ri4treUzlhMjqzpJhhfWrIRc5FgTYiXyIJGMXcfLuxr3aN9wtlaIJjdTifn/wf/8BRMqTOhoBtn5SM/Q3e8P1Z9gVIMjpSpQ3u1a261woXepIUnnM7RvQ8m0iyiHgXe+ztdl5lfIj0pg7uTZEIgmKGhBt/PokHI= ARC-Authentication-Results: i=1; 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=LBYuTDcf DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73C254B99F41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1781215157; 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=9ZMcizl6OsD0oHHR1UaRe2C/g8AbEdDL1fFxjs/qEbA=; b=LBYuTDcf6kOwegew5MEl9hxEU33JYOJHQrv+pH/rGnRqLngROKlJ79qBti0w3c2yp9AhtA jNkPybEn6bY6qjY4NO6hIq8aOzdUg8aZB+z1GBORKtS/cQgQtoVHv+XAxHQkkgxfKdTEDp u5uDAZ587KaAPt2gX9VrytNpBmU8tVM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-318-g-C6gEGyMPW3q5MXilIkGw-1; Thu, 11 Jun 2026 17:59:15 -0400 X-MC-Unique: g-C6gEGyMPW3q5MXilIkGw-1 X-Mimecast-MFC-AGG-ID: g-C6gEGyMPW3q5MXilIkGw_1781215154 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-490ab3f6e55so1581715e9.0 for ; Thu, 11 Jun 2026 14:59:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781215154; x=1781819954; 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=9ZMcizl6OsD0oHHR1UaRe2C/g8AbEdDL1fFxjs/qEbA=; b=TyW+TQ9ShyF4gw1v7T7mH4WwNU3Xi0YkpemYT7DX6cHo85rRQrPnwYQUDDXWS15dOY Ja29sGar5W8P7weQuL5xGrayzFHI7y62xMU6ZRGX6jvrZFqt46Ui56ZzRmRpvO/cTAMa 3dbDjX+SK8fC1pwNolVc08O0qouqkZ3LABLNRtXRgUWLmyT03lvhBR9wS79wSWhm9EkG s1/yS3jnSeWweiawhaBElwqTAovqi61DKWCWEVntWI3JFZHPzpol9SG9mb8C39xOnzUq bsiNiJv5WKjEUKPKdETGy77nue98vv/Di447QnrhKQmAZAUFpLtKF6kErBeNLh7R0DaP p+Zg== X-Gm-Message-State: AOJu0Yw02VM3WEJ5zVMAPCLq872RRvd4Wo+1U6epzTaEseiyYXTEaHna wcl32WXXzuKFO5yJa9qsRCAq7oAXQYmemy4zUdCJIKcB+kJ2Fts7z2uVgm20Cgm/SlNMV7MFw7h vK/4MrQvHkymFfaOUYFUjGS0R+n5dgafxp4Ncg0H/BDd3Y0IGWCdZXYwv2cn7/g3QjcmyTtC/em rOE4wVNXAsllDquoNsxIkTA/m3t5g2niDQOVTeK+rsWk4V5iQ= X-Gm-Gg: Acq92OEnhu0f2EJbPUpIfedYnX4VhTHnfCKJraHiiNdPFKM8zTw9vFNxFPRbViZM66l 9vpt7AO8Nv/OLmP1JnBwB7y3wLluXBg2a2TsIlHOP5jeMgxrGJyzplTNX6Qs6m+he5JCKNzmqBr FfV0tgILgKla/dULjmxpY5BfjUZBoUhn5rwCks73t8bNgYcLQFPSmtwjK9g9CW70ALT3N2D5P27 kjD7a75+CD7vwo0tLgYYMV8T1kS65jL92gX7n4uQB994v9kXZZaNqgHsfJ48aaL/ZntBOYMn1sY oWEdRbMe1CaU+bJS4e9e4XXVeTetvDIo9ovtckTAsjRxANI6BbnmlZP1nIxL3feBv9S+U+et7EJ DnOZuj/csYVcNEbIUsXlq3Lr6wNikIBD6BmIpeozK X-Received: by 2002:a05:600c:c093:b0:490:b28d:a6f9 with SMTP id 5b1f17b1804b1-490e55d4a5bmr65853725e9.8.1781215154271; Thu, 11 Jun 2026 14:59:14 -0700 (PDT) X-Received: by 2002:a05:600c:c093:b0:490:b28d:a6f9 with SMTP id 5b1f17b1804b1-490e55d4a5bmr65853465e9.8.1781215153698; Thu, 11 Jun 2026 14:59:13 -0700 (PDT) Received: from localhost (19.81.93.209.dyn.plus.net. [209.93.81.19]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490ea95b274sm10898705e9.1.2026.06.11.14.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jun 2026 14:59:12 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 3/3] gdb: allow 'until' to work in outermost frame Date: Thu, 11 Jun 2026 22:59:06 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: <41fe591d58ba010fa771e80ca674b61e30feef2f.1780942441.git.aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1JywgApnF-IHYYKiLtr7PLg0gVEcRjowG0aHvvCOkiQ_1781215154 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 The 'until' command with an argument, e.g. 'until *ADDRESS', is implemented by the until_break_command in breakpoint.c. The most important thing this function does is convert the location argument '*ADDRESS' in my example, into a vector of symtab_and_line objects. Each of these symtab_and_line objects is then used to create a temporary bp_until breakpoint. The other thing that until_break_command does is create a breakpoint in the caller frame. This breakpoint is there to ensure the inferior stops upon exiting the frame in which the 'until' command was issued, if the until breakpoint was not hit. When compiling an assembler file into a static test program, if I use 'starti' to stop the inferior within the outermost frame, and then try to use 'until *ADDRESS' I see the following error: (gdb) starti Starting program: /tmp/hello Program stopped. 0x0000000000401000 in _start () (gdb) bt #0 0x0000000000401000 in _start () (gdb) until *0x0000000000401015 Warning: Cannot insert breakpoint 0. Cannot access memory at address 0x1 Command aborted. (gdb) The problem here is the breakpoint that 'until' tries to create in the caller frame. Though the 'bt' in the above example indicates that there is only a single frame, the outermost frame, this is only because GDB has specific code in get_prev_frame to stop the backtrace at the outermost frame. If we turn this off and try 'bt' again: (gdb) set backtrace past-entry on (gdb) bt #0 0x0000000000401000 in _start () #1 0x0000000000000001 in ?? () #2 0x00007fffffffac73 in ?? () #3 0x0000000000000000 in ?? () (gdb) What we are seeing here is the garbage values that happen to be in the registers tricking GDB into thinking there are frames before _start. GDB's code to handle this is in get_prev_frame, however, when 'until' creates the caller frame breakpoint it calls frame_unwind_caller_pc which calls frame_unwind_caller_frame, which skips get_prev_frame and calls get_prev_frame_always. As a result, the 'until' command will try to place a breakpoint in the bogus frame #1 shown above. As the frame is at address 0x1, which is non-writable, we see an error when trying to insert the breakpoint. In this commit I propose using program_space::get_entry_point_info within frame_unwind_caller_frame to spot when we are attempting to unwind the inferior's entry frame. If this is detected then we will return nullptr indicating that there is no previous frame. In this case the 'until' command will skip creating the frame exit breakpoint. I specifically only check the inferior_entry_address value and not the exec_entry_address value within frame_unwind_caller_frame. It is possible that there are valid frames between the exec_entry_address frame and the inferior_entry_address frame, for example, in a dynamically linked executable the exec_entry_address frame will be the first frame within the main executable while the inferior_entry_address frame will be the first frame within the dynamic linker. It is only in this second case that there truly are no previous frames in which to place a breakpoint. I have made the new detection logic conditional on 'backtrace past-entry' being off. If the user turns this on then we assume that they know what they are doing, and that (for reasons we cannot know) there really are frames prior to the entry-frame. The test for this fix ran into an issue where the frame-id for the outermost frame would change between the first instruction and later instructions in the frame. I created bug PR gdb/34245 for this issue. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34245 --- gdb/frame.c | 24 +++ gdb/testsuite/gdb.base/until-in-entry-frame.c | 22 +++ .../gdb.base/until-in-entry-frame.exp | 163 ++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 gdb/testsuite/gdb.base/until-in-entry-frame.c create mode 100644 gdb/testsuite/gdb.base/until-in-entry-frame.exp diff --git a/gdb/frame.c b/gdb/frame.c index 3920d8d03f7..a1970096ab3 100644 --- a/gdb/frame.c +++ b/gdb/frame.c @@ -707,6 +707,30 @@ get_stack_frame_id (const frame_info_ptr &next_frame) static frame_info_ptr frame_unwind_caller_frame (const frame_info_ptr &initial_next_frame) { + /* Unless 'backtrace past-entry' is on then there is no caller for the + frame at the inferior entry address. Sometimes the register values + present when an inferior starts can make GDB believe that there are + frames before the entry frame, but trying to use that frame to + e.g. place a breakpoint, will fail. */ + if (initial_next_frame->level >= 0 + && get_frame_type (initial_next_frame) == NORMAL_FRAME + && !user_set_backtrace_options.backtrace_past_entry + && get_frame_pc_if_available (initial_next_frame).has_value ()) + { + const program_space::entry_point_info &ep_info + = current_program_space->get_entry_point_info (); + + CORE_ADDR frame_func_addr = get_frame_func (initial_next_frame); + + /* We only check the inferior entry address here, not the main + executable entry address. If there are frames between the + executable entry address frame and the inferior entry address + frame then these are likely valid, and can be used for placing + e.g. frame exit breakpoints. */ + if (ep_info.inferior_entry_address () == frame_func_addr) + return nullptr; + } + frame_info_ptr this_frame = get_prev_frame_always (initial_next_frame); if (this_frame == nullptr) return nullptr; diff --git a/gdb/testsuite/gdb.base/until-in-entry-frame.c b/gdb/testsuite/gdb.base/until-in-entry-frame.c new file mode 100644 index 00000000000..9b08ea9e1d9 --- /dev/null +++ b/gdb/testsuite/gdb.base/until-in-entry-frame.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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/until-in-entry-frame.exp b/gdb/testsuite/gdb.base/until-in-entry-frame.exp new file mode 100644 index 00000000000..9130f7a19cd --- /dev/null +++ b/gdb/testsuite/gdb.base/until-in-entry-frame.exp @@ -0,0 +1,163 @@ +# Copyright 2026 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 . + +# Check that 'until' in the outermost frame works as expected. + +standard_testfile + +if { [build_executable "failed to build" $testfile $srcfile] } { + return +} + +set testfile_static ${testfile}-static +if { [build_executable "failed to build" $testfile_static $srcfile \ + { debug additional_flags=-static } ] } { + return +} + +# Use 'bt 1' to get the name of the current function. Returns the +# name of the current function, or the empty string if the current +# function name cannot be established. +proc current_function_name { testname } { + set func_name "" + gdb_test_multiple "bt 1" $testname { + -re -wrap "#0\\s+\[^\r\n\]*\\s+in (\[^( \]+) \\(.*" { + set func_name $expect_out(1,string) + } + } + + return $func_name +} + +# Start the inferior with 'starti', then use 'until' within the +# outermost frame. This is the real outermost frame, not 'main'. +# +# PAST_ENTRY should be 'on' or 'off' and is used in a 'set backtrace +# past-entry ...' command. +proc run_test { use_stepi past_entry testfile } { + clean_restart $testfile + + if { [gdb_starti_cmd] < 0 } { + untested starti + return + } + + gdb_test "" "Program stopped\\..*" "starti" + + set func_name [current_function_name \ + "function name for first function"] + + gdb_test_no_output "set backtrace past-entry $past_entry" + + # On x86-64 GDB does get a valid frame-id for the very first + # instruction, but from the second instruction onwards it gets + # outer_frame_id. As a result an 'until' setup at the first + # instruction doesn't match later on within the same function as + # the frame-ids no longer match. + # + # Try to work around this by issuing a 'stepi' to move to the + # second instruction. + # + # This is only a heuristic though. On different architectures GDB + # might have a valid frame-id for multiple instructions within the + # outer frame, or might not have outer_frame_id for all + # instructions. + # + # Also, we need to consider that the very first instruction might + # be a control flow instruction, so using stepi could send the + # inferior to another function. We try to spot this case and + # perform an early return if that happens. + if { $use_stepi } { + gdb_test "stepi" + set func_name_after [current_function_name \ + "function name after stepi"] + if { $func_name ne $func_name_after } { + unsupported "stepi moved to another function" + return + } + } + + # If the 'until' doesn't trigger then stop in 'main' as a backup. + gdb_breakpoint main + + # Find an address to use in the 'until' command. + set until_address "" + set capture_address false + gdb_test_multiple "disassemble" "find address for until" { + -re "^=> $::hex \[^\r\n\]+\r\n" { + set capture_address true + exp_continue + } + + -re "^\\s+($::hex) \[^\r\n\]+\r\n" { + if { $capture_address } { + set until_address $expect_out(1,string) + set capture_address false + } + + exp_continue + } + + -re "^$::gdb_prompt $" { + set found_address [expr { $until_address ne "" }] + if { !$found_address } { + unsupported "$gdb_test_name (no instruction found)" + return + } + pass $gdb_test_name + } + + -re "^\[^\r\n\]*\r\n" { + exp_continue + } + } + + # Send the 'until' command and then wait for GDB to stop. See the + # notes above about the 'stepi' for why we sometimes expect to see + # GDB reach 'main' here. + send_gdb "until *${until_address}\n" + gdb_test_multiple "" "after until" { + -re -wrap "Breakpoint $::decimal, (?:\[^\r\n\]+ in )?main \\(\\).*" { + kfail "gdb/34245" "$gdb_test_name (skipped until breakpoint)" + } + + -re -wrap "$::hex in $func_name \\(\\).*" { + pass $gdb_test_name + } + + -re -wrap "Warning:\r\nCannot insert breakpoint 0\\.\r\nCannot access memory at address $::hex.*Command aborted\\." { + # With PAST_ENTRY 'on', GDB discovers some invalid frames + # past the entry frame based on whatever happens to be in + # the registers when the entry function is called. These + # invalid frames are often non-writable, so GDB will fail + # to insert the breakpoint when the inferior resumes. + # + # We still count this as a pass though; the user should + # only turn on 'backtrace past-entry' when they know that + # is needed, so failure here is totally expected. + gdb_assert { $past_entry eq "on" } "$gdb_test_name (caller breakpoint failed)" + } + } +} + +foreach_with_prefix past_entry { off on } { + foreach_with_prefix use_stepi { true false } { + run_test $use_stepi $past_entry $testfile + + with_test_prefix "static" { + run_test $use_stepi $past_entry $testfile_static + } + } +} -- 2.25.4