[Dwarf-Discuss] Array Information

John DelSignore jdelsign@totalviewtech.com
Wed Mar 12 16:35:22 GMT 2008


You are confused, but don't worry, this stuff is confusing...

Array types normally don't have names. An array is a composition of (a) some number of dimensions (in C 1-dimension) with bounds information, and (b) an element type, like "int" or "double" or "struct foobar".

Let's take a look at array "a" your example. In your code, you wrote:

  int a[20];

What does this mean in the C language? It means this:
1) "a" is a local variable.
2) "a" has type array of 20 "int" types.

This structure is reflected in the DWARF...

(1) "a" is a variable with the type at DIE <15c> and a location -100 off the frame base register:
<2><129>: Abbrev Number: 5 (DW_TAG_variable)
  <12a>     DW_AT_name        : a   
  <12c>     DW_AT_decl_file   : 1   
  <12d>     DW_AT_decl_line   : 5   
  <12e>     DW_AT_type        : <15c>   
  <132>     DW_AT_location    : 3 byte block: 91 9c 7f     (DW_OP_fbreg: -100)

(2) Type <15c> is an array with an element type of <bf>, and has 1 dimension with the implied lower bound of 0 and the declared upper bound of 10:
 <1><15c>: Abbrev Number: 6 (DW_TAG_array_type)
  <15d>     DW_AT_sibling     : <16c>   
  <161>     DW_AT_type        : <bf>   
 <2><165>: Abbrev Number: 7 (DW_TAG_subrange_type)
  <166>     DW_AT_type        : <fd>   
  <16a>     DW_AT_upper_bound : 19    

Note that the subrange type has its own type DIE <fd>, which is the index type for the subrange (unsigned int), but I won't go into that here.

(3) Type <bf> is is an "int", which is a 4-byte signed integer.
 <1><bf>: Abbrev Number: 3 (DW_TAG_base_type)
  <c0>     DW_AT_name        : int   
  <c4>     DW_AT_byte_size   : 4   
  <c5>     DW_AT_encoding    : 5    (signed)

So, given the above information, the DWARF consumer (usually a debugger) can reconstruct the type declaration for the variable named "a". It does this by understanding DWARF and the syntax of the programming language. Again, in the C language, the type of "a" is "array of subrange 0 to 19 of int". The way you express that in C "int a[20]". For example, in the TotalView debugger is you ask "what is a", it says:

d1.<> dwhat a
In thread 1.1:
Name: a; Type: int[20]; Size: 80 bytes; Addr: 0xfea7eca8
    Scope: ##/nfs/netapp0/user/home/jdelsign/a.out#xxx.c#main (Scope class: Any)
    Address class: auto_var (Local variable)
d1.<> 

It shows you the type of "a" as "Type: int[20]", because that is how "a" is defined in C.

And if I haven't already beaten this one to death, consider this example, where we give a name to the array type:

int main(int argc, char **argv)
{
  typedef int array_of_20_ints[20];
  array_of_20_ints a;
  a[0] = argc;
}

This should make it crystal clear, or much more confusing for you. Above, in the C language, we gave a name to "int[20]", and that name is "array_of_20_ints". typedef allows to name composite types like arrays. So, now in the TotalView debugger you see this structure reflected:

d1.<> dwhat a
In thread 1.1:
Name: a; Type: array_of_20_ints; Size: 80 bytes; Addr: 0xfa02eca8
    Scope: ##/nfs/netapp0/user/home/jdelsign/a.out#xxx.c#main (Scope class: Any)
    Address class: auto_var (Local variable)
d1.<> dwhat array_of_20_ints
In thread 1.1:
Type name: array_of_20_ints; Size: 80 bytes; Category: Array
    Target type name: int; Target size: 4 bytes; Target category: Signed Integer
d1.<> 

TotalView (or any decent debugger) is able to figure all of this out because it reads the DWARF information, builds internal symbol table data structures that represent the program's structure, and reconstructs and displays the program information.

Hope this helps.

Cheers, John D.




