blob: 9ba43e50e126d9a26a3d2d0a150338b3b8180be9 [file] [log] [blame]
[email protected]4306df72012-04-20 18:58:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]54fd1d32009-09-01 00:12:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Erik Chen7970a422018-08-10 18:02:125// Windows headers must come first.
6#include <windows.h>
[email protected]b90d7e802011-01-09 16:32:207
[email protected]54fd1d32009-09-01 00:12:588#include <psapi.h>
avi6846aef2015-12-26 01:09:389#include <stddef.h>
[email protected]d09a4ce1c2013-07-24 17:37:0210#include <TlHelp32.h>
[email protected]54fd1d32009-09-01 00:12:5811
Erik Chen7970a422018-08-10 18:02:1212#include "chrome/browser/memory_details.h"
13
dcheng4af48582016-04-19 00:29:3514#include <memory>
15
[email protected]24d69692011-10-21 18:26:5116#include "base/bind.h"
[email protected]54fd1d32009-09-01 00:12:5817#include "base/file_version_info.h"
[email protected]57999812013-02-24 05:40:5218#include "base/files/file_path.h"
asvitkine58409e4c2015-01-15 01:25:4519#include "base/path_service.h"
[email protected]f9b294362013-06-10 20:22:3120#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1821#include "base/strings/utf_string_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2822#include "base/task/post_task.h"
Etienne Pierre-doray0fbce432018-08-27 20:27:5123#include "base/threading/scoped_blocking_call.h"
[email protected]b90d7e802011-01-09 16:32:2024#include "base/win/scoped_handle.h"
[email protected]1e67c2b2011-03-04 01:17:3725#include "base/win/windows_version.h"
[email protected]54fd1d32009-09-01 00:12:5826#include "chrome/common/url_constants.h"
[email protected]af39f002014-08-22 10:18:1827#include "chrome/grit/chromium_strings.h"
sdefresne9fb67692015-08-03 18:48:2228#include "components/version_info/version_info.h"
Eric Seckler8652dcd52018-09-20 10:42:2829#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4930#include "content/public/browser/browser_thread.h"
[email protected]bd5d6cf2011-12-01 00:39:1231#include "content/public/common/process_type.h"
[email protected]c051a1b2011-01-21 23:30:1732#include "ui/base/l10n/l10n_util.h"
[email protected]54fd1d32009-09-01 00:12:5833
[email protected]631bb742011-11-02 11:29:3934using content::BrowserThread;
35
asvitkine89406d1f2015-01-17 06:57:1036MemoryDetails::MemoryDetails() {
asvitkine58409e4c2015-01-15 01:25:4537 base::FilePath browser_process_path;
Avi Drissman9098f9002018-05-04 00:11:5238 base::PathService::Get(base::FILE_EXE, &browser_process_path);
asvitkine58409e4c2015-01-15 01:25:4539
ellyjonescd6e449d2016-04-13 19:31:1540 ProcessData process;
41 process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
42 process.process_name = browser_process_path.BaseName().value();
43 process_data_.push_back(process);
[email protected]54fd1d32009-09-01 00:12:5844}
45
46ProcessData* MemoryDetails::ChromeBrowser() {
ellyjonescd6e449d2016-04-13 19:31:1547 return &process_data_[0];
[email protected]54fd1d32009-09-01 00:12:5848}
49
50void MemoryDetails::CollectProcessData(
[email protected]4df3ac62011-03-11 04:38:5251 const std::vector<ProcessMemoryInformation>& child_info) {
Etienne Pierre-doray0fbce432018-08-27 20:27:5152 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
[email protected]54fd1d32009-09-01 00:12:5853
54 // Clear old data.
ellyjonescd6e449d2016-04-13 19:31:1555 process_data_[0].processes.clear();
[email protected]54fd1d32009-09-01 00:12:5856
[email protected]b90d7e802011-01-09 16:32:2057 base::win::ScopedHandle snapshot(
58 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
[email protected]54fd1d32009-09-01 00:12:5859 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
60 if (!snapshot.Get()) {
jsbell3785ea02016-07-29 16:50:0361 LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError();
[email protected]54fd1d32009-09-01 00:12:5862 return;
63 }
rvargasd7743ba2014-09-25 01:35:2864 if (!::Process32First(snapshot.Get(), &process_entry)) {
[email protected]54fd1d32009-09-01 00:12:5865 LOG(ERROR) << "Process32First failed: " << GetLastError();
66 return;
67 }
68 do {
[email protected]a4dc33f2009-10-20 15:09:5569 base::ProcessId pid = process_entry.th32ProcessID;
[email protected]1e67c2b2011-03-04 01:17:3770 base::win::ScopedHandle process_handle(::OpenProcess(
[email protected]54fd1d32009-09-01 00:12:5871 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
rvargasd7743ba2014-09-25 01:35:2872 if (!process_handle.IsValid())
[email protected]54fd1d32009-09-01 00:12:5873 continue;
ellyjonescd6e449d2016-04-13 19:31:1574 if (_wcsicmp(process_data_[0].process_name.c_str(),
thestig0df2bae82016-07-26 17:59:3675 process_entry.szExeFile) != 0) {
ellyjonescd6e449d2016-04-13 19:31:1576 continue;
thestig0df2bae82016-07-26 17:59:3677 }
78
ellyjonescd6e449d2016-04-13 19:31:1579 // Get Memory Information.
80 ProcessMemoryInformation info;
81 info.pid = pid;
thestig0df2bae82016-07-26 17:59:3682 info.process_type = pid == GetCurrentProcessId()
83 ? content::PROCESS_TYPE_BROWSER
84 : content::PROCESS_TYPE_UNKNOWN;
ellyjonescd6e449d2016-04-13 19:31:1585
ellyjonescd6e449d2016-04-13 19:31:1586 // Get Version Information.
87 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber());
88 // Check if this is one of the child processes whose data we collected
89 // on the IO thread, and if so copy over that data.
thestig0df2bae82016-07-26 17:59:3690 for (const ProcessMemoryInformation& child : child_info) {
91 if (child.pid == info.pid) {
92 info.titles = child.titles;
93 info.process_type = child.process_type;
94 break;
95 }
[email protected]54fd1d32009-09-01 00:12:5896 }
ellyjonescd6e449d2016-04-13 19:31:1597
98 // Add the process info to our list.
99 process_data_[0].processes.push_back(info);
rvargasd7743ba2014-09-25 01:35:28100 } while (::Process32Next(snapshot.Get(), &process_entry));
[email protected]54fd1d32009-09-01 00:12:58101
102 // Finally return to the browser thread.
Eric Seckler8652dcd52018-09-20 10:42:28103 base::PostTaskWithTraits(
104 FROM_HERE, {BrowserThread::UI},
[email protected]24d69692011-10-21 18:26:51105 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]54fd1d32009-09-01 00:12:58106}