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