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 #ifndef __STDC_FORMAT_MACROS
6 #define __STDC_FORMAT_MACROS
7 #endif
8 
9 #include "google_breakpad/processor/proc_maps_linux.h"
10 
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <stdio.h>
14 
15 #include "common/using_std_string.h"
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 
ParseProcMaps(const string & input,std::vector<MappedMemoryRegion> * regions_out)29 bool ParseProcMaps(const 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 
36   // Split the string by newlines.
37   std::vector<string> lines;
38   string l = "";
39   for (size_t i = 0; i < input.size(); i++) {
40     if (input[i] != '\n' && input[i] != '\r') {
41       l.push_back(input[i]);
42     } else if (l.size() > 0) {
43       lines.push_back(l);
44       l.clear();
45     }
46   }
47   if (l.size() > 0) {
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.
66     if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4c %" SCNx64" %hhx:%hhx %"
67                SCNd64 " %n", &region.start, &region.end, permissions,
68                &region.offset, &region.major_device, &region.minor_device,
69                &region.inode, &path_index) < 7) {
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);
99     regions.back().line.assign(line);
100   }
101 
102   regions_out->swap(regions);
103   return true;
104 }
105 
106 }  // namespace google_breakpad
107