Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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.h" | |
| 6 | |
| 7 #if defined(OS_LINUX) | |
| 8 #include <inttypes.h> | |
| 9 #endif | |
| 10 | |
| 11 #if defined(OS_ANDROID) | |
| 12 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which | |
| 13 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int. | |
| 14 #define SCNxPTR "x" | |
|
scherkus (not reviewing)
2013/07/01 22:01:01
digit: any ideas on Bionic stdint.h vs inttypes.h?
digit
2013/07/02 09:24:18
Looks like a real bug. I just filed https://code.g
scherkus (not reviewing)
2013/07/02 18:21:00
Thanks for taking a look. Updated CL to include li
| |
| 15 #endif | |
| 16 | |
| 17 #include "base/file_util.h" | |
| 18 #include "base/strings/string_split.h" | |
| 19 | |
| 20 namespace base { | |
| 21 namespace debug { | |
| 22 | |
| 23 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 24 | |
| 25 std::string ReadProcMaps() { | |
| 26 std::string proc_maps; | |
| 27 FilePath proc_maps_path("/proc/self/maps"); | |
| 28 if (!file_util::ReadFileToString(proc_maps_path, &proc_maps)) | |
| 29 proc_maps.clear(); | |
| 30 return proc_maps; | |
| 31 } | |
| 32 | |
| 33 bool ParseProcMaps(const std::string& input, | |
| 34 std::vector<MappedMemoryRegion>* regions_out) { | |
| 35 std::vector<MappedMemoryRegion> regions; | |
| 36 | |
| 37 // This isn't async safe nor terribly efficient, but it doesn't need to be at | |
| 38 // this point in time. | |
| 39 std::vector<std::string> lines; | |
| 40 SplitString(input, '\n', &lines); | |
| 41 | |
| 42 for (size_t i = 0; i < lines.size(); ++i) { | |
| 43 // Ignore empty lines produced by splitting on '\n'. | |
| 44 if (lines[i].empty()) | |
| 45 continue; | |
| 46 | |
| 47 const char* line = lines[i].c_str(); | |
| 48 uintptr_t start = 0; | |
| 49 uintptr_t end = 0; | |
| 50 char permissions[4] = {'\0'}; | |
| 51 size_t offset = 0; | |
| 52 uint8 dev_major = 0; | |
| 53 uint8 dev_minor = 0; | |
| 54 int inode = 0; | |
| 55 int path_index = 0; | |
| 56 | |
| 57 // Sample format from man 5 proc: | |
| 58 // | |
| 59 // address perms offset dev inode pathname | |
| 60 // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm | |
| 61 // | |
| 62 // The final %n term captures the offset in the input string, which is used | |
| 63 // to determine the path name. | |
| 64 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %zx %hhx:%hhx %d %n", | |
| 65 &start, &end, permissions, &offset, &dev_major, &dev_minor, | |
| 66 &inode, &path_index) < 7) { | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 MappedMemoryRegion region; | |
| 71 region.start = start; | |
| 72 region.end = end; | |
| 73 region.offset = offset; | |
| 74 region.permissions = 0; | |
| 75 | |
| 76 if (permissions[0] == 'r') | |
| 77 region.permissions |= MappedMemoryRegion::READ; | |
| 78 else if (permissions[0] != '-') | |
| 79 return false; | |
| 80 | |
| 81 if (permissions[1] == 'w') | |
| 82 region.permissions |= MappedMemoryRegion::WRITE; | |
| 83 else if (permissions[1] != '-') | |
| 84 return false; | |
| 85 | |
| 86 if (permissions[2] == 'x') | |
| 87 region.permissions |= MappedMemoryRegion::EXECUTE; | |
| 88 else if (permissions[2] != '-') | |
| 89 return false; | |
| 90 | |
| 91 if (permissions[3] == 'p') | |
| 92 region.permissions |= MappedMemoryRegion::PRIVATE; | |
| 93 else if (permissions[3] != 's') // Shared memory. | |
| 94 return false; | |
| 95 | |
| 96 region.path = FilePath(line + path_index); | |
| 97 regions.push_back(region); | |
| 98 } | |
| 99 | |
| 100 regions_out->swap(regions); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 #endif | |
| 105 | |
| 106 } // namespace debug | |
| 107 } // namespace base | |
| OLD | NEW |