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