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); | |
|
grt (UTC plus 2)
2016/04/12 14:31:12
why "- 1" here and below? based on my read of stri
Patrick Monette
2016/04/12 19:13:00
Done.
| |
| 247 base::wcslcpy(crash_key.value, value, | |
| 248 kasko::api::CrashKey::kValueMaxLength - 1); | |
| 249 crash_keys->push_back(crash_key); | |
|
grt (UTC plus 2)
2016/04/12 14:31:12
this is doing extra copies of the strings (once in
Patrick Monette
2016/04/12 19:13:00
Done.
| |
| 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::win::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 base::win::WaitChain::reverse_iterator it; | |
|
grt (UTC plus 2)
2016/04/12 14:31:12
This is a perfect use of <algorithm> and a lambda:
Patrick Monette
2016/04/12 19:13:00
Good idea. Done.
| |
| 275 for (it = wait_chain.rbegin(); it != wait_chain.rend(); ++it) { | |
| 276 if (it->ObjectType == WctThreadType) | |
| 277 break; | |
| 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-%02zu", i).c_str(), | |
|
grt (UTC plus 2)
2016/04/12 14:31:12
#include "base/format_macros.h"
L"hung-process-wai
Patrick Monette
2016/04/12 19:13:00
Done.
| |
| 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 |