llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Incorrect location range for local variable in the register clobbered by callee for GDB tune

Open djtodoro opened this issue 5 years ago • 3 comments

Bugzilla Link 49641
Resolution DUPLICATE
Resolved on Mar 22, 2021 02:36
Version trunk
OS Linux
CC @adrian-prantl,@dstenb,@JDevlieghere,@walkerkd,@pogo59

Extended Description

Please consider these two files:

$ cat test2.c void f1(int); int f2(); void f3();

void f() { f1(0); int local = f2(); f1(local); f3(); }

$ cat main-for-core.c #include <stdio.h>

void f1(int param) { if (param % 2 == 0) return; printf("HELLO\n"); abort(); }

int f2() { int x; scanf("%d", &x); return x; }

void f3() { int y; scanf("%d", &y); printf("%d\n", y); }

int main() { f(); return 0; }

Compilation of the files:

$ clang -g -O2 test2.c -c -o object-file-test2.o $ clang -g -O2 main-for-core.c object-file-test2.o -o main-for-core

By examining debug_loc from object-file-test2.o for the local var we see: 00000000 000000000000000f 0000000000000016 (DW_OP_reg0 (rax))

And the code for the f() is: 0000000000000000 : 0: push %rax 1: xor %edi,%edi 3: callq 8 <f+0x8> 8: xor %eax,%eax a: callq f <f+0xf> f: mov %eax,%edi 11: callq 16 <f+0x16> 16: xor %eax,%eax 18: pop %rcx 19: jmpq 1e <f+0x1e>

While debugging it, by loading the core file generated due to abort() (as an input I typed 5):

$ gdb main-for-core ... (gdb) bt #​0 0x00007fb2d7aeb377 in raise () from /lib64/libc.so.6 #​1 0x00007fb2d7aeca68 in abort () from /lib64/libc.so.6 #​2 0x0000000000401197 in f1 (param=) at main-for-core.c:7 #​3 0x0000000000401216 in f () at test2.c:8 #​4 0x00000000004011f8 in main () at main-for-core.c:23 (gdb) f 3 #​3 0x0000000000401216 in f () at test2.c:8 8 f1(local); (gdb) disassemble Dump of assembler code for function f: 0x0000000000401200 <+0>: push %rax 0x0000000000401201 <+1>: xor %edi,%edi 0x0000000000401203 <+3>: callq 0x401180 0x0000000000401208 <+8>: xor %eax,%eax 0x000000000040120a <+10>: callq 0x4011a0 0x000000000040120f <+15>: mov %eax,%edi 0x0000000000401211 <+17>: callq 0x401180 => 0x0000000000401216 <+22>: xor %eax,%eax 0x0000000000401218 <+24>: pop %rcx 0x0000000000401219 <+25>: jmpq 0x4011c0 End of assembler dump. (gdb) p local $1 = 0

But it should be reported as <optimized_out>, because the %eax was clobbered by the call.

GCC produces different high pc address to keep GDB working properly: 00000000 000000000000000f 0000000000000015 (DW_OP_reg0 (rax))

LLDB works OK for this case.

I've tried to change the DwarfDebug, to generate the debug loc entry ending address as addr(endLabel) - 1, as GCC, for GDB tune, and it works -- GDB prints optimized_out for the local var.

djtodoro avatar Mar 19 '21 12:03 djtodoro

*** This bug has been marked as a duplicate of bug llvm/llvm-project#39099 ***

djtodoro avatar Mar 22 '21 09:03 djtodoro

@​David Thanks for the reply!

I see... it is the same issue. I've reproduced this on X86_64 target.

I prepared a patch to trim the ranges for GDB [1]. I've made a very similar one as well...

djtodoro avatar Mar 19 '21 14:03 djtodoro

This looks like [0]. I prepared a patch to trim the ranges for GDB [1]. However, people thought it was better for GDB to handle this, so after discussions I abandoned that and wrote a GDB ticket instead [2]. We have implemented this for our downstream target in GDB. I don't think this has happened for any upstream targets as far as I know, but it was a while seen I checked that.

[0] llvm/llvm-project#39099 [1] https://reviews.llvm.org/D68465 [2] https://sourceware.org/bugzilla/show_bug.cgi?id=25143

dstenb avatar Mar 19 '21 13:03 dstenb