Chromium Code Reviews| Index: components/tracing/common/process_metrics_memory_dump_provider.cc |
| diff --git a/components/tracing/common/process_metrics_memory_dump_provider.cc b/components/tracing/common/process_metrics_memory_dump_provider.cc |
| index 9bfe0a43acb0d9aee1a80b62c7a69da2a90ce532..3f816efb97c69f30d6693c661cefc7f2ffa1119a 100644 |
| --- a/components/tracing/common/process_metrics_memory_dump_provider.cc |
| +++ b/components/tracing/common/process_metrics_memory_dump_provider.cc |
| @@ -38,6 +38,14 @@ |
| #include "base/numerics/safe_math.h" |
| #endif // defined(OS_MACOSX) |
| +#if defined(OS_WIN) |
| +#include <psapi.h> |
| +#include <tchar.h> |
| +#include <windows.h> |
| + |
| +#include <base/strings/sys_string_conversions.h> |
| +#endif |
| + |
| namespace tracing { |
| namespace { |
| @@ -233,6 +241,56 @@ bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( |
| } |
| #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| +#if defined(OS_WIN) |
| +bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps( |
| + const base::trace_event::MemoryDumpArgs& args, |
| + base::trace_event::ProcessMemoryDump* pmd) { |
| + // Fetch handles to each of the modules loaded in this process. The process |
| + // itself retains ownership of the returned handles. |
| + DWORD requested_module_count = 1024; |
| + DWORD result_module_count = 0; |
| + std::vector<HMODULE> modules; |
| + while (true) { |
| + modules.resize(requested_module_count); |
| + DWORD output_size = 0; |
| + BOOL result = ::EnumProcessModules(::GetCurrentProcess(), modules.data(), |
| + requested_module_count * sizeof(HMODULE), |
| + &output_size); |
| + if (!result) |
| + return false; |
| + result_module_count = output_size / sizeof(HMODULE); |
|
etienneb
2017/02/23 21:11:36
The way we were used to handle these racy windows
erikchen
2017/02/23 21:33:33
Sorry, I don't understand what you mean. AFAICT, t
etienneb
2017/02/24 16:37:15
I'm fine with the switch. thx.
|
| + |
| + if (result_module_count <= requested_module_count) |
| + break; |
| + |
| + // Try again. Add a small buffer, since ::EnumProcessModules can be racy. |
| + requested_module_count = result_module_count + 5; |
| + } |
| + |
| + // Query the base address for each module, and attach it to the dump. |
| + for (DWORD i = 0; i < result_module_count; ++i) { |
| + wchar_t module_name[MAX_PATH]; |
| + BOOL result = ::GetModuleFileName(modules[i], module_name, MAX_PATH); |
| + if (!result) |
| + continue; |
| + |
| + MODULEINFO module_info; |
| + result = ::GetModuleInformation(::GetCurrentProcess(), modules[i], |
| + &module_info, sizeof(MODULEINFO)); |
| + if (!result) |
| + continue; |
| + base::trace_event::ProcessMemoryMaps::VMRegion region; |
| + region.size_in_bytes = module_info.SizeOfImage; |
| + region.mapped_file = base::SysWideToNativeMB(module_name); |
| + region.start_address = reinterpret_cast<uint64_t>(module_info.lpBaseOfDll); |
| + pmd->process_mmaps()->AddVMRegion(region); |
| + } |
| + if (!pmd->process_mmaps()->vm_regions().empty()) |
| + pmd->set_has_process_mmaps(); |
| + return true; |
| +} |
| +#endif // defined(OS_WIN) |
| + |
| #if defined(OS_MACOSX) |
| namespace { |
| @@ -547,11 +605,9 @@ bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( |
| base::trace_event::ProcessMemoryDump* pmd) { |
| bool res = DumpProcessTotals(args, pmd); |
| -#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) |
| if (args.level_of_detail == |
| base::trace_event::MemoryDumpLevelOfDetail::DETAILED) |
| res &= DumpProcessMemoryMaps(args, pmd); |
| -#endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX) |
| return res; |
| } |