| 1 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE |
| 2 |
|
| 3 |
#include <cpptrace/basic.hpp> |
| 4 |
#include "symbols/symbols.hpp" |
| 5 |
#include "platform/program_name.hpp" |
| 6 |
#include "utils/error.hpp" |
| 7 |
#include "utils/common.hpp" |
| 8 |
#include "options.hpp" |
| 9 |
|
| 10 |
#include <cstdint> |
| 11 |
#include <cstdio> |
| 12 |
#include <memory> |
| 13 |
#include <mutex> |
| 14 |
#include <stdexcept> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#ifdef CPPTRACE_BACKTRACE_PATH |
| 18 |
#include CPPTRACE_BACKTRACE_PATH |
| 19 |
#else |
| 20 |
#include <backtrace.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
CPPTRACE_BEGIN_NAMESPACE |
| 24 |
namespace detail { |
| 25 |
namespace libbacktrace { |
| 26 |
int full_callback(void* data, std::uintptr_t address, const char* file, int line, const char* symbol) { |
| 27 |
stacktrace_frame& frame = *static_cast<stacktrace_frame*>(data); |
| 28 |
frame.raw_address = address; |
| 29 |
frame.line = line; |
| 30 |
frame.filename = file ? file : ""; |
| 31 |
frame.symbol = symbol ? symbol : ""; |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
void syminfo_callback(void* data, std::uintptr_t address, const char* symbol, std::uintptr_t, std::uintptr_t) { |
| 36 |
stacktrace_frame& frame = *static_cast<stacktrace_frame*>(data); |
| 37 |
frame.raw_address = address; |
| 38 |
frame.line = 0; |
| 39 |
frame.filename = ""; |
| 40 |
frame.symbol = symbol ? symbol : ""; |
| 41 |
} |
| 42 |
|
| 43 |
void error_callback(void*, const char* msg, int errnum) { |
| 44 |
if(msg == std::string("no debug info in ELF executable")) { |
| 45 |
// https://github.com/jeremy-rifkin/cpptrace/issues/114 |
| 46 |
// https://github.com/ianlancetaylor/libbacktrace/blob/ae1e707dbacd4a5cc82fcf2d3816f410e9c5fec4/elf.c#L592 |
| 47 |
// not a critical error, just return |
| 48 |
return; |
| 49 |
} |
| 50 |
throw internal_error("Libbacktrace error: {}, code {}", msg, errnum); |
| 51 |
} |
| 52 |
|
| 53 |
backtrace_state* get_backtrace_state() { |
| 54 |
static std::mutex mutex; |
| 55 |
const std::lock_guard<std::mutex> lock(mutex); |
| 56 |
// backtrace_create_state must be called only one time per program |
| 57 |
static backtrace_state* state = nullptr; |
| 58 |
static bool called = false; |
| 59 |
if(!called) { |
| 60 |
state = backtrace_create_state(program_name(), true, error_callback, nullptr); |
| 61 |
called = true; |
| 62 |
} |
| 63 |
return state; |
| 64 |
} |
| 65 |
|
| 66 |
// TODO: Handle backtrace_pcinfo calling the callback multiple times on inlined functions |
| 67 |
stacktrace_frame resolve_frame(const frame_ptr addr) { |
| 68 |
try { |
| 69 |
stacktrace_frame frame = null_frame(); |
| 70 |
frame.raw_address = addr; |
| 71 |
backtrace_pcinfo( |
| 72 |
get_backtrace_state(), |
| 73 |
addr, |
| 74 |
full_callback, |
| 75 |
error_callback, |
| 76 |
&frame |
| 77 |
); |
| 78 |
if(frame.symbol.empty()) { |
| 79 |
// fallback, try to at least recover the symbol name with backtrace_syminfo |
| 80 |
backtrace_syminfo( |
| 81 |
get_backtrace_state(), |
| 82 |
addr, |
| 83 |
syminfo_callback, |
| 84 |
error_callback, |
| 85 |
&frame |
| 86 |
); |
| 87 |
} |
| 88 |
return frame; |
| 89 |
} catch(...) { // NOSONAR |
| 90 |
detail::log_and_maybe_propagate_exception(std::current_exception()); |
| 91 |
return null_frame(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
std::vector<stacktrace_frame> resolve_frames(const std::vector<frame_ptr>& frames) { |
| 96 |
std::vector<stacktrace_frame> trace; |
| 97 |
trace.reserve(frames.size()); |
| 98 |
for(const auto frame : frames) { |
| 99 |
trace.push_back(resolve_frame(frame)); |
| 100 |
} |
| 101 |
return trace; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
CPPTRACE_END_NAMESPACE |
| 106 |
|
| 107 |
#endif |