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