diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c699e7..d744624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/has_dladdr1.cpp b/cmake/has_dladdr1.cpp new file mode 100644 index 0000000..b92aad0 --- /dev/null +++ b/cmake/has_dladdr1.cpp @@ -0,0 +1,7 @@ +#include + +int main() { + Dl_info info; + link_map* link_map_info; + dladdr1(reinterpret_cast(&main), &info, reinterpret_cast(&link_map_info), RTLD_DL_LINKMAP); +} diff --git a/src/binary/object.cpp b/src/binary/object.cpp index ff307a5..db023e1 100644 --- a/src/binary/object.cpp +++ b/src/binary/object.cpp @@ -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;