Skip to content
Closed
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
3 changes: 3 additions & 0 deletions docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Useful switches:
`int3_skipped` event, so a boot can be followed past soft asserts (an int3 followed by code that carries on).
Diagnostic only: a correct run reaches none, so each skipped address is a finding, not a fix. A failed guest file
open is always traced as `file_open_failed` (path, error, caller) when `--trace` is on.
* `LEONIDA_HANG_DUMP=<seconds>` (with `--trace`) writes a `thread_stack` event for every thread that many seconds
after the trace opens: each thread is suspended only to read its registers, and its stack is walked after it
resumes. For a title that hangs, or whose watchdog gives up after a fixed time, it shows where each thread waits.
* `LEONIDA_VK_VALIDATION=1` enables the Khronos validation layer with the Vulkan backend (startup overhead; it is a
diagnostic).
* `DISABLE_RTSS_LAYER=1` and `DISABLE_VULKAN_OBS_CAPTURE=1` when MSI Afterburner or OBS is installed; their
Expand Down
1 change: 1 addition & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,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: first int3 at each address, which is then stepped over | `at` (module+RVA) |
| `thread_stack` | with `LEONIDA_HANG_DUMP=<seconds>` set: once, that many seconds after `trace_open`, one per thread of the process | `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
68 changes: 68 additions & 0 deletions runtime/shims/startup_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// {"seq":7,"t":0.020,"kind":"exception","code":"0xc0000005","at":"a.dll+0x4a00","access":"read","address":"0x0",
// "stack":["a.dll+0x4a00","a.dll+0x2210"]}
// {"seq":8,"t":0.030,"kind":"exit","code":0}
// {"seq":9,"t":20.000,"kind":"thread_stack","tid":1234,"stack":["a.dll+0x4a00","a.dll+0x2210"]} (LEONIDA_HANG_DUMP)
//
// LEONIDA_HANG_DUMP=<seconds> (diagnostic, with --trace): that many seconds after the trace opens, every other thread
// of the process is suspended just long enough to read its registers, then its stack is walked and written as a
// thread_stack event. A title that hangs (a watchdog that gives up after N seconds, a wait that never returns) then
// shows where each thread waits. Off by default; it changes nothing else.
//
// The exception handler is vectored, logs and returns EXCEPTION_CONTINUE_SEARCH: it never changes what the process
// does (except LEONIDA_SKIP_INT3, below).
Expand All @@ -21,7 +27,10 @@
#pragma once

#include <windows.h>
#include <tlhelp32.h>
#include <set>
#include <thread>
#include <vector>

#include <atomic>
#include <cerrno>
Expand Down Expand Up @@ -231,6 +240,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 @@ -256,6 +323,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