-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrashHandler.cpp
209 lines (180 loc) · 6.14 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
#include "CrashHandler.h"
#include "FrontendGlobalDef.h"
#include "BackendGlobalDef.h"
#include <VerRsrc.h>
#include <DbgHelp.h>
// ---------------------------------------------------------------------------------------------
// The CryCrashHandlerWindow default constructor.
CryCrashHandlerWindow::CryCrashHandlerWindow(const String& excMsg)
{
this->Title("Crash Report").Sizeable().SetRect(0, 0, 600, 300);
*this
<< this->mErrorImage.SetImage(CtrlImg::error()).LeftPos(10, 50).TopPos(10, 50)
<< this->mDescriptionLabel.SetLabel("An error has occured inside CrySearch. The information\r\nin this crash report can be used to inform the developer\r\nabout a bug or malfunction.").HSizePos(70, 5).TopPos(10, 60)
<< this->mCrashReport.SetEditable(false).HSizePos(5, 5).VSizePos(70, 35)
<< this->mOk.Ok().SetLabel("OK").RightPos(5, 70).BottomPos(5, 25)
<< this->mCopyToClipboard.SetLabel("Copy to Clipboard").LeftPos(5, 150).BottomPos(5, 25)
;
this->mOk <<= THISBACK(CloseWindow);
this->mCopyToClipboard <<= THISBACK(CopyCrashReport);
this->mDescriptionLabel.SetVAlign(ALIGN_TOP);
this->mCrashReport.Set(excMsg);
// an error stays an error, the beep is a nice addition.
BeepExclamation();
}
// The CryCrashHandlerWindow default destructor.
CryCrashHandlerWindow::~CryCrashHandlerWindow()
{
}
// Copies the content of the crash report.
void CryCrashHandlerWindow::CopyCrashReport()
{
WriteClipboardText(this->mCrashReport.Get());
}
// Executed when the crash handler window is closed.
void CryCrashHandlerWindow::CloseWindow()
{
this->Close();
}
// Finds information about a loaded module by its address.
const Win32ModuleInformation* FindLocalModuleAddress(const Index<Win32ModuleInformation>& mods, const SIZE_T current)
{
const int modCount = mods.GetCount();
for (int i = 0; i < modCount; ++i)
{
const Win32ModuleInformation& curMod = mods[i];
if (current >= curMod.BaseAddress && current < curMod.BaseAddress + curMod.Length)
{
return &curMod;
}
}
// The module was not found, so no information is returned.
return NULL;
}
// ---------------------------------------------------------------------------------------------
// Handles exceptions that are not caught just before the application crashes.
LONG __stdcall CrashHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
// Parse exception information.
const PEXCEPTION_RECORD exc = ExceptionInfo->ExceptionRecord;
const ULONG_PTR reason = exc->ExceptionInformation[0];
const LONG_PTR addr = exc->ExceptionInformation[1];
// Start off with the crash report string.
char versionString[256];
GetOSVersionString(versionString, 256);
String excMsg(versionString);
// Add information about the loaded process.
excMsg += "\r\nProcess Information:\r\n\r\n";
excMsg += Format("Name:\t\t\t\t%s\r\n", mMemoryScanner->GetProcessName());
excMsg += Format("Architecture:\t\t%s\r\n", mMemoryScanner->IsX86Process() ? "x86" : "x64");
// Start the exception information.
excMsg += "\r\nException Information:\r\n\r\n";
// Access violations are the most common and should be handled as detailedly as possible.
excMsg += "Exception:\t\t\t";
if (exc->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
excMsg += "Access violation ";
if (reason == 0)
{
// Access violation reading.
#ifdef _WIN64
excMsg += Format("reading location %llX\r\n", addr);
#else
excMsg += Format("reading location %lX\r\n", addr);
#endif
}
else if (reason == 1)
{
// Access violation writing.
#ifdef _WIN64
excMsg += Format("writing location %llX\r\n", addr);
#else
excMsg += Format("writing location %lX\r\n", addr);
#endif
}
else if (reason == 8)
{
// Data Execution Prevention access violation.
#ifdef _WIN64
excMsg += Format("executing location %llX\r\n", addr);
#else
excMsg += Format("executing location %lX\r\n", addr);
#endif
}
else
{
// Unknown access violation exception.
#ifdef _WIN64
excMsg += Format("at location %llX\r\n", addr);
#else
excMsg += Format("at location %lX\r\n", addr);
#endif
}
}
else
{
excMsg += ParseExceptionCode(exc->ExceptionCode);
}
// Add the call stack to the crash report.
HANDLE hCur = GetCurrentProcess();
SymInitialize(hCur, NULL, TRUE);
Vector<DWORD64> callstack;
#ifdef _WIN64
ConstructStackTrace(hCur, IMAGE_FILE_MACHINE_AMD64, ExceptionInfo->ContextRecord, callstack);
#else
ConstructStackTrace(hCur, IMAGE_FILE_MACHINE_I386, ExceptionInfo->ContextRecord, callstack);
#endif
excMsg += "\r\nStack Trace:\r\n\r\n";
if (!callstack.GetCount())
{
excMsg += "Failed to obtain the stack trace for the exception!";
}
// Retrieve a locally loaded module list to trace back the exception through modules.
Index<Win32ModuleInformation> localMods;
ModuleManager::EnumerateModules(GetCurrentProcessId(), localMods, NULL, NULL);
// Iterate the obtained stack trace.
const int count = callstack.GetCount();
for (int i = 0; i < count; ++i)
{
const DWORD64& current = callstack[i];
const Win32ModuleInformation* mod = NULL;
if (mod = FindLocalModuleAddress(localMods, (SIZE_T)current))
{
// Retrieve the module name of the local module that caused the exception.
StringBuffer buffer(MAX_PATH);
GetModuleFileName((HMODULE)mod->BaseAddress, buffer.Begin(), MAX_PATH);
buffer.Strlen();
String modName = GetFileNamePos(buffer.Begin());
// Retrieve the name of a symbol that is related to the call stack entry.
char symbolName[MAX_PATH];
if (GetSingleSymbolName(hCur, (SIZE_T)current, symbolName, MAX_PATH))
{
excMsg += Format("%s!%s\r\n", modName, symbolName);
}
else
{
excMsg += Format("%s!%llX\r\n", modName, (LONG_PTR)current);
}
}
else
{
excMsg += Format("%llX\r\n", (LONG_PTR)current);
}
}
SymCleanup(hCur);
// Pop up crash report window.
if (Thread::IsMain())
{
CryCrashHandlerWindow* cchw = new CryCrashHandlerWindow(excMsg);
cchw->Execute();
delete cchw;
}
else
{
// It was not the main thread that caused the exception. Dispatch the handling of it to
// the main thread and wait for it to complete.
mCrySearchWindowManager->GetParentWindow()->ExecuteCrashHandlerWindow(excMsg);
}
// Let the exception run into the next exception handler.
return EXCEPTION_EXECUTE_HANDLER;
}