crashlog_win.cpp

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