1 //===-- asan_thread_registry.h ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of AddressSanitizer, an address sanity checker. 11 // 12 // ASan-private header for asan_thread_registry.cc 13 //===----------------------------------------------------------------------===// 14 15 #ifndef ASAN_THREAD_REGISTRY_H 16 #define ASAN_THREAD_REGISTRY_H 17 18 #include "asan_lock.h" 19 #include "asan_stack.h" 20 #include "asan_stats.h" 21 #include "asan_thread.h" 22 23 namespace __asan { 24 25 // Stores summaries of all created threads, returns current thread, 26 // thread by tid, thread by stack address. There is a single instance 27 // of AsanThreadRegistry for the whole program. 28 // AsanThreadRegistry is thread-safe. 29 class AsanThreadRegistry { 30 public: 31 explicit AsanThreadRegistry(LinkerInitialized); 32 void Init(); 33 void RegisterThread(AsanThread *thread); 34 void UnregisterThread(AsanThread *thread); 35 36 AsanThread *GetMain(); 37 // Get the current thread. May return 0. 38 AsanThread *GetCurrent(); 39 void SetCurrent(AsanThread *t); 40 GetCurrentTidOrInvalid()41 u32 GetCurrentTidOrInvalid() { 42 if (!inited_) return 0; 43 AsanThread *t = GetCurrent(); 44 return t ? t->tid() : kInvalidTid; 45 } 46 47 // Returns stats for GetCurrent(), or stats for 48 // T0 if GetCurrent() returns 0. 49 AsanStats &GetCurrentThreadStats(); 50 // Flushes all thread-local stats to accumulated stats, and returns 51 // a copy of accumulated stats. 52 AsanStats GetAccumulatedStats(); 53 uptr GetCurrentAllocatedBytes(); 54 uptr GetHeapSize(); 55 uptr GetFreeBytes(); 56 57 AsanThreadSummary *FindByTid(u32 tid); 58 AsanThread *FindThreadByStackAddress(uptr addr); 59 60 private: 61 void UpdateAccumulatedStatsUnlocked(); 62 // Adds values of all counters in "stats" to accumulated stats, 63 // and fills "stats" with zeroes. 64 void FlushToAccumulatedStatsUnlocked(AsanStats *stats); 65 66 static const u32 kMaxNumberOfThreads = (1 << 22); // 4M 67 AsanThreadSummary *thread_summaries_[kMaxNumberOfThreads]; 68 AsanThread main_thread_; 69 AsanThreadSummary main_thread_summary_; 70 AsanStats accumulated_stats_; 71 u32 n_threads_; 72 AsanLock mu_; 73 bool inited_; 74 }; 75 76 // Returns a single instance of registry. 77 AsanThreadRegistry &asanThreadRegistry(); 78 79 } // namespace __asan 80 81 #endif // ASAN_THREAD_REGISTRY_H 82