# crashlog_win.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #if defined(_MSC_VER) && defined(_M_AMD64)
    00014 /* Redefine WinNT version to get RtlCaptureContext prototype. */
    00015 #undef _WIN32_WINNT
    00016 #undef NTDDI_VERSION
    00017 #define _WIN32_WINNT _WIN32_WINNT_WINXP
    00018 #endif /* defined(_MSC_VER) && defined(_M_AMD64) */
    00019 #include "../../crashlog.h"
    00020 #include "win32.h"
    00021 #include "../../core/alloc_func.hpp"
    00022 #include "../../core/math_func.hpp"
    00023 #include "../../string_func.h"
    00024 #include "../../fileio_func.h"
    00025 #include "../../strings_func.h"
    00026 #include "../../gamelog.h"
    00027 #include "../../saveload/saveload.h"
    00028
    00029 #include <windows.h>
    00030 #include <signal.h>
    00031
    00032 static const uint MAX_SYMBOL_LEN = 512;
    00033 static const uint MAX_FRAMES     = 64;
    00034
    00035 /* printf format specification for 32/64-bit addresses. */
    00036 #ifdef _M_AMD64
    00037 #define PRINTF_PTR "0x%016IX"
    00038 #else
    00039 #define PRINTF_PTR "0x%08X"
    00040 #endif
    00041
    00045 class CrashLogWindows : public CrashLog {
    00047   EXCEPTION_POINTERS *ep;
    00048
    00049   /* virtual */ char *LogOSVersion(char *buffer, const char *last) const;
    00050   /* virtual */ char *LogError(char *buffer, const char *last, const char *message) const;
    00051   /* virtual */ char *LogStacktrace(char *buffer, const char *last) const;
    00052   /* virtual */ char *LogRegisters(char *buffer, const char *last) const;
    00053   /* virtual */ char *LogModules(char *buffer, const char *last) const;
    00054 public:
    00055 #if defined(_MSC_VER)
    00056   /* virtual */ int WriteCrashDump(char *filename, const char *filename_last) const;
    00057   char *AppendDecodedStacktrace(char *buffer, const char *last) const;
    00058 #else
    00059   char *AppendDecodedStacktrace(char *buffer, const char *last) const { return buffer; }
    00060 #endif /* _MSC_VER */
    00061
    00063   char crashlog[65536];
    00065   char crashlog_filename[MAX_PATH];
    00067   char crashdump_filename[MAX_PATH];
    00069   char screenshot_filename[MAX_PATH];
    00070
    00075   CrashLogWindows(EXCEPTION_POINTERS *ep = NULL) :
    00076     ep(ep)
    00077   {
    00078     this->crashlog[0] = '\0';
    00079     this->crashlog_filename[0] = '\0';
    00080     this->crashdump_filename[0] = '\0';
    00081     this->screenshot_filename[0] = '\0';
    00082   }
    00083
    00087   static CrashLogWindows *current;
    00088 };
    00089
    00090 /* static */ CrashLogWindows *CrashLogWindows::current = NULL;
    00091
    00092 /* virtual */ char *CrashLogWindows::LogOSVersion(char *buffer, const char *last) const
    00093 {
    00094   _OSVERSIONINFOA os;
    00095   os.dwOSVersionInfoSize = sizeof(os);
    00096   GetVersionExA(&os);
    00097
    00098   return buffer + seprintf(buffer, last,
    00099       "Operating system:\n"
    00100       " Name:     Windows\n"
    00101       " Release:  %d.%d.%d (%s)\n",
    00102       (int)os.dwMajorVersion,
    00103       (int)os.dwMinorVersion,
    00104       (int)os.dwBuildNumber,
    00105       os.szCSDVersion
    00106   );
    00107
    00108 }
    00109
    00110 /* virtual */ char *CrashLogWindows::LogError(char *buffer, const char *last, const char *message) const
    00111 {
    00112   return buffer + seprintf(buffer, last,
    00113       "Crash reason:\n"
    00114       " Exception: %.8X\n"
    00115 #ifdef _M_AMD64
    00116       " Location:  %.16IX\n"
    00117 #else
    00118       " Location:  %.8X\n"
    00119 #endif
    00120       " Message:   %s\n\n",
    00121       (int)ep->ExceptionRecord->ExceptionCode,
    00122       (size_t)ep->ExceptionRecord->ExceptionAddress,
    00123       message == NULL ? "<none>" : message
    00124   );
    00125 }
    00126
    00127 struct DebugFileInfo {
    00128   uint32 size;
    00129   uint32 crc32;
    00130   SYSTEMTIME file_time;
    00131 };
    00132
    00133 static uint32 *_crc_table;
    00134
    00135 static void MakeCRCTable(uint32 *table)
    00136 {
    00137   uint32 crc, poly = 0xEDB88320L;
    00138   int i;
    00139   int j;
    00140
    00141   _crc_table = table;
    00142
    00143   for (i = 0; i != 256; i++) {
    00144     crc = i;
    00145     for (j = 8; j != 0; j--) {
    00146       crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
    00147     }
    00148     table[i] = crc;
    00149   }
    00150 }
    00151
    00152 static uint32 CalcCRC(byte *data, uint size, uint32 crc)
    00153 {
    00154   for (; size > 0; size--) {
    00155     crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF];
    00156   }
    00157   return crc;
    00158 }
    00159
    00160 static void GetFileInfo(DebugFileInfo *dfi, const TCHAR *filename)
    00161 {
    00162   HANDLE file;
    00163   memset(dfi, 0, sizeof(*dfi));
    00164
    00165   file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
    00166   if (file != INVALID_HANDLE_VALUE) {
    00167     byte buffer[1024];
    00168     DWORD numread;
    00169     uint32 filesize = 0;
    00170     FILETIME write_time;
    00171     uint32 crc = (uint32)-1;
    00172
    00173     for (;;) {
    00174       if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 || numread == 0) {
    00175         break;
    00176       }
    00177       filesize += numread;
    00178       crc = CalcCRC(buffer, numread, crc);
    00179     }
    00180     dfi->size = filesize;
    00181     dfi->crc32 = crc ^ (uint32)-1;
    00182
    00183     if (GetFileTime(file, NULL, NULL, &write_time)) {
    00184       FileTimeToSystemTime(&write_time, &dfi->file_time);
    00185     }
    00186     CloseHandle(file);
    00187   }
    00188 }
    00189
    00190
    00191 static char *PrintModuleInfo(char *output, const char *last, HMODULE mod)
    00192 {
    00193   TCHAR buffer[MAX_PATH];
    00194   DebugFileInfo dfi;
    00195
    00196   GetModuleFileName(mod, buffer, MAX_PATH);
    00197   GetFileInfo(&dfi, buffer);
    00198   output += seprintf(output, last, " %-20s handle: %p size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\n",
    00199     WIDE_TO_MB(buffer),
    00200     mod,
    00201     dfi.size,
    00202     dfi.crc32,
    00203     dfi.file_time.wYear,
    00204     dfi.file_time.wMonth,
    00205     dfi.file_time.wDay,
    00206     dfi.file_time.wHour,
    00207     dfi.file_time.wMinute,
    00208     dfi.file_time.wSecond
    00209   );
    00210   return output;
    00211 }
    00212
    00213 /* virtual */ char *CrashLogWindows::LogModules(char *output, const char *last) const
    00214 {
    00215   MakeCRCTable(AllocaM(uint32, 256));
    00216   BOOL (WINAPI *EnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
    00217
    00218   output += seprintf(output, last, "Module information:\n");
    00219
    00220   if (LoadLibraryList((Function*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0\0")) {
    00221     HMODULE modules[100];
    00222     DWORD needed;
    00223     BOOL res;
    00224
    00225     HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
    00226     if (proc != NULL) {
    00227       res = EnumProcessModules(proc, modules, sizeof(modules), &needed);
    00228       CloseHandle(proc);
    00229       if (res) {
    00230         size_t count = min(needed / sizeof(HMODULE), lengthof(modules));
    00231
    00232         for (size_t i = 0; i != count; i++) output = PrintModuleInfo(output, last, modules[i]);
    00233         return output + seprintf(output, last, "\n");
    00234       }
    00235     }
    00236   }
    00237   output = PrintModuleInfo(output, last, NULL);
    00238   return output + seprintf(output, last, "\n");
    00239 }
    00240
    00241 /* virtual */ char *CrashLogWindows::LogRegisters(char *buffer, const char *last) const
    00242 {
    00243   buffer += seprintf(buffer, last, "Registers:\n");
    00244 #ifdef _M_AMD64
    00245   buffer += seprintf(buffer, last,
    00246     " RAX: %.16llX RBX: %.16llX RCX: %.16llX RDX: %.16llX\n"
    00247     " RSI: %.16llX RDI: %.16llX RBP: %.16llX RSP: %.16llX\n"
    00248     " R8:  %.16llX R9:  %.16llX R10: %.16llX R11: %.16llX\n"
    00249     " R12: %.16llX R13: %.16llX R14: %.16llX R15: %.16llX\n"
    00250     " RIP: %.16llX EFLAGS: %.8X\n",
    00251     ep->ContextRecord->Rax,
    00252     ep->ContextRecord->Rbx,
    00253     ep->ContextRecord->Rcx,
    00254     ep->ContextRecord->Rdx,
    00255     ep->ContextRecord->Rsi,
    00256     ep->ContextRecord->Rdi,
    00257     ep->ContextRecord->Rbp,
    00258     ep->ContextRecord->Rsp,
    00259     ep->ContextRecord->R8,
    00260     ep->ContextRecord->R9,
    00261     ep->ContextRecord->R10,
    00262     ep->ContextRecord->R11,
    00263     ep->ContextRecord->R12,
    00264     ep->ContextRecord->R13,
    00265     ep->ContextRecord->R14,
    00266     ep->ContextRecord->R15,
    00267     ep->ContextRecord->Rip,
    00268     ep->ContextRecord->EFlags
    00269   );
    00270 #else
    00271   buffer += seprintf(buffer, last,
    00272     " EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\n"
    00273     " ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\n"
    00274     " EIP: %.8X EFLAGS: %.8X\n",
    00275     (int)ep->ContextRecord->Eax,
    00276     (int)ep->ContextRecord->Ebx,
    00277     (int)ep->ContextRecord->Ecx,
    00278     (int)ep->ContextRecord->Edx,
    00279     (int)ep->ContextRecord->Esi,
    00280     (int)ep->ContextRecord->Edi,
    00281     (int)ep->ContextRecord->Ebp,
    00282     (int)ep->ContextRecord->Esp,
    00283     (int)ep->ContextRecord->Eip,
    00284     (int)ep->ContextRecord->EFlags
    00285   );
    00286 #endif
    00287
    00288   buffer += seprintf(buffer, last, "\n Bytes at instruction pointer:\n");
    00289 #ifdef _M_AMD64
    00290   byte *b = (byte*)ep->ContextRecord->Rip;
    00291 #else
    00292   byte *b = (byte*)ep->ContextRecord->Eip;
    00293 #endif
    00294   for (int i = 0; i != 24; i++) {
    00295     if (IsBadReadPtr(b, 1)) {
    00296       buffer += seprintf(buffer, last, " ??"); // OCR: WAS: , 0);
    00297     } else {
    00298       buffer += seprintf(buffer, last, " %.2X", *b);
    00299     }
    00300     b++;
    00301   }
    00302   return buffer + seprintf(buffer, last, "\n\n");
    00303 }
    00304
    00305 /* virtual */ char *CrashLogWindows::LogStacktrace(char *buffer, const char *last) const
    00306 {
    00307   buffer += seprintf(buffer, last, "Stack trace:\n");
    00308 #ifdef _M_AMD64
    00309   uint32 *b = (uint32*)ep->ContextRecord->Rsp;
    00310 #else
    00311   uint32 *b = (uint32*)ep->ContextRecord->Esp;
    00312 #endif
    00313   for (int j = 0; j != 24; j++) {
    00314     for (int i = 0; i != 8; i++) {
    00315       if (IsBadReadPtr(b, sizeof(uint32))) {
    00316         buffer += seprintf(buffer, last, " ????????"); // OCR: WAS - , 0);
    00317       } else {
    00318         buffer += seprintf(buffer, last, " %.8X", *b);
    00319       }
    00320       b++;
    00321     }
    00322     buffer += seprintf(buffer, last, "\n");
    00323   }
    00324   return buffer + seprintf(buffer, last, "\n");
    00325 }
    00326
    00327 #if defined(_MSC_VER)
    00328 #include <dbghelp.h>
    00329
    00330 char *CrashLogWindows::AppendDecodedStacktrace(char *buffer, const char *last) const
    00331 {
    00332 #define M(x) x "\0"
    00333   static const char dbg_import[] =
    00334     M("dbghelp.dll")
    00335     M("SymInitialize")
    00336     M("SymSetOptions")
    00337     M("SymCleanup")
    00338     M("StackWalk64")
    00339     M("SymFunctionTableAccess64")
    00340     M("SymGetModuleBase64")
    00341     M("SymGetModuleInfo64")
    00342     M("SymGetSymFromAddr64")
    00343     M("SymGetLineFromAddr64")
    00344     M("")
    00345     ;
    00346 #undef M
    00347
    00348   struct ProcPtrs {
    00349     BOOL (WINAPI * pSymInitialize)(HANDLE, PCSTR, BOOL);
    00350     BOOL (WINAPI * pSymSetOptions)(DWORD);
    00351     BOOL (WINAPI * pSymCleanup)(HANDLE);
    00352     BOOL (WINAPI * pStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, PVOID, PREAD_PROCESS_MEMORY_ROUTINE64, PFUNCTION_TABLE_ACCESS_ROUTINE64, PGET_MODULE_BASE_ROUTINE64, PTRANSLATE_ADDRESS_ROUTINE64);
    00353     PVOID (WINAPI * pSymFunctionTableAccess64)(HANDLE, DWORD64);
    00354     DWORD64 (WINAPI * pSymGetModuleBase64)(HANDLE, DWORD64);
    00355     BOOL (WINAPI * pSymGetModuleInfo64)(HANDLE, DWORD64, PIMAGEHLP_MODULE64);
    00356     BOOL (WINAPI * pSymGetSymFromAddr64)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64);
    00357     BOOL (WINAPI * pSymGetLineFromAddr64)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64);
    00358   } proc;
    00359
    00360   buffer += seprintf(buffer, last, "\nDecoded stack trace:\n");
    00361
    00362   /* Try to load the functions from the DLL, if that fails because of a too old dbghelp.dll, just skip it. */
    00363   if (LoadLibraryList((Function*)&proc, dbg_import)) {
    00364     /* Initialize symbol handler. */
    00365     HANDLE hCur = GetCurrentProcess();
    00366     proc.pSymInitialize(hCur, NULL, TRUE);
    00367     /* Load symbols only when needed, fail silently on errors, demangle symbol names. */
    00368     proc.pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_UNDNAME);
    00369
    00370     /* Initialize starting stack frame from context record. */
    00371     STACKFRAME64 frame;
    00372     memset(&frame, 0, sizeof(frame));
    00373 #ifdef _M_AMD64
    00374     frame.AddrPC.Offset = ep->ContextRecord->Rip;
    00375     frame.AddrFrame.Offset = ep->ContextRecord->Rbp;
    00376     frame.AddrStack.Offset = ep->ContextRecord->Rsp;
    00377 #else
    00378     frame.AddrPC.Offset = ep->ContextRecord->Eip;
    00379     frame.AddrFrame.Offset = ep->ContextRecord->Ebp;
    00380     frame.AddrStack.Offset = ep->ContextRecord->Esp;
    00381 #endif
    00382     frame.AddrPC.Mode = AddrModeFlat;
    00383     frame.AddrFrame.Mode = AddrModeFlat;
    00384     frame.AddrStack.Mode = AddrModeFlat;
    00385
    00386     /* Copy context record as StackWalk64 may modify it. */
    00387     CONTEXT ctx;
    00388     memcpy(&ctx, ep->ContextRecord, sizeof(ctx));
    00389
    00390     /* Allocate space for symbol info. */
    00391     IMAGEHLP_SYMBOL64 *sym_info = (IMAGEHLP_SYMBOL64*)alloca(sizeof(IMAGEHLP_SYMBOL64) + MAX_SYMBOL_LEN - 1);
    00392     sym_info->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
    00393     sym_info->MaxNameLength = MAX_SYMBOL_LEN;
    00394
    00395     /* Walk stack at most MAX_FRAMES deep in case the stack is corrupt. */
    00396     for (uint num = 0; num < MAX_FRAMES; num++) {
    00397       if (!proc.pStackWalk64(
    00398 #ifdef _M_AMD64
    00399         IMAGE_FILE_MACHINE_AMD64,
    00400 #else
    00401         IMAGE_FILE_MACHINE_I386,
    00402 #endif
    00403         hCur, GetCurrentThread(), &frame, &ctx, NULL, proc.pSymFunctionTableAccess64, proc.pSymGetModuleBase64, NULL)) break;
    00404
    00405       if (frame.AddrPC.Offset == frame.AddrReturn.Offset) {
    00406         buffer += seprintf(buffer, last, " <infinite loop>\n");
    00407         break;
    00408       }
    00409
    00410       /* Get module name. */
    00411       const char *mod_name = "???";
    00412
    00413       IMAGEHLP_MODULE64 module;
    00414       module.SizeOfStruct = sizeof(module);
    00415       if (proc.pSymGetModuleInfo64(hCur, frame.AddrPC.Offset, &module)) {
    00416         mod_name = module.ModuleName;
    00417       }
    00418
    00419       /* Print module and instruction pointer. */
    00420       buffer += seprintf(buffer, last, "[%02d] %-20s " PRINTF_PTR, num, mod_name, frame.AddrPC.Offset);
    00421
    00422       /* Get symbol name and line info if possible. */
    00423       DWORD64 offset;
    00424       if (proc.pSymGetSymFromAddr64(hCur, frame.AddrPC.Offset, &offset, sym_info)) {
    00425         buffer += seprintf(buffer, last, " %s + %I64u", sym_info->Name, offset);
    00426
    00427         DWORD line_offs;
    00428         IMAGEHLP_LINE64 line;
    00429         line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
    00430         if (proc.pSymGetLineFromAddr64(hCur, frame.AddrPC.Offset, &line_offs, &line)) {
    00431           buffer += seprintf(buffer, last, " (%s:%d)", line.FileName, line.LineNumber);
    00432         }
    00433       }
    00434       buffer += seprintf(buffer, last, "\n");
    00435     }
    00436
    00437     proc.pSymCleanup(hCur);
    00438   }
    00439
    00440   return buffer + seprintf(buffer, last, "\n*** End of additional info ***\n");
    00441 }
    00442
    00443 /* virtual */ int CrashLogWindows::WriteCrashDump(char *filename, const char *filename_last) const
    00444 {
    00445   int ret = 0;
    00446   HMODULE dbghelp = LoadLibrary(_T("dbghelp.dll"));
    00447   if (dbghelp != NULL) {
    00448     typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE,
    00449         MINIDUMP_TYPE,
    00450         CONST PMINIDUMP_EXCEPTION_INFORMATION,
    00451         CONST PMINIDUMP_USER_STREAM_INFORMATION,
    00452         CONST PMINIDUMP_CALLBACK_INFORMATION);
    00453     MiniDumpWriteDump_t funcMiniDumpWriteDump = (MiniDumpWriteDump_t)GetProcAddress(dbghelp, "MiniDumpWriteDump");
    00454     if (funcMiniDumpWriteDump != NULL) {
    00455       seprintf(filename, filename_last, "%scrash.dmp", _personal_dir);
    00456       HANDLE file  = CreateFile(OTTD2FS(filename), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
    00457       HANDLE proc  = GetCurrentProcess();
    00458       DWORD procid = GetCurrentProcessId();
    00459       MINIDUMP_EXCEPTION_INFORMATION mdei;
    00460       MINIDUMP_USER_STREAM userstream;
    00461       MINIDUMP_USER_STREAM_INFORMATION musi;
    00462
    00463       userstream.Type        = LastReservedStream + 1;
    00464       userstream.Buffer      = (void*)this->crashlog;
    00465       userstream.BufferSize  = (ULONG)strlen(this->crashlog) + 1;
    00466
    00467       musi.UserStreamCount   = 1;
    00468       musi.UserStreamArray   = &userstream;
    00469
    00470       mdei.ThreadId = GetCurrentThreadId();
    00471       mdei.ExceptionPointers  = ep;
    00472       mdei.ClientPointers     = false;
    00473
    00474       funcMiniDumpWriteDump(proc, procid, file, MiniDumpWithDataSegs, &mdei, &musi, NULL);
    00475       ret = 1;
    00476     } else {
    00477       ret = -1;
    00478     }
    00479     FreeLibrary(dbghelp);
    00480   }
    00481   return ret;
    00482 }
    00483 #endif /* _MSC_VER */
    00484
    00485 extern bool CloseConsoleLogIfActive();
    00486 static void ShowCrashlogWindow();
    00487
    00492 void *_safe_esp = NULL;
    00493
    00494 static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
    00495 {
    00496   if (CrashLogWindows::current != NULL) {
    00497     CrashLog::AfterCrashLogCleanup();
    00498     ExitProcess(2);
    00499   }
    00500
    00501   if (GamelogTestEmergency()) {
    00502     static const TCHAR _emergency_crash[] =
    00503       _T("A serious fault condition occurred in the game. The game will shut down.\n")
    00504       _T("As you loaded an emergency savegame no crash information will be generated.\n");
    00505     MessageBox(NULL, _emergency_crash, _T("Fatal Application Failure"), MB_ICONERROR);
    00506     ExitProcess(3);
    00507   }
    00508
    00509   if (SaveloadCrashWithMissingNewGRFs()) {
    00510     static const TCHAR _saveload_crash[] =
    00511       _T("A serious fault condition occurred in the game. The game will shut down.\n")
    00512       _T("As you loaded an savegame for which you do not have the required NewGRFs\n")
    00513       _T("no crash information will be generated.\n");
    00514     MessageBox(NULL, _saveload_crash, _T("Fatal Application Failure"), MB_ICONERROR);
    00515     ExitProcess(3);
    00516   }
    00517
    00518   CrashLogWindows *log = new CrashLogWindows(ep);
    00519   CrashLogWindows::current = log;
    00520   char *buf = log->FillCrashLog(log->crashlog, lastof(log->crashlog));
    00521   log->WriteCrashDump(log->crashdump_filename, lastof(log->crashdump_filename));
    00522   log->AppendDecodedStacktrace(buf, lastof(log->crashlog));
    00523   log->WriteCrashLog(log->crashlog, log->crashlog_filename, lastof(log->crashlog_filename));
    00524   log->WriteScreenshot(log->screenshot_filename, lastof(log->screenshot_filename));
    00525
    00526   /* Close any possible log files */
    00527   CloseConsoleLogIfActive();
    00528
    00529   if (_safe_esp) {
    00530 #ifdef _M_AMD64
    00531     ep->ContextRecord->Rip = (DWORD64)ShowCrashlogWindow;
    00532     ep->ContextRecord->Rsp = (DWORD64)_safe_esp;
    00533 #else
    00534     ep->ContextRecord->Eip = (DWORD)ShowCrashlogWindow;
    00535     ep->ContextRecord->Esp = (DWORD)_safe_esp;
    00536 #endif
    00537     return EXCEPTION_CONTINUE_EXECUTION;
    00538   }
    00539
    00540   CrashLog::AfterCrashLogCleanup();
    00541   return EXCEPTION_EXECUTE_HANDLER;
    00542 }
    00543
    00544 static void CDECL CustomAbort(int signal)
    00545 {
    00546   RaiseException(0xE1212012, 0, 0, NULL);
    00547 }
    00548
    00549 /* static */ void CrashLog::InitialiseCrashLog()
    00550 {
    00551 #if defined(_MSC_VER)
    00552 #ifdef _M_AMD64
    00553   CONTEXT ctx;
    00554   RtlCaptureContext(&ctx);
    00555
    00556   /* The stack pointer for AMD64 must always be 16-byte aligned inside a
    00557    * function. As we are simulating a function call with the safe ESP value,
    00558    * we need to subtract 8 for the imaginary return address otherwise stack
    00559    * alignment would be wrong in the called function. */
    00560   _safe_esp = (void *)(ctx.Rsp - 8);
    00561 #else
    00562   _asm {
    00563     mov _safe_esp, esp
    00564   }
    00565 #endif
    00566 #else
    00567   asm("movl %esp, __safe_esp");
    00568 #endif
    00569
    00570   /* SIGABRT is not an unhandled exception, so we need to intercept it. */
    00571   signal(SIGABRT, CustomAbort);
    00572 #if defined(_MSC_VER)
    00573   /* Don't show abort message as we will get the crashlog window anyway. */
    00574   _set_abort_behavior(0, _WRITE_ABORT_MSG);
    00575 #endif
    00576   SetUnhandledExceptionFilter(ExceptionHandler);
    00577 }
    00578
    00579 /* The crash log GUI */
    00580
    00581 static bool _expanded;
    00582
    00583 static const TCHAR _crash_desc[] =
    00584   _T("A serious fault condition occurred in the game. The game will shut down.\n")
    00585   _T("Please send the crash information and the crash.dmp file (if any) to the developers.\n")
    00586   _T("This will greatly help debugging. The correct place to do this is http://bugs.openttd.org. ")
    00587   _T("The information contained in the report is displayed below.\n")
    00588   _T("Press \"Emergency save\" to attempt saving the game. Generated file(s):\n")
    00589   _T("%s");
    00590
    00591 static const TCHAR _save_succeeded[] =
    00592   _T("Emergency save succeeded.\nIts location is '%s'.\n")
    00593   _T("Be aware that critical parts of the internal game state may have become ")
    00594   _T("corrupted. The saved game is not guaranteed to work.");
    00595
    00596 static const TCHAR * const _expand_texts[] = {_T("S&how report >>"), _T("&Hide report <<") };
    00597
    00598 static void SetWndSize(HWND wnd, int mode)
    00599 {
    00600   RECT r, r2;
    00601
    00602   GetWindowRect(wnd, &r);
    00603   SetDlgItemText(wnd, 15, _expand_texts[mode == 1]);
    00604
    00605   if (mode >= 0) {
    00606     GetWindowRect(GetDlgItem(wnd, 11), &r2);
    00607     int offs = r2.bottom - r2.top + 10;
    00608     if (!mode) offs = -offs;
    00609     SetWindowPos(wnd, HWND_TOPMOST, 0, 0,
    00610       r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER);
    00611   } else {
    00612     SetWindowPos(wnd, HWND_TOPMOST,
    00613       (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2,
    00614       (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2,
    00615       0, 0, SWP_NOSIZE);
    00616   }
    00617 }
    00618
    00619 static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
    00620 {
    00621   switch (msg) {
    00622     case WM_INITDIALOG: {
    00623 #if defined(UNICODE)
    00624       /* We need to put the crash-log in a seperate buffer because the default
    00625        * buffer in MB_TO_WIDE is not large enough (512 chars) */
    00626       wchar_t crash_msgW[lengthof(CrashLogWindows::current->crashlog)];
    00627 #endif
    00628       /* Convert unix -> dos newlines because the edit box only supports that properly :( */
    00629       const char *unix_nl = CrashLogWindows::current->crashlog;
    00630       char dos_nl[lengthof(CrashLogWindows::current->crashlog)];
    00631       char *p = dos_nl;
    00632       WChar c;
    00633       while ((c = Utf8Consume(&unix_nl)) && p < lastof(dos_nl) - 4) { // 4 is max number of bytes per character
    00634         if (c == '\n') p += Utf8Encode(p, '\r');
    00635         p += Utf8Encode(p, c);
    00636       }
    00637       *p = '\0';
    00638
    00639       /* Add path to crash.log and crash.dmp (if any) to the crash window text */
    00640       size_t len = _tcslen(_crash_desc) + 2;
    00641       len += _tcslen(OTTD2FS(CrashLogWindows::current->crashlog_filename)) + 2;
    00642       len += _tcslen(OTTD2FS(CrashLogWindows::current->crashdump_filename)) + 2;
    00643       len += _tcslen(OTTD2FS(CrashLogWindows::current->screenshot_filename)) + 1;
    00644
    00645       TCHAR *text = AllocaM(TCHAR, len);
    00646       _sntprintf(text, len, _crash_desc, OTTD2FS(CrashLogWindows::current->crashlog_filename));
    00647       if (OTTD2FS(CrashLogWindows::current->crashdump_filename)[0] != _T('\0')) {
    00648         _tcscat(text, _T("\n"));
    00649         _tcscat(text, OTTD2FS(CrashLogWindows::current->crashdump_filename));
    00650       }
    00651       if (OTTD2FS(CrashLogWindows::current->screenshot_filename)[0] != _T('\0')) {
    00652         _tcscat(text, _T("\n"));
    00653         _tcscat(text, OTTD2FS(CrashLogWindows::current->screenshot_filename));
    00654       }
    00655
    00656       SetDlgItemText(wnd, 10, text);
    00657       SetDlgItemText(wnd, 11, MB_TO_WIDE_BUFFER(dos_nl, crash_msgW, lengthof(crash_msgW)));
    00658       SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
    00659       SetWndSize(wnd, -1);
    00660     } return TRUE;
    00661     case WM_COMMAND:
    00662       switch (wParam) {
    00663         case 12: // Close
    00664           CrashLog::AfterCrashLogCleanup();
    00665           ExitProcess(2);
    00666         case 13: // Emergency save
    00667           char filename[MAX_PATH];
    00668           if (CrashLogWindows::current->WriteSavegame(filename, lastof(filename))) {
    00669             size_t len = _tcslen(_save_succeeded) + _tcslen(OTTD2FS(filename)) + 1;
    00670             TCHAR *text = AllocaM(TCHAR, len);
    00671             _sntprintf(text, len, _save_succeeded, OTTD2FS(filename));
    00672             MessageBox(wnd, text, _T("Save successful"), MB_ICONINFORMATION);
    00673           } else {
    00674             MessageBox(wnd, _T("Save failed"), _T("Save failed"), MB_ICONINFORMATION);
    00675           }
    00676           break;
    00677         case 15: // Expand window to show crash-message
    00678           _expanded ^= 1;
    00679           SetWndSize(wnd, _expanded);
    00680           break;
    00681       }
    00682       return TRUE;
    00683     case WM_CLOSE:
    00684       CrashLog::AfterCrashLogCleanup();
    00685       ExitProcess(2);
    00686   }
    00687
    00688   return FALSE;
    00689 }
    00690
    00691 static void ShowCrashlogWindow()
    00692 {
    00693   ShowCursor(TRUE);
    00694   ShowWindow(GetActiveWindow(), FALSE);
    00695   DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(100), NULL, CrashDialogFunc);
    00696 }