tooling: stop at a chosen assert, dump threads mid-run and log large allocations in the startup debugger #1292

Merged
JohnConnorNPC merged 5 commits from upstream-pr/1299 into main 2026-11-24 10:15:41 +00:00
JohnConnorNPC commented 2026-11-24 07:17:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-24T07:17:47Z.


Refs #870.

Problem

When a startup run under tools/startup/debug_launch.py goes wrong some time before it stops, the stop report describes the last fault, not the cause. Three questions kept coming up and had no switch:

  • Which soft assert came first, and what were all threads doing at that moment? -ContinueAsserts steps over every main-image breakpoint, so the run only stops later, at the fault the assert led to.
  • What is a stalled run doing before a watchdog or the timeout ends it? Thread stacks are only written at the timeout.
  • Who made a large allocation? The stand-ins log their own entry points, but not allocations made through other paths, and not the caller.

Change

Three environment switches, all off by default, read by debug_launch.py:

Switch Effect
X2W_STOP_AT=<rva>[,...] Stop at a main-image breakpoint at one of these addresses even while --continue-asserts would step over it, and write every thread's stack to threads_stop.txt.
X2W_DUMP_AT=<seconds>[,...] Write every thread's stack to threads_<seconds>.txt at those times.
X2W_VALLOC_MIN=<hex bytes> Log each NtAllocateVirtualMemory / NtAllocateVirtualMemoryEx call of at least that size to files.txt as VALLOC, with base, size, type, protection and the main-image and stand-in return addresses found on the caller's stack. Needs the file hooks (on by default with --offline 1).

They reuse what the debugger already has: dump_threads, the breakpoint hooks on ntdll and the existing caller scan. Nothing changes when they are unset.

Evidence

Commit under test: the head of this branch, on main at 93cb6a2a.

New tests/startup_debug_check.py, registered as startup_debug, runs the debugger on a new source-built probe (tests/startup_debug_probe.cpp: two __debugbreak() sites in its own image, a 256 MiB MEM_RESERVE, a 3 s wait, exit 0):

startup_debug: PASS (stop at a chosen assert, timed thread dump, large-allocation log; 4 controls)

What it requires:

  • Baseline with --continue-asserts 10: both sites are stepped over, exit code 0, no threads_stop.txt, no VALLOC line. The two site addresses are read from events.txt, not hard-coded.
  • X2W_STOP_AT=<second site>: the first site is still stepped over, the run stops at the second (first chance), and threads_stop.txt names the probe image.
  • Controls: an address that is not a breakpoint site stops nothing; without --continue-asserts the run stops at the first site as before.
  • X2W_DUMP_AT=1: threads_1.txt exists and holds stacks; the baseline has none.
  • X2W_VALLOC_MIN=10000000: exactly one line with size=0x10000000 and type=0x2000, and a caller in the probe image. Control: X2W_VALLOC_MIN=20000000 logs nothing.

Run on: CPython 3.12 for Windows under Wine 11.0 (Proton Experimental) on a Linux host, with the probe built by clang-cl against the Windows SDK. That is a translation-layer result, not Windows.

Also ran python -B tests/docs_links_check.py (pass) and git diff --check (clean).

In use: these three switches are what located the cause behind #1290 (the first failing assert and its stack, then the one fixed-size block the executable reserves at start-up and who reserved it).

Not measured

  • Windows. I have no Windows host; CI is the first Windows run of the probe build and of the check. One thing I could not verify there: the check assumes VirtualAlloc of 256 MiB reaches NtAllocateVirtualMemory with that exact size, which holds under Wine.
  • The cost of X2W_VALLOC_MIN on a long run (every allocation call is a debugger round trip) is not measured; it is meant for short diagnostic runs.

Overlap

