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 "google_breakpad/processor/proc_maps_linux.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <stdio.h> | |
| 9 | |
| 10 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 11 #include <inttypes.h> | |
| 12 #endif | |
| 13 | |
| 14 #include "processor/logging.h" | |
| 15 | |
| 16 #if defined(OS_ANDROID) && !defined(__LP64__) | |
| 17 // In 32-bit mode, Bionic's inttypes.h defines PRI/SCNxPTR as an | |
| 18 // unsigned long int, which is incompatible with Bionic's stdint.h | |
| 19 // defining uintptr_t as an unsigned int: | |
| 20 // https://code.google.com/p/android/issues/detail?id=57218 | |
| 21 #undef SCNxPTR | |
| 22 #define SCNxPTR "x" | |
| 23 #endif | |
| 24 | |
| 25 namespace google_breakpad { | |
| 26 | |
| 27 bool ParseProcMaps(const std::string& input, | |
| 28 std::vector<google_breakpad::MappedMemoryRegion> | |
|
ivanpe
2015/07/28 00:18:57
Do you really need to specify the namespace here a
| |
| 29 *regions_out) { | |
| 30 std::vector<google_breakpad::MappedMemoryRegion> regions; | |
| 31 | |
| 32 // This isn't async safe nor terribly efficient, but it doesn't need to be at | |
| 33 // this point in time. | |
| 34 | |
| 35 // Split the string by newlines. | |
| 36 std::vector<std::string> lines; | |
| 37 std::string line = ""; | |
| 38 for (size_t i = 0; i < input.size(); i++) { | |
| 39 if (input[i] != '\n' && input[i] != '\r') { | |
| 40 line.push_back(input[i]); | |
| 41 } else if (line.size() > 0) { | |
| 42 lines.push_back(line); | |
| 43 line.clear(); | |
| 44 } | |
| 45 } | |
| 46 if (line.size() > 0) { | |
| 47 BPLOG(ERROR) << "Input doesn't end in newline"; | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 for (size_t i = 0; i < lines.size(); ++i) { | |
| 52 google_breakpad::MappedMemoryRegion region; | |
| 53 const char* line = lines[i].c_str(); | |
| 54 char permissions[5] = {'\0'}; // Ensure NUL-terminated string. | |
| 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. It *does not* increment the return value. | |
| 64 // Refer to man 3 sscanf for details. | |
| 65 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %lx %hhx:%hhx %ld %n", | |
| 66 ®ion.start, ®ion.end, permissions, ®ion.offset, | |
| 67 ®ion.major_device, ®ion.minor_device, ®ion.inode, | |
| 68 &path_index) < 7) { | |
| 69 BPLOG(ERROR) << "sscanf failed for line: " << line; | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 region.permissions = 0; | |
| 74 | |
| 75 if (permissions[0] == 'r') | |
| 76 region.permissions |= google_breakpad::MappedMemoryRegion::READ; | |
| 77 else if (permissions[0] != '-') | |
| 78 return false; | |
| 79 | |
| 80 if (permissions[1] == 'w') | |
| 81 region.permissions |= google_breakpad::MappedMemoryRegion::WRITE; | |
| 82 else if (permissions[1] != '-') | |
| 83 return false; | |
| 84 | |
| 85 if (permissions[2] == 'x') | |
| 86 region.permissions |= google_breakpad::MappedMemoryRegion::EXECUTE; | |
| 87 else if (permissions[2] != '-') | |
| 88 return false; | |
| 89 | |
| 90 if (permissions[3] == 'p') | |
| 91 region.permissions |= google_breakpad::MappedMemoryRegion::PRIVATE; | |
| 92 else if (permissions[3] != 's' && permissions[3] != 'S') // Shared memory. | |
| 93 return false; | |
| 94 | |
| 95 // Pushing then assigning saves us a string copy. | |
| 96 regions.push_back(region); | |
| 97 regions.back().path.assign(line + path_index); | |
| 98 } | |
| 99 | |
| 100 regions_out->swap(regions); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 } // namespace google_breakpad | |
| OLD | NEW |