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 #if defined(OS_LINUX) 8 #include <inttypes.h> 9 #endif 10 11 #include "base/file_util.h" 12 #include "base/strings/string_split.h" 13 14 #if defined(OS_ANDROID) 15 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which 16 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int: 17 // https://code.google.com/p/android/issues/detail?id=57218 18 #undef SCNxPTR 19 #define SCNxPTR "x" 20 #endif 21 22 namespace base { 23 namespace debug { 24 25 bool ReadProcMaps(std::string* proc_maps) { 26 FilePath proc_maps_path("/proc/self/maps"); 27 return file_util::ReadFileToString(proc_maps_path, proc_maps); 28 } 29 30 bool ParseProcMaps(const std::string& input, 31 std::vector<MappedMemoryRegion>* regions_out) { 32 std::vector<MappedMemoryRegion> regions; 33 34 // This isn't async safe nor terribly efficient, but it doesn't need to be at 35 // this point in time. 36 std::vector<std::string> lines; 37 SplitString(input, '\n', &lines); 38 39 for (size_t i = 0; i < lines.size(); ++i) { 40 // Due to splitting on '\n' the last line should be empty. 41 if (i == lines.size() - 1) { 42 if (!lines[i].empty()) 43 return false; 44 break; 45 } 46 47 MappedMemoryRegion region; 48 const char* line = lines[i].c_str(); 49 char permissions[5] = {'\0'}; // Ensure NUL-terminated string. 50 uint8 dev_major = 0; 51 uint8 dev_minor = 0; 52 long inode = 0; 53 int path_index = 0; 54 55 // Sample format from man 5 proc: 56 // 57 // address perms offset dev inode pathname 58 // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm 59 // 60 // The final %n term captures the offset in the input string, which is used 61 // to determine the path name. It *does not* increment the return value. 62 // Refer to man 3 sscanf for details. 63 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %llx %hhx:%hhx %ld %n", 64 ®ion.start, ®ion.end, permissions, ®ion.offset, 65 &dev_major, &dev_minor, &inode, &path_index) < 7) { 66 return false; 67 } 68 69 region.permissions = 0; 70 71 if (permissions[0] == 'r') 72 region.permissions |= MappedMemoryRegion::READ; 73 else if (permissions[0] != '-') 74 return false; 75 76 if (permissions[1] == 'w') 77 region.permissions |= MappedMemoryRegion::WRITE; 78 else if (permissions[1] != '-') 79 return false; 80 81 if (permissions[2] == 'x') 82 region.permissions |= MappedMemoryRegion::EXECUTE; 83 else if (permissions[2] != '-') 84 return false; 85 86 if (permissions[3] == 'p') 87 region.permissions |= MappedMemoryRegion::PRIVATE; 88 else if (permissions[3] != 's' && permissions[3] != 'S') // Shared memory. 89 return false; 90 91 // Pushing then assigning saves us a string copy. 92 regions.push_back(region); 93 regions.back().path.assign(line + path_index); 94 } 95 96 regions_out->swap(regions); 97 return true; 98 } 99 100 } // namespace debug 101 } // namespace base 102