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

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

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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
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"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 16
16 #if defined(OS_ANDROID) 17 #if defined(OS_ANDROID)
17 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which 18 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which
18 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int: 19 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int:
19 // https://code.google.com/p/android/issues/detail?id=57218 20 // https://code.google.com/p/android/issues/detail?id=57218
20 #undef SCNxPTR 21 #undef SCNxPTR
21 #define SCNxPTR "x" 22 #define SCNxPTR "x"
22 #endif 23 #endif
23 24
(...skipping 14 matching lines...) Expand all
38 // get duplicate entires. 39 // get duplicate entires.
39 return false; 40 return false;
40 #endif 41 #endif
41 } 42 }
42 43
43 bool ReadProcMaps(std::string* proc_maps) { 44 bool ReadProcMaps(std::string* proc_maps) {
44 // seq_file only writes out a page-sized amount on each call. Refer to header 45 // seq_file only writes out a page-sized amount on each call. Refer to header
45 // file for details. 46 // file for details.
46 const long kReadSize = sysconf(_SC_PAGESIZE); 47 const long kReadSize = sysconf(_SC_PAGESIZE);
47 48
48 int fd = HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)); 49 base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)));
49 if (fd == -1) { 50 if (!fd.is_valid()) {
50 DPLOG(ERROR) << "Couldn't open /proc/self/maps"; 51 DPLOG(ERROR) << "Couldn't open /proc/self/maps";
51 return false; 52 return false;
52 } 53 }
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, buffer, kReadSize)); 63 ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), 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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698