Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/chrome_watcher/chrome_watcher_main.cc

Issue 1834463002: Identify the hung thread using the Wait Chain Traversal API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments/questions Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/chrome_watcher/chrome_watcher.gypi ('k') | chrome/chrome_watcher/wait_chain_util_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 4
5 #include <windows.h> 5 #include <windows.h>
6 #include <sddl.h> 6 #include <sddl.h>
7 7
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/at_exit.h" 10 #include "base/at_exit.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/callback_helpers.h" 13 #include "base/callback_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/environment.h" 15 #include "base/environment.h"
16 #include "base/file_version_info.h" 16 #include "base/file_version_info.h"
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/logging_win.h" 18 #include "base/logging_win.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
21 #include "base/process/process.h" 21 #include "base/process/process.h"
22 #include "base/run_loop.h" 22 #include "base/run_loop.h"
23 #include "base/sequenced_task_runner.h" 23 #include "base/sequenced_task_runner.h"
24 #include "base/single_thread_task_runner.h" 24 #include "base/single_thread_task_runner.h"
25 #include "base/strings/string16.h" 25 #include "base/strings/string16.h"
26 #include "base/strings/string_number_conversions.h" 26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h" 27 #include "base/strings/string_piece.h"
28 #include "base/strings/stringprintf.h"
28 #include "base/strings/utf_string_conversions.h" 29 #include "base/strings/utf_string_conversions.h"
29 #include "base/synchronization/waitable_event.h" 30 #include "base/synchronization/waitable_event.h"
30 #include "base/thread_task_runner_handle.h" 31 #include "base/thread_task_runner_handle.h"
31 #include "base/threading/thread.h" 32 #include "base/threading/thread.h"
32 #include "base/time/time.h" 33 #include "base/time/time.h"
33 #include "base/win/scoped_handle.h" 34 #include "base/win/scoped_handle.h"
34 #include "base/win/win_util.h" 35 #include "base/win/win_util.h"
35 36
36 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" 37 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
37 #include "chrome/installer/util/util_constants.h" 38 #include "chrome/installer/util/util_constants.h"
38 #include "components/browser_watcher/endsession_watcher_window_win.h" 39 #include "components/browser_watcher/endsession_watcher_window_win.h"
39 #include "components/browser_watcher/exit_code_watcher_win.h" 40 #include "components/browser_watcher/exit_code_watcher_win.h"
40 #include "components/browser_watcher/window_hang_monitor_win.h" 41 #include "components/browser_watcher/window_hang_monitor_win.h"
41 #include "third_party/kasko/kasko_features.h" 42 #include "third_party/kasko/kasko_features.h"
42 43
43 #if BUILDFLAG(ENABLE_KASKO) 44 #if BUILDFLAG(ENABLE_KASKO)
45 #include "chrome/chrome_watcher/wait_chain_util_win.h"
44 #include "components/crash/content/app/crashpad.h" 46 #include "components/crash/content/app/crashpad.h"
45 #include "syzygy/kasko/api/reporter.h" 47 #include "syzygy/kasko/api/reporter.h"
46 #endif 48 #endif
47 49
48 namespace { 50 namespace {
49 51
50 // Use the same log facility as Chrome for convenience. 52 // Use the same log facility as Chrome for convenience.
51 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} 53 // {7FE69228-633E-4f06-80C1-527FEA23E3A7}
52 const GUID kChromeWatcherTraceProviderName = { 54 const GUID kChromeWatcherTraceProviderName = {
53 0x7fe69228, 0x633e, 0x4f06, 55 0x7fe69228, 0x633e, 0x4f06,
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 std::string env_var; 227 std::string env_var;
226 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) { 228 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) {
227 base::string16 wide_env_var; 229 base::string16 wide_env_var;
228 base::UTF8ToWide(env_var.c_str(), env_var.size(), &wide_env_var); 230 base::UTF8ToWide(env_var.c_str(), env_var.size(), &wide_env_var);
229 *base_dir = base::FilePath(wide_env_var); 231 *base_dir = base::FilePath(wide_env_var);
230 } else { 232 } else {
231 *base_dir = base::FilePath(browser_data_directory); 233 *base_dir = base::FilePath(browser_data_directory);
232 } 234 }
233 } 235 }
234 236
237 void AddCrashKey(const wchar_t* key,
238 const wchar_t* value,
239 std::vector<kasko::api::CrashKey>* crash_keys) {
240 DCHECK(key);
241 DCHECK(value);
242 DCHECK(crash_keys);
243
244 kasko::api::CrashKey crash_key;
245 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength);
manzagop (departed) 2016/03/31 20:24:45 Do you need a -1 to for \0?
Patrick Monette 2016/04/05 19:03:12 I'm not sure but I've gone for the safe way and ad
246 std::wcsncpy(crash_key.value, value, kasko::api::CrashKey::kValueMaxLength);
247 crash_keys->push_back(crash_key);
248 }
249
235 void DumpHungBrowserProcess(DWORD main_thread_id, 250 void DumpHungBrowserProcess(DWORD main_thread_id,
236 const base::string16& channel, 251 const base::string16& channel,
237 const base::Process& process) { 252 const base::Process& process) {
238 // Read the Crashpad module annotations for the process. 253 // Read the Crashpad module annotations for the process.
239 std::vector<kasko::api::CrashKey> annotations; 254 std::vector<kasko::api::CrashKey> annotations;
240 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); 255 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations);
241 256
242 // Add a special crash key to distinguish reports generated for a hung 257 // Add a special crash key to distinguish reports generated for a hung
243 // process. 258 // process.
244 annotations.push_back(kasko::api::CrashKey{L"hung-process", L"1"}); 259 AddCrashKey(L"hung-process", L"1", &annotations);
260
261 // Use the Wait Chain Traversal API to determine the hung thread. Defaults to
262 // UI thread on error.
263 std::vector<WAITCHAIN_NODE_INFO> wait_chain;
264 bool is_deadlock = false;
265
266 DWORD hung_thread_id = main_thread_id;
267 if (GetThreadWaitChain(main_thread_id, &wait_chain, &is_deadlock)) {
manzagop (departed) 2016/03/29 13:57:48 IIUC this can cross process boundaries and the cha
Sigurður Ásgeirsson 2016/03/29 16:57:32 oh - interesting - this is true.
Patrick Monette 2016/04/05 19:03:12 Done.
268 // The last thread in the wait chain is nominated as the hung thread.
269 DCHECK(wait_chain.back().ObjectType == WctThreadType);
manzagop (departed) 2016/03/29 13:57:48 How sure are we the last element is a thread? I gu
Patrick Monette 2016/04/05 19:03:12 Done. In some cases, it's possible that the last e
270 hung_thread_id = wait_chain.back().ThreadObject.ThreadId;
271
272 // The entire wait chain is added to the crash report via crash keys.
273 //
274 // As an example (key : value):
275 // hung-process-is-deadlock : false
276 // hung-process-wait-chain-00 : Thread #10242 with status Blocked
277 // hung-process-wait-chain-01 : Lock of type ThreadWait with status Owned
278 // hung-process-wait-chain-02 : Thread #77221 with status Blocked
279 //
280 AddCrashKey(L"hung-process-is-deadlock", is_deadlock ? L"true" : L"false",
281 &annotations);
282 for (size_t i = 0; i < wait_chain.size(); i++) {
283 AddCrashKey(
284 base::StringPrintf(L"hung-process-wait-chain-%02zu", i).c_str(),
285 WaitChainNodeToString(wait_chain[i]).c_str(), &annotations);
286 }
287 }
245 288
246 std::vector<const base::char16*> key_buffers; 289 std::vector<const base::char16*> key_buffers;
247 std::vector<const base::char16*> value_buffers; 290 std::vector<const base::char16*> value_buffers;
248 for (const auto& crash_key : annotations) { 291 for (const auto& crash_key : annotations) {
249 key_buffers.push_back(crash_key.name); 292 key_buffers.push_back(crash_key.name);
250 value_buffers.push_back(crash_key.value); 293 value_buffers.push_back(crash_key.value);
251 } 294 }
252 key_buffers.push_back(nullptr); 295 key_buffers.push_back(nullptr);
253 value_buffers.push_back(nullptr); 296 value_buffers.push_back(nullptr);
254 297
255 // Synthesize an exception for the main thread. Populate the record with the 298 // Synthesize an exception for the hung thread. Populate the record with the
256 // current context of the thread to get the stack trace bucketed on the crash 299 // current context of the thread to get the stack trace bucketed on the crash
257 // backend. 300 // backend.
258 CONTEXT thread_context = {}; 301 CONTEXT thread_context = {};
259 EXCEPTION_RECORD exception_record = {}; 302 EXCEPTION_RECORD exception_record = {};
260 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; 303 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
261 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; 304 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context};
262 305
263 base::win::ScopedHandle main_thread(::OpenThread( 306 base::win::ScopedHandle hung_thread(::OpenThread(
264 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, 307 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
265 FALSE, main_thread_id)); 308 FALSE, hung_thread_id));
266 309
267 bool have_context = false; 310 bool have_context = false;
268 if (main_thread.IsValid()) { 311 if (hung_thread.IsValid()) {
269 DWORD suspend_count = ::SuspendThread(main_thread.Get()); 312 DWORD suspend_count = ::SuspendThread(hung_thread.Get());
270 const DWORD kSuspendFailed = static_cast<DWORD>(-1); 313 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
271 if (suspend_count != kSuspendFailed) { 314 if (suspend_count != kSuspendFailed) {
272 // Best effort capture of the context. 315 // Best effort capture of the context.
273 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | 316 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS |
274 CONTEXT_INTEGER | CONTEXT_CONTROL; 317 CONTEXT_INTEGER | CONTEXT_CONTROL;
275 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE) 318 if (::GetThreadContext(hung_thread.Get(), &thread_context) == TRUE)
276 have_context = true; 319 have_context = true;
277 320
278 ::ResumeThread(main_thread.Get()); 321 ::ResumeThread(hung_thread.Get());
279 } 322 }
280 } 323 }
281 324
282 // TODO(erikwright): Make the dump-type channel-dependent. 325 // TODO(erikwright): Make the dump-type channel-dependent.
283 if (have_context) { 326 if (have_context) {
284 kasko::api::SendReportForProcess( 327 kasko::api::SendReportForProcess(
285 process.Handle(), main_thread_id, &exception_pointers, 328 process.Handle(), hung_thread_id, &exception_pointers,
286 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); 329 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data());
287 } else { 330 } else {
288 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, 331 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr,
289 kasko::api::LARGER_DUMP_TYPE, 332 kasko::api::LARGER_DUMP_TYPE,
290 key_buffers.data(), value_buffers.data()); 333 key_buffers.data(), value_buffers.data());
291 } 334 }
292 } 335 }
293 336
294 void LoggedDeregisterEventSource(HANDLE event_source_handle) { 337 void LoggedDeregisterEventSource(HANDLE event_source_handle) {
295 if (!::DeregisterEventSource(event_source_handle)) 338 if (!::DeregisterEventSource(event_source_handle))
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 480
438 // Wind logging down. 481 // Wind logging down.
439 logging::LogEventProvider::Uninitialize(); 482 logging::LogEventProvider::Uninitialize();
440 483
441 return 0; 484 return 0;
442 } 485 }
443 486
444 static_assert( 487 static_assert(
445 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, 488 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value,
446 "WatcherMain() has wrong type"); 489 "WatcherMain() has wrong type");
OLDNEW
« no previous file with comments | « chrome/chrome_watcher/chrome_watcher.gypi ('k') | chrome/chrome_watcher/wait_chain_util_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698