Skip to content

runtime, d3d12: both runtimes write an import call trace, and a tool compares two (RT-CALL-TRACE, D3D12-CALL-TRACE) - #2807

Open
ubiquitarianism wants to merge 9 commits into
mainfrom
rt-call-trace
Open

ubiquitarianism wants to merge 9 commits into
mainfrom
rt-call-trace

Conversation

@ubiquitarianism

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: RT-CALL-TRACE and D3D12-CALL-TRACE (#2570 (comment))

What and why

When gta6rt and leonida.exe stop at different places in the boot, there is no record of which import call they first handled differently. With LEONIDA_CALL_TRACE=<new file> set, both runtimes now record every call through the import tables of the title's image and its DLLs (normal and delay-load) in one shared v1 text format. Unset, nothing changes: wrap returns the import's address and no thunk block is allocated.

  • tools/call_trace_rules.def: the rules both runtimes read (gta6rt includes it, leonida-loader parses it), so the two traces are comparable. Stack walks and RtlCaptureContext2 are entry-only (a wrap frame would end the title's stack walk at the stub), and __vdecl vector math is skipped (its YMM arguments are not kept by the stubs).
  • Each import slot gets a 24-byte thunk to one of two common stubs: entry-only (E line, real return address) or wrapped (C and R lines with the return class). The stubs are registered for unwinding; the entry stub's unwind range ends before its final jump.
  • The file is written through 4 MiB mapped views, capped by LEONIDA_CALL_TRACE_MAX_MB (default 4096). The writer lock is taken with a bound of about 10 ms; a record that cannot get it is dropped and the next one writes # dropped N.
  • gta6rt shims read their caller through RT_CALLER(), which sees through the wrap frame.
  • tools/call_trace_diff.py pairs the two traces' threads (main first, then by start address and common call prefix), aligns each pair's calls and reports the first divergences: calls one side never makes, different return classes, different call counts, threads on one side only and calls still open at the end. It streams its input (two synthetic 1M-call traces: 14.0 s, 336 MB peak).

Not run in game.

Tested

Windows 11 (26200), Visual Studio 18 (2026) MSVC, AMD Radeon RX 6800 (driver 32.0.21030.2001), rebased on main at 66b5d0433:

  • cargo test --profile diag -p leonida-loader --lib -- call_trace: 29 passed
  • runtime RelWithDebInfo build; ctest: 205 of 205 passed, 2 disabled (gameinput, gameinput_script, no SDL3 here)
  • python -I tests/test_call_trace_diff.py, tests/test_call_trace_rules.py: OK; python -m compileall -q tools tests: OK
  • scripts/check_no_game_data.py on the changed files: OK

One full ctest -j 8 run failed call_trace (and the unrelated windows_crash_monitor); both passed alone and in the next full -j 8 run. case_dropped_record checks that a dropped call takes under 100 ms of wall time, which a machine running eight test jobs can exceed. The failing run's output was not kept, so that cause is likely, not confirmed.

Rules both runtimes read (gta6rt includes it, leonida-loader parses it) so their
LEONIDA_CALL_TRACE traces are comparable. Not run in game.
Reads the LEONIDA_CALL_TRACE v1 traces of gta6rt and leonida.exe, pairs threads
(main; then by start address and common call prefix), aligns each pair's calls
and reports the first divergences: calls one side never makes, different return
classes, different call counts, threads on one side only, and the calls still
open at the end of each thread. Streams the input (about 150 bytes per call).

Tested: python -I tests/test_call_trace_diff.py (Ran 76 tests, OK);
tests/test_text_write_line_endings.py OK; two synthetic 1M-call traces:
14.0 s / 336 MB peak. Not run in game.
…ALL_TRACE)

Every IAT slot of the title image and its DLLs (normal and delay) is traced by
import identity when LEONIDA_CALL_TRACE=<new file> is set (size cap
LEONIDA_CALL_TRACE_MAX_MB, default 4096); unset, wrap returns the address
unchanged and no thunk block is allocated. Per-import 24-byte thunk into the
common stub (entry-only E / wrapped C+R), writer and rules as in
tools/call_trace_rules.def (hash in the header). New leonida-loader call_trace
module with 23 tests (stub bytes against their assembler source, 20 integer
args, floats, struct return, last error, panic through the stubs, nesting,
classes, 4 threads across a view boundary, killed child, size cap).

Tested: cargo test -p leonida-loader: 96 + 2 + 1 passed; cargo test -p leonida-hle:
211 passed, 2 ignored; build -p leonida --profile diag --features diagnostics,mimalloc
ok. Not run in game.
LEONIDA_CALL_TRACE=<path> (cap LEONIDA_CALL_TRACE_MAX_MB, default 4096) records every call through the import
tables of the main image and the title DLLs in the shared v1 format, with the rules of tools/call_trace_rules.def.
Each import slot gets a 24-byte thunk to one of two common stubs (entry-only: jump, real return address; wrap: frame,
C and R lines), registered with RtlAddFunctionTable. The file is created new, written through 4 MiB mapped views.
Shims read their caller through RT_CALLER(), which sees through the wrap frame; unset, wrap() returns its target.
runtime/test/call_trace_test.cpp (ctest call_trace) and tests/test_call_trace_rules.py cover it.

Tested: full runtime build (RelWithDebInfo) passes; ctest on the 149 non-GPU tests that cannot open a Vulkan device or window: 100% tests passed, 0 tests failed out of 149; python tests of the rules, line endings, docs env vars pass. Not run in game.
…s, unset-switch no-ops)

