Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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(std::vector<kasko::api::CrashKey>* crash_keys, | |
|
Sigurður Ásgeirsson
2016/03/24 19:30:18
nit: crash_keys is an out param, by convention put
Patrick Monette
2016/03/24 23:21:03
Done.
| |
| 238 const wchar_t* key, | |
| 239 const wchar_t* value) { | |
| 240 DCHECK(crash_keys); | |
|
manzagop (departed)
2016/03/24 19:36:36
DCHECK the others as well?
Patrick Monette
2016/03/24 23:21:03
Done.
| |
| 241 | |
| 242 kasko::api::CrashKey crash_key; | |
| 243 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength); | |
| 244 std::wcsncpy(crash_key.value, value, kasko::api::CrashKey::kValueMaxLength); | |
| 245 crash_keys->push_back(crash_key); | |
| 246 } | |
| 247 | |
| 235 void DumpHungBrowserProcess(DWORD main_thread_id, | 248 void DumpHungBrowserProcess(DWORD main_thread_id, |
| 236 const base::string16& channel, | 249 const base::string16& channel, |
| 237 const base::Process& process) { | 250 const base::Process& process) { |
| 238 // Read the Crashpad module annotations for the process. | 251 // Read the Crashpad module annotations for the process. |
| 239 std::vector<kasko::api::CrashKey> annotations; | 252 std::vector<kasko::api::CrashKey> annotations; |
| 240 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); | 253 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); |
| 241 | 254 |
| 242 // Add a special crash key to distinguish reports generated for a hung | 255 // Add a special crash key to distinguish reports generated for a hung |
| 243 // process. | 256 // process. |
| 244 annotations.push_back(kasko::api::CrashKey{L"hung-process", L"1"}); | 257 AddCrashKey(&annotations, L"hung-process", L"1"); |
| 258 | |
| 259 // Use the Wait Chain Traversal API to determine the hung thread. Defaults to | |
| 260 // UI thread on error. | |
| 261 std::vector<WAITCHAIN_NODE_INFO> wait_chain; | |
| 262 bool is_deadlock = false; | |
| 263 | |
| 264 DWORD hung_thread_id = main_thread_id; | |
| 265 if (GetThreadWaitChain(main_thread_id, &wait_chain, &is_deadlock)) { | |
| 266 // The last thread in the wait chain is nominated as the hung thread. | |
| 267 DCHECK(wait_chain.back().ObjectType == WctThreadType); | |
| 268 hung_thread_id = wait_chain.back().ThreadObject.ThreadId; | |
| 269 | |
| 270 AddCrashKey(&annotations, L"hung-process-is-deadlock", | |
| 271 is_deadlock ? L"true" : L"false"); | |
| 272 | |
| 273 for (size_t i = 0; i < wait_chain.size(); i++) { | |
|
Sigurður Ásgeirsson
2016/03/24 19:30:18
Some running commentary here on what you expect th
Patrick Monette
2016/03/24 23:21:03
Done.
| |
| 274 AddCrashKey(&annotations, | |
| 275 base::StringPrintf(L"hung-process-wait-chain-%zu", i).c_str(), | |
|
manzagop (departed)
2016/03/24 19:36:36
Since the chain can contain up to 16 things, perha
Patrick Monette
2016/03/24 23:21:03
Done.
| |
| 276 WaitChainNodeToString(wait_chain[i]).c_str()); | |
| 277 } | |
| 278 } | |
| 245 | 279 |
| 246 std::vector<const base::char16*> key_buffers; | 280 std::vector<const base::char16*> key_buffers; |
| 247 std::vector<const base::char16*> value_buffers; | 281 std::vector<const base::char16*> value_buffers; |
| 248 for (const auto& crash_key : annotations) { | 282 for (const auto& crash_key : annotations) { |
| 249 key_buffers.push_back(crash_key.name); | 283 key_buffers.push_back(crash_key.name); |
| 250 value_buffers.push_back(crash_key.value); | 284 value_buffers.push_back(crash_key.value); |
| 251 } | 285 } |
| 252 key_buffers.push_back(nullptr); | 286 key_buffers.push_back(nullptr); |
| 253 value_buffers.push_back(nullptr); | 287 value_buffers.push_back(nullptr); |
| 254 | 288 |
| 255 // Synthesize an exception for the main thread. Populate the record with the | 289 // 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 | 290 // current context of the thread to get the stack trace bucketed on the crash |
| 257 // backend. | 291 // backend. |
| 258 CONTEXT thread_context = {}; | 292 CONTEXT thread_context = {}; |
| 259 EXCEPTION_RECORD exception_record = {}; | 293 EXCEPTION_RECORD exception_record = {}; |
| 260 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; | 294 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; |
| 261 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; | 295 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; |
| 262 | 296 |
| 263 base::win::ScopedHandle main_thread(::OpenThread( | 297 base::win::ScopedHandle hung_thread(::OpenThread( |
| 264 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, | 298 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, |
| 265 FALSE, main_thread_id)); | 299 FALSE, hung_thread_id)); |
| 266 | 300 |
| 267 bool have_context = false; | 301 bool have_context = false; |
| 268 if (main_thread.IsValid()) { | 302 if (hung_thread.IsValid()) { |
| 269 DWORD suspend_count = ::SuspendThread(main_thread.Get()); | 303 DWORD suspend_count = ::SuspendThread(hung_thread.Get()); |
| 270 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 304 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 271 if (suspend_count != kSuspendFailed) { | 305 if (suspend_count != kSuspendFailed) { |
| 272 // Best effort capture of the context. | 306 // Best effort capture of the context. |
| 273 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | | 307 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | |
| 274 CONTEXT_INTEGER | CONTEXT_CONTROL; | 308 CONTEXT_INTEGER | CONTEXT_CONTROL; |
| 275 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE) | 309 if (::GetThreadContext(hung_thread.Get(), &thread_context) == TRUE) |
| 276 have_context = true; | 310 have_context = true; |
| 277 | 311 |
| 278 ::ResumeThread(main_thread.Get()); | 312 ::ResumeThread(hung_thread.Get()); |
| 279 } | 313 } |
| 280 } | 314 } |
| 281 | 315 |
| 282 // TODO(erikwright): Make the dump-type channel-dependent. | 316 // TODO(erikwright): Make the dump-type channel-dependent. |
| 283 if (have_context) { | 317 if (have_context) { |
| 284 kasko::api::SendReportForProcess( | 318 kasko::api::SendReportForProcess( |
| 285 process.Handle(), main_thread_id, &exception_pointers, | 319 process.Handle(), hung_thread_id, &exception_pointers, |
| 286 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); | 320 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); |
| 287 } else { | 321 } else { |
| 288 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, | 322 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, |
| 289 kasko::api::LARGER_DUMP_TYPE, | 323 kasko::api::LARGER_DUMP_TYPE, |
| 290 key_buffers.data(), value_buffers.data()); | 324 key_buffers.data(), value_buffers.data()); |
| 291 } | 325 } |
| 292 } | 326 } |
| 293 | 327 |
| 294 void LoggedDeregisterEventSource(HANDLE event_source_handle) { | 328 void LoggedDeregisterEventSource(HANDLE event_source_handle) { |
| 295 if (!::DeregisterEventSource(event_source_handle)) | 329 if (!::DeregisterEventSource(event_source_handle)) |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 | 471 |
| 438 // Wind logging down. | 472 // Wind logging down. |
| 439 logging::LogEventProvider::Uninitialize(); | 473 logging::LogEventProvider::Uninitialize(); |
| 440 | 474 |
| 441 return 0; | 475 return 0; |
| 442 } | 476 } |
| 443 | 477 |
| 444 static_assert( | 478 static_assert( |
| 445 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, | 479 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, |
| 446 "WatcherMain() has wrong type"); | 480 "WatcherMain() has wrong type"); |
| OLD | NEW |