linux: Add CrashpadClient::SetFirstChanceExceptionHandler()

Bug: crashpad:30
Change-Id: Idde7fd5c8ddec7c807c7720cd5b4958bf7f13fe8
Reviewed-on: https://chromium-review.googlesource.com/933363
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Joshua Peraza 2018-02-23 11:32:06 -08:00
parent 2b05eb522f
commit fa2a03fbdd
3 changed files with 87 additions and 18 deletions

View File

@ -31,6 +31,9 @@
#elif defined(OS_WIN)
#include <windows.h>
#include "util/win/scoped_handle.h"
#elif defined(OS_LINUX) || defined(OS_ANDROID)
#include <signal.h>
#include <ucontext.h>
#endif
namespace crashpad {
@ -173,6 +176,8 @@ class CrashpadClient {
//! \brief Requests that the handler capture a dump even though there hasn't
//! been a crash.
//!
//! A handler must have already been installed before calling this method.
//!
//! TODO(jperaza): Floating point information in the context is zeroed out
//! until CaptureContext() supports collecting that information.
//!
@ -180,6 +185,27 @@ class CrashpadClient {
//! CaptureContext() or similar.
static void DumpWithoutCrash(NativeCPUContext* context);
//! \brief The type for custom handlers installed by clients.
using FirstChanceHandler = bool (*)(int, siginfo_t*, ucontext_t*);
//! \brief Installs a custom crash signal handler which runs before the
//! currently installed Crashpad handler.
//!
//! Handling signals appropriately can be tricky and use of this method
//! should be avoided, if possible.
//!
//! A handler must have already been installed before calling this method.
//!
//! The custom handler runs in a signal handler context and must be safe for
//! that purpose.
//!
//! If the custom handler returns `true`, the signal is considered handled and
//! the signal handler returns. Otherwise, the currently installed Crashpad
//! signal handler is run.
//!
//! \param[in] handler The custom crash signal handler to install.
static void SetFirstChanceExceptionHandler(FirstChanceHandler handler);
#endif // OS_LINUX || OS_ANDROID || DOXYGEN
#if defined(OS_MACOSX) || DOXYGEN

View File

@ -98,13 +98,19 @@ class SignalHandler {
virtual void HandleCrashFatal(int signo,
siginfo_t* siginfo,
void* context) = 0;
virtual void HandleCrashNonFatal(int signo,
virtual bool HandleCrashNonFatal(int signo,
siginfo_t* siginfo,
void* context) = 0;
void SetFirstChanceHandler(CrashpadClient::FirstChanceHandler handler) {
first_chance_handler_ = handler;
}
protected:
SignalHandler() = default;
~SignalHandler() = default;
CrashpadClient::FirstChanceHandler first_chance_handler_ = nullptr;
};
// Launches a single use handler to snapshot this process.
@ -125,9 +131,15 @@ class LaunchAtCrashHandler : public SignalHandler {
return Signals::InstallCrashHandlers(HandleCrash, 0, nullptr);
}
void HandleCrashNonFatal(int signo,
bool HandleCrashNonFatal(int signo,
siginfo_t* siginfo,
void* context) override {
if (first_chance_handler_ &&
first_chance_handler_(
signo, siginfo, static_cast<ucontext_t*>(context))) {
return true;
}
exception_information_.siginfo_address =
FromPointerCast<decltype(exception_information_.siginfo_address)>(
siginfo);
@ -140,7 +152,7 @@ class LaunchAtCrashHandler : public SignalHandler {
pid_t pid = fork();
if (pid < 0) {
return;
return false;
}
if (pid == 0) {
execv(argv_[0], const_cast<char* const*>(argv_.data()));
@ -149,10 +161,13 @@ class LaunchAtCrashHandler : public SignalHandler {
int status;
waitpid(pid, &status, 0);
return false;
}
void HandleCrashFatal(int signo, siginfo_t* siginfo, void* context) override {
HandleCrashNonFatal(signo, siginfo, context);
if (HandleCrashNonFatal(signo, siginfo, context)) {
return;
}
Signals::RestoreHandlerAndReraiseSignalOnReturn(siginfo, nullptr);
}
@ -239,10 +254,7 @@ bool CrashpadClient::StartHandlerForClient(
// static
void CrashpadClient::DumpWithoutCrash(NativeCPUContext* context) {
if (!g_crash_handler) {
LOG(WARNING) << "No crash handler installed";
return;
}
DCHECK(g_crash_handler);
#if defined(ARCH_CPU_X86)
memset(&context->__fpregs_mem, 0, sizeof(context->__fpregs_mem));
@ -267,4 +279,11 @@ void CrashpadClient::DumpWithoutCrash(NativeCPUContext* context) {
siginfo.si_signo, &siginfo, reinterpret_cast<void*>(context));
}
// static
void CrashpadClient::SetFirstChanceExceptionHandler(
FirstChanceHandler handler) {
DCHECK(g_crash_handler);
g_crash_handler->SetFirstChanceHandler(handler);
}
} // namespace crashpad

View File

@ -39,6 +39,10 @@ namespace crashpad {
namespace test {
namespace {
bool HandleCrashSuccessfully(int, siginfo_t*, ucontext_t*) {
return true;
}
TEST(CrashpadClient, SimulateCrash) {
ScopedTempDir temp_dir;
@ -53,21 +57,41 @@ TEST(CrashpadClient, SimulateCrash) {
std::map<std::string, std::string>(),
std::vector<std::string>()));
CRASHPAD_SIMULATE_CRASH();
auto database =
CrashReportDatabase::InitializeWithoutCreating(temp_dir.path());
ASSERT_TRUE(database);
std::vector<CrashReportDatabase::Report> reports;
ASSERT_EQ(database->GetPendingReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 0u);
{
CrashpadClient::SetFirstChanceExceptionHandler(HandleCrashSuccessfully);
reports.clear();
ASSERT_EQ(database->GetCompletedReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 1u);
CRASHPAD_SIMULATE_CRASH();
std::vector<CrashReportDatabase::Report> reports;
ASSERT_EQ(database->GetPendingReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 0u);
reports.clear();
ASSERT_EQ(database->GetCompletedReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 0u);
}
{
CrashpadClient::SetFirstChanceExceptionHandler(nullptr);
CRASHPAD_SIMULATE_CRASH();
std::vector<CrashReportDatabase::Report> reports;
ASSERT_EQ(database->GetPendingReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 0u);
reports.clear();
ASSERT_EQ(database->GetCompletedReports(&reports),
CrashReportDatabase::kNoError);
EXPECT_EQ(reports.size(), 1u);
}
}
CRASHPAD_CHILD_TEST_MAIN(StartHandlerAtCrashChild) {