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.129.124]) by sourceware.org (Postfix) with ESMTPS id 381BB38233C5 for ; Wed, 31 Aug 2022 12:16:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 381BB38233C5 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-U-tAVrQlPuOlppvf8buxAg-1; Wed, 31 Aug 2022 08:16:34 -0400 X-MC-Unique: U-tAVrQlPuOlppvf8buxAg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 63A54185A7B2; Wed, 31 Aug 2022 12:16:34 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.43.2.105]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B1715909FF; Wed, 31 Aug 2022 12:16:33 +0000 (UTC) From: Bruno Larsen To: gdb-patches@sourceware.org Cc: pedro@palves.net, aburgess@redhat.com, Bruno Larsen Subject: [PATCH v3 2/2] gdb/reverse: Fix stepping over recursive functions Date: Wed, 31 Aug 2022 14:07:28 +0200 Message-Id: <20220831120727.2742360-3-blarsen@redhat.com> In-Reply-To: <20220831120727.2742360-1-blarsen@redhat.com> References: <20220831120727.2742360-1-blarsen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 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=-12.4 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_LOW, 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 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: Wed, 31 Aug 2022 12:16:40 -0000 Currently, when using GDB to do reverse debugging, if we try to use the command "reverse next" to skip a recursive function, instead of skipping all of the recursive calls and stopping in the previous line, we stop at the second to last recursive call, and need to manually step backwards until we leave the first call. This is well documented in PR gdb/16678. This bug happens because when GDB notices that a reverse step has entered into a function, GDB will add a step_resume_breakpoint at the start of the function, then single step out of the prologue once that breakpoint is hit. The problem was happening because GDB wouldn't give that step_resume_breakpoint a frame-id, so the first time the breakpoint was hit, the inferior would be stopped. This is fixed by giving the current frame-id to the breakpoint. This commit also changes gdb.reverse/step-reverse.c to contain a recursive function and attempt to both, skip it altogether, and to skip the second call from inside the first call, as this setup broke a previous version of the patch. --- gdb/infrun.c | 2 +- gdb/testsuite/gdb.reverse/step-precsave.exp | 6 ++- gdb/testsuite/gdb.reverse/step-reverse.c | 18 ++++++- gdb/testsuite/gdb.reverse/step-reverse.exp | 58 +++++++++++++++++++-- 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index 033699bc3f7..679a0c83ece 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -7133,7 +7133,7 @@ process_event_stop_test (struct execution_control_state *ecs) sr_sal.pc = ecs->stop_func_start; sr_sal.pspace = get_frame_program_space (frame); insert_step_resume_breakpoint_at_sal (gdbarch, - sr_sal, null_frame_id); + sr_sal, get_stack_frame_id (frame)); } } else diff --git a/gdb/testsuite/gdb.reverse/step-precsave.exp b/gdb/testsuite/gdb.reverse/step-precsave.exp index 0836ed2629f..3279b6ce879 100644 --- a/gdb/testsuite/gdb.reverse/step-precsave.exp +++ b/gdb/testsuite/gdb.reverse/step-precsave.exp @@ -86,7 +86,8 @@ gdb_test "step 3" ".*STEP TEST 2.*" "step test 2" # step over call -gdb_test "step" ".*NEXT OVER THIS CALL.*" "step up to call" +gdb_test "step" ".*NEXT OVER THIS RECURSION.*" "step up to call" +gdb_test "next" ".*NEXT OVER THIS CALL.*" "skip recursive call" gdb_test "next" ".*STEP INTO THIS CALL.*" "next over call" # step into call @@ -280,9 +281,10 @@ gdb_test_multiple "step" "$test_message" { } } -# next backward over call +# Next backward over calls. gdb_test "next" ".*NEXT OVER THIS CALL.*" "reverse next over call" +gdb_test "next" ".*NEXT OVER THIS RECURSION.*" "reverse next over recursive call" # step/next backward with count diff --git a/gdb/testsuite/gdb.reverse/step-reverse.c b/gdb/testsuite/gdb.reverse/step-reverse.c index aea2a98541d..3d647b9b29d 100644 --- a/gdb/testsuite/gdb.reverse/step-reverse.c +++ b/gdb/testsuite/gdb.reverse/step-reverse.c @@ -26,6 +26,19 @@ int callee() { /* ENTER CALLEE */ return myglob++; /* ARRIVED IN CALLEE */ } /* RETURN FROM CALLEE */ +/* We need to make this function take more than a single instruction + to run, otherwise it could hide PR gdb/16678, as reverse execution can + step over a single-instruction function. */ +int +recursive_callee (int val) +{ + if (val == 0) return 0; + val /= 2; + if (val > 1) + val++; + return recursive_callee (val); /* RECURSIVE CALL */ +} /* EXIT RECURSIVE FUNCTION */ + /* A structure which, we hope, will need to be passed using memcpy. */ struct rhomboidal { int rather_large[100]; @@ -51,6 +64,9 @@ int main () { y = y + 4; z = z + 5; /* STEP TEST 2 */ + /* Test that next goes over recursive calls too */ + recursive_callee (32); /* NEXT OVER THIS RECURSION */ + /* Test that "next" goes over a call */ callee(); /* NEXT OVER THIS CALL */ @@ -60,7 +76,7 @@ int main () { /* Test "stepi" */ a[5] = a[3] - a[4]; /* FINISH TEST */ callee(); /* STEPI TEST */ - + /* Test "nexti" */ callee(); /* NEXTI TEST */ diff --git a/gdb/testsuite/gdb.reverse/step-reverse.exp b/gdb/testsuite/gdb.reverse/step-reverse.exp index 997b62604d5..a540b1f88ce 100644 --- a/gdb/testsuite/gdb.reverse/step-reverse.exp +++ b/gdb/testsuite/gdb.reverse/step-reverse.exp @@ -47,9 +47,11 @@ gdb_test "step" ".*STEP TEST 1.*" "step test 1" gdb_test "next 2" ".*NEXT TEST 2.*" "next test 2" gdb_test "step 3" ".*STEP TEST 2.*" "step test 2" +# Next through a recursive function call. +gdb_test "next 2" "NEXT OVER THIS CALL.*" "next over recursion" + # step over call -gdb_test "step" ".*NEXT OVER THIS CALL.*" "step up to call" gdb_test "next" ".*STEP INTO THIS CALL.*" "next over call" # step into call @@ -118,7 +120,7 @@ gdb_test_multiple "stepi" "$test_message" { set test_message "stepi back from function call" gdb_test_multiple "stepi" "$test_message" { - -re "NEXTI TEST.*$gdb_prompt $" { + -re -wrap "NEXTI TEST.*" { pass "$test_message" } -re "ARRIVED IN CALLEE.*$gdb_prompt $" { @@ -143,7 +145,6 @@ gdb_test_multiple "stepi" "$test_message" { ### # Set reverse execution direction - gdb_test_no_output "set exec-dir reverse" "set reverse execution" # stepi backward thru return and into a function @@ -243,10 +244,59 @@ gdb_test_multiple "step" "$test_message" { } } -# next backward over call +# Next backward over call. gdb_test "next" ".*NEXT OVER THIS CALL.*" "reverse next over call" +set step_out 0 +gdb_test_multiple "next" "reverse next over recursion" { + -re -wrap ".*NEXT OVER THIS RECURSION.*" { + pass "$gdb_test_name" + } + -re -wrap ".*RECURSIVE CALL.*" { + fail "$gdb_test_name" + set step_out 1 + } +} +if { "$step_out" == 1 } { + gdb_test_multiple "next" "stepping out of recursion" { + -re -wrap "NEXT OVER THIS RECURSION.*" { + set step_out 0 + } + -re -wrap ".*" { + send_gdb "reverse-next\n" + exp_continue + } + } +} + +# Step forward over recursion again so we can test stepping over calls +# inside the recursion itself. +gdb_test_no_output "set exec-dir forward" "forward again to test recursion" +gdb_test "next" "NEXT OVER THIS CALL.*" "reverse next over recursion again" +gdb_test_no_output "set exec-dir reverse" "reverse again to test recursion" + +gdb_test "step" ".*EXIT RECURSIVE FUNCTION.*" "enter recursive function" +set step_pass 1 +gdb_test_multiple "next" "step over recursion inside the recursion" { + -re -wrap ".*EXIT RECURSIVE FUNCTION.*" { + set step_pass 0 + send_gdb "next\n" + exp_continue + } + -re -wrap ".*NEXT OVER THIS RECURSION.*" { + if {$step_pass} { + pass "step over recursion inside the recursion" + } else { + fail "step over recursion inside the recursion" + } + } + -re -wrap ".*" { + send_gdb "next\n" + exp_continue + } +} + # step/next backward with count gdb_test "step 3" ".*REVERSE STEP TEST 1.*" "reverse step test 1" -- 2.37.2