Tested: cargo test -p leonida-loader: 99 + 2 + 1 passed; cargo test -p leonida-hle:
211 passed, 2 ignored; build -p leonida --profile diag --features diagnostics,mimalloc
ok. Not run in game.
…skip __vdecl math

From the second review: a wrap frame would end the title's stack walks at the stub
and SVML vector math passes arguments in YMM registers the stubs do not keep.
Not run in game.
Tested: Ran 79 tests in 22.512s ... OK (test_call_trace_diff.py); Ran 3 tests in 15.268s ... OK (test_text_write_line_endings.py). Not run in game.
…ed writer lock)

Tested: cargo test -p leonida-loader: 102 + 2 + 1 passed; cargo test -p leonida-hle:
211 passed, 2 ignored; build -p leonida --profile diag --features diagnostics,mimalloc
ok. Not run in game.
…lock, caller in helpers)

The entry stub's RUNTIME_FUNCTION now ends before its final jmp [r10], so a stack walk there finds the return
address. The writer lock is taken with a 10 ms bound: a dropped record is counted and the next one writes
`# dropped <count>`. The next view is mapped ahead so a failed remap still ends the file in `# truncated`.
title_free_impl and create_file_w take the caller from the exported shim; a failed VirtualProtect deletes the
function table; pe_loader builds the module name only when tracing. Rules: 205, hash 873cd3bb78a70bc5.

Tested: full runtime build passes; ctest on the 149 non-GPU tests: 100% tests passed, 0 tests failed out of 149; xmem alone: 1/1 passed; xs_proxy_mock|xmem_: 100% tests passed, 0 tests failed out of 13; python -I tests/test_call_trace_rules.py OK. Not run in game.
@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, head e2572082c, main b7a1be296. Partial check: the Python tools, plus a Windows type-check of the D3D12 side. leonida-loader does not build natively on macOS (it uses windows_sys and core::arch::x86_64, on main as well), and the runtime side (gta6rt, runtime/shims/call_trace*.h, call_trace_test.cpp) was not built here.

MEASURED

  • git merge-tree onto main gives no conflicts.
  • Python, red (main + only this PR's tests): test_call_trace_diff.py errors at import (tools/call_trace_diff.py doesn't exist on main), and test_call_trace_rules.py fails. Green (main + this PR): test_call_trace_diff.py 79 passed, 42 subtests passed (10.9 s); test_call_trace_rules.py 9 passed.
  • cargo check --target x86_64-pc-windows-msvc (no MSVC; the cc build steps are replaced with stand-ins), main vs main + this PR, exit 0 on both sides. The sorted warning lists are identical: -p leonida-loader --tests 0 / 0 (this compiles call_trace.rs and its 1,474-line test module), -p leonida 19 / 19 (run.rs).

READ IN CODE

  • The switch exists and is opt-in. In d3d12/apps/leonida/src/run.rs, call_trace::open_from_env()? runs only when LEONIDA_CALL_TRACE is set, and add_module is called for the image, the title DLLs and the late-loaded DLLs. With the switch unset, add_module is called but (per the description) wrap returns the import's address unchanged and no thunk block is allocated. I didn't run that path, so "no change when unset" is from the code and your description, not measured here.
  • open_from_env()? propagates an error, so a LEONIDA_CALL_TRACE path that already exists stops the title from starting, as the comment says ("the file must be new").

ASSUMED

  • The Rust unit tests (29), ctest 205/205, and the case_dropped_record flake you describe are from your Windows run. Not reproduced. Not run in game, as you say.

AI-assisted: Claude Code

@fiestorra

Copy link
Copy Markdown
Contributor

Verified in all three layers it touches, on Windows, and none of them fails.

main d920d850d plus this head:

Python (the two suites it adds):

python -m pytest tests/test_call_trace_diff.py tests/test_call_trace_rules.py -q
  88 passed, 42 subtests passed   (25.45 s)

C++:

