| 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 "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/trace_event/process_memory_dump.h" | 10 #include "base/trace_event/process_memory_dump.h" |
| 11 #include "base/trace_event/process_memory_maps.h" | 11 #include "base/trace_event/process_memory_maps.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace trace_event { | 14 namespace trace_event { |
| 15 | 15 |
| 16 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 17 // static | 16 // static |
| 18 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; | 17 FILE* ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = nullptr; |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 const uint32 kMaxLineSize = 4096; | 21 const uint32 kMaxLineSize = 4096; |
| 23 | 22 |
| 24 bool ParseSmapsHeader(const char* header_line, | 23 bool ParseSmapsHeader(const char* header_line, |
| 25 ProcessMemoryMaps::VMRegion* region) { | 24 ProcessMemoryMaps::VMRegion* region) { |
| 26 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" | 25 // e.g., "00400000-00421000 r-xp 00000000 fc:01 1234 /foo.so\n" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 ++num_valid_regions; | 126 ++num_valid_regions; |
| 128 should_add_current_region = false; | 127 should_add_current_region = false; |
| 129 } | 128 } |
| 130 } | 129 } |
| 131 } | 130 } |
| 132 } | 131 } |
| 133 return num_valid_regions; | 132 return num_valid_regions; |
| 134 } | 133 } |
| 135 | 134 |
| 136 } // namespace | 135 } // namespace |
| 137 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
| 138 | 136 |
| 139 // static | 137 // static |
| 140 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { | 138 ProcessMemoryMapsDumpProvider* ProcessMemoryMapsDumpProvider::GetInstance() { |
| 141 return Singleton<ProcessMemoryMapsDumpProvider, | 139 return Singleton<ProcessMemoryMapsDumpProvider, |
| 142 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); | 140 LeakySingletonTraits<ProcessMemoryMapsDumpProvider>>::get(); |
| 143 } | 141 } |
| 144 | 142 |
| 145 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { | 143 ProcessMemoryMapsDumpProvider::ProcessMemoryMapsDumpProvider() { |
| 146 } | 144 } |
| 147 | 145 |
| 148 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() { | 146 ProcessMemoryMapsDumpProvider::~ProcessMemoryMapsDumpProvider() { |
| 149 } | 147 } |
| 150 | 148 |
| 151 // Called at trace dump point time. Creates a snapshot of the memory maps for | 149 // Called at trace dump point time. Creates a snapshot of the memory maps for |
| 152 // the current process. | 150 // the current process. |
| 153 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 151 bool ProcessMemoryMapsDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 154 ProcessMemoryDump* pmd) { | 152 ProcessMemoryDump* pmd) { |
| 155 // Snapshot of memory maps is not taken for light dump requests. | 153 // Snapshot of memory maps is not taken for light dump requests. |
| 156 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) | 154 if (args.level_of_detail == MemoryDumpLevelOfDetail::LIGHT) |
| 157 return true; | 155 return true; |
| 158 | 156 |
| 159 uint32 res = 0; | 157 uint32 res = 0; |
| 160 | |
| 161 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 162 if (UNLIKELY(proc_smaps_for_testing)) { | 158 if (UNLIKELY(proc_smaps_for_testing)) { |
| 163 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); | 159 res = ReadLinuxProcSmapsFile(proc_smaps_for_testing, pmd->process_mmaps()); |
| 164 } else { | 160 } else { |
| 165 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); | 161 ScopedFILE smaps_file(fopen("/proc/self/smaps", "r")); |
| 166 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); | 162 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); |
| 167 } | 163 } |
| 168 #else | |
| 169 LOG(ERROR) << "ProcessMemoryMaps dump provider is supported only on Linux"; | |
| 170 #endif | |
| 171 | 164 |
| 172 if (res > 0) { | 165 if (res > 0) { |
| 173 pmd->set_has_process_mmaps(); | 166 pmd->set_has_process_mmaps(); |
| 174 return true; | 167 return true; |
| 175 } | 168 } |
| 176 | |
| 177 return false; | 169 return false; |
| 178 } | 170 } |
| 179 | 171 |
| 180 } // namespace trace_event | 172 } // namespace trace_event |
| 181 } // namespace base | 173 } // namespace base |
| OLD | NEW |