diff --git a/include/unifex/tracing/async_stack-inl.hpp b/include/unifex/tracing/async_stack-inl.hpp index add04bde..99e84987 100644 --- a/include/unifex/tracing/async_stack-inl.hpp +++ b/include/unifex/tracing/async_stack-inl.hpp @@ -136,9 +136,10 @@ inline AsyncStackFrame* AsyncStackRoot::getTopFrame() const noexcept { } inline void AsyncStackRoot::setStackFrameContext( - frame_ptr framePtr, instruction_ptr ip) noexcept { + frame_ptr framePtr, instruction_ptr ip, uint64_t tId) noexcept { stackFramePtr = framePtr; returnAddress = ip; + threadId = tId; } inline frame_ptr AsyncStackRoot::getStackFramePointer() const noexcept { diff --git a/include/unifex/tracing/async_stack.hpp b/include/unifex/tracing/async_stack.hpp index ec7f67d6..e8702ce0 100644 --- a/include/unifex/tracing/async_stack.hpp +++ b/include/unifex/tracing/async_stack.hpp @@ -24,6 +24,15 @@ #include #include +#include + +#if defined(__APPLE__) +#define UNIFEX_SYS_gettid SYS_thread_selfid +#elif defined(SYS_gettid) +#define UNIFEX_SYS_gettid SYS_gettid +#else +#define UNIFEX_SYS_gettid __NR_gettid +#endif namespace unifex { @@ -150,7 +159,11 @@ struct AsyncStackRoot; struct AsyncStackFrame; namespace detail { class ScopedAsyncStackRoot; -} +} // namespace detail + +namespace utils { + uint64_t getOSThreadID(); +} // namespace utils // Get access to the current thread's top-most AsyncStackRoot. // @@ -454,7 +467,8 @@ struct AsyncStackRoot { // normal stack-trace. void setStackFrameContext( frame_ptr fp = frame_ptr::read_frame_pointer(), - instruction_ptr ip = instruction_ptr::read_return_address()) noexcept; + instruction_ptr ip = instruction_ptr::read_return_address(), + uint64_t tId = utils::getOSThreadID()) noexcept; frame_ptr getStackFramePointer() const noexcept; instruction_ptr getReturnAddress() const noexcept; @@ -501,6 +515,7 @@ struct AsyncStackRoot { // Typically initialise with instruction_ptr::read_return_address() or // setStackFrameContext(). instruction_ptr returnAddress; + uint64_t threadId = 0; }; namespace detail { diff --git a/source/async_stack.cpp b/source/async_stack.cpp index 9cc0465b..4a2b5a99 100644 --- a/source/async_stack.cpp +++ b/source/async_stack.cpp @@ -145,6 +145,20 @@ static thread_local AsyncStackRootHolder currentThreadAsyncStackRoot; } // namespace +namespace utils { + uint64_t getOSThreadID() { + #if defined(__APPLE__) + uint64_t tid; + pthread_threadid_np(nullptr, &tid); + return tid; + #elif defined(_WIN32) + return uint64_t(GetCurrentThreadId()); + #else + return uint64_t(syscall(UNIFEX_SYS_gettid)); + #endif + } +} + AsyncStackRoot* tryGetCurrentAsyncStackRoot() noexcept { return currentThreadAsyncStackRoot.get(); }