From 4a89a30556573a43fdb6a92b377a1378d1959e42 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 21 Dec 2021 13:52:52 +0100 Subject: [PATCH] Implement checking if any TID is within current process. --- client/TracySysTrace.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/TracySysTrace.cpp b/client/TracySysTrace.cpp index 26a1c6ed..34e9ea90 100644 --- a/client/TracySysTrace.cpp +++ b/client/TracySysTrace.cpp @@ -652,6 +652,31 @@ static int s_ctxBufferIdx = 0; static constexpr size_t RingBufSize = 256*1024; static RingBuffer* s_ring = nullptr; +static const int ThreadHashSize = 4 * 1024; +static uint32_t s_threadHash[ThreadHashSize] = {}; + +static bool CurrentProcOwnsThread( uint32_t tid ) +{ + const auto hash = tid & ( ThreadHashSize-1 ); + const auto hv = s_threadHash[hash]; + if( hv == tid ) return true; + if( hv == -tid ) return false; + + char path[256]; + sprintf( path, "/proc/self/task/%d", tid ); + struct stat st; + if( stat( path, &st ) == 0 ) + { + s_threadHash[hash] = tid; + return true; + } + else + { + s_threadHash[hash] = -tid; + return false; + } +} + static int perf_event_open( struct perf_event_attr* hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags ) { return syscall( __NR_perf_event_open, hw_event, pid, cpu, group_fd, flags );