1 //===-- memprof_thread.h ---------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of MemProfiler, a memory profiler. 10 // 11 // MemProf-private header for memprof_thread.cpp. 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MEMPROF_THREAD_H 15 #define MEMPROF_THREAD_H 16 17 #include "memprof_allocator.h" 18 #include "memprof_internal.h" 19 #include "memprof_stats.h" 20 #include "sanitizer_common/sanitizer_common.h" 21 #include "sanitizer_common/sanitizer_libc.h" 22 #include "sanitizer_common/sanitizer_thread_registry.h" 23 24 namespace __sanitizer { 25 struct DTLS; 26 } // namespace __sanitizer 27 28 namespace __memprof { 29 30 const u32 kMaxNumberOfThreads = (1 << 22); // 4M 31 32 class MemprofThread; 33 34 // These objects are created for every thread and are never deleted, 35 // so we can find them by tid even if the thread is long dead. 36 struct MemprofThreadContext final : public ThreadContextBase { 37 explicit MemprofThreadContext(int tid) 38 : ThreadContextBase(tid), announced(false), 39 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0), 40 thread(nullptr) {} 41 bool announced; 42 u8 destructor_iterations; 43 u32 stack_id; 44 MemprofThread *thread; 45 46 void OnCreated(void *arg) override; 47 void OnFinished() override; 48 49 struct CreateThreadContextArgs { 50 MemprofThread *thread; 51 StackTrace *stack; 52 }; 53 }; 54 55 // MemprofThreadContext objects are never freed, so we need many of them. 56 COMPILER_CHECK(sizeof(MemprofThreadContext) <= 256); 57 58 // MemprofThread are stored in TSD and destroyed when the thread dies. 59 class MemprofThread { 60 public: 61 static MemprofThread *Create(thread_callback_t start_routine, void *arg, 62 u32 parent_tid, StackTrace *stack, 63 bool detached); 64 static void TSDDtor(void *tsd); 65 void Destroy(); 66 67 struct InitOptions; 68 void Init(const InitOptions *options = nullptr); 69 70 thread_return_t ThreadStart(tid_t os_id, 71 atomic_uintptr_t *signal_thread_is_registered); 72 73 uptr stack_top(); 74 uptr stack_bottom(); 75 uptr stack_size(); 76 uptr tls_begin() { return tls_begin_; } 77 uptr tls_end() { return tls_end_; } 78 DTLS *dtls() { return dtls_; } 79 u32 tid() { return context_->tid; } 80 MemprofThreadContext *context() { return context_; } 81 void set_context(MemprofThreadContext *context) { context_ = context; } 82 83 bool AddrIsInStack(uptr addr); 84 85 // True is this thread is currently unwinding stack (i.e. collecting a stack 86 // trace). Used to prevent deadlocks on platforms where libc unwinder calls 87 // malloc internally. See PR17116 for more details. 88 bool isUnwinding() const { return unwinding_; } 89 void setUnwinding(bool b) { unwinding_ = b; } 90 91 MemprofThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } 92 MemprofStats &stats() { return stats_; } 93 94 private: 95 // NOTE: There is no MemprofThread constructor. It is allocated 96 // via mmap() and *must* be valid in zero-initialized state. 97 98 void SetThreadStackAndTls(const InitOptions *options); 99 100 struct StackBounds { 101 uptr bottom; 102 uptr top; 103 }; 104 StackBounds GetStackBounds() const; 105 106 MemprofThreadContext *context_; 107 thread_callback_t start_routine_; 108 void *arg_; 109 110 uptr stack_top_; 111 uptr stack_bottom_; 112 113 uptr tls_begin_; 114 uptr tls_end_; 115 DTLS *dtls_; 116 117 MemprofThreadLocalMallocStorage malloc_storage_; 118 MemprofStats stats_; 119 bool unwinding_; 120 }; 121 122 // Returns a single instance of registry. 123 ThreadRegistry &memprofThreadRegistry(); 124 125 // Must be called under ThreadRegistryLock. 126 MemprofThreadContext *GetThreadContextByTidLocked(u32 tid); 127 128 // Get the current thread. May return 0. 129 MemprofThread *GetCurrentThread(); 130 void SetCurrentThread(MemprofThread *t); 131 u32 GetCurrentTidOrInvalid(); 132 133 // Used to handle fork(). 134 void EnsureMainThreadIDIsCorrect(); 135 } // namespace __memprof 136 137 #endif // MEMPROF_THREAD_H 138