| 1 |
#ifdef CPPTRACE_UNWIND_WITH_DBGHELP |
| 2 |
|
| 3 |
#include <cpptrace/basic.hpp> |
| 4 |
#include "unwind/unwind.hpp" |
| 5 |
#include "utils/common.hpp" |
| 6 |
#include "utils/utils.hpp" |
| 7 |
#include "platform/dbghelp_utils.hpp" |
| 8 |
|
| 9 |
#include <vector> |
| 10 |
#include <cstddef> |
| 11 |
|
| 12 |
#include <windows.h> |
| 13 |
#include <dbghelp.h> |
| 14 |
|
| 15 |
// Fucking windows headers |
| 16 |
#ifdef min |
| 17 |
#undef min |
| 18 |
#endif |
| 19 |
|
| 20 |
CPPTRACE_BEGIN_NAMESPACE |
| 21 |
namespace detail { |
| 22 |
#if IS_MSVC |
| 23 |
#pragma warning(push) |
| 24 |
#pragma warning(disable: 4740) // warning C4740: flow in or out of inline asm code suppresses global optimization |
| 25 |
#endif |
| 26 |
CPPTRACE_FORCE_NO_INLINE |
| 27 |
std::vector<frame_ptr> capture_frames( |
| 28 |
std::size_t skip, |
| 29 |
std::size_t max_depth, |
| 30 |
EXCEPTION_POINTERS* exception_pointers |
| 31 |
) { |
| 32 |
// https://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/ |
| 33 |
|
| 34 |
// Get current thread context |
| 35 |
// GetThreadContext cannot be used on the current thread. |
| 36 |
// RtlCaptureContext doesn't work on i386 |
| 37 |
CONTEXT context; |
| 38 |
ZeroMemory(&context, sizeof(CONTEXT)); |
| 39 |
if(exception_pointers) { |
| 40 |
context = *exception_pointers->ContextRecord; |
| 41 |
} else { |
| 42 |
skip++; // we're unwinding from the capture_frames frame, skip it |
| 43 |
#if defined(_M_IX86) || defined(__i386__) |
| 44 |
context.ContextFlags = CONTEXT_CONTROL; |
| 45 |
#if IS_MSVC |
| 46 |
__asm { |
| 47 |
label: |
| 48 |
mov [context.Ebp], ebp; |
| 49 |
mov [context.Esp], esp; |
| 50 |
mov eax, [label]; |
| 51 |
mov [context.Eip], eax; |
| 52 |
} |
| 53 |
#else |
| 54 |
asm( |
| 55 |
"label:\n\t" |
| 56 |
"mov{l %%ebp, %[cEbp] | %[cEbp], ebp};\n\t" |
| 57 |
"mov{l %%esp, %[cEsp] | %[cEsp], esp};\n\t" |
| 58 |
"mov{l $label, %%eax | eax, OFFSET label};\n\t" |
| 59 |
"mov{l %%eax, %[cEip] | %[cEip], eax};\n\t" |
| 60 |
: [cEbp] "=r" (context.Ebp), |
| 61 |
[cEsp] "=r" (context.Esp), |
| 62 |
[cEip] "=r" (context.Eip) |
| 63 |
); |
| 64 |
#endif |
| 65 |
#else |
| 66 |
RtlCaptureContext(&context); |
| 67 |
#endif |
| 68 |
} |
| 69 |
// Setup current frame |
| 70 |
STACKFRAME64 frame; |
| 71 |
ZeroMemory(&frame, sizeof(STACKFRAME64)); |
| 72 |
DWORD machine_type; |
| 73 |
#if defined(_M_IX86) || defined(__i386__) |
| 74 |
machine_type = IMAGE_FILE_MACHINE_I386; |
| 75 |
frame.AddrPC.Offset = context.Eip; |
| 76 |
frame.AddrPC.Mode = AddrModeFlat; |
| 77 |
frame.AddrFrame.Offset = context.Ebp; |
| 78 |
frame.AddrFrame.Mode = AddrModeFlat; |
| 79 |
frame.AddrStack.Offset = context.Esp; |
| 80 |
frame.AddrStack.Mode = AddrModeFlat; |
| 81 |
#elif defined(_M_X64) || defined(__x86_64__) |
| 82 |
machine_type = IMAGE_FILE_MACHINE_AMD64; |
| 83 |
frame.AddrPC.Offset = context.Rip; |
| 84 |
frame.AddrPC.Mode = AddrModeFlat; |
| 85 |
frame.AddrFrame.Offset = context.Rsp; |
| 86 |
frame.AddrFrame.Mode = AddrModeFlat; |
| 87 |
frame.AddrStack.Offset = context.Rsp; |
| 88 |
frame.AddrStack.Mode = AddrModeFlat; |
| 89 |
#elif defined(_M_IA64) |
| 90 |
machine_type = IMAGE_FILE_MACHINE_IA64; |
| 91 |
frame.AddrPC.Offset = context.StIIP; |
| 92 |
frame.AddrPC.Mode = AddrModeFlat; |
| 93 |
frame.AddrFrame.Offset = context.IntSp; |
| 94 |
frame.AddrFrame.Mode = AddrModeFlat; |
| 95 |
frame.AddrBStore.Offset= context.RsBSP; |
| 96 |
frame.AddrBStore.Mode = AddrModeFlat; |
| 97 |
frame.AddrStack.Offset = context.IntSp; |
| 98 |
frame.AddrStack.Mode = AddrModeFlat; |
| 99 |
#elif defined(_M_ARM) || defined(__arm__) |
| 100 |
machine_type = IMAGE_FILE_MACHINE_ARM; |
| 101 |
frame.AddrPC.Offset = context.Pc; |
| 102 |
frame.AddrPC.Mode = AddrModeFlat; |
| 103 |
frame.AddrFrame.Offset = context.R11; |
| 104 |
frame.AddrFrame.Mode = AddrModeFlat; |
| 105 |
frame.AddrStack.Offset = context.Sp; |
| 106 |
frame.AddrStack.Mode = AddrModeFlat; |
| 107 |
#elif defined(_M_ARM64) || defined(__aarch64__) |
| 108 |
machine_type = IMAGE_FILE_MACHINE_ARM64; |
| 109 |
frame.AddrPC.Offset = context.Pc; |
| 110 |
frame.AddrPC.Mode = AddrModeFlat; |
| 111 |
frame.AddrFrame.Offset = context.Fp; |
| 112 |
frame.AddrFrame.Mode = AddrModeFlat; |
| 113 |
frame.AddrStack.Offset = context.Sp; |
| 114 |
frame.AddrStack.Mode = AddrModeFlat; |
| 115 |
#else |
| 116 |
#error "Cpptrace: StackWalk64 not supported for this platform yet" |
| 117 |
#endif |
| 118 |
|
| 119 |
std::vector<frame_ptr> trace; |
| 120 |
|
| 121 |
// Dbghelp is is single-threaded, so acquire a lock. |
| 122 |
auto lock = get_dbghelp_lock(); |
| 123 |
// For some reason SymInitialize must be called before StackWalk64 |
| 124 |
// Note that the code assumes that |
| 125 |
// SymInitialize( GetCurrentProcess(), NULL, TRUE ) has |
| 126 |
// already been called. |
| 127 |
// |
| 128 |
auto syminit_info = ensure_syminit(); |
| 129 |
HANDLE thread = GetCurrentThread(); |
| 130 |
while(trace.size() < max_depth) { |
| 131 |
if( |
| 132 |
!StackWalk64( |
| 133 |
machine_type, |
| 134 |
syminit_info.get_process_handle(), |
| 135 |
thread, |
| 136 |
&frame, |
| 137 |
machine_type == IMAGE_FILE_MACHINE_I386 ? NULL : &context, |
| 138 |
NULL, |
| 139 |
SymFunctionTableAccess64, |
| 140 |
SymGetModuleBase64, |
| 141 |
NULL |
| 142 |
) |
| 143 |
) { |
| 144 |
// Either failed or finished walking |
| 145 |
break; |
| 146 |
} |
| 147 |
if(frame.AddrPC.Offset != 0) { |
| 148 |
// Valid frame |
| 149 |
if(skip) { |
| 150 |
skip--; |
| 151 |
} else { |
| 152 |
// On x86/x64/arm, as far as I can tell, the frame return address is always one after the call |
| 153 |
// So we just decrement to get the pc back inside the `call` / `bl` |
| 154 |
// This is done with _Unwind too but conditionally based on info from _Unwind_GetIPInfo. |
| 155 |
trace.push_back(to_frame_ptr(frame.AddrPC.Offset) - 1); |
| 156 |
} |
| 157 |
} else { |
| 158 |
// base |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
return trace; |
| 163 |
} |
| 164 |
|
| 165 |
CPPTRACE_FORCE_NO_INLINE |
| 166 |
std::size_t safe_capture_frames(frame_ptr*, std::size_t, std::size_t, std::size_t) { |
| 167 |
// Can't safe trace with dbghelp |
| 168 |
return 0; |
| 169 |
} |
| 170 |
#if IS_MSVC |
| 171 |
#pragma warning(pop) |
| 172 |
#endif |
| 173 |
|
| 174 |
bool has_safe_unwind() { |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
CPPTRACE_END_NAMESPACE |
| 179 |
|
| 180 |
#endif |