forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrashHandler.cpp
331 lines (295 loc) · 11.2 KB
/
CrashHandler.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "stdafx.h"
#include "CrashHandler.h"
#include "BlackBox.h"
#include "MemoryStatus.h"
#include "StackTrace.h"
#include <cstdio>
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <dbghelp.h>
#include <cassert>
#include "vpversion.h"
namespace
{
char s_miniDumpFileName[MAX_PATH] = "crash.dmp";
char s_reportFileName[MAX_PATH] = "crash.txt";
void WriteProcessName(FILE* f)
{
fprintf(f, "Process: ");
char buffer[MAX_PATH + 1];
const HMODULE hModule = nullptr;
GetModuleFileName(hModule, buffer, MAX_PATH);
const char* lastSeparatorPos = strrchr(buffer, PATH_SEPARATOR_CHAR);
if (lastSeparatorPos != 0)
fprintf(f, "%s", lastSeparatorPos + 1); // +1 -> skip over separator
else
fprintf(f, "%s", buffer);
}
typedef HRESULT(STDAPICALLTYPE *pRGV)(LPOSVERSIONINFOEXW osi);
static pRGV mRtlGetVersion = nullptr;
void WriteSystemInfo(FILE* f)
{
if (mRtlGetVersion == nullptr)
mRtlGetVersion = (pRGV)GetProcAddress(GetModuleHandle(TEXT("ntdll")), "RtlGetVersion"); // apparently the only really reliable solution to get the OS version (as of Win10 1803)
DWORD major, minor, build;
BYTE product;
if (mRtlGetVersion != nullptr) // Windows 10 1803 and above
{
OSVERSIONINFOEXW osInfo;
osInfo.dwOSVersionInfoSize = sizeof(osInfo);
mRtlGetVersion(&osInfo);
major = osInfo.dwMajorVersion;
minor = osInfo.dwMinorVersion;
build = osInfo.dwBuildNumber;
product = osInfo.wProductType;
}
else
{
OSVERSIONINFOEX sysInfo = {};
sysInfo.dwOSVersionInfoSize = sizeof(sysInfo);
::GetVersionEx((OSVERSIONINFO*)&sysInfo);
major = sysInfo.dwMajorVersion;
minor = sysInfo.dwMinorVersion;
build = sysInfo.dwBuildNumber;
product = sysInfo.wProductType;
}
// We're mainly interested in rough info and latest systems
fprintf(f, "System: ");
if (major == 10)
{
fprintf(f, "Windows 10 (%lu.%lu %lu%s)\n", major, minor, build, VER_NT_WORKSTATION ? "" : " Server");
}
else if (major == 6 && minor == 2)
{
if (product != VER_NT_WORKSTATION)
fprintf(f, "Windows Server 2012 (or above)\n"); //!! as otherwise the manifest should be adapted to target 8.1 or 10
else
fprintf(f, "Windows 8 (or above)\n"); //!! as otherwise the manifest should be adapted to target 8.1 or 10
}
else if (major == 6 && minor == 1)
{
if (product != VER_NT_WORKSTATION)
fprintf(f, "Windows Server 2008 R2\n");
else
fprintf(f, "Windows 7\n");
}
else if (major == 6 && minor == 0)
{
if (product != VER_NT_WORKSTATION)
fprintf(f, "Windows Server 2008\n");
else
fprintf(f, "Windows Vista\n");
}
else if (major == 5)
{
if (minor == 2)
fprintf(f, "Windows Server 2003\n");
else if (minor == 1)
fprintf(f, "Windows XP\n");
else if (minor == 0)
fprintf(f, "Windows 2000\n");
}
else
fprintf(f, "Unknown Windows version - %lu.%lu (%lu)\n", major, minor, build);
}
void WriteProcessorInfo(FILE* f)
{
SYSTEM_INFO sysInfo = {};
GetSystemInfo(&sysInfo);
fprintf(f, "Number of CPUs: %lu\nProcessor type: %lu\n",
sysInfo.dwNumberOfProcessors, sysInfo.dwProcessorType);
}
void WriteDateTime(FILE* f)
{
SYSTEMTIME st;
::GetLocalTime(&st);
fprintf(f, "Date/time: %u/%u/%u, %02u:%02u:%02u:%u\n",
st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute,
st.wSecond, st.wMilliseconds);
}
void WriteHeader(FILE* f)
{
fprintf(f, "Crash report VPX - %s\n============\n", VP_VERSION_STRING_FULL_LITERAL);
}
const char* GetExceptionString(DWORD exc)
{
#define EXC_CASE(EXC) case EXCEPTION_##EXC : return "EXCEPTION_" #EXC
switch (exc)
{
EXC_CASE(ACCESS_VIOLATION);
EXC_CASE(DATATYPE_MISALIGNMENT);
EXC_CASE(BREAKPOINT);
EXC_CASE(SINGLE_STEP);
EXC_CASE(ARRAY_BOUNDS_EXCEEDED);
EXC_CASE(FLT_DENORMAL_OPERAND);
EXC_CASE(FLT_DIVIDE_BY_ZERO);
EXC_CASE(FLT_INEXACT_RESULT);
EXC_CASE(FLT_INVALID_OPERATION);
EXC_CASE(FLT_OVERFLOW);
EXC_CASE(FLT_STACK_CHECK);
EXC_CASE(FLT_UNDERFLOW);
EXC_CASE(INT_DIVIDE_BY_ZERO);
EXC_CASE(INT_OVERFLOW);
EXC_CASE(PRIV_INSTRUCTION);
EXC_CASE(IN_PAGE_ERROR);
EXC_CASE(ILLEGAL_INSTRUCTION);
EXC_CASE(NONCONTINUABLE_EXCEPTION);
EXC_CASE(STACK_OVERFLOW);
EXC_CASE(INVALID_DISPOSITION);
EXC_CASE(GUARD_PAGE);
EXC_CASE(INVALID_HANDLE);
default:
return "UNKNOWN";
}
#undef EXC_CASE
}
void WriteExceptionInfo(FILE* f, const EXCEPTION_POINTERS* exceptionPtrs)
{
WriteProcessName(f);
fprintf(f, "\n");
fprintf(f, "Reason: 0x%X - %s", exceptionPtrs->ExceptionRecord->ExceptionCode,
GetExceptionString(exceptionPtrs->ExceptionRecord->ExceptionCode));
#if defined(_M_ARM64)
#pragma message ( "Warning: No CPU exception debug output implemented yet" )
#else
fprintf(f, " at %04X:%p\n", exceptionPtrs->ContextRecord->SegCs,
exceptionPtrs->ExceptionRecord->ExceptionAddress);
#endif
if (exceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
fprintf(f, "Attempt to %s 0x%08X\n",
(exceptionPtrs->ExceptionRecord->ExceptionInformation[0] == 1 ?
"write to" : "read from"), (unsigned int) exceptionPtrs->ExceptionRecord->ExceptionInformation[1]);
}
const DWORD threadId = ::GetCurrentThreadId();
fprintf(f, "Thread ID: 0x%X [%lu]\n\n", threadId, threadId);
}
void WriteEnvironmentInfo(FILE* f)
{
fprintf(f, "Environment\n===========\n");
WriteDateTime(f);
WriteProcessorInfo(f);
WriteSystemInfo(f);
fprintf(f, "\n");
}
bool WriteMiniDump(EXCEPTION_POINTERS* exceptionPtrs, const char* fileName)
{
const HANDLE hDump = ::CreateFile(fileName, GENERIC_WRITE, FILE_SHARE_READ, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hDump != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION dumpInfo = { 0 };
dumpInfo.ClientPointers = TRUE;
dumpInfo.ExceptionPointers = exceptionPtrs;
dumpInfo.ThreadId = ::GetCurrentThreadId();
const MINIDUMP_TYPE dumpType = (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory |
MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules);
const BOOL success = ::MiniDumpWriteDump(
::GetCurrentProcess(), ::GetCurrentProcessId(), hDump,
dumpType, &dumpInfo, 0, 0);
::CloseHandle(hDump);
return success == TRUE;
}
return false;
}
void WriteBlackBoxMessages(FILE* f)
{
const int numMessages = rde::BlackBox::GetNumMessages();
if (numMessages <= 0)
return;
fprintf(f, "BlackBox messages\n=================\n");
for (int i = 0; i < numMessages; ++i)
{
int threadId;
const char* message = rde::BlackBox::GetBoxMessage(i, threadId);
fprintf(f, "%d: Thread 0x%X: %s\n", i, threadId, message);
}
}
void WriteMemoryStatus(FILE* f, const rde::MemoryStatus& status)
{
fprintf(f, "Memory status\n=============\n");
fprintf(f, "Total Reserved: %uK (%uM) bytes\n", (unsigned int)status.totalReserved >> 10, (unsigned int)status.totalReserved >> 20);
fprintf(f, "Total Commited: %uK (%uM) bytes\n", (unsigned int)status.totalCommited >> 10, (unsigned int)status.totalCommited >> 20);
fprintf(f, "Total Free: %uK (%uM) bytes\n", (unsigned int)status.totalFree >> 10, (unsigned int)status.totalFree >> 20);
fprintf(f, "Largest Free: %uK (%uM) bytes\n\n", (unsigned int)status.largestFree >> 10, (unsigned int)status.largestFree >> 20);
}
void WriteRegisters(FILE* f, const EXCEPTION_POINTERS* exceptionPtrs)
{
if (IsBadReadPtr(exceptionPtrs, sizeof(EXCEPTION_POINTERS)))
return;
const CONTEXT* ctx = exceptionPtrs->ContextRecord;
fprintf(f, "Registers\n=========\n");
#if defined(_M_ARM64)
#pragma message ( "Warning: No CPU state debug output implemented yet" )
#else
#ifdef _WIN64
fprintf(f, "RAX=%08X RBX=%08X RCX=%08X RDX=%08X\n" \
"RSI=%08X RDI=%08X RBP=%08X RSP=%08X RIP=%08X\n" \
"FLG=%08X CS=%04X DS=%04X SS=%04X ES=%04X FS=%04X GS=%04X\n\n",
(unsigned int)ctx->Rax, (unsigned int)ctx->Rbx, (unsigned int)ctx->Rcx, (unsigned int)ctx->Rdx,
(unsigned int)ctx->Rsi, (unsigned int)ctx->Rdi, (unsigned int)ctx->Rbp, (unsigned int)ctx->Rsp, (unsigned int)ctx->Rip,
(unsigned int)ctx->EFlags, (unsigned int)ctx->SegCs, (unsigned int)ctx->SegDs, (unsigned int)ctx->SegSs, (unsigned int)ctx->SegEs, (unsigned int)ctx->SegFs, (unsigned int)ctx->SegGs);
#else
fprintf(f, "EAX=%08X EBX=%08X ECX=%08X EDX=%08X\n" \
"ESI=%08X EDI=%08X EBP=%08X ESP=%08X EIP=%08X\n" \
"FLG=%08X CS=%04X DS=%04X SS=%04X ES=%04X FS=%04X GS=%04X\n\n",
ctx->Eax, ctx->Ebx, ctx->Ecx, ctx->Edx, ctx->Esi, ctx->Edi,
ctx->Ebp, ctx->Esp, ctx->Eip, ctx->EFlags, ctx->SegCs,
ctx->SegDs, ctx->SegSs, ctx->SegEs, ctx->SegFs, ctx->SegGs);
#endif
#endif
}
void WriteCallStack(FILE* f, PCONTEXT context)
{
char callStack[2048] = {};
rde::StackTrace::GetCallStack(context, true, callStack, sizeof(callStack) - 1);
fprintf(f, "Call stack\n==========\n%s\n", callStack);
}
volatile unsigned long s_inFilter = 0;
LONG __stdcall MyExceptionFilter(EXCEPTION_POINTERS* exceptionPtrs)
{
constexpr LONG returnCode = EXCEPTION_CONTINUE_SEARCH;
// Ignore multiple calls.
if (s_inFilter != 0)
return EXCEPTION_CONTINUE_EXECUTION;
s_inFilter = 1;
// Cannot really do much in case of stack overflow, it'll probably bomb soon
// anyway.
if (exceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
{
OutputDebugString("*** FATAL ERROR: EXCEPTION_STACK_OVERFLOW detected!");
}
const bool miniDumpOK = WriteMiniDump(exceptionPtrs, s_miniDumpFileName);
FILE* f;
if ((fopen_s(&f, s_reportFileName, "wt") == 0) && f)
{
WriteHeader(f);
WriteExceptionInfo(f, exceptionPtrs);
WriteCallStack(f, exceptionPtrs->ContextRecord);
WriteEnvironmentInfo(f);
const rde::MemoryStatus memStatus = rde::MemoryStatus::GetCurrent();
WriteMemoryStatus(f, memStatus);
WriteRegisters(f, exceptionPtrs);
WriteBlackBoxMessages(f);
fprintf(f, (miniDumpOK ? "\nMini dump saved successfully.\n" : "\nFailed to save minidump.\n"));
::fclose(f);
}
return returnCode;
}
} // namespace
namespace rde
{
void CrashHandler::Init()
{
SetUnhandledExceptionFilter(MyExceptionFilter);
}
void CrashHandler::SetMiniDumpFileName(const char* name)
{
strncpy_s(s_miniDumpFileName, name, sizeof(s_miniDumpFileName)-1);
}
void CrashHandler::SetCrashReportFileName(const char* name)
{
strncpy_s(s_reportFileName, name, sizeof(s_reportFileName)-1);
}
}