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

Unified Diff: client/crashpad_client_win.cc

Issue 1356383002: win: Implement CRASHPAD_SIMULATE_CRASH() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: client/crashpad_client_win.cc
diff --git a/client/crashpad_client_win.cc b/client/crashpad_client_win.cc
index e19aa7469d81a8015f5bd9bc70a52f97761ab999..e65f526b65170586aa17930747700525872f095b 100644
--- a/client/crashpad_client_win.cc
+++ b/client/crashpad_client_win.cc
@@ -27,16 +27,18 @@
namespace {
-// This handle is never closed.
+// These handles are never closed.
HANDLE g_signal_exception = INVALID_HANDLE_VALUE;
+HANDLE g_dump_completed = INVALID_HANDLE_VALUE;
// Where we store the exception information that the crash handler reads.
crashpad::ExceptionInformation g_exception_information;
-LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) {
- // Tracks whether a thread has already entered UnhandledExceptionHandler.
- static base::subtle::AtomicWord have_crashed;
+// Tracks whether a thread has already entered UnhandledExceptionHandler() or
+// DumpWithoutCrash().
+base::subtle::AtomicWord g_capturing_dump;
+LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) {
// This is a per-process handler. While this handler is being invoked, other
// threads are still executing as usual, so multiple threads could enter at
// the same time. Because we're in a crashing state, we shouldn't be doing
@@ -49,7 +51,7 @@ LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) {
// that we won't save the exception pointers from the second and further
// crashes, but contention here is very unlikely, and we'll still have a stack
// that's blocked at this location.
- if (base::subtle::Barrier_AtomicIncrement(&have_crashed, 1) > 1) {
+ if (base::subtle::Barrier_AtomicIncrement(&g_capturing_dump, 1) > 1) {
Mark Mentovai 2015/09/23 20:00:52 Maybe it’d be nice to not have an in-process non-c
scottmg 2015/09/23 22:03:40 Yeah, that would be better. Ideally it might be ju
scottmg 2015/09/24 19:16:52 Restructured.
SleepEx(INFINITE, false);
Mark Mentovai 2015/09/23 20:00:52 But this line is a bigger problem. g_capturing_dum
scottmg 2015/09/24 19:16:52 This problem is gone now, only the crashing path u
}
@@ -59,24 +61,25 @@ LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) {
g_exception_information.exception_pointers =
reinterpret_cast<crashpad::WinVMAddress>(exception_pointers);
- // Now signal the crash server, which will take a dump and then terminate us
- // when it's complete.
+ // Now signal the crash server, which will take a dump and then signal back on
+ // the other event when it's done.
SetEvent(g_signal_exception);
// Time to wait for the handler to create a dump.
const DWORD kMillisecondsUntilTerminate = 60 * 1000;
- // Sleep for a while to allow it to process us. Eventually, we terminate
- // ourselves in case the crash server is gone, so that we don't leave zombies
- // around. This would ideally never happen.
- // TODO(scottmg): Re-add the "reply" event here, for implementing
- // DumpWithoutCrashing.
- Sleep(kMillisecondsUntilTerminate);
-
- LOG(ERROR) << "crash server did not respond, self-terminating";
+ // Once the server has told us it's finished, terminate. If
+ // WaitForSingleObject() times out or fails in some other way, our only
+ // recourse is still to kill ourselves, but we use a different exit code than
+ // if we terminated via a normal path.
+ DWORD result =
+ WaitForSingleObject(g_dump_completed, kMillisecondsUntilTerminate);
- const UINT kCrashExitCodeNoDump = 0xffff7001;
- TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump);
+ const unsigned int kCrashExitCodeNoDump = 0xffff7001;
+ TerminateProcess(GetCurrentProcess(),
+ result == WAIT_OBJECT_0
+ ? exception_pointers->ExceptionRecord->ExceptionCode
+ : kCrashExitCodeNoDump);
return EXCEPTION_CONTINUE_SEARCH;
}
@@ -120,16 +123,60 @@ bool CrashpadClient::SetHandler(const std::string& ipc_port) {
// The server returns these already duplicated to be valid in this process.
g_signal_exception = reinterpret_cast<HANDLE>(
static_cast<uintptr_t>(response.registration.request_report_event));
+ g_dump_completed = reinterpret_cast<HANDLE>(
+ static_cast<uintptr_t>(response.registration.report_completed_event));
return true;
}
bool CrashpadClient::UseHandler() {
- if (g_signal_exception == INVALID_HANDLE_VALUE)
+ if (g_signal_exception == INVALID_HANDLE_VALUE ||
+ g_dump_completed == INVALID_HANDLE_VALUE) {
return false;
+ }
// In theory we could store the previous handler but it is not clear what
// use we have for it.
SetUnhandledExceptionFilter(&UnhandledExceptionHandler);
return true;
}
+// static
+bool CrashpadClient::DumpWithoutCrash() {
+ if (g_signal_exception == INVALID_HANDLE_VALUE ||
+ g_dump_completed == INVALID_HANDLE_VALUE) {
+ LOG(ERROR) << "not registered with a server";
+ return false;
+ }
+
+ if (base::subtle::Barrier_AtomicIncrement(&g_capturing_dump, 1) > 1) {
+ // Someone else is either writing or has crashed, simply abort here.
Mark Mentovai 2015/09/23 20:00:52 Decrement to undo what you just did, since there’s
+ LOG(WARNING) << "already writing";
+ return false;
+ }
+
+ // Create a fake EXCEPTION_POINTERS (with a null EXCEPTION_RECORD), so that
Mark Mentovai 2015/09/23 20:00:52 Expand on the thing about the “null EXCEPTION_RECO
scottmg 2015/09/24 19:16:52 Done.
+ // the handler has a context to start from.
+ CONTEXT context;
+ // TODO(scottmg): Replace with our custom version. See:
+ // https://code.google.com/p/crashpad/issues/detail?id=53.
+ RtlCaptureContext(&context);
Mark Mentovai 2015/09/23 20:00:52 We’ll actually want to capture the context from th
scottmg 2015/09/24 19:16:52 I guess as we discovered, this happens to be what
Mark Mentovai 2015/09/24 19:26:05 Yeah, I already have an x86 CaptureContext for Win
scottmg 2015/09/24 20:15:22 OK, I can just land it slightly borked for now.
+ EXCEPTION_POINTERS exception_pointers = {0};
Mark Mentovai 2015/09/23 22:20:38 P.S. On Mac, we have our own made-up exception ID
scottmg 2015/09/24 19:16:52 My thinking was that we don't really want it to ha
Mark Mentovai 2015/09/24 19:26:05 Do we at least wind up with a minidump with a MINI
scottmg 2015/09/24 20:15:22 Oh, you're right. I had only run the (in-memory) t
+ exception_pointers.ContextRecord = &context;
+
+ g_exception_information.thread_id = GetCurrentThreadId();
Mark Mentovai 2015/09/23 20:00:52 This through WaitForSingleObject() looks duplicate
+ g_exception_information.exception_pointers =
+ reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers);
+
+ // Now signal the crash server, which will take a dump and then signal back on
+ // the other event when it's done.
+ SetEvent(g_signal_exception);
+
+ // Wait until the server tells us it's finished.
+ DWORD result = WaitForSingleObject(g_dump_completed, INFINITE);
+ PLOG_IF(ERROR, result != WAIT_OBJECT_0) << "WaitForSingleObject";
+
+ base::subtle::Barrier_AtomicIncrement(&g_capturing_dump, -1);
+ return true;
+}
+
+
Mark Mentovai 2015/09/23 20:00:52 Extra blank line.
scottmg 2015/09/24 19:16:52 Done.
} // namespace crashpad

Powered by Google App Engine
This is Rietveld 408576698