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

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

Issue 18661009: Update ReadProcMaps() to reflect lack of atomicity when reading /proc/self/maps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes Created 7 years, 3 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
« no previous file with comments | « base/debug/proc_maps_linux.h ('k') | base/debug/proc_maps_linux_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/debug/proc_maps_linux.h" 5 #include "base/debug/proc_maps_linux.h"
6 6
7 #include <fcntl.h>
8
7 #if defined(OS_LINUX) 9 #if defined(OS_LINUX)
8 #include <inttypes.h> 10 #include <inttypes.h>
9 #endif 11 #endif
10 12
11 #include "base/file_util.h" 13 #include "base/file_util.h"
12 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
13 15
14 #if defined(OS_ANDROID) 16 #if defined(OS_ANDROID)
15 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which 17 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which
16 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int: 18 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int:
17 // https://code.google.com/p/android/issues/detail?id=57218 19 // https://code.google.com/p/android/issues/detail?id=57218
18 #undef SCNxPTR 20 #undef SCNxPTR
19 #define SCNxPTR "x" 21 #define SCNxPTR "x"
20 #endif 22 #endif
21 23
22 namespace base { 24 namespace base {
23 namespace debug { 25 namespace debug {
24 26
27 // Scans |proc_maps| starting from |pos| returning true if the gate VMA was
28 // found, otherwise returns false.
29 static bool ContainsGateVMA(std::string* proc_maps, size_t pos) {
30 #if defined(ARCH_CPU_ARM_FAMILY)
31 // The gate VMA on ARM kernels is the interrupt vectors page.
32 return proc_maps->find(" [vectors]\n", pos) != std::string::npos;
33 #elif defined(ARCH_CPU_X86_64)
34 // The gate VMA on x86 64-bit kernels is the virtual system call page.
35 return proc_maps->find(" [vsyscall]\n", pos) != std::string::npos;
36 #else
37 // Otherwise assume there is no gate VMA in which case we shouldn't
38 // get duplicate entires.
39 return false;
40 #endif
41 }
42
25 bool ReadProcMaps(std::string* proc_maps) { 43 bool ReadProcMaps(std::string* proc_maps) {
26 FilePath proc_maps_path("/proc/self/maps"); 44 // seq_file only writes out a page-sized amount on each call. Refer to header
27 return ReadFileToString(proc_maps_path, proc_maps); 45 // file for details.
46 const long kBufferSize = sysconf(_SC_PAGESIZE);
47 scoped_ptr<char[]> buffer(new char[kBufferSize]);
48
49 int fd = HANDLE_EINTR(open("/proc/self/maps", O_RDONLY));
50 if (fd == -1) {
51 DPLOG(ERROR) << "Couldn't open /proc/self/maps";
52 return false;
53 }
54 file_util::ScopedFD fd_closer(&fd);
55 proc_maps->clear();
56
57 while (true) {
58 ssize_t bytes_read = HANDLE_EINTR(read(fd, buffer.get(), kBufferSize));
Mark Mentovai 2013/09/05 17:52:43 I suppose it doesn’t really matter for this non-pe
scherkus (not reviewing) 2013/09/05 19:01:40 I like it. Done.
59 if (bytes_read < 0) {
60 DPLOG(ERROR) << "Couldn't read /proc/self/maps";
61 return false;
62 }
63
64 if (bytes_read == 0)
65 break;
66
67 // Use 2-arg version of append() as |buffer| *is not* NUL-terminated. This
68 // also avoids an unnecessary length computation.
69 size_t pos = proc_maps->size();
70 proc_maps->append(buffer.get(), bytes_read);
71
72 // The gate VMA is handled as a special case after seq_file has finished
73 // iterating through all entries in the virtual memory table.
74 //
75 // Unfortunately, if additional entries are added at this point in time
76 // seq_file gets confused and the next call to read() will return duplicate
77 // entries including the gate VMA again.
78 //
79 // Avoid this by searching for the gate VMA and breaking early.
80 if (ContainsGateVMA(proc_maps, pos))
81 break;
82 }
83
84 return true;
28 } 85 }
29 86
30 bool ParseProcMaps(const std::string& input, 87 bool ParseProcMaps(const std::string& input,
31 std::vector<MappedMemoryRegion>* regions_out) { 88 std::vector<MappedMemoryRegion>* regions_out) {
32 std::vector<MappedMemoryRegion> regions; 89 std::vector<MappedMemoryRegion> regions;
33 90
34 // This isn't async safe nor terribly efficient, but it doesn't need to be at 91 // This isn't async safe nor terribly efficient, but it doesn't need to be at
35 // this point in time. 92 // this point in time.
36 std::vector<std::string> lines; 93 std::vector<std::string> lines;
37 SplitString(input, '\n', &lines); 94 SplitString(input, '\n', &lines);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 regions.push_back(region); 149 regions.push_back(region);
93 regions.back().path.assign(line + path_index); 150 regions.back().path.assign(line + path_index);
94 } 151 }
95 152
96 regions_out->swap(regions); 153 regions_out->swap(regions);
97 return true; 154 return true;
98 } 155 }
99 156
100 } // namespace debug 157 } // namespace debug
101 } // namespace base 158 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/proc_maps_linux.h ('k') | base/debug/proc_maps_linux_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698