binutils-gdb/gdb/testsuite/gdb.reverse
Carl Love d04ead7406 Fix GDB reverse-step and reverse-next command behavior
Currently GDB when executing in reverse over multiple statements in a single
line of source code, GDB stops in the middle of the line.  Thus requiring
multiple commands to reach the previous line.  GDB should stop at the first
instruction of the line, not in the middle of the line.

The following description of the incorrect behavior was taken from an
earlier message by Pedro Alves <pedro@palves.net>:

https://sourceware.org/pipermail/gdb-patches/2023-January/196110.html

---------------------------------

The source line looks like:

   func1 ();  func2 ();

in the test case:

(gdb) list 1
1       void func1 ()
2       {
3       }
4
5       void func2 ()
6       {
7       }
8
9       int main ()
10      {
11        func1 (); func2 ();
12      }

compiled with:

 $ gcc reverse.c -o reverse -g3 -O0
 $ gcc -v
 ...
 gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

Now let's debug it with target record, using current gdb git master
(f3d8ae90b2),

 $ gdb ~/reverse
 GNU gdb (GDB) 14.0.50.20230124-git
 ...
 Reading symbols from /home/pedro/reverse...
 (gdb) start
 Temporary breakpoint 1 at 0x1147: file reverse.c, line 11.
 Starting program: /home/pedro/reverse
 [Thread debugging using libthread_db enabled]
 Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

 Temporary breakpoint 1, main () at reverse.c:11
 11        func1 (); func2 ();
 (gdb) record

 (gdb) disassemble /s
 Dump of assembler code for function main:
 reverse.c:
 10      {
    0x000055555555513f <+0>:     endbr64
    0x0000555555555143 <+4>:     push   %rbp
    0x0000555555555144 <+5>:     mov    %rsp,%rbp

 11        func1 (); func2 ();
 => 0x0000555555555147 <+8>:     mov    $0x0,%eax
    0x000055555555514c <+13>:    call   0x555555555129 <func1>
    0x0000555555555151 <+18>:    mov    $0x0,%eax
    0x0000555555555156 <+23>:    call   0x555555555134 <func2>
    0x000055555555515b <+28>:    mov    $0x0,%eax

 12      }
    0x0000555555555160 <+33>:    pop    %rbp
    0x0000555555555161 <+34>:    ret
 End of assembler dump.

 (gdb) n
 12      }

So far so good, a "next" stepped over the whole of line 11 and stopped at
line 12.

Let's confirm where we are now:

 (gdb) disassemble /s
 Dump of assembler code for function main:
 reverse.c:
 10      {
    0x000055555555513f <+0>:     endbr64
    0x0000555555555143 <+4>:     push   %rbp
    0x0000555555555144 <+5>:     mov    %rsp,%rbp

 11        func1 (); func2 ();
    0x0000555555555147 <+8>:     mov    $0x0,%eax
    0x000055555555514c <+13>:    call   0x555555555129 <func1>
    0x0000555555555151 <+18>:    mov    $0x0,%eax
    0x0000555555555156 <+23>:    call   0x555555555134 <func2>
    0x000055555555515b <+28>:    mov    $0x0,%eax

 12      }
 => 0x0000555555555160 <+33>:    pop    %rbp
    0x0000555555555161 <+34>:    ret
 End of assembler dump.

Good, we're at the first instruction of line 12.

Now let's undo the "next", with "reverse-next":

 (gdb) reverse-next
 11        func1 (); func2 ();

