Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,12 @@ These are measured on one executable; treat them as hypotheses for others.
[#2618](https://github.com/R0Y00/Xbox2Windows/issues/2618)
- **Soft asserts.** Retail code keeps `int3` instructions followed by a continuation. Without a
debugger the title's handler steps over them; under the startup debugger use
`-ContinueAsserts N`. One of them fires about ten times a second in the texture-streaming path
`-AssertSites N` (`--assert-sites N` for `run.sh`), which steps over every hit at up to N distinct
main-image `int3` sites, so a site that repeats never runs the budget out however long the run is.
The first hit at one more site still stops the run, as do `X2W_STOP_AT` sites. `-ContinueAsserts N`
counts hits instead: it steps over the first N hits at any site. A hit is stepped over when either
budget allows it, and each budget counts only the hits it covers: the site budget takes the first N
distinct sites, and the hit budget counts the hits at other sites. Run `ctest --preset dev --no-tests=error -R "^startup_debug"`. One of them fires about ten times a second in the texture-streaming path
(a chunked read whose length ends inside a compressed chunk); its cause is open.
- **Struct-returning methods.** When both register arguments of a wrapped method look like objects,
the one on the calling thread's stack is the return buffer.
Expand Down
38 changes: 38 additions & 0 deletions tests/startup_debug_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
ends the run as the timeout does; one left from an earlier run is removed at start. The launcher keeps the debug heap off unless
the caller sets _NO_DEBUG_HEAP=0. X2W_NOP_SOFT_ASSERTS=1 replaces a main-image INT3 with a NOP after
the first continue, and the probe repeats one site when STARTUP_DEBUG_PROBE_ASSERTS is set.
--assert-sites N steps over every hit at up to N distinct main-image sites, however often each
repeats, and stops at the first hit of one more site.
With both budgets, each counts only the hits it covers (hits A, B, A with --assert-sites 1
--continue-asserts 2 all continue).
tests/startup_debug_probe.cpp is the main input: it hits two breakpoints, optionally repeats a
third, reserves 256 MiB, prints its debug-heap flags, waits 3 s and exits 0.

Expand Down Expand Up @@ -212,6 +216,40 @@ def stack_read(address, size):
assert "process exited with 0x00000000" in out, out
assert breakpoint_sites(logdir, image).count(repeated[0]) == repeats, text(logdir / "events.txt")

# --assert-sites 3: every hit at the three sites is stepped over, however often the third
# repeats, with no hit budget at all.
many = 20
out, logdir = run(probe, work, "assert-sites", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)},
options=("--assert-sites", "3"))
assert "process exited with 0x00000000" in out, out
assert f"debug-probe repeated {many}\n" in text(logdir / "stdout.txt"), text(logdir / "stdout.txt")
assert breakpoint_sites(logdir, image).count(repeated[0]) == many, text(logdir / "events.txt")
# Control: a hit budget of three runs out at the repeated site's second hit.
out, _ = run(probe, work, "assert-sites-control", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)},
options=("--continue-asserts", "3"))
assert f"stopped at breakpoint (0x80000003) at {image}+{repeated[0]:#x}" in out, out
# Two sites: the first hit at the third site stops the run.
out, logdir = run(probe, work, "assert-sites-two", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)},
options=("--assert-sites", "2"))
assert f"stopped at breakpoint (0x80000003) at {image}+{repeated[0]:#x}" in out, out
assert breakpoint_sites(logdir, image).count(repeated[0]) == 1, text(logdir / "events.txt")
# X2W_STOP_AT still stops at a site the site budget covers.
out, _ = run(probe, work, "assert-sites-stop", {"X2W_STOP_AT": f"{sites[1]:x}"},
options=("--assert-sites", "3"))
assert f"stopped at breakpoint (0x80000003) at {image}+{sites[1]:#x}" in out, out
# Both budgets: the hits A, B, A with --assert-sites 1 --continue-asserts 2. The site budget
# covers A, the hit budget B, and A's second hit is still A, so the probe runs to its end.
out, logdir = run(probe, work, "assert-sites-combined", {"STARTUP_DEBUG_PROBE_REVISIT": "1"},
options=("--assert-sites", "1", "--continue-asserts", "2"))
assert "process exited with 0x00000000" in out, out
assert breakpoint_sites(logdir, image) == [sites[0], sites[1], sites[0]], text(logdir / "events.txt")
# Control: the hit budget alone runs out at A's second hit, so the site budget is what continues it.
out, _ = run(probe, work, "assert-sites-combined-control", {"STARTUP_DEBUG_PROBE_REVISIT": "1"},
options=("--continue-asserts", "2"))
assert f"stopped at breakpoint (0x80000003) at {image}+{sites[0]:#x}" in out, out
out, _ = run(probe, work, "assert-sites-negative", {}, options=("--assert-sites", "-1"))
assert "expected an --assert-sites value of 0 or greater" in out, out

