libdwarf
Using dwarf_init_path_dl()

Example focused on GNU debuglink data.

In case GNU debuglink data is followed the true_pathbuf content will not match path. The path actually used is copied to true_path_out.

In the case of MacOS dSYM the true_path_out may not match path.

If debuglink data is missing from the Elf executable or shared-object (ie, it is a normal object!) or unusable by libdwarf or true_path_buffer len is zero or true_path_out_buffer is zero libdwarf accepts the path given as the object to report on, no debuglink or dSYM processing will be used.

See also
https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html

An example calling dwarf_init_path_dl() and dwarf_finish()

Parameters
pathPath to an object we wish to open.
groupnumberDesired groupnumber. Use DW_DW_GROUPNUMBER_ANY unless you have reason to do otherwise.
errorA pointer we can use to record error details.
Returns
Returns the applicable result. DW_DLV_OK etc.
*/
int exampleinit_dl(const char *path, unsigned groupnumber,
Dwarf_Error *error)
{
static char true_pathbuf[FILENAME_MAX];
static const char *glpath[3] = {
"/usr/local/debug",
"/usr/local/private/debug",
"/usr/local/libdwarf/debug"
};
unsigned tpathlen = FILENAME_MAX;
Dwarf_Handler errhand = 0;
Dwarf_Ptr errarg = 0;
Dwarf_Debug dbg = 0;
int res = 0;
unsigned char path_source = 0;
res = dwarf_init_path_dl(path,true_pathbuf,
tpathlen,groupnumber,errhand,
errarg,&dbg,
(char **)glpath,
3,
&path_source,
error);
if (res == DW_DLV_ERROR) {
/* We are not returning dbg, so we must do:
dwarf_dealloc_error(dbg,*error);
here to free the error details. */
dwarf_dealloc_error(dbg,*error);
*error = 0;
return res;
}
if (res == DW_DLV_NO_ENTRY) {
return res;
}
printf("The file we actually opened is %s\n",
true_pathbuf);
/* Call libdwarf functions here */
return res;
}
struct Dwarf_Debug_s * Dwarf_Debug
Definition: libdwarf.h:603
struct Dwarf_Error_s * Dwarf_Error
Definition: libdwarf.h:597
void(* Dwarf_Handler)(Dwarf_Error dw_error, Dwarf_Ptr dw_errarg)
Definition: libdwarf.h:718
void * Dwarf_Ptr
Definition: libdwarf.h:208
void dwarf_dealloc_error(Dwarf_Debug dw_dbg, Dwarf_Error dw_error)
Free (dealloc) an Dwarf_Error something created.
int dwarf_init_path_dl(const char *dw_path, char *dw_true_path_out_buffer, unsigned int dw_true_path_bufferlen, unsigned int dw_groupnumber, Dwarf_Handler dw_errhand, Dwarf_Ptr dw_errarg, Dwarf_Debug *dw_dbg, char **dw_dl_path_array, unsigned int dw_dl_path_array_size, unsigned char *dw_dl_path_source, Dwarf_Error *dw_error)
Initialization following GNU debuglink section data.
int dwarf_finish(Dwarf_Debug dw_dbg)
Close the initialized dw_dbg and free all data libdwarf has for this dw_dbg.