-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathExceptionHandler_win32.cpp
More file actions
303 lines (254 loc) · 9.87 KB
/
Copy pathExceptionHandler_win32.cpp
File metadata and controls
303 lines (254 loc) · 9.87 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
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
#include "Common/precompiled.h"
#include "Cafe/CafeSystem.h"
#include "ExceptionHandler.h"
#include <Windows.h>
#include <Dbghelp.h>
#include <shellapi.h>
#include "config/ActiveSettings.h"
#include "config/CemuConfig.h"
#include "Cafe/OS/libs/coreinit/coreinit_Thread.h"
#include "Cafe/HW/Espresso/PPCState.h"
#include "Cafe/HW/Espresso/Debugger/GDBStub.h"
LONG handleException_SINGLE_STEP(PEXCEPTION_POINTERS pExceptionInfo)
{
return EXCEPTION_CONTINUE_SEARCH;
}
#include <boost/algorithm/string.hpp>
BOOL CALLBACK MyMiniDumpCallback(PVOID pParam, const PMINIDUMP_CALLBACK_INPUT pInput, PMINIDUMP_CALLBACK_OUTPUT pOutput)
{
if (!pInput || !pOutput)
return FALSE;
switch (pInput->CallbackType)
{
case IncludeModuleCallback:
case IncludeThreadCallback:
case ThreadCallback:
case ThreadExCallback:
return TRUE;
case ModuleCallback:
if (!(pOutput->ModuleWriteFlags & ModuleReferencedByMemory))
pOutput->ModuleWriteFlags &= ~ModuleWriteModule;
return TRUE;
}
return FALSE;
}
bool CreateMiniDump(CrashDump dump, EXCEPTION_POINTERS* pep)
{
if (dump == CrashDump::Disabled)
return true;
fs::path p = ActiveSettings::GetUserDataPath("crashdump");
std::error_code ec;
fs::create_directories(p, ec);
if (ec)
return false;
const auto now = std::chrono::system_clock::now();
const auto temp_time = std::chrono::system_clock::to_time_t(now);
const auto& time = *std::gmtime(&temp_time);
p /= fmt::format("crash_{:04d}{:02d}{:02d}_{:02d}{:02d}{:02d}.dmp", 1900 + time.tm_year, time.tm_mon + 1, time.tm_mday, time.tm_year, time.tm_hour, time.tm_min, time.tm_sec);
const auto hFile = CreateFileW(p.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
return false;
MINIDUMP_EXCEPTION_INFORMATION mdei;
mdei.ThreadId = GetCurrentThreadId();
mdei.ExceptionPointers = pep;
mdei.ClientPointers = FALSE;
MINIDUMP_CALLBACK_INFORMATION mci;
mci.CallbackRoutine = (MINIDUMP_CALLBACK_ROUTINE)MyMiniDumpCallback;
mci.CallbackParam = nullptr;
MINIDUMP_TYPE mdt;
if (dump == CrashDump::Full)
{
mdt = (MINIDUMP_TYPE)(MiniDumpWithPrivateReadWriteMemory |
MiniDumpWithDataSegs |
MiniDumpWithHandleData |
MiniDumpWithFullMemoryInfo |
MiniDumpWithThreadInfo |
MiniDumpWithUnloadedModules);
}
else
{
mdt = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory);
}
const auto result = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, &mdei, nullptr, &mci);
CloseHandle(hFile);
return result != FALSE;
}
void DumpThreadStackTrace()
{
HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);
char dumpLine[1024 * 4];
void* stack[100];
const unsigned short frames = CaptureStackBackTrace(0, 40, stack, NULL);
SYMBOL_INFO* symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
CrashLog_WriteHeader("Stack trace");
for (unsigned int i = 0; i < frames; i++)
{
DWORD64 stackTraceOffset = (DWORD64)stack[i];
SymFromAddr(process, stackTraceOffset, 0, symbol);
sprintf(dumpLine, "0x%016I64x ", (uint64)(size_t)stack[i]);
cemuLog_writePlainToLog(dumpLine);
// module name
HMODULE stackModule;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)stackTraceOffset, &stackModule))
{
char moduleName[512];
moduleName[0] = '\0';
GetModuleFileNameA(stackModule, moduleName, 512);
sint32 moduleNameStartIndex = std::max((sint32)0, (sint32)strlen(moduleName) - 1);
while (moduleNameStartIndex > 0)
{
if (moduleName[moduleNameStartIndex] == '\\' || moduleName[moduleNameStartIndex] == '/')
{
moduleNameStartIndex++;
break;
}
moduleNameStartIndex--;
}
DWORD64 moduleAddress = (DWORD64)GetModuleHandleA(moduleName);
sint32 relativeOffset = 0;
if (moduleAddress != 0)
relativeOffset = stackTraceOffset - moduleAddress;
sprintf(dumpLine, "+0x%08x %-16s", relativeOffset, moduleName + moduleNameStartIndex);
cemuLog_writePlainToLog(dumpLine);
}
else
{
sprintf(dumpLine, "+0x00000000 %-16s", "NULL");
cemuLog_writePlainToLog(dumpLine);
}
// function name
sprintf(dumpLine, " %s\n", symbol->Name);
cemuLog_writePlainToLog(dumpLine);
}
free(symbol);
}
void createCrashlog(EXCEPTION_POINTERS* e, PCONTEXT context)
{
if(!CrashLog_Create())
return; // give up if crashlog was already created
const auto crash_dump = GetConfig().crash_dump.GetValue();
const auto dump_written = CreateMiniDump(crash_dump, e);
if (!dump_written)
cemuLog_writeLineToLog(fmt::format("couldn't write minidump {:#x}", GetLastError()), false, true);
char dumpLine[1024 * 4];
// info about Cemu version
sprintf(dumpLine, "\nCrashlog for %s\n", BUILD_VERSION_WITH_NAME_STRING);
cemuLog_writePlainToLog(dumpLine);
SYSTEMTIME sysTime;
GetSystemTime(&sysTime);
sprintf(dumpLine, "Date: %02d-%02d-%04d %02d:%02d:%02d\n\n", (sint32)sysTime.wDay, (sint32)sysTime.wMonth, (sint32)sysTime.wYear, (sint32)sysTime.wHour, (sint32)sysTime.wMinute, (sint32)sysTime.wSecond);
cemuLog_writePlainToLog(dumpLine);
DumpThreadStackTrace();
// info about exception
if (e->ExceptionRecord)
{
HMODULE exceptionModule;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)(e->ExceptionRecord->ExceptionAddress), &exceptionModule))
{
char moduleName[512];
moduleName[0] = '\0';
GetModuleFileNameA(exceptionModule, moduleName, 512);
sint32 moduleNameStartIndex = std::max((sint32)0, (sint32)strlen(moduleName) - 1);
while (moduleNameStartIndex > 0)
{
if (moduleName[moduleNameStartIndex] == '\\' || moduleName[moduleNameStartIndex] == '/')
{
moduleNameStartIndex++;
break;
}
moduleNameStartIndex--;
}
sprintf(dumpLine, "Exception 0x%08x at 0x%I64x(+0x%I64x) in module %s\n", (uint32)e->ExceptionRecord->ExceptionCode, (uint64)e->ExceptionRecord->ExceptionAddress, (uint64)e->ExceptionRecord->ExceptionAddress - (uint64)exceptionModule, moduleName + moduleNameStartIndex);
cemuLog_writePlainToLog(dumpLine);
}
else
{
sprintf(dumpLine, "Exception 0x%08x at 0x%I64x\n", (uint32)e->ExceptionRecord->ExceptionCode, (uint64)e->ExceptionRecord->ExceptionAddress);
cemuLog_writePlainToLog(dumpLine);
}
}
sprintf(dumpLine, "cemu.exe at 0x%I64x\n", (uint64)GetModuleHandle(NULL));
cemuLog_writePlainToLog(dumpLine);
// register info
sprintf(dumpLine, "\n");
cemuLog_writePlainToLog(dumpLine);
#if defined(ARCH_X86_64)
sprintf(dumpLine, "RAX=%016I64x RBX=%016I64x RCX=%016I64x RDX=%016I64x\n", context->Rax, context->Rbx, context->Rcx, context->Rdx);
cemuLog_writePlainToLog(dumpLine);
sprintf(dumpLine, "RSP=%016I64x RBP=%016I64x RDI=%016I64x RSI=%016I64x\n", context->Rsp, context->Rbp, context->Rdi, context->Rsi);
cemuLog_writePlainToLog(dumpLine);
sprintf(dumpLine, "R8 =%016I64x R9 =%016I64x R10=%016I64x R11=%016I64x\n", context->R8, context->R9, context->R10, context->R11);
cemuLog_writePlainToLog(dumpLine);
sprintf(dumpLine, "R12=%016I64x R13=%016I64x R14=%016I64x R15=%016I64x\n", context->R12, context->R13, context->R14, context->R15);
cemuLog_writePlainToLog(dumpLine);
#else // AArch64
for (int i = 0; i < 28; i += 4)
{
sprintf(dumpLine, "X%-2d=%016I64x X%-2d=%016I64x X%-2d=%016I64x X%-2d=%016I64x\n",
i, context->X[i], i + 1, context->X[i + 1], i + 2, context->X[i + 2], i + 3, context->X[i + 3]);
cemuLog_writePlainToLog(dumpLine);
}
sprintf(dumpLine, "X28=%016I64x FP =%016I64x LR =%016I64x SP =%016I64x PC =%016I64x\n",
context->X[28], context->Fp, context->Lr, context->Sp, context->Pc);
cemuLog_writePlainToLog(dumpLine);
#endif
CrashLog_SetOutputChannels(false, true);
ExceptionHandler_LogGeneralInfo();
CrashLog_SetOutputChannels(true, true);
cemuLog_waitForFlush();
// save log with the dump
if (dump_written && crash_dump != CrashDump::Disabled)
{
const auto now = std::chrono::system_clock::now();
const auto temp_time = std::chrono::system_clock::to_time_t(now);
const auto& time = *std::gmtime(&temp_time);
fs::path p = ActiveSettings::GetUserDataPath("crashdump");
p /= fmt::format("log_{:04d}{:02d}{:02d}_{:02d}{:02d}{:02d}.txt", 1900 + time.tm_year, time.tm_mon + 1, time.tm_mday, time.tm_year, time.tm_hour, time.tm_min, time.tm_sec);
std::error_code ec;
fs::copy_file(ActiveSettings::GetUserDataPath("log.txt"), p, ec);
}
exit(0);
return;
}
bool logCrashlog;
int crashlogThread(void* exceptionInfoRawPtr)
{
PEXCEPTION_POINTERS pExceptionInfo = (PEXCEPTION_POINTERS)exceptionInfoRawPtr;
createCrashlog(pExceptionInfo, pExceptionInfo->ContextRecord);
logCrashlog = true;
return 0;
}
void debugger_handleSingleStepException(uint64 dr6);
LONG WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP)
{
LONG r = handleException_SINGLE_STEP(pExceptionInfo);
if (r != EXCEPTION_CONTINUE_SEARCH)
return r;
#if defined(ARCH_X86_64)
if (GetBits(pExceptionInfo->ContextRecord->Dr6, 0, 1) || GetBits(pExceptionInfo->ContextRecord->Dr6, 1, 1))
debugger_handleSingleStepException(pExceptionInfo->ContextRecord->Dr6);
else if (GetBits(pExceptionInfo->ContextRecord->Dr6, 2, 1) || GetBits(pExceptionInfo->ContextRecord->Dr6, 3, 1))
g_gdbstub->HandleAccessException(pExceptionInfo->ContextRecord->Dr6);
return EXCEPTION_CONTINUE_EXECUTION;
#else // AArch64: hardware debug registers (Dr6) not available; debugger single-step unsupported for now
return EXCEPTION_CONTINUE_SEARCH;
#endif
}
return EXCEPTION_CONTINUE_SEARCH;
}
LONG WINAPI cemu_unhandledExceptionFilter(EXCEPTION_POINTERS* pExceptionInfo)
{
createCrashlog(pExceptionInfo, pExceptionInfo->ContextRecord);
return EXCEPTION_NONCONTINUABLE_EXCEPTION;
}
void ExceptionHandler_Init()
{
SetUnhandledExceptionFilter(cemu_unhandledExceptionFilter);
AddVectoredExceptionHandler(1, VectoredExceptionHandler);
SetErrorMode(SEM_FAILCRITICALERRORS);
}