#1274, #1251, #1280 and #1229 also touch debug_launch.py, in other places. Whichever lands later will need a small rebase.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6576706112 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1299). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-24T07:17:47Z. --- Refs #870. ## Problem When a startup run under `tools/startup/debug_launch.py` goes wrong some time before it stops, the stop report describes the last fault, not the cause. Three questions kept coming up and had no switch: - Which soft assert came first, and what were all threads doing at that moment? `-ContinueAsserts` steps over every main-image breakpoint, so the run only stops later, at the fault the assert led to. - What is a stalled run doing before a watchdog or the timeout ends it? Thread stacks are only written at the timeout. - Who made a large allocation? The stand-ins log their own entry points, but not allocations made through other paths, and not the caller. ## Change Three environment switches, all off by default, read by `debug_launch.py`: | Switch | Effect | |---|---| | `X2W_STOP_AT=<rva>[,...]` | Stop at a main-image breakpoint at one of these addresses even while `--continue-asserts` would step over it, and write every thread's stack to `threads_stop.txt`. | | `X2W_DUMP_AT=<seconds>[,...]` | Write every thread's stack to `threads_<seconds>.txt` at those times. | | `X2W_VALLOC_MIN=<hex bytes>` | Log each `NtAllocateVirtualMemory` / `NtAllocateVirtualMemoryEx` call of at least that size to `files.txt` as `VALLOC`, with base, size, type, protection and the main-image and stand-in return addresses found on the caller's stack. Needs the file hooks (on by default with `--offline 1`). | They reuse what the debugger already has: `dump_threads`, the breakpoint hooks on ntdll and the existing caller scan. Nothing changes when they are unset. ## Evidence Commit under test: the head of this branch, on `main` at `93cb6a2a`. New `tests/startup_debug_check.py`, registered as `startup_debug`, runs the debugger on a new source-built probe (`tests/startup_debug_probe.cpp`: two `__debugbreak()` sites in its own image, a 256 MiB `MEM_RESERVE`, a 3 s wait, exit 0): ``` startup_debug: PASS (stop at a chosen assert, timed thread dump, large-allocation log; 4 controls) ``` What it requires: - Baseline with `--continue-asserts 10`: both sites are stepped over, exit code 0, no `threads_stop.txt`, no `VALLOC` line. The two site addresses are read from `events.txt`, not hard-coded. - `X2W_STOP_AT=<second site>`: the first site is still stepped over, the run stops at the second (first chance), and `threads_stop.txt` names the probe image. - Controls: an address that is not a breakpoint site stops nothing; without `--continue-asserts` the run stops at the first site as before. - `X2W_DUMP_AT=1`: `threads_1.txt` exists and holds stacks; the baseline has none. - `X2W_VALLOC_MIN=10000000`: exactly one line with `size=0x10000000` and `type=0x2000`, and a caller in the probe image. Control: `X2W_VALLOC_MIN=20000000` logs nothing. Run on: CPython 3.12 for Windows under Wine 11.0 (Proton Experimental) on a Linux host, with the probe built by clang-cl against the Windows SDK. That is a translation-layer result, not Windows. Also ran `python -B tests/docs_links_check.py` (pass) and `git diff --check` (clean). In use: these three switches are what located the cause behind #1290 (the first failing assert and its stack, then the one fixed-size block the executable reserves at start-up and who reserved it). ## Not measured - **Windows.** I have no Windows host; CI is the first Windows run of the probe build and of the check. One thing I could not verify there: the check assumes `VirtualAlloc` of 256 MiB reaches `NtAllocateVirtualMemory` with that exact size, which holds under Wine. - The cost of `X2W_VALLOC_MIN` on a long run (every allocation call is a debugger round trip) is not measured; it is meant for short diagnostic runs. ## Overlap #1274, #1251, #1280 and #1229 also touch `debug_launch.py`, in other places. Whichever lands later will need a small rebase.
tedin7 commented 2026-11-24 08:01:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T07:28:35Z.

Review: COMMENTED


Dual-platform review at commit 8cccacdf737f000c4d2bd4d53860c0a66cb7bd95:

Verification

  • Linux:
    • git diff --check origin/main...HEAD: clean (exit 0)
    • /root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: passed (16 Markdown files, 286 links)
    • /root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: passed (1081 tracked files)
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • Debug (--preset dev):
      • cmake --build --preset dev --target startup_debug_probe -- -j 6 (3.61s)
      • ctest --preset dev -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure: 1/1 passed (15.74s: startup_debug 15.68s)
    • Release /O2 (--preset release):
      • cmake --build --preset release --target startup_debug_probe -- -j 6 (0.98s)
      • ctest --preset release -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure: 1/1 passed (15.72s: startup_debug 15.66s)

Code Review Notes

  • tools/startup/debug_launch.py:
    • NtAllocateVirtualMemory / NtAllocateVirtualMemoryEx register/stack argument decoding (Rdx -> &base, R9/R8 -> &size, [rsp+0x28] / R9 -> type, [rsp+0x30] / [rsp+0x28] -> protect) matches the x64 Windows syscall ABI.
    • X2W_STOP_AT, X2W_DUMP_AT, and X2W_VALLOC_MIN are cleanly integrated with tests/startup_debug_check.py covering positive and negative controls (non-matching RVA, no --continue-asserts, and higher X2W_VALLOC_MIN threshold).
    • Minor edge cases (non-blocking):
      1. int(os.environ.get("X2W_VALLOC_MIN", "0"), 16) will raise ValueError if X2W_VALLOC_MIN="" is set in the environment (or "0" would guard against an empty string).
      2. base in VALLOC logs the input *BaseAddress at syscall entry (which is 0x0 for VirtualAlloc(nullptr, ...)), rather than the returned allocation base.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6035265674 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1299#pullrequestreview-6035265674). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T07:28:35Z. **Review:** COMMENTED --- Dual-platform review at commit `8cccacdf737f000c4d2bd4d53860c0a66cb7bd95`: ### Verification - **Linux**: - `git diff --check origin/main...HEAD`: clean (exit 0) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py`: passed (16 Markdown files, 286 links) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py`: passed (1081 tracked files) - **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)**: - **Debug (`--preset dev`)**: - `cmake --build --preset dev --target startup_debug_probe -- -j 6` (`3.61s`) - `ctest --preset dev -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure`: **1/1 passed** (`15.74s`: `startup_debug` `15.68s`) - **Release `/O2` (`--preset release`)**: - `cmake --build --preset release --target startup_debug_probe -- -j 6` (`0.98s`) - `ctest --preset release -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure`: **1/1 passed** (`15.72s`: `startup_debug` `15.66s`) ### Code Review Notes - `tools/startup/debug_launch.py`: - `NtAllocateVirtualMemory` / `NtAllocateVirtualMemoryEx` register/stack argument decoding (`Rdx` -> `&base`, `R9`/`R8` -> `&size`, `[rsp+0x28]` / `R9` -> `type`, `[rsp+0x30]` / `[rsp+0x28]` -> `protect`) matches the x64 Windows syscall ABI. - `X2W_STOP_AT`, `X2W_DUMP_AT`, and `X2W_VALLOC_MIN` are cleanly integrated with `tests/startup_debug_check.py` covering positive and negative controls (non-matching RVA, no `--continue-asserts`, and higher `X2W_VALLOC_MIN` threshold). - Minor edge cases (non-blocking): 1. `int(os.environ.get("X2W_VALLOC_MIN", "0"), 16)` will raise `ValueError` if `X2W_VALLOC_MIN=""` is set in the environment (`or "0"` would guard against an empty string). 2. `base` in `VALLOC` logs the input `*BaseAddress` at syscall entry (which is `0x0` for `VirtualAlloc(nullptr, ...)`), rather than the returned allocation base.
