feat: added thread naming functionality (#44)

feat: improve function name sanitization
This commit is contained in:
Ranieri
2026-02-05 02:07:49 -03:00
committed by GitHub
parent 5d3be0e2e7
commit e567c4cf60
4 changed files with 87 additions and 38 deletions
+10 -18
View File
@@ -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,14 +252,14 @@ namespace ps2recomp
std::string funcName = getFunctionName(target);
bool isInternalTarget = internalTargets.contains(target);
if (!funcName.empty())
{
targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName);
}
else if (isInternalTarget)
if (isInternalTarget)
{
targetAction = fmt::format("goto label_{:x};", target);
}
else if (!funcName.empty())
{
targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName);
}
else
{
targetAction = fmt::format("ctx->pc = 0x{:X}; return;", target);
@@ -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);
}
}
}
+22 -13
View File
@@ -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<std::string, int> 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)
+53
View File
@@ -0,0 +1,53 @@
#pragma once
#include <string_view>
#include <string>
#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 <pthread.h>
#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<SetThreadDescriptionFn>(::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
}
}
+2 -7
View File
@@ -9,6 +9,7 @@
#include <thread>
#include <unordered_map>
#include "raylib.h"
#include <ThreadNaming.h>
#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<uint32_t, int> seen;
@@ -426,6 +420,7 @@ void PS2Runtime::run()
std::thread gameThread([&, entryPoint]()
{
ThreadNaming::SetCurrentThreadName("GameThread");
try
{
entryPoint(m_memory.getRDRAM(), &m_cpuContext, this);