OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/trace_event/process_memory_maps_dump_provider.h" | 5 #include "base/trace_event/process_memory_maps_dump_provider.h" |
6 | 6 |
7 #include <cctype> | 7 #include "base/logging.h" |
8 #include <fstream> | 8 #include "base/trace_event/process_memory_dump.h" |
9 | 9 |
10 #include "base/logging.h" | 10 #if defined(OS_LINUX) || defined(OS_ANDROID) |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
Don't really need to hide these. Either you mask t
ssid
2015/10/06 18:47:05
Done.
| |
11 #include "base/process/process_metrics.h" | 11 #include "base/format_macros.h" |
12 #include "base/trace_event/process_memory_dump.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/trace_event/process_memory_maps.h" | 13 #include "base/trace_event/process_memory_maps.h" |
14 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace trace_event { | 17 namespace trace_event { |
17 | 18 |
18 #if defined(OS_LINUX) || defined(OS_ANDROID) | 19 #if defined(OS_LINUX) || defined(OS_ANDROID) |
19 // static | 20 // static |
20 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | 21 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const uint32 kMaxLineSize = 4096; | 25 const uint32 kMaxLineSize = 4096; |
25 | 26 |
26 bool ParseSmapsHeader(std::istream* smaps, | 27 bool ParseSmapsHeader(char* header_line, ProcessMemoryMaps::VMRegion* region) { |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
+const -> const char*
ssid
2015/10/06 18:47:05
Done.
| |
27 ProcessMemoryMaps::VMRegion* region) { | |
28 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" | 28 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" |
29 bool res = true; // Whether this region should be appended or skipped. | 29 bool res = true; // Whether this region should be appended or skipped. |
30 uint64 end_addr; | 30 uint64 end_addr = 0; |
31 std::string protection_flags; | 31 char protection_flags[5] = {0}; |
32 std::string ignored; | 32 char mapped_file[kMaxLineSize]; |
33 *smaps >> std::hex >> region->start_address; | 33 |
34 smaps->ignore(1); | 34 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", |
35 *smaps >> std::hex >> end_addr; | 35 ®ion->start_address, &end_addr, protection_flags, |
36 mapped_file) != 4) | |
37 return false; | |
38 | |
36 if (end_addr > region->start_address) { | 39 if (end_addr > region->start_address) { |
37 region->size_in_bytes = end_addr - region->start_address; | 40 region->size_in_bytes = end_addr - region->start_address; |
38 } else { | 41 } else { |
39 // This is not just paranoia, it can actually happen (See crbug.com/461237). | 42 // This is not just paranoia, it can actually happen (See crbug.com/461237). |
40 region->size_in_bytes = 0; | 43 region->size_in_bytes = 0; |
41 res = false; | 44 res = false; |
42 } | 45 } |
43 | 46 |
44 region->protection_flags = 0; | 47 region->protection_flags = 0; |
45 *smaps >> protection_flags; | |
46 CHECK_EQ(4UL, protection_flags.size()); | |
47 if (protection_flags[0] == 'r') { | 48 if (protection_flags[0] == 'r') { |
48 region->protection_flags |= | 49 region->protection_flags |= |
49 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; | 50 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; |
50 } | 51 } |
51 if (protection_flags[1] == 'w') { | 52 if (protection_flags[1] == 'w') { |
52 region->protection_flags |= | 53 region->protection_flags |= |
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; | 54 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; |
54 } | 55 } |
55 if (protection_flags[2] == 'x') { | 56 if (protection_flags[2] == 'x') { |
56 region->protection_flags |= | 57 region->protection_flags |= |
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | 58 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; |
58 } | 59 } |
59 *smaps >> ignored; // Ignore mapped file offset. | |
60 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above). | |
61 *smaps >> ignored; // Ignore inode number (1234 in the example above). | |
62 | 60 |
63 while (smaps->peek() == ' ') | |
64 smaps->ignore(1); | |
65 char mapped_file[kMaxLineSize]; | |
66 smaps->getline(mapped_file, sizeof(mapped_file)); | |
67 region->mapped_file = mapped_file; | 61 region->mapped_file = mapped_file; |
62 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, ®ion->mapped_file); | |
68 | 63 |
69 return res; | 64 return res; |
70 } | 65 } |
71 | 66 |
72 uint64 ReadCounterBytes(std::istream* smaps) { | 67 uint64 ReadCounterBytes(char* counter_line) { |
73 uint64 counter_value = 0; | 68 uint64 counter_value = 0; |
74 *smaps >> std::dec >> counter_value; | 69 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); |
70 CHECK_EQ(1, res); | |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
You can probably relax this to be a DCHECK, no nee
ssid
2015/10/06 18:47:04
Done.
| |
75 return counter_value * 1024; | 71 return counter_value * 1024; |
76 } | 72 } |
77 | 73 |
78 uint32 ParseSmapsCounter(std::istream* smaps, | 74 uint32 ParseSmapsCounter(char* counter_line, |
79 ProcessMemoryMaps::VMRegion* region) { | 75 ProcessMemoryMaps::VMRegion* region) { |
80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" | 76 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" |
81 uint32 res = 1; | 77 uint32 res = 1; |
82 std::string counter_name; | 78 const uint32 kMaxCounterNameSize = 20; |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
Actually doesn't seem you use this anywhere else,
ssid
2015/10/06 18:47:05
Done.
| |
83 *smaps >> counter_name; | 79 char counter_name[kMaxCounterNameSize]; |
80 int did_read = sscanf(counter_line, "%s", counter_name); | |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
replace %s with %19[^\n]s, so this won't overflow
ssid
2015/10/06 18:47:04
Done.
| |
81 DCHECK_EQ(1, did_read); | |
84 | 82 |
85 // TODO(primiano): "Swap" should also be accounted as resident. Check | 83 if (strcmp(counter_name, "Pss:") == 0) { |
86 // whether Rss isn't already counting swapped and fix below if that is | 84 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); |
87 // the case. | 85 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { |
88 if (counter_name == "Pss:") { | 86 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); |
89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps); | 87 } else if (strcmp(counter_name, "Private_Clean:") == 0) { |
90 } else if (counter_name == "Private_Dirty:") { | 88 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); |
91 region->byte_stats_private_dirty_resident = ReadCounterBytes(smaps); | 89 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { |
92 } else if (counter_name == "Private_Clean:") { | 90 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); |
93 region->byte_stats_private_clean_resident = ReadCounterBytes(smaps); | 91 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { |
94 } else if (counter_name == "Shared_Dirty:") { | 92 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); |
95 region->byte_stats_shared_dirty_resident = ReadCounterBytes(smaps); | 93 } else if (strcmp(counter_name, "Swap:") == 0) { |
96 } else if (counter_name == "Shared_Clean:") { | 94 region->byte_stats_swapped = ReadCounterBytes(counter_line); |
97 region->byte_stats_shared_clean_resident = ReadCounterBytes(smaps); | |
98 } else if (counter_name == "Swap:") { | |
99 region->byte_stats_swapped = ReadCounterBytes(smaps); | |
100 } else { | 95 } else { |
101 res = 0; | 96 res = 0; |
102 } | 97 } |
103 | 98 |
104 #ifndef NDEBUG | |
105 // Paranoid check against changes of the Kernel /proc interface. | |
106 if (res) { | |
107 std::string unit; | |
108 *smaps >> unit; | |
109 DCHECK_EQ("kB", unit); | |
110 } | |
111 #endif | |
112 | |
113 smaps->ignore(kMaxLineSize, '\n'); | |
114 | |
115 return res; | 99 return res; |
116 } | 100 } |
117 | 101 |
118 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) { | 102 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { |
119 if (!smaps->good()) | 103 if (!smaps_file) |
120 return 0; | 104 return 0; |
121 | 105 |
106 fseek(smaps_file, 0, SEEK_SET); | |
107 | |
108 char line[kMaxLineSize]; | |
122 const uint32 kNumExpectedCountersPerRegion = 6; | 109 const uint32 kNumExpectedCountersPerRegion = 6; |
123 uint32 counters_parsed_for_current_region = 0; | 110 uint32 counters_parsed_for_current_region = 0; |
124 uint32 num_valid_regions = 0; | 111 uint32 num_valid_regions = 0; |
125 ProcessMemoryMaps::VMRegion region; | 112 ProcessMemoryMaps::VMRegion region; |
126 bool should_add_current_region = false; | 113 bool should_add_current_region = false; |
127 for (;;) { | 114 for (;;) { |
128 int next = smaps->peek(); | 115 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) |
Primiano Tucci (use gerrit)
2015/10/06 16:37:31
maybe set line[0] = '\0' before the fgets so if yo
ssid
2015/10/06 18:47:04
Done.
| |
129 if (next == std::ifstream::traits_type::eof() || next == '\n') | |
130 break; | 116 break; |
131 if (isxdigit(next) && !isupper(next)) { | 117 DCHECK_GT(strlen(line), 0u); |
118 if (isxdigit(line[0]) && !isupper(line[0])) { | |
132 region = ProcessMemoryMaps::VMRegion(); | 119 region = ProcessMemoryMaps::VMRegion(); |
133 counters_parsed_for_current_region = 0; | 120 counters_parsed_for_current_region = 0; |
134 should_add_current_region = ParseSmapsHeader(smaps, ®ion); | 121 should_add_current_region = ParseSmapsHeader(line, ®ion); |
135 } else { | 122 } else { |
136 counters_parsed_for_current_region += ParseSmapsCounter(smaps, ®ion); | 123 counters_parsed_for_current_region += ParseSmapsCounter(line, ®ion); |
137 DCHECK_LE(counters_parsed_for_current_region, | 124 DCHECK_LE(counters_parsed_for_current_region, |
138 kNumExpectedCountersPerRegion); | 125 kNumExpectedCountersPerRegion); |
139 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | 126 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { |
140 if (should_add_current_region) { | 127 if (should_add_current_region) { |
141 pmm->AddVMRegion(region); | 128 pmm->AddVMRegion(region); |
142 ++num_valid_regions; | 129 ++num_valid_regions; |
143 should_add_current_region = false; | 130 should_add_current_region = false; |
144 } | 131 } |
145 } | 132 } |
146 } | 133 } |
(...skipping 23 matching lines...) Expand all Loading... | |
170 // Snapshot of memory maps is not taken for light dump requests. | 157 // Snapshot of memory maps is not taken for light dump requests. |
171 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) | 158 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) |
172 return true; | 159 return true; |
173 | 160 |
174 uint32 res = 0; | 161 uint32 res = 0; |
175 | 162 |
176 #if defined(OS_LINUX) || defined(OS_ANDROID) | 163 #if defined(OS_LINUX) || defined(OS_ANDROID) |
177 if (UNLIKELY(proc_smaps_for_testing)) { | 164 if (UNLIKELY(proc_smaps_for_testing)) { |
178 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 165 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); |
179 } else { | 166 } else { |
180 std::ifstream proc_self_smaps("/proc/self/smaps"); | 167 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); |
181 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps()); | 168 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
182 } | 169 } |
183 #else | 170 #else |
184 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; | 171 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; |
185 #endif | 172 #endif |
186 | 173 |
187 if (res > 0) { | 174 if (res > 0) { |
188 pmd->set_has_process_mmaps(); | 175 pmd->set_has_process_mmaps(); |
189 return true; | 176 return true; |
190 } | 177 } |
191 | 178 |
192 return false; | 179 return false; |
193 } | 180 } |
194 | 181 |
195 } // namespace trace_event | 182 } // namespace trace_event |
196 } // namespace base | 183 } // namespace base |
OLD | NEW |