Skip to content
Draft
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
2 changes: 2 additions & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_TITLE_WATCHDOG` | unset (armed) | The title's own hang watchdog stays armed by default, as on the console: if the game's main loop does not report for 30 seconds, the title ends itself. `0` makes `gta6rt --run` keep it from firing while the game thread waits for the driver, for example on the first boot after a driver update (`src/main.cpp`). **Since RT-BOOT-COMBINED `0` also needs `LEONIDA_TITLE_WATCHDOG_KICK`**: the watchdog's addresses are no longer built in. With `0` and no `LEONIDA_TITLE_WATCHDOG_KICK`, the loader says once that the watchdog stays armed. |
| `LEONIDA_TITLE_WATCHDOG_KICK` | unset | `<trap RVA>,<kick RVA>` (hex, measured locally and not kept in the repository): where the watchdog's trap instruction is and the byte the game's main loop sets. Used only with `LEONIDA_TITLE_WATCHDOG=0`, and only when the watchdog's trap instruction is at the first RVA; set without `LEONIDA_TITLE_WATCHDOG=0`, the watchdog stays armed and the loader says so once. |
| `LEONIDA_POOL_DIAG_AT` | unset | *Diagnostic.* `<module>+0x<offset>` of the title's transient-pool assert, as the trace prints it (measured locally): the trace dumps the pool's resource list when that int3 is hit (`shims/startup_trace.h`). |
| `LEONIDA_HANG_DUMP` | unset | *Diagnostic.* `<seconds>`: when the trace opens, a detached thread waits that many seconds and then writes one `thread_stack` event per thread of the process, except its own: each thread is suspended only while its context is copied, and the stack is unwound after it resumes, so no lock it holds can block the walk; a thread that is really hung waits where it is, so its stack does not change in between (`shims/startup_trace.h`). A value that is missing, empty or not a positive whole number leaves the dump off. |
| `LEONIDA_PEEK` | unset | *Diagnostic.* `<rva>[:n],<rva>,...` (hex): a thread prints the 8-byte values at these RVAs of the title's image (`:n` = n consecutive values) every `LEONIDA_PEEK_SECONDS` (`src/main.cpp`). The RVAs are measured locally and not kept in the repository. `scripts/windows-boot/boot.ps1` no longer sets the game-state and session-state RVAs by default: it sets `LEONIDA_PEEK` only from `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA`, and without them its RESULT states read `?` ([scripts/windows-boot/README.md](../scripts/windows-boot/README.md)). |
| `LEONIDA_PEEK_SECONDS` | `10` | *Diagnostic.* The period of the `LEONIDA_PEEK` thread (`src/main.cpp`), which prints 8-byte values of the title's image. |
| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` separated by commas (`a@70,a@85,start@120`), each held 300 ms or its own `button@seconds:hold`; the clock starts with the shim. Works with and without SDL3: with SDL3 it drives the keyboard pad (`LEONIDA_KEYBOARD_PAD=always`), without SDL3 one scripted pad that is the only device. Controllers and the keyboard pad still need SDL3. Format and buttons in [docs/gameinput.md](../docs/gameinput.md#environment-variables). |
Expand Down Expand Up @@ -658,6 +659,7 @@ from the ANSI code page.
| `xmem_physical_pages` | the first `XMemAllocatePhysicalPages` for each distinct attribute word | `api`, `attributes`, `pages`, `caller` |
| `file_open_failed` | a guest `CreateFileW`/`CreateFile2` that fails (at most 256 events) | `api`, `path` (as the title passed it), `error` (Win32), `caller` (module+RVA) |
| `int3_skipped` | with `LEONIDA_SKIP_INT3` set when the trace opens: first int3 at each address, which is then stepped over (after 4096 distinct addresses, a new one is logged each time it is hit) | `at` (module+RVA) |
| `thread_stack` | with `LEONIDA_HANG_DUMP` set when the trace opens, after that many seconds: one per thread of the process, except the dump thread's own; each thread is suspended only while its context is copied, so the walk sees a hung thread where it waits | `tid`, `stack` (up to 16 frames) |
| `debug_string` | `OutputDebugStringA/W` with no debugger attached | `text` |
| `exception` | any exception, before handlers run | `code`, `at`, `access`/`address` for access violations, `cpp` for C++ throws, `stack` (up to 16 frames) |
| `exit` / `error` | `--run` returns, or the loader throws | `code` / `what` |
Expand Down
62 changes: 62 additions & 0 deletions runtime/shims/startup_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include <windows.h>
#include <tlhelp32.h>

#include <atomic>
#include <cerrno>
Expand All @@ -34,6 +35,8 @@
#include <initializer_list>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

#include "shims.h"

Expand Down Expand Up @@ -363,6 +366,64 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) {
in_handler = false;
return EXCEPTION_CONTINUE_SEARCH;
}