Seemingly stopped at line 11.  Let's see exactly where:

 (gdb) disassemble /s
 Dump of assembler code for function main:
 reverse.c:
 10      {
    0x000055555555513f <+0>:     endbr64
    0x0000555555555143 <+4>:     push   %rbp
    0x0000555555555144 <+5>:     mov    %rsp,%rbp

 11        func1 (); func2 ();
    0x0000555555555147 <+8>:     mov    $0x0,%eax
    0x000055555555514c <+13>:    call   0x555555555129 <func1>
 => 0x0000555555555151 <+18>:    mov    $0x0,%eax
    0x0000555555555156 <+23>:    call   0x555555555134 <func2>
    0x000055555555515b <+28>:    mov    $0x0,%eax

 12      }
    0x0000555555555160 <+33>:    pop    %rbp
    0x0000555555555161 <+34>:    ret
 End of assembler dump.
 (gdb)

And lo, we stopped in the middle of line 11!  That is a bug, we should have
stepped back all the way to the beginning of the line.  The "reverse-next"
should have fully undone the prior "next" command.

--------------------

This patch fixes the incorrect GDB behavior by ensuring that GDB  stops at
the first instruction in the line.

The test case gdb.reverse/func-map-to-same-line.exp is added to testsuite
to verify this fix when the line table information is and is not available.
2024-01-02 17:46:12 -05:00
..
amd64-tailcall-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
amd64-tailcall-reverse.exp [gdb/testsuite] Add and use is_x86_64_m64_target 2023-01-26 10:09:44 +01:00
amd64-tailcall-reverse.S Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
break-precsave.exp Use require supports_process_record 2023-01-13 13:18:55 -07:00
break-reverse.c gdb/testsuite: Fix many errors in gdb.reverse with clang 2023-08-24 11:08:35 +02:00
break-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
consecutive-precsave.exp Use require supports_process_record 2023-01-13 13:18:55 -07:00
consecutive-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
consecutive-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
finish-precsave.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
finish-reverse-bkpt.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
finish-reverse-bkpt.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
finish-reverse-next.c PowerPC: fix for gdb.reverse/finish-precsave.exp and gdb.reverse/finish-reverse.exp 2023-03-17 16:02:57 -04:00
finish-reverse-next.exp Fix GDB reverse-step and reverse-next command behavior 2024-01-02 17:46:12 -05:00
finish-reverse.c gdb/testsuite: Fix many errors in gdb.reverse with clang 2023-08-24 11:08:35 +02:00
finish-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
fstatat-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
fstatat-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
func-map-to-same-line.c Fix GDB reverse-step and reverse-next command behavior 2024-01-02 17:46:12 -05:00
func-map-to-same-line.exp Fix GDB reverse-step and reverse-next command behavior 2024-01-02 17:46:12 -05:00
getrandom.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
getrandom.exp [gdb/testsuite] Check for sys/random.h in gdb.reverse/getrandom.exp 2023-08-29 11:02:08 +02:00
getresuid-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
getresuid-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
i386-precsave.exp Use require is_x86_like_target 2023-01-13 13:18:55 -07:00
i386-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
i386-reverse.exp Use require is_x86_like_target 2023-01-13 13:18:55 -07:00
i386-sse-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
i386-sse-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
i387-env-reverse.c
i387-env-reverse.exp Use require is_x86_like_target 2023-01-13 13:18:55 -07:00
i387-stack-reverse.c
i387-stack-reverse.exp Use require is_x86_like_target 2023-01-13 13:18:55 -07:00
insn-reverse-aarch64.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
insn-reverse-arm.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
insn-reverse-x86.c gdb/record: Support for rdtscp in i386_process_record. 2023-12-07 10:55:55 +00:00
insn-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
insn-reverse.exp gdb/testsuite: Multiple improvements for gdb.reverse/insn-reverse.exp 2023-08-24 11:08:35 +02:00
machinestate-precsave.exp gdb/testsuite: change newline patterns used in gdb_test 2023-04-27 13:56:37 +01:00
machinestate.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
machinestate.exp gdb/testsuite: change newline patterns used in gdb_test 2023-04-27 13:56:37 +01:00
maint-print-instruction.c gdb: add 'maintenance print record-instruction' command 2023-01-04 11:21:57 +01:00
maint-print-instruction.exp gdb: add 'maintenance print record-instruction' command 2023-01-04 11:21:57 +01:00
map-to-same-line.c PowerPC and aarch64: Fix reverse stepping failure 2024-01-02 17:46:02 -05:00
map-to-same-line.exp PowerPC and aarch64: Fix reverse stepping failure 2024-01-02 17:46:02 -05:00
ms1.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
next-reverse-bkpt-over-sr.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
next-reverse-bkpt-over-sr.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
pipe-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
pipe-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
ppc_record_test_isa_2_06.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
ppc_record_test_isa_2_06.exp Use require with istarget 2023-01-25 09:02:11 -07:00
ppc_record_test_isa_3_1.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
ppc_record_test_isa_3_1.exp Use require with istarget 2023-01-25 09:02:11 -07:00
readv-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
readv-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
recursion.c gdb/record: print frame information when exiting a recursive call 2023-11-20 10:54:03 +01:00
recursion.exp gdb/record: print frame information when exiting a recursive call 2023-11-20 10:54:03 +01:00
recvmsg-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
recvmsg-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
rerun-prec.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
rerun-prec.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
s390-mvcle.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
s390-mvcle.exp Use require with istarget 2023-01-25 09:02:11 -07:00
shr.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
shr1.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
shr2.c gdb/testsuite: fix gdb.reverse/solib-*.exp tests when using clang 2023-08-24 11:08:35 +02:00
sigall-precsave.exp Use require with target_info 2023-03-10 08:21:46 -07:00
sigall-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
sigall-reverse.exp Use require with target_info 2023-03-10 08:21:46 -07:00
singlejmp-reverse-nodebug.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
singlejmp-reverse-nodebug.S Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
singlejmp-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
singlejmp-reverse.exp [gdb/testsuite] Add and use is_x86_64_m64_target 2023-01-26 10:09:44 +01:00
singlejmp-reverse.S Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
solib-precsave.exp gdb/testsuite: fix gdb.reverse/solib-*.exp tests when using clang 2023-08-24 11:08:35 +02:00
solib-reverse.c gdb/testsuite: fix gdb.reverse/solib-*.exp tests when using clang 2023-08-24 11:08:35 +02:00
solib-reverse.exp gdb/testsuite: fix gdb.reverse/solib-*.exp tests when using clang 2023-08-24 11:08:35 +02:00
step-indirect-call-thunk.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
step-indirect-call-thunk.exp Move step_until procedure 2023-03-17 16:02:48 -04:00
step-precsave.exp gdb/testsuite: recognize one more unsupported instruction in gdb.reverse/step-precsave.exp 2023-08-29 13:44:01 -04:00
step-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
step-reverse.exp gdb/testsuite: fix testing gdb.reverse/step-reverse.exp with clang 2023-08-24 11:08:35 +02:00
test_ioctl_TCSETSW.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
test_ioctl_TCSETSW.exp Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
time-reverse.c gdb.reverse/time-reverse.exp: test both time syscall and C time function 2023-02-22 18:09:08 +00:00
time-reverse.exp gdb.reverse/time-reverse.exp: test both time syscall and C time function 2023-02-22 18:09:08 +00:00
until-precsave.exp Revert "X86: reverse-finish fix" 2023-01-18 11:13:17 -05:00
until-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
until-reverse.exp Revert "X86: reverse-finish fix" 2023-01-18 11:13:17 -05:00
ur1.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
waitpid-reverse.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
waitpid-reverse.exp Use require supports_reverse 2023-01-13 13:18:55 -07:00
watch-precsave.exp Use require supports_process_record 2023-01-13 13:18:55 -07:00
watch-reverse.c gdb/testsuite: Fix many errors in gdb.reverse with clang 2023-08-24 11:08:35 +02:00
watch-reverse.exp Fix hardware watchpoints in replay mode 2023-12-06 13:45:59 +01:00