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

Side by Side 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: fixes Created 5 years, 2 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 unified diff | Download patch
« no previous file with comments | « client/crashpad_client.h ('k') | client/simulate_crash.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
73 // DumpWithoutCrashing.
74 Sleep(kMillisecondsUntilTerminate); 87 Sleep(kMillisecondsUntilTerminate);
75 88
76 LOG(ERROR) << "crash server did not respond, self-terminating"; 89 LOG(ERROR) << "crash server did not respond, self-terminating";
77 90
78 const UINT kCrashExitCodeNoDump = 0xffff7001; 91 const UINT kCrashExitCodeNoDump = 0xffff7001;
79 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump); 92 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump);
80 93
81 return EXCEPTION_CONTINUE_SEARCH; 94 return EXCEPTION_CONTINUE_SEARCH;
82 } 95 }
83 96
(...skipping 11 matching lines...) Expand all
95 const base::FilePath& handler, 108 const base::FilePath& handler,
96 const base::FilePath& database, 109 const base::FilePath& database,
97 const std::string& url, 110 const std::string& url,
98 const std::map<std::string, std::string>& annotations, 111 const std::map<std::string, std::string>& annotations,
99 const std::vector<std::string>& arguments) { 112 const std::vector<std::string>& arguments) {
100 LOG(FATAL) << "SetHandler should be used on Windows"; 113 LOG(FATAL) << "SetHandler should be used on Windows";
101 return false; 114 return false;
102 } 115 }
103 116
104 bool CrashpadClient::SetHandler(const std::string& ipc_port) { 117 bool CrashpadClient::SetHandler(const std::string& ipc_port) {
118 DCHECK_EQ(g_signal_exception, INVALID_HANDLE_VALUE);
119 DCHECK_EQ(g_signal_non_crash_dump, INVALID_HANDLE_VALUE);
120 DCHECK_EQ(g_non_crash_dump_done, INVALID_HANDLE_VALUE);
121
105 ClientToServerMessage message; 122 ClientToServerMessage message;
106 memset(&message, 0, sizeof(message)); 123 memset(&message, 0, sizeof(message));
107 message.type = ClientToServerMessage::kRegister; 124 message.type = ClientToServerMessage::kRegister;
108 message.registration.version = RegistrationRequest::kMessageVersion; 125 message.registration.version = RegistrationRequest::kMessageVersion;
109 message.registration.client_process_id = GetCurrentProcessId(); 126 message.registration.client_process_id = GetCurrentProcessId();
110 message.registration.exception_information = 127 message.registration.crash_exception_information =
111 reinterpret_cast<WinVMAddress>(&g_exception_information); 128 reinterpret_cast<WinVMAddress>(&g_crash_exception_information);
129 message.registration.non_crash_exception_information =
130 reinterpret_cast<WinVMAddress>(&g_non_crash_exception_information);
112 131
113 ServerToClientMessage response = {0}; 132 ServerToClientMessage response = {0};
114 133
115 if (!SendToCrashHandlerServer( 134 if (!SendToCrashHandlerServer(
116 base::UTF8ToUTF16(ipc_port), message, &response)) { 135 base::UTF8ToUTF16(ipc_port), message, &response)) {
117 return false; 136 return false;
118 } 137 }
119 138
120 // The server returns these already duplicated to be valid in this process. 139 // The server returns these already duplicated to be valid in this process.
121 g_signal_exception = reinterpret_cast<HANDLE>( 140 g_signal_exception = reinterpret_cast<HANDLE>(
122 static_cast<uintptr_t>(response.registration.request_report_event)); 141 static_cast<uintptr_t>(response.registration.request_crash_dump_event));
142 g_signal_non_crash_dump = reinterpret_cast<HANDLE>(static_cast<uintptr_t>(
143 response.registration.request_non_crash_dump_event));
144 g_non_crash_dump_done = reinterpret_cast<HANDLE>(static_cast<uintptr_t>(
145 response.registration.non_crash_dump_completed_event));
146
147 g_non_crash_dump_lock = new base::Lock();
148
123 return true; 149 return true;
124 } 150 }
125 151
126 bool CrashpadClient::UseHandler() { 152 bool CrashpadClient::UseHandler() {
127 if (g_signal_exception == INVALID_HANDLE_VALUE) 153 if (g_signal_exception == INVALID_HANDLE_VALUE ||
154 g_signal_non_crash_dump == INVALID_HANDLE_VALUE ||
155 g_non_crash_dump_done == INVALID_HANDLE_VALUE) {
128 return false; 156 return false;
157 }
158
129 // In theory we could store the previous handler but it is not clear what 159 // In theory we could store the previous handler but it is not clear what
130 // use we have for it. 160 // use we have for it.
131 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); 161 SetUnhandledExceptionFilter(&UnhandledExceptionHandler);
132 return true; 162 return true;
133 } 163 }
134 164
165 // static
166 void CrashpadClient::DumpWithoutCrash(const CONTEXT& context) {
167 if (g_signal_non_crash_dump == INVALID_HANDLE_VALUE ||
168 g_non_crash_dump_done == INVALID_HANDLE_VALUE) {
169 LOG(ERROR) << "haven't called SetHandler()";
170 return;
171 }
172
173 // In the non-crashing case, we aren't concerned about avoiding calls into
174 // Win32 APIs, so just use regular locking here in case of multiple threads
175 // calling this function. If a crash occurs while we're in here, the worst
176 // that can happen is that the server captures a partial dump for this path
177 // because on the other thread gathering a crash dump, it TerminateProcess()d,
178 // causing this one to abort.
179 base::AutoLock lock(*g_non_crash_dump_lock);
180
181 // Create a fake EXCEPTION_POINTERS to give the handler something to work
182 // with.
183 EXCEPTION_POINTERS exception_pointers = {0};
184
185 // This is logically const, but EXCEPTION_POINTERS does not declare it as
186 // const, so we have to cast that away from the argument.
187 exception_pointers.ContextRecord = const_cast<CONTEXT*>(&context);
188
189 // We include a fake exception and use a code of '0x517a7ed' (something like
190 // "simulated") so that it's relatively obvious in windbg that it's not
191 // actually an exception. Most values in
192 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363082.aspx have
193 // some of the top nibble set, so we make sure to pick a value that doesn't,
194 // so as to be unlikely to conflict.
195 const uint32_t kSimulatedExceptionCode = 0x517a7ed;
196 EXCEPTION_RECORD record = {0};
197 record.ExceptionCode = kSimulatedExceptionCode;
198 #if defined(ARCH_CPU_64_BITS)
199 record.ExceptionAddress = reinterpret_cast<void*>(context.Rip);
200 #else
201 record.ExceptionAddress = reinterpret_cast<void*>(context.Eip);
202 #endif // ARCH_CPU_64_BITS
203
204 exception_pointers.ExceptionRecord = &record;
205
206 g_non_crash_exception_information.thread_id = GetCurrentThreadId();
207 g_non_crash_exception_information.exception_pointers =
208 reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers);
209
210 bool set_event_result = SetEvent(g_signal_non_crash_dump);
211 PLOG_IF(ERROR, !set_event_result) << "SetEvent";
212
213 DWORD wfso_result = WaitForSingleObject(g_non_crash_dump_done, INFINITE);
214 PLOG_IF(ERROR, wfso_result != WAIT_OBJECT_0) << "WaitForSingleObject";
215 }
216
135 } // namespace crashpad 217 } // namespace crashpad
OLDNEW
« no previous file with comments | « client/crashpad_client.h ('k') | client/simulate_crash.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698