| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/debug/gdi_debug_util_win.h" | 4 #include "base/debug/gdi_debug_util_win.h" |
| 5 | 5 |
| 6 #include <cmath> | 6 #include <cmath> |
| 7 | 7 |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <TlHelp32.h> | 10 #include <TlHelp32.h> |
| 11 | 11 |
| 12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
| 15 #include "base/win/win_util.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 void CollectChildGDIUsageAndDie(DWORD parent_pid) { | 19 void CollectChildGDIUsageAndDie(DWORD parent_pid) { |
| 19 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 20 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 20 CHECK_NE(INVALID_HANDLE_VALUE, snapshot); | 21 CHECK_NE(INVALID_HANDLE_VALUE, snapshot); |
| 21 | 22 |
| 22 int child_count = 0; | 23 int child_count = 0; |
| 23 base::debug::Alias(&child_count); | 24 base::debug::Alias(&child_count); |
| 24 int peak_gdi_count = 0; | 25 int peak_gdi_count = 0; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 58 |
| 58 CloseHandle(snapshot); | 59 CloseHandle(snapshot); |
| 59 CHECK(false); | 60 CHECK(false); |
| 60 } | 61 } |
| 61 | 62 |
| 62 } // namespace | 63 } // namespace |
| 63 | 64 |
| 64 namespace base { | 65 namespace base { |
| 65 namespace debug { | 66 namespace debug { |
| 66 | 67 |
| 67 void GDIBitmapAllocFailure(BITMAPINFOHEADER* header, HANDLE shared_section) { | 68 void CollectGDIUsageAndDie(BITMAPINFOHEADER* header, HANDLE shared_section) { |
| 68 // Make sure parameters are saved in the minidump. | 69 // Make sure parameters are saved in the minidump. |
| 69 DWORD last_error = GetLastError(); | 70 DWORD last_error = GetLastError(); |
| 71 bool is_gdi_available = base::win::IsUser32AndGdi32Available(); |
| 70 | 72 |
| 71 LONG width = header->biWidth; | 73 LONG width = header ? header->biWidth : 0; |
| 72 LONG heigth = header->biHeight; | 74 LONG height = header ? header->biHeight : 0; |
| 73 | 75 |
| 74 base::debug::Alias(&last_error); | 76 base::debug::Alias(&last_error); |
| 77 base::debug::Alias(&is_gdi_available); |
| 75 base::debug::Alias(&width); | 78 base::debug::Alias(&width); |
| 76 base::debug::Alias(&heigth); | 79 base::debug::Alias(&height); |
| 77 base::debug::Alias(&shared_section); | 80 base::debug::Alias(&shared_section); |
| 78 | 81 |
| 79 DWORD num_user_handles = GetGuiResources(GetCurrentProcess(), GR_USEROBJECTS); | 82 DWORD num_user_handles = GetGuiResources(GetCurrentProcess(), GR_USEROBJECTS); |
| 80 | 83 |
| 81 DWORD num_gdi_handles = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS); | 84 DWORD num_gdi_handles = GetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS); |
| 82 if (num_gdi_handles == 0) { | 85 if (num_gdi_handles == 0) { |
| 83 DWORD get_gui_resources_error = GetLastError(); | 86 DWORD get_gui_resources_error = GetLastError(); |
| 84 base::debug::Alias(&get_gui_resources_error); | 87 base::debug::Alias(&get_gui_resources_error); |
| 85 CHECK(false); | 88 CHECK(false); |
| 86 } | 89 } |
| 87 | 90 |
| 88 base::debug::Alias(&num_gdi_handles); | 91 base::debug::Alias(&num_gdi_handles); |
| 89 base::debug::Alias(&num_user_handles); | 92 base::debug::Alias(&num_user_handles); |
| 90 | 93 |
| 91 const DWORD kLotsOfHandles = 9990; | 94 const DWORD kLotsOfHandles = 9990; |
| 92 CHECK_LE(num_gdi_handles, kLotsOfHandles); | 95 CHECK_LE(num_gdi_handles, kLotsOfHandles); |
| 93 | 96 |
| 94 PROCESS_MEMORY_COUNTERS_EX pmc; | 97 PROCESS_MEMORY_COUNTERS_EX pmc; |
| 95 pmc.cb = sizeof(pmc); | 98 pmc.cb = sizeof(pmc); |
| 96 CHECK(GetProcessMemoryInfo(GetCurrentProcess(), | 99 CHECK(GetProcessMemoryInfo(GetCurrentProcess(), |
| 97 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), | 100 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), |
| 98 sizeof(pmc))); | 101 sizeof(pmc))); |
| 99 const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB | 102 const size_t kLotsOfMemory = 1500 * 1024 * 1024; // 1.5GB |
| 100 CHECK_LE(pmc.PagefileUsage, kLotsOfMemory); | 103 CHECK_LE(pmc.PagefileUsage, kLotsOfMemory); |
| 101 CHECK_LE(pmc.PrivateUsage, kLotsOfMemory); | 104 CHECK_LE(pmc.PrivateUsage, kLotsOfMemory); |
| 102 | 105 |
| 103 void* small_data = NULL; | 106 void* small_data = nullptr; |
| 104 base::debug::Alias(&small_data); | 107 base::debug::Alias(&small_data); |
| 105 | 108 |
| 106 if (std::abs(heigth) * width > 100) { | 109 if (std::abs(height) * width > 100) { |
| 107 // Huh, that's weird. We don't have crazy handle count, we don't have | 110 // Huh, that's weird. We don't have crazy handle count, we don't have |
| 108 // ridiculous memory usage. Try to allocate a small bitmap and see if that | 111 // ridiculous memory usage. Try to allocate a small bitmap and see if that |
| 109 // fails too. | 112 // fails too. |
| 110 header->biWidth = 5; | 113 header->biWidth = 5; |
| 111 header->biHeight = -5; | 114 header->biHeight = -5; |
| 112 HBITMAP small_bitmap = CreateDIBSection( | 115 HBITMAP small_bitmap = CreateDIBSection( |
| 113 NULL, reinterpret_cast<BITMAPINFO*>(&header), | 116 nullptr, reinterpret_cast<BITMAPINFO*>(&header), |
| 114 0, &small_data, shared_section, 0); | 117 0, &small_data, shared_section, 0); |
| 115 CHECK(small_bitmap != NULL); | 118 CHECK(small_bitmap != nullptr); |
| 116 DeleteObject(small_bitmap); | 119 DeleteObject(small_bitmap); |
| 117 } | 120 } |
| 118 // Maybe the child processes are the ones leaking GDI or USER resouces. | 121 // Maybe the child processes are the ones leaking GDI or USER resouces. |
| 119 CollectChildGDIUsageAndDie(GetCurrentProcessId()); | 122 CollectChildGDIUsageAndDie(GetCurrentProcessId()); |
| 120 } | 123 } |
| 121 | 124 |
| 122 } // namespace debug | 125 } // namespace debug |
| 123 } // namespace base | 126 } // namespace base |
| OLD | NEW |