upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
authorLeonardo Guimaraes <leozinhoszg@gmail.com>2026-11-22 01:24:50 -0300
committerLeonardo Guimaraes <leozinhoszg@gmail.com>2026-11-22 12:03:22 -0300
commit2dca597bd9f143992032010037f2d5c3b970d312 (patch)
tree6efb3183be9afd7e8b73a20c5157982503e4569c /runtime/src/main.cpp
parent6a6704df1592e1dc4a3215ddf1423a78adb52045 (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/main.cpp')
-rw-r--r--runtime/src/main.cpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 560f68349..5ac73301d 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -9,7 +9,7 @@
9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR] 9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR]
10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. 10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
11// 11//
12// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [-- game arguments...] 12// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]
13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and 13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and
14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run 14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run
15// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code. 15// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code.
@@ -29,6 +29,7 @@
29#include "../shims/file_system.h" 29#include "../shims/file_system.h"
30#include "../shims/process_shim.h" 30#include "../shims/process_shim.h"
31#include "../shims/shims.h" 31#include "../shims/shims.h"
32#include "../shims/startup_trace.h"
32 33
33namespace { 34namespace {
34 35
@@ -43,9 +44,9 @@ const char* binding_name(rt::Binding b) {
43 44
44int usage() { 45int usage() {
45 std::fprintf(stderr, 46 std::fprintf(stderr,
46 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR]\n" 47 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
47 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--trace-d3d12]\n" 48 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE]\n"
48 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [-- game arguments...]\n"); 49 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n");
49 return 2; 50 return 2;
50} 51}
51 52
@@ -55,6 +56,7 @@ int wmain(int argc, wchar_t** argv) {
55 std::filesystem::path image, game_dir, overlay_dir; 56 std::filesystem::path image, game_dir, overlay_dir;
56 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 57 bool dry_run = false, init = false, run = false, trace_d3d12 = false;
57 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 58 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
59 std::wstring trace_file;
58 for (int i = 1; i < argc; ++i) { 60 for (int i = 1; i < argc; ++i) {
59 std::wstring a = argv[i]; 61 std::wstring a = argv[i];
60 if (a == L"--") { 62 if (a == L"--") {
@@ -65,6 +67,7 @@ int wmain(int argc, wchar_t** argv) {
65 else if (a == L"--init") init = true; 67 else if (a == L"--init") init = true;
66 else if (a == L"--run") run = true; 68 else if (a == L"--run") run = true;
67 else if (a == L"--trace-d3d12") trace_d3d12 = true; 69 else if (a == L"--trace-d3d12") trace_d3d12 = true;
70 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
68 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 71 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
69 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i]; 72 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i];
70 else if (image.empty()) image = a; 73 else if (image.empty()) image = a;
@@ -82,6 +85,10 @@ int wmain(int argc, wchar_t** argv) {
82 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 85 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
83 rt::fs::configure(game_dir, overlay_dir); 86 rt::fs::configure(game_dir, overlay_dir);
84 rt::d3d12_trace::set_enabled(trace_d3d12); 87 rt::d3d12_trace::set_enabled(trace_d3d12);
88 if (!trace_file.empty() && !rt::startup_trace::open(trace_file.c_str())) {
89 std::fprintf(stderr, "error: cannot open the trace file\n");
90 return 2;
91 }
85 92
86 try { 93 try {
87 rt::Loader loader(game_dir); 94 rt::Loader loader(game_dir);
@@ -112,7 +119,10 @@ int wmain(int argc, wchar_t** argv) {
112 std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented); 119 std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented);
113 std::printf("unimplemented by return policy: hresult=%d other=%d\n", unimplemented_hresult, 120 std::printf("unimplemented by return policy: hresult=%d other=%d\n", unimplemented_hresult,
114 unimplemented - unimplemented_hresult); 121 unimplemented - unimplemented_hresult);
115 if (!run) return 0; 122 if (!run) {
123 rt::startup_trace::event("exit", "\"code\":0");
124 return 0;
125 }
116 if (!main_module.entry_rva) { 126 if (!main_module.entry_rva) {
117 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); 127 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str());
118 return 1; 128 return 1;
@@ -125,6 +135,7 @@ int wmain(int argc, wchar_t** argv) {
125 std::fflush(stdout); 135 std::fflush(stdout);
126 std::printf("game exited with code %d\n", code); 136 std::printf("game exited with code %d\n", code);
127 rt::shims::report_called_unimplemented(); 137 rt::shims::report_called_unimplemented();
138 rt::startup_trace::event("exit", "\"code\":" + std::to_string(code));
128 std::fflush(stdout); 139 std::fflush(stdout);
129 std::fflush(stderr); 140 std::fflush(stderr);
130 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its 141 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its
@@ -132,6 +143,7 @@ int wmain(int argc, wchar_t** argv) {
132 ExitProcess(static_cast<UINT>(code)); 143 ExitProcess(static_cast<UINT>(code));
133 } catch (const std::exception& e) { 144 } catch (const std::exception& e) {
134 std::fprintf(stderr, "error: %s\n", e.what()); 145 std::fprintf(stderr, "error: %s\n", e.what());
146 rt::startup_trace::event("error", "\"what\":" + rt::startup_trace::quote(e.what()));
135 return 1; 147 return 1;
136 } 148 }
137} 149}