tedin7 commented 2026-11-24 08:01:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T07:55:15Z.

Review: COMMENTED


Retest & dual-platform review at commit c0aed1de4dc128b55007e32117b218fb8a223a05 (since 8cccacdf):

Checks

  • Linux:
    • git diff --check origin/main...HEAD: passed
    • tests/docs_links_check.py: passed (16 Markdown files, 290 links)
    • tests/bidi_controls_check.py: passed (1095 tracked files)
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • Debug (--preset dev): cmake --build --preset dev --target startup_debug_probe -- -j 6 (2.75s), ctest --preset dev -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure: 1/1 passed (18.77s)
    • Release /O2 (--preset release): cmake --build --preset release --target startup_debug_probe -- -j 6 (0.25s), ctest --preset release -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure: 1/1 passed (18.74s)

Verification of Prior Notes (8cccacdf)

  1. Empty X2W_VALLOC_MIN="" guard (tools/startup/debug_launch.py): Fixed — int(os.environ.get("X2W_VALLOC_MIN") or "0", 16) handles empty strings cleanly, and tests/startup_debug_check.py now includes an explicit valloc-empty control.
  2. Entry-point address field naming (tools/startup/debug_launch.py, docs/startup-runtime.md): Fixed — renamed to requested= and documented as the requested address on syscall entry.

No remaining issues found.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6035539334 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1299#pullrequestreview-6035539334). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T07:55:15Z. **Review:** COMMENTED --- Retest & dual-platform review at commit `c0aed1de4dc128b55007e32117b218fb8a223a05` (since `8cccacdf`): ### Checks - **Linux**: - `git diff --check origin/main...HEAD`: passed - `tests/docs_links_check.py`: passed (16 Markdown files, 290 links) - `tests/bidi_controls_check.py`: passed (1095 tracked files) - **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)**: - **Debug (`--preset dev`)**: `cmake --build --preset dev --target startup_debug_probe -- -j 6` (`2.75s`), `ctest --preset dev -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure`: **1/1 passed** (`18.77s`) - **Release `/O2` (`--preset release`)**: `cmake --build --preset release --target startup_debug_probe -- -j 6` (`0.25s`), `ctest --preset release -R '^startup_debug$' --parallel 4 --no-tests=error --output-on-failure`: **1/1 passed** (`18.74s`) ### Verification of Prior Notes (`8cccacdf`) 1. **Empty `X2W_VALLOC_MIN=""` guard (`tools/startup/debug_launch.py`)**: **Fixed** — `int(os.environ.get("X2W_VALLOC_MIN") or "0", 16)` handles empty strings cleanly, and `tests/startup_debug_check.py` now includes an explicit `valloc-empty` control. 2. **Entry-point address field naming (`tools/startup/debug_launch.py`, `docs/startup-runtime.md`)**: **Fixed** — renamed to `requested=` and documented as the requested address on syscall entry. No remaining issues found.
upstream-sync closed this pull request 2026-11-24 11:02:35 +00:00
R0Y00 commented 2026-11-24 11:02:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:13:57Z.

Review: APPROVED


Retested the updated launcher on Windows x64 using the source-built startup debug probe. The diagnostic and five controls pass, the short pointer fixture covers all incomplete widths, and the host CPU regression passes with 112 native SHA results. The documentation check resolved 313 links.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6037344494 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1299#pullrequestreview-6037344494). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T10:13:57Z. **Review:** APPROVED --- Retested the updated launcher on Windows x64 using the source-built startup debug probe. The diagnostic and five controls pass, the short pointer fixture covers all incomplete widths, and the host CPU regression passes with 112 native SHA results. The documentation check resolved 313 links.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1292
No description provided.