cmake --build runtime/build --target call_trace_test     exit 0
runtime\build\call_trace_test.exe                        exit 0
  error: LEONIDA_CALL_TRACE: the file already exists; the call trace only creates a new file
  error: LEONIDA_CALL_TRACE_MAX_MB: expected a whole number of MiB from 1 to 1048576
  error: LEONIDA_CALL_TRACE: cannot create the file (error 3)
  call_trace_test: ok

Those three error lines are the test doing its job - it drives the refusal paths and prints them - and call_trace_test: ok is the verdict.

Rust (six files under d3d12/):

cargo test --workspace       exit 101
  ... the workspace's own suites all `ok` (403 passed / 5 ignored in the largest)
  FAILED: xtask policy::tests::repository_passes_policy

That failure is not this pull request's: it is main failing its own asset policy on two files over 640 KiB (leonida-gpu/src/host.rs, leonida-shader/src/emit/lower.rs), which #2815 fixes, and it fails identically on main alone. Everything else in the workspace is green with this change, including the crates it touches.

What I could not test: a real import call trace from a title - that needs the game and the two runtimes the note describes, and this is a 24 GB desktop, not the 8 GB laptop the trace was written for. The diff tool and its rules are what the 88 Python tests cover, and they are green.

One thing worth saying about the size: +6,558 across 23 files is a lot to review, and the shape helps - 795 lines of C++ test, 1,189 of Python tests, 731 of tool, 236 of rules, and the rest is the runtime hooks the notes describe. The tests are two thirds of it, which is the right ratio for a diagnostic that has to be trusted.

AI-assisted: yes - DeepSeek v4.1 (flash), which ran all three layers on the machine above.

Written with an AI assistant.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The tracer is sound. Off costs nothing — wrap returns the target and allocates no thunk (call_trace.h:700) — and on is one atomic per call plus a bounded writer lock, with the line formatted and sequenced inside the lock (:442-520), so lines cannot interleave. The stream is capped (:361, call_trace.rs:796) and goes through 4 MiB mapped views, so RAM does not grow with the trace. Every test I ran passes: call_trace ctest, 79 test_call_trace_diff tests, 9 test_call_trace_rules, 29 Rust call_trace tests. Mutation: removing the return comparison in compare_thread (tools/call_trace_diff.py:519) fails 12 tests, so the diff's core is genuinely pinned.

One unbounded path, and it is a real inconsistency between the two runtimes:

  • call_trace.rs:865 treats an unparsable or 0 LEONIDA_CALL_TRACE_MAX_MB as 4096 silently, and mb.saturating_mul(1 << 20) turns a parseable but huge value into no cap at all. gta6rt refuses both cases with a message and exit 2, and its test asserts that (call_trace.h:577-590). Make the Rust parser match the C++ one, and never let a value saturate to "unbounded".

Two more, in the tool:

  • rules differ, truncated=yes and a missing header only warn and can still exit 0 (test_differing_rules_print_a_warning asserts exactly that). A script reads 0 as "identical", so give those a distinct exit code — a rules mismatch means the traces are not comparable at all. A warning when both sides report the same stack= would also help.
  • case_dropped_record's wall-clock < 100 ms is load-dependent (the body already saw it fail under -j8); assert the drop count and the # dropped ordering, keep the timing as a print.

Documentation worth one line in the README: the trace records identity and the return class, no arguments and no strings — so a divergence that lives only in an argument is invisible to the diff (a hash of the first argument qwords would be safe, unlike strings). And RT_CALLER's 0x140 duplicates the stub's 0x138 frame by hand — a static_assert against the blob would keep them tied.

@Neutron-0

Copy link
Copy Markdown

A heads-up for this pull request: the page of D3D12 switches (d3d12/docs/switches.md) is written by tools/d3d12_switches.py from the sources of d3d12/, and 3 names this pull request reads are not on the page yet: LEONIDA_CALL_TRACE, LEONIDA_CALL_TRACE_MAX_MB, LEONIDA_CT_CHILD.

Nothing checks that today, so the page has drifted before (it was a name behind when this was measured). A test that checks it is waiting to be opened (DOCS-SWITCHES-PAGE-CHECK), and it would make this pull request's CI red until the page is written again. One command does that:

python -I tools/d3d12_switches.py --out d3d12/docs/switches.md

It writes the page with the names in it and nothing else; the diff is the new rows.

@merzleckeier

Copy link
Copy Markdown
Contributor

Confirmed the Rust/C++ trace-cap mismatch under Wine on e2572082c + 6c7f6b60c. The stock C++ call_trace_test passed, as did the 79 diff tests and 9 rule tests. In a separate synthetic Rust child-process regression, a cap of 0 returned Ok(true) instead of refusing the setting before creating the trace. A local change to validate the configured range and format before opening the file made the new test pass for 0, 12x, 1048577 and whitespace-prefixed 1. All 31 Rust call_trace::tests passed in that build, including the existing 1 MiB truncation check. I used temporary trace files only.

AI-assisted with OpenAI Codex.

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.

6 participants