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