2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_procmaps_linux.cc
blobe77e33fb2d776350ce3a26d56a84ad4a2b1f8aa3
1 //===-- sanitizer_procmaps_linux.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 (Linux-specific parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
12 #if SANITIZER_LINUX
13 #include "sanitizer_common.h"
14 #include "sanitizer_procmaps.h"
16 namespace __sanitizer {
18 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
19 CHECK(ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
20 &proc_maps->mmaped_size, &proc_maps->len));
23 static bool IsOneOf(char c, char c1, char c2) {
24 return c == c1 || c == c2;
27 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
28 char filename[], uptr filename_size,
29 uptr *protection) {
30 char *last = proc_self_maps_.data + proc_self_maps_.len;
31 if (current_ >= last) return false;
32 uptr dummy;
33 if (!start) start = &dummy;
34 if (!end) end = &dummy;
35 if (!offset) offset = &dummy;
36 if (!protection) protection = &dummy;
37 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
38 if (next_line == 0)
39 next_line = last;
40 // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
41 *start = ParseHex(&current_);
42 CHECK_EQ(*current_++, '-');
43 *end = ParseHex(&current_);
44 CHECK_EQ(*current_++, ' ');
45 CHECK(IsOneOf(*current_, '-', 'r'));
46 *protection = 0;
47 if (*current_++ == 'r')
48 *protection |= kProtectionRead;
49 CHECK(IsOneOf(*current_, '-', 'w'));
50 if (*current_++ == 'w')
51 *protection |= kProtectionWrite;
52 CHECK(IsOneOf(*current_, '-', 'x'));
53 if (*current_++ == 'x')
54 *protection |= kProtectionExecute;
55 CHECK(IsOneOf(*current_, 's', 'p'));
56 if (*current_++ == 's')
57 *protection |= kProtectionShared;
58 CHECK_EQ(*current_++, ' ');
59 *offset = ParseHex(&current_);
60 CHECK_EQ(*current_++, ' ');
61 ParseHex(&current_);
62 CHECK_EQ(*current_++, ':');
63 ParseHex(&current_);
64 CHECK_EQ(*current_++, ' ');
65 while (IsDecimal(*current_))
66 current_++;
67 // Qemu may lack the trailing space.
68 // http://code.google.com/p/address-sanitizer/issues/detail?id=160
69 // CHECK_EQ(*current_++, ' ');
70 // Skip spaces.
71 while (current_ < next_line && *current_ == ' ')
72 current_++;
73 // Fill in the filename.
74 uptr i = 0;
75 while (current_ < next_line) {
76 if (filename && i < filename_size - 1)
77 filename[i++] = *current_;
78 current_++;
80 if (filename && i < filename_size)
81 filename[i] = 0;
82 current_ = next_line + 1;
83 return true;
86 } // namespace __sanitizer
88 #endif // SANITIZER_LINUX