OLD | NEW |
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> | 7 #include <fcntl.h> |
8 | 8 |
9 #if defined(OS_LINUX) | 9 #if defined(OS_LINUX) |
10 #include <inttypes.h> | 10 #include <inttypes.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/files/scoped_file.h" | |
15 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
16 | 15 |
17 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
18 // 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 |
19 // 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: |
20 // https://code.google.com/p/android/issues/detail?id=57218 | 19 // https://code.google.com/p/android/issues/detail?id=57218 |
21 #undef SCNxPTR | 20 #undef SCNxPTR |
22 #define SCNxPTR "x" | 21 #define SCNxPTR "x" |
23 #endif | 22 #endif |
24 | 23 |
(...skipping 14 matching lines...) Expand all Loading... |
39 // get duplicate entires. | 38 // get duplicate entires. |
40 return false; | 39 return false; |
41 #endif | 40 #endif |
42 } | 41 } |
43 | 42 |
44 bool ReadProcMaps(std::string* proc_maps) { | 43 bool ReadProcMaps(std::string* proc_maps) { |
45 // seq_file only writes out a page-sized amount on each call. Refer to header | 44 // seq_file only writes out a page-sized amount on each call. Refer to header |
46 // file for details. | 45 // file for details. |
47 const long kReadSize = sysconf(_SC_PAGESIZE); | 46 const long kReadSize = sysconf(_SC_PAGESIZE); |
48 | 47 |
49 base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY))); | 48 int fd = HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)); |
50 if (!fd.is_valid()) { | 49 if (fd == -1) { |
51 DPLOG(ERROR) << "Couldn't open /proc/self/maps"; | 50 DPLOG(ERROR) << "Couldn't open /proc/self/maps"; |
52 return false; | 51 return false; |
53 } | 52 } |
| 53 file_util::ScopedFD fd_closer(&fd); |
54 proc_maps->clear(); | 54 proc_maps->clear(); |
55 | 55 |
56 while (true) { | 56 while (true) { |
57 // To avoid a copy, resize |proc_maps| so read() can write directly into it. | 57 // To avoid a copy, resize |proc_maps| so read() can write directly into it. |
58 // Compute |buffer| afterwards since resize() may reallocate. | 58 // Compute |buffer| afterwards since resize() may reallocate. |
59 size_t pos = proc_maps->size(); | 59 size_t pos = proc_maps->size(); |
60 proc_maps->resize(pos + kReadSize); | 60 proc_maps->resize(pos + kReadSize); |
61 void* buffer = &(*proc_maps)[pos]; | 61 void* buffer = &(*proc_maps)[pos]; |
62 | 62 |
63 ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, kReadSize)); | 63 ssize_t bytes_read = HANDLE_EINTR(read(fd, buffer, kReadSize)); |
64 if (bytes_read < 0) { | 64 if (bytes_read < 0) { |
65 DPLOG(ERROR) << "Couldn't read /proc/self/maps"; | 65 DPLOG(ERROR) << "Couldn't read /proc/self/maps"; |
66 proc_maps->clear(); | 66 proc_maps->clear(); |
67 return false; | 67 return false; |
68 } | 68 } |
69 | 69 |
70 // ... and don't forget to trim off excess bytes. | 70 // ... and don't forget to trim off excess bytes. |
71 proc_maps->resize(pos + bytes_read); | 71 proc_maps->resize(pos + bytes_read); |
72 | 72 |
73 if (bytes_read == 0) | 73 if (bytes_read == 0) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 regions.push_back(region); | 153 regions.push_back(region); |
154 regions.back().path.assign(line + path_index); | 154 regions.back().path.assign(line + path_index); |
155 } | 155 } |
156 | 156 |
157 regions_out->swap(regions); | 157 regions_out->swap(regions); |
158 return true; | 158 return true; |
159 } | 159 } |
160 | 160 |
161 } // namespace debug | 161 } // namespace debug |
162 } // namespace base | 162 } // namespace base |
OLD | NEW |