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