Out-of-order Stepping Behavior Introduced for Constant Infinite Loops at O0
| Bugzilla Link | 41953 |
| Version | trunk |
| OS | Linux |
| CC | @dwblaikie,@DougGregor,@jmorse,@pogo59,@zygoloid,@wjristow |
Extended Description
This behavior is seen at O0 with debug info enabled. It is observed with constant infinite 'while' and 'for' loops. For example:
$ nl test.cpp 1 int main(void) { 2 int a = 0; 3 while (true) 4 ++a; 5 return a; 6 } $
When first stepping into the program the debugger will step to line 4, then line 3. Under some circumstances this would be acceptable for an infinite loop, but imagine that the user sets a breakpoint at line 3. If they run the program and ask for the value of 'a', the debugger will report a value of 1, making it look as if the debugger has skipped an iteration.
Looking at the IR, the line entry for the top of the loop is now attached to an unconditional branch at the bottom of the loop body.
$ clang --version | grep version clang version 9.0.0 (https://github.com/llvm/llvm-project.git f672b6170c... $ clang -g -S -emit-llvm test.cpp $ cat test.ll ... define i32 @main() #0 !dbg !8 { entry: ... while.body: %0 = load i32, i32* %a, align 4, !dbg !16 %inc = add nsw i32 %0, 1, !dbg !16 store i32 %inc, i32* %a, align 4, !dbg !16 br label %while.body, !dbg !17, !llvm.loop !19 } ... !16 = !DILocation(line: 4, scope: !8) !17 = !DILocation(line: 3, scope: !18) ...
The leads to the out-of-order stepping behavior seen in the debugger.
The problem here is that 'while (true)' has no instruction to execute, and therefore nowhere to hang the loop-top line number. If it was 'while (var_that_is_always_true)' there would be a test-and-branch and the stepping behavior would be as expected.
I observe that gcc does not use line 3 for the branch-to-loop-top, while clang does. So, when you say "break 3" for the gcc-built program, there isn't an exact match, and you get the first line-4 instruction, which is what the user expects.