upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-26 02:28:26 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-26 02:28:26 -0500
commit704bbfd1f95e659e6df22b921baecfb47dbc38c5 (patch)
tree66e6c1e62797e92b695ebe3a3b8ce8a9c7799d84 /runtime/shims/process.cpp
parent10adc5c6854d7c1a1708010f8f6cf75017052e2d (diff)
process: log the title's debug output and its TerminateProcess
Port VIctory's kernel.rs (GPL-3.0-or-later) OutputDebugStringA/W and TerminateProcess: the title's debug strings go to stderr as "[title] ..." (LEONIDA_TITLE_DEBUG_OUTPUT=0 silences them; the host still gets them for an attached debugger), and a TerminateProcess of the game's own process is logged with its code and caller before it is done, instead of ending gta6rt without a word. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index b66c2e590..95eb917df 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -20,6 +20,7 @@
20// umbrella libraries a GDK title links (OneCore_apiset.lib, mincore.lib) bind them to. The retail package's own DLLs 20// umbrella libraries a GDK title links (OneCore_apiset.lib, mincore.lib) bind them to. The retail package's own DLLs
21// import ExitProcess from api-ms-win-core-processthreads-l1-1-0.dll. 21// import ExitProcess from api-ms-win-core-processthreads-l1-1-0.dll.
22#include <windows.h> 22#include <windows.h>
23#include <intrin.h>
23 24
24#include <atomic> 25#include <atomic>
25#include <cerrno> 26#include <cerrno>
@@ -420,6 +421,52 @@ int run_entry(void* entry, size_t stack_bytes) {
420 421
421namespace { 422namespace {
422using namespace rt::process; 423using namespace rt::process;
424
425// The title's debug output goes to the log, as VIctory's kernel.rs OutputDebugStringA/W do (GPL-3.0-or-later): no
426// debugger needs to be attached to read what the title says about itself. "[title] <text>", one line per call, the
427// trailing newline dropped; LEONIDA_TITLE_DEBUG_OUTPUT=0 silences it. The host's function still gets the text, so an
428// attached debugger sees it as before.
429bool title_debug_output_on() {
430 static const bool on = [] {
431 char v[8] = {};
432 const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_DEBUG_OUTPUT", v, sizeof(v));
433 return !(n > 0 && n < sizeof(v) && v[0] == '0');
434 }();
435 return on;
436}
437void log_title_text(std::string text) {
438 while (!text.empty() && (text.back() == '\n' || text.back() == '\r')) text.pop_back();
439 std::fprintf(stderr, "[title] %s\n", text.c_str());
440}
441void WINAPI game_output_debug_string_a(LPCSTR text) {
442 if (text && title_debug_output_on()) log_title_text(text);
443 OutputDebugStringA(text);
444}
445void WINAPI game_output_debug_string_w(LPCWSTR text) {
446 if (text && title_debug_output_on()) {
447 const int n = WideCharToMultiByte(CP_UTF8, 0, text, -1, nullptr, 0, nullptr, nullptr);
448 std::string utf8(n > 1 ? static_cast<size_t>(n - 1) : 0, ' ');
449 if (n > 1) WideCharToMultiByte(CP_UTF8, 0, text, -1, utf8.data(), n, nullptr, nullptr);
450 log_title_text(std::move(utf8));
451 }
452 OutputDebugStringW(text);
453}
454
455// TerminateProcess of the game's own process ends gta6rt without a word otherwise; it is logged with its code and
456// caller first (VIctory kernel.rs TerminateProcess), then done as asked.
457BOOL WINAPI game_terminate_process(HANDLE process, UINT code) {
458 if (process == GetCurrentProcess() || GetProcessId(process) == GetCurrentProcessId()) {
459 std::fprintf(stderr, "[rt] process: the game called TerminateProcess(code %u) from %s\n", code,
460 rt::shims::module_offset(reinterpret_cast<uint64_t>(_ReturnAddress())).c_str());
461 std::fflush(nullptr);
462 }
463 return TerminateProcess(process, code);
464}
465
466#define DEBUG_OUTPUT(dll) \
467 {dll, "OutputDebugStringA", reinterpret_cast<void*>(&game_output_debug_string_a)}, \
468 {dll, "OutputDebugStringW", reinterpret_cast<void*>(&game_output_debug_string_w)}
469#define TERMINATE_PROCESS(dll) {dll, "TerminateProcess", reinterpret_cast<void*>(&game_terminate_process)}
423#define UCRT(dll) \ 470#define UCRT(dll) \
424 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 471 {dll, "exit", reinterpret_cast<void*>(&game_exit)}, {dll, "_exit", reinterpret_cast<void*>(&game_quick_exit)}, \
425 {dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \ 472 {dll, "_Exit", reinterpret_cast<void*>(&game_quick_exit)}, \
@@ -459,5 +506,12 @@ SHIM_TABLE(process,
459 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"), 506 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"),
460 COMMAND_LINE("kernel32.dll"), 507 COMMAND_LINE("kernel32.dll"),
461 COMMAND_LINE("kernelbase.dll"), 508 COMMAND_LINE("kernelbase.dll"),
509 DEBUG_OUTPUT("kernel32.dll"),
510 DEBUG_OUTPUT("kernelbase.dll"),
511 DEBUG_OUTPUT("api-ms-win-core-debug-l1-1-0.dll"),
512 DEBUG_OUTPUT("api-ms-win-core-debug-l1-1-1.dll"),
513 TERMINATE_PROCESS("kernel32.dll"),
514 TERMINATE_PROCESS("kernelbase.dll"),
515 TERMINATE_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"),
462 COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"), 516 COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"),
463); 517);