| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/trace_event/process_memory_maps_dump_provider.h" | |
| 6 | |
| 7 #include "base/files/scoped_file.h" | |
| 8 #include "base/format_macros.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/trace_event/process_memory_dump.h" | |
| 12 #include "base/trace_event/process_memory_maps.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace trace_event { | |
| 16 | |
| 17 // static | |
| 18 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const uint32 kMaxLineSize = 4096; | |
| 23 | |
| 24 bool ParseSmapsHeader(const char* header_line, | |
| 25 ProcessMemoryMaps::VMRegion* region) { | |
| 26 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" | |
| 27 bool res = true; // Whether this region should be appended or skipped. | |
| 28 uint64 end_addr = 0; | |
| 29 char protection_flags[5] = {0}; | |
| 30 char mapped_file[kMaxLineSize]; | |
| 31 | |
| 32 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", | |
| 33 ®ion->start_address, &end_addr, protection_flags, | |
| 34 mapped_file) != 4) | |
| 35 return false; | |
| 36 | |
| 37 if (end_addr > region->start_address) { | |
| 38 region->size_in_bytes = end_addr - region->start_address; | |
| 39 } else { | |
| 40 // This is not just paranoia, it can actually happen (See crbug.com/461237). | |
| 41 region->size_in_bytes = 0; | |
| 42 res = false; | |
| 43 } | |
| 44 | |
| 45 region->protection_flags = 0; | |
| 46 if (protection_flags[0] == 'r') { | |
| 47 region->protection_flags |= | |
| 48 ProcessMemoryMaps::VMRegion::kProtectionFlagsRead; | |
| 49 } | |
| 50 if (protection_flags[1] == 'w') { | |
| 51 region->protection_flags |= | |
| 52 ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite; | |
| 53 } | |
| 54 if (protection_flags[2] == 'x') { | |
| 55 region->protection_flags |= | |
| 56 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | |
| 57 } | |
| 58 | |
| 59 region->mapped_file = mapped_file; | |
| 60 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, ®ion->mapped_file); | |
| 61 | |
| 62 return res; | |
| 63 } | |
| 64 | |
| 65 uint64 ReadCounterBytes(char* counter_line) { | |
| 66 uint64 counter_value = 0; | |
| 67 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); | |
| 68 DCHECK_EQ(1, res); | |
| 69 return counter_value * 1024; | |
| 70 } | |
| 71 | |
| 72 uint32 ParseSmapsCounter(char* counter_line, | |
| 73 ProcessMemoryMaps::VMRegion* region) { | |
| 74 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" | |
| 75 uint32 res = 1; | |
| 76 char counter_name[20]; | |
| 77 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name); | |
| 78 DCHECK_EQ(1, did_read); | |
| 79 | |
| 80 if (strcmp(counter_name, "Pss:") == 0) { | |
| 81 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); | |
| 82 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { | |
| 83 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); | |
| 84 } else if (strcmp(counter_name, "Private_Clean:") == 0) { | |
| 85 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); | |
| 86 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { | |
| 87 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); | |
| 88 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { | |
| 89 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); | |
| 90 } else if (strcmp(counter_name, "Swap:") == 0) { | |
| 91 region->byte_stats_swapped = ReadCounterBytes(counter_line); | |
| 92 } else { | |
| 93 res = 0; | |
| 94 } | |
| 95 | |
| 96 return res; | |
| 97 } | |
| 98 | |
| 99 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { | |
| 100 if (!smaps_file) | |
| 101 return 0; | |
| 102 | |
| 103 fseek(smaps_file, 0, SEEK_SET); | |
| 104 | |
| 105 char line[kMaxLineSize]; | |
| 106 const uint32 kNumExpectedCountersPerRegion = 6; | |
| 107 uint32 counters_parsed_for_current_region = 0; | |
| 108 uint32 num_valid_regions = 0; | |
| 109 ProcessMemoryMaps::VMRegion region; | |
| 110 bool should_add_current_region = false; | |
| 111 for (;;) { | |
| 112 line[0] = '\0'; | |
| 113 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) | |
| 114 break; | |
| 115 DCHECK_GT(strlen(line), 0u); | |
| 116 if (isxdigit(line[0]) && !isupper(line[0])) { | |
| 117 region = ProcessMemoryMaps::VMRegion(); | |
| 118 counters_parsed_for_current_region = 0; | |
| 119 should_add_current_region = ParseSmapsHeader(line, ®ion); | |
| 120 } else { | |
| 121 counters_parsed_for_current_region += ParseSmapsCounter(line, ®ion); | |
| 122 DCHECK_LE(counters_parsed_for_current_region, | |
| 123 kNumExpectedCountersPerRegion); | |
| 124 if (counters_parsed_for_current_region == kNumExpectedCountersPerRegion) { | |
| 125 if (should_add_current_region) { | |
| 126 pmm->AddVMRegion(region); | |
| 127 ++num_valid_regions; | |
| 128 should_add_current_region = false; | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 return num_valid_regions; | |
| 134 } | |
| 135 | |
| 136 } // namespace | |
| 137 | |
| 138 // static | |
| 139 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { | |
| 140 return Singleton<ProcessMemoryMapsDumpProvider, | |
| 141 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); | |
| 142 } | |
| 143 | |
| 144 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { | |
| 145 } | |
| 146 | |
| 147 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() { | |
| 148 } | |
| 149 | |
| 150 // Called at trace dump point time. Creates a snapshot of the memory maps for | |
| 151 // the current process. | |
| 152 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | |
| 153 ProcessMemoryDump* pmd) { | |
| 154 // Snapshot of memory maps is not taken for light dump requests. | |
| 155 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) | |
| 156 return true; | |
| 157 | |
| 158 uint32 res = 0; | |
| 159 if (UNLIKELY(proc_smaps_for_testing)) { | |
| 160 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | |
| 161 } else { | |
| 162 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); | |
| 163 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | |
| 164 } | |
| 165 | |
| 166 if (res > 0) { | |
| 167 pmd->set_has_process_mmaps(); | |
| 168 return true; | |
| 169 } | |
| 170 return false; | |
| 171 } | |
| 172 | |
| 173 } // namespace trace_event | |
| 174 } // namespace base | |
| OLD | NEW |