OLD | NEW |
---|---|
(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 #include <inttypes.h> | |
8 | |
9 #include "base/file_util.h" | |
10 #include "base/strings/string_split.h" | |
11 | |
12 namespace base { | |
13 namespace debug { | |
14 | |
15 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
16 | |
17 std::string ReadProcMaps() { | |
18 std::string proc_maps; | |
19 FilePath proc_maps_path("/proc/self/maps"); | |
20 if (!file_util::ReadFileToString(proc_maps_path, &proc_maps)) | |
21 proc_maps.clear(); | |
22 return proc_maps; | |
23 } | |
24 | |
25 void ParseProcMaps(const std::string& input, | |
26 std::vector<MappedMemoryRegion>* regions) { | |
27 // Sample format from man 5 proc: | |
28 // | |
29 // address perms offset dev inode pathname | |
30 // 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm | |
31 // | |
32 // The final %n term captures the offset in the input string, which is used | |
33 // to determine the path name. | |
34 static const char kProcMapFormat[] = | |
satorux1
2013/07/01 03:59:56
remove static? function-local 'static' is not guar
scherkus (not reviewing)
2013/07/01 21:59:45
Done.
| |
35 "%" SCNxPTR "-%" SCNxPTR " %*s %zx %*x:%*x %*d %n"; | |
satorux1
2013/07/01 03:59:56
didn't know about SCNxPTR. :)
scherkus (not reviewing)
2013/07/01 21:59:45
yeah I didn't either until I starting looking thro
| |
36 | |
37 regions->clear(); | |
38 | |
39 // This isn't async safe nor terribly efficient, but it doesn't need to be at | |
40 // this point in time. | |
41 std::vector<std::string> lines; | |
42 SplitString(input, '\n', &lines); | |
43 | |
44 for (size_t i = 0; i < lines.size(); ++i) { | |
45 const char* line = lines[i].c_str(); | |
46 uintptr_t start = 0; | |
47 uintptr_t end = 0; | |
48 size_t offset = 0; | |
49 int path_index = 0; | |
50 | |
51 if (sscanf(line, kProcMapFormat, &start, &end, &offset, &path_index) < 3) | |
52 continue; | |
53 | |
54 MappedMemoryRegion region; | |
55 region.start = start; | |
56 region.end = end; | |
57 region.offset = offset; | |
58 region.path = FilePath(line + path_index); | |
59 regions->push_back(region); | |
60 } | |
61 } | |
62 | |
63 #endif | |
64 | |
65 } // namespace debug | |
66 } // namespace base | |
OLD | NEW |