forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.hpp
More file actions
198 lines (190 loc) · 7.77 KB
/
object.hpp
File metadata and controls
198 lines (190 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef OBJECT_HPP
#define OBJECT_HPP
#include "../utils/common.hpp"
#include "../utils/utils.hpp"
#include <string>
#include <vector>
#include <mutex>
#include <unordered_map>
#if IS_LINUX || IS_APPLE
#include <unistd.h>
#include <dlfcn.h>
#if IS_APPLE
#include "mach-o.hpp"
#else
#include "elf.hpp"
#endif
#ifdef CPPTRACE_HAS_DL_FIND_OBJECT
#include <link.h>
#endif
#elif IS_WINDOWS
#include <windows.h>
#include "pe.hpp"
#endif
namespace cpptrace {
namespace detail {
#if IS_LINUX || IS_APPLE
#if !IS_APPLE
inline std::uintptr_t get_module_image_base(const std::string& object_path) {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::unordered_map<std::string, std::uintptr_t> cache;
auto it = cache.find(object_path);
if(it == cache.end()) {
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not
// have two threads try to do the same computation
auto base = elf_get_module_image_base(object_path);
cache.insert(it, {object_path, base});
return base;
} else {
return it->second;
}
}
#else
inline std::uintptr_t get_module_image_base(const std::string& object_path) {
// We have to parse the Mach-O to find the offset of the text section.....
// I don't know how addresses are handled if there is more than one __TEXT load command. I'm assuming for
// now that there is only one, and I'm using only the first section entry within that load command.
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::unordered_map<std::string, std::uintptr_t> cache;
auto it = cache.find(object_path);
if(it == cache.end()) {
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not
// have two threads try to do the same computation
auto base = mach_o(object_path).get_text_vmaddr();
cache.insert(it, {object_path, base});
return base;
} else {
return it->second;
}
}
#endif
#ifdef CPPTRACE_HAS_DL_FIND_OBJECT
inline std::vector<object_frame> get_frames_object_info(const std::vector<frame_ptr>& addrs) {
// Use _dl_find_object when we can, it's orders of magnitude faster
std::vector<object_frame> frames;
frames.reserve(addrs.size());
for(const frame_ptr addr : addrs) {
object_frame frame;
frame.raw_address = addr;
frame.object_address = 0;
dl_find_object result;
if(_dl_find_object(reinterpret_cast<void*>(addr), &result) == 0) { // thread safe
if(result.dlfo_link_map->l_name != nullptr && result.dlfo_link_map->l_name[0] != 0) {
frame.object_path = result.dlfo_link_map->l_name;
} else {
// empty l_name, this means it's the currently running executable
// TODO: Caching and proper handling
char buffer[CPPTRACE_PATH_MAX + 1]{};
auto res = readlink("/proc/self/exe", buffer, CPPTRACE_PATH_MAX);
if(res == -1) {
// error handling?
} else {
frame.object_path = buffer;
}
}
frame.object_address = addr
- to_frame_ptr(result.dlfo_link_map->l_addr)
+ get_module_image_base(frame.object_path);
}
frames.push_back(frame);
}
return frames;
}
#else
// aladdr queries are needed to get pre-ASLR addresses and targets to run addr2line on
inline std::vector<object_frame> get_frames_object_info(const std::vector<frame_ptr>& addrs) {
// reference: https://github.com/bminor/glibc/blob/master/debug/backtracesyms.c
std::vector<object_frame> frames;
frames.reserve(addrs.size());
for(const frame_ptr addr : addrs) {
Dl_info info;
object_frame frame;
frame.raw_address = addr;
frame.object_address = 0;
if(dladdr(reinterpret_cast<void*>(addr), &info)) { // thread safe
frame.object_path = info.dli_fname;
frame.object_address = addr
- reinterpret_cast<std::uintptr_t>(info.dli_fbase)
+ get_module_image_base(info.dli_fname);
}
frames.push_back(frame);
}
return frames;
}
#endif
#else
inline std::string get_module_name(HMODULE handle) {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::unordered_map<HMODULE, std::string> cache;
auto it = cache.find(handle);
if(it == cache.end()) {
char path[MAX_PATH];
if(GetModuleFileNameA(handle, path, sizeof(path))) {
///std::fprintf(stderr, "path: %s base: %p\n", path, handle);
cache.insert(it, {handle, path});
return path;
} else {
std::fprintf(stderr, "%s\n", std::system_error(GetLastError(), std::system_category()).what());
cache.insert(it, {handle, ""});
return "";
}
} else {
return it->second;
}
}
inline std::uintptr_t get_module_image_base(const std::string& object_path) {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::unordered_map<std::string, std::uintptr_t> cache;
auto it = cache.find(object_path);
if(it == cache.end()) {
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not
// have two threads try to do the same computation
auto base = pe_get_module_image_base(object_path);
cache.insert(it, {object_path, base});
return base;
} else {
return it->second;
}
}
// aladdr queries are needed to get pre-ASLR addresses and targets to run addr2line on
inline std::vector<object_frame> get_frames_object_info(const std::vector<frame_ptr>& addrs) {
// reference: https://github.com/bminor/glibc/blob/master/debug/backtracesyms.c
std::vector<object_frame> frames;
frames.reserve(addrs.size());
for(const frame_ptr addr : addrs) {
object_frame frame;
frame.raw_address = addr;
frame.object_address = 0;
HMODULE handle;
// Multithread safe as long as another thread doesn't come along and free the module
if(GetModuleHandleExA(
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
reinterpret_cast<const char*>(addr),
&handle
)) {
frame.object_path = get_module_name(handle);
frame.object_address = addr
- reinterpret_cast<std::uintptr_t>(handle)
+ get_module_image_base(frame.object_path);
} else {
std::fprintf(stderr, "%s\n", std::system_error(GetLastError(), std::system_category()).what());
}
frames.push_back(frame);
}
return frames;
}
#endif
inline object_frame resolve_safe_object_frame(const safe_object_frame& frame) {
return {
frame.raw_address,
frame.address_relative_to_object_start + get_module_image_base(frame.object_path),
frame.object_path
};
}
}
}
#endif