-
Notifications
You must be signed in to change notification settings - Fork 23
/
CrashDump.cpp
executable file
·298 lines (248 loc) · 8.76 KB
/
CrashDump.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
#include "ist.h"
#ifndef _DEBUG
#pragma optimize("s", on)
#pragma auto_inline(off)
#endif
#pragma code_seg(DEBUG_SEG)
#include "environ.h"
#include "rc_vm.h"
#include "interprt.h"
#include "VMExcept.h"
#include "RegKey.h"
static const DWORD DefaultStackDepth = 300;
static const DWORD DefaultWalkbackDepth = static_cast<DWORD>(-1);
static const int MAXDUMPPARMCHARS = 40;
extern wchar_t achImagePath[];
// Warning about SEH and destructable objects
#pragma warning (disable : 4509)
using namespace std;
wostream& operator<<(wostream& stream, const CONTEXT* pCtx)
{
wostream::char_type cFill = stream.fill('0');
stream.setf(ios::uppercase);
stream << std::hex
<< L"EAX = " << setw(8) << pCtx->Eax<< L" EBX = " << setw(8) << pCtx->Ebx<< L" ECX = " << setw(8) << pCtx->Ecx << std::endl
<< L"ESI = " << setw(8) << pCtx->Esi<< L" EDI = " << setw(8) << pCtx->Edi<< L" EIP = " << setw(8) << pCtx->Eip << std::endl
<< L"ESP = " << setw(8) << pCtx->Esp<< L" EBP = " << setw(8) << pCtx->Ebp<< L" EFL = " << setw(8) << pCtx->EFlags << std::endl
<< L"CS = " << setw(4) << pCtx->SegCs<< L" SS = " << setw(4) << pCtx->SegSs<< L" DS = " << setw(4) << pCtx->SegDs << std::endl
<< L"ES = " << setw(4) << pCtx->SegEs<< L" FS = " << setw(4) << pCtx->SegFs<< L" GS = " << setw(4) << pCtx->SegGs << std::endl;
stream.fill(cFill);
stream.unsetf(ios::uppercase);
return stream;
}
void CrashDump(EXCEPTION_POINTERS *pExceptionInfo, wostream* pStream, DWORD nStackDepth, DWORD nWalkbackDepth)
{
SYSTEMTIME stNow;
GetLocalTime(&stNow);
if (pStream == NULL)
pStream = &TRACESTREAM;
*pStream << std::endl;
for (int i=0;i<80;i++)
*pStream << L'*';
EXCEPTION_RECORD* pExRec = pExceptionInfo->ExceptionRecord;
DWORD exceptionCode = pExRec->ExceptionCode;
*pStream << std::endl;
for (int i=0;i<26;i++)
*pStream << L'*';
*pStream<< L" Dolphin Crash Dump Report ";
for (int i=0;i<27;i++)
*pStream << L'*';
wchar_t szModule[_MAX_PATH+1];
LPWSTR szFileName = 0;
{
wchar_t szPath[_MAX_PATH+1];
::GetModuleFileNameW(GetModuleHandle(NULL), szPath, _MAX_PATH);
::GetFullPathNameW(szPath, _MAX_PATH, szModule, &szFileName);
}
*pStream << std::endl << std::endl << stNow
<< L": " << szFileName
<< L" caused an unhandled Win32 Exception "
<< PVOID(exceptionCode) << std::endl
<< L"at " << pExRec->ExceptionAddress;
// Determine the module in which it occurred
MEMORY_BASIC_INFORMATION mbi;
::VirtualQuery(pExRec->ExceptionAddress, &mbi, sizeof(mbi));
HMODULE hMod = HMODULE(mbi.AllocationBase);
wcscpy_s(szModule, L"<UNKNOWN>");
::GetModuleFileNameW(hMod, szModule, _MAX_PATH);
*pStream<< L" in module " << hMod<< L" (" << szModule<< L")" << std::endl << std::endl;
const DWORD NumParms = pExRec->NumberParameters;
if (NumParms > 0)
{
*pStream << L"*----> Exception Parameters <----*" << std::endl;
wostream::char_type cFill = pStream->fill(L'0');
pStream->setf(ios::uppercase);
*pStream << std::hex;
for (unsigned i=0;i<NumParms;i++)
{
DWORD dwParm = pExRec->ExceptionInformation[i];
*pStream << setw(8) << dwParm<< L" ";
BYTE* pBytes = reinterpret_cast<BYTE*>(dwParm);
if (!IsBadReadPtr(pBytes, MAXDUMPPARMCHARS))
{
wchar_t buf[MAXDUMPPARMCHARS+1];
wcsncpy_s(buf, reinterpret_cast<LPCWSTR>(pBytes), MAXDUMPPARMCHARS);
buf[MAXDUMPPARMCHARS] = 0;
*pStream << buf;
}
*pStream << std::endl;
}
pStream->fill(cFill);
pStream->unsetf(ios::uppercase);
*pStream << std::endl;
}
DWORD dwThreadId = GetCurrentThreadId();
PCONTEXT pCtx = pExceptionInfo->ContextRecord;
*pStream<< L"*----> CPU Context for thread 0x" << std::hex << dwThreadId
<< L" <----*" << std::endl
<< pCtx << std::endl;
DWORD dwMainThreadId = Interpreter::MainThreadId();
if (dwThreadId == dwMainThreadId)
{
DWORD dwCode=0;
_try
{
Interpreter::DumpContext(pExceptionInfo, *pStream);
*pStream << std::endl<< L"*----> Stack Back Trace <----*" << std::endl;
Interpreter::StackTraceOn(*pStream, NULL, nWalkbackDepth);
Interpreter::DumpStack(*pStream, nStackDepth);
}
_except((dwCode=GetExceptionCode()), EXCEPTION_EXECUTE_HANDLER)
{
*pStream << std::endl<< L"***Unable to generate complete exception report ("
<< std::hex << dwCode << L')' << std::endl;
}
}
else
{
*pStream <<L"****N.B. This exception did NOT occur in the main Dolphin execution thread ****" << std::endl << std::endl;
*pStream<< L"*----> CPU Context for main thread 0x" << std::hex << dwMainThreadId
<< L" <----*" << std::endl;
// An overlapped thread
HANDLE hMain = Interpreter::MainThreadHandle();
DWORD dwRet = SuspendThread(hMain);
if (int(dwRet) >= 0)
{
CONTEXT ctxMain;
memset(&ctxMain, 0, sizeof(CONTEXT));
ctxMain.ContextFlags = CONTEXT_FULL;
if (::GetThreadContext(hMain, &ctxMain))
{
*pStream << &ctxMain << std::endl;
::VirtualQuery(reinterpret_cast<void*>(ctxMain.Eip), &mbi, sizeof(mbi));
hMod = HMODULE(mbi.AllocationBase);
wcscpy_s(szModule, L"<UNKNOWN>");
::GetModuleFileNameW(hMod, szModule, _MAX_PATH);
*pStream<< L"In module " << hMod<< L" (" << szModule<< L")" << std::endl << std::endl;
}
else
*pStream << std::endl<< L"*** Unable to access main interpter thread context ("
<< std::dec << GetLastError()<< L")" << std::endl;
::ResumeThread(hMain);
}
else
{
*pStream << std::endl<< L"*** Unable to suspend main interpreter thread (" << std::dec << GetLastError()<< L")"
<< std::endl;
}
}
*pStream << std::endl<< L"***** End of crash report *****" << std::endl << std::endl;
pStream->flush();
}
wostream* OpenLogStream(const wchar_t* achLogPath, const wchar_t* achImagePath, wofstream& fStream)
{
wchar_t path[_MAX_PATH];
if (achLogPath == NULL || !wcslen(achLogPath))
{
// Write the dump to the errors file
wchar_t drive[_MAX_DRIVE];
wchar_t dir[_MAX_DIR];
wchar_t fname[_MAX_FNAME];
_wsplitpath_s(achImagePath, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, NULL, 0);
_wmakepath(path, drive, dir, fname, L".ERRORS");
achLogPath = path;
}
trace(L"Dolphin: Writing dump to '%.260s'\n", achLogPath);
wostream* pStream = NULL;
// Open the error log for appending
fStream.open(achLogPath, ios::out | ios::app | ios::ate);
if (fStream.fail())
trace(L"Dolphin: Unable to open crash dump log '%.260s', dump follows:\n\n", achLogPath);
else
pStream = &fStream;
return pStream;
}
void CrashDump(EXCEPTION_POINTERS *pExceptionInfo, const wchar_t* achImagePath)
{
DWORD nStackDepth = DefaultStackDepth;
DWORD nWalkbackDepth = DefaultWalkbackDepth;
wostream* pStream = NULL;
wofstream fStream;
CRegKey rkDump;
if (OpenDolphinKey(rkDump, L"CrashDump", KEY_READ)==ERROR_SUCCESS)
{
wchar_t achLogPath[_MAX_PATH+1];
achLogPath[0] = 0;
unsigned long size = _MAX_PATH;
rkDump.QueryStringValue(L"", achLogPath, &size);
pStream = OpenLogStream(achLogPath, achImagePath, fStream);
if (rkDump.QueryDWORDValue(L"StackDepth", nStackDepth)!=ERROR_SUCCESS || nStackDepth == 0)
nStackDepth = DefaultStackDepth;
if (rkDump.QueryDWORDValue(L"WalkbackDepth", nWalkbackDepth)!=ERROR_SUCCESS || nWalkbackDepth == 0)
nWalkbackDepth = DefaultWalkbackDepth;
}
else
pStream = OpenLogStream(nullptr, achImagePath, fStream);
CrashDump(pExceptionInfo, pStream, nStackDepth, nWalkbackDepth);
}
void __cdecl DebugCrashDump(const wchar_t* szFormat, ...)
{
wchar_t buf[1024];
va_list args;
va_start(args, szFormat);
::StringCbVPrintfW(buf, sizeof(buf), szFormat, args);
va_end(args);
DWORD dwArgs[1];
dwArgs[0] = reinterpret_cast<DWORD>(&buf);
RaiseException(SE_VMDUMPSTATUS, 0, 1, dwArgs);
}
void __stdcall Dump2(const wchar_t* szMsg, wostream* pStream, int nStackDepth, int nWalkbackDepth)
{
if (pStream == NULL)
pStream = &TRACESTREAM;
*pStream << std::endl;
for (int i=0;i<26;i++)
*pStream << L'*';
*pStream<< L" Dolphin Virtual Machine Dump Report ";
for (int i=0;i<27;i++)
*pStream << L'*';
// Dump the time and message
{
SYSTEMTIME stNow;
GetLocalTime(&stNow);
*pStream << std::endl << std::endl << stNow
<< L": " << szMsg << std::endl << std::endl;
}
Interpreter::DumpContext(*pStream);
*pStream << std::endl<< L"*----> Stack Back Trace <----*" << std::endl;
Interpreter::StackTraceOn(*pStream, NULL, nWalkbackDepth);
Interpreter::DumpStack(*pStream, nStackDepth);
*pStream << std::endl<< L"***** End of dump *****" << std::endl << std::endl;
pStream->flush();
}
extern"C" void __stdcall Dump(const wchar_t* szMsg, const wchar_t* szPath, int nStackDepth, int nWalkbackDepth)
{
wofstream fStream;
wostream* pStream = OpenLogStream(szPath, achImagePath, fStream);
Dump2(szMsg, pStream, nStackDepth, nWalkbackDepth);
}
void __stdcall DebugDump(const wchar_t* szFormat, ...)
{
wchar_t buf[1024];
va_list args;
va_start(args, szFormat);
::StringCbVPrintfW(buf, sizeof(buf), szFormat, args);
va_end(args);
tracelock lock(TRACESTREAM);
Dump2(buf, &TRACESTREAM, 0, -1);
}