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