# X2W_DUMP_AT: a dump one second in, while the probe is still alive.
out, logdir = run(probe, work, "dump", {"X2W_DUMP_AT": "1"})
assert "process exited with 0x00000000" in out, out
Expand Down
6 changes: 5 additions & 1 deletion tests/startup_debug_probe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Input for the startup debugger's diagnostics check (tests/startup_debug_check.py). It reaches two
// breakpoints in its own image, the way a retail image keeps soft asserts, and with
// STARTUP_DEBUG_PROBE_ASSERTS=<n> n more at a third site (the repeated-assert case). It also
// STARTUP_DEBUG_PROBE_ASSERTS=<n> n more at a third site (the repeated-assert case). With
// STARTUP_DEBUG_PROBE_REVISIT=1 it reaches the first breakpoint once more after the second. It also
// reserves one large range, reports whether the loader gave it the debug heap, stays alive for a
// moment and exits 0.
// With STARTUP_DEBUG_PROBE_DLL=<file> and STARTUP_DEBUG_PROBE_EXPORT=<name> it also loads that DLL
Expand Down Expand Up @@ -145,6 +146,9 @@ int main()
{
first_assert();
second_assert();
if (const char* const revisit = std::getenv("STARTUP_DEBUG_PROBE_REVISIT"); revisit && *revisit == '1') {
first_assert();
}
const char* const repeated = std::getenv("STARTUP_DEBUG_PROBE_ASSERTS");
const int repeated_count = repeated ? std::atoi(repeated) : 0;
repeated_assert(repeated_count);
Expand Down
4 changes: 2 additions & 2 deletions tests/startup_run_ps1_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
command = [shell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(startup / "run.ps1"),
"-Exe", str(exe), "-DataDir", str(data), "-LogDir", str(log),
"-BuildDir", build_arg,
"-Seconds", "9", "-MemoryGiB", "3", "-ContinueAsserts", "4", *switches]
"-Seconds", "9", "-MemoryGiB", "3", "-ContinueAsserts", "4", "-AssertSites", "2", *switches]
sdk = root / "dxc"
if native:
sdk.mkdir()
Expand Down Expand Up @@ -272,7 +272,7 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(),
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]
"--trace-files", "1", "--continue-asserts", "4", "--assert-sites", "2", *passed]
record = startup / "debugger.json"
got = json.loads(record.read_text(encoding="utf-8")) if record.is_file() else None
expected = [want, "1" if "-TraceApi" in switches else trace_api_env,
Expand Down
4 changes: 2 additions & 2 deletions tests/startup_run_sh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def run_case(script, code, work, switches=(), passed=(), env_extra=None, drive_d
log = data / "logs"
command = ["bash", str(startup / "run.sh"), "--exe", str(exe), "--data", "G:\\" if drive_data else str(data),
"--log", log_arg, "--build-dir", "bld", "--seconds", "9", "--memory-gib", "3",
"--continue-asserts", "4", *switches]
"--continue-asserts", "4", "--assert-sites", "2", *switches]
if translate:
command += ["--render", "--translate", "--shader-cache", "cache"]
(startup / "translate.flag").write_bytes(b"")
Expand Down Expand Up @@ -292,7 +292,7 @@ def run_case(script, code, work, switches=(), passed=(), env_extra=None, drive_d
if missing:
found.append(f"{label}: stand-in DLLs not copied next to the executable: {missing}")
want = [win(exe), "G:\\" if drive_data else win(data), log_win, "--timeout", "9", "--memory-gib", "3",
"--trace-files", "1", "--continue-asserts", "4", *passed]
"--trace-files", "1", "--continue-asserts", "4", "--assert-sites", "2", *passed]
got = read_json(startup / "argv.json")
if got != want:
found.append(f"{label}: the debugger received {got}, expected {want}")
Expand Down
24 changes: 22 additions & 2 deletions tools/startup/debug_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

usage: debug_launch.py <exe> <cwd> <logdir> [--timeout S] [--memory-gib N] [--stop-first-chance 1]
[--suspend-others 0] [--capture-every S]
[--continue-asserts N] [--assert-sites N]

Records module loads, every exception (code, address as module+offset, first/second chance) and, for
the stopping exception, the integer registers, the stack words and the code bytes around RIP.
Stops at a second-chance exception, an uncontinued main-image breakpoint, or the timeout.
A timeout of zero disables the timer. An assertion continuation limit of -1 removes that limit.
--continue-asserts N steps over the first N main-image int3 hits in total. --assert-sites N instead
steps over every hit at up to N distinct main-image int3 sites, so a site that repeats does not use
the budget up however long the run lasts; the first hit at one more site stops the run. A hit is
stepped over when either budget allows it.
A file named `stop` created in <logdir> during the run ends it as the timeout does (checked twice a
second; a stop file left from an earlier run is removed at start).
"""
Expand Down Expand Up @@ -131,8 +136,10 @@ def print(*args, **kwargs):
timeout = float(opts.get("--timeout", 15)) # zero leaves the session running until exit or a debug stop
memory_gib = float(opts.get("--memory-gib", 16))
skip_asserts = int(opts.get("--continue-asserts", "0")) # -1 continues all diagnostic assertions
assert_sites = int(opts.get("--assert-sites", "0")) # distinct sites whose every hit is continued
except ValueError:
print("expected numeric --timeout and --memory-gib values and an integer --continue-asserts value")
print("expected numeric --timeout and --memory-gib values and integer --continue-asserts and "
"--assert-sites values")
return 2
stop_first = opts.get("--stop-first-chance") == "1"
try:
Expand All @@ -145,12 +152,20 @@ def print(*args, **kwargs):
if timeout < 0 or not timeout < float("inf") or skip_asserts < -1:
print("expected a finite nonnegative timeout and an assertion limit of -1 or greater")
return 2
if assert_sites < 0:
print("expected an --assert-sites value of 0 or greater")
return 2
# The job always sets JOB_MEMORY (0x200), so a limit of 0 would deny the process any commit.
if not 1 <= memory_gib * 2**30 < 2**(8 * ctypes.sizeof(ctypes.c_size_t)):
print("expected a finite --memory-gib that is at least one byte and fits the Windows size_t limit")
return 2
memory = int(memory_gib * 2**30)
asserts_seen = {}
# The two assert budgets keep their own books, so a hit one of them covers never uses up the other:
# assert_sites_admitted holds the sites --assert-sites covers (the first assert_sites distinct ones),
# asserts_counted the hits --continue-asserts stepped over.
assert_sites_admitted = set()
asserts_counted = 0
# X2W_NOP_SOFT_ASSERTS=1: after the first hit of a main-image int3 that is continued, replace the
# trap byte with NOP. DBG_CONTINUE already bypasses the title's handler, so the visible behavior is
# unchanged, but a long mapping that traps once per chunk then runs at native speed.
Expand Down Expand Up @@ -1053,8 +1068,13 @@ def capture_loop():
log(f"X2W_LOG_AT ignored {rva:#x}: cannot arm a main-image instruction breakpoint")
del log_at[rva]
elif (ex_code == 0x80000003 and first and main_base and main_base <= ex_addr < main_base + 0x10000000
and (skip_asserts == -1 or sum(asserts_seen.values()) < skip_asserts)
and (ex_addr in assert_sites_admitted or len(assert_sites_admitted) < assert_sites
or skip_asserts == -1 or asserts_counted < skip_asserts)
and (ex_addr - main_base) not in stop_at):
if ex_addr in assert_sites_admitted or len(assert_sites_admitted) < assert_sites:
assert_sites_admitted.add(ex_addr)
else:
asserts_counted += 1
asserts_seen[ex_addr] = asserts_seen.get(ex_addr, 0) + 1
if nop_soft_asserts and ex_addr not in nop_sites and read(ex_addr, 1) == b"\xcc" and write(ex_addr, b"\x90"):
nop_sites[ex_addr] = 1
Expand Down
3 changes: 2 additions & 1 deletion tools/startup/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ param(
[string]$BuildDir = "out/build/release",
[int]$MemoryGiB = 14,
[int]$ContinueAsserts = 0, # main-image int3s to log and step over (title soft asserts)
[int]$AssertSites = 0, # distinct main-image int3 sites whose every hit is stepped over
[string]$InputScript = "", # X2W_INPUT_SCRIPT, e.g. "4@70" = A at 70 s
[string]$MapDrive = "", # e.g. "G=D:\package": rewrite opens on a console letter
[string]$StoragePath = "", # X2W_STORAGE_PATH: root of the console drive the title sees (it appends x2w-gamesave)
Expand Down Expand Up @@ -113,7 +114,7 @@ try {
& {
$ErrorActionPreference = "Continue"
& $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB `
--trace-files 1 --continue-asserts $ContinueAsserts @mapArgs @traceArgs @captureArgs
--trace-files 1 --continue-asserts $ContinueAsserts --assert-sites $AssertSites @mapArgs @traceArgs @captureArgs
$script:debuggerExit = $LASTEXITCODE
} *> "$LogDir.out"
} finally {
Expand Down
9 changes: 5 additions & 4 deletions tools/startup/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ set -uo pipefail

usage() {
echo "usage: run.sh --exe EXE --data DIR --log DIR [--seconds 120] [--build-dir out/build/release]" >&2
echo " [--memory-gib 14] [--continue-asserts 0] [--input-script S] [--storage DIR]" >&2
echo " [--memory-gib 14] [--continue-asserts 0] [--assert-sites 0] [--input-script S] [--storage DIR]" >&2
echo " [--shader-cache out/startup/shader_cache] [--console 0|1] [--render] [--fake-user]" >&2
echo " [--gpu-mirror] [--host-cpu] [--translate] [--startup-below-40] [--trace-calls]" >&2
echo " [--hide-title-window] [--capture-every S] [--fresh-shader-cache]" >&2
Expand All @@ -36,18 +36,19 @@ usage() {

repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
here="$repo/tools/startup"
exe="" data="" log="" seconds=120 build_dir=out/build/release memory_gib=14 continue_asserts=0
exe="" data="" log="" seconds=120 build_dir=out/build/release memory_gib=14 continue_asserts=0 assert_sites=0
input_script="" storage="" shader_cache=out/startup/shader_cache console=1 capture_every=0
render="" fake_user="" gpu_mirror="" host_cpu="" translate="" below_40="" trace_calls="" hide_title="" fresh_cache=""
while [ $# -gt 0 ]; do
case $1 in
--exe|--data|--log|--seconds|--build-dir|--memory-gib|--continue-asserts|--input-script|--storage|\
--exe|--data|--log|--seconds|--build-dir|--memory-gib|--continue-asserts|--assert-sites|--input-script|--storage|\
--shader-cache|--console|--capture-every)
[ $# -ge 2 ] || usage
case $1 in
--exe) exe=$2 ;; --data) data=$2 ;; --log) log=$2 ;; --seconds) seconds=$2 ;;
--build-dir) build_dir=$2 ;; --memory-gib) memory_gib=$2 ;;
--continue-asserts) continue_asserts=$2 ;; # main-image int3s to log and step over
--assert-sites) assert_sites=$2 ;; # distinct int3 sites whose every hit is stepped over
--input-script) input_script=$2 ;; # X2W_INPUT_SCRIPT, e.g. "4@70" = A at 70 s
--storage) storage=$2 ;; # X2W_STORAGE_PATH: persistent local storage root
--shader-cache) shader_cache=$2 ;; --console) console=$2 ;;
Expand Down Expand Up @@ -229,7 +230,7 @@ capture_args=()
[ "$capture_every" = 0 ] || capture_args=(--capture-every "$capture_every")
"$wine" "$X2W_WIN_PYTHON" -B "$(win_path "$here/debug_launch.py")" "$(win_path "$exe")" "$(win_path "$data")" \
"$(win_path "$log")" --timeout "$seconds" --memory-gib "$memory_gib" --trace-files 1 \
--continue-asserts "$continue_asserts" "${trace_args[@]}" "${console_args[@]}" "${capture_args[@]}" > "$log_host.out" 2>&1
--continue-asserts "$continue_asserts" --assert-sites "$assert_sites" "${trace_args[@]}" "${console_args[@]}" "${capture_args[@]}" > "$log_host.out" 2>&1
debugger_exit=$?
if [ -n "$translate" ] && [ -n "$render" ] && [ -z "$revision_warned" ]; then check_cache_revision; fi
head -n 5 "$log_host.out"
Expand Down
Loading