blob: bf8f199d8ab44006fda963c068d59d1fac22a59a [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 Bergeron436d42212019-02-26 17:15:1252 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
53 base::BlockingType::MAY_BLOCK);
[email protected]54fd1d32009-09-01 00:12:5854
55 // Clear old data.
ellyjonescd6e449d2016-04-13 19:31:1556 process_data_[0].processes.clear();
[email protected]54fd1d32009-09-01 00:12:5857
[email protected]b90d7e802011-01-09 16:32:2058 base::win::ScopedHandle snapshot(
59 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
[email protected]54fd1d32009-09-01 00:12:5860 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
61 if (!snapshot.Get()) {
jsbell3785ea02016-07-29 16:50:0362 LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError();
[email protected]54fd1d32009-09-01 00:12:5863 return;
64 }
rvargasd7743ba2014-09-25 01:35:2865 if (!::Process32First(snapshot.Get(), &process_entry)) {
[email protected]54fd1d32009-09-01 00:12:5866 LOG(ERROR) << "Process32First failed: " << GetLastError();
67 return;
68 }
69 do {
[email protected]a4dc33f2009-10-20 15:09:5570 base::ProcessId pid = process_entry.th32ProcessID;
[email protected]1e67c2b2011-03-04 01:17:3771 base::win::ScopedHandle process_handle(::OpenProcess(
[email protected]54fd1d32009-09-01 00:12:5872 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
rvargasd7743ba2014-09-25 01:35:2873 if (!process_handle.IsValid())
[email protected]54fd1d32009-09-01 00:12:5874 continue;
ellyjonescd6e449d2016-04-13 19:31:1575 if (_wcsicmp(process_data_[0].process_name.c_str(),
thestig0df2bae82016-07-26 17:59:3676 process_entry.szExeFile) != 0) {
ellyjonescd6e449d2016-04-13 19:31:1577 continue;
thestig0df2bae82016-07-26 17:59:3678 }
79
ellyjonescd6e449d2016-04-13 19:31:1580 // Get Memory Information.
81 ProcessMemoryInformation info;
82 info.pid = pid;
thestig0df2bae82016-07-26 17:59:3683 info.process_type = pid == GetCurrentProcessId()
84 ? content::PROCESS_TYPE_BROWSER
85 : content::PROCESS_TYPE_UNKNOWN;
ellyjonescd6e449d2016-04-13 19:31:1586
ellyjonescd6e449d2016-04-13 19:31:1587 // Get Version Information.
88 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber());
89 // Check if this is one of the child processes whose data we collected
90 // on the IO thread, and if so copy over that data.
thestig0df2bae82016-07-26 17:59:3691 for (const ProcessMemoryInformation& child : child_info) {
92 if (child.pid == info.pid) {
93 info.titles = child.titles;
94 info.process_type = child.process_type;
95 break;
96 }
[email protected]54fd1d32009-09-01 00:12:5897 }
ellyjonescd6e449d2016-04-13 19:31:1598
99 // Add the process info to our list.
100 process_data_[0].processes.push_back(info);
rvargasd7743ba2014-09-25 01:35:28101 } while (::Process32Next(snapshot.Get(), &process_entry));
[email protected]54fd1d32009-09-01 00:12:58102
103 // Finally return to the browser thread.
Eric Seckler8652dcd52018-09-20 10:42:28104 base::PostTaskWithTraits(
105 FROM_HERE, {BrowserThread::UI},
kylechar99ef9042019-02-25 18:09:43106 base::BindOnce(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]54fd1d32009-09-01 00:12:58107}