Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Side by Side Diff: base/debug/proc_maps.cc

Issue 18178015: Implement /proc/self/maps/ parsing code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add link Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/debug/proc_maps.h"
6
7 #if defined(OS_LINUX)
8 #include <inttypes.h>
9 #endif
10
11 #if defined(OS_ANDROID)
12 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which
13 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int:
14 // https://code.google.com/p/android/issues/detail?id=57218
15 #define SCNxPTR "x"
16 #endif
17
18 #include "base/file_util.h"
19 #include "base/strings/string_split.h"
20
21 namespace base {
22 namespace debug {
Mark Mentovai 2013/07/02 19:36:57 No more nested namespaces in new code.
23
24 #if defined(OS_LINUX) || defined(OS_ANDROID)
25
26 std::string ReadProcMaps() {
Mark Mentovai 2013/07/02 19:36:57 For the sake of performance, this would likely be
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
27 std::string proc_maps;
28 FilePath proc_maps_path("/proc/self/maps");
29 if (!file_util::ReadFileToString(proc_maps_path, &proc_maps))
30 proc_maps.clear();
31 return proc_maps;
32 }
33
34 bool ParseProcMaps(const std::string& input,
35 std::vector<MappedMemoryRegion>* regions_out) {
36 std::vector<MappedMemoryRegion> regions;
37
38 // This isn't async safe nor terribly efficient, but it doesn't need to be at
39 // this point in time.
40 std::vector<std::string> lines;
41 SplitString(input, '\n', &lines);
42
43 for (size_t i = 0; i < lines.size(); ++i) {
44 // Ignore empty lines produced by splitting on '\n'.
Mark Mentovai 2013/07/02 19:36:57 This should only be the last line, right? Since yo
scherkus (not reviewing) 2013/07/02 21:43:01 Gah! I had that code written but ditched it, becau
45 if (lines[i].empty())
46 continue;
47
48 const char* line = lines[i].c_str();
49 uintptr_t start = 0;
50 uintptr_t end = 0;
51 char permissions[4] = {'\0'};
Mark Mentovai 2013/07/02 19:36:57 Recommend [5] so that you always have a NUL-termin
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
52 size_t offset = 0;
Mark Mentovai 2013/07/02 19:36:57 The kernel calls this an unsigned long long.
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
53 uint8 dev_major = 0;
54 uint8 dev_minor = 0;
55 int inode = 0;
Mark Mentovai 2013/07/02 19:36:57 The kernel calls this a long. You can verify the
scherkus (not reviewing) 2013/07/02 21:43:01 Done. Thanks for the tip!
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.
65 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %zx %hhx:%hhx %d %n",
66 &start, &end, permissions, &offset, &dev_major, &dev_minor,
67 &inode, &path_index) < 7) {
jar (doing other things) 2013/07/02 19:58:57 nit: I think you have 8 fields, rather than 7. Ma
scherkus (not reviewing) 2013/07/02 21:43:01 man 3 sscanf has an interesting note about %n: ""
68 return false;
69 }
70
71 MappedMemoryRegion region;
72 region.start = start;
Mark Mentovai 2013/07/02 19:36:57 What’s with the copies? If it’s because there are
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
73 region.end = end;
74 region.offset = offset;
75 region.permissions = 0;
76
77 if (permissions[0] == 'r')
78 region.permissions |= MappedMemoryRegion::READ;
79 else if (permissions[0] != '-')
80 return false;
81
82 if (permissions[1] == 'w')
83 region.permissions |= MappedMemoryRegion::WRITE;
84 else if (permissions[1] != '-')
85 return false;
86
87 if (permissions[2] == 'x')
88 region.permissions |= MappedMemoryRegion::EXECUTE;
89 else if (permissions[2] != '-')
90 return false;
91
92 if (permissions[3] == 'p')
93 region.permissions |= MappedMemoryRegion::PRIVATE;
94 else if (permissions[3] != 's') // Shared memory.
Mark Mentovai 2013/07/02 19:36:57 fs/proc/task_nommu.c nommu_vma_show permits 'S' to
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
95 return false;
96
97 region.path = FilePath(line + path_index);
Mark Mentovai 2013/07/02 19:36:57 Since it might not be a path, maybe FilePath isn’t
scherkus (not reviewing) 2013/07/02 21:43:01 Done.
98 regions.push_back(region);
99 }
100
101 regions_out->swap(regions);
102 return true;
103 }
104
105 #endif
106
107 } // namespace debug
108 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698