|
From: <sv...@va...> - 2005-06-13 17:33:30
|
Author: sewardj
Date: 2005-06-13 18:33:27 +0100 (Mon, 13 Jun 2005)
New Revision: 3908
Log:
Partially merge in a (heavily modified) patch from Eric Estievenart
which adds support for reading directory names from DWARF2 debug info.
Also rework the representation of file and directory tables in the
DWARF2 reader. This removes a longstanding but only-just-discovered
curiousity that the previous code expanded the filename table one
entry at a time, so that reading file names from a DWARF2 object was
quadratic in the number of file names. It's now N log N.
Modified:
trunk/coregrind/m_debuginfo/dwarf.c
Modified: trunk/coregrind/m_debuginfo/dwarf.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/coregrind/m_debuginfo/dwarf.c 2005-06-13 16:50:29 UTC (rev 3907=
)
+++ trunk/coregrind/m_debuginfo/dwarf.c 2005-06-13 17:33:27 UTC (rev 3908=
)
@@ -36,6 +36,81 @@
#include "pub_core_options.h"
#include "priv_symtab.h"
=20
+
+/*------------------------------------------------------------*/
+/*--- Expanding arrays of words, for holding file name and ---*/
+/*--- directory name arrays. ---*/
+/*------------------------------------------------------------*/
+
+typedef
+ struct {
+ Word* tab;
+ UInt tab_size;
+ UInt tab_used;
+ }
+ WordArray;
+
+static void init_WordArray ( WordArray* wa )
+{
+ wa->tab =3D NULL;
+ wa->tab_size =3D 0;
+ wa->tab_used =3D 0;
+}
+
+static void free_WordArray ( WordArray* wa )
+{
+ if (wa->tab) {
+ vg_assert(wa->tab_size > 0);
+ VG_(arena_free)(VG_AR_SYMTAB, wa->tab);
+ }
+ init_WordArray(wa);
+}
+
+static void addto_WordArray ( WordArray* wa, Word w )
+{
+ UInt new_size, i;
+ Word* new_tab;
+
+ if (0) VG_(printf)("<<ADD %p (new sz =3D %d) >>\n",=20
+ (HChar*)w, wa->tab_used+1);
+
+ if (wa->tab_used < wa->tab_size) {
+ /* fine */
+ } else {
+ /* expand array */
+ if (0) VG_(printf)("EXPAND ARRAY from %d\n", wa->tab_size);
+ vg_assert(wa->tab_used =3D=3D wa->tab_size);
+ vg_assert( (wa->tab_size =3D=3D 0 && wa->tab =3D=3D NULL)
+ || (wa->tab_size !=3D 0 && wa->tab !=3D NULL) );
+ new_size =3D wa->tab_size =3D=3D 0 ? 8 : 2 * wa->tab_size;
+ new_tab =3D VG_(arena_malloc)(VG_AR_SYMTAB,=20
+ new_size * sizeof(Word));
+ vg_assert(new_tab !=3D NULL);
+ for (i =3D 0; i < wa->tab_used; i++)
+ new_tab[i] =3D wa->tab[i];
+ wa->tab_size =3D new_size;
+ if (wa->tab)
+ VG_(arena_free)(VG_AR_SYMTAB, wa->tab);
+ wa->tab =3D new_tab;
+ }
+
+ vg_assert(wa->tab_used < wa->tab_size);
+ vg_assert(wa->tab_size > 0);
+ wa->tab[wa->tab_used] =3D w;
+ wa->tab_used++;
+}
+
+static Word index_WordArray ( WordArray* wa, Int i )
+{
+ vg_assert(i >=3D 0 && i < wa->tab_used);
+ return wa->tab[i];
+}
+
+
+/*------------------------------------------------------------*/
+/*--- Read DWARF2 format line number info. ---*/
+/*------------------------------------------------------------*/
+
/* Structure found in the .debug_line section. */
typedef struct
{
@@ -105,9 +180,6 @@
Int is_stmt;
Int basic_block;
Int end_sequence;
- /* This variable hold the number of the last entry seen
- in the File Table. */
- UInt last_file_entry;
} SMR;
=20
=20
@@ -157,13 +229,27 @@
state_machine_regs.is_stmt =3D is_stmt;
state_machine_regs.basic_block =3D 0;
state_machine_regs.end_sequence =3D 0;
- state_machine_regs.last_file_entry =3D 0;
}
=20
+/* Look up a directory name, or return NULL if unknown. */
+static
+Char* lookupDir ( Int filename_index,
+ WordArray* fnidx2dir,
+ WordArray* dirnames )
+{
+ Word diridx =3D index_WordArray( fnidx2dir, filename_index );
+ Word dirname =3D index_WordArray( dirnames, (Int)diridx );
+ return (Char*)dirname;
+}
+
+
/* Handled an extend line op. Returns true if this is the end
of sequence. */
static=20
-int process_extended_line_op( SegInfo *si, Char*** fnames,=20
+int process_extended_line_op( SegInfo *si,=20
+ WordArray* filenames,=20
+ WordArray* dirnames,=20
+ WordArray* fnidx2dir,=20
UChar* data, Int is_stmt)
{
UChar op_code;
@@ -197,10 +283,15 @@
*/
if (state_machine_regs.is_stmt) {
if (state_machine_regs.last_address)
- VG_(addLineInfo) (si, (*fnames)[state_machine_regs.last_file],=20
- si->offset + state_machine_regs.last_address,=20
- si->offset + state_machine_regs.address,=20
- state_machine_regs.last_line, 0);
+ VG_(addLineInfo) (
+ si,=20
+ (Char*)index_WordArray(filenames, state_machine_regs.last=
_file),=20
+ lookupDir( state_machine_regs.last_file,
+ fnidx2dir, dirnames ),
+ si->offset + state_machine_regs.last_address,=20
+ si->offset + state_machine_regs.address,=20
+ state_machine_regs.last_line, 0
+ );
}
reset_state_machine (is_stmt);
break;
@@ -212,15 +303,8 @@
break;
=20
case DW_LNE_define_file:
- ++ state_machine_regs.last_file_entry;
name =3D data;
- if (*fnames =3D=3D NULL)
- *fnames =3D VG_(arena_malloc)(VG_AR_SYMTAB, sizeof (Char *) * 2)=
;
- else
- *fnames =3D VG_(arena_realloc)(
- VG_AR_SYMTAB, *fnames,
- sizeof(Char *) * (state_machine_regs.last_file_entr=
y + 1));
- (*fnames)[state_machine_regs.last_file_entry] =3D VG_(addStr) (si,=
name, -1);
+ addto_WordArray( filenames, (Word)VG_(addStr)(si,name,-1) );
data +=3D VG_(strlen) ((char *) data) + 1;
read_leb128 (data, & bytes_read, 0);
data +=3D bytes_read;
@@ -239,37 +323,78 @@
=20
void VG_(read_debuginfo_dwarf2) ( SegInfo* si, UChar* dwarf2, Int dwarf2=
_sz )
{
- DWARF2_External_LineInfo * external;
- DWARF2_Internal_LineInfo info;
- UChar * standard_opcodes;
- UChar * data =3D dwarf2;
- UChar * end =3D dwarf2 + dwarf2_sz;
- UChar * end_of_sequence;
- Char ** fnames =3D NULL;
+ DWARF2_External_LineInfo* external;
+ DWARF2_Internal_LineInfo info;
+ UChar* standard_opcodes;
+ UChar* data =3D dwarf2;
+ UChar* end =3D dwarf2 + dwarf2_sz;
+ UChar* end_of_sequence;
+ WordArray filenames;
+ WordArray dirnames;
+ WordArray fnidx2dir;
=20
+ /* filenames is an array of file names harvested from the DWARF2 info.=
=20
+ Entry [0] is NULL and is never referred to by the state machine.
+
+ Similarly, dirnames is an array of directory names. Entry [0] is
+ also NULL and denotes "we don't know what the path is", since
+ that is different from "the path is the empty string". Unlike
+ the file name table, the state machine does refer to entry [0],
+ which basically means "." ("the current directory of the
+ compilation", whatever that means, according to the DWARF3 spec.)
+
+ fnidx2dir is an array of indexes into the dirnames table.
+ (confused yet?) filenames[] and fnidx2dir[] are indexed together.
+ That is, for some index i in the filename table, then
+
+ the filename is filenames[i]
+ the diretory is dirnames[ fnidx2dir[i] ] */
+
/* Fails due to gcc padding ...
vg_assert(sizeof(DWARF2_External_LineInfo)
=3D=3D sizeof(DWARF2_Internal_LineInfo));
*/
=20
- while (data < end)
- {
+ init_WordArray(&filenames);
+ init_WordArray(&dirnames);
+ init_WordArray(&fnidx2dir);
+
+ while (data < end) {
+
+ /* Dump the file/dirname tables and start over. */
+ free_WordArray(&filenames);
+ free_WordArray(&dirnames);
+ free_WordArray(&fnidx2dir);
+
+ init_WordArray(&filenames);
+ init_WordArray(&dirnames);
+ init_WordArray(&fnidx2dir);
+
+ /* DWARF2 starts numbering filename entries at 1, so we need to
+ add a dummy zeroth entry to the table. The zeroth dirnames
+ entry denotes 'current directory of compilation' so we might
+ as well make the fnidx2dir zeroth entry denote that.=20
+ */
+ addto_WordArray( &filenames, (Word)NULL );
+ addto_WordArray( &dirnames, (Word)VG_(addStr)(si,".",-1) );
+ addto_WordArray( &fnidx2dir, (Word)0 ); /* "." */
+
external =3D (DWARF2_External_LineInfo *) data;
=20
/* Check the length of the block. */
info.li_length =3D * ((UInt *)(external->li_length));
=20
- if (info.li_length =3D=3D 0xffffffff)
+ if (info.li_length =3D=3D 0xffffffff)=20
{
VG_(symerr)("64-bit DWARF line info is not supported yet.");
- break;
+ goto out;
}
=20
if (info.li_length + sizeof (external->li_length) > dwarf2_sz)
{
VG_(symerr)("DWARF line info appears to be corrupt "
"- the section is too small");
- return;
+ goto out;
}
=20
/* Check its version number. */
@@ -278,7 +403,7 @@
{
VG_(symerr)("Only DWARF version 2 line info "
"is currently supported.");
- return;
+ goto out;
}
=20
info.li_prologue_length =3D * ((UInt *) (external->li_prologue_len=
gth));
@@ -313,7 +438,7 @@
info.li_opcode_base =3D * ((UChar *)(external->li_opcode_base)=
);=20
=20
if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\=
n",
- info.li_line_base, info.li_line_range, info.li_opcode_base);
+ info.li_line_base, info.li_line_range, info.li_opcode_=
base);
=20
/* Sign extend the line base field. */
info.li_line_base <<=3D 24;
@@ -330,64 +455,39 @@
/* Read the contents of the Directory table. */
data =3D standard_opcodes + info.li_opcode_base - 1;
=20
- if (* data =3D=3D 0)=20
- {
- }
- else
- {
- /* We ignore the directory table, since gcc gives the entire
- path as part of the filename */
- while (* data !=3D 0)
- {
- data +=3D VG_(strlen) ((char *) data) + 1;
- }
- }
-
- /* Skip the NUL at the end of the table. */
+ while (* data !=3D 0) {
+ addto_WordArray( &dirnames, (Word)VG_(addStr)(si,data,-1) );
+ data +=3D VG_(strlen) ((char *) data) + 1;
+ }
if (*data !=3D 0) {
VG_(symerr)("can't find NUL at end of DWARF2 directory table");
- return;
+ goto out;
}
data ++;
=20
- /* Read the contents of the File Name table. */
- if (* data =3D=3D 0)
- {
- }
- else
- {
- while (* data !=3D 0)
- {
- UChar * name;
- Int bytes_read;
+ /* Read the contents of the File Name table. This produces a
+ bunch of file names, and for each, an index to the
+ corresponding direcory name entry. */
+ while (* data !=3D 0) {
+ UChar* name;
+ Int bytes_read, diridx;
+ name =3D data;
+ data +=3D VG_(strlen) ((Char *) data) + 1;
=20
- ++ state_machine_regs.last_file_entry;
- name =3D data;
- /* Since we don't have realloc (0, ....) =3D=3D malloc (...=
)
- semantics, we need to malloc the first time. */
+ diridx =3D read_leb128 (data, & bytes_read, 0);
+ data +=3D bytes_read;
+ read_leb128 (data, & bytes_read, 0);
+ data +=3D bytes_read;
+ read_leb128 (data, & bytes_read, 0);
+ data +=3D bytes_read;
=20
- if (fnames =3D=3D NULL)
- fnames =3D VG_(arena_malloc)(VG_AR_SYMTAB, sizeof (Char *=
) * 2);
- else
- fnames =3D VG_(arena_realloc)(VG_AR_SYMTAB, fnames,
- sizeof(Char *)=20
- * (state_machine_regs.last_file_entry + 1)=
);
- data +=3D VG_(strlen) ((Char *) data) + 1;
- fnames[state_machine_regs.last_file_entry] =3D VG_(addStr) =
(si,name, -1);
-
- read_leb128 (data, & bytes_read, 0);
- data +=3D bytes_read;
- read_leb128 (data, & bytes_read, 0);
- data +=3D bytes_read;
- read_leb128 (data, & bytes_read, 0);
- data +=3D bytes_read;
- }
- }
-
- /* Skip the NUL at the end of the table. */
+ addto_WordArray( &filenames, (Word)VG_(addStr)(si,name,-1) );
+ addto_WordArray( &fnidx2dir, (Word)diridx );
+ if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
+ }
if (*data !=3D 0) {
VG_(symerr)("can't find NUL at end of DWARF2 file name table");
- return;
+ goto out;
}
data ++;
=20
@@ -401,7 +501,7 @@
=20
op_code =3D * data ++;
=20
- if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code);
+ if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code);
=20
if (op_code >=3D info.li_opcode_base)
{
@@ -420,10 +520,17 @@
if (state_machine_regs.is_stmt) {
/* only add a statement if there was a previous boundary */
if (state_machine_regs.last_address)=20
- VG_(addLineInfo) (si, fnames[state_machine_regs.last_file],=20
- si->offset + state_machine_regs.last_address,=20
- si->offset + state_machine_regs.address,=20
- state_machine_regs.last_line, 0);
+ VG_(addLineInfo)(
+ si,=20
+ (Char*)index_WordArray( &filenames,
+ state_machine_regs.last_=
file ),
+ lookupDir( state_machine_regs.last_file,
+ &fnidx2dir, &dirnames ),
+ si->offset + state_machine_regs.last_address,=20
+ si->offset + state_machine_regs.address,=20
+ state_machine_regs.last_line,=20
+ 0
+ );
state_machine_regs.last_address =3D state_machine_regs.address;
state_machine_regs.last_file =3D state_machine_regs.file;
state_machine_regs.last_line =3D state_machine_regs.line;
@@ -433,8 +540,8 @@
{
case DW_LNS_extended_op:
data +=3D process_extended_line_op (
- si, &fnames, data,=20
- info.li_default_is_stmt);
+ si, &filenames, &dirnames, &fnidx2dir,
+ data, info.li_default_is_stmt);
break;
=20
case DW_LNS_copy:
@@ -443,10 +550,17 @@
if (state_machine_regs.is_stmt) {
/* only add a statement if there was a previous boundary */
if (state_machine_regs.last_address)=20
- VG_(addLineInfo) (si, fnames[state_machine_regs.last_file],=20
- si->offset + state_machine_regs.last_address,=20
- si->offset + state_machine_regs.address,
- state_machine_regs.last_line, 0);
+ VG_(addLineInfo)(
+ si,=20
+ (Char*)index_WordArray( &filenames,
+ state_machine_regs.last_=
file ),
+ lookupDir( state_machine_regs.last_file,
+ &fnidx2dir, &dirnames ),
+ si->offset + state_machine_regs.last_address,=20
+ si->offset + state_machine_regs.address,
+ state_machine_regs.last_line,=20
+ 0
+ );
state_machine_regs.last_address =3D state_machine_regs.address;
state_machine_regs.last_file =3D state_machine_regs.file;
state_machine_regs.last_line =3D state_machine_regs.line;
@@ -527,10 +641,14 @@
}
break;
}
- }
- VG_(arena_free)(VG_AR_SYMTAB, fnames);
- fnames =3D NULL;
- }
+ } /* while (data < end_of_sequence) */
+
+ } /* while (data < end) */
+
+ out:
+ free_WordArray(&filenames);
+ free_WordArray(&dirnames);
+ free_WordArray(&fnidx2dir);
}
=20
=20
@@ -813,7 +931,7 @@
if (delta > 0 && prev_line > 0) {
if (0) VG_(printf) (" %d %d-%d\n",
prev_line, prev_delta, delta-1);
- VG_(addLineInfo) ( si, curr_filenm,=20
+ VG_(addLineInfo) ( si, curr_filenm, NULL,
base + prev_delta, base + delta,
prev_line, 0 );
}
|