blob: 8c16c2b56b5c1f0c1d2c037b7f600f4ca584af51 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[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"
Lei Zhang46a1cad2022-01-17 10:40:3119#include "base/logging.h"
asvitkine58409e4c2015-01-15 01:25:4520#include "base/path_service.h"
[email protected]f9b294362013-06-10 20:22:3121#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1822#include "base/strings/utf_string_conversions.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
asvitkine89406d1f2015-01-17 06:57:1034MemoryDetails::MemoryDetails() {
asvitkine58409e4c2015-01-15 01:25:4535 base::FilePath browser_process_path;
Avi Drissman9098f9002018-05-04 00:11:5236 base::PathService::Get(base::FILE_EXE, &browser_process_path);
asvitkine58409e4c2015-01-15 01:25:4537
ellyjonescd6e449d2016-04-13 19:31:1538 ProcessData process;
39 process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
Peter Kasting5105d612021-02-09 22:41:4240 process.process_name = browser_process_path.BaseName().AsUTF16Unsafe();
ellyjonescd6e449d2016-04-13 19:31:1541 process_data_.push_back(process);
[email protected]54fd1d32009-09-01 00:12:5842}
43
44ProcessData* MemoryDetails::ChromeBrowser() {
ellyjonescd6e449d2016-04-13 19:31:1545 return &process_data_[0];
[email protected]54fd1d32009-09-01 00:12:5846}
47
48void MemoryDetails::CollectProcessData(
[email protected]4df3ac62011-03-11 04:38:5249 const std::vector<ProcessMemoryInformation>& child_info) {
Etienne Bergeron436d42212019-02-26 17:15:1250 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
51 base::BlockingType::MAY_BLOCK);
[email protected]54fd1d32009-09-01 00:12:5852
53 // Clear old data.
ellyjonescd6e449d2016-04-13 19:31:1554 process_data_[0].processes.clear();
[email protected]54fd1d32009-09-01 00:12:5855
[email protected]b90d7e802011-01-09 16:32:2056 base::win::ScopedHandle snapshot(
57 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
[email protected]54fd1d32009-09-01 00:12:5858 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
59 if (!snapshot.Get()) {
jsbell3785ea02016-07-29 16:50:0360 LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError();
[email protected]54fd1d32009-09-01 00:12:5861 return;
62 }
rvargasd7743ba2014-09-25 01:35:2863 if (!::Process32First(snapshot.Get(), &process_entry)) {
[email protected]54fd1d32009-09-01 00:12:5864 LOG(ERROR) << "Process32First failed: " << GetLastError();
65 return;
66 }
67 do {
[email protected]a4dc33f2009-10-20 15:09:5568 base::ProcessId pid = process_entry.th32ProcessID;
[email protected]1e67c2b2011-03-04 01:17:3769 base::win::ScopedHandle process_handle(::OpenProcess(
[email protected]54fd1d32009-09-01 00:12:5870 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
rvargasd7743ba2014-09-25 01:35:2871 if (!process_handle.IsValid())
[email protected]54fd1d32009-09-01 00:12:5872 continue;
Peter Kasting5105d612021-02-09 22:41:4273 if (_wcsicmp(base::as_wcstr(process_data_[0].process_name),
thestig0df2bae82016-07-26 17:59:3674 process_entry.szExeFile) != 0) {
ellyjonescd6e449d2016-04-13 19:31:1575 continue;
thestig0df2bae82016-07-26 17:59:3676 }
77
ellyjonescd6e449d2016-04-13 19:31:1578 // Get Memory Information.
79 ProcessMemoryInformation info;
80 info.pid = pid;
thestig0df2bae82016-07-26 17:59:3681 info.process_type = pid == GetCurrentProcessId()
82 ? content::PROCESS_TYPE_BROWSER
83 : content::PROCESS_TYPE_UNKNOWN;
ellyjonescd6e449d2016-04-13 19:31:1584
ellyjonescd6e449d2016-04-13 19:31:1585 // Get Version Information.
86 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber());
87 // Check if this is one of the child processes whose data we collected
88 // on the IO thread, and if so copy over that data.
thestig0df2bae82016-07-26 17:59:3689 for (const ProcessMemoryInformation& child : child_info) {
90 if (child.pid == info.pid) {
91 info.titles = child.titles;
92 info.process_type = child.process_type;
93 break;
94 }
[email protected]54fd1d32009-09-01 00:12:5895 }
ellyjonescd6e449d2016-04-13 19:31:1596
97 // Add the process info to our list.
98 process_data_[0].processes.push_back(info);
rvargasd7743ba2014-09-25 01:35:2899 } while (::Process32Next(snapshot.Get(), &process_entry));
[email protected]54fd1d32009-09-01 00:12:58100
101 // Finally return to the browser thread.
Gabriel Charettee7cdc5cd2020-05-27 23:35:05102 content::GetUIThreadTaskRunner({})->PostTask(
103 FROM_HERE,
kylechar99ef9042019-02-25 18:09:43104 base::BindOnce(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]54fd1d32009-09-01 00:12:58105}