ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / base / trace_event / process_memory_maps_dump_provider.cc
blob93feded9e80fe2ba6495d5526abbaf6cd743d719
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 namespace {
19 const char kDumperFriendlyName[] = "ProcessMemoryMaps";
22 #if defined(OS_LINUX) || defined(OS_ANDROID)
23 // static
24 std::istream* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr;
26 namespace {
28 const uint32 kMaxLineSize = 4096;
30 bool ParseSmapsHeader(std::istream* smaps,
31 ProcessMemoryMaps::VMRegion* region) {
32 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n"
33 bool res = true; // Whether this region should be appended or skipped.
34 uint64 end_addr;
35 std::string protection_flags;
36 std::string ignored;
37 *smaps >> std::hex >> region->start_address;
38 smaps->ignore(1);
39 *smaps >> std::hex >> end_addr;
40 if (end_addr > region->start_address) {
41 region->size_in_bytes = end_addr - region->start_address;
42 } else {
43 // This is not just paranoia, it can actually happen (See crbug.com/461237).
44 region->size_in_bytes = 0;
45 res = false;
48 region->protection_flags = 0;
49 *smaps >> protection_flags;
50 CHECK(4UL == protection_flags.size());
51 if (protection_flags[0] == 'r') {
52 region->protection_flags |=
53 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
55 if (protection_flags[1] == 'w') {
56 region->protection_flags |=
57 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
59 if (protection_flags[2] == 'x') {
60 region->protection_flags |=
61 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
63 *smaps >> std::hex >> region->mapped_file_offset;
64 *smaps >> ignored; // Ignore device maj-min (fc:01 in the example above).
65 *smaps >> ignored; // Ignore inode number (1234 in the example above).
67 while (smaps->peek() == ' ')
68 smaps->ignore(1);
69 char mapped_file[kMaxLineSize];
70 smaps->getline(mapped_file, sizeof(mapped_file));
71 region->mapped_file = mapped_file;
73 return res;
76 uint32 ParseSmapsCounter(std::istream* smaps,
77 ProcessMemoryMaps::VMRegion* region) {
78 // e.g., "RSS: 0 Kb\n"
79 uint32 res = 0;
80 std::string counter_name;
81 *smaps >> counter_name;
83 // TODO(primiano): "Swap" should also be accounted as resident. Check
84 // whether Rss isn't already counting swapped and fix below if that is
85 // the case.
86 if (counter_name == "Rss:") {
87 *smaps >> std::dec >> region->byte_stats_resident;
88 region->byte_stats_resident *= 1024;
89 res = 1;
90 } else if (counter_name == "Anonymous:") {
91 *smaps >> std::dec >> region->byte_stats_anonymous;
92 region->byte_stats_anonymous *= 1024;
93 res = 1;
96 #ifndef NDEBUG
97 // Paranoid check against changes of the Kernel /proc interface.
98 if (res) {
99 std::string unit;
100 *smaps >> unit;
101 DCHECK_EQ("kB", unit);
103 #endif
105 smaps->ignore(kMaxLineSize, '\n');
107 return res;
110 uint32 ReadLinuxProcSmapsFile(std::istream* smaps, ProcessMemoryMaps* pmm) {
111 if (!smaps->good())
112 return 0;
114 const uint32 kNumExpectedCountersPerRegion = 2;
115 uint32 counters_parsed_for_current_region = 0;
116 uint32 num_valid_regions = 0;
117 ProcessMemoryMaps::VMRegion region;
118 bool should_add_current_region = false;
119 for (;;) {
120 int next = smaps->peek();
121 if (next == std::ifstream::traits_type::eof() || next == '\n')
122 break;
123 if (isxdigit(next) && !isupper(next)) {
124 region = {0};
125 counters_parsed_for_current_region = 0;
126 should_add_current_region = ParseSmapsHeader(smaps, &region);
127 } else {
128 counters_parsed_for_current_region += ParseSmapsCounter(smaps, &region);
129 DCHECK_LE(counters_parsed_for_current_region,
130 kNumExpectedCountersPerRegion);
131 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) {
132 if (should_add_current_region) {
133 pmm->AddVMRegion(region);
134 ++num_valid_regions;
135 should_add_current_region = false;
140 return num_valid_regions;
143 } // namespace
144 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
146 // static
147 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() {
148 return Singleton<ProcessMemoryMapsDumpProvider,
149 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get();
152 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() {
155 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() {
158 // Called at trace dump point time. Creates a snapshot the memory maps for the
159 // current process.
160 bool ProcessMemoryMapsDumpProvider::DumpInto(ProcessMemoryDump* pmd) {
161 uint32 res = 0;
163 #if defined(OS_LINUX) || defined(OS_ANDROID)
164 if (UNLIKELY(proc_smaps_for_testing)) {
165 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps());
166 } else {
167 std::ifstream proc_self_smaps("/proc/self/smaps");
168 res = ReadLinuxProcSmapsFile(&proc_self_smaps, pmd->process_mmaps());
170 #else
171 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux";
172 #endif
174 if (res > 0) {
175 pmd->set_has_process_mmaps();
176 return true;
179 return false;
182 const char* ProcessMemoryMapsDumpProvider::GetFriendlyName() const {
183 return kDumperFriendlyName;
186 } // namespace trace_event
187 } // namespace base