| 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/rand_util.h" |
| 22 #include "base/strings/string16.h" | 23 #include "base/strings/string16.h" |
| 24 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 24 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 25 #include "util/file/file_io.h" | 27 #include "util/file/file_io.h" |
| 28 #include "util/win/command_line.h" |
| 26 #include "util/win/critical_section_with_debug_info.h" | 29 #include "util/win/critical_section_with_debug_info.h" |
| 27 #include "util/win/registration_protocol_win.h" | 30 #include "util/win/registration_protocol_win.h" |
| 28 #include "util/win/scoped_handle.h" | 31 #include "util/win/scoped_handle.h" |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 // This handle is never closed. This is used to signal to the server that a dump | 35 // This handle is never closed. This is used to signal to the server that a dump |
| 33 // should be taken in the event of a crash. | 36 // should be taken in the event of a crash. |
| 34 HANDLE g_signal_exception = INVALID_HANDLE_VALUE; | 37 HANDLE g_signal_exception = INVALID_HANDLE_VALUE; |
| 35 | 38 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 Sleep(kMillisecondsUntilTerminate); | 98 Sleep(kMillisecondsUntilTerminate); |
| 96 | 99 |
| 97 LOG(ERROR) << "crash server did not respond, self-terminating"; | 100 LOG(ERROR) << "crash server did not respond, self-terminating"; |
| 98 | 101 |
| 99 const UINT kCrashExitCodeNoDump = 0xffff7001; | 102 const UINT kCrashExitCodeNoDump = 0xffff7001; |
| 100 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump); | 103 TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump); |
| 101 | 104 |
| 102 return EXCEPTION_CONTINUE_SEARCH; | 105 return EXCEPTION_CONTINUE_SEARCH; |
| 103 } | 106 } |
| 104 | 107 |
| 108 std::wstring FormatArgumentString(const std::string& name, |
| 109 const std::wstring& value) { |
| 110 return std::wstring(L"--") + base::UTF8ToUTF16(name) + L"=" + value; |
| 111 } |
| 112 |
| 105 } // namespace | 113 } // namespace |
| 106 | 114 |
| 107 namespace crashpad { | 115 namespace crashpad { |
| 108 | 116 |
| 109 CrashpadClient::CrashpadClient() { | 117 CrashpadClient::CrashpadClient() |
| 118 : ipc_port_() { |
| 110 } | 119 } |
| 111 | 120 |
| 112 CrashpadClient::~CrashpadClient() { | 121 CrashpadClient::~CrashpadClient() { |
| 113 } | 122 } |
| 114 | 123 |
| 115 bool CrashpadClient::StartHandler( | 124 bool CrashpadClient::StartHandler( |
| 116 const base::FilePath& handler, | 125 const base::FilePath& handler, |
| 117 const base::FilePath& database, | 126 const base::FilePath& database, |
| 118 const std::string& url, | 127 const std::string& url, |
| 119 const std::map<std::string, std::string>& annotations, | 128 const std::map<std::string, std::string>& annotations, |
| 120 const std::vector<std::string>& arguments) { | 129 const std::vector<std::string>& arguments) { |
| 121 LOG(FATAL) << "SetHandler should be used on Windows"; | 130 DCHECK(ipc_port_.empty()); |
| 122 return false; | 131 |
| 132 ipc_port_ = |
| 133 base::StringPrintf("\\\\.\\pipe\\crashpad_%d_", GetCurrentProcessId()); |
| 134 for (int index = 0; index < 16; ++index) { |
| 135 ipc_port_.append(1, static_cast<char>(base::RandInt('A', 'Z'))); |
| 136 } |
| 137 |
| 138 std::wstring command_line; |
| 139 AppendCommandLineArgument(handler.value(), &command_line); |
| 140 for (const std::string& argument : arguments) { |
| 141 AppendCommandLineArgument(base::UTF8ToUTF16(argument), &command_line); |
| 142 } |
| 143 if (!database.value().empty()) { |
| 144 AppendCommandLineArgument(FormatArgumentString("database", |
| 145 database.value()), |
| 146 &command_line); |
| 147 } |
| 148 if (!url.empty()) { |
| 149 AppendCommandLineArgument(FormatArgumentString("url", |
| 150 base::UTF8ToUTF16(url)), |
| 151 &command_line); |
| 152 } |
| 153 for (const auto& kv : annotations) { |
| 154 AppendCommandLineArgument( |
| 155 FormatArgumentString("annotation", |
| 156 base::UTF8ToUTF16(kv.first + '=' + kv.second)), |
| 157 &command_line); |
| 158 } |
| 159 AppendCommandLineArgument(FormatArgumentString("pipe-name", |
| 160 base::UTF8ToUTF16(ipc_port_)), |
| 161 &command_line); |
| 162 |
| 163 STARTUPINFO startup_info = {}; |
| 164 startup_info.cb = sizeof(startup_info); |
| 165 startup_info.dwFlags = STARTF_USESTDHANDLES; |
| 166 startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 167 startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 168 startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 169 PROCESS_INFORMATION process_info; |
| 170 BOOL rv = CreateProcess(handler.value().c_str(), |
| 171 &command_line[0], |
| 172 nullptr, |
| 173 nullptr, |
| 174 true, |
| 175 0, |
| 176 nullptr, |
| 177 nullptr, |
| 178 &startup_info, |
| 179 &process_info); |
| 180 if (!rv) { |
| 181 PLOG(ERROR) << "CreateProcess"; |
| 182 return false; |
| 183 } |
| 184 |
| 185 rv = CloseHandle(process_info.hThread); |
| 186 PLOG_IF(WARNING, !rv) << "CloseHandle thread"; |
| 187 |
| 188 rv = CloseHandle(process_info.hProcess); |
| 189 PLOG_IF(WARNING, !rv) << "CloseHandle process"; |
| 190 |
| 191 return true; |
| 123 } | 192 } |
| 124 | 193 |
| 125 bool CrashpadClient::SetHandler(const std::string& ipc_port) { | 194 bool CrashpadClient::SetHandler(const std::string& ipc_port) { |
| 195 DCHECK(ipc_port_.empty()); |
| 196 DCHECK(!ipc_port.empty()); |
| 197 |
| 198 ipc_port_ = ipc_port; |
| 199 |
| 200 return true; |
| 201 } |
| 202 |
| 203 bool CrashpadClient::UseHandler() { |
| 204 DCHECK(!ipc_port_.empty()); |
| 126 DCHECK_EQ(g_signal_exception, INVALID_HANDLE_VALUE); | 205 DCHECK_EQ(g_signal_exception, INVALID_HANDLE_VALUE); |
| 127 DCHECK_EQ(g_signal_non_crash_dump, INVALID_HANDLE_VALUE); | 206 DCHECK_EQ(g_signal_non_crash_dump, INVALID_HANDLE_VALUE); |
| 128 DCHECK_EQ(g_non_crash_dump_done, INVALID_HANDLE_VALUE); | 207 DCHECK_EQ(g_non_crash_dump_done, INVALID_HANDLE_VALUE); |
| 129 DCHECK(!g_critical_section_with_debug_info.DebugInfo); | 208 DCHECK(!g_critical_section_with_debug_info.DebugInfo); |
| 130 | 209 |
| 131 ClientToServerMessage message; | 210 ClientToServerMessage message; |
| 132 memset(&message, 0, sizeof(message)); | 211 memset(&message, 0, sizeof(message)); |
| 133 message.type = ClientToServerMessage::kRegister; | 212 message.type = ClientToServerMessage::kRegister; |
| 134 message.registration.version = RegistrationRequest::kMessageVersion; | 213 message.registration.version = RegistrationRequest::kMessageVersion; |
| 135 message.registration.client_process_id = GetCurrentProcessId(); | 214 message.registration.client_process_id = GetCurrentProcessId(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 147 // machine, we don't have a way of getting that pointer. | 226 // machine, we don't have a way of getting that pointer. |
| 148 if (InitializeCriticalSectionWithDebugInfoIfPossible( | 227 if (InitializeCriticalSectionWithDebugInfoIfPossible( |
| 149 &g_critical_section_with_debug_info)) { | 228 &g_critical_section_with_debug_info)) { |
| 150 message.registration.critical_section_address = | 229 message.registration.critical_section_address = |
| 151 reinterpret_cast<WinVMAddress>(&g_critical_section_with_debug_info); | 230 reinterpret_cast<WinVMAddress>(&g_critical_section_with_debug_info); |
| 152 } | 231 } |
| 153 | 232 |
| 154 ServerToClientMessage response = {0}; | 233 ServerToClientMessage response = {0}; |
| 155 | 234 |
| 156 if (!SendToCrashHandlerServer( | 235 if (!SendToCrashHandlerServer( |
| 157 base::UTF8ToUTF16(ipc_port), message, &response)) { | 236 base::UTF8ToUTF16(ipc_port_), message, &response)) { |
| 158 return false; | 237 return false; |
| 159 } | 238 } |
| 160 | 239 |
| 161 // The server returns these already duplicated to be valid in this process. | 240 // The server returns these already duplicated to be valid in this process. |
| 162 g_signal_exception = reinterpret_cast<HANDLE>( | 241 g_signal_exception = reinterpret_cast<HANDLE>( |
| 163 static_cast<uintptr_t>(response.registration.request_crash_dump_event)); | 242 static_cast<uintptr_t>(response.registration.request_crash_dump_event)); |
| 164 g_signal_non_crash_dump = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | 243 g_signal_non_crash_dump = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( |
| 165 response.registration.request_non_crash_dump_event)); | 244 response.registration.request_non_crash_dump_event)); |
| 166 g_non_crash_dump_done = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( | 245 g_non_crash_dump_done = reinterpret_cast<HANDLE>(static_cast<uintptr_t>( |
| 167 response.registration.non_crash_dump_completed_event)); | 246 response.registration.non_crash_dump_completed_event)); |
| 168 | 247 |
| 169 g_non_crash_dump_lock = new base::Lock(); | 248 g_non_crash_dump_lock = new base::Lock(); |
| 170 | 249 |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 bool CrashpadClient::UseHandler() { | |
| 175 if (g_signal_exception == INVALID_HANDLE_VALUE || | |
| 176 g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | |
| 177 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | |
| 178 return false; | |
| 179 } | |
| 180 | |
| 181 // In theory we could store the previous handler but it is not clear what | 250 // In theory we could store the previous handler but it is not clear what |
| 182 // use we have for it. | 251 // use we have for it. |
| 183 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); | 252 SetUnhandledExceptionFilter(&UnhandledExceptionHandler); |
| 184 return true; | 253 return true; |
| 185 } | 254 } |
| 186 | 255 |
| 187 // static | 256 // static |
| 188 void CrashpadClient::DumpWithoutCrash(const CONTEXT& context) { | 257 void CrashpadClient::DumpWithoutCrash(const CONTEXT& context) { |
| 189 if (g_signal_non_crash_dump == INVALID_HANDLE_VALUE || | 258 if (g_signal_non_crash_dump == INVALID_HANDLE_VALUE || |
| 190 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { | 259 g_non_crash_dump_done == INVALID_HANDLE_VALUE) { |
| 191 LOG(ERROR) << "haven't called SetHandler()"; | 260 LOG(ERROR) << "haven't called UseHandler()"; |
| 192 return; | 261 return; |
| 193 } | 262 } |
| 194 | 263 |
| 195 // In the non-crashing case, we aren't concerned about avoiding calls into | 264 // In the non-crashing case, we aren't concerned about avoiding calls into |
| 196 // Win32 APIs, so just use regular locking here in case of multiple threads | 265 // Win32 APIs, so just use regular locking here in case of multiple threads |
| 197 // calling this function. If a crash occurs while we're in here, the worst | 266 // calling this function. If a crash occurs while we're in here, the worst |
| 198 // that can happen is that the server captures a partial dump for this path | 267 // that can happen is that the server captures a partial dump for this path |
| 199 // because on the other thread gathering a crash dump, it TerminateProcess()d, | 268 // because on the other thread gathering a crash dump, it TerminateProcess()d, |
| 200 // causing this one to abort. | 269 // causing this one to abort. |
| 201 base::AutoLock lock(*g_non_crash_dump_lock); | 270 base::AutoLock lock(*g_non_crash_dump_lock); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 230 reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers); | 299 reinterpret_cast<crashpad::WinVMAddress>(&exception_pointers); |
| 231 | 300 |
| 232 bool set_event_result = !!SetEvent(g_signal_non_crash_dump); | 301 bool set_event_result = !!SetEvent(g_signal_non_crash_dump); |
| 233 PLOG_IF(ERROR, !set_event_result) << "SetEvent"; | 302 PLOG_IF(ERROR, !set_event_result) << "SetEvent"; |
| 234 | 303 |
| 235 DWORD wfso_result = WaitForSingleObject(g_non_crash_dump_done, INFINITE); | 304 DWORD wfso_result = WaitForSingleObject(g_non_crash_dump_done, INFINITE); |
| 236 PLOG_IF(ERROR, wfso_result != WAIT_OBJECT_0) << "WaitForSingleObject"; | 305 PLOG_IF(ERROR, wfso_result != WAIT_OBJECT_0) << "WaitForSingleObject"; |
| 237 } | 306 } |
| 238 | 307 |
| 239 } // namespace crashpad | 308 } // namespace crashpad |
| OLD | NEW |