1 //===-- sanitizer_procmaps_common.cc --------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Information about the process mappings (common parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
12 #if SANITIZER_FREEBSD || SANITIZER_LINUX
13 #include "sanitizer_common.h"
14 #include "sanitizer_placement_new.h"
15 #include "sanitizer_procmaps.h"
17 namespace __sanitizer
{
19 // Linker initialized.
20 ProcSelfMapsBuff
MemoryMappingLayout::cached_proc_self_maps_
;
21 StaticSpinMutex
MemoryMappingLayout::cache_lock_
; // Linker initialized.
23 static int TranslateDigit(char c
) {
24 if (c
>= '0' && c
<= '9')
26 if (c
>= 'a' && c
<= 'f')
28 if (c
>= 'A' && c
<= 'F')
33 // Parse a number and promote 'p' up to the first non-digit character.
34 static uptr
ParseNumber(const char **p
, int base
) {
37 CHECK(base
>= 2 && base
<= 16);
38 while ((d
= TranslateDigit(**p
)) >= 0 && d
< base
) {
45 bool IsDecimal(char c
) {
46 int d
= TranslateDigit(c
);
47 return d
>= 0 && d
< 10;
50 uptr
ParseDecimal(const char **p
) {
51 return ParseNumber(p
, 10);
55 int d
= TranslateDigit(c
);
56 return d
>= 0 && d
< 16;
59 uptr
ParseHex(const char **p
) {
60 return ParseNumber(p
, 16);
63 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled
) {
64 ReadProcMaps(&proc_self_maps_
);
66 if (proc_self_maps_
.mmaped_size
== 0) {
68 CHECK_GT(proc_self_maps_
.len
, 0);
71 CHECK_GT(proc_self_maps_
.mmaped_size
, 0);
74 // FIXME: in the future we may want to cache the mappings on demand only.
76 CacheMemoryMappings();
79 MemoryMappingLayout::~MemoryMappingLayout() {
80 // Only unmap the buffer if it is different from the cached one. Otherwise
81 // it will be unmapped when the cache is refreshed.
82 if (proc_self_maps_
.data
!= cached_proc_self_maps_
.data
) {
83 UnmapOrDie(proc_self_maps_
.data
, proc_self_maps_
.mmaped_size
);
87 void MemoryMappingLayout::Reset() {
88 current_
= proc_self_maps_
.data
;
92 void MemoryMappingLayout::CacheMemoryMappings() {
93 SpinMutexLock
l(&cache_lock_
);
94 // Don't invalidate the cache if the mappings are unavailable.
95 ProcSelfMapsBuff old_proc_self_maps
;
96 old_proc_self_maps
= cached_proc_self_maps_
;
97 ReadProcMaps(&cached_proc_self_maps_
);
98 if (cached_proc_self_maps_
.mmaped_size
== 0) {
99 cached_proc_self_maps_
= old_proc_self_maps
;
101 if (old_proc_self_maps
.mmaped_size
) {
102 UnmapOrDie(old_proc_self_maps
.data
,
103 old_proc_self_maps
.mmaped_size
);
108 void MemoryMappingLayout::LoadFromCache() {
109 SpinMutexLock
l(&cache_lock_
);
110 if (cached_proc_self_maps_
.data
) {
111 proc_self_maps_
= cached_proc_self_maps_
;
115 uptr
MemoryMappingLayout::DumpListOfModules(LoadedModule
*modules
,
117 string_predicate_t filter
) {
119 uptr cur_beg
, cur_end
, cur_offset
, prot
;
120 InternalScopedBuffer
<char> module_name(kMaxPathLength
);
122 for (uptr i
= 0; n_modules
< max_modules
&&
123 Next(&cur_beg
, &cur_end
, &cur_offset
, module_name
.data(),
124 module_name
.size(), &prot
);
126 const char *cur_name
= module_name
.data();
127 if (cur_name
[0] == '\0')
129 if (filter
&& !filter(cur_name
))
131 void *mem
= &modules
[n_modules
];
132 // Don't subtract 'cur_beg' from the first entry:
133 // * If a binary is compiled w/o -pie, then the first entry in
134 // process maps is likely the binary itself (all dynamic libs
135 // are mapped higher in address space). For such a binary,
136 // instruction offset in binary coincides with the actual
137 // instruction address in virtual memory (as code section
138 // is mapped to a fixed memory range).
139 // * If a binary is compiled with -pie, all the modules are
140 // mapped high at address space (in particular, higher than
141 // shadow memory of the tool), so the module can't be the
143 uptr base_address
= (i
? cur_beg
: 0) - cur_offset
;
144 LoadedModule
*cur_module
= new(mem
) LoadedModule(cur_name
, base_address
);
145 cur_module
->addAddressRange(cur_beg
, cur_end
, prot
& kProtectionExecute
);
151 void GetMemoryProfile(fill_profile_f cb
, uptr
*stats
, uptr stats_size
) {
154 uptr smaps_len
= ReadFileToBuffer("/proc/self/smaps",
155 &smaps
, &smaps_cap
, 64<<20);
158 const char *pos
= smaps
;
159 while (pos
< smaps
+ smaps_len
) {
161 start
= ParseHex(&pos
);
162 for (; *pos
!= '/' && *pos
> '\n'; pos
++) {}
164 } else if (internal_strncmp(pos
, "Rss:", 4) == 0) {
165 while (!IsDecimal(*pos
)) pos
++;
166 uptr rss
= ParseDecimal(&pos
) * 1024;
167 cb(start
, rss
, file
, stats
, stats_size
);
169 while (*pos
++ != '\n') {}
171 UnmapOrDie(smaps
, smaps_cap
);
174 } // namespace __sanitizer
176 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX