[DebubInfo]location is not right for static variable when optimization is enabled.
static int data2 = 10;
void set1()
{
data2 = 100;
}
void set2()
{
data2 = 10;
}
int get()
{
return data2;
}
int main()
{
set2();
set1();
return get();
}
clang 1.c -O2 -g
$ llvm-dwarfdump -i a.out
.debug_info contents:
0x00000000: Compile Unit: length = 0x0000007e, format = DWARF32, version = 0x0005, unit_type = DW_UT_compile, abbr_offset = 0x0000, addr_size = 0x08 (next unit at 0x00000082)
0x00000027: DW_TAG_variable
DW_AT_name ("data2")
DW_AT_type (0x0000003a "int")
DW_AT_decl_file ("103.c")
DW_AT_decl_line (1)
DW_AT_location (DW_OP_addrx 0x0, DW_OP_deref_size 0x1, DW_OP_constu 0x5a, DW_OP_mul, DW_OP_lit10, DW_OP_plus, DW_OP_stack_value)
The location for data2 is not right, the content at address index addrx 0x0 is 0, so it always returns 10 for p data2 in GDB.
(gdb) b main
Breakpoint 1 at 0x10958: file 103.c, line 18.
(gdb) r
Starting program: a.out
Breakpoint 1, set1 () at 103.c:4
4 data2 = 100;
(gdb) p data2
$1 = 10
(gdb) n
main () at 103.c:19
19 return get();
(gdb) p data2
$2 = 10 ////This seems not right, should be 100 now.
@llvm/issue-subscribers-debuginfo
I can't replicate this with 830b359d3ab, where the assembly is:
(gdb) disassemble
Dump of assembler code for function main:
=> 0x0000555555555170 <+0>: movb $0x1,0x2eb5(%rip) # 0x55555555802c <data2>
0x0000555555555177 <+7>: mov $0x64,%eax
0x000055555555517c <+12>: retq
The value of data2 is presented as 10 on the first instruction, and 100 on the second and third. Using line stepping, gdb stops on the call to get and presents the value as 100:
(gdb) start
Temporary breakpoint 1 at 0x1170: file test.c, line 16.
Starting program: /tmp/out
Temporary breakpoint 1, main () at test.c:18
18 set1();
(gdb) watch data2
Watchpoint 2: data2
(gdb) s
set1 () at test.c:16
16 {
(gdb) s
Watchpoint 2: data2
Old value = 10
New value = 100
main () at test.c:19
19 return get();
(gdb) s
Weird, here is what I get for X86_64 target:
clang 103.c -g --target=x86_64 -O2 -c
llvm-dwarfdump -a 103.o
0x00000023: DW_TAG_variable
DW_AT_name ("data2")
DW_AT_type (0x00000036 "int")
DW_AT_decl_file ("103.c")
DW_AT_decl_line (1)
DW_AT_location (DW_OP_addrx 0x0, DW_OP_deref_size 0x1, DW_OP_constu 0x5a, DW_OP_mul, DW_OP_lit10, DW_OP_plus, DW_OP_stack_value)
With this DWARF info, I don't understand above behavior in debugger. The data2 should be a constant 10. Content in DW_OP_addrx 0x0(unresolved) is 0.
Oh, I know what I am wrong here. I thought the content in DW_OP_addrx 0x0 will not be changed, but that's not true, when data2 is changed, like @jmorse pointed out, the content there is also changed, so we can get data2 is changed to 100.
Sorry for the confusion.