Bug 13277

Summary: gdb does not resolve opaque structures in binaries compiled with ICC.
Product: gdb Reporter: John Steele Scott <toojays>
Component: symtabAssignee: John Steele Scott <toojays>
Status: RESOLVED FIXED    
Severity: normal CC: jan, tromey
Priority: P2    
Version: 7.3   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: Simple test case which can be used to demonstrate bug 13277.
Patch against gdb-7.3.1 to resolve opaque types on ICC compiled binaries.
Simple test case which can be used to demonstrate bug 13277.

Description John Steele Scott 2011-10-09 10:27:14 UTC
gdb does not resolve opaque structures in binaries compiled with ICC.

An example program using a pointer to an opaque structure looks like:

$ cat opaque.c
#include <stdio.h>
#include <stdlib.h>

struct opaque_t
  {
   int wrapped_value;
  };

struct opaque_t *opaque_create (int initializer)
{
 struct opaque_t *opaque = malloc(sizeof(struct opaque_t));

 if (opaque != NULL)
    opaque->wrapped_value = initializer;

 return opaque;
}

void opaque_print (struct opaque_t *p_opaque)
{
 printf("value is %d.\n", p_opaque->wrapped_value);
}

void opaque_destroy (struct opaque_t *p_opaque)
{
 free(p_opaque);
}

$ cat main.c 
#include <stdlib.h>

#include "opaque.h"

int main ()
{
 struct opaque_t *p_opaque = opaque_create(1234);

 if (p_opaque == NULL)
    return 1;

 opaque_print(p_opaque);
 opaque_destroy(p_opaque);

 return 0;
}

If I break this program at line 12 of main.c, and try to print p_opaque, gdb complains "no data fields":

Breakpoint 1, main () at main.c:12
12	 opaque_print(p_opaque);
(gdb) print *p_opaque
$1 = {<No data fields>}

But if I step into a function in opaque.c, gdb *can* see into the structure:

(gdb) step
opaque_print (p_opaque=0xc51e010) at opaque.c:21
21	 printf("value is %d.\n", p_opaque->wrapped_value);
(gdb) print *p_opaque
$2 = {wrapped_value = 1234}

This case works fine when the program is compiled with GCC. Does not work with ICC, even when compiled with "-debug extended".

The problem seems to be that ICC does not set the DW_AT_declaration attribute on the opaque pointer in main.c.

dwarfdumps of binaries produced with GCC and ICC follow.

This binary was produced with 'CC="gcc -g" make':
jscott@saaz:~/src/random/gdb-icc-opaque$ dwarfdump -S match=opaque_t opaque-demo

COMPILE_UNIT<header overall offset = 0>:
<0><   11>	DW_TAG_compile_unit
		DW_AT_producer              GNU C 4.4.5
		DW_AT_language              DW_LANG_C89
		DW_AT_name                  main.c
		DW_AT_comp_dir              /home/jscott/src/random/gdb-icc-opaque
		DW_AT_low_pc                0x400584
		DW_AT_high_pc               0x4005c7
		DW_AT_stmt_list             0

LOCAL_SYMBOLS:
<1><  125>	DW_TAG_subprogram
		DW_AT_external              yes(1)
		DW_AT_name                  main
		DW_AT_decl_file             1 /home/jscott/src/random/gdb-icc-opaque/main.c
		DW_AT_decl_line             5
		DW_AT_type                  <52>
		DW_AT_low_pc                0x400584
		DW_AT_high_pc               0x4005c7
		DW_AT_frame_base            <loclist with 3 entries follows>
			[ 0]<lowpc=0x0><highpc=0x1>DW_OP_breg7+8
			[ 1]<lowpc=0x1><highpc=0x4>DW_OP_breg7+16
			[ 2]<lowpc=0x4><highpc=0x43>DW_OP_breg6+16
		DW_AT_sibling               <182>
<2><  161>	DW_TAG_structure_type
		DW_AT_name                  opaque_t
		DW_AT_declaration           yes(1)

COMPILE_UNIT<header overall offset = 189>:
<0><   11>	DW_TAG_compile_unit
		DW_AT_producer              GNU C 4.4.5
		DW_AT_language              DW_LANG_C89
		DW_AT_name                  opaque.c
		DW_AT_comp_dir              /home/jscott/src/random/gdb-icc-opaque
		DW_AT_low_pc                0x4005c8
		DW_AT_high_pc               0x400639
		DW_AT_stmt_list             63

LOCAL_SYMBOLS:
<1><  125>	DW_TAG_structure_type
		DW_AT_name                  opaque_t
		DW_AT_byte_size             4
		DW_AT_decl_file             1 /home/jscott/src/random/gdb-icc-opaque/opaque.c
		DW_AT_decl_line             5
		DW_AT_sibling               <152>



