| 1 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDL |
| 2 |
|
| 3 |
#include <cpptrace/basic.hpp> |
| 4 |
#include "symbols/symbols.hpp" |
| 5 |
#include "binary/module_base.hpp" |
| 6 |
|
| 7 |
#include <cstdint> |
| 8 |
#include <memory> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include <dlfcn.h> |
| 12 |
|
| 13 |
CPPTRACE_BEGIN_NAMESPACE |
| 14 |
namespace detail { |
| 15 |
namespace libdl { |
| 16 |
stacktrace_frame resolve_frame(const frame_ptr addr) { |
| 17 |
Dl_info info; |
| 18 |
if(dladdr(reinterpret_cast<void*>(addr), &info)) { // thread-safe |
| 19 |
auto base = get_module_image_base(info.dli_fname); |
| 20 |
return { |
| 21 |
addr, |
| 22 |
base.has_value() |
| 23 |
? addr - reinterpret_cast<std::uintptr_t>(info.dli_fbase) + base.unwrap_value() |
| 24 |
: 0, |
| 25 |
nullable<std::uint32_t>::null(), |
| 26 |
nullable<std::uint32_t>::null(), |
| 27 |
info.dli_fname ? info.dli_fname : "", |
| 28 |
info.dli_sname ? info.dli_sname : "", |
| 29 |
false |
| 30 |
}; |
| 31 |
} else { |
| 32 |
return { |
| 33 |
addr, |
| 34 |
0, |
| 35 |
nullable<std::uint32_t>::null(), |
| 36 |
nullable<std::uint32_t>::null(), |
| 37 |
"", |
| 38 |
"", |
| 39 |
false |
| 40 |
}; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
std::vector<stacktrace_frame> resolve_frames(const std::vector<frame_ptr>& frames) { |
| 45 |
std::vector<stacktrace_frame> trace; |
| 46 |
trace.reserve(frames.size()); |
| 47 |
for(const auto frame : frames) { |
| 48 |
trace.push_back(resolve_frame(frame)); |
| 49 |
} |
| 50 |
return trace; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
CPPTRACE_END_NAMESPACE |
| 55 |
|
| 56 |
#endif |