inline void log_thread_stack(DWORD tid, const CONTEXT& c) {
event("thread_stack", "\"tid\":" + std::to_string(tid) + ",\"stack\":" + stack_from(c));
}

// No C++ objects here, so the SEH frame is allowed under /EHsc (as in handler()).
inline void log_thread_stack_guarded(DWORD tid, const CONTEXT* c) {
#ifdef _MSC_VER
__try {
log_thread_stack(tid, *c);
} __except (EXCEPTION_EXECUTE_HANDLER) {
// A stack that changed under the walk: skip that thread.
}
#else
log_thread_stack(tid, *c);
#endif
}

// LEONIDA_HANG_DUMP: one thread_stack event per thread of this process except the caller. Each thread is suspended
// only while its context is read; the stack is walked after it resumes, so no lock it holds can block the walk. A
// thread that is really hung waits where it is, so its stack does not change in between.
inline void dump_thread_stacks() {
const DWORD self = GetCurrentThreadId();
const DWORD pid = GetCurrentProcessId();
std::vector<std::pair<DWORD, CONTEXT>> contexts;
const HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snap == INVALID_HANDLE_VALUE) return;
THREADENTRY32 entry{};
entry.dwSize = sizeof entry;
for (BOOL more = Thread32First(snap, &entry); more; more = Thread32Next(snap, &entry)) {
if (entry.th32OwnerProcessID != pid || entry.th32ThreadID == self) continue;
const HANDLE thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT, FALSE, entry.th32ThreadID);
if (!thread) continue;
CONTEXT c{};
c.ContextFlags = CONTEXT_FULL;
if (SuspendThread(thread) != static_cast<DWORD>(-1)) {
const BOOL ok = GetThreadContext(thread, &c);
ResumeThread(thread);
if (ok) contexts.emplace_back(entry.th32ThreadID, c);
}
CloseHandle(thread);
}
CloseHandle(snap);
std::fprintf(stderr, "[rt] LEONIDA_HANG_DUMP: %zu thread stack(s) written to the trace\n", contexts.size());
for (const auto& [tid, c] : contexts) log_thread_stack_guarded(tid, &c);
}

inline void start_hang_dump() {
wchar_t value[32] = {};
if (!GetEnvironmentVariableW(L"LEONIDA_HANG_DUMP", value, 32)) return;
const long seconds = std::wcstol(value, nullptr, 10);
if (seconds <= 0) return;
std::fprintf(stderr, "[rt] LEONIDA_HANG_DUMP: thread stacks in %ld s\n", seconds);
std::thread([seconds] {
Sleep(static_cast<DWORD>(seconds) * 1000);
if (enabled()) dump_thread_stacks();
}).detach();
}
} // namespace detail

inline void close();
Expand All @@ -389,6 +450,7 @@ inline bool open(const wchar_t* path, int* error = nullptr) {
detail::veh = AddVectoredExceptionHandler(1, &detail::handler);
detail::on.store(true, std::memory_order_release);
event("trace_open", "\"pid\":" + std::to_string(GetCurrentProcessId()));
detail::start_hang_dump();
return true;
}

Expand Down
Loading