blob: 0bb44b45ac3ca48259c53c0b0e1944e86fc5f555 [file] [log] [blame]
[email protected]959a8bf2013-07-03 02:02:231// 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
[email protected]35c80832013-09-06 05:07:507#include <fcntl.h>
aviebe805c2015-12-24 08:20:288#include <stddef.h>
[email protected]959a8bf2013-07-03 02:02:239
[email protected]e3177dd52014-08-13 20:22:1410#include "base/files/file_util.h"
[email protected]42f558fd2014-03-17 19:02:3511#include "base/files/scoped_file.h"
[email protected]959a8bf2013-07-03 02:02:2312#include "base/strings/string_split.h"
aviebe805c2015-12-24 08:20:2813#include "build/build_config.h"
14
15#if defined(OS_LINUX) || defined(OS_ANDROID)
16#include <inttypes.h>
17#endif
[email protected]959a8bf2013-07-03 02:02:2318
[email protected]fa1e7c22014-03-19 12:00:4719#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:
[email protected]959a8bf2013-07-03 02:02:2323// https://code.google.com/p/android/issues/detail?id=57218
24#undef SCNxPTR
25#define SCNxPTR "x"
26#endif
27
28namespace base {
29namespace debug {
30
[email protected]35c80832013-09-06 05:07:5031// Scans |proc_maps| starting from |pos| returning true if the gate VMA was
32// found, otherwise returns false.
33static 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
[email protected]959a8bf2013-07-03 02:02:2347bool ReadProcMaps(std::string* proc_maps) {
[email protected]35c80832013-09-06 05:07:5048 // 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
[email protected]42f558fd2014-03-17 19:02:3552 base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)));
53 if (!fd.is_valid()) {
[email protected]35c80832013-09-06 05:07:5054 DPLOG(ERROR) << "Couldn't open /proc/self/maps";
55 return false;
56 }
[email protected]35c80832013-09-06 05:07:5057 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
[email protected]42f558fd2014-03-17 19:02:3566 ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, kReadSize));
[email protected]35c80832013-09-06 05:07:5067 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;
[email protected]959a8bf2013-07-03 02:02:2392}
93
94bool ParseProcMaps(const std::string& input,
95 std::vector<MappedMemoryRegion>* regions_out) {
[email protected]ad9a0122014-03-22 00:34:5296 CHECK(regions_out);
[email protected]959a8bf2013-07-03 02:02:2397 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.
brettw26dab8f02015-08-08 00:28:47101 std::vector<std::string> lines = SplitString(
102 input, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
[email protected]959a8bf2013-07-03 02:02:23103
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) {
[email protected]ad9a0122014-03-22 00:34:52107 if (!lines[i].empty()) {
108 DLOG(WARNING) << "Last line not empty";
[email protected]959a8bf2013-07-03 02:02:23109 return false;
[email protected]ad9a0122014-03-22 00:34:52110 }
[email protected]959a8bf2013-07-03 02:02:23111 break;
112 }
113
114 MappedMemoryRegion region;
115 const char* line = lines[i].c_str();
116 char permissions[5] = {'\0'}; // Ensure NUL-terminated string.
aviebe805c2015-12-24 08:20:28117 uint8_t dev_major = 0;
118 uint8_t dev_minor = 0;
[email protected]959a8bf2013-07-03 02:02:23119 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) {
[email protected]ad9a0122014-03-22 00:34:52133 DPLOG(WARNING) << "sscanf failed for line: " << line;
[email protected]959a8bf2013-07-03 02:02:23134 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