Attempt to fallback to dladdr if dladdr1 is not available

This commit is contained in:
Jeremy 2024-05-24 23:03:10 -05:00
parent f8d28a6469
commit 7916f10278
No known key found for this signature in database
GPG Key ID: BE03111EB7ED6E2E
3 changed files with 21 additions and 4 deletions

View File

@ -94,6 +94,9 @@ endif()
if(UNIX AND NOT APPLE)
check_support(HAS_DL_FIND_OBJECT has_dl_find_object.cpp "" "" "")
if(NOT HAS_DL_FIND_OBJECT)
check_support(HAS_DLADDR1 has_dladdr1.cpp "" "" "")
endif()
endif()
# =============================================== Autoconfig unwinding ===============================================
@ -316,6 +319,10 @@ if(HAS_DL_FIND_OBJECT)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_HAS_DL_FIND_OBJECT)
endif()
if(HAS_DLADDR1)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_HAS_DLADDR1)
endif()
# Symbols
if(CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE)
if(NOT HAS_BACKTRACE)

7
cmake/has_dladdr1.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <dlfcn.h>
int main() {
Dl_info info;
link_map* link_map_info;
dladdr1(reinterpret_cast<void*>(&main), &info, reinterpret_cast<void**>(&link_map_info), RTLD_DL_LINKMAP);
}

View File

@ -21,7 +21,8 @@
namespace cpptrace {
namespace detail {
#if IS_LINUX
#if IS_LINUX || IS_APPLE
#if defined(CPPTRACE_HAS_DL_FIND_OBJECT) || defined(HAS_DLADDR1)
std::string resolve_l_name(const char* l_name) {
if(l_name != nullptr && l_name[0] != 0) {
return l_name;
@ -37,6 +38,7 @@ namespace detail {
}
}
}
#endif
#ifdef CPPTRACE_HAS_DL_FIND_OBJECT // we don't even check for this on apple
object_frame get_frame_object_info(frame_ptr address) {
// Use _dl_find_object when we can, it's orders of magnitude faster
@ -50,7 +52,7 @@ namespace detail {
}
return frame;
}
#else
#elif defined(HAS_DLADDR1)
// dladdr queries are needed to get pre-ASLR addresses and targets to run addr2line on
object_frame get_frame_object_info(frame_ptr address) {
// reference: https://github.com/bminor/glibc/blob/master/debug/backtracesyms.c
@ -76,8 +78,8 @@ namespace detail {
}
return frame;
}
#endif
#elif IS_APPLE
#else
// TODO: Try to verify this is correct in the context of bad argv[0]
// macos doesn't have dladdr1 but it seems its dli_fname behaves more sensibly?
// dladdr queries are needed to get pre-ASLR addresses and targets to run addr2line on
object_frame get_frame_object_info(frame_ptr address) {
@ -99,6 +101,7 @@ namespace detail {
}
return frame;
}
#endif
#else
std::string get_module_name(HMODULE handle) {
static std::mutex mutex;