| 1 |
#include <cpptrace/basic.hpp> |
| 2 |
#include <cpptrace/utils.hpp> |
| 3 |
|
| 4 |
#include "cpptrace/forward.hpp" |
| 5 |
#include "symbols/symbols.hpp" |
| 6 |
|
| 7 |
#include <vector> |
| 8 |
#include <unordered_map> |
| 9 |
|
| 10 |
#include "utils/error.hpp" |
| 11 |
#include "binary/object.hpp" |
| 12 |
|
| 13 |
CPPTRACE_BEGIN_NAMESPACE |
| 14 |
namespace detail { |
| 15 |
template<typename CollatedVec, typename Entry> |
| 16 |
std::unordered_map<std::string, CollatedVec> collate_frames( |
| 17 |
const std::vector<object_frame>& frames, |
| 18 |
std::vector<Entry>& trace |
| 19 |
) { |
| 20 |
std::unordered_map<std::string, CollatedVec> entries; |
| 21 |
for(std::size_t i = 0; i < frames.size(); i++) { |
| 22 |
const auto& entry = frames[i]; |
| 23 |
// The path may be empty. This can happens if libdl fails to find the shared object for a frame, e.g. I've |
| 24 |
// observed this on macos when looking up the shared object containing `start`. |
| 25 |
// It can also happen for JIT frames. As such, we don't exclude them from the output. |
| 26 |
entries[entry.object_path].emplace_back( |
| 27 |
entry, |
| 28 |
trace[i] |
| 29 |
); |
| 30 |
} |
| 31 |
return entries; |
| 32 |
} |
| 33 |
|
| 34 |
std::unordered_map<std::string, collated_vec> collate_frames( |
| 35 |
const std::vector<object_frame>& frames, |
| 36 |
std::vector<stacktrace_frame>& trace |
| 37 |
) { |
| 38 |
return collate_frames<collated_vec>(frames, trace); |
| 39 |
} |
| 40 |
std::unordered_map<std::string, collated_vec_with_inlines> collate_frames( |
| 41 |
const std::vector<object_frame>& frames, |
| 42 |
std::vector<frame_with_inlines>& trace |
| 43 |
) { |
| 44 |
return collate_frames<collated_vec_with_inlines>(frames, trace); |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* |
| 49 |
* |
| 50 |
* All the code here is awful and I'm not proud of it. |
| 51 |
* |
| 52 |
* |
| 53 |
* |
| 54 |
*/ |
| 55 |
|
| 56 |
// Resolver must not support walking inlines |
| 57 |
void fill_blanks( |
| 58 |
std::vector<stacktrace_frame>& vec, |
| 59 |
std::vector<stacktrace_frame> (*resolver)(const std::vector<frame_ptr>&) |
| 60 |
) { |
| 61 |
std::vector<frame_ptr> addresses; |
| 62 |
for(const auto& frame : vec) { |
| 63 |
if(frame.symbol.empty() || frame.filename.empty()) { |
| 64 |
addresses.push_back(frame.raw_address); |
| 65 |
} |
| 66 |
} |
| 67 |
std::vector<stacktrace_frame> new_frames = resolver(addresses); |
| 68 |
std::size_t i = 0; |
| 69 |
for(auto& frame : vec) { |
| 70 |
if(frame.symbol.empty() || frame.filename.empty()) { |
| 71 |
// three cases to handle, either partially overwrite or fully overwrite |
| 72 |
if(frame.symbol.empty() && frame.filename.empty()) { |
| 73 |
frame = new_frames[i]; |
| 74 |
} else if(frame.symbol.empty() && !frame.filename.empty()) { |
| 75 |
frame.symbol = new_frames[i].symbol; |
| 76 |
} else { |
| 77 |
ASSERT(!frame.symbol.empty() && frame.filename.empty()); |
| 78 |
frame.filename = new_frames[i].filename; |
| 79 |
frame.line = new_frames[i].line; |
| 80 |
frame.column = new_frames[i].column; |
| 81 |
} |
| 82 |
i++; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// TODO: Symbol resolution code should probably handle when object addresses are 0 |
| 88 |
|
| 89 |
std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) { |
| 90 |
#if defined(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF) && defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) |
| 91 |
std::vector<stacktrace_frame> trace = libdwarf::resolve_frames(frames); |
| 92 |
fill_blanks(trace, dbghelp::resolve_frames); |
| 93 |
return trace; |
| 94 |
#else |
| 95 |
#if defined(CPPTRACE_GET_SYMBOLS_WITH_LIBDL) \ |
| 96 |
|| defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) \ |
| 97 |
|| defined(CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE) |
| 98 |
// actually need to go backwards to a void* |
| 99 |
std::vector<frame_ptr> raw_frames(frames.size()); |
| 100 |
for(std::size_t i = 0; i < frames.size(); i++) { |
| 101 |
raw_frames[i] = frames[i].raw_address; |
| 102 |
} |
| 103 |
#endif |
| 104 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDL |
| 105 |
return libdl::resolve_frames(raw_frames); |
| 106 |
#endif |
| 107 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF |
| 108 |
return libdwarf::resolve_frames(frames); |
| 109 |
#endif |
| 110 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_DBGHELP |
| 111 |
return dbghelp::resolve_frames(raw_frames); |
| 112 |
#endif |
| 113 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE |
| 114 |
return addr2line::resolve_frames(frames); |
| 115 |
#endif |
| 116 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE |
| 117 |
return libbacktrace::resolve_frames(raw_frames); |
| 118 |
#endif |
| 119 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_NOTHING |
| 120 |
return nothing::resolve_frames(frames); |
| 121 |
#endif |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
std::vector<stacktrace_frame> resolve_frames(const std::vector<frame_ptr>& frames) { |
| 126 |
#if defined(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF) \ |
| 127 |
|| defined(CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE) |
| 128 |
auto dlframes = get_frames_object_info(frames); |
| 129 |
#endif |
| 130 |
#if defined(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF) && defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) |
| 131 |
std::vector<stacktrace_frame> trace = libdwarf::resolve_frames(dlframes); |
| 132 |
fill_blanks(trace, dbghelp::resolve_frames); |
| 133 |
return trace; |
| 134 |
#else |
| 135 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDL |
| 136 |
return libdl::resolve_frames(frames); |
| 137 |
#endif |
| 138 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF |
| 139 |
return libdwarf::resolve_frames(dlframes); |
| 140 |
#endif |
| 141 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_DBGHELP |
| 142 |
return dbghelp::resolve_frames(frames); |
| 143 |
#endif |
| 144 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE |
| 145 |
return addr2line::resolve_frames(dlframes); |
| 146 |
#endif |
| 147 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE |
| 148 |
return libbacktrace::resolve_frames(frames); |
| 149 |
#endif |
| 150 |
#ifdef CPPTRACE_GET_SYMBOLS_WITH_NOTHING |
| 151 |
return nothing::resolve_frames(frames); |
| 152 |
#endif |
| 153 |
#endif |
| 154 |
} |
| 155 |
} |
| 156 |
CPPTRACE_END_NAMESPACE |