10 #include "../../stdafx.h"
11 #include "../../crashlog.h"
13 #include "../../core/alloc_func.hpp"
14 #include "../../core/math_func.hpp"
15 #include "../../string_func.h"
16 #include "../../fileio_func.h"
17 #include "../../strings_func.h"
18 #include "../../gamelog.h"
19 #include "../../saveload/saveload.h"
20 #include "../../video/video_driver.hpp"
27 #include "../../safeguards.h"
31 #define PRINTF_PTR "0x%016IX"
33 #define PRINTF_PTR "0x%08X"
41 EXCEPTION_POINTERS *
ep;
43 char *
LogOSVersion(
char *buffer,
const char *last)
const override;
44 char *
LogError(
char *buffer,
const char *last,
const char *
message)
const override;
45 char *
LogStacktrace(
char *buffer,
const char *last)
const override;
46 char *
LogRegisters(
char *buffer,
const char *last)
const override;
47 char *
LogModules(
char *buffer,
const char *last)
const override;
50 int WriteCrashDump(
char *filename,
const char *filename_last)
const override;
51 char *AppendDecodedStacktrace(
char *buffer,
const char *last)
const;
53 char *AppendDecodedStacktrace(
char *buffer,
const char *last)
const {
return buffer; }
72 this->crashlog[0] =
'\0';
73 this->crashlog_filename[0] =
'\0';
74 this->crashdump_filename[0] =
'\0';
75 this->screenshot_filename[0] =
'\0';
89 os.dwOSVersionInfoSize =
sizeof(os);
92 return buffer +
seprintf(buffer, last,
95 " Release: %d.%d.%d (%s)\n",
96 (
int)os.dwMajorVersion,
97 (
int)os.dwMinorVersion,
98 (
int)os.dwBuildNumber,
106 return buffer +
seprintf(buffer, last,
110 " Location: %.16IX\n"
115 (
int)
ep->ExceptionRecord->ExceptionCode,
116 (
size_t)
ep->ExceptionRecord->ExceptionAddress,
124 SYSTEMTIME file_time;
127 static uint32 *_crc_table;
129 static void MakeCRCTable(uint32 *table)
131 uint32 crc, poly = 0xEDB88320L;
137 for (i = 0; i != 256; i++) {
139 for (j = 8; j != 0; j--) {
140 crc = (crc & 1 ? (crc >> 1) ^ poly : crc >> 1);
146 static uint32 CalcCRC(
byte *data, uint size, uint32 crc)
148 for (; size > 0; size--) {
149 crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF];
154 static void GetFileInfo(
DebugFileInfo *dfi,
const wchar_t *filename)
157 memset(dfi, 0,
sizeof(*dfi));
159 file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, 0, 0);
160 if (file != INVALID_HANDLE_VALUE) {
165 uint32 crc = (uint32)-1;
168 if (ReadFile(file, buffer,
sizeof(buffer), &numread,
nullptr) == 0 || numread == 0) {
172 crc = CalcCRC(buffer, numread, crc);
174 dfi->size = filesize;
175 dfi->crc32 = crc ^ (uint32)-1;
177 if (GetFileTime(file,
nullptr,
nullptr, &write_time)) {
178 FileTimeToSystemTime(&write_time, &dfi->file_time);
185 static char *PrintModuleInfo(
char *output,
const char *last, HMODULE mod)
187 wchar_t buffer[MAX_PATH];
190 GetModuleFileName(mod, buffer, MAX_PATH);
191 GetFileInfo(&dfi, buffer);
192 output +=
seprintf(output, last,
" %-20s handle: %p size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\n",
198 dfi.file_time.wMonth,
201 dfi.file_time.wMinute,
202 dfi.file_time.wSecond
209 MakeCRCTable(
AllocaM(uint32, 256));
210 output +=
seprintf(output, last,
"Module information:\n");
212 HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
213 if (proc !=
nullptr) {
214 HMODULE modules[100];
216 BOOL res = EnumProcessModules(proc, modules,
sizeof(modules), &needed);
219 size_t count = std::min<DWORD>(needed /
sizeof(HMODULE),
lengthof(modules));
221 for (
size_t i = 0; i != count; i++) output = PrintModuleInfo(output, last, modules[i]);
222 return output +
seprintf(output, last,
"\n");
225 output = PrintModuleInfo(output, last,
nullptr);
226 return output +
seprintf(output, last,
"\n");
231 buffer +=
seprintf(buffer, last,
"Registers:\n");
234 " RAX: %.16I64X RBX: %.16I64X RCX: %.16I64X RDX: %.16I64X\n"
235 " RSI: %.16I64X RDI: %.16I64X RBP: %.16I64X RSP: %.16I64X\n"
236 " R8: %.16I64X R9: %.16I64X R10: %.16I64X R11: %.16I64X\n"
237 " R12: %.16I64X R13: %.16I64X R14: %.16I64X R15: %.16I64X\n"
238 " RIP: %.16I64X EFLAGS: %.8lX\n",
239 ep->ContextRecord->Rax,
240 ep->ContextRecord->Rbx,
241 ep->ContextRecord->Rcx,
242 ep->ContextRecord->Rdx,
243 ep->ContextRecord->Rsi,
244 ep->ContextRecord->Rdi,
245 ep->ContextRecord->Rbp,
246 ep->ContextRecord->Rsp,
247 ep->ContextRecord->R8,
248 ep->ContextRecord->R9,
249 ep->ContextRecord->R10,
250 ep->ContextRecord->R11,
251 ep->ContextRecord->R12,
252 ep->ContextRecord->R13,
253 ep->ContextRecord->R14,
254 ep->ContextRecord->R15,
255 ep->ContextRecord->Rip,
256 ep->ContextRecord->EFlags
258 #elif defined(_M_IX86)
260 " EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\n"
261 " ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\n"
262 " EIP: %.8X EFLAGS: %.8X\n",
263 (
int)
ep->ContextRecord->Eax,
264 (
int)
ep->ContextRecord->Ebx,
265 (
int)
ep->ContextRecord->Ecx,
266 (
int)
ep->ContextRecord->Edx,
267 (
int)
ep->ContextRecord->Esi,
268 (
int)
ep->ContextRecord->Edi,
269 (
int)
ep->ContextRecord->Ebp,
270 (
int)
ep->ContextRecord->Esp,
271 (
int)
ep->ContextRecord->Eip,
272 (
int)
ep->ContextRecord->EFlags
274 #elif defined(_M_ARM64)
276 " X0: %.16I64X X1: %.16I64X X2: %.16I64X X3: %.16I64X\n"
277 " X4: %.16I64X X5: %.16I64X X6: %.16I64X X7: %.16I64X\n"
278 " X8: %.16I64X X9: %.16I64X X10: %.16I64X X11: %.16I64X\n"
279 " X12: %.16I64X X13: %.16I64X X14: %.16I64X X15: %.16I64X\n"
280 " X16: %.16I64X X17: %.16I64X X18: %.16I64X X19: %.16I64X\n"
281 " X20: %.16I64X X21: %.16I64X X22: %.16I64X X23: %.16I64X\n"
282 " X24: %.16I64X X25: %.16I64X X26: %.16I64X X27: %.16I64X\n"
283 " X28: %.16I64X Fp: %.16I64X Lr: %.16I64X\n",
284 ep->ContextRecord->X0,
285 ep->ContextRecord->X1,
286 ep->ContextRecord->X2,
287 ep->ContextRecord->X3,
288 ep->ContextRecord->X4,
289 ep->ContextRecord->X5,
290 ep->ContextRecord->X6,
291 ep->ContextRecord->X7,
292 ep->ContextRecord->X8,
293 ep->ContextRecord->X9,
294 ep->ContextRecord->X10,
295 ep->ContextRecord->X11,
296 ep->ContextRecord->X12,
297 ep->ContextRecord->X13,
298 ep->ContextRecord->X14,
299 ep->ContextRecord->X15,
300 ep->ContextRecord->X16,
301 ep->ContextRecord->X17,
302 ep->ContextRecord->X18,
303 ep->ContextRecord->X19,
304 ep->ContextRecord->X20,
305 ep->ContextRecord->X21,
306 ep->ContextRecord->X22,
307 ep->ContextRecord->X23,
308 ep->ContextRecord->X24,
309 ep->ContextRecord->X25,
310 ep->ContextRecord->X26,
311 ep->ContextRecord->X27,
312 ep->ContextRecord->X28,
313 ep->ContextRecord->Fp,
314 ep->ContextRecord->Lr
318 buffer +=
seprintf(buffer, last,
"\n Bytes at instruction pointer:\n");
320 byte *b = (
byte*)
ep->ContextRecord->Rip;
321 #elif defined(_M_IX86)
322 byte *b = (
byte*)
ep->ContextRecord->Eip;
323 #elif defined(_M_ARM64)
324 byte *b = (
byte*)
ep->ContextRecord->Pc;
326 for (
int i = 0; i != 24; i++) {
327 if (IsBadReadPtr(b, 1)) {
328 buffer +=
seprintf(buffer, last,
" ??");
330 buffer +=
seprintf(buffer, last,
" %.2X", *b);
334 return buffer +
seprintf(buffer, last,
"\n\n");
339 buffer +=
seprintf(buffer, last,
"Stack trace:\n");
341 uint32 *b = (uint32*)
ep->ContextRecord->Rsp;
342 #elif defined(_M_IX86)
343 uint32 *b = (uint32*)
ep->ContextRecord->Esp;
344 #elif defined(_M_ARM64)
345 uint32 *b = (uint32*)
ep->ContextRecord->Sp;
347 for (
int j = 0; j != 24; j++) {
348 for (
int i = 0; i != 8; i++) {
349 if (IsBadReadPtr(b,
sizeof(uint32))) {
350 buffer +=
seprintf(buffer, last,
" ????????");
352 buffer +=
seprintf(buffer, last,
" %.8X", *b);
356 buffer +=
seprintf(buffer, last,
"\n");
358 return buffer +
seprintf(buffer, last,
"\n");
361 #if defined(_MSC_VER)
362 static const uint MAX_SYMBOL_LEN = 512;
363 static const uint MAX_FRAMES = 64;
365 #pragma warning(disable:4091)
367 #pragma warning(default:4091)
369 char *CrashLogWindows::AppendDecodedStacktrace(
char *buffer,
const char *last)
const
373 BOOL (WINAPI * pSymInitialize)(HANDLE, PCSTR, BOOL);
374 BOOL (WINAPI * pSymSetOptions)(DWORD);
375 BOOL (WINAPI * pSymCleanup)(HANDLE);
376 BOOL (WINAPI * pStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, PVOID, PREAD_PROCESS_MEMORY_ROUTINE64, PFUNCTION_TABLE_ACCESS_ROUTINE64, PGET_MODULE_BASE_ROUTINE64, PTRANSLATE_ADDRESS_ROUTINE64);
377 PVOID (WINAPI * pSymFunctionTableAccess64)(HANDLE, DWORD64);
378 DWORD64 (WINAPI * pSymGetModuleBase64)(HANDLE, DWORD64);
379 BOOL (WINAPI * pSymGetModuleInfo64)(HANDLE, DWORD64, PIMAGEHLP_MODULE64);
380 BOOL (WINAPI * pSymGetSymFromAddr64)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64);
381 BOOL (WINAPI * pSymGetLineFromAddr64)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64);
383 dbghelp.GetProcAddress(
"SymInitialize"),
384 dbghelp.GetProcAddress(
"SymSetOptions"),
385 dbghelp.GetProcAddress(
"SymCleanup"),
386 dbghelp.GetProcAddress(
"StackWalk64"),
387 dbghelp.GetProcAddress(
"SymFunctionTableAccess64"),
388 dbghelp.GetProcAddress(
"SymGetModuleBase64"),
389 dbghelp.GetProcAddress(
"SymGetModuleInfo64"),
390 dbghelp.GetProcAddress(
"SymGetSymFromAddr64"),
391 dbghelp.GetProcAddress(
"SymGetLineFromAddr64"),
394 buffer +=
seprintf(buffer, last,
"\nDecoded stack trace:\n");
397 if (dbghelp.Success()) {
399 HANDLE hCur = GetCurrentProcess();
400 proc.pSymInitialize(hCur,
nullptr, TRUE);
402 proc.pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_UNDNAME);
406 memset(&frame, 0,
sizeof(frame));
408 frame.AddrPC.Offset =
ep->ContextRecord->Rip;
409 frame.AddrFrame.Offset =
ep->ContextRecord->Rbp;
410 frame.AddrStack.Offset =
ep->ContextRecord->Rsp;
411 #elif defined(_M_IX86)
412 frame.AddrPC.Offset =
ep->ContextRecord->Eip;
413 frame.AddrFrame.Offset =
ep->ContextRecord->Ebp;
414 frame.AddrStack.Offset =
ep->ContextRecord->Esp;
415 #elif defined(_M_ARM64)
416 frame.AddrPC.Offset =
ep->ContextRecord->Pc;
417 frame.AddrFrame.Offset =
ep->ContextRecord->Fp;
418 frame.AddrStack.Offset =
ep->ContextRecord->Sp;
420 frame.AddrPC.Mode = AddrModeFlat;
421 frame.AddrFrame.Mode = AddrModeFlat;
422 frame.AddrStack.Mode = AddrModeFlat;
426 memcpy(&ctx,
ep->ContextRecord,
sizeof(ctx));
429 IMAGEHLP_SYMBOL64 *sym_info = (IMAGEHLP_SYMBOL64*)alloca(
sizeof(IMAGEHLP_SYMBOL64) + MAX_SYMBOL_LEN - 1);
430 sym_info->SizeOfStruct =
sizeof(IMAGEHLP_SYMBOL64);
431 sym_info->MaxNameLength = MAX_SYMBOL_LEN;
434 for (uint num = 0; num < MAX_FRAMES; num++) {
435 if (!proc.pStackWalk64(
437 IMAGE_FILE_MACHINE_AMD64,
439 IMAGE_FILE_MACHINE_I386,
441 hCur, GetCurrentThread(), &frame, &ctx,
nullptr, proc.pSymFunctionTableAccess64, proc.pSymGetModuleBase64,
nullptr))
break;
443 if (frame.AddrPC.Offset == frame.AddrReturn.Offset) {
444 buffer +=
seprintf(buffer, last,
" <infinite loop>\n");
449 const char *mod_name =
"???";
451 IMAGEHLP_MODULE64 module;
452 module.SizeOfStruct =
sizeof(module);
453 if (proc.pSymGetModuleInfo64(hCur, frame.AddrPC.Offset, &module)) {
454 mod_name = module.ModuleName;
458 buffer +=
seprintf(buffer, last,
"[%02d] %-20s " PRINTF_PTR, num, mod_name, frame.AddrPC.Offset);
462 if (proc.pSymGetSymFromAddr64(hCur, frame.AddrPC.Offset, &offset, sym_info)) {
463 buffer +=
seprintf(buffer, last,
" %s + %I64u", sym_info->Name, offset);
466 IMAGEHLP_LINE64 line;
467 line.SizeOfStruct =
sizeof(IMAGEHLP_LINE64);
468 if (proc.pSymGetLineFromAddr64(hCur, frame.AddrPC.Offset, &line_offs, &line)) {
469 buffer +=
seprintf(buffer, last,
" (%s:%d)", line.FileName, line.LineNumber);
472 buffer +=
seprintf(buffer, last,
"\n");
475 proc.pSymCleanup(hCur);
478 return buffer +
seprintf(buffer, last,
"\n*** End of additional info ***\n");
485 if (dbghelp.Success()) {
486 typedef BOOL (WINAPI *MiniDumpWriteDump_t)(HANDLE, DWORD, HANDLE,
488 CONST PMINIDUMP_EXCEPTION_INFORMATION,
489 CONST PMINIDUMP_USER_STREAM_INFORMATION,
490 CONST PMINIDUMP_CALLBACK_INFORMATION);
491 MiniDumpWriteDump_t funcMiniDumpWriteDump = dbghelp.GetProcAddress(
"MiniDumpWriteDump");
492 if (funcMiniDumpWriteDump !=
nullptr) {
494 HANDLE file = CreateFile(
OTTD2FS(filename).c_str(), GENERIC_WRITE, 0,
nullptr, CREATE_ALWAYS, 0, 0);
495 HANDLE proc = GetCurrentProcess();
496 DWORD procid = GetCurrentProcessId();
497 MINIDUMP_EXCEPTION_INFORMATION mdei;
498 MINIDUMP_USER_STREAM userstream;
499 MINIDUMP_USER_STREAM_INFORMATION musi;
501 userstream.Type = LastReservedStream + 1;
502 userstream.Buffer = (
void*)this->
crashlog;
503 userstream.BufferSize = (ULONG)strlen(this->
crashlog) + 1;
505 musi.UserStreamCount = 1;
506 musi.UserStreamArray = &userstream;
508 mdei.ThreadId = GetCurrentThreadId();
509 mdei.ExceptionPointers =
ep;
510 mdei.ClientPointers =
false;
512 funcMiniDumpWriteDump(proc, procid, file, MiniDumpWithDataSegs, &mdei, &musi,
nullptr);
522 extern bool CloseConsoleLogIfActive();
523 static void ShowCrashlogWindow();
531 static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep)
537 SetWindowLongPtr(GetActiveWindow(), GWLP_WNDPROC, (LONG_PTR)&DefWindowProc);
545 static const wchar_t _emergency_crash[] =
546 L
"A serious fault condition occurred in the game. The game will shut down.\n"
547 L
"As you loaded an emergency savegame no crash information will be generated.\n";
548 MessageBox(
nullptr, _emergency_crash, L
"Fatal Application Failure", MB_ICONERROR);
553 static const wchar_t _saveload_crash[] =
554 L
"A serious fault condition occurred in the game. The game will shut down.\n"
555 L
"As you loaded an savegame for which you do not have the required NewGRFs\n"
556 L
"no crash information will be generated.\n";
557 MessageBox(
nullptr, _saveload_crash, L
"Fatal Application Failure", MB_ICONERROR);
570 CloseConsoleLogIfActive();
574 ep->ContextRecord->Rip = (DWORD64)ShowCrashlogWindow;
575 ep->ContextRecord->Rsp = (DWORD64)
_safe_esp;
576 #elif defined(_M_IX86)
577 ep->ContextRecord->Eip = (DWORD)ShowCrashlogWindow;
578 ep->ContextRecord->Esp = (DWORD)
_safe_esp;
579 #elif defined(_M_ARM64)
580 ep->ContextRecord->Pc = (DWORD64)ShowCrashlogWindow;
581 ep->ContextRecord->Sp = (DWORD64)
_safe_esp;
583 return EXCEPTION_CONTINUE_EXECUTION;
587 return EXCEPTION_EXECUTE_HANDLER;
590 static void CDECL CustomAbort(
int signal)
592 RaiseException(0xE1212012, 0, 0,
nullptr);
600 signal(SIGABRT, CustomAbort);
601 #if defined(_MSC_VER)
603 _set_abort_behavior(0, _WRITE_ABORT_MSG);
605 SetUnhandledExceptionFilter(ExceptionHandler);
610 #if defined(_M_AMD64) || defined(_M_ARM64)
612 RtlCaptureContext(&ctx);
618 # if defined(_M_ARM64)
625 # if defined(_MSC_VER)
630 asm(
"movl %%esp, %0" :
"=rm" (safe_esp));
638 static bool _expanded;
640 static const wchar_t _crash_desc[] =
641 L
"A serious fault condition occurred in the game. The game will shut down.\n"
642 L
"Please send the crash information and the crash.dmp file (if any) to the developers.\n"
643 L
"This will greatly help debugging. The correct place to do this is https://github.com/OpenTTD/OpenTTD/issues. "
644 L
"The information contained in the report is displayed below.\n"
645 L
"Press \"Emergency save\" to attempt saving the game. Generated file(s):\n"
648 static const wchar_t _save_succeeded[] =
649 L
"Emergency save succeeded.\nIts location is '%s'.\n"
650 L
"Be aware that critical parts of the internal game state may have become "
651 L
"corrupted. The saved game is not guaranteed to work.";
653 static const wchar_t *
const _expand_texts[] = {L
"S&how report >>", L
"&Hide report <<" };
655 static void SetWndSize(HWND wnd,
int mode)
659 GetWindowRect(wnd, &r);
660 SetDlgItemText(wnd, 15, _expand_texts[mode == 1]);
663 GetWindowRect(GetDlgItem(wnd, 11), &r2);
664 int offs = r2.bottom - r2.top + 10;
665 if (mode == 0) offs = -offs;
666 SetWindowPos(wnd, HWND_TOPMOST, 0, 0,
667 r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER);
669 SetWindowPos(wnd, HWND_TOPMOST,
670 (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2,
671 (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2,
676 static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
679 case WM_INITDIALOG: {
682 wchar_t filenamebuf[MAX_PATH * 2];
689 while ((c = Utf8Consume(&unix_nl)) && p <
lastof(dos_nl) - 4) {
696 size_t len = wcslen(_crash_desc) + 2;
701 wchar_t *text =
AllocaM(
wchar_t, len);
703 if (printed < 0 || (
size_t)printed > len) {
704 MessageBox(wnd, L
"Catastrophic failure trying to display crash message. Could not perform text formatting.", L
"OpenTTD", MB_ICONERROR);
709 wcscat(text, filenamebuf);
713 wcscat(text, filenamebuf);
716 SetDlgItemText(wnd, 10, text);
718 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
727 wchar_t filenamebuf[MAX_PATH * 2];
728 char filename[MAX_PATH];
731 size_t len =
lengthof(_save_succeeded) + wcslen(filenamebuf) + 1;
732 wchar_t *text =
AllocaM(
wchar_t, len);
733 _snwprintf(text, len, _save_succeeded, filenamebuf);
734 MessageBox(wnd, text, L
"Save successful", MB_ICONINFORMATION);
736 MessageBox(wnd, L
"Save failed", L
"Save failed", MB_ICONINFORMATION);
740 _expanded = !_expanded;
741 SetWndSize(wnd, _expanded);
753 static void ShowCrashlogWindow()
756 ShowWindow(GetActiveWindow(), FALSE);
757 DialogBox(GetModuleHandle(
nullptr), MAKEINTRESOURCE(100),
nullptr, CrashDialogFunc);