Home | History | Annotate | Download | only in asan
      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 NULL.
     38   AsanThread *GetCurrent();
     39   void SetCurrent(AsanThread *t);
     40 
     41   int GetCurrentTidOrMinusOne() {
     42     if (!inited_) return 0;
     43     AsanThread *t = GetCurrent();
     44     return t ? t->tid() : -1;
     45   }
     46 
     47   // Returns stats for GetCurrent(), or stats for
     48   // T0 if GetCurrent() returns NULL.
     49   AsanStats &GetCurrentThreadStats();
     50   // Flushes all thread-local stats to accumulated stats, and returns
     51   // a copy of accumulated stats.
     52   AsanStats GetAccumulatedStats();
     53   size_t GetCurrentAllocatedBytes();
     54   size_t GetHeapSize();
     55   size_t GetFreeBytes();
     56 
     57   AsanThreadSummary *FindByTid(int tid);
     58   AsanThread *FindThreadByStackAddress(uintptr_t 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 int kMaxNumberOfThreads = (1 << 22);  // 4M
     67   AsanThreadSummary *thread_summaries_[kMaxNumberOfThreads];
     68   AsanThread main_thread_;
     69   AsanThreadSummary main_thread_summary_;
     70   AsanStats accumulated_stats_;
     71   int 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