00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../../stdafx.h"
00013 #include "../../crashlog.h"
00014 #include "win32.h"
00015 #include "../../core/alloc_func.hpp"
00016 #include "../../core/math_func.hpp"
00017 #include "../../string_func.h"
00018 #include "../../fileio_func.h"
00019 #include "../../strings_func.h"
00020 #include "../../gamelog.h"
00021 #include "../../saveload/saveload.h"
00022
00023 #include <windows.h>
00024 #include <signal.h>
00025
00029 class CrashLogWindows : public CrashLog {
00031 EXCEPTION_POINTERS *ep;
00032
00033 char *LogOSVersion(char *buffer, const char *last) const;
00034 char *LogError(char *buffer, const char *last, const char *message) const;
00035 char *LogStacktrace(char *buffer, const char *last) const;
00036 char *LogRegisters(char *buffer, const char *last) const;
00037 char *LogModules(char *buffer, const char *last) const;
00038 public:
00039 #if defined(_MSC_VER)
00040 int WriteCrashDump(char *filename, const char *filename_last) const;
00041 #endif
00042
00044 char crashlog[65536];
00046 char crashlog_filename[MAX_PATH];
00048 char crashdump_filename[MAX_PATH];
00050 char screenshot_filename[MAX_PATH];
00051
00056 CrashLogWindows(EXCEPTION_POINTERS *ep = NULL) :
00057 ep(ep)
00058 {
00059 this->crashlog[0] = '\0';
00060 this->crashlog_filename[0] = '\0';
00061 this->crashdump_filename[0] = '\0';
00062 this->screenshot_filename[0] = '\0';
00063 }
00064
00068 static CrashLogWindows *current;
00069 };
00070
00071 CrashLogWindows *CrashLogWindows::current = NULL;
00072
00073 char *CrashLogWindows::LogOSVersion(char *buffer, const char *last) const
00074 {
00075 _OSVERSIONINFOA os;
00076 os.dwOSVersionInfoSize = sizeof(os);
00077 GetVersionExA(&os);
00078
00079 return buffer + seprintf(buffer, last,
00080 "Operating system:\n"
00081 " Name: Windows\n"
00082 " Release: %d.%d.%d (%s)\n",
00083 (int)os.dwMajorVersion,
00084 (int)os.dwMinorVersion,
00085 (int)os.dwBuildNumber,
00086 os.szCSDVersion
00087 );
00088
00089 }
00090
00091 char *CrashLogWindows::LogError(char *buffer, const char *last, const char *message) const
00092 {
00093 return buffer + seprintf(buffer, last,
00094 "Crash reason:\n"
00095 " Exception: %.8X\n"
00096 #ifdef _M_AMD64
00097 " Location: %.16IX\n"
00098 #else
00099 " Location: %.8X\n"
00100 #endif
00101 " Message: %s\n\n",
00102 (int)ep->ExceptionRecord->ExceptionCode,
00103 (size_t)ep->ExceptionRecord->ExceptionAddress,
00104 message == NULL ? "<none>" : message
00105 );
00106 }
00107
00108 struct DebugFileInfo {
00109 uint32 size;
00110 uint32 crc32;
00111 SYSTEMTIME file_time;
00112 };
00113
00114 static uint32 *_crc_table;
00115
00116 static void MakeCRCTable(uint32 *table)
00117 {
00118 uint32 crc, poly = 0xEDB88320L;
00119 int i;
00120 int j;
00121
00122 _crc_table = table;
00123
00124 for (i = 0; i != 256; i++) {
00125 crc = i;
00126 for (j = 8; j != 0; j--) {
00127 crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
00128 }
00129 table[i] = crc;
00130 }
00131 }
00132
00133 static uint32 CalcCRC(byte *data, uint size, uint32 crc)
00134 {
00135 for (; size > 0; size--) {
00136 crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF];
00137 }
00138 return crc;
00139 }
00140
00141 static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename)
00142 {
00143 HANDLE file;
00144 memset(dfi, 0, sizeof(*dfi));
00145
00146 file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
00147 if (file != INVALID_HANDLE_VALUE) {
00148 byte buffer[1024];
00149 DWORD numread;
00150 uint32 filesize = 0;
00151 FILETIME write_time;
00152 uint32 crc = (uint32)-1;
00153
00154 for (;;) {
00155 if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 || numread == 0)
00156 break;
00157 filesize += numread;
00158 crc = CalcCRC(buffer, numread, crc);
00159 }
00160 dfi->size = filesize;
00161 dfi->crc32 = crc ^ (uint32)-1;
00162
00163 if (GetFileTime(file, NULL, NULL, &write_time)) {
00164 FileTimeToSystemTime(&write_time, &dfi->file_time);
00165 }
00166 CloseHandle(file);
00167 }
00168 }
00169
00170
00171 static char *PrintModuleInfo(char *output, const char *last, HMODULE mod)
00172 {
00173 TCHAR buffer[MAX_PATH];
00174 DebugFileInfo dfi;
00175
00176 GetModuleFileName(mod, buffer, MAX_PATH);
00177 GetFileInfo(&dfi, buffer);
00178 output += seprintf(output, last, " %-20s handle: %p size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\n",
00179 WIDE_TO_MB(buffer),
00180 mod,
00181 dfi.size,
00182 dfi.crc32,
00183 dfi.file_time.wYear,
00184 dfi.file_time.wMonth,
00185 dfi.file_time.wDay,
00186 dfi.file_time.wHour,
00187 dfi.file_time.wMinute,
00188 dfi.file_time.wSecond
00189 );
00190 return output;
00191 }
00192
00193 char *CrashLogWindows::LogModules(char *output, const char *last) const
00194 {
00195 MakeCRCTable(AllocaM(uint32, 256));
00196 BOOL (WINAPI *EnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
00197
00198 output += seprintf(output, last, "Module information:\n");
00199
00200 if (LoadLibraryList((Function*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0\0")) {
00201 HMODULE modules[100];
00202 DWORD needed;
00203 BOOL res;
00204
00205 HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
00206 if (proc != NULL) {
00207 res = EnumProcessModules(proc, modules, sizeof(modules), &needed);
00208 CloseHandle(proc);
00209 if (res) {
00210 size_t count = min(needed / sizeof(HMODULE), lengthof(modules));
00211
00212 for (size_t i = 0; i != count; i++) output = PrintModuleInfo(output, last, modules[i]);
00213 return output + seprintf(output, last, "\n");
00214 }
00215 }
00216 }
00217 output = PrintModuleInfo(output, last, NULL);
00218 return output + seprintf(output, last, "\n");
00219 }
00220
00221 char *CrashLogWindows::LogRegisters(char *buffer, const char *last) const
00222 {
00223 buffer += seprintf(buffer, last, "Registers:\n");
00224 #ifdef _M_AMD64
00225 buffer += seprintf(buffer, last,
00226 " RAX: %.16llX RBX: %.16llX RCX: %.16llX RDX: %.16llX\n"
00227 " RSI: %.16llX RDI: %.16llX RBP: %.16llX RSP: %.16llX\n"
00228 " R8: %.16llX R9: %.16llX R10: %.16llX R11: %.16llX\n"
00229 " R12: %.16llX R13: %.16llX R14: %.16llX R15: %.16llX\n"
00230 " RIP: %.16llX EFLAGS: %.8X\n",
00231 ep->ContextRecord->Rax,
00232 ep->ContextRecord->Rbx,
00233 ep->ContextRecord->Rcx,
00234 ep->ContextRecord->Rdx,
00235 ep->ContextRecord->Rsi,
00236 ep->ContextRecord->Rdi,
00237 ep->ContextRecord->Rbp,
00238 ep->ContextRecord->Rsp,
00239 ep->ContextRecord->R8,
00240 ep->ContextRecord->R9,
00241 ep->ContextRecord->R10,
00242 ep->ContextRecord->R11,
00243 ep->ContextRecord->R12,
00244 ep->ContextRecord->R13,
00245 ep->ContextRecord->R14,
00246 ep->ContextRecord->R15,
00247 ep->ContextRecord->Rip,
00248 ep->ContextRecord->EFlags
00249 );
00250 #else
00251 buffer += seprintf(buffer, last,
00252 " EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\n"
00253 " ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\n"
00254 " EIP: %.8X EFLAGS: %.8X\n",
00255 (int)ep->ContextRecord->Eax,
00256 (int)ep->ContextRecord->Ebx,
00257 (int)ep->ContextRecord->Ecx,
00258 (int)ep->ContextRecord->Edx,
00259 (int)ep->ContextRecord->Esi,
00260 (int)ep->ContextRecord->Edi,
00261 (int)ep->ContextRecord->Ebp,
00262 (int)ep->ContextRecord->Esp,
00263 (int)ep->ContextRecord->Eip,
00264 (int)ep->ContextRecord->EFlags
00265 );
00266 #endif
00267
00268 buffer += seprintf(buffer, last, "\n Bytes at instruction pointer:\n");
00269 #ifdef _M_AMD64
00270 byte *b = (byte*)ep->ContextRecord->Rip;
00271 #else
00272 byte *b = (byte*)ep->ContextRecord->Eip;
00273 #endif
00274 for (int i = 0; i != 24; i++) {
00275 if (IsBadReadPtr(b, 1)) {
00276 buffer += seprintf(buffer, last, " ??");
00277 } else {
00278 buffer += seprintf(buffer, last, " %.2X", *b);
00279 }
00280 b++;
00281 }
00282 return buffer + seprintf(buffer, last, "\n\n");
00283 }
00284
00285 char *CrashLogWindows::LogStacktrace(char *buffer, const char *last) const
00286 {
00287 buffer += seprintf(buffer, last, "Stack trace:\n");
00288 #ifdef _M_AMD64
00289 uint32 *b = (uint32*)ep->ContextRecord->Rsp;
00290 #else
00291 uint32 *b = (uint32*)ep->ContextRecord->Esp;
00292 #endif
00293 for (int j = 0; j != 24; j++) {
00294 for (int i = 0; i != 8; i++) {
00295 if (IsBadReadPtr(b, sizeof(uint32))) {
00296 buffer += seprintf(buffer, last, " ????????");
00297 } else {
00298 buffer += seprintf(buffer, last, " %.8X", *b);
00299 }
00300 b++;
00301 }
00302 buffer += seprintf(buffer, last, "\n");
00303 }
00304 return buffer + seprintf(buffer, last, "\n");
00305 }
00306
00307 #if defined(_MSC_VER)
00308 #include <dbghelp.h>
00309
00310 int CrashLogWindows::WriteCrashDump(char *filename, const char *filename_last) const
00311 {
00312 int ret = 0;
00313 HMODULE dbghelp = LoadLibrary(_T("dbghelp.dll"));
00314 if (dbghelp != NULL) {
00315 typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE,
00316 MINIDUMP_TYPE,
00317 CONST PMINIDUMP_EXCEPTION_INFORMATION,
00318 CONST PMINIDUMP_USER_STREAM_INFORMATION,
00319 CONST PMINIDUMP_CALLBACK_INFORMATION);
00320 MiniDumpWriteDump_t funcMiniDumpWriteDump = (MiniDumpWriteDump_t)GetProcAddress(dbghelp, "MiniDumpWriteDump");
00321 if (funcMiniDumpWriteDump != NULL) {
00322 seprintf(filename, filename_last, "%scrash.dmp", _personal_dir);
00323 HANDLE file = CreateFile(OTTD2FS(filename), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
00324 HANDLE proc = GetCurrentProcess();
00325 DWORD procid = GetCurrentProcessId();
00326 MINIDUMP_EXCEPTION_INFORMATION mdei;
00327 MINIDUMP_USER_STREAM userstream;
00328 MINIDUMP_USER_STREAM_INFORMATION musi;
00329
00330 userstream.Type = LastReservedStream + 1;
00331 userstream.Buffer = (void*)this->crashlog;
00332 userstream.BufferSize = (ULONG)strlen(this->crashlog) + 1;
00333
00334 musi.UserStreamCount = 1;
00335 musi.UserStreamArray = &userstream;
00336
00337 mdei.ThreadId = GetCurrentThreadId();
00338 mdei.ExceptionPointers = ep;
00339 mdei.ClientPointers = false;
00340
00341 funcMiniDumpWriteDump(proc, procid, file, MiniDumpWithDataSegs, &mdei, &musi, NULL);
00342 ret = 1;
00343 } else {
00344 ret = -1;
00345 }
00346 FreeLibrary(dbghelp);
00347 }
00348 return ret;
00349 }
00350 #endif
00351
00352 extern bool CloseConsoleLogIfActive();
00353 static void ShowCrashlogWindow();
00354
00359 void *_safe_esp = NULL;
00360
00361 static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
00362 {
00363 if (CrashLogWindows::current != NULL) {
00364 CrashLog::AfterCrashLogCleanup();
00365 ExitProcess(2);
00366 }
00367
00368 if (GamelogTestEmergency()) {
00369 static const TCHAR _emergency_crash[] =
00370 _T("A serious fault condition occured in the game. The game will shut down.\n")
00371 _T("As you loaded an emergency savegame no crash information will be generated.\n");
00372 MessageBox(NULL, _emergency_crash, _T("Fatal Application Failure"), MB_ICONERROR);
00373 ExitProcess(3);
00374 }
00375
00376 if (SaveloadCrashWithMissingNewGRFs()) {
00377 static const TCHAR _saveload_crash[] =
00378 _T("A serious fault condition occured in the game. The game will shut down.\n")
00379 _T("As you loaded an savegame for which you do not have the required NewGRFs\n")
00380 _T("no crash information will be generated.\n");
00381 MessageBox(NULL, _saveload_crash, _T("Fatal Application Failure"), MB_ICONERROR);
00382 ExitProcess(3);
00383 }
00384
00385 CrashLogWindows *log = new CrashLogWindows(ep);
00386 CrashLogWindows::current = log;
00387 log->FillCrashLog(log->crashlog, lastof(log->crashlog));
00388 log->WriteCrashLog(log->crashlog, log->crashlog_filename, lastof(log->crashlog_filename));
00389 log->WriteCrashDump(log->crashdump_filename, lastof(log->crashdump_filename));
00390 log->WriteScreenshot(log->screenshot_filename, lastof(log->screenshot_filename));
00391
00392
00393 CloseConsoleLogIfActive();
00394
00395 if (_safe_esp) {
00396 #ifdef _M_AMD64
00397 ep->ContextRecord->Rip = (DWORD64)ShowCrashlogWindow;
00398 ep->ContextRecord->Rsp = (DWORD64)_safe_esp;
00399 #else
00400 ep->ContextRecord->Eip = (DWORD)ShowCrashlogWindow;
00401 ep->ContextRecord->Esp = (DWORD)_safe_esp;
00402 #endif
00403 return EXCEPTION_CONTINUE_EXECUTION;
00404 }
00405
00406 CrashLog::AfterCrashLogCleanup();
00407 return EXCEPTION_EXECUTE_HANDLER;
00408 }
00409
00410 #ifdef _M_AMD64
00411 extern "C" void *_get_safe_esp();
00412 #endif
00413
00414 static void CDECL CustomAbort(int signal)
00415 {
00416 RaiseException(0xE1212012, 0, 0, NULL);
00417 }
00418
00419 void CrashLog::InitialiseCrashLog()
00420 {
00421 #if defined(_MSC_VER)
00422 #ifdef _M_AMD64
00423 _safe_esp = _get_safe_esp();
00424 #else
00425 _asm {
00426 mov _safe_esp, esp
00427 }
00428 #endif
00429 #else
00430 asm("movl %esp, __safe_esp");
00431 #endif
00432
00433
00434 signal(SIGABRT, CustomAbort);
00435 #if defined(_MSC_VER)
00436
00437 _set_abort_behavior(0, _WRITE_ABORT_MSG);
00438 #endif
00439 SetUnhandledExceptionFilter(ExceptionHandler);
00440 }
00441
00442
00443
00444 static bool _expanded;
00445
00446 static const TCHAR _crash_desc[] =
00447 _T("A serious fault condition occured in the game. The game will shut down.\n")
00448 _T("Please send the crash information and the crash.dmp file (if any) to the developers.\n")
00449 _T("This will greatly help debugging. The correct place to do this is http:
00450 _T("The information contained in the report is displayed below.\n")
00451 _T("Press \"Emergency save\" to attempt saving the game. Generated file(s):\n")
00452 _T("%s");
00453
00454 static const TCHAR _save_succeeded[] =
00455 _T("Emergency save succeeded.\nIts location is '%s'.\n")
00456 _T("Be aware that critical parts of the internal game state may have become ")
00457 _T("corrupted. The saved game is not guaranteed to work.");
00458
00459 static const TCHAR * const _expand_texts[] = {_T("S&how report >>"), _T("&Hide report <<") };
00460
00461 static void SetWndSize(HWND wnd, int mode)
00462 {
00463 RECT r, r2;
00464
00465 GetWindowRect(wnd, &r);
00466 SetDlgItemText(wnd, 15, _expand_texts[mode == 1]);
00467
00468 if (mode >= 0) {
00469 GetWindowRect(GetDlgItem(wnd, 11), &r2);
00470 int offs = r2.bottom - r2.top + 10;
00471 if (!mode) offs = -offs;
00472 SetWindowPos(wnd, HWND_TOPMOST, 0, 0,
00473 r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER);
00474 } else {
00475 SetWindowPos(wnd, HWND_TOPMOST,
00476 (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2,
00477 (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2,
00478 0, 0, SWP_NOSIZE);
00479 }
00480 }
00481
00482 static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
00483 {
00484 switch (msg) {
00485 case WM_INITDIALOG: {
00486 #if defined(UNICODE)
00487
00488
00489 wchar_t crash_msgW[lengthof(CrashLogWindows::current->crashlog)];
00490 #endif
00491
00492 const char *unix_nl = CrashLogWindows::current->crashlog;
00493 char dos_nl[lengthof(CrashLogWindows::current->crashlog)];
00494 char *p = dos_nl;
00495 WChar c;
00496 while ((c = Utf8Consume(&unix_nl)) && p < lastof(dos_nl) - 4) {
00497 if (c == '\n') p += Utf8Encode(p, '\r');
00498 p += Utf8Encode(p, c);
00499 }
00500 *p = '\0';
00501
00502
00503 size_t len = _tcslen(_crash_desc) + 2;
00504 len += _tcslen(OTTD2FS(CrashLogWindows::current->crashlog_filename)) + 2;
00505 len += _tcslen(OTTD2FS(CrashLogWindows::current->crashdump_filename)) + 2;
00506 len += _tcslen(OTTD2FS(CrashLogWindows::current->screenshot_filename)) + 1;
00507
00508 TCHAR *text = AllocaM(TCHAR, len);
00509 _sntprintf(text, len, _crash_desc, OTTD2FS(CrashLogWindows::current->crashlog_filename));
00510 if (OTTD2FS(CrashLogWindows::current->crashdump_filename)[0] != _T('\0')) {
00511 _tcscat(text, _T("\n"));
00512 _tcscat(text, OTTD2FS(CrashLogWindows::current->crashdump_filename));
00513 }
00514 if (OTTD2FS(CrashLogWindows::current->screenshot_filename)[0] != _T('\0')) {
00515 _tcscat(text, _T("\n"));
00516 _tcscat(text, OTTD2FS(CrashLogWindows::current->screenshot_filename));
00517 }
00518
00519 SetDlgItemText(wnd, 10, text);
00520 SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(dos_nl, crash_msgW, lengthof(crash_msgW)));
00521 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
00522 SetWndSize(wnd, -1);
00523 } return TRUE;
00524 case WM_COMMAND:
00525 switch (wParam) {
00526 case 12:
00527 CrashLog::AfterCrashLogCleanup();
00528 ExitProcess(2);
00529 case 13:
00530 char filename[MAX_PATH];
00531 if (CrashLogWindows::current->WriteSavegame(filename, lastof(filename))) {
00532 size_t len = _tcslen(_save_succeeded) + _tcslen(OTTD2FS(filename)) + 1;
00533 TCHAR *text = AllocaM(TCHAR, len);
00534 _sntprintf(text, len, _save_succeeded, OTTD2FS(filename));
00535 MessageBox(wnd, text, _T("Save successful"), MB_ICONINFORMATION);
00536 } else {
00537 MessageBox(wnd, _T("Save failed"), _T("Save failed"), MB_ICONINFORMATION);
00538 }
00539 break;
00540 case 15:
00541 _expanded ^= 1;
00542 SetWndSize(wnd, _expanded);
00543 break;
00544 }
00545 return TRUE;
00546 case WM_CLOSE:
00547 CrashLog::AfterCrashLogCleanup();
00548 ExitProcess(2);
00549 }
00550
00551 return FALSE;
00552 }
00553
00554 static void ShowCrashlogWindow()
00555 {
00556 ShowCursor(TRUE);
00557 ShowWindow(GetActiveWindow(), FALSE);
00558 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(100), NULL, CrashDialogFunc);
00559 }