Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / debug / gdi_debug_util_win.cc
blob2db10d1782290e7877e45291d6ffa80c87bb7c90
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "base/debug/gdi_debug_util_win.h"
6 #include <cmath>
8 #include <psapi.h>
9 #include <TlHelp32.h>
11 #include "base/debug/alias.h"
12 #include "base/logging.h"
13 #include "base/win/scoped_handle.h"
15 namespace {
17 void CollectChildGDIUsageAndDie(DWORD parent_pid) {
18 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
19 CHECK_NE(INVALID_HANDLE_VALUE, snapshot);
21 int child_count = 0;
22 base::debug::Alias(&child_count);
23 int peak_gdi_count = 0;
24 base::debug::Alias(&peak_gdi_count);
25 int sum_gdi_count = 0;
26 base::debug::Alias(&sum_gdi_count);
27 int sum_user_count = 0;
28 base::debug::Alias(&sum_user_count);
30 PROCESSENTRY32 proc_entry = {0};
31 proc_entry.dwSize = sizeof(PROCESSENTRY32);
32 CHECK(Process32First(snapshot, &proc_entry));
34 do {
35 if (parent_pid != proc_entry.th32ParentProcessID)
36 continue;
37 // Got a child process. Compute GDI usage.
38 base::win::ScopedHandle process(
39 OpenProcess(PROCESS_QUERY_INFORMATION,
40 FALSE,
41 proc_entry.th32ParentProcessID));
42 if (!process.IsValid())
43 continue;
45 int num_gdi_handles = GetGuiResources(process.Get(), GR_GDIOBJECTS);
46 int num_user_handles = GetGuiResources(process.Get(), GR_USEROBJECTS);
48 // Compute sum and peak counts.
49 ++child_count;
50 sum_user_count += num_user_handles;
51 sum_gdi_count += num_gdi_handles;
52 if (peak_gdi_count < num_gdi_handles)
53 peak_gdi_count = num_gdi_handles;
55 } while (Process32Next(snapshot, &proc_entry));
57 CloseHandle(snapshot);
58 CHECK(false);
61 } // namespace
63 namespace base {
64 namespace debug {
66 void GDIBitmapAllocFailure(BITMAPINFOHEADER* header, HANDLE shared_section) {
67 // Make sure parameters are saved in the minidump.
68 DWORD last_error = GetLastError();
70 LONG width = header->biWidth;
71 LONG heigth = header->biHeight;
73 base::debug::Alias(&last_error);
74 base::debug::Alias(&width);
75 base::debug::Alias(&heigth);
76 base::debug::Alias(&shared_section);
78 DWORD num_user_handles = GetGuiResources(GetCurrentProcess(), GR_USEROBJECTS);
80 DWORD num_gdi_handles = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS);
81 if (num_gdi_handles == 0) {
82 DWORD get_gui_resources_error = GetLastError();
83 base::debug::Alias(&get_gui_resources_error);
84 CHECK(false);
87 base::debug::Alias(&num_gdi_handles);
88 base::debug::Alias(&num_user_handles);
90 const DWORD kLotsOfHandles = 9990;
91 CHECK_LE(num_gdi_handles, kLotsOfHandles);
93 PROCESS_MEMORY_COUNTERS_EX pmc;
94 pmc.cb = sizeof(pmc);
95 CHECK(GetProcessMemoryInfo(GetCurrentProcess(),
96 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc),
97 sizeof(pmc)));
98 const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB
99 CHECK_LE(pmc.PagefileUsage, kLotsOfMemory);
100 CHECK_LE(pmc.PrivateUsage, kLotsOfMemory);
102 void* small_data = NULL;
103 base::debug::Alias(&small_data);
105 if (std::abs(heigth) * width > 100) {
106 // Huh, that's weird. We don't have crazy handle count, we don't have
107 // ridiculous memory usage. Try to allocate a small bitmap and see if that
108 // fails too.
109 header->biWidth = 5;
110 header->biHeight = -5;
111 HBITMAP small_bitmap = CreateDIBSection(
112 NULL, reinterpret_cast<BITMAPINFO*>(&header),
113 0, &small_data, shared_section, 0);
114 CHECK(small_bitmap != NULL);
115 DeleteObject(small_bitmap);
117 // Maybe the child processes are the ones leaking GDI or USER resouces.
118 CollectChildGDIUsageAndDie(GetCurrentProcessId());
121 } // namespace debug
122 } // namespace base