Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 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 | |
primiano@chromium.org | 3bc3dad | 2015-08-19 07:24:21 +0000 | [diff] [blame] | 5 | #ifndef __STDC_FORMAT_MACROS |
| 6 | #define __STDC_FORMAT_MACROS |
| 7 | #endif |
| 8 | |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 9 | #include "google_breakpad/processor/proc_maps_linux.h" |
| 10 | |
| 11 | #include <fcntl.h> |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 12 | #include <inttypes.h> |
ted.mielczarek@gmail.com | dc5c386 | 2015-08-17 11:55:15 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 14 | |
Ivan Penkov | 704f41e | 2016-08-30 14:00:56 -0700 | [diff] [blame] | 15 | #include "common/using_std_string.h" |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 16 | #include "processor/logging.h" |
| 17 | |
| 18 | #if defined(OS_ANDROID) && !defined(__LP64__) |
| 19 | // In 32-bit mode, Bionic's inttypes.h defines PRI/SCNxPTR as an |
| 20 | // unsigned long int, which is incompatible with Bionic's stdint.h |
| 21 | // defining uintptr_t as an unsigned int: |
| 22 | // https://code.google.com/p/android/issues/detail?id=57218 |
| 23 | #undef SCNxPTR |
| 24 | #define SCNxPTR "x" |
| 25 | #endif |
| 26 | |
| 27 | namespace google_breakpad { |
| 28 | |
Ivan Penkov | 704f41e | 2016-08-30 14:00:56 -0700 | [diff] [blame] | 29 | bool ParseProcMaps(const string& input, |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 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 | |
| 36 | // Split the string by newlines. |
Ivan Penkov | 704f41e | 2016-08-30 14:00:56 -0700 | [diff] [blame] | 37 | std::vector<string> lines; |
| 38 | string l = ""; |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 39 | for (size_t i = 0; i < input.size(); i++) { |
| 40 | if (input[i] != '\n' && input[i] != '\r') { |
Ted Mielczarek | dbf56c5 | 2015-10-06 08:03:57 -0400 | [diff] [blame] | 41 | l.push_back(input[i]); |
| 42 | } else if (l.size() > 0) { |
| 43 | lines.push_back(l); |
| 44 | l.clear(); |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
Ted Mielczarek | dbf56c5 | 2015-10-06 08:03:57 -0400 | [diff] [blame] | 47 | if (l.size() > 0) { |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 48 | BPLOG(ERROR) << "Input doesn't end in newline"; |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | for (size_t i = 0; i < lines.size(); ++i) { |
| 53 | MappedMemoryRegion region; |
| 54 | const char* line = lines[i].c_str(); |
| 55 | char permissions[5] = {'\0'}; // Ensure NUL-terminated string. |
| 56 | int path_index = 0; |
| 57 | |
| 58 | // Sample format from man 5 proc: |
| 59 | // |
| 60 | // address perms offset dev inode pathname |
| 61 | // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm |
| 62 | // |
| 63 | // The final %n term captures the offset in the input string, which is used |
| 64 | // to determine the path name. It *does not* increment the return value. |
| 65 | // Refer to man 3 sscanf for details. |
Liu.andrew.x@gmail.com | 8794e39 | 2015-08-13 20:13:55 +0000 | [diff] [blame] | 66 | if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4c %" SCNx64" %hhx:%hhx %" |
| 67 | SCNd64 " %n", ®ion.start, ®ion.end, permissions, |
| 68 | ®ion.offset, ®ion.major_device, ®ion.minor_device, |
| 69 | ®ion.inode, &path_index) < 7) { |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 70 | BPLOG(ERROR) << "sscanf failed for line: " << line; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 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' && permissions[3] != 'S') // Shared memory. |
| 94 | return false; |
| 95 | |
| 96 | // Pushing then assigning saves us a string copy. |
| 97 | regions.push_back(region); |
| 98 | regions.back().path.assign(line + path_index); |
Liu.andrew.x@gmail.com | 6a1a906 | 2015-08-11 16:05:48 +0000 | [diff] [blame] | 99 | regions.back().line.assign(line); |
Liu.andrew.x@gmail.com | 2997f45 | 2015-07-28 00:53:44 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | regions_out->swap(regions); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | } // namespace google_breakpad |