/
CCrashHandler.cpp
318 lines (263 loc) · 11.6 KB
/
CCrashHandler.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: core/CCrashHandler.cpp
* PURPOSE: Crash handler class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include "CCrashHandler.h"
#include "version.h"
#ifdef WIN32
#include "CExceptionInformation_Impl.h"
#else
#include <execinfo.h>
#endif
// clang-format off
#ifndef WIN32
#if __has_include(<ncursesw/curses.h>)
#include <ncursesw/curses.h>
#else
#include <ncurses.h>
#endif
extern "C" WINDOW* m_wndMenu;
extern "C" WINDOW* m_wndInput;
extern "C" bool g_bNoCurses;
extern bool g_bSilent;
#ifdef __APPLE__
#include <client/mac/handler/exception_handler.h>
bool DumpCallback(const char* dump_dir, const char* minidump_id, void* context, bool succeeded);
#else
#include <client/linux/handler/exception_handler.h>
bool DumpCallback(const google_breakpad::MinidumpDescriptor& descriptor, void* context, bool succeeded);
#endif
static SString ms_strDumpPathFilename;
#endif
// clang-format on
static SString ms_strDumpPath;
#ifdef WIN32
#include <ctime>
#include <tchar.h>
#include <dbghelp.h>
typedef BOOL(WINAPI* MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFile, MINIDUMP_TYPE DumpType,
CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
#endif
void CCrashHandler::Init(const SString& strInServerPath)
{
SString strServerPath = strInServerPath;
if (strServerPath == "")
strServerPath = GetSystemCurrentDirectory();
ms_strDumpPath = PathJoin(strServerPath, SERVER_DUMP_PATH);
// Set a global filter
#ifdef WIN32
SetCrashHandlerFilter(HandleExceptionGlobal);
#else
// Prepare initial dumpfile name
time_t pTime = time(NULL);
struct tm* tm = localtime(&pTime);
SString strFilename("server_%s_%04d%02d%02d_%02d%02d.dmp", MTA_DM_BUILDTAG_LONG, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
ms_strDumpPathFilename = PathJoin(ms_strDumpPath, strFilename);
MakeSureDirExists(ms_strDumpPathFilename);
#ifdef WITH_BACKTRACE_ONLY
signal(SIGSEGV, HandleExceptionGlobal);
#else
#ifdef __APPLE__
static google_breakpad::ExceptionHandler eh(ms_strDumpPath, NULL, DumpCallback, NULL, true, NULL);
#else
google_breakpad::MinidumpDescriptor descriptor(ms_strDumpPath);
static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1);
#endif
#endif
#endif
}
#ifndef WIN32
// Save basic backtrace info into a file. Forced inline to avoid backtrace pollution
inline __attribute__((always_inline)) static void SaveBacktraceSummary()
{
// Collect backtrace information
void* buffer[100];
int iAmount = backtrace(buffer, sizeof buffer);
iAmount = std::min<int>(iAmount, NUMELMS(buffer));
char** symbols = backtrace_symbols(buffer, iAmount);
// Generate a .log file
time_t pTime = time(NULL);
struct tm* tm = localtime(&pTime);
SString sFileName;
sFileName.Format("server_%s_%04d%02d%02d_%02d%02d.log", MTA_DM_BUILDTYPE, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
SString sContent;
sContent += SString("MTA:SA Server v%s-r%d-%s crash report.\n", MTA_DM_VERSIONSTRING, MTASA_VERSION_BUILD, MTA_DM_BUILDTYPE);
sContent += SString("%04d-%02d-%02d %02d:%02d\n", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
sContent += SString("Caught %d addresses ...\n\n", iAmount);
sContent += "Backtrace:\n";
for (int i = 0; i < iAmount; i++)
{
if (symbols[i])
{
sContent += SString("#%d - %s\n", i, symbols[i]);
}
}
sContent += std::string(80, '-') + "\n";
// Write the content to the file and close
MakeSureDirExists(PathJoin(ms_strDumpPath, sFileName));
FileAppend(PathJoin(ms_strDumpPath, sFileName), sContent);
FileAppend(PathJoin(ms_strDumpPath, "server_pending_upload.log"), sContent);
free(symbols);
// Try to close window gracefully
if (!g_bSilent && !g_bNoCurses && m_wndInput)
{
if (m_wndMenu)
{
delwin(m_wndMenu);
m_wndMenu = NULL;
}
delwin(m_wndInput);
m_wndInput = NULL;
endwin();
}
}
// Linux/Mac crash callback when using google-breakpad
#ifdef __APPLE__
bool DumpCallback(const char* dump_dir, const char* minidump_id, void* context, bool succeeded)
{
auto path = PathJoin(dump_dir, SString("%s.dmp", minidump_id));
#else
bool DumpCallback(const google_breakpad::MinidumpDescriptor& descriptor, void* context, bool succeeded)
{
auto path = descriptor.path();
#endif
// Set inital dump file name (Safeish)
File::Rename(path, ms_strDumpPathFilename);
// Set final dump file name (Not so safe)
time_t pTime = time(NULL);
struct tm* tm = localtime(&pTime);
SString strFilename("server_%s_%04d%02d%02d_%02d%02d.dmp", MTA_DM_BUILDTAG_LONG, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
SString strFinalDumpPathFilename = PathJoin(ms_strDumpPath, strFilename);
File::Rename(ms_strDumpPathFilename, strFinalDumpPathFilename);
SaveBacktraceSummary();
FileSave(PathJoin(ms_strDumpPath, "server_pending_upload_filename"), strFinalDumpPathFilename);
// Return false to indicate exception has not been handled (and allow core dump?)
return false;
}
void CCrashHandler::HandleExceptionGlobal(int iSig)
{
SaveBacktraceSummary();
exit(EXIT_FAILURE);
}
#else
long WINAPI CCrashHandler::HandleExceptionGlobal(_EXCEPTION_POINTERS* pException)
{
// Create the exception information class
CExceptionInformation_Impl* pExceptionInformation = new CExceptionInformation_Impl;
pExceptionInformation->Set(pException->ExceptionRecord->ExceptionCode, pException);
// Write the dump
DumpMiniDump(pException, pExceptionInformation);
RunErrorTool();
TerminateProcess(GetCurrentProcess(), 1);
return EXCEPTION_CONTINUE_SEARCH;
}
void CCrashHandler::DumpMiniDump(_EXCEPTION_POINTERS* pException, CExceptionInformation* pExceptionInformation)
{
// Try to load the DLL in our directory
HMODULE hDll = NULL;
char szDbgHelpPath[MAX_PATH];
if (GetModuleFileNameA(NULL, szDbgHelpPath, MAX_PATH))
{
char* pSlash = _tcsrchr(szDbgHelpPath, '\\');
if (pSlash)
{
_tcscpy(pSlash + 1, "DBGHELP.DLL");
hDll = LoadLibrary(szDbgHelpPath);
}
}
// If we couldn't load the one in our dir, load any version available
if (!hDll)
{
hDll = LoadLibrary("DBGHELP.DLL");
}
// We could load a dll?
if (hDll)
{
// Grab the MiniDumpWriteDump proc address
MINIDUMPWRITEDUMP pDump = reinterpret_cast<MINIDUMPWRITEDUMP>(GetProcAddress(hDll, "MiniDumpWriteDump"));
if (pDump)
{
// Grab the current time
// Ask windows for the system time.
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
// Create the dump directory
CreateDirectory(ms_strDumpPath, 0);
CreateDirectory(PathJoin(ms_strDumpPath, "private"), 0);
SString strModuleName = pExceptionInformation->GetModuleBaseName();
strModuleName = strModuleName.ReplaceI(".dll", "").Replace(".exe", "").Replace("_", "").Replace(".", "").Replace("-", "");
if (strModuleName.length() == 0)
strModuleName = "unknown";
#ifdef _WIN64
strModuleName += "64";
#endif
SString strFilename("server_%s_%s_%08x_%x_%04d%02d%02d_%02d%02d.dmp", MTA_DM_BUILDTAG_LONG, strModuleName.c_str(),
pExceptionInformation->GetAddressModuleOffset(), pExceptionInformation->GetCode() & 0xffff, SystemTime.wYear, SystemTime.wMonth,
SystemTime.wDay, SystemTime.wHour, SystemTime.wMinute);
SString strFinalDumpPathFilename = PathJoin(ms_strDumpPath, "private", strFilename);
// Create the file
HANDLE hFile = CreateFile(strFinalDumpPathFilename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// Create an exception information struct
_MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = GetCurrentThreadId();
ExInfo.ExceptionPointers = pException;
ExInfo.ClientPointers = FALSE;
// Write the dump
pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, (MINIDUMP_TYPE)(MiniDumpNormal | MiniDumpWithIndirectlyReferencedMemory), &ExInfo,
NULL, NULL);
// Close the dumpfile
CloseHandle(hFile);
FileSave(PathJoin(ms_strDumpPath, "server_pending_upload_filename"), strFinalDumpPathFilename);
}
// Write a log with the generic exception information
FILE* pFile = File::Fopen(PathJoin(ms_strDumpPath, "server_pending_upload.log"), "a+");
if (pFile)
{
// Header
fprintf(pFile, "%s", "** -- Unhandled exception -- **\n\n");
// Write the time
time_t timeTemp;
time(&timeTemp);
SString strMTAVersionFull = SString("%s", MTA_DM_BUILDTAG_LONG);
SString strInfo;
strInfo += SString("Version = %s\n", strMTAVersionFull.c_str());
strInfo += SString("Time = %s", ctime(&timeTemp));
strInfo += SString("Module = %s\n", pExceptionInformation->GetModulePathName());
// Write the basic exception information
strInfo += SString("Code = 0x%08X\n", pExceptionInformation->GetCode());
strInfo += SString("Offset = 0x%08X\n\n", pExceptionInformation->GetAddressModuleOffset());
// Write the register info
strInfo += SString(
"EAX=%08X EBX=%08X ECX=%08X EDX=%08X ESI=%08X\n"
"EDI=%08X EBP=%08X ESP=%08X EIP=%08X FLG=%08X\n"
"CS=%04X DS=%04X SS=%04X ES=%04X "
"FS=%04X GS=%04X\n\n",
pExceptionInformation->GetEAX(), pExceptionInformation->GetEBX(), pExceptionInformation->GetECX(), pExceptionInformation->GetEDX(),
pExceptionInformation->GetESI(), pExceptionInformation->GetEDI(), pExceptionInformation->GetEBP(), pExceptionInformation->GetESP(),
pExceptionInformation->GetEIP(), pExceptionInformation->GetEFlags(), pExceptionInformation->GetCS(), pExceptionInformation->GetDS(),
pExceptionInformation->GetSS(), pExceptionInformation->GetES(), pExceptionInformation->GetFS(), pExceptionInformation->GetGS());
fprintf(pFile, "%s", strInfo.c_str());
// End of unhandled exception
fprintf(pFile, "%s", "** -- End of unhandled exception -- **\n\n\n");
// Close the file
fclose(pFile);
}
}
// Free the DLL again
FreeLibrary(hDll);
}
}
void CCrashHandler::RunErrorTool()
{
}
#endif