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<MappedMemoryRegion>* regions_out) { |
| 29 std::vector<MappedMemoryRegion> regions; |
| 30 |
| 31 // This isn't async safe nor terribly efficient, but it doesn't need to be at |
| 32 // this point in time. |
| 33 |
| 34 // Split the string by newlines. |
| 35 std::vector<std::string> lines; |
| 36 std::string line = ""; |
| 37 for (size_t i = 0; i < input.size(); i++) { |
| 38 if (input[i] != '\n' && input[i] != '\r') { |
| 39 line.push_back(input[i]); |
| 40 } else if (line.size() > 0) { |
| 41 lines.push_back(line); |
| 42 line.clear(); |
| 43 } |
| 44 } |
| 45 if (line.size() > 0) { |
| 46 BPLOG(ERROR) << "Input doesn't end in newline"; |
| 47 return false; |
| 48 } |
| 49 |
| 50 for (size_t i = 0; i < lines.size(); ++i) { |
| 51 MappedMemoryRegion region; |
| 52 const char* line = lines[i].c_str(); |
| 53 char permissions[5] = {'\0'}; // Ensure NUL-terminated string. |
| 54 int path_index = 0; |
| 55 |
| 56 // Sample format from man 5 proc: |
| 57 // |
| 58 // address perms offset dev inode pathname |
| 59 // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm |
| 60 // |
| 61 // The final %n term captures the offset in the input string, which is used |
| 62 // to determine the path name. It *does not* increment the return value. |
| 63 // Refer to man 3 sscanf for details. |
| 64 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %lx %hhx:%hhx %ld %n", |
| 65 ®ion.start, ®ion.end, permissions, ®ion.offset, |
| 66 ®ion.major_device, ®ion.minor_device, ®ion.inode, |
| 67 &path_index) < 7) { |
| 68 BPLOG(ERROR) << "sscanf failed for line: " << line; |
| 69 return false; |
| 70 } |
| 71 |
| 72 region.permissions = 0; |
| 73 |
| 74 if (permissions[0] == 'r') |
| 75 region.permissions |= MappedMemoryRegion::READ; |
| 76 else if (permissions[0] != '-') |
| 77 return false; |
| 78 |
| 79 if (permissions[1] == 'w') |
| 80 region.permissions |= MappedMemoryRegion::WRITE; |
| 81 else if (permissions[1] != '-') |
| 82 return false; |
| 83 |
| 84 if (permissions[2] == 'x') |
| 85 region.permissions |= MappedMemoryRegion::EXECUTE; |
| 86 else if (permissions[2] != '-') |
| 87 return false; |
| 88 |
| 89 if (permissions[3] == 'p') |
| 90 region.permissions |= MappedMemoryRegion::PRIVATE; |
| 91 else if (permissions[3] != 's' && permissions[3] != 'S') // Shared memory. |
| 92 return false; |
| 93 |
| 94 // Pushing then assigning saves us a string copy. |
| 95 regions.push_back(region); |
| 96 regions.back().path.assign(line + path_index); |
| 97 } |
| 98 |
| 99 regions_out->swap(regions); |
| 100 return true; |
| 101 } |
| 102 |
| 103 } // namespace google_breakpad |
OLD | NEW |