diff options
| author | Leonardo Guimaraes <leozinhoszg@gmail.com> | 2026-11-22 01:24:50 -0300 |
|---|---|---|
| committer | Leonardo Guimaraes <leozinhoszg@gmail.com> | 2026-11-22 12:03:22 -0300 |
| commit | 2dca597bd9f143992032010037f2d5c3b970d312 (patch) | |
| tree | 6efb3183be9afd7e8b73a20c5157982503e4569c /runtime/src/pe_loader.cpp | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
Runtime: opt-in startup trace (gta6rt --trace FILE)
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.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 387051756..f153acdb1 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <vector> | 14 | #include <vector> |
| 15 | 15 | ||
| 16 | #include "../shims/shims.h" | 16 | #include "../shims/shims.h" |
| 17 | #include "../shims/startup_trace.h" | ||
| 17 | #include "base_reservation.h" | 18 | #include "base_reservation.h" |
| 18 | 19 | ||
| 19 | namespace rt { | 20 | namespace rt { |
| @@ -214,7 +215,9 @@ void uninstall_thread_block(Module& m) { | |||
| 214 | release_thread_block(); | 215 | release_thread_block(); |
| 215 | } | 216 | } |
| 216 | 217 | ||
| 217 | void run_callbacks(Module& m, uint32_t reason) { | 218 | // `trace`: record each callback in the startup trace. Only the Loader's own calls (process attach and detach) pass |
| 219 | // true; the thread events run under the Windows loader lock and stay out of the trace. | ||
| 220 | void run_callbacks(Module& m, uint32_t reason, bool trace = false) { | ||
| 218 | auto* tls = tls_directory(m); | 221 | auto* tls = tls_directory(m); |
| 219 | if (!tls || !tls->AddressOfCallBacks) return; | 222 | if (!tls || !tls->AddressOfCallBacks) return; |
| 220 | const uint64_t table_rva = rva_in(m, tls->AddressOfCallBacks, "the TLS callback table"); | 223 | const uint64_t table_rva = rva_in(m, tls->AddressOfCallBacks, "the TLS callback table"); |
| @@ -226,6 +229,10 @@ void run_callbacks(Module& m, uint32_t reason) { | |||
| 226 | if (!fn) break; | 229 | if (!fn) break; |
| 227 | auto callback = | 230 | auto callback = |
| 228 | reinterpret_cast<PIMAGE_TLS_CALLBACK>(m.base + rva_in(m, fn, "a TLS callback")); | 231 | reinterpret_cast<PIMAGE_TLS_CALLBACK>(m.base + rva_in(m, fn, "a TLS callback")); |
| 232 | if (trace && startup_trace::enabled()) | ||
| 233 | startup_trace::event("tls_callback", "\"module\":" + startup_trace::quote(m.name) + ",\"at\":" + | ||
| 234 | startup_trace::quote(startup_trace::where(reinterpret_cast<uint64_t>(callback))) + | ||
| 235 | ",\"reason\":" + std::to_string(reason)); | ||
| 229 | callback(m.base, static_cast<DWORD>(reason), nullptr); | 236 | callback(m.base, static_cast<DWORD>(reason), nullptr); |
| 230 | } | 237 | } |
| 231 | } | 238 | } |
| @@ -401,6 +408,11 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 401 | protect_sections(m); | 408 | protect_sections(m); |
| 402 | register_unwind(m); | 409 | register_unwind(m); |
| 403 | init_order_.push_back(&m); | 410 | init_order_.push_back(&m); |
| 411 | if (startup_trace::enabled()) | ||
| 412 | startup_trace::event("map", "\"module\":" + startup_trace::quote(m.name) + | ||
| 413 | ",\"base\":" + startup_trace::quote(startup_trace::hex(reinterpret_cast<uint64_t>(m.base))) + | ||
| 414 | ",\"size\":" + startup_trace::quote(startup_trace::hex(m.size)) + | ||
| 415 | ",\"relocated\":" + (m.relocated ? "true" : "false")); | ||
| 404 | return m; | 416 | return m; |
| 405 | } | 417 | } |
| 406 | 418 | ||
| @@ -458,7 +470,11 @@ void Loader::initialise(Module& m) { | |||
| 458 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { | 470 | if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { |
| 459 | if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image"); | 471 | if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image"); |
| 460 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); | 472 | auto entry = reinterpret_cast<BOOL(NTAPI*)(void*, DWORD, void*)>(m.base + m.entry_rva); |
| 461 | if (!entry(m.base, DLL_PROCESS_ATTACH, nullptr)) { | 473 | const BOOL ok = entry(m.base, DLL_PROCESS_ATTACH, nullptr); |
| 474 | if (startup_trace::enabled()) | ||
| 475 | startup_trace::event("dllmain", "\"module\":" + startup_trace::quote(m.name) + | ||
| 476 | ",\"result\":" + std::to_string(ok)); | ||
| 477 | if (!ok) { | ||
| 462 | set_initialised(m, true); // it ran: a second DLL_PROCESS_ATTACH would be wrong | 478 | set_initialised(m, true); // it ran: a second DLL_PROCESS_ATTACH would be wrong |
| 463 | throw std::runtime_error(m.name + ": DllMain returned FALSE"); | 479 | throw std::runtime_error(m.name + ": DllMain returned FALSE"); |
| 464 | } | 480 | } |
| @@ -645,7 +661,7 @@ void Loader::install_tls_for_thread(Module& m) { install_thread_block(m); } | |||
| 645 | 661 | ||
| 646 | void Loader::uninstall_tls_for_thread(Module& m) { uninstall_thread_block(m); } | 662 | void Loader::uninstall_tls_for_thread(Module& m) { uninstall_thread_block(m); } |
| 647 | 663 | ||
| 648 | void Loader::run_tls_callbacks(Module& m, uint32_t reason) { run_callbacks(m, reason); } | 664 | void Loader::run_tls_callbacks(Module& m, uint32_t reason) { run_callbacks(m, reason, /*trace=*/true); } |
| 649 | 665 | ||
| 650 | void Loader::parse_exports(Module& m) { | 666 | void Loader::parse_exports(Module& m) { |
| 651 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 667 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |