Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: components/tracing/common/process_metrics_memory_dump_provider.cc

Issue 2710163002: Implement basic support for Windows module emission. (Closed)
Patch Set: Comments from etienne. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/tracing/common/process_metrics_memory_dump_provider.h" 5 #include "components/tracing/common/process_metrics_memory_dump_provider.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 20 matching lines...) Expand all
31 #include <mach/shared_region.h> 31 #include <mach/shared_region.h>
32 #include <sys/param.h> 32 #include <sys/param.h>
33 33
34 #include <mach-o/dyld_images.h> 34 #include <mach-o/dyld_images.h>
35 #include <mach-o/loader.h> 35 #include <mach-o/loader.h>
36 #include <mach/mach.h> 36 #include <mach/mach.h>
37 37
38 #include "base/numerics/safe_math.h" 38 #include "base/numerics/safe_math.h"
39 #endif // defined(OS_MACOSX) 39 #endif // defined(OS_MACOSX)
40 40
41 #if defined(OS_WIN)
42 #include <psapi.h>
43 #include <tchar.h>
44 #include <windows.h>
45
46 #include <base/strings/sys_string_conversions.h>
47 #include <base/win/win_util.h>
48 #endif
Wez 2017/02/23 23:35:03 nit: // defined(OS_WIN) here, since the block is g
erikchen 2017/02/24 17:32:48 Done.
49
41 namespace tracing { 50 namespace tracing {
42 51
43 namespace { 52 namespace {
44 53
45 base::LazyInstance< 54 base::LazyInstance<
46 std::map<base::ProcessId, 55 std::map<base::ProcessId,
47 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky 56 std::unique_ptr<ProcessMetricsMemoryDumpProvider>>>::Leaky
48 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER; 57 g_dump_providers_map = LAZY_INSTANCE_INITIALIZER;
49 58
50 #if defined(OS_LINUX) || defined(OS_ANDROID) 59 #if defined(OS_LINUX) || defined(OS_ANDROID)
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r")); 235 base::ScopedFILE smaps_file(fopen(file_name.c_str(), "r"));
227 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps()); 236 res = ReadLinuxProcSmapsFile(smaps_file.get(), pmd->process_mmaps());
228 } 237 }
229 238
230 if (res) 239 if (res)
231 pmd->set_has_process_mmaps(); 240 pmd->set_has_process_mmaps();
232 return res; 241 return res;
233 } 242 }
234 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 243 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
235 244
245 #if defined(OS_WIN)
246 bool ProcessMetricsMemoryDumpProvider::DumpProcessMemoryMaps(
247 const base::trace_event::MemoryDumpArgs& args,
248 base::trace_event::ProcessMemoryDump* pmd) {
249 std::vector<HMODULE> modules;
250 bool result =
251 base::win::GetLoadedModulesSnapshot(::GetCurrentProcess(), &modules);
252 if (!result)
Wez 2017/02/23 23:35:03 nit: Do you need |result|, or can you just if (!Ge
erikchen 2017/02/24 17:32:48 Done.
253 return false;
254
255 // Query the base address for each module, and attach it to the dump.
256 for (size_t i = 0; i < modules.size(); ++i) {
257 wchar_t module_name[MAX_PATH];
258 BOOL result = ::GetModuleFileName(modules[i], module_name, MAX_PATH);
etienneb 2017/02/24 16:37:15 nit: I agree with wez@ on the previous message. Yo
etienneb 2017/02/24 17:06:43 I remember a corner case with the function GetModu
erikchen 2017/02/24 17:32:48 Noted. It looks like we mostly ignore this problem
259 if (!result)
260 continue;
261
262 MODULEINFO module_info;
263 result = ::GetModuleInformation(::GetCurrentProcess(), modules[i],
264 &module_info, sizeof(MODULEINFO));
265 if (!result)
266 continue;
267 base::trace_event::ProcessMemoryMaps::VMRegion region;
268 region.size_in_bytes = module_info.SizeOfImage;
269 region.mapped_file = base::SysWideToNativeMB(module_name);
270 region.start_address = reinterpret_cast<uint64_t>(module_info.lpBaseOfDll);
271 pmd->process_mmaps()->AddVMRegion(region);
272 }
273 if (!pmd->process_mmaps()->vm_regions().empty())
274 pmd->set_has_process_mmaps();
275 return true;
276 }
277 #endif // defined(OS_WIN)
278
236 #if defined(OS_MACOSX) 279 #if defined(OS_MACOSX)
237 280
238 namespace { 281 namespace {
239 282
240 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion; 283 using VMRegion = base::trace_event::ProcessMemoryMaps::VMRegion;
241 284
242 bool IsAddressInSharedRegion(uint64_t address) { 285 bool IsAddressInSharedRegion(uint64_t address) {
243 return address >= SHARED_REGION_BASE_X86_64 && 286 return address >= SHARED_REGION_BASE_X86_64 &&
244 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); 287 address < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
245 } 288 }
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 583
541 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {} 584 ProcessMetricsMemoryDumpProvider::~ProcessMetricsMemoryDumpProvider() {}
542 585
543 // Called at trace dump point time. Creates a snapshot of the memory maps for 586 // Called at trace dump point time. Creates a snapshot of the memory maps for
544 // the current process. 587 // the current process.
545 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump( 588 bool ProcessMetricsMemoryDumpProvider::OnMemoryDump(
546 const base::trace_event::MemoryDumpArgs& args, 589 const base::trace_event::MemoryDumpArgs& args,
547 base::trace_event::ProcessMemoryDump* pmd) { 590 base::trace_event::ProcessMemoryDump* pmd) {
548 bool res = DumpProcessTotals(args, pmd); 591 bool res = DumpProcessTotals(args, pmd);
549 592
550 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
551 if (args.level_of_detail == 593 if (args.level_of_detail ==
552 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) 594 base::trace_event::MemoryDumpLevelOfDetail::DETAILED)
553 res &= DumpProcessMemoryMaps(args, pmd); 595 res &= DumpProcessMemoryMaps(args, pmd);
554 #endif // defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
555 return res; 596 return res;
556 } 597 }
557 598
558 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( 599 bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals(
559 const base::trace_event::MemoryDumpArgs& args, 600 const base::trace_event::MemoryDumpArgs& args,
560 base::trace_event::ProcessMemoryDump* pmd) { 601 base::trace_event::ProcessMemoryDump* pmd) {
561 const uint64_t rss_bytes = rss_bytes_for_testing 602 const uint64_t rss_bytes = rss_bytes_for_testing
562 ? rss_bytes_for_testing 603 ? rss_bytes_for_testing
563 : process_metrics_->GetWorkingSetSize(); 604 : process_metrics_->GetWorkingSetSize();
564 605
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 #endif 688 #endif
648 } 689 }
649 690
650 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { 691 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() {
651 #if defined(OS_LINUX) || defined(OS_ANDROID) 692 #if defined(OS_LINUX) || defined(OS_ANDROID)
652 fast_polling_statm_fd_.reset(); 693 fast_polling_statm_fd_.reset();
653 #endif 694 #endif
654 } 695 }
655 696
656 } // namespace tracing 697 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698