2010-02-09 18:08:56 +01:00
|
|
|
// Copyright (c) 2010 Google Inc.
|
2007-03-12 02:53:18 +01:00
|
|
|
// All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
|
// met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
|
// distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// The ExceptionHandler object installs signal handlers for a number of
|
|
|
|
|
// signals. We rely on the signal handler running on the thread which crashed
|
|
|
|
|
// in order to identify it. This is true of the synchronous signals (SEGV etc),
|
|
|
|
|
// but not true of ABRT. Thus, if you send ABRT to yourself in a program which
|
|
|
|
|
// uses ExceptionHandler, you need to use tgkill to direct it to the current
|
|
|
|
|
// thread.
|
|
|
|
|
//
|
|
|
|
|
// The signal flow looks like this:
|
|
|
|
|
//
|
|
|
|
|
// SignalHandler (uses a global stack of ExceptionHandler objects to find
|
|
|
|
|
// | one to handle the signal. If the first rejects it, try
|
|
|
|
|
// | the second etc...)
|
|
|
|
|
// V
|
|
|
|
|
// HandleSignal ----------------------------| (clones a new process which
|
|
|
|
|
// | | shares an address space with
|
|
|
|
|
// (wait for cloned | the crashed process. This
|
|
|
|
|
// process) | allows us to ptrace the crashed
|
|
|
|
|
// | | process)
|
|
|
|
|
// V V
|
|
|
|
|
// (set signal handler to ThreadEntry (static function to bounce
|
|
|
|
|
// SIG_DFL and rethrow, | back into the object)
|
|
|
|
|
// killing the crashed |
|
|
|
|
|
// process) V
|
|
|
|
|
// DoDump (writes minidump)
|
|
|
|
|
// |
|
|
|
|
|
// V
|
|
|
|
|
// sys_exit
|
|
|
|
|
//
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// This code is a little fragmented. Different functions of the ExceptionHandler
|
|
|
|
|
// class run in a number of different contexts. Some of them run in a normal
|
|
|
|
|
// context and are easy to code, others run in a compromised context and the
|
|
|
|
|
// restrictions at the top of minidump_writer.cc apply: no libc and use the
|
|
|
|
|
// alternative malloc. Each function should have comment above it detailing the
|
|
|
|
|
// context which it runs in.
|
2007-03-12 02:53:18 +01:00
|
|
|
|
|
|
|
|
#include "client/linux/handler/exception_handler.h"
|
2009-08-18 01:12:53 +02:00
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <linux/limits.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/mman.h>
|
2010-08-14 03:41:39 +02:00
|
|
|
#include <sys/prctl.h>
|
2009-08-18 01:12:53 +02:00
|
|
|
#include <sys/syscall.h>
|
2012-01-24 01:46:54 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/signal.h>
|
2009-08-18 01:12:53 +02:00
|
|
|
#include <sys/ucontext.h>
|
|
|
|
|
#include <sys/user.h>
|
2009-12-16 21:10:57 +01:00
|
|
|
#include <ucontext.h>
|
2009-08-18 01:12:53 +02:00
|
|
|
|
2010-03-02 01:39:48 +01:00
|
|
|
#include <algorithm>
|
2010-12-13 23:10:23 +01:00
|
|
|
#include <utility>
|
2010-03-02 01:39:48 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
#include "common/linux/linux_libc_support.h"
|
2010-09-23 16:55:50 +02:00
|
|
|
#include "common/memory.h"
|
2012-04-03 18:38:53 +02:00
|
|
|
#include "client/linux/log/log.h"
|
2010-12-13 23:10:23 +01:00
|
|
|
#include "client/linux/minidump_writer/linux_dumper.h"
|
2009-12-16 21:10:57 +01:00
|
|
|
#include "client/linux/minidump_writer/minidump_writer.h"
|
2010-08-27 15:18:49 +02:00
|
|
|
#include "common/linux/eintr_wrapper.h"
|
2010-09-16 00:31:57 +02:00
|
|
|
#include "third_party/lss/linux_syscall_support.h"
|
2010-08-27 15:18:49 +02:00
|
|
|
|
2011-05-31 14:12:30 +02:00
|
|
|
#include "linux/sched.h"
|
|
|
|
|
|
2010-08-27 15:18:49 +02:00
|
|
|
#ifndef PR_SET_PTRACER
|
|
|
|
|
#define PR_SET_PTRACER 0x59616d61
|
|
|
|
|
#endif
|
2009-08-18 01:12:53 +02:00
|
|
|
|
|
|
|
|
// A wrapper for the tgkill syscall: send a signal to a specific thread.
|
|
|
|
|
static int tgkill(pid_t tgid, pid_t tid, int sig) {
|
2010-03-02 01:39:48 +01:00
|
|
|
return syscall(__NR_tgkill, tgid, tid, sig);
|
2009-09-29 23:55:19 +02:00
|
|
|
return 0;
|
2009-08-18 01:12:53 +02:00
|
|
|
}
|
2007-03-12 02:53:18 +01:00
|
|
|
|
|
|
|
|
namespace google_breakpad {
|
|
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
namespace {
|
2009-08-18 01:12:53 +02:00
|
|
|
// The list of signals which we consider to be crashes. The default action for
|
|
|
|
|
// all these signals must be Core (see man 7 signal) because we rethrow the
|
|
|
|
|
// signal after handling it and expect that it'll be fatal.
|
2012-09-05 00:38:41 +02:00
|
|
|
const int kExceptionSignals[] = {
|
|
|
|
|
SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS
|
2007-03-12 02:53:18 +01:00
|
|
|
};
|
2012-09-05 00:38:41 +02:00
|
|
|
const int kNumHandledSignals =
|
|
|
|
|
sizeof(kExceptionSignals) / sizeof(kExceptionSignals[0]);
|
2012-09-06 16:24:05 +02:00
|
|
|
struct sigaction old_handlers[kNumHandledSignals];
|
2012-09-05 00:38:41 +02:00
|
|
|
bool handlers_installed = false;
|
|
|
|
|
|
|
|
|
|
// InstallAlternateStackLocked will store the newly installed stack in new_stack
|
|
|
|
|
// and (if it exists) the previously installed stack in old_stack.
|
|
|
|
|
stack_t old_stack;
|
|
|
|
|
stack_t new_stack;
|
|
|
|
|
bool stack_installed = false;
|
|
|
|
|
|
|
|
|
|
// Create an alternative stack to run the signal handlers on. This is done since
|
|
|
|
|
// the signal might have been caused by a stack overflow.
|
|
|
|
|
// Runs before crashing: normal context.
|
|
|
|
|
void InstallAlternateStackLocked() {
|
|
|
|
|
if (stack_installed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
memset(&old_stack, 0, sizeof(old_stack));
|
|
|
|
|
memset(&new_stack, 0, sizeof(new_stack));
|
|
|
|
|
|
|
|
|
|
// SIGSTKSZ may be too small to prevent the signal handlers from overrunning
|
|
|
|
|
// the alternative stack. Ensure that the size of the alternative stack is
|
|
|
|
|
// large enough.
|
|
|
|
|
static const unsigned kSigStackSize = std::max(8192, SIGSTKSZ);
|
|
|
|
|
|
|
|
|
|
// Only set an alternative stack if there isn't already one, or if the current
|
|
|
|
|
// one is too small.
|
|
|
|
|
if (sys_sigaltstack(NULL, &old_stack) == -1 || !old_stack.ss_sp ||
|
|
|
|
|
old_stack.ss_size < kSigStackSize) {
|
|
|
|
|
new_stack.ss_sp = malloc(kSigStackSize);
|
|
|
|
|
new_stack.ss_size = kSigStackSize;
|
|
|
|
|
|
|
|
|
|
if (sys_sigaltstack(&new_stack, NULL) == -1) {
|
|
|
|
|
free(new_stack.ss_sp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
stack_installed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Runs before crashing: normal context.
|
|
|
|
|
void RestoreAlternateStackLocked() {
|
|
|
|
|
if (!stack_installed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stack_t current_stack;
|
|
|
|
|
if (sys_sigaltstack(NULL, ¤t_stack) == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Only restore the old_stack if the current alternative stack is the one
|
|
|
|
|
// installed by the call to InstallAlternateStackLocked.
|
|
|
|
|
if (current_stack.ss_sp == new_stack.ss_sp) {
|
|
|
|
|
if (old_stack.ss_sp) {
|
|
|
|
|
if (sys_sigaltstack(&old_stack, NULL) == -1)
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
stack_t disable_stack;
|
|
|
|
|
disable_stack.ss_flags = SS_DISABLE;
|
|
|
|
|
if (sys_sigaltstack(&disable_stack, NULL) == -1)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(new_stack.ss_sp);
|
|
|
|
|
stack_installed = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// We can stack multiple exception handlers. In that case, this is the global
|
|
|
|
|
// which holds the stack.
|
|
|
|
|
std::vector<ExceptionHandler*>* ExceptionHandler::handler_stack_ = NULL;
|
2007-03-12 02:53:18 +01:00
|
|
|
pthread_mutex_t ExceptionHandler::handler_stack_mutex_ =
|
2009-08-18 01:12:53 +02:00
|
|
|
PTHREAD_MUTEX_INITIALIZER;
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// Runs before crashing: normal context.
|
2012-08-10 00:59:58 +02:00
|
|
|
ExceptionHandler::ExceptionHandler(const MinidumpDescriptor& descriptor,
|
2010-02-05 19:21:31 +01:00
|
|
|
FilterCallback filter,
|
|
|
|
|
MinidumpCallback callback,
|
|
|
|
|
void* callback_context,
|
|
|
|
|
bool install_handler,
|
|
|
|
|
const int server_fd)
|
2012-08-10 00:59:58 +02:00
|
|
|
: filter_(filter),
|
|
|
|
|
callback_(callback),
|
|
|
|
|
callback_context_(callback_context),
|
|
|
|
|
minidump_descriptor_(descriptor),
|
|
|
|
|
crash_handler_(NULL) {
|
|
|
|
|
if (server_fd >= 0)
|
|
|
|
|
crash_generation_client_.reset(CrashGenerationClient::TryCreate(server_fd));
|
|
|
|
|
|
|
|
|
|
if (!IsOutOfProcess() && !minidump_descriptor_.IsFD())
|
|
|
|
|
minidump_descriptor_.UpdatePath();
|
2010-02-05 19:21:31 +01:00
|
|
|
|
|
|
|
|
pthread_mutex_lock(&handler_stack_mutex_);
|
2012-08-10 00:59:58 +02:00
|
|
|
if (!handler_stack_)
|
|
|
|
|
handler_stack_ = new std::vector<ExceptionHandler*>;
|
2012-09-05 00:38:41 +02:00
|
|
|
if (install_handler) {
|
|
|
|
|
InstallAlternateStackLocked();
|
|
|
|
|
InstallHandlersLocked();
|
|
|
|
|
}
|
2010-02-05 19:21:31 +01:00
|
|
|
handler_stack_->push_back(this);
|
|
|
|
|
pthread_mutex_unlock(&handler_stack_mutex_);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 00:59:58 +02:00
|
|
|
// Runs before crashing: normal context.
|
|
|
|
|
ExceptionHandler::~ExceptionHandler() {
|
2012-09-05 00:38:41 +02:00
|
|
|
pthread_mutex_lock(&handler_stack_mutex_);
|
|
|
|
|
std::vector<ExceptionHandler*>::iterator handler =
|
|
|
|
|
std::find(handler_stack_->begin(), handler_stack_->end(), this);
|
|
|
|
|
handler_stack_->erase(handler);
|
|
|
|
|
if (handler_stack_->empty()) {
|
|
|
|
|
RestoreAlternateStackLocked();
|
|
|
|
|
RestoreHandlersLocked();
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(&handler_stack_mutex_);
|
2012-08-10 00:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// Runs before crashing: normal context.
|
2012-09-05 00:38:41 +02:00
|
|
|
// static
|
|
|
|
|
bool ExceptionHandler::InstallHandlersLocked() {
|
|
|
|
|
if (handlers_installed)
|
|
|
|
|
return false;
|
2012-02-14 14:21:39 +01:00
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
// Fail if unable to store all the old handlers.
|
2012-11-21 02:33:08 +01:00
|
|
|
for (int i = 0; i < kNumHandledSignals; ++i) {
|
2012-09-05 00:38:41 +02:00
|
|
|
if (sigaction(kExceptionSignals[i], NULL, &old_handlers[i]) == -1)
|
2012-02-14 14:21:39 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
struct sigaction sa;
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
2009-02-26 22:31:53 +01:00
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
// Mask all exception signals when we're handling one of them.
|
2012-11-21 02:33:08 +01:00
|
|
|
for (int i = 0; i < kNumHandledSignals; ++i)
|
2009-08-18 01:12:53 +02:00
|
|
|
sigaddset(&sa.sa_mask, kExceptionSignals[i]);
|
2009-02-26 22:31:53 +01:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
sa.sa_sigaction = SignalHandler;
|
|
|
|
|
sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2012-11-21 02:33:08 +01:00
|
|
|
for (int i = 0; i < kNumHandledSignals; ++i) {
|
2012-09-05 00:38:41 +02:00
|
|
|
if (sigaction(kExceptionSignals[i], &sa, NULL) == -1) {
|
|
|
|
|
// At this point it is impractical to back out changes, and so failure to
|
|
|
|
|
// install a signal is intentionally ignored.
|
|
|
|
|
}
|
2009-08-18 01:12:53 +02:00
|
|
|
}
|
2012-09-05 00:38:41 +02:00
|
|
|
handlers_installed = true;
|
2009-09-29 23:55:19 +02:00
|
|
|
return true;
|
2009-02-26 22:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
// Runs on the crashing thread.
|
|
|
|
|
// static
|
|
|
|
|
void ExceptionHandler::RestoreHandlersLocked() {
|
|
|
|
|
if (!handlers_installed)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-21 02:33:08 +01:00
|
|
|
for (int i = 0; i < kNumHandledSignals; ++i) {
|
2012-09-05 00:38:41 +02:00
|
|
|
if (sigaction(kExceptionSignals[i], &old_handlers[i], NULL) == -1) {
|
|
|
|
|
signal(kExceptionSignals[i], SIG_DFL);
|
|
|
|
|
}
|
2007-03-12 02:53:18 +01:00
|
|
|
}
|
2012-09-05 00:38:41 +02:00
|
|
|
handlers_installed = false;
|
2007-03-12 02:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-02 01:39:48 +01:00
|
|
|
// void ExceptionHandler::set_crash_handler(HandlerCallback callback) {
|
|
|
|
|
// crash_handler_ = callback;
|
|
|
|
|
// }
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
// Runs on the crashing thread.
|
2007-03-12 02:53:18 +01:00
|
|
|
// static
|
2009-08-18 01:12:53 +02:00
|
|
|
void ExceptionHandler::SignalHandler(int sig, siginfo_t* info, void* uc) {
|
|
|
|
|
// All the exception signals are blocked at this point.
|
2007-03-12 02:53:18 +01:00
|
|
|
pthread_mutex_lock(&handler_stack_mutex_);
|
|
|
|
|
|
2012-10-09 20:03:25 +02:00
|
|
|
// Sometimes, Breakpad runs inside a process where some other buggy code
|
|
|
|
|
// saves and restores signal handlers temporarily with 'signal'
|
|
|
|
|
// instead of 'sigaction'. This loses the SA_SIGINFO flag associated
|
|
|
|
|
// with this function. As a consequence, the values of 'info' and 'uc'
|
|
|
|
|
// become totally bogus, generally inducing a crash.
|
|
|
|
|
//
|
|
|
|
|
// The following code tries to detect this case. When it does, it
|
|
|
|
|
// resets the signal handlers with sigaction + SA_SIGINFO and returns.
|
|
|
|
|
// This forces the signal to be thrown again, but this time the kernel
|
|
|
|
|
// will call the function with the right arguments.
|
|
|
|
|
struct sigaction cur_handler;
|
|
|
|
|
if (sigaction(sig, NULL, &cur_handler) == 0 &&
|
|
|
|
|
(cur_handler.sa_flags & SA_SIGINFO) == 0) {
|
|
|
|
|
// Reset signal handler with the right flags.
|
|
|
|
|
sigemptyset(&cur_handler.sa_mask);
|
|
|
|
|
sigaddset(&cur_handler.sa_mask, sig);
|
|
|
|
|
|
|
|
|
|
cur_handler.sa_sigaction = SignalHandler;
|
|
|
|
|
cur_handler.sa_flags = SA_ONSTACK | SA_SIGINFO;
|
|
|
|
|
|
|
|
|
|
if (sigaction(sig, &cur_handler, NULL) == -1) {
|
|
|
|
|
// When resetting the handler fails, try to reset the
|
|
|
|
|
// default one to avoid an infinite loop here.
|
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(&handler_stack_mutex_);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
bool handled = false;
|
|
|
|
|
for (int i = handler_stack_->size() - 1; !handled && i >= 0; --i) {
|
|
|
|
|
handled = (*handler_stack_)[i]->HandleSignal(sig, info, uc);
|
2009-08-18 01:12:53 +02:00
|
|
|
}
|
2009-02-26 22:31:53 +01:00
|
|
|
|
2012-09-05 00:38:41 +02:00
|
|
|
// Upon returning from this signal handler, sig will become unmasked and then
|
|
|
|
|
// it will be retriggered. If one of the ExceptionHandlers handled it
|
|
|
|
|
// successfully, restore the default handler. Otherwise, restore the
|
|
|
|
|
// previously installed handler. Then, when the signal is retriggered, it will
|
|
|
|
|
// be delivered to the appropriate handler.
|
|
|
|
|
if (handled) {
|
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
|
} else {
|
|
|
|
|
RestoreHandlersLocked();
|
2007-03-12 02:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&handler_stack_mutex_);
|
2009-08-18 01:12:53 +02:00
|
|
|
|
2010-08-17 04:59:01 +02:00
|
|
|
if (info->si_pid) {
|
|
|
|
|
// This signal was triggered by somebody sending us the signal with kill().
|
|
|
|
|
// In order to retrigger it, we have to queue a new signal by calling
|
|
|
|
|
// kill() ourselves.
|
|
|
|
|
if (tgkill(getpid(), syscall(__NR_gettid), sig) < 0) {
|
|
|
|
|
// If we failed to kill ourselves (e.g. because a sandbox disallows us
|
|
|
|
|
// to do so), we instead resort to terminating our process. This will
|
|
|
|
|
// result in an incorrect exit code.
|
|
|
|
|
_exit(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// This was a synchronous signal triggered by a hard fault (e.g. SIGSEGV).
|
|
|
|
|
// No need to reissue the signal. It will automatically trigger again,
|
|
|
|
|
// when we return from the signal handler.
|
|
|
|
|
}
|
2009-08-18 01:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ThreadArgument {
|
|
|
|
|
pid_t pid; // the crashing process
|
2012-08-10 00:59:58 +02:00
|
|
|
const MinidumpDescriptor* minidump_descriptor;
|
2009-08-18 01:12:53 +02:00
|
|
|
ExceptionHandler* handler;
|
|
|
|
|
const void* context; // a CrashContext structure
|
|
|
|
|
size_t context_size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This is the entry function for the cloned process. We are in a compromised
|
|
|
|
|
// context here: see the top of the file.
|
|
|
|
|
// static
|
|
|
|
|
int ExceptionHandler::ThreadEntry(void *arg) {
|
|
|
|
|
const ThreadArgument *thread_arg = reinterpret_cast<ThreadArgument*>(arg);
|
2010-08-27 15:18:49 +02:00
|
|
|
|
|
|
|
|
// Block here until the crashing process unblocks us when
|
|
|
|
|
// we're allowed to use ptrace
|
|
|
|
|
thread_arg->handler->WaitForContinueSignal();
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
return thread_arg->handler->DoDump(thread_arg->pid, thread_arg->context,
|
|
|
|
|
thread_arg->context_size) == false;
|
2007-03-12 02:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
// Runs on the crashing thread.
|
|
|
|
|
bool ExceptionHandler::HandleSignal(int sig, siginfo_t* info, void* uc) {
|
2007-03-12 02:53:18 +01:00
|
|
|
if (filter_ && !filter_(callback_context_))
|
2007-03-29 03:32:39 +02:00
|
|
|
return false;
|
2007-03-12 02:53:18 +01:00
|
|
|
|
2011-03-22 01:29:37 +01:00
|
|
|
// Allow ourselves to be dumped if the signal is trusted.
|
|
|
|
|
bool signal_trusted = info->si_code > 0;
|
|
|
|
|
bool signal_pid_trusted = info->si_code == SI_USER ||
|
|
|
|
|
info->si_code == SI_TKILL;
|
|
|
|
|
if (signal_trusted || (signal_pid_trusted && info->si_pid == getpid())) {
|
|
|
|
|
sys_prctl(PR_SET_DUMPABLE, 1);
|
|
|
|
|
}
|
2009-08-18 01:12:53 +02:00
|
|
|
CrashContext context;
|
|
|
|
|
memcpy(&context.siginfo, info, sizeof(siginfo_t));
|
|
|
|
|
memcpy(&context.context, uc, sizeof(struct ucontext));
|
2010-03-02 01:39:48 +01:00
|
|
|
#if !defined(__ARM_EABI__)
|
|
|
|
|
// FP state is not part of user ABI on ARM Linux.
|
|
|
|
|
struct ucontext *uc_ptr = (struct ucontext*)uc;
|
|
|
|
|
if (uc_ptr->uc_mcontext.fpregs) {
|
|
|
|
|
memcpy(&context.float_state,
|
|
|
|
|
uc_ptr->uc_mcontext.fpregs,
|
|
|
|
|
sizeof(context.float_state));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2010-08-14 03:41:39 +02:00
|
|
|
context.tid = syscall(__NR_gettid);
|
2010-03-02 01:39:48 +01:00
|
|
|
if (crash_handler_ != NULL) {
|
2012-09-05 00:38:41 +02:00
|
|
|
if (crash_handler_(&context, sizeof(context), callback_context_)) {
|
2010-03-02 01:39:48 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-16 21:10:57 +01:00
|
|
|
return GenerateDump(&context);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 17:41:19 +02:00
|
|
|
// This is a public interface to HandleSignal that allows the client to
|
|
|
|
|
// generate a crash dump. This function may run in a compromised context.
|
|
|
|
|
bool ExceptionHandler::SimulateSignalDelivery(int sig) {
|
2013-01-14 19:53:18 +01:00
|
|
|
siginfo_t siginfo = {};
|
|
|
|
|
// Mimic a trusted signal to allow tracing the process (see
|
|
|
|
|
// ExceptionHandler::HandleSignal().
|
|
|
|
|
siginfo.si_code = SI_USER;
|
|
|
|
|
siginfo.si_pid = getpid();
|
2012-08-10 17:41:19 +02:00
|
|
|
struct ucontext context;
|
|
|
|
|
getcontext(&context);
|
|
|
|
|
return HandleSignal(sig, &siginfo, &context);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 21:10:57 +01:00
|
|
|
// This function may run in a compromised context: see the top of the file.
|
|
|
|
|
bool ExceptionHandler::GenerateDump(CrashContext *context) {
|
2010-02-05 19:21:31 +01:00
|
|
|
if (IsOutOfProcess())
|
|
|
|
|
return crash_generation_client_->RequestDump(context, sizeof(*context));
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
static const unsigned kChildStackSize = 8000;
|
|
|
|
|
PageAllocator allocator;
|
|
|
|
|
uint8_t* stack = (uint8_t*) allocator.Alloc(kChildStackSize);
|
|
|
|
|
if (!stack)
|
|
|
|
|
return false;
|
|
|
|
|
// clone() needs the top-most address. (scrub just to be safe)
|
|
|
|
|
stack += kChildStackSize;
|
|
|
|
|
my_memset(stack - 16, 0, 16);
|
|
|
|
|
|
|
|
|
|
ThreadArgument thread_arg;
|
|
|
|
|
thread_arg.handler = this;
|
2012-08-10 00:59:58 +02:00
|
|
|
thread_arg.minidump_descriptor = &minidump_descriptor_;
|
2009-08-18 01:12:53 +02:00
|
|
|
thread_arg.pid = getpid();
|
2009-12-16 21:10:57 +01:00
|
|
|
thread_arg.context = context;
|
|
|
|
|
thread_arg.context_size = sizeof(*context);
|
2009-08-18 01:12:53 +02:00
|
|
|
|
2010-08-27 15:18:49 +02:00
|
|
|
// We need to explicitly enable ptrace of parent processes on some
|
|
|
|
|
// kernels, but we need to know the PID of the cloned process before we
|
|
|
|
|
// can do this. Create a pipe here which we can use to block the
|
|
|
|
|
// cloned process after creating it, until we have explicitly enabled ptrace
|
|
|
|
|
if(sys_pipe(fdes) == -1) {
|
|
|
|
|
// Creating the pipe failed. We'll log an error but carry on anyway,
|
|
|
|
|
// as we'll probably still get a useful crash report. All that will happen
|
|
|
|
|
// is the write() and read() calls will fail with EBADF
|
|
|
|
|
static const char no_pipe_msg[] = "ExceptionHandler::GenerateDump \
|
|
|
|
|
sys_pipe failed:";
|
2012-04-03 18:38:53 +02:00
|
|
|
logger::write(no_pipe_msg, sizeof(no_pipe_msg) - 1);
|
|
|
|
|
logger::write(strerror(errno), strlen(strerror(errno)));
|
|
|
|
|
logger::write("\n", 1);
|
2010-08-27 15:18:49 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
const pid_t child = sys_clone(
|
|
|
|
|
ThreadEntry, stack, CLONE_FILES | CLONE_FS | CLONE_UNTRACED,
|
|
|
|
|
&thread_arg, NULL, NULL, NULL);
|
2012-06-27 16:04:52 +02:00
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
int r, status;
|
2010-08-27 15:18:49 +02:00
|
|
|
// Allow the child to ptrace us
|
2012-01-24 01:46:54 +01:00
|
|
|
sys_prctl(PR_SET_PTRACER, child);
|
2010-08-27 15:18:49 +02:00
|
|
|
SendContinueSignalToChild();
|
2009-08-18 01:12:53 +02:00
|
|
|
do {
|
|
|
|
|
r = sys_waitpid(child, &status, __WALL);
|
|
|
|
|
} while (r == -1 && errno == EINTR);
|
|
|
|
|
|
2010-08-27 15:18:49 +02:00
|
|
|
sys_close(fdes[0]);
|
|
|
|
|
sys_close(fdes[1]);
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
if (r == -1) {
|
2009-12-16 21:10:57 +01:00
|
|
|
static const char msg[] = "ExceptionHandler::GenerateDump waitpid failed:";
|
2012-04-03 18:38:53 +02:00
|
|
|
logger::write(msg, sizeof(msg) - 1);
|
|
|
|
|
logger::write(strerror(errno), strlen(strerror(errno)));
|
|
|
|
|
logger::write("\n", 1);
|
2008-10-14 13:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
bool success = r != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
2008-10-14 13:21:34 +02:00
|
|
|
if (callback_)
|
2012-08-10 00:59:58 +02:00
|
|
|
success = callback_(minidump_descriptor_, callback_context_, success);
|
2008-10-14 13:21:34 +02:00
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-27 15:18:49 +02:00
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
void ExceptionHandler::SendContinueSignalToChild() {
|
|
|
|
|
static const char okToContinueMessage = 'a';
|
|
|
|
|
int r;
|
|
|
|
|
r = HANDLE_EINTR(sys_write(fdes[1], &okToContinueMessage, sizeof(char)));
|
|
|
|
|
if(r == -1) {
|
|
|
|
|
static const char msg[] = "ExceptionHandler::SendContinueSignalToChild \
|
|
|
|
|
sys_write failed:";
|
2012-04-03 18:38:53 +02:00
|
|
|
logger::write(msg, sizeof(msg) - 1);
|
|
|
|
|
logger::write(strerror(errno), strlen(strerror(errno)));
|
|
|
|
|
logger::write("\n", 1);
|
2010-08-27 15:18:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
// Runs on the cloned process.
|
|
|
|
|
void ExceptionHandler::WaitForContinueSignal() {
|
|
|
|
|
int r;
|
|
|
|
|
char receivedMessage;
|
|
|
|
|
r = HANDLE_EINTR(sys_read(fdes[0], &receivedMessage, sizeof(char)));
|
|
|
|
|
if(r == -1) {
|
|
|
|
|
static const char msg[] = "ExceptionHandler::WaitForContinueSignal \
|
|
|
|
|
sys_read failed:";
|
2012-04-03 18:38:53 +02:00
|
|
|
logger::write(msg, sizeof(msg) - 1);
|
|
|
|
|
logger::write(strerror(errno), strlen(strerror(errno)));
|
|
|
|
|
logger::write("\n", 1);
|
2010-08-27 15:18:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-18 01:12:53 +02:00
|
|
|
// This function runs in a compromised context: see the top of the file.
|
|
|
|
|
// Runs on the cloned process.
|
|
|
|
|
bool ExceptionHandler::DoDump(pid_t crashing_process, const void* context,
|
|
|
|
|
size_t context_size) {
|
2012-08-10 00:59:58 +02:00
|
|
|
if (minidump_descriptor_.IsFD()) {
|
|
|
|
|
return google_breakpad::WriteMinidump(minidump_descriptor_.fd(),
|
Add optional file size limit for minidumps
When there are upwards of 200 threads in a crashing process, each having an
8KB stack, this can result in a huge, 1.8MB minidump file. So I added a
parameter that, if set, can compel the minidump writer to dump less stack.
More specifically, if the writer expects to go over the limit (due to the
number of threads), then it will dump less of a thread's stack after the
first 20 threads.
There are two ways to specify the limit, depending on how you write minidumps:
1) If you call WriteMinidump() directly, there's now a version of the
function that takes the minidump size limit as an argument.
2) If you use the ExceptionHandler class, the MinidumpDescriptor object you
pass to it now has a set_size_limit() method you would call before
passing it to the constructor.
BUG=chromium-os:31447, chromium:154546
TEST=Wrote a size-limit unittest; Ran unittests
Review URL: https://breakpad.appspot.com/487002
git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1082 4c0a9323-5329-0410-9bdc-e9ce6186880e
2012-11-15 01:01:13 +01:00
|
|
|
minidump_descriptor_.size_limit(),
|
2012-08-10 00:59:58 +02:00
|
|
|
crashing_process,
|
|
|
|
|
context,
|
|
|
|
|
context_size,
|
2012-09-18 15:54:58 +02:00
|
|
|
mapping_list_,
|
|
|
|
|
app_memory_list_);
|
2012-08-10 00:59:58 +02:00
|
|
|
}
|
|
|
|
|
return google_breakpad::WriteMinidump(minidump_descriptor_.path(),
|
Add optional file size limit for minidumps
When there are upwards of 200 threads in a crashing process, each having an
8KB stack, this can result in a huge, 1.8MB minidump file. So I added a
parameter that, if set, can compel the minidump writer to dump less stack.
More specifically, if the writer expects to go over the limit (due to the
number of threads), then it will dump less of a thread's stack after the
first 20 threads.
There are two ways to specify the limit, depending on how you write minidumps:
1) If you call WriteMinidump() directly, there's now a version of the
function that takes the minidump size limit as an argument.
2) If you use the ExceptionHandler class, the MinidumpDescriptor object you
pass to it now has a set_size_limit() method you would call before
passing it to the constructor.
BUG=chromium-os:31447, chromium:154546
TEST=Wrote a size-limit unittest; Ran unittests
Review URL: https://breakpad.appspot.com/487002
git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1082 4c0a9323-5329-0410-9bdc-e9ce6186880e
2012-11-15 01:01:13 +01:00
|
|
|
minidump_descriptor_.size_limit(),
|
2010-12-13 23:10:23 +01:00
|
|
|
crashing_process,
|
|
|
|
|
context,
|
|
|
|
|
context_size,
|
2012-09-18 15:54:58 +02:00
|
|
|
mapping_list_,
|
|
|
|
|
app_memory_list_);
|
2007-03-12 02:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
2009-12-16 21:10:57 +01:00
|
|
|
// static
|
2012-08-10 00:59:58 +02:00
|
|
|
bool ExceptionHandler::WriteMinidump(const string& dump_path,
|
2009-12-16 21:10:57 +01:00
|
|
|
MinidumpCallback callback,
|
|
|
|
|
void* callback_context) {
|
2012-08-10 00:59:58 +02:00
|
|
|
MinidumpDescriptor descriptor(dump_path);
|
|
|
|
|
ExceptionHandler eh(descriptor, NULL, callback, callback_context, false, -1);
|
2009-12-16 21:10:57 +01:00
|
|
|
return eh.WriteMinidump();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExceptionHandler::WriteMinidump() {
|
2012-08-10 00:59:58 +02:00
|
|
|
if (!IsOutOfProcess() && !minidump_descriptor_.IsFD()) {
|
|
|
|
|
// Update the path of the minidump so that this can be called multiple times
|
|
|
|
|
// and new files are created for each minidump. This is done before the
|
|
|
|
|
// generation happens, as clients may want to access the MinidumpDescriptor
|
|
|
|
|
// after this call to find the exact path to the minidump file.
|
|
|
|
|
minidump_descriptor_.UpdatePath();
|
|
|
|
|
} else if (minidump_descriptor_.IsFD()) {
|
|
|
|
|
// Reposition the FD to its beginning and resize it to get rid of the
|
|
|
|
|
// previous minidump info.
|
|
|
|
|
lseek(minidump_descriptor_.fd(), 0, SEEK_SET);
|
2012-09-06 16:24:05 +02:00
|
|
|
static_cast<void>(ftruncate(minidump_descriptor_.fd(), 0));
|
2012-08-10 00:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-18 20:51:56 +02:00
|
|
|
// Allow this process to be dumped.
|
2009-12-16 21:10:57 +01:00
|
|
|
sys_prctl(PR_SET_DUMPABLE, 1);
|
|
|
|
|
|
|
|
|
|
CrashContext context;
|
|
|
|
|
int getcontext_result = getcontext(&context.context);
|
|
|
|
|
if (getcontext_result)
|
|
|
|
|
return false;
|
2012-08-31 20:38:29 +02:00
|
|
|
#if !defined(__ARM_EABI__)
|
|
|
|
|
// FPU state is not part of ARM EABI ucontext_t.
|
2009-12-16 21:10:57 +01:00
|
|
|
memcpy(&context.float_state, context.context.uc_mcontext.fpregs,
|
|
|
|
|
sizeof(context.float_state));
|
2012-08-31 20:38:29 +02:00
|
|
|
#endif
|
2009-12-16 21:10:57 +01:00
|
|
|
context.tid = sys_gettid();
|
|
|
|
|
|
2012-09-18 20:51:56 +02:00
|
|
|
// Add an exception stream to the minidump for better reporting.
|
|
|
|
|
memset(&context.siginfo, 0, sizeof(context.siginfo));
|
|
|
|
|
context.siginfo.si_signo = MD_EXCEPTION_CODE_LIN_DUMP_REQUESTED;
|
|
|
|
|
#if defined(__i386__)
|
|
|
|
|
context.siginfo.si_addr =
|
|
|
|
|
reinterpret_cast<void*>(context.context.uc_mcontext.gregs[REG_EIP]);
|
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
|
context.siginfo.si_addr =
|
|
|
|
|
reinterpret_cast<void*>(context.context.uc_mcontext.gregs[REG_RIP]);
|
|
|
|
|
#elif defined(__arm__)
|
|
|
|
|
context.siginfo.si_addr =
|
|
|
|
|
reinterpret_cast<void*>(context.context.uc_mcontext.arm_pc);
|
|
|
|
|
#else
|
|
|
|
|
#error "This code has not been ported to your platform yet."
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-08-10 00:59:58 +02:00
|
|
|
return GenerateDump(&context);
|
2009-12-16 21:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
2012-06-29 00:46:01 +02:00
|
|
|
void ExceptionHandler::AddMappingInfo(const string& name,
|
2010-12-13 23:10:23 +01:00
|
|
|
const u_int8_t identifier[sizeof(MDGUID)],
|
|
|
|
|
uintptr_t start_address,
|
|
|
|
|
size_t mapping_size,
|
|
|
|
|
size_t file_offset) {
|
|
|
|
|
MappingInfo info;
|
|
|
|
|
info.start_addr = start_address;
|
|
|
|
|
info.size = mapping_size;
|
|
|
|
|
info.offset = file_offset;
|
2011-11-09 23:39:26 +01:00
|
|
|
strncpy(info.name, name.c_str(), sizeof(info.name) - 1);
|
|
|
|
|
info.name[sizeof(info.name) - 1] = '\0';
|
2010-12-13 23:10:23 +01:00
|
|
|
|
|
|
|
|
MappingEntry mapping;
|
|
|
|
|
mapping.first = info;
|
|
|
|
|
memcpy(mapping.second, identifier, sizeof(MDGUID));
|
|
|
|
|
mapping_list_.push_back(mapping);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 15:54:58 +02:00
|
|
|
void ExceptionHandler::RegisterAppMemory(void* ptr, size_t length) {
|
|
|
|
|
AppMemoryList::iterator iter =
|
|
|
|
|
std::find(app_memory_list_.begin(), app_memory_list_.end(), ptr);
|
|
|
|
|
if (iter != app_memory_list_.end()) {
|
|
|
|
|
// Don't allow registering the same pointer twice.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppMemory app_memory;
|
|
|
|
|
app_memory.ptr = ptr;
|
|
|
|
|
app_memory.length = length;
|
|
|
|
|
app_memory_list_.push_back(app_memory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExceptionHandler::UnregisterAppMemory(void* ptr) {
|
|
|
|
|
AppMemoryList::iterator iter =
|
|
|
|
|
std::find(app_memory_list_.begin(), app_memory_list_.end(), ptr);
|
|
|
|
|
if (iter != app_memory_list_.end()) {
|
|
|
|
|
app_memory_list_.erase(iter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 20:51:56 +02:00
|
|
|
// static
|
|
|
|
|
bool ExceptionHandler::WriteMinidumpForChild(pid_t child,
|
|
|
|
|
pid_t child_blamed_thread,
|
|
|
|
|
const string& dump_path,
|
|
|
|
|
MinidumpCallback callback,
|
|
|
|
|
void* callback_context) {
|
|
|
|
|
// This function is not run in a compromised context.
|
|
|
|
|
MinidumpDescriptor descriptor(dump_path);
|
|
|
|
|
descriptor.UpdatePath();
|
|
|
|
|
if (!google_breakpad::WriteMinidump(descriptor.path(),
|
|
|
|
|
child,
|
|
|
|
|
child_blamed_thread))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return callback ? callback(descriptor, callback_context, true) : true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-12 02:53:18 +01:00
|
|
|
} // namespace google_breakpad
|