This binary was produced with 'CC="icc -debug extended" make':

jscott@saaz:~/src/random/gdb-icc-opaque$ dwarfdump -S match=opaque_t opaque-demo-icc-12.0.4 

COMPILE_UNIT<header overall offset = 0>:
<0><   11>	DW_TAG_compile_unit
		DW_AT_comp_dir              /root/gdb-icc-opaque
		DW_AT_low_pc                0x40059c
		DW_AT_language              DW_LANG_C89
		DW_AT_name                  main.c
		DW_AT_producer              Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.0.4.191 Build 20110427
 Fixes SameLinkageName MemberPointers

		DW_AT_stmt_list             0

LOCAL_SYMBOLS:
<1><  275>	DW_TAG_structure_type
		DW_AT_decl_line             1
		DW_AT_decl_column           8
		DW_AT_decl_file             2 /root/gdb-icc-opaque/opaque.h
		DW_AT_accessibility         DW_ACCESS_public
		DW_AT_byte_size             0
		DW_AT_name                  opaque_t

COMPILE_UNIT<header overall offset = 294>:
<0><   11>	DW_TAG_compile_unit
		DW_AT_comp_dir              /root/gdb-icc-opaque
		DW_AT_low_pc                0x4005ec
		DW_AT_language              DW_LANG_C89
		DW_AT_name                  opaque.c
		DW_AT_producer              Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.0.4.191 Build 20110427
 Fixes SameLinkageName MemberPointers

		DW_AT_stmt_list             121

LOCAL_SYMBOLS:
<1><  209>	DW_TAG_structure_type
		DW_AT_decl_line             4
		DW_AT_decl_column           8
		DW_AT_decl_file             1 /root/gdb-icc-opaque/opaque.c
		DW_AT_accessibility         DW_ACCESS_public
		DW_AT_byte_size             4
		DW_AT_name                  opaque_t
Comment 1 John Steele Scott 2011-10-09 10:32:20 UTC
Created attachment 5968 [details]
Simple test case which can be used to demonstrate bug 13277.

The attached tarball contains the source code for this testcase. Binaries produced with ICC and GCC are also included for comparison.

The ICC binary was produced with 'CC="icc -debug extended" make'.

The GCC binary was produced with 'CC="gcc -g" make'.
Comment 2 John Steele Scott 2011-10-09 10:39:38 UTC
Created attachment 5969 [details]
Patch against gdb-7.3.1 to resolve opaque types on ICC compiled binaries.

This patch against gdb-7.3.1 sets the "stub" flag on structures with a zero size attribute, which allows GDB to resolve the opaque structure pointers produced by ICC.
Comment 3 Tom Tromey 2011-10-10 19:03:58 UTC
For the patch, please follow:
http://sourceware.org/gdb/contribute/
Comment 4 John Steele Scott 2011-10-16 21:47:12 UTC
Patch submission is at http://sourceware.org/ml/gdb-patches/2011-10/msg00442.html
Comment 5 John Steele Scott 2011-11-13 09:18:20 UTC
Created attachment 6055 [details]
Simple test case which can be used to demonstrate bug 13277.

I've made a simpler testcase, which I'm attaching here.

The tarball also includes the assembler generated by ICC. I've used that to construct a test case suitable for committing to the GDB mainline, I'll send that to gdb-patches shortly.
Comment 6 Sourceware Commits 2012-05-24 22:09:26 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	jkratoch@sourceware.org	2012-05-24 22:09:21

Modified files:
	gdb            : ChangeLog dwarf2read.c 
	gdb/testsuite  : ChangeLog 
Added files:
	gdb/testsuite/gdb.dwarf2: dw2-icc-opaque.S dw2-icc-opaque.exp 

Log message:
	gdb/
	PR symtab/13277: Resolving opaque structures in ICC generated binaries.
	* dwarf2read.c (struct dwarf2_cu) <producer_is_icc>: New field.
	(producer_is_gxx_lt_4_6): Move the checking and caching to...
	(check_producer): ... this new function, which also checks for ICC
	and caches the result.
	(producer_is_icc): New function.
	(read_structure_type): Don't set TYPE_STUB_SUPPORTED if the
	producer was ICC.
	
	gdb/testsuite/
	PR symtab/13277: Resolving opaque structures in ICC generated binaries.
	* gdb.dwarf2/dw2-icc-opaque.S: New file.
	* gdb.dwarf2/dw2-icc-opaque.exp: New file.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.14292&r2=1.14293
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/dwarf2read.c.diff?cvsroot=src&r1=1.655&r2=1.656
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.3205&r2=1.3206
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-icc-opaque.S.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-icc-opaque.exp.diff?cvsroot=src&r1=NONE&r2=1.1
Comment 7 Jan Kratochvil 2012-05-24 22:28:05 UTC
Checked in.