Clang/LLVM producing incomplete & erroneous debug information

Hi everyone,

I’m trying to get debug information for a C/pthreads application, but it seems like clang/LLVM are producing limited & erroneous debugging information. I’ve attached a simple test example to reproduce the problem – I’m using clang/LLVM 3.7.1 built from source on Ubuntu 14.04.

I compile the attached file with:

$ clang -O1 -g test.c -lpthread

If I run:

$ readelf --debug-dump=info ./a.out

I see the following debugging information in the binary for “main”:

<1>: Abbrev Number: 7 (DW_TAG_subprogram)
DW_AT_low_pc : 0x400880
DW_AT_high_pc : 0xc9
Unknown AT value: 3fe7: 1
DW_AT_frame_base : 1 byte block: 57 (DW_OP_reg7 (rsp))
DW_AT_name : (indirect string, offset: 0xaf): main
DW_AT_decl_file : 1
DW_AT_decl_line : 16
DW_AT_prototyped : 1
DW_AT_type : <0x3f>
DW_AT_external : 1
Unknown AT value: 3fe1: 1
<2>: Abbrev Number: 8 (DW_TAG_formal_parameter)
DW_AT_location : 0x23 (location list)
DW_AT_name : (indirect string, offset: 0xc2): argc
DW_AT_decl_file : 1
DW_AT_decl_line : 16
DW_AT_type : <0x3f>
<2>: Abbrev Number: 8 (DW_TAG_formal_parameter)
DW_AT_location : 0x48 (location list)
DW_AT_name : (indirect string, offset: 0xc7): argv
DW_AT_decl_file : 1
DW_AT_decl_line : 16
DW_AT_type : <0x5e>
<2>: Abbrev Number: 10 (DW_TAG_variable)
DW_AT_const_value : 0
DW_AT_name : (indirect string, offset: 0xcc): i
DW_AT_decl_file : 1
DW_AT_decl_line : 18
DW_AT_type : <0x3f>
<2>: Abbrev Number: 11 (DW_TAG_variable)
DW_AT_name : (indirect string, offset: 0xce): children
DW_AT_decl_file : 1
DW_AT_decl_line : 19
DW_AT_type : <0x47>
<2>: Abbrev Number: 0

Notice that there are no location lists for local variable “children” (a malloc’d pthread_t pointer), and the information for local variable “i” says it has a constant value, even though it changes in the application!

When I compile with gcc:
$ gcc -O1 -g test.c -lpthread

And do a readelf, I get the following information:

<1><3c3>: Abbrev Number: 21 (DW_TAG_subprogram)
<3c4> DW_AT_external : 1
<3c4> DW_AT_name : (indirect string, offset: 0x23f): main
<3c8> DW_AT_decl_file : 1
<3c9> DW_AT_decl_line : 16
<3ca> DW_AT_prototyped : 1
<3ca> DW_AT_type : <0x3f>
<3ce> DW_AT_low_pc : 0x4008a0
<3d6> DW_AT_high_pc : 0xe5
<3de> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)
<3e0> DW_AT_GNU_all_call_sites: 1
<3e0> DW_AT_sibling : <0x58b>
<2><3e4>: Abbrev Number: 22 (DW_TAG_formal_parameter)
<3e5> DW_AT_name : (indirect string, offset: 0x138): argc
<3e9> DW_AT_decl_file : 1
<3ea> DW_AT_decl_line : 16
<3eb> DW_AT_type : <0x3f>
<3ef> DW_AT_location : 0x6f (location list)
<2><3f3>: Abbrev Number: 22 (DW_TAG_formal_parameter)
<3f4> DW_AT_name : (indirect string, offset: 0x107): argv
<3f8> DW_AT_decl_file : 1
<3f9> DW_AT_decl_line : 16
<3fa> DW_AT_type : <0x58b>
<3fe> DW_AT_location : 0xa8 (location list)
<2><402>: Abbrev Number: 23 (DW_TAG_variable)
<403> DW_AT_name : i
<405> DW_AT_decl_file : 1
<406> DW_AT_decl_line : 18
<407> DW_AT_type : <0x3f>
<40b> DW_AT_location : 0xe1 (location list)
<2><40f>: Abbrev Number: 28 (DW_TAG_variable)
<410> DW_AT_name : (indirect string, offset: 0xf4): children
<414> DW_AT_decl_file : 1
<415> DW_AT_decl_line : 19
<416> DW_AT_type : <0x591>
<41a> DW_AT_location : 0x13f (location list)
<2><41e>: Abbrev Number: 24 (DW_TAG_inlined_subroutine)
<41f> DW_AT_abstract_origin: <0x305>
<423> DW_AT_low_pc : 0x4008ab

…(more information for main)…

The “children” variable has a location list, and “I” is not listed as having a constant value. Note that if I compile with clang using -O0, there’s a location list for both “children” and “i”.

Does anybody know what’s going on here? I understand the nature of debugging information – the compiler provides a best-effort attempt to specify where things are, but optimization make things tricky. However, I would counter with the fact that this is a very simple program being compiled with basic optimizations. Is the compiler just unable to generate variable locations for this program?

test.c (782 Bytes)

I’ve seen these kinds of problems before:

https://llvm.org/bugs/show_bug.cgi?id=21881

https://llvm.org/bugs/show_bug.cgi?id=22099

Basically, there are various problems with debug info for optimized code and nobody has yet invested the time and energy to track them down and fix them. I am more guilty than most, as I make noise about it and then can’t manage to get it to bubble up high enough on my priority list.

Sorry I can’t be more help (at least not right now)…

–paulr