• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/debug/proc_maps_linux.h"
6 
7 #include <fcntl.h>
8 #include <stddef.h>
9 
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_file.h"
12 #include "base/strings/string_split.h"
13 #include "build/build_config.h"
14 
15 #if defined(OS_LINUX) || defined(OS_ANDROID)
16 #include <inttypes.h>
17 #endif
18 
19 #if defined(OS_ANDROID) && !defined(__LP64__)
20 // In 32-bit mode, Bionic's inttypes.h defines PRI/SCNxPTR as an
21 // unsigned long int, which is incompatible with Bionic's stdint.h
22 // defining uintptr_t as an unsigned int:
23 // https://code.google.com/p/android/issues/detail?id=57218
24 #undef SCNxPTR
25 #define SCNxPTR "x"
26 #endif
27 
28 namespace base {
29 namespace debug {
30 
31 // Scans |proc_maps| starting from |pos| returning true if the gate VMA was
32 // found, otherwise returns false.
ContainsGateVMA(std::string * proc_maps,size_t pos)33 static bool ContainsGateVMA(std::string* proc_maps, size_t pos) {
34 #if defined(ARCH_CPU_ARM_FAMILY)
35   // The gate VMA on ARM kernels is the interrupt vectors page.
36   return proc_maps->find(" [vectors]\n", pos) != std::string::npos;
37 #elif defined(ARCH_CPU_X86_64)
38   // The gate VMA on x86 64-bit kernels is the virtual system call page.
39   return proc_maps->find(" [vsyscall]\n", pos) != std::string::npos;
40 #else
41   // Otherwise assume there is no gate VMA in which case we shouldn't
42   // get duplicate entires.
43   return false;
44 #endif
45 }
46 
ReadProcMaps(std::string * proc_maps)47 bool ReadProcMaps(std::string* proc_maps) {
48   // seq_file only writes out a page-sized amount on each call. Refer to header
49   // file for details.
50   const long kReadSize = sysconf(_SC_PAGESIZE);
51 
52   base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)));
53   if (!fd.is_valid()) {
54     DPLOG(ERROR) << "Couldn't open /proc/self/maps";
55     return false;
56   }
57   proc_maps->clear();
58 
59   while (true) {
60     // To avoid a copy, resize |proc_maps| so read() can write directly into it.
61     // Compute |buffer| afterwards since resize() may reallocate.
62     size_t pos = proc_maps->size();
63     proc_maps->resize(pos + kReadSize);
64     void* buffer = &(*proc_maps)[pos];
65 
66     ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, kReadSize));
67     if (bytes_read < 0) {
68       DPLOG(ERROR) << "Couldn't read /proc/self/maps";
69       proc_maps->clear();
70       return false;
71     }
72 
73     // ... and don't forget to trim off excess bytes.
74     proc_maps->resize(pos + bytes_read);
75 
76     if (bytes_read == 0)
77       break;
78 
79     // The gate VMA is handled as a special case after seq_file has finished
80     // iterating through all entries in the virtual memory table.
81     //
82     // Unfortunately, if additional entries are added at this point in time
83     // seq_file gets confused and the next call to read() will return duplicate
84     // entries including the gate VMA again.
85     //
86     // Avoid this by searching for the gate VMA and breaking early.
87     if (ContainsGateVMA(proc_maps, pos))
88       break;
89   }
90 
91   return true;
92 }
93 
ParseProcMaps(const std::string & input,std::vector<MappedMemoryRegion> * regions_out)94 bool ParseProcMaps(const std::string& input,
95                    std::vector<MappedMemoryRegion>* regions_out) {
96   CHECK(regions_out);
97   std::vector<MappedMemoryRegion> regions;
98 
99   // This isn't async safe nor terribly efficient, but it doesn't need to be at
100   // this point in time.
101   std::vector<std::string> lines = SplitString(
102       input, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
103 
104   for (size_t i = 0; i < lines.size(); ++i) {
105     // Due to splitting on '\n' the last line should be empty.
106     if (i == lines.size() - 1) {
107       if (!lines[i].empty()) {
108         DLOG(WARNING) << "Last line not empty";
109         return false;
110       }
111       break;
112     }
113 
114     MappedMemoryRegion region;
115     const char* line = lines[i].c_str();
116     char permissions[5] = {'\0'};  // Ensure NUL-terminated string.
117     uint8_t dev_major = 0;
118     uint8_t dev_minor = 0;
119     long inode = 0;
120     int path_index = 0;
121 
122     // Sample format from man 5 proc:
123     //
124     // address           perms offset  dev   inode   pathname
125     // 08048000-08056000 r-xp 00000000 03:0c 64593   /usr/sbin/gpm
126     //
127     // The final %n term captures the offset in the input string, which is used
128     // to determine the path name. It *does not* increment the return value.
129     // Refer to man 3 sscanf for details.
130     if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %llx %hhx:%hhx %ld %n",
131                &region.start, &region.end, permissions, &region.offset,
132                &dev_major, &dev_minor, &inode, &path_index) < 7) {
133       DPLOG(WARNING) << "sscanf failed for line: " << line;
134       return false;
135     }
136 
137     region.permissions = 0;
138 
139     if (permissions[0] == 'r')
140       region.permissions |= MappedMemoryRegion::READ;
141     else if (permissions[0] != '-')
142       return false;
143 
144     if (permissions[1] == 'w')
145       region.permissions |= MappedMemoryRegion::WRITE;
146     else if (permissions[1] != '-')
147       return false;
148 
149     if (permissions[2] == 'x')
150       region.permissions |= MappedMemoryRegion::EXECUTE;
151     else if (permissions[2] != '-')
152       return false;
153 
154     if (permissions[3] == 'p')
155       region.permissions |= MappedMemoryRegion::PRIVATE;
156     else if (permissions[3] != 's' && permissions[3] != 'S')  // Shared memory.
157       return false;
158 
159     // Pushing then assigning saves us a string copy.
160     regions.push_back(region);
161     regions.back().path.assign(line + path_index);
162   }
163 
164   regions_out->swap(regions);
165   return true;
166 }
167 
168 }  // namespace debug
169 }  // namespace base
170