public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "vries at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug gdb/29996] New: [gdb/reverse] Reverse stepping skips first line in function in optimized code
Date: Fri, 13 Jan 2023 11:25:20 +0000	[thread overview]
Message-ID: <bug-29996-4717@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=29996

            Bug ID: 29996
           Summary: [gdb/reverse] Reverse stepping skips first line in
                    function in optimized code
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: gdb
          Assignee: unassigned at sourceware dot org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

Consider test-case test.c:
...
$ cat -n test.c
     1  int i, j, k;
     2  int x, y, z;
     3
     4  void __attribute__((noinline, noclone))
     5  foo (void)
     6  {
     7    i = x;
     8    j = y;
     9    k = z;
    10  }
    11
    12  int
    13  main (void)
    14  {
    15    foo ();
    16    return 0;
    17  }
...

Compiled with -O2 (using gcc 7.5.0):
...
$ gcc -g -O2 test.c
...

Let's step through the program, while recording:
...
$ gdb -q a.out -ex start -ex record
Reading symbols from a.out...
Temporary breakpoint 1 at 0x4003c0: file test.c, line 15.
Starting program: a.out 

Temporary breakpoint 1, main () at test.c:15
15        foo ();
(gdb) step
foo () at test.c:7
7         i = x;
(gdb) step
8         j = y;
(gdb) step
9         k = z;
(gdb) step
10      }
(gdb) 
...

So far, so good.

Now let's try reverse stepping:
...
10      }
(gdb) reverse-step
9         k = z;
(gdb) reverse-step
8         j = y;
(gdb) reverse-step

No more reverse-execution history.
main () at test.c:15
15        foo ();
(gdb) 
...

Hmm, why did we skip over line 7?

Minimal reproducer:
...
$ gdb -q -batch a.out -ex start -ex record -ex "break 8" -ex continue \
  -ex reverse-step
Temporary breakpoint 1 at 0x4003c0: file test.c, line 15.

Temporary breakpoint 1, main () at test.c:15
15        foo ();
Breakpoint 2 at 0x4004bc: file test.c, line 8.

Breakpoint 2, foo () at test.c:8
8         j = y;

No more reverse-execution history.
main () at test.c:15
15        foo ();
...

It's because of this piece of code:
...
      /* When stepping backward, stop at beginning of line range                
         (unless it's the function entry point, in which case                   
         keep going back to the call point).  */
      CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
      if (stop_pc == ecs->event_thread->control.step_range_start
          && stop_pc != ecs->stop_func_start
          && execution_direction == EXEC_REVERSE)
        end_stepping_range (ecs);
      else
        keep_going (ecs);
...
which does the wrong thing for optimized code.

For forward execution, the relevant logic is in skip_prologue_sal, see the
locations_valid () test.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

                 reply	other threads:[~2023-01-13 11:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-29996-4717@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).