Fix date
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_procmaps_common.cc
blobc725c2e7b6642e2678974375e042174583fbf23f
1 //===-- sanitizer_procmaps_common.cc --------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Information about the process mappings (common parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
13 #if SANITIZER_FREEBSD || SANITIZER_LINUX
15 #include "sanitizer_common.h"
16 #include "sanitizer_placement_new.h"
17 #include "sanitizer_procmaps.h"
19 namespace __sanitizer {
21 // Linker initialized.
22 ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
23 StaticSpinMutex MemoryMappingLayout::cache_lock_; // Linker initialized.
25 static int TranslateDigit(char c) {
26 if (c >= '0' && c <= '9')
27 return c - '0';
28 if (c >= 'a' && c <= 'f')
29 return c - 'a' + 10;
30 if (c >= 'A' && c <= 'F')
31 return c - 'A' + 10;
32 return -1;
35 // Parse a number and promote 'p' up to the first non-digit character.
36 static uptr ParseNumber(const char **p, int base) {
37 uptr n = 0;
38 int d;
39 CHECK(base >= 2 && base <= 16);
40 while ((d = TranslateDigit(**p)) >= 0 && d < base) {
41 n = n * base + d;
42 (*p)++;
44 return n;
47 bool IsDecimal(char c) {
48 int d = TranslateDigit(c);
49 return d >= 0 && d < 10;
52 uptr ParseDecimal(const char **p) {
53 return ParseNumber(p, 10);
56 bool IsHex(char c) {
57 int d = TranslateDigit(c);
58 return d >= 0 && d < 16;
61 uptr ParseHex(const char **p) {
62 return ParseNumber(p, 16);
65 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
66 ReadProcMaps(&proc_self_maps_);
67 if (cache_enabled) {
68 if (proc_self_maps_.mmaped_size == 0) {
69 LoadFromCache();
70 CHECK_GT(proc_self_maps_.len, 0);
72 } else {
73 CHECK_GT(proc_self_maps_.mmaped_size, 0);
75 Reset();
76 // FIXME: in the future we may want to cache the mappings on demand only.
77 if (cache_enabled)
78 CacheMemoryMappings();
81 MemoryMappingLayout::~MemoryMappingLayout() {
82 // Only unmap the buffer if it is different from the cached one. Otherwise
83 // it will be unmapped when the cache is refreshed.
84 if (proc_self_maps_.data != cached_proc_self_maps_.data) {
85 UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size);
89 void MemoryMappingLayout::Reset() {
90 current_ = proc_self_maps_.data;
93 // static
94 void MemoryMappingLayout::CacheMemoryMappings() {
95 SpinMutexLock l(&cache_lock_);
96 // Don't invalidate the cache if the mappings are unavailable.
97 ProcSelfMapsBuff old_proc_self_maps;
98 old_proc_self_maps = cached_proc_self_maps_;
99 ReadProcMaps(&cached_proc_self_maps_);
100 if (cached_proc_self_maps_.mmaped_size == 0) {
101 cached_proc_self_maps_ = old_proc_self_maps;
102 } else {
103 if (old_proc_self_maps.mmaped_size) {
104 UnmapOrDie(old_proc_self_maps.data,
105 old_proc_self_maps.mmaped_size);
110 void MemoryMappingLayout::LoadFromCache() {
111 SpinMutexLock l(&cache_lock_);
112 if (cached_proc_self_maps_.data) {
113 proc_self_maps_ = cached_proc_self_maps_;
117 void MemoryMappingLayout::DumpListOfModules(
118 InternalMmapVector<LoadedModule> *modules) {
119 Reset();
120 uptr cur_beg, cur_end, cur_offset, prot;
121 InternalScopedString module_name(kMaxPathLength);
122 for (uptr i = 0; Next(&cur_beg, &cur_end, &cur_offset, module_name.data(),
123 module_name.size(), &prot);
124 i++) {
125 const char *cur_name = module_name.data();
126 if (cur_name[0] == '\0')
127 continue;
128 // Don't subtract 'cur_beg' from the first entry:
129 // * If a binary is compiled w/o -pie, then the first entry in
130 // process maps is likely the binary itself (all dynamic libs
131 // are mapped higher in address space). For such a binary,
132 // instruction offset in binary coincides with the actual
133 // instruction address in virtual memory (as code section
134 // is mapped to a fixed memory range).
135 // * If a binary is compiled with -pie, all the modules are
136 // mapped high at address space (in particular, higher than
137 // shadow memory of the tool), so the module can't be the
138 // first entry.
139 uptr base_address = (i ? cur_beg : 0) - cur_offset;
140 LoadedModule cur_module;
141 cur_module.set(cur_name, base_address);
142 cur_module.addAddressRange(cur_beg, cur_end, prot & kProtectionExecute);
143 modules->push_back(cur_module);
147 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {
148 char *smaps = nullptr;
149 uptr smaps_cap = 0;
150 uptr smaps_len = 0;
151 if (!ReadFileToBuffer("/proc/self/smaps", &smaps, &smaps_cap, &smaps_len))
152 return;
153 uptr start = 0;
154 bool file = false;
155 const char *pos = smaps;
156 while (pos < smaps + smaps_len) {
157 if (IsHex(pos[0])) {
158 start = ParseHex(&pos);
159 for (; *pos != '/' && *pos > '\n'; pos++) {}
160 file = *pos == '/';
161 } else if (internal_strncmp(pos, "Rss:", 4) == 0) {
162 while (!IsDecimal(*pos)) pos++;
163 uptr rss = ParseDecimal(&pos) * 1024;
164 cb(start, rss, file, stats, stats_size);
166 while (*pos++ != '\n') {}
168 UnmapOrDie(smaps, smaps_cap);
171 } // namespace __sanitizer
173 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX