mirror of
https://github.com/chromium/crashpad.git
synced 2025-03-10 14:46:07 +00:00
This replaces the registration server, and adds dispatch to a delegate on crash requests. (As you are already aware) we went around in circles on trying to come up with a slightly-too-fancy threading design. All of them seemed to have problems when it comes to out of order events, and orderly shutdown, so I've gone back to something not-too-fancy. Two named pipe instances (that clients connect to) are created. These are used only for registration (which should take <1ms), so 2 should be sufficient to avoid any waits. When a client registers, we duplicate an event to it, which is used to signal when it wants a dump taken. The server registers threadpool waits on that event, and also on the process handle (which will be signalled when the client process exits). These requests (in particular the taking of the dump) are serviced on the threadpool, which avoids us needing to manage those threads, but still allows parallelism in taking dumps. On process termination, we use an IO Completion Port to post a message back to the main thread to request cleanup. This complexity is necessary so that we can unregister the threadpool waits without being on the threadpool, which we need to do synchronously so that we can be sure that no further callbacks will execute (and expect to have the client data around still). In a followup, I will readd support for DumpWithoutCrashing -- I don't think it will be too difficult now that we have an orderly way to clean up client records in the server. R=cpu@chromium.org, mark@chromium.org, jschuh@chromium.org BUG=crashpad:1,crashpad:45 Review URL: https://codereview.chromium.org/1301853002 .
135 lines
4.8 KiB
C++
135 lines
4.8 KiB
C++
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "client/crashpad_client.h"
|
|
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
|
|
#include "base/atomicops.h"
|
|
#include "base/logging.h"
|
|
#include "base/strings/string16.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "util/file/file_io.h"
|
|
#include "util/win/registration_protocol_win.h"
|
|
#include "util/win/scoped_handle.h"
|
|
|
|
namespace {
|
|
|
|
// This handle is never closed.
|
|
HANDLE g_signal_exception = 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;
|
|
|
|
// 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
|
|
// anything that might cause allocations, call into kernel mode, etc. So, we
|
|
// don't want to take a critical section here to avoid simultaneous access to
|
|
// the global exception pointers in ExceptionInformation. Because the crash
|
|
// handler will record all threads, it's fine to simply have the second and
|
|
// subsequent entrants block here. They will soon be suspended by the crash
|
|
// handler, and then the entire process will be terminated below. This means
|
|
// 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) {
|
|
SleepEx(INFINITE, false);
|
|
}
|
|
|
|
// Otherwise, we're the first thread, so record the exception pointer and
|
|
// signal the crash handler.
|
|
g_exception_information.thread_id = GetCurrentThreadId();
|
|
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.
|
|
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";
|
|
|
|
const UINT kCrashExitCodeNoDump = 0xffff7001;
|
|
TerminateProcess(GetCurrentProcess(), kCrashExitCodeNoDump);
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace crashpad {
|
|
|
|
CrashpadClient::CrashpadClient() {
|
|
}
|
|
|
|
CrashpadClient::~CrashpadClient() {
|
|
}
|
|
|
|
bool CrashpadClient::StartHandler(
|
|
const base::FilePath& handler,
|
|
const base::FilePath& database,
|
|
const std::string& url,
|
|
const std::map<std::string, std::string>& annotations,
|
|
const std::vector<std::string>& arguments) {
|
|
LOG(FATAL) << "SetHandler should be used on Windows";
|
|
return false;
|
|
}
|
|
|
|
bool CrashpadClient::SetHandler(const std::string& ipc_port) {
|
|
ClientToServerMessage message;
|
|
memset(&message, 0, sizeof(message));
|
|
message.type = ClientToServerMessage::kRegister;
|
|
message.registration.client_process_id = GetCurrentProcessId();
|
|
message.registration.exception_information =
|
|
reinterpret_cast<WinVMAddress>(&g_exception_information);
|
|
|
|
ServerToClientMessage response = {0};
|
|
|
|
if (!SendToCrashHandlerServer(
|
|
base::UTF8ToUTF16(ipc_port), message, &response)) {
|
|
return false;
|
|
}
|
|
|
|
// The server returns these already duplicated to be valid in this process.
|
|
g_signal_exception =
|
|
reinterpret_cast<HANDLE>(response.registration.request_report_event);
|
|
return true;
|
|
}
|
|
|
|
bool CrashpadClient::UseHandler() {
|
|
if (g_signal_exception == 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;
|
|
}
|
|
|
|
} // namespace crashpad
|