Skip to content

Runtime: opt-in startup trace (gta6rt --trace FILE) (RT-29) - #1756

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
leozinhoszg:rt-startup-trace
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
leozinhoszg:rt-startup-trace

Conversation

@leozinhoszg

@leozinhoszg leozinhoszg commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-29 (claim: #634 (comment))

What and why

Nothing records what happens once --init or --run starts running mapped code. Today a crash is a bare 0xC0000005 with a host address. This adds an opt-in trace, gta6rt --trace FILE, that writes one JSON object per line and flushes after each event, so the file survives a crash:

kind When
map a module is mapped, bound and protected (module, base, size, relocated)
tls_callback before each TLS callback the Loader runs on process attach and detach (at as module+RVA)
dllmain after DllMain(DLL_PROCESS_ATTACH) returns (result)
unimplemented_call the first call of each unimplemented import (import as dll!name, caller as module+RVA)
debug_string OutputDebugStringA/W when no debugger is attached
exception any exception, before handlers run (code, at, access kind and address for AVs, up to 16 unwound frames)
exit / error --run returns, or the loader throws
  • Behavior is unchanged. The vectored handler only records and returns EXCEPTION_CONTINUE_SEARCH. The trace is off by default, and when it is off each hook costs one relaxed load.
  • No new stub. It reuses the existing shims::make_unimplemented_thunk path: rt_unimplemented logs the first call per import. It works with any LEONIDA_UNIMPLEMENTED policy.
  • No shim for debug strings. OutputDebugString is captured through DBG_PRINTEXCEPTION_C / DBG_PRINTEXCEPTION_WIDE_C in the same handler.
  • Thread TLS events are not traced. run_callbacks also runs them from gta6rt_tls_host.dll's DllMain, under the Windows loader lock. To keep file I/O and the module-lookup mutex out of that context, only the Loader's own process attach and detach calls pass trace=true.
  • Address helper. shims::module_offset() formats an address as a.dll+0x1234, host:ntdll.dll+0x1234 or bare hex, using the existing module lookups.
  • Related: RT-20 / Runtime: add opt-in D3D12 call tracing #701 (--trace-d3d12) covers the D3D12 layer. This covers the loader and process side. The two flags are independent.

The file can quote whatever the game prints, so the README says it stays on the player's machine. Only counts or lines from synthetic fixtures should go in issues and PRs.

Code written from scratch for this repository.

Tested

  • New startup_trace_test: it builds a synthetic DLL image by hand, with one import that binds to nothing, kernel32!OutputDebugStringA, and a read of address 0. It checks every event kind, including the module+RVA of the caller and of the faulting instruction, and that a repeated call logs only once. No game files and no GPU.
  • ctest -R "startup_trace|^loader|unimplemented_policy": 4 of 4 pass (loader, loader_tls, unimplemented_policy, startup_trace). main.cpp and pe_loader.cpp compile for gta6rt.
  • Not run: the full suite. gta6rt_d3d12_com does not compile here with Windows SDK 10.0.26100, because its d3d12.h has no D3D12_STANDARD_MULTISAMPLE_PATTERN / D3D12_CENTER_MULTISAMPLE_PATTERN (used in d3d12_vulkan_device.cpp since D3D-49). This happens on current main too and is unrelated to this change.
  • Environment: Windows 11 Pro 23H2, MSVC 14.44 (VS 2022), Ninja, RelWithDebInfo. GPU: NVIDIA GeForce RTX 4060, driver 621.14 (Vulkan 1.4.357). The tests above are CPU-only.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

@evilclanker9000 evilclanker9000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tested c620c2c on Windows with a small MSVC probe using the actual trace header and synthetic inputs.

  • Trace output can replace an input. wmain opens the trace before the loader reads the image, and open() uses wb. If the two paths refer to the same file, the image gets truncated before loading. The probe confirmed that an existing synthetic file was replaced. Please create the trace only if the destination is new and add a control that checks the existing input bytes survive.
  • ANSI debug output can break the JSONL. An ANSI debug record containing a single-byte é produced a literal 0xe9 in the file and failed strict UTF-8 decoding. Convert the ANSI payload to UTF-8 before quoting it and exercise a non-ASCII input. The Windows documentation describes the locale-dependent ANSI path.

No game inputs or GPU tests. AI-assisted.

@Thatters

Copy link
Copy Markdown
Contributor

Tested c620c2ca on Windows 11 Pro 25H2, MSVC 14.51 + Ninja (scripts/build_runtime.ps1), RTX 5070 Ti / 621.24: pass.

I didn't run gta6rt --trace end to end, since there's no game executable here.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): no regression from this PR. RTX 4080 SUPER: 101/102 pass. d3d12_draw fails on both GPUs, but it fails the same way on every PR built on current main (bundle blend pixel (0, 0): got 255,0,0,255, expected red 64), so it's a main regression, not from this PR. I'm bisecting it now and will report in the coordination issue. Other Intel UHD 770 failures are the known ones in #1548.

