forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwind_with_dbghelp.cpp
More file actions
171 lines (160 loc) · 5.67 KB
/
unwind_with_dbghelp.cpp
File metadata and controls
171 lines (160 loc) · 5.67 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
#ifdef CPPTRACE_UNWIND_WITH_DBGHELP
#include <cpptrace/cpptrace.hpp>
#include "unwind.hpp"
#include "../utils/common.hpp"
#include "../utils/utils.hpp"
#include "../utils/dbghelp_syminit_manager.hpp"
#include <algorithm>
#include <cstdint>
#include <vector>
#include <mutex>
#include <windows.h>
#include <dbghelp.h>
// Fucking windows headers
#ifdef min
#undef min
#endif
namespace cpptrace {
namespace detail {
#if IS_MSVC
#pragma warning(push)
#pragma warning(disable: 4740) // warning C4740: flow in or out of inline asm code suppresses global optimization
#endif
CPPTRACE_FORCE_NO_INLINE
std::vector<frame_ptr> capture_frames(std::size_t skip, std::size_t max_depth) {
skip++;
// https://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/
// Get current thread context
// GetThreadContext cannot be used on the current thread.
// RtlCaptureContext doesn't work on i386
CONTEXT context;
#if defined(_M_IX86) || defined(__i386__)
ZeroMemory(&context, sizeof(CONTEXT));
context.ContextFlags = CONTEXT_CONTROL;
#if IS_MSVC
__asm {
label:
mov [context.Ebp], ebp;
mov [context.Esp], esp;
mov eax, [label];
mov [context.Eip], eax;
}
#else
asm(
"label:\n\t"
"mov{l %%ebp, %[cEbp] | %[cEbp], ebp};\n\t"
"mov{l %%esp, %[cEsp] | %[cEsp], esp};\n\t"
"mov{l $label, %%eax | eax, OFFSET label};\n\t"
"mov{l %%eax, %[cEip] | %[cEip], eax};\n\t"
: [cEbp] "=r" (context.Ebp),
[cEsp] "=r" (context.Esp),
[cEip] "=r" (context.Eip)
);
#endif
#else
RtlCaptureContext(&context);
#endif
// Setup current frame
STACKFRAME64 frame;
ZeroMemory(&frame, sizeof(STACKFRAME64));
DWORD machine_type;
#if defined(_M_IX86) || defined(__i386__)
machine_type = IMAGE_FILE_MACHINE_I386;
frame.AddrPC.Offset = context.Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context.Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Offset = context.Esp;
frame.AddrStack.Mode = AddrModeFlat;
#elif defined(_M_X64) || defined(__x86_64__)
machine_type = IMAGE_FILE_MACHINE_AMD64;
frame.AddrPC.Offset = context.Rip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context.Rsp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrStack.Offset = context.Rsp;
frame.AddrStack.Mode = AddrModeFlat;
#elif defined(_M_IA64) || defined(__aarch64__)
machine_type = IMAGE_FILE_MACHINE_IA64;
frame.AddrPC.Offset = context.StIIP;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context.IntSp;
frame.AddrFrame.Mode = AddrModeFlat;
frame.AddrBStore.Offset= context.RsBSP;
frame.AddrBStore.Mode = AddrModeFlat;
frame.AddrStack.Offset = context.IntSp;
frame.AddrStack.Mode = AddrModeFlat;
#else
#error "Cpptrace: StackWalk64 not supported for this platform yet"
#endif
std::vector<frame_ptr> trace;
// Dbghelp is is single-threaded, so acquire a lock.
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
// For some reason SymInitialize must be called before StackWalk64
// Note that the code assumes that
// SymInitialize( GetCurrentProcess(), NULL, TRUE ) has
// already been called.
//
HANDLE proc = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
if(get_cache_mode() == cache_mode::prioritize_speed) {
get_syminit_manager().init(proc);
} else {
if(!SymInitialize(proc, NULL, TRUE)) {
throw internal_error("SymInitialize failed");
}
}
while(trace.size() < max_depth) {
if(
!StackWalk64(
machine_type,
proc,
thread,
&frame,
machine_type == IMAGE_FILE_MACHINE_I386 ? NULL : &context,
NULL,
SymFunctionTableAccess64,
SymGetModuleBase64,
NULL
)
) {
// Either failed or finished walking
break;
}
if(frame.AddrPC.Offset != 0) {
// Valid frame
if(skip) {
skip--;
} else {
// On x86/x64/arm, as far as I can tell, the frame return address is always one after the call
// So we just decrement to get the pc back inside the `call` / `bl`
// This is done with _Unwind too but conditionally based on info from _Unwind_GetIPInfo.
trace.push_back(to_frame_ptr(frame.AddrPC.Offset) - 1);
}
} else {
// base
break;
}
}
if(get_cache_mode() != cache_mode::prioritize_speed) {
if(!SymCleanup(proc)) {
throw internal_error("SymCleanup failed");
}
}
return trace;
}
CPPTRACE_FORCE_NO_INLINE
std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) {
// Can't safe trace with dbghelp
return 0;
}
#if IS_MSVC
#pragma warning(pop)
#endif
bool has_safe_unwind() {
return false;
}
}
}
#endif