From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7862) id 00425385356E; Fri, 21 Oct 2022 10:49:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00425385356E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666349387; bh=D4a4ufuXSO2hj2lL9VYbrCyXkwF3iZcqQLpYKtt46nI=; h=From:To:Subject:Date:From; b=GK8DI+MDKUlIO32dCO+HMxxLtg+PN7//cGcz4xGfmRHP1QM5QoDbaGiSW2nMYjJ7c FqIhxCe7/TEMEDWKA8IJU/VH8rCX8M3rnJKlvjpBOOTzFkW0nQbH4WlTII1FYbOiZa i0P1nxv4/dv5ObESNW5niCL8tUxM39dxCIKWYdsQ= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Bruno Larsen To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/reverse: Fix stepping over recursive functions X-Act-Checkin: binutils-gdb X-Git-Author: Bruno Larsen X-Git-Refname: refs/heads/master X-Git-Oldrev: 49d7cd733a7f1b87aa1d40318b3d7c2b65aca5ac X-Git-Newrev: 1f3e37e057e876b37db49dbd8ed5ca22c33f6772 Message-Id: <20221021104947.00425385356E@sourceware.org> Date: Fri, 21 Oct 2022 10:49:46 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D1f3e37e057e8= 76b37db49dbd8ed5ca22c33f6772 commit 1f3e37e057e876b37db49dbd8ed5ca22c33f6772 Author: Bruno Larsen Date: Wed May 25 15:02:47 2022 -0300 gdb/reverse: Fix stepping over recursive functions =20 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. =20 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. =20 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. Diff: --- gdb/infrun.c | 2 +- gdb/testsuite/gdb.reverse/step-precsave.exp | 6 ++-- gdb/testsuite/gdb.reverse/step-reverse.c | 19 +++++++++- gdb/testsuite/gdb.reverse/step-reverse.exp | 55 +++++++++++++++++++++++++= +--- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index 9a9f42fc903..fcc4d80fc73 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -7130,7 +7130,7 @@ process_event_stop_test (struct execution_control_sta= te *ecs) sr_sal.pc =3D ecs->stop_func_start; sr_sal.pspace =3D 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/gd= b.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" =20 # step over call =20 -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" =20 # step into call @@ -280,9 +281,10 @@ gdb_test_multiple "step" "$test_message" { } } =20 -# next backward over call +# Next backward over calls. =20 gdb_test "next" ".*NEXT OVER THIS CALL.*" "reverse next over call" +gdb_test "next" ".*NEXT OVER THIS RECURSION.*" "reverse next over recursiv= e call" =20 # step/next backward with count =20 diff --git a/gdb/testsuite/gdb.reverse/step-reverse.c b/gdb/testsuite/gdb.r= everse/step-reverse.c index aea2a98541d..809c7d16dc9 100644 --- a/gdb/testsuite/gdb.reverse/step-reverse.c +++ b/gdb/testsuite/gdb.reverse/step-reverse.c @@ -26,6 +26,20 @@ int callee() { /* ENTER CALLEE */ return myglob++; /* ARRIVED IN CALLEE */ } /* RETURN FROM CALLEE */ =20 +/* 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 =3D=3D 0) + return 0; + val /=3D 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 +65,9 @@ int main () { y =3D y + 4; z =3D z + 5; /* STEP TEST 2 */ =20 + /* 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 */ =20 @@ -60,7 +77,7 @@ int main () { /* Test "stepi" */ a[5] =3D a[3] - a[4]; /* FINISH TEST */ callee(); /* STEPI TEST */ - =20 + /* Test "nexti" */ callee(); /* NEXTI TEST */ =20 diff --git a/gdb/testsuite/gdb.reverse/step-reverse.exp b/gdb/testsuite/gdb= .reverse/step-reverse.exp index 997b62604d5..c28e1f6db4f 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" =20 +# Next through a recursive function call. +gdb_test "next 2" "NEXT OVER THIS CALL.*" "next over recursion" + # step over call =20 -gdb_test "step" ".*NEXT OVER THIS CALL.*" "step up to call" gdb_test "next" ".*STEP INTO THIS CALL.*" "next over call" =20 # step into call @@ -118,7 +120,7 @@ gdb_test_multiple "stepi" "$test_message" { =20 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" { ### =20 # Set reverse execution direction - gdb_test_no_output "set exec-dir reverse" "set reverse execution" =20 # stepi backward thru return and into a function @@ -243,10 +244,56 @@ gdb_test_multiple "step" "$test_message" { } } =20 -# next backward over call +# Next backward over call. =20 gdb_test "next" ".*NEXT OVER THIS CALL.*" "reverse next over call" =20 +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" =3D=3D 1 } { + gdb_test_multiple "next" "stepping out of recursion" { + -re -wrap "NEXT OVER THIS RECURSION.*" { + set step_out 0 + } + -re -wrap ".*" { + send_gdb "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 seen_recursive_call 0 +gdb_test_multiple "next" "step over recursion inside the recursion" { + -re -wrap ".*RECURSIVE CALL.*" { + incr seen_recursive_call + send_gdb "next\n" + exp_continue + } + -re -wrap ".*NEXT OVER THIS RECURSION.*" { + gdb_assert {"$seen_recursive_call" =3D=3D 1} \ + "step over recursion inside the recursion" + } + -re -wrap ".*" { + send_gdb "next\n" + exp_continue + } +} + # step/next backward with count =20 gdb_test "step 3" ".*REVERSE STEP TEST 1.*" "reverse step test 1"