ashim saikia wrote:
> No, actually It is showing the array name as a variable and all the
> array information in another DW_AT (attribute). So going through the
> information how can I conclude that array "a" information is the
> information specified in the DW_TAG_array_type DIE. Actually how can I
> correlate this info....
> 
> Suppose in a large program I have 10 nos of array... the array var is
> shown differently and its information differently
> 
> thats what my question is
> 
> ----- Original Message ----
> From: Daniel Berlin <dberlin at dberlin.org>
> To: ashim saikia <ar.saikia at yahoo.com>
> Cc: dwarf-discuss at lists.dwarfstd.org
> Sent: Wednesday, March 12, 2008 5:48:37 PM
> Subject: Re: [Dwarf-Discuss] Array Information
> 
> On Wed, Mar 12, 2008 at 6:53 AM, ashim saikia <ar.saikia at yahoo.com
> <mailto:ar.saikia at yahoo.com>> wrote:
>>
>> Hi,
>> I am unable to get the array info using dwarf2. Should I migrate to
> dwarf3.
>> I have written a simple c program containing array.
>> #include <stdio.h>
>>
>> main()
>> {
>>        int a[20];
>>        int b[30];
>>        int i=0;
>>        int *p=NULL;
>>
>>        for(i=0;i<20;i++) {
>>          a[i]=2;
>>          b[i]=2;
>>        }
>>
>>        *p=30;
>>        for(i=0;i<20;i++)
>>          printf("\n a[%d]=%d",i,a[i]);
>> }
>>
>> The following steps I have followed to get the dwarf info:
>>
>> $ gcc -g -c testarray.c
>> $ objdump -h testarray.o
>>
>> $readelf -wi testarray.o
>> The result is:-
>>
>> he section .debug_info contains:
>>
>>  Compilation Unit @ offset 0x0:
>>    Length:        383
>>    Version:      2
>>    Abbrev Offset: 0
>>    Pointer Size:  4
>>  <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
>>  < c>    DW_AT_stmt_list  : 0
>>  <10>    DW_AT_high_pc    : 0x8048458
>>  <14>    DW_AT_low_pc      : 0x80483c4
>>  <18>    DW_AT_producer    : GNU C 4.1.2 20070925 (Red Hat 4.1.2-33)
>>  <40>    DW_AT_language    : 1    (ANSI C)
>>  <41>    DW_AT_name        : testarray.c
>>  <4d>    DW_AT_comp_dir    : /usr1/ashim/cprogs
>>  <2><129>: Abbrev Number: 5 (DW_TAG_variable)
>>  <12a>    DW_AT_name        : a
>>  <12c>    DW_AT_decl_file  : 1
>>  <12d>    DW_AT_decl_line  : 5
>>  <12e>    DW_AT_type        : <15c>
>>  <132>    DW_AT_location    : 3 byte block: 91 9c 7f    (DW_OP_fbreg:
>> -100)
>>  <1><15c>: Abbrev Number: 6 (DW_TAG_array_type)
>>  <15d>    DW_AT_sibling    : <16c>
>>  <161>    DW_AT_type        : <bf>
>>
>> Here at DW_TAG_array type I am not getting the DW_AT_name (i.e. the
> name of
>> the array) without which I cannot establish the relation between the
> array,
>> its type and the number of elements.
> ????
> 
> 
> 1.  You are getting the name, because the variable has the name, not the
> type.
> 2. The type of the array is clearly marked.
> 3. The number of elements is clearly marked by the subrange type child
> of the array type, as allowed by the standard ("Subrange
> type entries may also be used to represent the bounds of array dimensions.")
> 
> I'm not sure what you think you are missing.
> You seem to be confused about the difference between the name of a
> type and a name of the variable.
> 
> 
> ------------------------------------------------------------------------
> Never miss a thing. Make Yahoo your homepage.
> <http://us.rd.yahoo.com/evt=51438/*http://www.yahoo.com/r/hs>
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Dwarf-Discuss mailing list
> Dwarf-Discuss at lists.dwarfstd.org
> http://lists.dwarfstd.org/listinfo.cgi/dwarf-discuss-dwarfstd.org




More information about the Dwarf-discuss mailing list