Crashlog: Unix: Use dladdr1 for symbol offset for addr2line

Subtract 1 from offset given to addr2line
pull/491/head
Jonathan G Rennison 1 year ago
parent 86979dd167
commit 97acdfaa05

@ -10,11 +10,27 @@ check_cxx_source_compiles("
DL_FOUND DL_FOUND
) )
check_cxx_source_compiles("
#include <dlfcn.h>
#include <link.h>
int main() {
Dl_info info;
struct link_map *lm = nullptr;
return dladdr1(0, &info, (void **)&lm, RTLD_DL_LINKMAP);
}"
DL_FOUND2
)
if (DL_FOUND) if (DL_FOUND)
add_compile_options( add_compile_options(
-DWITH_DL -DWITH_DL
) )
link_libraries(dl) link_libraries(dl)
if (DL_FOUND2)
add_compile_options(
-DWITH_DL2
)
endif (DL_FOUND2)
endif (DL_FOUND) endif (DL_FOUND)
set(CMAKE_REQUIRED_LIBRARIES "") set(CMAKE_REQUIRED_LIBRARIES "")

@ -37,6 +37,9 @@
# include <execinfo.h> # include <execinfo.h>
#if defined(WITH_DL) #if defined(WITH_DL)
# include <dlfcn.h> # include <dlfcn.h>
#if defined(WITH_DL2)
# include <link.h>
#endif
#endif #endif
#if defined(WITH_DEMANGLE) #if defined(WITH_DEMANGLE)
# include <cxxabi.h> # include <cxxabi.h>
@ -535,16 +538,23 @@ class CrashLogUnix : public CrashLog {
for (int i = 0; i < trace_size; i++) { for (int i = 0; i < trace_size; i++) {
#if defined(WITH_DL) #if defined(WITH_DL)
Dl_info info; Dl_info info;
#if defined(WITH_DL2)
struct link_map *dl_lm = nullptr;
int dladdr_result = dladdr1(trace[i], &info, (void **)&dl_lm, RTLD_DL_LINKMAP);
#else
int dladdr_result = dladdr(trace[i], &info); int dladdr_result = dladdr(trace[i], &info);
#endif /* WITH_DL2 */
const char *func_name = info.dli_sname; const char *func_name = info.dli_sname;
void *func_addr = info.dli_saddr; void *func_addr = info.dli_saddr;
const char *file_name = nullptr; const char *file_name = nullptr;
unsigned int line_num = 0; unsigned int line_num = 0;
const int ptr_str_size = (2 + sizeof(void*) * 2); const int ptr_str_size = (2 + sizeof(void*) * 2);
if (dladdr_result && info.dli_fname) { #if defined(WITH_DL2)
if (dladdr_result && info.dli_fname && dl_lm != nullptr) {
char *saved_buffer = buffer; char *saved_buffer = buffer;
char addr_ptr_buffer[64]; char addr_ptr_buffer[64];
seprintf(addr_ptr_buffer, lastof(addr_ptr_buffer), PRINTF_SIZEX, (char *)trace[i] - (char *)info.dli_fbase); /* subtract one to get the line before the return address, i.e. the function call line */
seprintf(addr_ptr_buffer, lastof(addr_ptr_buffer), PRINTF_SIZEX, (char *)trace[i] - (char *)dl_lm->l_addr - 1);
const char *args[] = { const char *args[] = {
"addr2line", "addr2line",
"-e", "-e",
@ -566,6 +576,7 @@ class CrashLogUnix : public CrashLog {
buffer = saved_buffer; buffer = saved_buffer;
*buffer = 0; *buffer = 0;
} }
#endif /* WITH_DL2 */
#if defined(WITH_BFD) #if defined(WITH_BFD)
/* subtract one to get the line before the return address, i.e. the function call line */ /* subtract one to get the line before the return address, i.e. the function call line */
sym_info_bfd bfd_info(reinterpret_cast<bfd_vma>(trace[i]) - reinterpret_cast<bfd_vma>(info.dli_fbase) - 1); sym_info_bfd bfd_info(reinterpret_cast<bfd_vma>(trace[i]) - reinterpret_cast<bfd_vma>(info.dli_fbase) - 1);

Loading…
Cancel
Save