From 590db9ca7c8c121b917f8431cf4d0bc19b4c1870 Mon Sep 17 00:00:00 2001 From: Ran-j Date: Thu, 5 Feb 2026 01:58:05 -0300 Subject: [PATCH] feat: added thread naming functionality feat: improve function name sanitization --- ps2xRecomp/src/lib/code_generator.cpp | 26 +++++-------- ps2xRecomp/src/lib/ps2_recompiler.cpp | 35 +++++++++++------- ps2xRuntime/include/ThreadNaming.h | 53 +++++++++++++++++++++++++++ ps2xRuntime/src/lib/ps2_runtime.cpp | 9 +---- 4 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 ps2xRuntime/include/ThreadNaming.h diff --git a/ps2xRecomp/src/lib/code_generator.cpp b/ps2xRecomp/src/lib/code_generator.cpp index 0ed9982..a912f88 100644 --- a/ps2xRecomp/src/lib/code_generator.cpp +++ b/ps2xRecomp/src/lib/code_generator.cpp @@ -28,7 +28,7 @@ namespace ps2recomp namespace ps2recomp { - static bool isReservedCxxIdentifier(const std::string& name) + static bool isReservedCxxIdentifier(const std::string &name) { if (name.size() >= 2 && name[0] == '_' && name[1] == '_') return true; @@ -37,7 +37,7 @@ namespace ps2recomp return false; } - static bool isReservedCxxKeyword(const std::string& name) + static bool isReservedCxxKeyword(const std::string &name) { return kKeywords.contains(name); } @@ -75,7 +75,7 @@ namespace ps2recomp } return ""; - } + } std::string CodeGenerator::sanitizeFunctionName(const std::string &name) const { @@ -252,13 +252,13 @@ namespace ps2recomp std::string funcName = getFunctionName(target); bool isInternalTarget = internalTargets.contains(target); - if (!funcName.empty()) + if (isInternalTarget) { - targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName); + targetAction = fmt::format("goto label_{:x};", target); } - else if (isInternalTarget) + else if (!funcName.empty()) { - targetAction = fmt::format("goto label_{:x};", target); + targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName); } else { @@ -325,11 +325,7 @@ namespace ps2recomp if (target >= function.start && target < function.end) { - std::string funcName = getFunctionName(target); - if (funcName.empty()) - { - targets.insert(target); - } + targets.insert(target); } } else if (isStaticJump) @@ -337,11 +333,7 @@ namespace ps2recomp uint32_t target = (inst.address & 0xF0000000) | (inst.target << 2); if (target >= function.start && target < function.end) { - std::string funcName = getFunctionName(target); - if (funcName.empty()) - { - targets.insert(target); - } + targets.insert(target); } } } diff --git a/ps2xRecomp/src/lib/ps2_recompiler.cpp b/ps2xRecomp/src/lib/ps2_recompiler.cpp index d9fe7f9..d420510 100644 --- a/ps2xRecomp/src/lib/ps2_recompiler.cpp +++ b/ps2xRecomp/src/lib/ps2_recompiler.cpp @@ -218,12 +218,24 @@ namespace ps2recomp { m_functionRenames.clear(); + auto makeName = [&](const Function &function) -> std::string + { + std::string sanitized = sanitizeFunctionName(function.name); + if (sanitized.empty()) + { + std::stringstream ss; + ss << "func_" << std::hex << function.start; + sanitized = ss.str(); + } + return sanitized; + }; + std::unordered_map nameCounts; for (const auto &function : m_functions) { if (!function.isRecompiled && !function.isStub) continue; - std::string sanitized = sanitizeFunctionName(function.name); + std::string sanitized = makeName(function); nameCounts[sanitized]++; } @@ -232,22 +244,19 @@ namespace ps2recomp if (!function.isRecompiled && !function.isStub) continue; - std::string sanitized = sanitizeFunctionName(function.name); + std::string sanitized = makeName(function); bool isDuplicate = nameCounts[sanitized] > 1; - if (isDuplicate || sanitized != function.name) + std::stringstream ss; + if (isDuplicate) { - std::stringstream ss; - if (isDuplicate) - { - ss << sanitized << "_0x" << std::hex << function.start; - } - else - { - ss << sanitized; - } - m_functionRenames[function.start] = ss.str(); + ss << sanitized << "_0x" << std::hex << function.start; + } + else + { + ss << sanitized; } + m_functionRenames[function.start] = ss.str(); } if (m_codeGenerator) diff --git a/ps2xRuntime/include/ThreadNaming.h b/ps2xRuntime/include/ThreadNaming.h new file mode 100644 index 0000000..b82ecc1 --- /dev/null +++ b/ps2xRuntime/include/ThreadNaming.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +#if defined(_WIN32) +#ifndef NOMINMAX +#define NOMINMAX +#endif +#ifndef WINAPI +#define WINAPI __stdcall +#endif + +extern "C" +{ + typedef void *HANDLE; + typedef void *HMODULE; + typedef const wchar_t *PCWSTR; + typedef long HRESULT; + + __declspec(dllimport) HMODULE WINAPI GetModuleHandleW(const wchar_t *lpModuleName); + __declspec(dllimport) void *WINAPI GetProcAddress(HMODULE hModule, const char *lpProcName); + __declspec(dllimport) HANDLE WINAPI GetCurrentThread(void); +} +#elif defined(__APPLE__) || defined(__linux__) +#include +#endif + +namespace ThreadNaming +{ + inline void SetCurrentThreadName(std::string_view name) + { +#if defined(_WIN32) + using SetThreadDescriptionFn = HRESULT(WINAPI*)(HANDLE, PCWSTR); + + HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll"); + auto setThreadDescription = + reinterpret_cast(::GetProcAddress(kernel32, "SetThreadDescription")); + + if (setThreadDescription) + { + std::wstring wname(name.begin(), name.end()); + setThreadDescription(::GetCurrentThread(), wname.c_str()); + } + +#elif defined(__APPLE__) + pthread_setname_np(name.data()); + +#elif defined(__linux__) + pthread_setname_np(pthread_self(), name.data()); +#endif + } +} diff --git a/ps2xRuntime/src/lib/ps2_runtime.cpp b/ps2xRuntime/src/lib/ps2_runtime.cpp index 425d069..cb54baa 100644 --- a/ps2xRuntime/src/lib/ps2_runtime.cpp +++ b/ps2xRuntime/src/lib/ps2_runtime.cpp @@ -9,6 +9,7 @@ #include #include #include "raylib.h" +#include #define ELF_MAGIC 0x464C457F // "\x7FELF" in little endian #define ET_EXEC 2 // Executable file @@ -107,12 +108,7 @@ static void UploadFrame(Texture2D &tex, PS2Runtime *rt) return; } - constexpr uint32_t DEFAULT_FB_ADDR = 0x00100000; uint32_t baseBytes = fbp * 2048; - if (fbp == 0) - { - baseBytes = DEFAULT_FB_ADDR; - } uint32_t strideBytes = (fbw ? fbw : (FB_WIDTH / 64)) * 64 * 4; uint8_t *rdram = rt->memory().getRDRAM(); uint8_t *gsvram = rt->memory().getGSVRAM(); @@ -165,7 +161,6 @@ static void UploadFrame(Texture2D &tex, PS2Runtime *rt) UpdateTexture(tex, scratch.data()); } - PS2Runtime::PS2Runtime() { std::memset(&m_cpuContext, 0, sizeof(m_cpuContext)); @@ -322,7 +317,6 @@ void PS2Runtime::SignalException(R5900Context *ctx, PS2Exception exception) } } - void PS2Runtime::executeVU0Microprogram(uint8_t *rdram, R5900Context *ctx, uint32_t address) { static std::unordered_map seen; @@ -426,6 +420,7 @@ void PS2Runtime::run() std::thread gameThread([&, entryPoint]() { + ThreadNaming::SetCurrentThreadName("GameThread"); try { entryPoint(m_memory.getRDRAM(), &m_cpuContext, this);