Skip to content

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

Merged
R0Y00 merged 5 commits into
R0Y00:mainfrom
JohnConnorNPC:tooling/startup-debugger-stop-dump-valloc
Nov 24, 2026
Merged

R0Y00 merged 5 commits into
R0Y00:mainfrom
JohnConnorNPC:tooling/startup-debugger-stop-dump-valloc

Conversation

@JohnConnorNPC

@JohnConnorNPC JohnConnorNPC commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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.

…allocations in the startup debugger

X2W_STOP_AT stops at a main-image breakpoint at a given address while
--continue-asserts steps over the others and writes threads_stop.txt.
X2W_DUMP_AT writes every thread's stack at given times during the run.
X2W_VALLOC_MIN logs virtual-memory allocations of at least a given size
with the main-image and stand-in callers found on the stack.

tests/startup_debug_check.py runs the debugger on a source-built probe
that hits two breakpoints, reserves 256 MiB and waits.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

…s requested

Review: an empty value raised ValueError, and the address logged at
entry is the one the caller asked for, not the one returned.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 enabled auto-merge November 24, 2026 10:15
@R0Y00
R0Y00 merged commit 5084945 into R0Y00:main Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants