diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 792db4e4..84d4d02b 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -86,6 +86,44 @@ def free_gb(path): return 0.0 +# Exit codes that end the game without a line of its own in its log, and what they mean. The game's crash report exits +# with 71 after writing its FATAL lines (runtime/main.c crash_handler); the others come from Windows when something +# ends the process before or around that report. +EXIT_CODES = { + 70: 'the game could not start itself again', + 71: 'the crash report ended the game', + 0xC0000005: 'access violation', + 0xC00000FD: 'stack overflow', + 0xC0000142: 'a DLL failed to start', + 0xC0000374: 'heap corruption', + 0xC0000409: 'fail-fast: a security check or abort ended the process without an exception', + 0xC0000417: 'invalid parameter to a C runtime function', + 0xC000041D: 'a fault inside a Windows callback', + 0xE06D7363: 'a C++ exception no one caught', +} + + +def exit_code_text(code): + """The exit code as the game's log and Windows show it: hex for a Windows status, with its meaning when known.""" + code &= 0xFFFFFFFF + meaning = EXIT_CODES.get(code) + shown = f'0x{code:08X}' if code >= 0x80000000 else str(code) + return f'{shown} ({meaning})' if meaning else shown + + +def note_exit_in_log(game_dir, text): + """Adds the exit code to the end of the game's log, so a log a player sends shows how the run ended even when the + process died before it could write a line itself.""" + if not game_dir: + return + log = Path(game_dir) / 'logs' / 'nx2navi.log' + try: + with open(log, 'a', encoding='utf-8') as f: + f.write(f'[launcher] the game ended with code {text} at {time.strftime("%Y-%m-%d %H:%M:%S")}\n') + except OSError: + pass + + def drive_of(path): return Path(path).anchor.rstrip('\\/') or str(Path(path).parent) @@ -870,8 +908,10 @@ class App: self.root.deiconify() self.root.lift() if code not in (0, None): - self.ctx.log(f'the game ended with code {code}') - self.message = f'The game ended with code {code}. Its log is logs\\nx2navi.log in the game folder.' + text = exit_code_text(code) + self.ctx.log(f'the game ended with code {text}') + self.message = f'The game ended with code {text}. Its log is logs\\nx2navi.log in the game folder.' + note_exit_in_log(self.game_dir(), text) if self.install_staged('the game exited'): return self.show('home', self.focus) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index fc249ed7..33d64139 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -500,6 +500,10 @@ # last one it finished. Unset: on AMD GPUs (VK_AMD_buffer_marker). 1: also on NVIDIA GPUs # (VK_NV_device_diagnostic_checkpoints), which NVIDIA says costs a little. 0: off. # pass_crumbs = 1 +# Once a second, puts the crash report's exception filter back if a GPU driver or an overlay loaded after the start set +# its own in its place, and says so in the log. Without it, a crash in such a DLL can end the game with no FATAL line in +# the log. 0: off. +# crash_filter_guard = 1 # Render targets up to this size (bytes, 0 to 1073741824) go back to their game memory whenever the game waits for a # GPU fence. A larger target goes back too unless the GPU drew it in each of the last 16 frames. A game that reads a # larger target drawn every frame without invalidating it sees stale bytes at the default. With trace = gfx the log diff --git a/runtime/cfg.c b/runtime/cfg.c index d9780b4c..2b7ee4d2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -41,7 +41,7 @@ static const char *const known[] = { "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", - "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", + "DUMP_LOG", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", diff --git a/runtime/perf.c b/runtime/perf.c index a39e3419..2545c68d 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -498,8 +498,24 @@ static void cpu_placement(void) { plat_process_affinity(on ? one : all); } +// NX2NAVI_CRASH_FILTER_GUARD (default 1; 0 off): once a second, the crash report's last-chance exception filter is put +// back if a DLL loaded after the start (a GPU driver, an overlay) set its own in its place. Without it, a fault in such +// a DLL ends the process with no FATAL line in the log. The first eight replacements are logged with the module. +static void crash_filter_guard(uint64_t frame) { + static int on = -1, logged; + if (on < 0) on = rt_knob("CRASH_FILTER_GUARD", "1"); + if (!on || frame % 60) return; + char who[160]; + if (plat_crash_filter_reassert(who, sizeof who) && logged < 8) { + logged++; + rt_log("diag: frame %llu: an exception filter from %s had replaced the crash report's; the crash report's is " + "back (NX2NAVI_CRASH_FILTER_GUARD)", (unsigned long long)frame, who); + } +} + void perf_present(uint64_t frame) { static uint64_t last, sum, max, n, win_start, ab_b; + crash_filter_guard(frame); static int64_t win_prev[PERF_CNT_N]; prof_render_tid = plat_thread_id(); if (rt_trace & RT_TRACE_SCHED) kern_sched_report(frame); diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 9cdae4b5..df99a891 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1051,6 +1051,7 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) { } void plat_crash_context(const char *s) { (void)s; } // the Windows crash notes only +int plat_crash_filter_reassert(char *who, size_t n) { (void)who, (void)n; return 0; } // Windows only // plat_jmp_save / plat_jmp_go (platform.h): leaf code that does not move rsp before it leaves, as on Windows. _Static_assert(sizeof(PlatJmp) == 72 && __builtin_offsetof(PlatJmp, mxcsr) == 64, "plat_jmp_save layout"); diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index d261896e..82001447 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -271,6 +271,9 @@ void plat_crash_hook(void (*fn)(const PlatCrash *c)); // What this thread is doing, named by the crash notes (a stack overflow leaves no room for the full report); NULL // clears it. s must stay valid until it is cleared. void plat_crash_context(const char *s); +// Puts the crash hook's last-chance exception filter back in place. 1 when a DLL loaded later had set its own in its +// place (who: that filter's module and offset), 0 when it was still there. Windows only; 0 elsewhere. +int plat_crash_filter_reassert(char *who, size_t n); int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory int plat_mem_readable(uint64_t addr, uint64_t n); // every page of [addr, addr + n) can be read; no fault void *plat_aligned_alloc(size_t size, size_t align); // free with plat_aligned_free; NULL on failure diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index bc58aa1f..66d8966a 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1007,6 +1007,21 @@ void plat_crash_hook(void (*fn)(const PlatCrash *c)) { _set_invalid_parameter_handler(invalid_parameter); } +// The last-chance filter is set once, at the start, before the GPU driver, the window and audio libraries and the +// movie decoder load. A DLL that sets its own filter later takes the place of crash_filter, and a fault outside this +// executable then ends the process with no line in the log. This puts crash_filter back; when another filter had +// taken its place, `who` gets the module and offset of that filter and the call returns 1. +int plat_crash_filter_reassert(char *who, size_t n) { + LPTOP_LEVEL_EXCEPTION_FILTER prev = SetUnhandledExceptionFilter(crash_filter); + if (prev == crash_filter) return 0; + void *base = NULL; + if (prev) RtlPcToFileHeader((void *)prev, &base); + if (!prev) snprintf(who, n, "no filter (removed)"); + else if (!base) snprintf(who, n, "%p, outside every module", (void *)prev); + else snprintf(who, n, "%s+%#llx", image_name(base), (unsigned long long)((uintptr_t)prev - (uintptr_t)base)); + return 1; +} + // A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent // waits and exits with the child's code. void plat_reexec(void) {