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

Broken bitfield debug info in Dwarf4

Open mveriksson opened this issue 10 years ago • 5 comments

Bugzilla Link 27758
Resolution FIXED
Resolved on May 27, 2016 10:21
Version trunk
OS Linux
CC @adrian-prantl,@echristo,@pogo59

Extended Description

I am debugging an issue with bitfields and debug info and found that this commit is where the problems begin.

The file tmp.c:

struct fields { unsigned int a : 1; unsigned int b : 1; } f;

int main () { f.a = 1; return 0; }

Built with top-of-tree clang and run in gdb:

gdb a.out GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1 ... Reading symbols from a.out...done. (gdb) break main Breakpoint 1 at 0x40058d: file tmp.c, line 9. (gdb) run Starting program: a.out

Breakpoint 1, main () at tmp.c:9 9 f.a = 1; (gdb) display f 1: f = {a = 0, b = 0} (gdb) n 10 return 0; 1: f = {a = 1, b = 1} <<<--- Unexpected value of b! (gdb) x &f 0x402024 : 0x00000001 (gdb)

I have identified the first commit where this is broken as this one: http://reviews.llvm.org/D19630

mveriksson avatar May 16 '16 18:05 mveriksson

I committed r269827, "Debug Info: Don't emit bitfields in the DWARF4 format when tuning for GDB.".

Let's hope we get GDB support soon!

adrian-prantl avatar May 17 '16 22:05 adrian-prantl

Thank you for looking at this!

When DW_AT_data_bit_offset was discussed on a gcc mailing list (2011) they say that gdb does not support it.

https://gcc.gnu.org/ml/gcc-patches/2011-03/msg00077.html

I looked through gdb's changelog and could not find any mention of it since either. So maybe gdb still lacks the support?

mveriksson avatar May 17 '16 10:05 mveriksson

I fixed the DW_AT_data_member bug in r269731.

Now the DWARF looks like this:

0x0000003f: DW_TAG_structure_type [3] * DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000d9] = "fields") DW_AT_byte_size [DW_FORM_data1] (0x04) DW_AT_decl_file [DW_FORM_data1] ("test.c") DW_AT_decl_line [DW_FORM_data1] (1)

0x00000047: DW_TAG_member [4]
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000c8] = "a") DW_AT_type [DW_FORM_ref4] (cu + 0x0062 => {0x00000062}) DW_AT_decl_file [DW_FORM_data1] ("test.c") DW_AT_decl_line [DW_FORM_data1] (3) DW_AT_bit_size [DW_FORM_data1] (0x01) DW_AT_data_bit_offset [DW_FORM_data1] (0x00)

0x00000054: DW_TAG_member [4]
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000d7] = "b") DW_AT_type [DW_FORM_ref4] (cu + 0x0062 => {0x00000062}) DW_AT_decl_file [DW_FORM_data1] ("test.c") DW_AT_decl_line [DW_FORM_data1] (4) DW_AT_bit_size [DW_FORM_data1] (0x01) DW_AT_data_bit_offset [DW_FORM_data1] (0x01)

It looks like this still isn't enough for gdb to understand the format. I would suggest to report this as a bug to the gdb developers.

Meanwhile I suppose we can hide the DWARF 4 representation of bitfields behind the gdb debugger tuning option.

Eric and Paul on the CC list — do you see any errors in the above representation that I might have missed?

adrian-prantl avatar May 17 '16 04:05 adrian-prantl

I fixed the DW_AT_byte_size bug in r269714. Same thing also goes for DW_AT_member_location:

"The member entry corresponding to a data member that is defined in a structure, union or class may have either a DW_AT_data_member_location attribute or a DW_AT_data_bit_offset attribute."

adrian-prantl avatar May 17 '16 00:05 adrian-prantl

0x0000003f: TAG_structure_type [3] * AT_name( "fields" ) AT_byte_size( 0x04 ) AT_decl_file( "/Volumes/Data/llvm/test.c" ) AT_decl_line( 1 )

0x00000047: TAG_member [4]
AT_name( "a" ) AT_type( {0x00000066} ( unsigned int ) ) AT_decl_file( "/Volumes/Data/llvm/test.c" ) AT_decl_line( 3 ) AT_byte_size( 0x04 ) AT_bit_size( 0x01 ) AT_data_bit_offset( 0x00 ) AT_data_member_location( 0x00 )

0x00000056: TAG_member [4]
AT_name( "b" ) AT_type( {0x00000066} ( unsigned int ) ) AT_decl_file( "/Volumes/Data/llvm/test.c" ) AT_decl_line( 4 ) AT_byte_size( 0x04 ) AT_bit_size( 0x01 ) AT_data_bit_offset( 0x01 ) AT_data_member_location( 0x00 )

0x00000065: NULL

The DWARF contains a violation of the spec (emphasis mine): "If the size of a data member is not the same as the size of the type given for the data member, the data member has either a DW_AT_byte_size or a DW_AT_bit_size attribute whose integer constant value (see Section 2.19 on page 51) is the amount of storage needed to hold the value of the data member."

We should not be emitting a DW_AT_byte_size for bit fields. I don't know whether that is what gdb is falling over.

adrian-prantl avatar May 16 '16 22:05 adrian-prantl