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(). | |
45 base::Lock g_non_crash_dump_lock; | |
Mark Mentovai
2015/09/24 20:37:29
This carries a static constructor. This should be
scottmg
2015/09/24 21:16:59
Done.
| |
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 |
(...skipping 30 matching lines...) Expand all Loading... | |
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) { |
105 ClientToServerMessage message; | 120 ClientToServerMessage message; |
106 memset(&message, 0, sizeof(message)); | 121 memset(&message, 0, sizeof(message)); |
107 message.type = ClientToServerMessage::kRegister; | 122 message.type = ClientToServerMessage::kRegister; |
108 message.registration.version = RegistrationRequest::kMessageVersion; | 123 message.registration.version = RegistrationRequest::kMessageVersion; |
109 message.registration.client_process_id = GetCurrentProcessId(); | 124 message.registration.client_process_id = GetCurrentProcessId(); |
110 message.registration.exception_information = | 125 message.registration.crash_exception_information = |
111 reinterpret_cast<WinVMAddress>(&g_exception_information); | 126 reinterpret_cast<WinVMAddress>(&g_crash_exception_information); |
127 message.registration.non_crash_exception_information = | |
128 reinterpret_cast<WinVMAddress>(&g_non_crash_exception_information); | |
112 | 129 |
113 ServerToClientMessage response = {0}; | 130 ServerToClientMessage response = {0}; |
114 | 131 |
115 if (!SendToCrashHandlerServer( | 132 if (!SendToCrashHandlerServer( |
116 base::UTF8ToUTF16(ipc_port), message, &response)) { | 133 base::UTF8ToUTF16(ipc_port), message, &response)) { |
117 return false; | 134 return false; |
118 } | 135 } |
119 | 136 |
120 // The server returns these already duplicated to be valid in this process. | 137 // The server returns these already duplicated to be valid in this process. |
121 g_signal_exception = reinterpret_cast<HANDLE>( | 138 g_signal_exception = reinterpret_cast<HANDLE>( |
122 static_cast<uintptr_t>(response.registration.request_report_event)); | 139 static_cast<uintptr_t>(response.registration.request_crash_dump_event)); |
140 g_signal_non_crash_dump = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | |
141 response.registration.request_non_crash_dump_event)); | |
142 g_non_crash_dump_done = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | |
143 response.registration.non_crash_dump_completed_event)); | |
144 | |
123 return true; | 145 return true; |
124 } | 146 } |
125 | 147 |
126 bool CrashpadClient::UseHandler() { | 148 bool CrashpadClient::UseHandler() { |
127 if (g_signal_exception == INVALID_HANDLE_VALUE) | 149 if (g_signal_exception == INVALID_HANDLE_VALUE || |
150 g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | |
151 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | |
128 return false; | 152 return false; |
153 } | |
154 | |
129 // In theory we could store the previous handler but it is not clear what | 155 // In theory we could store the previous handler but it is not clear what |
130 // use we have for it. | 156 // use we have for it. |
131 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); | 157 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); |
132 return true; | 158 return true; |
133 } | 159 } |
134 | 160 |
161 // static | |
162 bool CrashpadClient::DumpWithoutCrash(const CONTEXT& context) { | |
163 if (g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | |
164 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | |
165 LOG(ERROR) << "haven't called SetHandler()"; | |
166 return false; | |
167 } | |
168 | |
169 // In the non-crashing case, we aren't concerned about avoiding calls into | |
170 // Win32 APIs, so just use regular locking here in case of multiple threads | |
171 // calling this function. If a crash occurs while we're in here, the worst | |
172 // that can happen is that the server captures a partial dump for this path | |
173 // because on the other thread gathering a crash dump, it TerminateProcess()d, | |
174 // causing this one to abort. | |
175 base::AutoLock lock(g_non_crash_dump_lock); | |
176 | |
177 // Create a fake EXCEPTION_POINTERS (with a null EXCEPTION_RECORD), so that | |
178 // the handler has a CONTEXT to start from. We don't include an | |
179 // EXCEPTION_RECORD as there hasn't actually been an exception. | |
180 EXCEPTION_POINTERS exception_pointers = {0}; | |
181 // This is logically const, but EXCEPTION_POINTERS does not declare it as | |
Mark Mentovai
2015/09/24 20:37:29
Blank line before this, so that EXCEPTION_POINTERS
scottmg
2015/09/24 21:16:59
Done.
| |
182 // const, so we have to cast that away from the argument. | |
183 exception_pointers.ContextRecord = const_cast<CONTEXT*>(&context); | |
184 | |
185 g_non_crash_exception_information.thread_id = GetCurrentThreadId(); | |
186 g_non_crash_exception_information.exception_pointers = | |
187 reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers); | |
188 | |
189 bool set_event_result = SetEvent(g_signal_non_crash_dump); | |
190 PLOG_IF(ERROR, !set_event_result) << "SetEvent"; | |
191 | |
192 DWORD wfso_result = WaitForSingleObject(g_non_crash_dump_done, INFINITE); | |
193 PLOG_IF(ERROR, wfso_result != WAIT_OBJECT_0) << "WaitForSingleObject"; | |
194 | |
195 return true; | |
196 } | |
197 | |
135 } // namespace crashpad | 198 } // namespace crashpad |
OLD | NEW |