forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackTrace.cpp
301 lines (272 loc) · 7.89 KB
/
StackTrace.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "stdafx.h"
#include "StackTrace.h"
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <dbghelp.h>
#include <cstdlib>
#include <strsafe.h>
#pragma comment(lib, "dbghelp.lib")
namespace
{
void GetFileFromPath(const char* path, char* file, int fileNameSize)
{
char ext[_MAX_EXT] = { 0 };
_splitpath_s(path, 0, 0, 0, 0, file, fileNameSize, ext, _MAX_EXT);
strncat_s(file, fileNameSize, ext, _MAX_EXT);
}
void InitStackFrameFromContext(PCONTEXT context, STACKFRAME64& stackFrame)
{
#if defined(_M_ARM64)
#pragma message ( "Warning: No CPU stack debug implemented yet" )
#else
#ifdef _WIN64
stackFrame.AddrPC.Offset = context->Rip;
stackFrame.AddrFrame.Offset = context->Rbp;
stackFrame.AddrStack.Offset = context->Rsp;
#else
stackFrame.AddrPC.Offset = context->Eip;
stackFrame.AddrFrame.Offset = context->Ebp;
stackFrame.AddrStack.Offset = context->Esp;
#endif
#endif
}
void** GetNextStackFrame(void** prevSP)
{
void** newSP = (void**)(*prevSP);
if (newSP == prevSP)
return 0;
// Difference between stack pointers has to be sane.
if (newSP > prevSP && ((uintptr_t)newSP - (uintptr_t)prevSP) > 1000000)
return 0;
if ((uintptr_t)newSP & (sizeof(void*) - 1))
return 0;
return newSP;
}
} // namespace
namespace rde
{
bool StackTrace::InitSymbols()
{
static bool ls_initialized(false);
if (!ls_initialized)
{
DWORD options = SYMOPT_FAIL_CRITICAL_ERRORS |
SYMOPT_DEFERRED_LOADS |
SYMOPT_LOAD_LINES |
SYMOPT_UNDNAME;
SymSetOptions(options);
const char* dir = nullptr;
if (!SymInitialize(GetCurrentProcess(), dir, options & SYMOPT_DEFERRED_LOADS))
{
OutputDebugString("Cannot initialize symbol engine");
return false;
}
ls_initialized = true;
}
return true;
}
int StackTrace::GetCallStack(Address* callStack, int maxDepth, int entriesToSkip)
{
PCONTEXT pContext(0);
const HMODULE hKernel32Dll = GetModuleHandle("kernel32.dll");
typedef void(WINAPI* pRtlCaptureContext)(PCONTEXT);
static pRtlCaptureContext RtlCaptureContext = nullptr;
if(RtlCaptureContext == nullptr)
RtlCaptureContext = (pRtlCaptureContext)GetProcAddress(hKernel32Dll, "RtlCaptureContext");
CONTEXT context;
if (RtlCaptureContext)
{
memset(&context, 0, sizeof(context));
context.ContextFlags = CONTEXT_FULL;
RtlCaptureContext(&context);
pContext = &context;
}
// +1 -> skip over "us"
return GetCallStack(pContext, callStack, maxDepth, entriesToSkip + 1);
}
int StackTrace::GetCallStack(void* vcontext, Address* callStack, int maxDepth,
int entriesToSkip)
{
#if defined(_M_ARM64)
#pragma message ( "Warning: No CPU stack debug implemented yet" )
uintptr_t ebpReg[2];
uintptr_t espReg;
#else
#ifndef _WIN64
uintptr_t* ebpReg;
uintptr_t espReg;
// clang-format off
__asm mov [ebpReg], ebp
__asm mov [espReg], esp
// clang-format on
#else
uintptr_t ebpReg[2];
uintptr_t espReg;
CONTEXT Context;
RtlCaptureContext(&Context);
ebpReg[1] = Context.Rip;
ebpReg[0] = Context.Rbp;
espReg = Context.Rsp;
#endif
#endif
InitSymbols();
STACKFRAME64 stackFrame = {};
PCONTEXT context = (PCONTEXT)vcontext;
if (context == 0)
{
stackFrame.AddrPC.Offset = ebpReg[1];
stackFrame.AddrFrame.Offset = ebpReg[0];
stackFrame.AddrStack.Offset = espReg;
}
else
{
InitStackFrameFromContext(context, stackFrame);
}
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Mode = AddrModeFlat;
HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread();
int numEntries(0);
while (::StackWalk64(IMAGE_FILE_MACHINE_I386, process, thread,
&stackFrame, context, 0, SymFunctionTableAccess64, SymGetModuleBase64, nullptr) &&
stackFrame.AddrFrame.Offset != 0 && numEntries < maxDepth)
{
if (entriesToSkip > 0)
--entriesToSkip;
else
callStack[numEntries++] = reinterpret_cast<Address>(stackFrame.AddrPC.Offset);
}
return numEntries;
}
int StackTrace::GetCallStack_Fast(Address* callStack, int maxDepth, int entriesToSkip)
{
uintptr_t ebpReg;
#if defined(_M_ARM64)
#pragma message ( "Warning: No CPU stack debug implemented yet" )
#else
#ifndef _WIN64
__asm mov [ebpReg], ebp
#else
CONTEXT Context;
RtlCaptureContext(&Context);
ebpReg = Context.Rbp;
#endif
#endif
void** sp = (void**)ebpReg;
int numEntries(0);
while (sp && numEntries < maxDepth)
{
if (entriesToSkip > 0)
--entriesToSkip;
else
callStack[numEntries++] = sp[1];
sp = ::GetNextStackFrame(sp);
}
return numEntries;
}
int StackTrace::GetSymbolInfo(Address address, char* symbol, int maxSymbolLen)
{
if (!InitSymbols())
return 0;
// Start with address.
int charsAdded =
_snprintf_s(symbol, maxSymbolLen, _TRUNCATE, "%p ", address);
symbol += charsAdded;
maxSymbolLen -= charsAdded;
if (maxSymbolLen < 0)
return charsAdded;
const DWORD64 address64 = (DWORD64)address;
// Module name
IMAGEHLP_MODULE64 moduleInfo = {};
moduleInfo.SizeOfStruct = sizeof(moduleInfo);
const HANDLE hCurrentProcess = GetCurrentProcess();
if (SymGetModuleInfo64(hCurrentProcess, address64, &moduleInfo))
{
char moduleName[MAXSTRING + 1];
GetFileFromPath(moduleInfo.ImageName, moduleName, MAXSTRING);
const int moduleLen = (int)strnlen_s(moduleName,sizeof(moduleName));
strncpy_s(symbol, maxSymbolLen, moduleName, maxSymbolLen-1);
symbol += moduleLen;
charsAdded += moduleLen;
maxSymbolLen -= moduleLen;
}
if (maxSymbolLen <= 0)
return charsAdded;
// Symbol name
ULONG64 symbolBuffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME*sizeof(TCHAR) +
sizeof(ULONG64) - 1) / sizeof(ULONG64)] = { 0 };
IMAGEHLP_SYMBOL64* symbolInfo = reinterpret_cast<IMAGEHLP_SYMBOL64*>(symbolBuffer);
symbolInfo->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
symbolInfo->MaxNameLength = MAX_SYM_NAME;
DWORD64 disp(0);
if (SymGetSymFromAddr64(hCurrentProcess, address64, &disp, symbolInfo))
{
const int symbolChars =
_snprintf_s(symbol, maxSymbolLen, _TRUNCATE, " %s + 0x%llX", symbolInfo->Name, disp);
symbol += symbolChars;
maxSymbolLen -= symbolChars;
charsAdded += symbolChars;
}
if (maxSymbolLen <= 0)
return charsAdded;
// File + line
DWORD displacementLine;
IMAGEHLP_LINE64 lineInfo = {};
lineInfo.SizeOfStruct = sizeof(lineInfo);
if (SymGetLineFromAddr64(hCurrentProcess, address64, &displacementLine, &lineInfo))
{
char fileName[MAXSTRING + 1];
GetFileFromPath(lineInfo.FileName, fileName, MAXSTRING);
int fileLineChars;
if (displacementLine > 0)
{
fileLineChars = _snprintf_s(symbol, maxSymbolLen, _TRUNCATE,
" %s(%u+%04u byte(s))", fileName, lineInfo.LineNumber, displacementLine);
}
else
{
fileLineChars = _snprintf_s(symbol, maxSymbolLen, _TRUNCATE,
" %s(%u)", fileName, lineInfo.LineNumber);
}
symbol += fileLineChars;
maxSymbolLen -= fileLineChars;
charsAdded += fileLineChars;
}
return charsAdded;
}
void StackTrace::GetCallStack(void* vcontext, bool includeArguments,
char* symbol, int maxSymbolLen)
{
const PCONTEXT context = (PCONTEXT)vcontext;
if (context == 0)
return;
InitSymbols();
STACKFRAME64 stackFrame = {};
InitStackFrameFromContext(context, stackFrame);
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Mode = AddrModeFlat;
while (maxSymbolLen > 0 &&
::StackWalk64(IMAGE_FILE_MACHINE_I386,
::GetCurrentProcess(), ::GetCurrentThread(), &stackFrame,
context, nullptr, /*Internal_ReadProcessMemory,*/
SymFunctionTableAccess64, SymGetModuleBase64, nullptr) != FALSE &&
stackFrame.AddrFrame.Offset != 0)
{
const Address addr = reinterpret_cast<Address>(stackFrame.AddrPC.Offset);
int charsAdded = GetSymbolInfo(addr, symbol, maxSymbolLen);
maxSymbolLen -= charsAdded;
symbol += charsAdded;
if (maxSymbolLen > 0 && includeArguments)
{
charsAdded = _snprintf_s(symbol, maxSymbolLen, _TRUNCATE,
" (0x%08llX 0x%08llX 0x%08llX 0x%08llx)\n", stackFrame.Params[0],
stackFrame.Params[1], stackFrame.Params[2], stackFrame.Params[3]);
maxSymbolLen -= charsAdded;
symbol += charsAdded;
}
}
}
}