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 bool ParseProcMaps(const std::string& input, |
| 26 std::vector<MappedMemoryRegion>* regions_out) { |
| 27 std::vector<MappedMemoryRegion> regions; |
| 28 |
| 29 // This isn't async safe nor terribly efficient, but it doesn't need to be at |
| 30 // this point in time. |
| 31 |
| 32 // Split the string by newlines. |
| 33 std::vector<std::string> lines; |
| 34 std::string line = ""; |
| 35 for (size_t i = 0; i < input.size(); i++) { |
| 36 if (input[i] != '\n' && input[i] != '\r') { |
| 37 line.push_back(input[i]); |
| 38 } else if (line.size() > 0) { |
| 39 lines.push_back(line); |
| 40 line.clear(); |
| 41 } |
| 42 } |
| 43 if (line.size() > 0) { |
| 44 BPLOG(ERROR) << "Input doesn't end in newline"; |
| 45 return false; |
| 46 } |
| 47 |
| 48 for (size_t i = 0; i < lines.size(); ++i) { |
| 49 MappedMemoryRegion region; |
| 50 const char* line = lines[i].c_str(); |
| 51 char permissions[5] = {'\0'}; // Ensure NUL-terminated string. |
| 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 ®ion.major_device, ®ion.minor_device, ®ion.inode, |
| 65 &path_index) < 7) { |
| 66 BPLOG(ERROR) << "sscanf failed for line: " << line; |
| 67 return false; |
| 68 } |
| 69 |
| 70 region.permissions = 0; |
| 71 |
| 72 if (permissions[0] == 'r') |
| 73 region.permissions |= MappedMemoryRegion::READ; |
| 74 else if (permissions[0] != '-') |
| 75 return false; |
| 76 |
| 77 if (permissions[1] == 'w') |
| 78 region.permissions |= MappedMemoryRegion::WRITE; |
| 79 else if (permissions[1] != '-') |
| 80 return false; |
| 81 |
| 82 if (permissions[2] == 'x') |
| 83 region.permissions |= MappedMemoryRegion::EXECUTE; |
| 84 else if (permissions[2] != '-') |
| 85 return false; |
| 86 |
| 87 if (permissions[3] == 'p') |
| 88 region.permissions |= MappedMemoryRegion::PRIVATE; |
| 89 else if (permissions[3] != 's' && permissions[3] != 'S') // Shared memory. |
| 90 return false; |
| 91 |
| 92 // Pushing then assigning saves us a string copy. |
| 93 regions.push_back(region); |
| 94 regions.back().path.assign(line + path_index); |
| 95 } |
| 96 |
| 97 regions_out->swap(regions); |
| 98 return true; |
| 99 } |
OLD | NEW |