| 1 |
#include "binary/object.hpp" |
| 2 |
|
| 3 |
#include "platform/platform.hpp" |
| 4 |
#include "utils/utils.hpp" |
| 5 |
#include "binary/module_base.hpp" |
| 6 |
#include "logging.hpp" |
| 7 |
|
| 8 |
#include <string> |
| 9 |
#include <system_error> |
| 10 |
#include <vector> |
| 11 |
#include <mutex> |
| 12 |
#include <unordered_map> |
| 13 |
|
| 14 |
#if IS_LINUX || IS_APPLE |
| 15 |
#include <unistd.h> |
| 16 |
#include <dlfcn.h> |
| 17 |
#if IS_LINUX |
| 18 |
#include <link.h> // needed for dladdr1's link_map info |
| 19 |
#endif |
| 20 |
#elif IS_WINDOWS |
| 21 |
#ifndef WIN32_LEAN_AND_MEAN |
| 22 |
#define WIN32_LEAN_AND_MEAN |
| 23 |
#endif |
| 24 |
#include <windows.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
CPPTRACE_BEGIN_NAMESPACE |
| 28 |
namespace detail { |
| 29 |
#if IS_LINUX || IS_APPLE |
| 30 |
#if defined(CPPTRACE_HAS_DL_FIND_OBJECT) || defined(CPPTRACE_HAS_DLADDR1) |
| 31 |
std::string resolve_l_name(const char* l_name) { |
| 32 |
if(l_name != nullptr && l_name[0] != 0) { |
| 33 |
return l_name; |
| 34 |
} else { |
| 35 |
// empty l_name, this means it's the currently running executable |
| 36 |
// TODO: Caching and proper handling |
| 37 |
char buffer[CPPTRACE_PATH_MAX + 1]{}; |
| 38 |
auto res = readlink("/proc/self/exe", buffer, CPPTRACE_PATH_MAX); |
| 39 |
if(res == -1) { |
| 40 |
return ""; // TODO |
| 41 |
} else { |
| 42 |
return buffer; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
#endif |
| 47 |
// dladdr queries are needed to get pre-ASLR addresses and targets to run symbol resolution on |
| 48 |
// _dl_find_object is preferred if at all possible as it is much faster (added in glibc 2.35) |
| 49 |
// dladdr1 is preferred if possible because it allows for a more accurate object path to be resolved (glibc 2.3.3) |
| 50 |
#ifdef CPPTRACE_HAS_DL_FIND_OBJECT // we don't even check for this on apple |
| 51 |
object_frame get_frame_object_info(frame_ptr address) { |
| 52 |
// Use _dl_find_object when we can, it's orders of magnitude faster |
| 53 |
object_frame frame; |
| 54 |
frame.raw_address = address; |
| 55 |
frame.object_address = 0; |
| 56 |
dl_find_object result; |
| 57 |
if(_dl_find_object(reinterpret_cast<void*>(address), &result) == 0) { // thread safe |
| 58 |
frame.object_path = resolve_l_name(result.dlfo_link_map->l_name); |
| 59 |
frame.object_address = address - to_frame_ptr(result.dlfo_link_map->l_addr); |
| 60 |
} |
| 61 |
return frame; |
| 62 |
} |
| 63 |
#elif defined(CPPTRACE_HAS_DLADDR1) |
| 64 |
object_frame get_frame_object_info(frame_ptr address) { |
| 65 |
// https://github.com/bminor/glibc/blob/91695ee4598b39d181ab8df579b888a8863c4cab/elf/dl-addr.c#L26 |
| 66 |
Dl_info info; |
| 67 |
link_map* link_map_info; |
| 68 |
object_frame frame; |
| 69 |
frame.raw_address = address; |
| 70 |
frame.object_address = 0; |
| 71 |
if( |
| 72 |
// thread safe |
| 73 |
dladdr1(reinterpret_cast<void*>(address), &info, reinterpret_cast<void**>(&link_map_info), RTLD_DL_LINKMAP) |
| 74 |
) { |
| 75 |
frame.object_path = resolve_l_name(link_map_info->l_name); |
| 76 |
auto base = get_module_image_base(frame.object_path); |
| 77 |
if(base.has_value()) { |
| 78 |
frame.object_address = address |
| 79 |
- reinterpret_cast<std::uintptr_t>(info.dli_fbase) |
| 80 |
+ base.unwrap_value(); |
| 81 |
} else { |
| 82 |
if(!should_absorb_trace_exceptions()) { |
| 83 |
base.drop_error(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
return frame; |
| 88 |
} |
| 89 |
#else |
| 90 |
// glibc dladdr may not return an accurate dli_fname as it uses argv[0] for addresses in the main executable |
| 91 |
// https://github.com/bminor/glibc/blob/caed1f5c0b2e31b5f4e0f21fea4b2c9ecd3b5b30/elf/dl-addr.c#L33-L36 |
| 92 |
// macos doesn't have dladdr1 but its dli_fname behaves more sensibly, same with some other libc's like musl |
| 93 |
object_frame get_frame_object_info(frame_ptr address) { |
| 94 |
// reference: https://github.com/bminor/glibc/blob/master/debug/backtracesyms.c |
| 95 |
Dl_info info; |
| 96 |
object_frame frame; |
| 97 |
frame.raw_address = address; |
| 98 |
frame.object_address = 0; |
| 99 |
if(dladdr(reinterpret_cast<void*>(address), &info)) { // thread safe |
| 100 |
frame.object_path = info.dli_fname; |
| 101 |
auto base = get_module_image_base(info.dli_fname); |
| 102 |
if(base.has_value()) { |
| 103 |
frame.object_address = address |
| 104 |
- reinterpret_cast<std::uintptr_t>(info.dli_fbase) |
| 105 |
+ base.unwrap_value(); |
| 106 |
} else { |
| 107 |
if(!should_absorb_trace_exceptions()) { |
| 108 |
base.drop_error(); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return frame; |
| 113 |
} |
| 114 |
#endif |
| 115 |
#else |
| 116 |
std::string get_module_name(HMODULE handle) { |
| 117 |
static std::mutex mutex; |
| 118 |
std::lock_guard<std::mutex> lock(mutex); |
| 119 |
static std::unordered_map<HMODULE, std::string> cache; |
| 120 |
auto it = cache.find(handle); |
| 121 |
if(it == cache.end()) { |
| 122 |
char path[MAX_PATH]; |
| 123 |
if(GetModuleFileNameA(handle, path, sizeof(path))) { |
| 124 |
cache.insert(it, {handle, path}); |
| 125 |
return path; |
| 126 |
} else { |
| 127 |
log::error(std::system_error(GetLastError(), std::system_category()).what()); |
| 128 |
cache.insert(it, {handle, ""}); |
| 129 |
return ""; |
| 130 |
} |
| 131 |
} else { |
| 132 |
return it->second; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
object_frame get_frame_object_info(frame_ptr address) { |
| 137 |
object_frame frame; |
| 138 |
frame.raw_address = address; |
| 139 |
frame.object_address = 0; |
| 140 |
HMODULE handle; |
| 141 |
// Multithread safe as long as another thread doesn't come along and free the module |
| 142 |
if(GetModuleHandleExA( |
| 143 |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
| 144 |
reinterpret_cast<const char*>(address), |
| 145 |
&handle |
| 146 |
)) { |
| 147 |
frame.object_path = get_module_name(handle); |
| 148 |
auto base = get_module_image_base(frame.object_path); |
| 149 |
if(base.has_value()) { |
| 150 |
frame.object_address = address |
| 151 |
- reinterpret_cast<std::uintptr_t>(handle) |
| 152 |
+ base.unwrap_value(); |
| 153 |
} else { |
| 154 |
if(!should_absorb_trace_exceptions()) { |
| 155 |
base.drop_error(); |
| 156 |
} |
| 157 |
} |
| 158 |
} else { |
| 159 |
log::error(std::system_error(GetLastError(), std::system_category()).what()); |
| 160 |
} |
| 161 |
return frame; |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
std::vector<object_frame> get_frames_object_info(const std::vector<frame_ptr>& addresses) { |
| 166 |
std::vector<object_frame> frames; |
| 167 |
frames.reserve(addresses.size()); |
| 168 |
for(const frame_ptr address : addresses) { |
| 169 |
frames.push_back(get_frame_object_info(address)); |
| 170 |
} |
| 171 |
return frames; |
| 172 |
} |
| 173 |
|
| 174 |
object_frame resolve_safe_object_frame(const safe_object_frame& frame) { |
| 175 |
std::string object_path = frame.object_path; |
| 176 |
if(object_path.empty()) { |
| 177 |
return { |
| 178 |
frame.raw_address, |
| 179 |
0, |
| 180 |
"" |
| 181 |
}; |
| 182 |
} |
| 183 |
return { |
| 184 |
frame.raw_address, |
| 185 |
frame.address_relative_to_object_start, |
| 186 |
std::move(object_path) |
| 187 |
}; |
| 188 |
} |
| 189 |
} |
| 190 |
CPPTRACE_END_NAMESPACE |