libdwarf
Using dwarf_srcfiles()

Example getting source file names given a DIE.

*/
int examplee(Dwarf_Debug dbg,Dwarf_Die somedie,Dwarf_Error *error)
{
/* It is an annoying historical mistake in libdwarf
that the count is a signed value. */
Dwarf_Signed count = 0;
char **srcfiles = 0;
Dwarf_Signed i = 0;
int res = 0;
Dwarf_Line_Context line_context = 0;
Dwarf_Small table_count = 0;
Dwarf_Unsigned lineversion = 0;
res = dwarf_srclines_b(somedie,&lineversion,
&table_count,&line_context,error);
if (res != DW_DLV_OK) {
/* dwarf_finish() will dealloc srcfiles, not doing
that here. */
return res;
}
res = dwarf_srcfiles(somedie, &srcfiles,&count,error);
if (res != DW_DLV_OK) {
dwarf_srclines_dealloc_b(line_context);
return res;
}
for (i = 0; i < count; ++i) {
Dwarf_Signed propernumber = 0;
/* Use srcfiles[i] If you wish to print 'i'
mostusefully
you should reflect the numbering that
a DW_AT_decl_file attribute would report in
this CU. */
if (lineversion == 5) {
propernumber = i;
} else {
propernumber = i+1;
}
printf("File %4ld %s\n",(unsigned long)propernumber,
srcfiles[i]);
dwarf_dealloc(dbg, srcfiles[i], DW_DLA_STRING);
srcfiles[i] = 0;
}
/* We could leave all dealloc to dwarf_finish() to
handle, but this tidies up sooner. */
dwarf_dealloc(dbg, srcfiles, DW_DLA_LIST);
dwarf_srclines_dealloc_b(line_context);
return DW_DLV_OK;
}
struct Dwarf_Debug_s * Dwarf_Debug
Definition: libdwarf.h:586
struct Dwarf_Die_s * Dwarf_Die
Definition: libdwarf.h:591
struct Dwarf_Error_s * Dwarf_Error
Definition: libdwarf.h:580
struct Dwarf_Line_Context_s * Dwarf_Line_Context
Definition: libdwarf.h:680
unsigned char Dwarf_Small
Definition: libdwarf.h:204
signed long long Dwarf_Signed
Definition: libdwarf.h:197
unsigned long long Dwarf_Unsigned
Definition: libdwarf.h:196
void dwarf_dealloc(Dwarf_Debug dw_dbg, void *dw_space, Dwarf_Unsigned dw_type)
The generic dealloc (free) function. It requires you know the correct DW_DLA value to pass in,...
int dwarf_srcfiles(Dwarf_Die dw_cu_die, char ***dw_srcfiles, Dwarf_Signed *dw_filecount, Dwarf_Error *dw_error)
The list of source files from the line table header.
void dwarf_srclines_dealloc_b(Dwarf_Line_Context dw_context)
Dealloc the memory allocated by dwarf_srclines_b.
int dwarf_srclines_b(Dwarf_Die dw_cudie, Dwarf_Unsigned *dw_version_out, Dwarf_Small *dw_table_count, Dwarf_Line_Context *dw_linecontext, Dwarf_Error *dw_error)
Initialize Dwarf_Line_Context for line table access.