Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "client/crashpad_client.h" | 15 #include "client/crashpad_client.h" |
| 16 | 16 |
| 17 #include <string.h> | 17 #include <string.h> |
| 18 #include <windows.h> | 18 #include <windows.h> |
| 19 | 19 |
| 20 #include "base/atomicops.h" | 20 #include "base/atomicops.h" |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/synchronization/lock.h" | |
| 24 #include "util/file/file_io.h" | 25 #include "util/file/file_io.h" |
| 25 #include "util/win/registration_protocol_win.h" | 26 #include "util/win/registration_protocol_win.h" |
| 26 #include "util/win/scoped_handle.h" | 27 #include "util/win/scoped_handle.h" |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 // This handle is never closed. | 31 // This handle is never closed. This is used to signal to the server that a dump |
| 32 // should be taken in the event of a crash. | |
| 31 HANDLE g_signal_exception = INVALID_HANDLE_VALUE; | 33 HANDLE g_signal_exception = INVALID_HANDLE_VALUE; |
| 32 | 34 |
| 33 // Where we store the exception information that the crash handler reads. | 35 // Where we store the exception information that the crash handler reads. |
| 34 crashpad::ExceptionInformation g_exception_information; | 36 crashpad::ExceptionInformation g_crash_exception_information; |
| 37 | |
| 38 // These handles are never closed. g_signal_non_crash_dump is used to signal to | |
| 39 // the server to take a dump (not due to an exception), and the server will | |
| 40 // signal g_non_crash_dump_done when the dump is completed. | |
| 41 HANDLE g_signal_non_crash_dump = INVALID_HANDLE_VALUE; | |
| 42 HANDLE g_non_crash_dump_done = INVALID_HANDLE_VALUE; | |
| 43 | |
| 44 // Guards multiple simultaneous calls to DumpWithoutCrash(). This is leaked. | |
| 45 base::Lock* g_non_crash_dump_lock; | |
| 46 | |
| 47 // Where we store a pointer to the context information when taking a non-crash | |
| 48 // dump. | |
| 49 crashpad::ExceptionInformation g_non_crash_exception_information; | |
| 35 | 50 |
| 36 LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) { | 51 LONG WINAPI UnhandledExceptionHandler(EXCEPTION_POINTERS* exception_pointers) { |
| 37 // Tracks whether a thread has already entered UnhandledExceptionHandler. | 52 // Tracks whether a thread has already entered UnhandledExceptionHandler. |
| 38 static base::subtle::AtomicWord have_crashed; | 53 static base::subtle::AtomicWord have_crashed; |
| 39 | 54 |
| 40 // This is a per-process handler. While this handler is being invoked, other | 55 // This is a per-process handler. While this handler is being invoked, other |
| 41 // threads are still executing as usual, so multiple threads could enter at | 56 // threads are still executing as usual, so multiple threads could enter at |
| 42 // the same time. Because we're in a crashing state, we shouldn't be doing | 57 // the same time. Because we're in a crashing state, we shouldn't be doing |
| 43 // anything that might cause allocations, call into kernel mode, etc. So, we | 58 // anything that might cause allocations, call into kernel mode, etc. So, we |
| 44 // don't want to take a critical section here to avoid simultaneous access to | 59 // don't want to take a critical section here to avoid simultaneous access to |
| 45 // the global exception pointers in ExceptionInformation. Because the crash | 60 // the global exception pointers in ExceptionInformation. Because the crash |
| 46 // handler will record all threads, it's fine to simply have the second and | 61 // handler will record all threads, it's fine to simply have the second and |
| 47 // subsequent entrants block here. They will soon be suspended by the crash | 62 // subsequent entrants block here. They will soon be suspended by the crash |
| 48 // handler, and then the entire process will be terminated below. This means | 63 // handler, and then the entire process will be terminated below. This means |
| 49 // that we won't save the exception pointers from the second and further | 64 // that we won't save the exception pointers from the second and further |
| 50 // crashes, but contention here is very unlikely, and we'll still have a stack | 65 // crashes, but contention here is very unlikely, and we'll still have a stack |
| 51 // that's blocked at this location. | 66 // that's blocked at this location. |
| 52 if (base::subtle::Barrier_AtomicIncrement(&have_crashed, 1) > 1) { | 67 if (base::subtle::Barrier_AtomicIncrement(&have_crashed, 1) > 1) { |
| 53 SleepEx(INFINITE, false); | 68 SleepEx(INFINITE, false); |
| 54 } | 69 } |
| 55 | 70 |
| 56 // Otherwise, we're the first thread, so record the exception pointer and | 71 // Otherwise, we're the first thread, so record the exception pointer and |
| 57 // signal the crash handler. | 72 // signal the crash handler. |
| 58 g_exception_information.thread_id = GetCurrentThreadId(); | 73 g_crash_exception_information.thread_id = GetCurrentThreadId(); |
| 59 g_exception_information.exception_pointers = | 74 g_crash_exception_information.exception_pointers = |
| 60 reinterpret_cast<crashpad::WinVMAddress>(exception_pointers); | 75 reinterpret_cast<crashpad::WinVMAddress>(exception_pointers); |
| 61 | 76 |
| 62 // Now signal the crash server, which will take a dump and then terminate us | 77 // Now signal the crash server, which will take a dump and then terminate us |
| 63 // when it's complete. | 78 // when it's complete. |
| 64 SetEvent(g_signal_exception); | 79 SetEvent(g_signal_exception); |
| 65 | 80 |
| 66 // Time to wait for the handler to create a dump. | 81 // Time to wait for the handler to create a dump. |
| 67 const DWORD kMillisecondsUntilTerminate = 60 * 1000; | 82 const DWORD kMillisecondsUntilTerminate = 60 * 1000; |
| 68 | 83 |
| 69 // Sleep for a while to allow it to process us. Eventually, we terminate | 84 // Sleep for a while to allow it to process us. Eventually, we terminate |
| 70 // ourselves in case the crash server is gone, so that we don't leave zombies | 85 // ourselves in case the crash server is gone, so that we don't leave zombies |
| 71 // around. This would ideally never happen. | 86 // around. This would ideally never happen. |
| 72 // TODO(scottmg): Re-add the "reply" event here, for implementing | 87 // TODO(scottmg): Re-add the "reply" event here, for implementing |
|
Mark Mentovai
2015/09/25 17:21:01
This comment is obsolete.
scottmg
2015/09/25 19:41:05
TODONE.
| |
| 73 // DumpWithoutCrashing. | 88 // DumpWithoutCrashing. |
| 74 Sleep(kMillisecondsUntilTerminate); | 89 Sleep(kMillisecondsUntilTerminate); |
| 75 | 90 |
| 76 LOG(ERROR) << "crash server did not respond, self-terminating"; | 91 LOG(ERROR) << "crash server did not respond, self-terminating"; |
| 77 | 92 |
| 78 const UINT kCrashExitCodeNoDump = 0xffff7001; | 93 const UINT kCrashExitCodeNoDump = 0xffff7001; |
| 79 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump); | 94 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump); |
| 80 | 95 |
| 81 return EXCEPTION_CONTINUE_SEARCH; | 96 return EXCEPTION_CONTINUE_SEARCH; |
| 82 } | 97 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 95 const base::FilePath& handler, | 110 const base::FilePath& handler, |
| 96 const base::FilePath& database, | 111 const base::FilePath& database, |
| 97 const std::string& url, | 112 const std::string& url, |
| 98 const std::map<std::string, std::string>& annotations, | 113 const std::map<std::string, std::string>& annotations, |
| 99 const std::vector<std::string>& arguments) { | 114 const std::vector<std::string>& arguments) { |
| 100 LOG(FATAL) << "SetHandler should be used on Windows"; | 115 LOG(FATAL) << "SetHandler should be used on Windows"; |
| 101 return false; | 116 return false; |
| 102 } | 117 } |
| 103 | 118 |
| 104 bool CrashpadClient::SetHandler(const std::string& ipc_port) { | 119 bool CrashpadClient::SetHandler(const std::string& ipc_port) { |
| 120 DCHECK_EQ(g_signal_exception, INVALID_HANDLE_VALUE); | |
| 121 DCHECK_EQ(g_signal_non_crash_dump, INVALID_HANDLE_VALUE); | |
| 122 DCHECK_EQ(g_non_crash_dump_done, INVALID_HANDLE_VALUE); | |
| 123 | |
| 105 ClientToServerMessage message; | 124 ClientToServerMessage message; |
| 106 memset(&message, 0, sizeof(message)); | 125 memset(&message, 0, sizeof(message)); |
| 107 message.type = ClientToServerMessage::kRegister; | 126 message.type = ClientToServerMessage::kRegister; |
| 108 message.registration.version = RegistrationRequest::kMessageVersion; | 127 message.registration.version = RegistrationRequest::kMessageVersion; |
| 109 message.registration.client_process_id = GetCurrentProcessId(); | 128 message.registration.client_process_id = GetCurrentProcessId(); |
| 110 message.registration.exception_information = | 129 message.registration.crash_exception_information = |
| 111 reinterpret_cast<WinVMAddress>(&g_exception_information); | 130 reinterpret_cast<WinVMAddress>(&g_crash_exception_information); |
| 131 message.registration.non_crash_exception_information = | |
| 132 reinterpret_cast<WinVMAddress>(&g_non_crash_exception_information); | |
| 112 | 133 |
| 113 ServerToClientMessage response = {0}; | 134 ServerToClientMessage response = {0}; |
| 114 | 135 |
| 115 if (!SendToCrashHandlerServer( | 136 if (!SendToCrashHandlerServer( |
| 116 base::UTF8ToUTF16(ipc_port), message, &response)) { | 137 base::UTF8ToUTF16(ipc_port), message, &response)) { |
| 117 return false; | 138 return false; |
| 118 } | 139 } |
| 119 | 140 |
| 120 // The server returns these already duplicated to be valid in this process. | 141 // The server returns these already duplicated to be valid in this process. |
| 121 g_signal_exception = reinterpret_cast<HANDLE>( | 142 g_signal_exception = reinterpret_cast<HANDLE>( |
| 122 static_cast<uintptr_t>(response.registration.request_report_event)); | 143 static_cast<uintptr_t>(response.registration.request_crash_dump_event)); |
| 144 g_signal_non_crash_dump = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | |
| 145 response.registration.request_non_crash_dump_event)); | |
| 146 g_non_crash_dump_done = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | |
| 147 response.registration.non_crash_dump_completed_event)); | |
| 148 | |
| 149 g_non_crash_dump_lock = new base::Lock(); | |
| 150 | |
| 123 return true; | 151 return true; |
| 124 } | 152 } |
| 125 | 153 |
| 126 bool CrashpadClient::UseHandler() { | 154 bool CrashpadClient::UseHandler() { |
| 127 if (g_signal_exception == INVALID_HANDLE_VALUE) | 155 if (g_signal_exception == INVALID_HANDLE_VALUE || |
| 156 g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | |
| 157 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | |
| 128 return false; | 158 return false; |
| 159 } | |
| 160 | |
| 129 // In theory we could store the previous handler but it is not clear what | 161 // In theory we could store the previous handler but it is not clear what |
| 130 // use we have for it. | 162 // use we have for it. |
| 131 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); | 163 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); |
| 132 return true; | 164 return true; |
| 133 } | 165 } |
| 134 | 166 |
| 167 // static | |
| 168 void CrashpadClient::DumpWithoutCrash(const CONTEXT& context) { | |
| 169 if (g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | |
| 170 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | |
| 171 LOG(ERROR) << "haven't called SetHandler()"; | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 // In the non-crashing case, we aren't concerned about avoiding calls into | |
| 176 // Win32 APIs, so just use regular locking here in case of multiple threads | |
| 177 // calling this function. If a crash occurs while we're in here, the worst | |
| 178 // that can happen is that the server captures a partial dump for this path | |
| 179 // because on the other thread gathering a crash dump, it TerminateProcess()d, | |
| 180 // causing this one to abort. | |
| 181 base::AutoLock lock(*g_non_crash_dump_lock); | |
| 182 | |
| 183 // Create a fake EXCEPTION_POINTERS to give the handler something to work | |
| 184 // with. | |
| 185 EXCEPTION_POINTERS exception_pointers = {0}; | |
| 186 | |
| 187 // This is logically const, but EXCEPTION_POINTERS does not declare it as | |
| 188 // const, so we have to cast that away from the argument. | |
| 189 exception_pointers.ContextRecord = const_cast<CONTEXT*>(&context); | |
| 190 | |
| 191 // We include a fake exception and use a code of '0x517a7ed' (something like | |
| 192 // "simulated") so that it's relatively obvious in windbg that it's not | |
| 193 // actually an exception. Most values in | |
| 194 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363082.aspx have | |
| 195 // some of the top nibble set, so we make sure to pick a value that doesn't, | |
| 196 // so as to be unlikely to conflict. | |
| 197 const uint32_t kSimulatedExceptionCode = 0x517a7ed; | |
| 198 EXCEPTION_RECORD record = {0}; | |
| 199 record.ExceptionCode = kSimulatedExceptionCode; | |
| 200 #if defined(ARCH_CPU_64_BITS) | |
| 201 record.ExceptionAddress = reinterpret_cast<void*>(context.Rip); | |
| 202 #else | |
| 203 record.ExceptionAddress = reinterpret_cast<void*>(context.Eip); | |
| 204 #endif // ARCH_CPU_64_BITS | |
| 205 | |
| 206 exception_pointers.ExceptionRecord = &record; | |
| 207 | |
| 208 g_non_crash_exception_information.thread_id = GetCurrentThreadId(); | |
| 209 g_non_crash_exception_information.exception_pointers = | |
| 210 reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers); | |
| 211 | |
| 212 bool set_event_result = SetEvent(g_signal_non_crash_dump); | |
| 213 PLOG_IF(ERROR, !set_event_result) << "SetEvent"; | |
| 214 | |
| 215 DWORD wfso_result = WaitForSingleObject(g_non_crash_dump_done, INFINITE); | |
| 216 PLOG_IF(ERROR, wfso_result != WAIT_OBJECT_0) << "WaitForSingleObject"; | |
| 217 } | |
| 218 | |
| 135 } // namespace crashpad | 219 } // namespace crashpad |
| OLD | NEW |