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:
authorjaybeebabageyus <jaybeebabageyus@users.noreply.github.com>2026-11-22 20:57:07 +0100
committerjaybeebabageyus <jaybeebabageyus@users.noreply.github.com>2026-11-22 20:57:07 +0100
commit50e9dd32fa8d9ebb6ba22e559ca62fa95920b88d (patch)
treed5b82116e556cfc96f96b7e575b91a049dd42fb9 /runtime/src/main.cpp
parent1188e6406e31870119a7e50c5972599db554a406 (diff)
parent162821017ea3c286c8bec860f3ff94f037912061 (diff)
Merge remote-tracking branch 'upstream/main' into 2070-lifecycle-fix
# Conflicts: # runtime/src/pe_loader.cpp # runtime/src/pe_loader.h # runtime/test/loader_tls_test.cpp
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index c8875d853..fc06389e9 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,11 @@ 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 (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
89 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
90 : "error: cannot create the trace file\n");
91 return 2;
92 }
85 93
86 try { 94 try {
87 rt::Loader loader(game_dir); 95 rt::Loader loader(game_dir);
@@ -112,7 +120,10 @@ int wmain(int argc, wchar_t** argv) {
112 std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented); 120 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, 121 std::printf("unimplemented by return policy: hresult=%d other=%d\n", unimplemented_hresult,
114 unimplemented - unimplemented_hresult); 122 unimplemented - unimplemented_hresult);
115 if (!run) return 0; 123 if (!run) {
124 rt::startup_trace::event("exit", "\"code\":0");
125 return 0;
126 }
116 if (!main_module.entry_rva) { 127 if (!main_module.entry_rva) {
117 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); 128 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str());
118 return 1; 129 return 1;
@@ -125,6 +136,7 @@ int wmain(int argc, wchar_t** argv) {
125 std::fflush(stdout); 136 std::fflush(stdout);
126 std::printf("game exited with code %d\n", code); 137 std::printf("game exited with code %d\n", code);
127 rt::shims::report_called_unimplemented(); 138 rt::shims::report_called_unimplemented();
139 rt::startup_trace::event("exit", "\"code\":" + std::to_string(code));
128 std::fflush(stdout); 140 std::fflush(stdout);
129 std::fflush(stderr); 141 std::fflush(stderr);
130 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its 142 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its
@@ -134,6 +146,7 @@ int wmain(int argc, wchar_t** argv) {
134 ExitProcess(static_cast<UINT>(code)); 146 ExitProcess(static_cast<UINT>(code));
135 } catch (const std::exception& e) { 147 } catch (const std::exception& e) {
136 std::fprintf(stderr, "error: %s\n", e.what()); 148 std::fprintf(stderr, "error: %s\n", e.what());
149 rt::startup_trace::event("error", "\"what\":" + rt::startup_trace::quote(e.what()));
137 return 1; 150 return 1;
138 } 151 }
139} 152}