Description
Problem
During LLDB debug session, global variables that are declared in a previous expression evaluation is sometimes reported as undeclared identifier after a program restart.
TL;DR LLDB output (see full output at the end):
(lldb) p int $i = 32; $i
(int) 32
...
(lldb) r
...
(lldb) p $i
(int) 32
(lldb) p $i + a
error: <user expression 2>:1:1: use of undeclared identifier '$i'
1 | $i + a
| ^
(lldb) p $i
error: <user expression 3>:1:1: use of undeclared identifier '$i'
1 | $i
| ^
(lldb)
In the above, there are two issues:
$i
in$i + a
was deemed undeclared after a print of$i
itself was successful.- After the above issue happened, the printing of
$i
itself no longer works, either.
Repro steps
Create a main.cpp
:
int main() {
int a = 1;
return a;
}
Compile:
clang++ -g -O0 main.cpp
Start lldb a.out
then type the following comands. The output from LLDB follows.
b /main/
r
p a
p int $i = 32; $i
p $i + a
r
p $i
p $i + a
p $i
Full LLDB output:
(lldb) target create "a.out"
Current executable set to '/Users/<username>/tmp2/a.out' (arm64).
(lldb) b /main/
Breakpoint 1: where = a.out`main + 12 at main.cpp:2:9, address = 0x0000000100003f98
(lldb) r
Process 73517 launched: '/Users/<username>/tmp2/a.out' (arm64)
Process 73517 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100003f98 a.out`main at main.cpp:2:9
1 int main() {
-> 2 int a = 1;
3 return a;
4 }
(lldb) p a
(int) -2035101492
(lldb) p int $i = 32; $i
(int) 32
(lldb) p $i + a
(int) -2035101460
(lldb) r
There is a running process, kill it and restart?: [Y/n]
Process 73517 exited with status = 9 (0x00000009) killed
Process 73523 launched: '/Users/<username>/tmp2/a.out' (arm64)
Process 73523 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100003f98 a.out`main at main.cpp:2:9
1 int main() {
-> 2 int a = 1;
3 return a;
4 }
(lldb) p $i
(int) 32
(lldb) p $i + a
error: <user expression 2>:1:1: use of undeclared identifier '$i'
1 | $i + a
| ^
(lldb) p $i
error: <user expression 3>:1:1: use of undeclared identifier '$i'
1 | $i
| ^
(lldb)