From db3f59aac079128004085f2411dd108cab141dd6 Mon Sep 17 00:00:00 2001 From: louisdibiasi <59670693+louisdibiasi@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:50:37 -0500 Subject: [PATCH] startup: preserve debugger stderr and exit status --- tests/startup_run_ps1_check.py | 45 +++++++++++++++++++++++++++++----- tools/startup/run.ps1 | 11 ++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/tests/startup_run_ps1_check.py b/tests/startup_run_ps1_check.py index 2f90f43b49..968f471945 100644 --- a/tests/startup_run_ps1_check.py +++ b/tests/startup_run_ps1_check.py @@ -20,6 +20,7 @@ RUN_PS1 = Path(__file__).resolve().parent.parent / "tools" / "startup" / "run.ps1" DLLS = ["xmem", "d3d12_xs", "AcpHal", "XCRDAPI", "chad", "xapu", "xg_xs", "GameInput"] STUB_LINES = [f"stub debugger line {n}" for n in range(1, 8)] +STUB_ERROR = "synthetic debugger error" HEAD_LINES = 5 # run.ps1 prints this many lines of ".out" # The stub stands in for tools/startup/debug_launch.py: it records its arguments, # prints seven lines and exits with the code under test. @@ -49,6 +50,8 @@ time.sleep(0.05) for n in range(1, 8): print("stub debugger line", n) +if {emit_stderr}: + print("synthetic debugger error", file=sys.stderr) sys.exit({code}) """ # Stands in for tools/startup/gfx2hlsl.py under -Translate: starts a child process, as the parallel @@ -76,7 +79,7 @@ WATCH_CLEANUP = re.compile( r"if\s*\(\s*\$translator\s+-and\s+-not\s+\$translator\.HasExited\s*\)\s*\{\s*&\s*\{\s*" r"""\$ErrorActionPreference\s*=\s*['"]Continue['"]\s+&\s+taskkill\.exe\b[^|#\n]*\s/T\b""", re.I) -CAPTURE = re.compile(r"\$(\w+)\s*=\s*\$LASTEXITCODE", re.I) +CAPTURE = re.compile(r"\$(?:script:)?(\w+)\s*=\s*\$LASTEXITCODE", re.I) EXIT = re.compile(r"exit\s+\$(\w+)", re.I) HEAD = 'Get-Content "$LogDir.out" -TotalCount 5' # Without -FakeUser, user APIs hit the host runtime; run.ps1 must warn (docs/startup-runtime.md). @@ -136,7 +139,8 @@ def alive(pid): def run_case(shell, script, code, work, translate=False, switches=(), passed=(), fake_user_env=False, - early_exit=False, trace_api_env=None, absolute_build=False, missing_build=False): + early_exit=False, trace_api_env=None, absolute_build=False, missing_build=False, emit_stderr=False, + missing_python=False, real_log_error=False): """Run one copy of the script on a stub that exits with `code`; return what is wrong. `switches` are extra run.ps1 switches and `passed` the debugger arguments they must add. @@ -148,7 +152,11 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), startup = root / "tools" / "startup" startup.mkdir(parents=True) (startup / "run.ps1").write_bytes(script.encode("utf-8")) - (startup / "debug_launch.py").write_text(STUB.format(code=code, early_exit=early_exit), encoding="utf-8") + if real_log_error: + for name in ("debug_launch.py", "launch.py", "host_cpu.py"): + shutil.copyfile(RUN_PS1.parent / name, startup / name) + else: + (startup / "debug_launch.py").write_text(STUB.format(code=code, early_exit=early_exit, emit_stderr=emit_stderr), encoding="utf-8") (startup / "gfx2hlsl.py").write_text(WATCH_STUB.format(early_exit=early_exit), encoding="utf-8") build = root / ("build [with spaces]" if absolute_build else "bld") build.mkdir() @@ -158,6 +166,8 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), exe.parent.mkdir() exe.write_bytes(b"") data.mkdir() + if real_log_error: + (log / "events.txt").mkdir(parents=True) env = dict(os.environ) # Compare PowerShell's warning text consistently on hosts that enable ANSI colors. env["NO_COLOR"] = "1" @@ -171,6 +181,8 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), # In this tree run.ps1 finds `python` through PATH. This interpreter's directory # goes first so the stub runs under the Python that runs this check. env["PATH"] = os.path.dirname(sys.executable) + os.pathsep + env.get("PATH", "") + if missing_python: + env["PATH"] = str(Path(os.environ["SystemRoot"]) / "System32") build_arg = str(build) if absolute_build else "bld" if missing_build: build_arg = str(root / "missing build") @@ -193,23 +205,35 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), stderr.seek(0) printed = [line.strip() for line in stdout if line.strip()] error_text = stderr.read().strip() + if missing_python: + return (["missing Python returned success"] if out.returncode == 0 else []) + ( + ["missing Python launched debugger"] if (startup / "debugger.json").exists() else []) + if real_log_error: + summary = Path(str(log) + ".out") + text = decode(summary.read_bytes()) if summary.is_file() else "" + return ([f"real debugger log error returned {out.returncode}, expected 1"] if out.returncode != 1 else []) + ( + ["real debugger log error was not captured in the summary"] if "PermissionError" not in text else []) found = [] for dll in DLLS: if not (exe.parent / f"{dll}.dll").is_file(): found.append(f"stand-in {dll}.dll was not copied") if out.returncode != code: found.append(f"stub debugger exited {code}, run.ps1 exited {out.returncode}") - want_output = STUB_LINES[:HEAD_LINES] + summary = Path(str(log) + ".out") + summary_lines = [line.strip() for line in decode(summary.read_bytes()).splitlines() if line.strip()] if summary.is_file() else [] + want_output = summary_lines[:HEAD_LINES] if emit_stderr else STUB_LINES[:HEAD_LINES] if not fake_user_env and "-FakeUser" not in switches: want_output = [warning_prefix(shell, env) + FAKE_USER_WARNING, *want_output] printed = unwrap_first_line(printed, want_output[0]) if printed != want_output: found.append(f"stub exit {code}: printed {printed}, expected {want_output} " f"(stderr: {error_text!r})") - summary = Path(str(log) + ".out") if not summary.is_file(): found.append(f"stub exit {code}: {summary.name} was not written") - elif [line.strip() for line in decode(summary.read_bytes()).splitlines() if line.strip()] != STUB_LINES: + elif emit_stderr and ([line for line in summary_lines if line in STUB_LINES] != STUB_LINES or + not any(STUB_ERROR in line for line in summary_lines)): + found.append(f"stub exit {code}: {summary.name} does not hold stdout and stderr") + elif not emit_stderr and summary_lines != STUB_LINES: found.append(f"stub exit {code}: {summary.name} does not hold the debugger's output") want = [str(exe), str(data), str(log), "--timeout", "9", "--memory-gib", "3", "--trace-files", "1", "--continue-asserts", "4", *passed] @@ -242,6 +266,13 @@ def run_problems(shell, script, work, first_only): if found and first_only: return found found += run_case(shell, script, 7, work, absolute_build=True) + if found and first_only: + return found + for code in (0, 7): + found += run_case(shell, script, code, work, emit_stderr=True) + if found and first_only: + return found + found += run_case(shell, script, 7, work, translate=True, emit_stderr=True) if found and first_only: return found found += run_case(shell, script, 0, work, switches=["-FakeUser"]) @@ -333,6 +364,8 @@ def shell_failures(shell, script, tmp): if mutant is None or not run_problems(shell, mutant, tmp, True): failures.append(f"{name}: mutant '{label}' was not detected") if shell: + failures += [f"{name}: {item}" for item in run_case(shell, script, 7, tmp, real_log_error=True)] + failures += [f"{name}: {item}" for item in run_case(shell, script, 7, tmp, missing_python=True)] missing = run_case(shell, script, 7, tmp, missing_build=True) if not any("debugger [argv" in item and "was None" in item for item in missing): failures.append(f"{name}: missing build directory did not prevent debugger startup") diff --git a/tools/startup/run.ps1 b/tools/startup/run.ps1 index e9fceffffb..035296cc88 100644 --- a/tools/startup/run.ps1 +++ b/tools/startup/run.ps1 @@ -78,9 +78,14 @@ if ($Translate -and $Render) { $traceArgs = @() if ($TraceCalls) { $traceArgs = @("--trace-calls", "1") } try { - & $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB ` - --trace-files 1 --continue-asserts $ContinueAsserts @traceArgs *> "$LogDir.out" - $debuggerExit = $LASTEXITCODE + # Windows PowerShell 5.1 wraps redirected native stderr as an error record. Keep that + # from aborting the launcher before it can report the debugger's exit code. + & { + $ErrorActionPreference = "Continue" + & $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB ` + --trace-files 1 --continue-asserts $ContinueAsserts @traceArgs + $script:debuggerExit = $LASTEXITCODE + } *> "$LogDir.out" } finally { # taskkill /T needs a live parent PID to reach the watcher's workers. if ($translator -and -not $translator.HasExited) {