| 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 <stdint.h> |
| 8 |
| 7 #include "base/files/scoped_file.h" | 9 #include "base/files/scoped_file.h" |
| 8 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/trace_event/process_memory_dump.h" | 13 #include "base/trace_event/process_memory_dump.h" |
| 12 #include "base/trace_event/process_memory_maps.h" | 14 #include "base/trace_event/process_memory_maps.h" |
| 13 | 15 |
| 14 namespace base { | 16 namespace base { |
| 15 namespace trace_event { | 17 namespace trace_event { |
| 16 | 18 |
| 17 // static | 19 // static |
| 18 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | 20 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const uint32 kMaxLineSize = 4096; | 24 const uint32_t kMaxLineSize = 4096; |
| 23 | 25 |
| 24 bool ParseSmapsHeader(const char* header_line, | 26 bool ParseSmapsHeader(const char* header_line, |
| 25 ProcessMemoryMaps::VMRegion* region) { | 27 ProcessMemoryMaps::VMRegion* region) { |
| 26 // 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" |
| 27 bool res = true; // Whether this region should be appended or skipped. | 29 bool res = true; // Whether this region should be appended or skipped. |
| 28 uint64 end_addr = 0; | 30 uint64_t end_addr = 0; |
| 29 char protection_flags[5] = {0}; | 31 char protection_flags[5] = {0}; |
| 30 char mapped_file[kMaxLineSize]; | 32 char mapped_file[kMaxLineSize]; |
| 31 | 33 |
| 32 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", | 34 if (sscanf(header_line, "%" SCNx64 "-%" SCNx64 " %4c %*s %*s %*s%4095[^\n]\n", |
| 33 ®ion->start_address, &end_addr, protection_flags, | 35 ®ion->start_address, &end_addr, protection_flags, |
| 34 mapped_file) != 4) | 36 mapped_file) != 4) |
| 35 return false; | 37 return false; |
| 36 | 38 |
| 37 if (end_addr > region->start_address) { | 39 if (end_addr > region->start_address) { |
| 38 region->size_in_bytes = end_addr - region->start_address; | 40 region->size_in_bytes = end_addr - region->start_address; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 region->protection_flags |= | 57 region->protection_flags |= |
| 56 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; | 58 ProcessMemoryMaps::VMRegion::kProtectionFlagsExec; |
| 57 } | 59 } |
| 58 | 60 |
| 59 region->mapped_file = mapped_file; | 61 region->mapped_file = mapped_file; |
| 60 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, ®ion->mapped_file); | 62 TrimWhitespaceASCII(region->mapped_file, TRIM_ALL, ®ion->mapped_file); |
| 61 | 63 |
| 62 return res; | 64 return res; |
| 63 } | 65 } |
| 64 | 66 |
| 65 uint64 ReadCounterBytes(char* counter_line) { | 67 uint64_t ReadCounterBytes(char* counter_line) { |
| 66 uint64 counter_value = 0; | 68 uint64_t counter_value = 0; |
| 67 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); | 69 int res = sscanf(counter_line, "%*s %" SCNu64 " kB", &counter_value); |
| 68 DCHECK_EQ(1, res); | 70 DCHECK_EQ(1, res); |
| 69 return counter_value * 1024; | 71 return counter_value * 1024; |
| 70 } | 72 } |
| 71 | 73 |
| 72 uint32 ParseSmapsCounter(char* counter_line, | 74 uint32_t ParseSmapsCounter(char* counter_line, |
| 73 ProcessMemoryMaps::VMRegion* region) { | 75 ProcessMemoryMaps::VMRegion* region) { |
| 74 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" | 76 // A smaps counter lines looks as follows: "RSS: 0 Kb\n" |
| 75 uint32 res = 1; | 77 uint32_t res = 1; |
| 76 char counter_name[20]; | 78 char counter_name[20]; |
| 77 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name); | 79 int did_read = sscanf(counter_line, "%19[^\n ]", counter_name); |
| 78 DCHECK_EQ(1, did_read); | 80 DCHECK_EQ(1, did_read); |
| 79 | 81 |
| 80 if (strcmp(counter_name, "Pss:") == 0) { | 82 if (strcmp(counter_name, "Pss:") == 0) { |
| 81 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); | 83 region->byte_stats_proportional_resident = ReadCounterBytes(counter_line); |
| 82 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { | 84 } else if (strcmp(counter_name, "Private_Dirty:") == 0) { |
| 83 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); | 85 region->byte_stats_private_dirty_resident = ReadCounterBytes(counter_line); |
| 84 } else if (strcmp(counter_name, "Private_Clean:") == 0) { | 86 } else if (strcmp(counter_name, "Private_Clean:") == 0) { |
| 85 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); | 87 region->byte_stats_private_clean_resident = ReadCounterBytes(counter_line); |
| 86 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { | 88 } else if (strcmp(counter_name, "Shared_Dirty:") == 0) { |
| 87 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); | 89 region->byte_stats_shared_dirty_resident = ReadCounterBytes(counter_line); |
| 88 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { | 90 } else if (strcmp(counter_name, "Shared_Clean:") == 0) { |
| 89 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); | 91 region->byte_stats_shared_clean_resident = ReadCounterBytes(counter_line); |
| 90 } else if (strcmp(counter_name, "Swap:") == 0) { | 92 } else if (strcmp(counter_name, "Swap:") == 0) { |
| 91 region->byte_stats_swapped = ReadCounterBytes(counter_line); | 93 region->byte_stats_swapped = ReadCounterBytes(counter_line); |
| 92 } else { | 94 } else { |
| 93 res = 0; | 95 res = 0; |
| 94 } | 96 } |
| 95 | 97 |
| 96 return res; | 98 return res; |
| 97 } | 99 } |
| 98 | 100 |
| 99 uint32 ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { | 101 uint32_t ReadLinuxProcSmapsFile(FILE* smaps_file, ProcessMemoryMaps* pmm) { |
| 100 if (!smaps_file) | 102 if (!smaps_file) |
| 101 return 0; | 103 return 0; |
| 102 | 104 |
| 103 fseek(smaps_file, 0, SEEK_SET); | 105 fseek(smaps_file, 0, SEEK_SET); |
| 104 | 106 |
| 105 char line[kMaxLineSize]; | 107 char line[kMaxLineSize]; |
| 106 const uint32 kNumExpectedCountersPerRegion = 6; | 108 const uint32_t kNumExpectedCountersPerRegion = 6; |
| 107 uint32 counters_parsed_for_current_region = 0; | 109 uint32_t counters_parsed_for_current_region = 0; |
| 108 uint32 num_valid_regions = 0; | 110 uint32_t num_valid_regions = 0; |
| 109 ProcessMemoryMaps::VMRegion region; | 111 ProcessMemoryMaps::VMRegion region; |
| 110 bool should_add_current_region = false; | 112 bool should_add_current_region = false; |
| 111 for (;;) { | 113 for (;;) { |
| 112 line[0] = '\0'; | 114 line[0] = '\0'; |
| 113 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) | 115 if (fgets(line, kMaxLineSize, smaps_file) == nullptr) |
| 114 break; | 116 break; |
| 115 DCHECK_GT(strlen(line), 0u); | 117 DCHECK_GT(strlen(line), 0u); |
| 116 if (isxdigit(line[0]) && !isupper(line[0])) { | 118 if (isxdigit(line[0]) && !isupper(line[0])) { |
| 117 region = ProcessMemoryMaps::VMRegion(); | 119 region = ProcessMemoryMaps::VMRegion(); |
| 118 counters_parsed_for_current_region = 0; | 120 counters_parsed_for_current_region = 0; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 148 } | 150 } |
| 149 | 151 |
| 150 // Called at trace dump point time. Creates a snapshot of the memory maps for | 152 // Called at trace dump point time. Creates a snapshot of the memory maps for |
| 151 // the current process. | 153 // the current process. |
| 152 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 154 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 153 ProcessMemoryDump* pmd) { | 155 ProcessMemoryDump* pmd) { |
| 154 // Snapshot of memory maps is not taken for light dump requests. | 156 // Snapshot of memory maps is not taken for light dump requests. |
| 155 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) | 157 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) |
| 156 return true; | 158 return true; |
| 157 | 159 |
| 158 uint32 res = 0; | 160 uint32_t res = 0; |
| 159 if (UNLIKELY(proc_smaps_for_testing)) { | 161 if (UNLIKELY(proc_smaps_for_testing)) { |
| 160 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 162 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); |
| 161 } else { | 163 } else { |
| 162 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); | 164 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); |
| 163 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 165 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 164 } | 166 } |
| 165 | 167 |
| 166 if (res > 0) { | 168 if (res > 0) { |
| 167 pmd->set_has_process_mmaps(); | 169 pmd->set_has_process_mmaps(); |
| 168 return true; | 170 return true; |
| 169 } | 171 } |
| 170 return false; | 172 return false; |
| 171 } | 173 } |
| 172 | 174 |
| 173 } // namespace trace_event | 175 } // namespace trace_event |
| 174 } // namespace base | 176 } // namespace base |
| OLD | NEW |