Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / base / trace_event / process_memory_maps_dump_provider.cc
blob680fa29609ebdf042b1bc5db69a33515cec6d5ad
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
3 // found in the LICENSE file.
5 #include "base/trace_event/process_memory_maps_dump_provider.h"
7 #include <cctype>
8 #include <fstream>
10 #include "base/logging.h"
11 #include "base/process/process_metrics.h"
12 #include "base/trace_event/process_memory_dump.h"
13 #include "base/trace_event/process_memory_maps.h"
15 namespace base {
16 namespace trace_event {
18 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 // static
20 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr;
22 namespace {
24 const uint32 kMaxLineSize = 4096;
26 bool ParseSmapsHeader(std::istream* smaps,
27 ProcessMemoryMaps::VMRegion* region) {
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.
30 uint64 end_addr;
31 std::string protection_flags;
32 std::string ignored;
33 *smaps >> std::hex >> region->start_address;
34 smaps->ignore(1);
35 *smaps >> std::hex >> end_addr;
36 if (end_addr > region->start_address) {
37 region->size_in_bytes = end_addr - region->start_address;
38 } else {
39 // This is not just paranoia, it can actually happen (See crbug.com/461237).
40 region->size_in_bytes = 0;
41 res = false;
44 region->protection_flags = 0;
45 *smaps >> protection_flags;
46 CHECK_EQ(4UL, protection_flags.size());
47 if (protection_flags[0] == 'r') {
48 region->protection_flags |=
49 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
51 if (protection_flags[1] == 'w') {
52 region->protection_flags |=
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
55 if (protection_flags[2] == 'x') {
56 region->protection_flags |=
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
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).
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;
69 return res;
72 uint64 ReadCounterBytes(std::istream* smaps) {
73 uint64 counter_value = 0;
74 *smaps >> std::dec >> counter_value;
75 return counter_value * 1024;
78 uint32 ParseSmapsCounter(std::istream* smaps,
79 ProcessMemoryMaps::VMRegion* region) {
80 // A smaps counter lines looks as follows: "RSS: 0 Kb\n"
81 uint32 res = 0;
82 std::string counter_name;
83 *smaps >> counter_name;
85 // TODO(primiano): "Swap" should also be accounted as resident. Check
86 // whether Rss isn't already counting swapped and fix below if that is
87 // the case.
88 if (counter_name == "Pss:") {
89 region->byte_stats_proportional_resident = ReadCounterBytes(smaps);
90 res = 1;
91 } else if (counter_name == "Private_Dirty:" ||
92 counter_name == "Private_Clean:") {
93 // For Private and Shared counters keep the sum of the dirty + clean stats.
94 region->byte_stats_private_resident += ReadCounterBytes(smaps);
95 res = 1;
96 } else if (counter_name == "Shared_Dirty:" ||
97 counter_name == "Shared_Clean:") {
98 region->byte_stats_shared_resident += ReadCounterBytes(smaps);
99 res = 1;
102 #ifndef NDEBUG
103 // Paranoid check against changes of the Kernel /proc interface.
104 if (res) {
105 std::string unit;
106 *smaps >> unit;
107 DCHECK_EQ("kB", unit);
109 #endif
111 smaps->ignore(kMaxLineSize, '\n');
113 return res;
116 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) {
117 if (!smaps->good())
118 return 0;
120 const uint32 kNumExpectedCountersPerRegion = 5;
121 uint32 counters_parsed_for_current_region = 0;
122 uint32 num_valid_regions = 0;
123 ProcessMemoryMaps::VMRegion region;
124 bool should_add_current_region = false;
125 for (;;) {
126 int next = smaps->peek();
127 if (next == std::ifstream::traits_type::eof() || next == '\n')
128 break;
129 if (isxdigit(next) && !isupper(next)) {
130 region = {0};
131 counters_parsed_for_current_region = 0;
132 should_add_current_region = ParseSmapsHeader(smaps, &region);
133 } else {
134 counters_parsed_for_current_region += ParseSmapsCounter(smaps, &region);
135 DCHECK_LE(counters_parsed_for_current_region,
136 kNumExpectedCountersPerRegion);
137 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) {
138 if (should_add_current_region) {
139 pmm->AddVMRegion(region);
140 ++num_valid_regions;
141 should_add_current_region = false;
146 return num_valid_regions;
149 } // namespace
150 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
152 // static
153 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() {
154 return Singleton<ProcessMemoryMapsDumpProvider,
155 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get();
158 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() {
161 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() {
164 // Called at trace dump point time. Creates a snapshot the memory maps for the
165 // current process.
166 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) {
167 uint32 res = 0;
169 #if defined(OS_LINUX) || defined(OS_ANDROID)
170 if (UNLIKELY(proc_smaps_for_testing)) {
171 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps());
172 } else {
173 std::ifstream proc_self_smaps("/proc/self/smaps");
174 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps());
176 #else
177 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux";
178 #endif
180 if (res > 0) {
181 pmd->set_has_process_mmaps();
182 return true;
185 return false;
188 } // namespace trace_event
189 } // namespace base