Skip to content

Commit

Permalink
Add threadid to AsyncStackRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
jesswong committed Nov 27, 2024
1 parent 256bb4a commit 5b2a016
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/unifex/tracing/async_stack-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 17 additions & 2 deletions include/unifex/tracing/async_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
#include <thread>

#include <unifex/detail/prologue.hpp>
#include <sys/syscall.h>

#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 {

Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions source/async_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 5b2a016

Please sign in to comment.