Bug 26058 - Inconsistent order of execution at -O2
Summary: Inconsistent order of execution at -O2
Status: RESOLVED DUPLICATE of bug 25507
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-27 22:01 UTC by Anonymous
Modified: 2021-01-13 12:23 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
a.out (2.83 KB, application/x-executable)
2020-05-27 22:01 UTC, Anonymous
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Anonymous 2020-05-27 22:01:55 UTC
Created attachment 12574 [details]
a.out

$ gdb --version
GNU gdb (GDB) 10.0.50.20200517-git
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Consider test-case:
...
$ cat small.c
__attribute__((noinline))
void g(int x) {
  asm volatile ("" : "+r" (x));
}

__attribute__((noinline))
int f(int a, int b, int d) {
  int r = -1;
  b += d;
  if (d == a)
    r = b - d;
  g (b);
  return r;
}

int main() {
  int l;
  asm ("" : "=r" (l) : "0" (0));
  f (l + 0, l + 1, l + 4);
  return 0;
}
...

When stepping through the program by using "step", line 11 is skipping over. 
...
$ gcc -O2 -g small.c; gdb -q a.out
Reading symbols from a.out...
(gdb) b main
Breakpoint 1 at 0x401020: file small.c, line 18.
(gdb) r
Starting program: /home/yibiao/Debugger/a.out 

Breakpoint 1, main () at small.c:18
18	  asm ("" : "=r" (l) : "0" (0));
(gdb) step
19	  f (l + 0, l + 1, l + 4);
(gdb) step
f (a=a@entry=0, b=b@entry=1, d=d@entry=4) at small.c:9
9	  b += d;
(gdb) step
10	  if (d == a)
(gdb) step
12	  g (b);
(gdb) 


...

When stepping through the program by using "stepi", line 11 is hit by gdb after line 9. 
...

$ gcc -O2 -g small.c; gdb -q a.out
Reading symbols from a.out...
(gdb) b main
Breakpoint 1 at 0x401020: file small.c, line 18.
(gdb) r
Starting program: /home/yibiao/Debugger/a.out 

Breakpoint 1, main () at small.c:18
18	  asm ("" : "=r" (l) : "0" (0));
(gdb) stepi
19	  f (l + 0, l + 1, l + 4);
(gdb) stepi
0x0000000000401025	19	  f (l + 0, l + 1, l + 4);
(gdb) stepi
0x0000000000401028	19	  f (l + 0, l + 1, l + 4);
(gdb) stepi
f (a=a@entry=0, b=b@entry=1, d=d@entry=4) at small.c:9
9	  b += d;
(gdb) stepi
0x0000000000401133	11	    r = b - d;
(gdb)
Comment 1 Anonymous 2021-01-13 12:23:04 UTC
Gdb does not support location views. This is all known behaviour.

*** This bug has been marked as a duplicate of bug 25507 ***