diff options
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 54 |
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 | ||
| 421 | namespace { | 422 | namespace { |
| 422 | using namespace rt::process; | 423 | using 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. | ||
| 429 | bool 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 | } | ||
| 437 | void 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 | } | ||
| 441 | void WINAPI game_output_debug_string_a(LPCSTR text) { | ||
| 442 | if (text && title_debug_output_on()) log_title_text(text); | ||
| 443 | OutputDebugStringA(text); | ||
| 444 | } | ||
| 445 | void 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. | ||
| 457 | BOOL 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 | ); |