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/string_util.h" | |
| 29 #include "base/strings/stringprintf.h" | |
| 28 #include "base/strings/utf_string_conversions.h" | 30 #include "base/strings/utf_string_conversions.h" |
| 29 #include "base/synchronization/waitable_event.h" | 31 #include "base/synchronization/waitable_event.h" |
| 30 #include "base/thread_task_runner_handle.h" | 32 #include "base/thread_task_runner_handle.h" |
| 31 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
| 32 #include "base/time/time.h" | 34 #include "base/time/time.h" |
| 33 #include "base/win/scoped_handle.h" | 35 #include "base/win/scoped_handle.h" |
| 34 #include "base/win/win_util.h" | 36 #include "base/win/win_util.h" |
| 35 | 37 |
| 36 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" | 38 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" |
| 37 #include "chrome/installer/util/util_constants.h" | 39 #include "chrome/installer/util/util_constants.h" |
| 38 #include "components/browser_watcher/endsession_watcher_window_win.h" | 40 #include "components/browser_watcher/endsession_watcher_window_win.h" |
| 39 #include "components/browser_watcher/exit_code_watcher_win.h" | 41 #include "components/browser_watcher/exit_code_watcher_win.h" |
| 40 #include "components/browser_watcher/window_hang_monitor_win.h" | 42 #include "components/browser_watcher/window_hang_monitor_win.h" |
| 41 #include "third_party/kasko/kasko_features.h" | 43 #include "third_party/kasko/kasko_features.h" |
| 42 | 44 |
| 43 #if BUILDFLAG(ENABLE_KASKO) | 45 #if BUILDFLAG(ENABLE_KASKO) |
| 46 #include "base/win/wait_chain.h" | |
| 44 #include "components/crash/content/app/crashpad.h" | 47 #include "components/crash/content/app/crashpad.h" |
| 45 #include "syzygy/kasko/api/reporter.h" | 48 #include "syzygy/kasko/api/reporter.h" |
| 46 #endif | 49 #endif |
| 47 | 50 |
| 48 namespace { | 51 namespace { |
| 49 | 52 |
| 50 // Use the same log facility as Chrome for convenience. | 53 // Use the same log facility as Chrome for convenience. |
| 51 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} | 54 // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
| 52 const GUID kChromeWatcherTraceProviderName = { | 55 const GUID kChromeWatcherTraceProviderName = { |
| 53 0x7fe69228, 0x633e, 0x4f06, | 56 0x7fe69228, 0x633e, 0x4f06, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 std::string env_var; | 228 std::string env_var; |
| 226 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) { | 229 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) { |
| 227 base::string16 wide_env_var; | 230 base::string16 wide_env_var; |
| 228 base::UTF8ToWide(env_var.c_str(), env_var.size(), &wide_env_var); | 231 base::UTF8ToWide(env_var.c_str(), env_var.size(), &wide_env_var); |
| 229 *base_dir = base::FilePath(wide_env_var); | 232 *base_dir = base::FilePath(wide_env_var); |
| 230 } else { | 233 } else { |
| 231 *base_dir = base::FilePath(browser_data_directory); | 234 *base_dir = base::FilePath(browser_data_directory); |
| 232 } | 235 } |
| 233 } | 236 } |
| 234 | 237 |
| 238 void AddCrashKey(const wchar_t* key, | |
| 239 const wchar_t* value, | |
| 240 std::vector<kasko::api::CrashKey>* crash_keys) { | |
| 241 DCHECK(key); | |
| 242 DCHECK(value); | |
| 243 DCHECK(crash_keys); | |
| 244 | |
| 245 kasko::api::CrashKey crash_key; | |
| 246 base::wcslcpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength - 1); | |
| 247 base::wcslcpy(crash_key.value, value, | |
| 248 kasko::api::CrashKey::kValueMaxLength - 1); | |
| 249 crash_keys->push_back(crash_key); | |
| 250 } | |
| 251 | |
| 235 void DumpHungBrowserProcess(DWORD main_thread_id, | 252 void DumpHungBrowserProcess(DWORD main_thread_id, |
| 236 const base::string16& channel, | 253 const base::string16& channel, |
| 237 const base::Process& process) { | 254 const base::Process& process) { |
| 238 // Read the Crashpad module annotations for the process. | 255 // Read the Crashpad module annotations for the process. |
| 239 std::vector<kasko::api::CrashKey> annotations; | 256 std::vector<kasko::api::CrashKey> annotations; |
| 240 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); | 257 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); |
| 241 | 258 |
| 242 // Add a special crash key to distinguish reports generated for a hung | 259 // Add a special crash key to distinguish reports generated for a hung |
| 243 // process. | 260 // process. |
| 244 annotations.push_back(kasko::api::CrashKey{L"hung-process", L"1"}); | 261 AddCrashKey(L"hung-process", L"1", &annotations); |
| 262 | |
| 263 // Use the Wait Chain Traversal API to determine the hung thread. Defaults to | |
| 264 // UI thread on error. The wait chain may point to a different thread in a | |
| 265 // different process for the hung thread. | |
| 266 DWORD hung_thread_id = main_thread_id; | |
| 267 base::Process hung_process = process.Duplicate(); | |
| 268 | |
| 269 base::WaitChain wait_chain; | |
| 270 bool is_deadlock = false; | |
| 271 if (base::win::GetThreadWaitChain(main_thread_id, &wait_chain, | |
| 272 &is_deadlock)) { | |
| 273 // The last thread in the wait chain is nominated as the hung thread. | |
| 274 const WAITCHAIN_NODE_INFO* wait_chain_node = wait_chain.back(); | |
| 275 while (wait_chain_node.ObjectType != WctThreadType) { | |
|
Sigurður Ásgeirsson
2016/04/05 20:07:02
this can underrun the vector.
Patrick Monette
2016/04/05 22:55:31
Fixed.
| |
| 276 wait_chain_node--; | |
| 277 } | |
| 278 | |
| 279 // DO NOT COMMIT: I'll add the same check as Pierre-Antoine for the process | |
| 280 // id race. | |
| 281 hung_process = base::Process::Open(wait_chain_node.ThreadObject.ProcessId); | |
| 282 hung_thread_id = wait_chain_node.ThreadObject.ThreadId; | |
| 283 | |
| 284 // The entire wait chain is added to the crash report via crash keys. | |
| 285 // | |
| 286 // As an example (key : value): | |
| 287 // hung-process-is-deadlock : false | |
| 288 // hung-process-wait-chain-00 : Thread #10242 with status Blocked | |
| 289 // hung-process-wait-chain-01 : Lock of type ThreadWait with status Owned | |
| 290 // hung-process-wait-chain-02 : Thread #77221 with status Blocked | |
| 291 // | |
| 292 AddCrashKey(L"hung-process-is-deadlock", is_deadlock ? L"true" : L"false", | |
| 293 &annotations); | |
| 294 for (size_t i = 0; i < wait_chain.size(); i++) { | |
| 295 AddCrashKey( | |
| 296 base::StringPrintf(L"hung-process-wait-chain-%02zu", i).c_str(), | |
| 297 base::win::WaitChainNodeToString(wait_chain[i]).c_str(), | |
| 298 &annotations); | |
| 299 } | |
| 300 } | |
| 245 | 301 |
| 246 std::vector<const base::char16*> key_buffers; | 302 std::vector<const base::char16*> key_buffers; |
| 247 std::vector<const base::char16*> value_buffers; | 303 std::vector<const base::char16*> value_buffers; |
| 248 for (const auto& crash_key : annotations) { | 304 for (const auto& crash_key : annotations) { |
| 249 key_buffers.push_back(crash_key.name); | 305 key_buffers.push_back(crash_key.name); |
| 250 value_buffers.push_back(crash_key.value); | 306 value_buffers.push_back(crash_key.value); |
| 251 } | 307 } |
| 252 key_buffers.push_back(nullptr); | 308 key_buffers.push_back(nullptr); |
| 253 value_buffers.push_back(nullptr); | 309 value_buffers.push_back(nullptr); |
| 254 | 310 |
| 255 // Synthesize an exception for the main thread. Populate the record with the | 311 // 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 | 312 // current context of the thread to get the stack trace bucketed on the crash |
| 257 // backend. | 313 // backend. |
| 258 CONTEXT thread_context = {}; | 314 CONTEXT thread_context = {}; |
| 259 EXCEPTION_RECORD exception_record = {}; | 315 EXCEPTION_RECORD exception_record = {}; |
| 260 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; | 316 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; |
| 261 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; | 317 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; |
| 262 | 318 |
| 263 base::win::ScopedHandle main_thread(::OpenThread( | 319 base::win::ScopedHandle hung_thread(::OpenThread( |
| 264 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, | 320 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, |
| 265 FALSE, main_thread_id)); | 321 FALSE, hung_thread_id)); |
| 266 | 322 |
| 267 bool have_context = false; | 323 bool have_context = false; |
| 268 if (main_thread.IsValid()) { | 324 if (hung_thread.IsValid()) { |
| 269 DWORD suspend_count = ::SuspendThread(main_thread.Get()); | 325 DWORD suspend_count = ::SuspendThread(hung_thread.Get()); |
| 270 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 326 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 271 if (suspend_count != kSuspendFailed) { | 327 if (suspend_count != kSuspendFailed) { |
| 272 // Best effort capture of the context. | 328 // Best effort capture of the context. |
| 273 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | | 329 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | |
| 274 CONTEXT_INTEGER | CONTEXT_CONTROL; | 330 CONTEXT_INTEGER | CONTEXT_CONTROL; |
| 275 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE) | 331 if (::GetThreadContext(hung_thread.Get(), &thread_context) == TRUE) |
| 276 have_context = true; | 332 have_context = true; |
| 277 | 333 |
| 278 ::ResumeThread(main_thread.Get()); | 334 ::ResumeThread(hung_thread.Get()); |
| 279 } | 335 } |
| 280 } | 336 } |
| 281 | 337 |
| 282 // TODO(erikwright): Make the dump-type channel-dependent. | 338 // TODO(erikwright): Make the dump-type channel-dependent. |
| 283 if (have_context) { | 339 if (have_context) { |
| 284 kasko::api::SendReportForProcess( | 340 kasko::api::SendReportForProcess( |
| 285 process.Handle(), main_thread_id, &exception_pointers, | 341 hung_process.Handle(), hung_thread_id, &exception_pointers, |
| 286 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); | 342 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); |
| 287 } else { | 343 } else { |
| 288 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, | 344 kasko::api::SendReportForProcess(hung_process.Handle(), 0, nullptr, |
| 289 kasko::api::LARGER_DUMP_TYPE, | 345 kasko::api::LARGER_DUMP_TYPE, |
| 290 key_buffers.data(), value_buffers.data()); | 346 key_buffers.data(), value_buffers.data()); |
| 291 } | 347 } |
| 292 } | 348 } |
| 293 | 349 |
| 294 void LoggedDeregisterEventSource(HANDLE event_source_handle) { | 350 void LoggedDeregisterEventSource(HANDLE event_source_handle) { |
| 295 if (!::DeregisterEventSource(event_source_handle)) | 351 if (!::DeregisterEventSource(event_source_handle)) |
| 296 DPLOG(ERROR) << "DeregisterEventSource"; | 352 DPLOG(ERROR) << "DeregisterEventSource"; |
| 297 } | 353 } |
| 298 | 354 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 | 493 |
| 438 // Wind logging down. | 494 // Wind logging down. |
| 439 logging::LogEventProvider::Uninitialize(); | 495 logging::LogEventProvider::Uninitialize(); |
| 440 | 496 |
| 441 return 0; | 497 return 0; |
| 442 } | 498 } |
| 443 | 499 |
| 444 static_assert( | 500 static_assert( |
| 445 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, | 501 std::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value, |
| 446 "WatcherMain() has wrong type"); | 502 "WatcherMain() has wrong type"); |
| OLD | NEW |