Writes one JSON object per line, flushed per event: module maps, TLS
callbacks, DllMain results, the first call of each unimplemented import
with its caller as module+RVA, OutputDebugString text, exceptions with
module+RVA and a short unwind, and the exit code. A vectored handler
records and returns EXCEPTION_CONTINUE_SEARCH, so behaviour is unchanged.
Off by default.

startup_trace_test builds a synthetic DLL image (no game data) and
checks each event kind.
Review on ItsNotRexed#1756 (Vonsek):
- The trace is opened before the loader reads the image, so `wb` could
  truncate an input named by mistake (the image itself). open() now
  uses "x" mode and fails with EEXIST on an existing path; gta6rt says
  so and exits with 2.
- OutputDebugStringA text is in the ANSI code page. It is converted
  to UTF-8 before quoting, so a byte such as 0xE9 no longer breaks the
  JSON lines. Host module names now come from GetModuleFileNameW,
  converted to UTF-8, for the same reason.

startup_trace_test: an existing file is refused with EEXIST and its
bytes survive, the trace stays off; an ANSI debug string with 0xE9
appears as its UTF-8 form and the whole file is valid UTF-8.
@leozinhoszg

Copy link
Copy Markdown
Contributor Author

@Vonsek thanks, both reproduced and fixed in d6443b1. The branch is also rebased on current main, which resolves the pe_loader.cpp include conflict.

  • Existing file: startup_trace::open() now creates the file with "wxb" and fails with EEXIST if the path exists. gta6rt then prints error: the trace file already exists; --trace only creates a new file and exits with 2, before the loader reads anything. The test adds a control: an existing 8-byte file is refused with EEXIST, the trace stays off, and the file's bytes are unchanged.
  • ANSI text: DBG_PRINTEXCEPTION_C text now goes CP_ACP → UTF-16 → UTF-8 before quoting. Host module names had the same problem through GetModuleFileNameA, so they now use GetModuleFileNameW and UTF-8 too. The test sends "caf\xe9 probe" through OutputDebugStringA. It checks that the line holds the UTF-8 form of that ANSI text, and that the whole trace passes MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS).
  • The README now says FILE must be new and that the file is UTF-8.

startup_trace_test: 23 checks, 0 failed (6 new). loader, loader_tls and unimplemented_policy also pass. Tested with Windows 11 Pro 23H2 and MSVC 14.44, no game files. AI-assisted.

@Thatters @kapitche thanks for the runs. The d3d12_draw failure is the main one (#1793), as you found.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Thanks. I built the isolated startup_trace_test target from d6443b152469c467f6295889de855b2931095161 and ran ctest -R "^startup_trace$" on Windows with MSVC 19.44. All 23 checks passed. The existing synthetic file was refused with EEXIST and its bytes were preserved. The ANSI debug string appeared in its UTF-8 form and the full trace passed strict UTF-8 decoding. Both findings from my earlier review are resolved.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 86e6848 into ItsNotRexed:main Nov 22, 2026
10 of 11 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.

5 participants