diff --git a/CMakeLists.txt b/CMakeLists.txt index 56297a9..05491ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,13 @@ else() endif() install(TARGETS wibo DESTINATION bin) +# Check for features +include(CheckSymbolExists) +set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) +set(CMAKE_REQUIRED_LIBRARIES pthread) +check_symbol_exists(pthread_gettid_np "pthread.h" HAVE_PTHREAD_GETTID_NP) +target_compile_definitions(wibo PRIVATE -DHAVE_PTHREAD_GETTID_NP=${HAVE_PTHREAD_GETTID_NP}) + find_package(Python3 COMPONENTS Interpreter REQUIRED) # Track down libclang for trampoline generation. diff --git a/dll/kernel32/processthreadsapi.cpp b/dll/kernel32/processthreadsapi.cpp index 7fd25ee..362f98d 100644 --- a/dll/kernel32/processthreadsapi.cpp +++ b/dll/kernel32/processthreadsapi.cpp @@ -182,7 +182,7 @@ BOOL WINAPI IsProcessorFeaturePresent(DWORD ProcessorFeature) { HANDLE WINAPI GetCurrentProcess() { HOST_CONTEXT_GUARD(); - DEBUG_LOG("GetCurrentProcess() -> %p\n", reinterpret_cast(static_cast(-1))); + DEBUG_LOG("GetCurrentProcess() -> %d\n", kPseudoCurrentProcessHandleValue); return kPseudoCurrentProcessHandleValue; } @@ -195,12 +195,7 @@ DWORD WINAPI GetCurrentProcessId() { DWORD WINAPI GetCurrentThreadId() { HOST_CONTEXT_GUARD(); - pthread_t thread = pthread_self(); -#ifdef __linux__ - const auto threadId = static_cast(thread); -#else - const auto threadId = static_cast(reinterpret_cast(thread)); -#endif + DWORD threadId = wibo::getThreadId(); DEBUG_LOG("GetCurrentThreadId() -> %u\n", threadId); return threadId; } diff --git a/src/main.cpp b/src/main.cpp index 0ba4d62..82e3955 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,12 +36,7 @@ void wibo::debug_log(const char *fmt, ...) { if (wibo::debugEnabled) { for (size_t i = 0; i < wibo::debugIndent; i++) fprintf(stderr, "\t"); - pthread_t threadId = pthread_self(); -#ifdef __APPLE__ - fprintf(stderr, "[thread %p] ", threadId); -#else - fprintf(stderr, "[thread %lu] ", threadId); -#endif + fprintf(stderr, "[thread %x] ", getThreadId()); vfprintf(stderr, fmt, args); fflush(stderr); } @@ -95,7 +90,7 @@ bool wibo::installTibForCurrentThread(TEB *tibPtr) { } void wibo::uninstallTebForCurrentThread() { - TEB* teb = std::exchange(currentThreadTeb, nullptr); + TEB *teb = std::exchange(currentThreadTeb, nullptr); tebThreadTeardown(teb); } diff --git a/src/processes.h b/src/processes.h index c1790c8..1e529ee 100644 --- a/src/processes.h +++ b/src/processes.h @@ -55,4 +55,6 @@ int spawnWithArgv(const std::string &applicationName, const std::vector &pinOut); std::vector splitCommandLine(const char *commandLine); +DWORD getThreadId(); + } // namespace wibo diff --git a/src/processes_common.cpp b/src/processes_common.cpp index a5e99a8..965f24d 100644 --- a/src/processes_common.cpp +++ b/src/processes_common.cpp @@ -16,7 +16,11 @@ #include #include +#include + +#ifdef __APPLE__ extern char **environ; +#endif using kernel32::ProcessObject; @@ -46,9 +50,7 @@ bool ProcessManager::addProcess(Pin po) { return mImpl->addProcess(std::move(po)); } -bool ProcessManager::running() const { - return mImpl && mImpl->running(); -} +bool ProcessManager::running() const { return mImpl && mImpl->running(); } ProcessManager &processes() { static ProcessManager mgr; @@ -350,5 +352,18 @@ std::vector splitCommandLine(const char *commandLine) { return result; } -} // namespace wibo +DWORD getThreadId() { +#if HAVE_PTHREAD_GETTID_NP + pid_t threadId = pthread_gettid_np(pthread_self()); +#elif defined(__linux__) + pid_t threadId = gettid(); +#elif defined(__APPLE__) + uint64_t threadId = 0; + pthread_threadid_np(nullptr, &threadId); +#else +#error "Unknown platform" +#endif + return static_cast(threadId); +} +} // namespace wibo