michael@0: // Copyright (c) 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Author: Alfred Peng michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "client/solaris/handler/exception_handler.h" michael@0: #include "common/solaris/guid_creator.h" michael@0: #include "common/solaris/message_output.h" michael@0: #include "google_breakpad/common/minidump_format.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // Signals that we are interested. michael@0: static const int kSigTable[] = { michael@0: SIGSEGV, michael@0: SIGABRT, michael@0: SIGFPE, michael@0: SIGILL, michael@0: SIGBUS michael@0: }; michael@0: michael@0: std::vector *ExceptionHandler::handler_stack_ = NULL; michael@0: int ExceptionHandler::handler_stack_index_ = 0; michael@0: pthread_mutex_t ExceptionHandler::handler_stack_mutex_ = michael@0: PTHREAD_MUTEX_INITIALIZER; michael@0: michael@0: ExceptionHandler::ExceptionHandler(const string &dump_path, michael@0: FilterCallback filter, michael@0: MinidumpCallback callback, michael@0: void *callback_context, michael@0: bool install_handler) michael@0: : filter_(filter), michael@0: callback_(callback), michael@0: callback_context_(callback_context), michael@0: dump_path_(), michael@0: installed_handler_(install_handler) { michael@0: set_dump_path(dump_path); michael@0: michael@0: if (install_handler) { michael@0: SetupHandler(); michael@0: } michael@0: michael@0: if (install_handler) { michael@0: pthread_mutex_lock(&handler_stack_mutex_); michael@0: michael@0: if (handler_stack_ == NULL) michael@0: handler_stack_ = new std::vector; michael@0: handler_stack_->push_back(this); michael@0: pthread_mutex_unlock(&handler_stack_mutex_); michael@0: } michael@0: } michael@0: michael@0: ExceptionHandler::~ExceptionHandler() { michael@0: TeardownAllHandlers(); michael@0: pthread_mutex_lock(&handler_stack_mutex_); michael@0: if (handler_stack_->back() == this) { michael@0: handler_stack_->pop_back(); michael@0: } else { michael@0: print_message1(2, "warning: removing Breakpad handler out of order\n"); michael@0: for (std::vector::iterator iterator = michael@0: handler_stack_->begin(); michael@0: iterator != handler_stack_->end(); michael@0: ++iterator) { michael@0: if (*iterator == this) { michael@0: handler_stack_->erase(iterator); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (handler_stack_->empty()) { michael@0: // When destroying the last ExceptionHandler that installed a handler, michael@0: // clean up the handler stack. michael@0: delete handler_stack_; michael@0: handler_stack_ = NULL; michael@0: } michael@0: pthread_mutex_unlock(&handler_stack_mutex_); michael@0: } michael@0: michael@0: bool ExceptionHandler::WriteMinidump() { michael@0: return InternalWriteMinidump(0, 0, NULL); michael@0: } michael@0: michael@0: // static michael@0: bool ExceptionHandler::WriteMinidump(const string &dump_path, michael@0: MinidumpCallback callback, michael@0: void *callback_context) { michael@0: ExceptionHandler handler(dump_path, NULL, callback, michael@0: callback_context, false); michael@0: return handler.InternalWriteMinidump(0, 0, NULL); michael@0: } michael@0: michael@0: void ExceptionHandler::SetupHandler() { michael@0: // Signal on a different stack to avoid using the stack michael@0: // of the crashing lwp. michael@0: struct sigaltstack sig_stack; michael@0: sig_stack.ss_sp = malloc(MINSIGSTKSZ); michael@0: if (sig_stack.ss_sp == NULL) michael@0: return; michael@0: sig_stack.ss_size = MINSIGSTKSZ; michael@0: sig_stack.ss_flags = 0; michael@0: michael@0: if (sigaltstack(&sig_stack, NULL) < 0) michael@0: return; michael@0: for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i) michael@0: SetupHandler(kSigTable[i]); michael@0: } michael@0: michael@0: void ExceptionHandler::SetupHandler(int signo) { michael@0: struct sigaction act, old_act; michael@0: act.sa_handler = HandleException; michael@0: act.sa_flags = SA_ONSTACK; michael@0: if (sigaction(signo, &act, &old_act) < 0) michael@0: return; michael@0: old_handlers_[signo] = old_act.sa_handler; michael@0: } michael@0: michael@0: void ExceptionHandler::TeardownHandler(int signo) { michael@0: if (old_handlers_.find(signo) != old_handlers_.end()) { michael@0: struct sigaction act; michael@0: act.sa_handler = old_handlers_[signo]; michael@0: act.sa_flags = 0; michael@0: sigaction(signo, &act, 0); michael@0: } michael@0: } michael@0: michael@0: void ExceptionHandler::TeardownAllHandlers() { michael@0: for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i) { michael@0: TeardownHandler(kSigTable[i]); michael@0: } michael@0: } michael@0: michael@0: // static michael@0: void ExceptionHandler::HandleException(int signo) { michael@0: //void ExceptionHandler::HandleException(int signo, siginfo_t *sip, ucontext_t *sig_ctx) { michael@0: // The context information about the signal is put on the stack of michael@0: // the signal handler frame as value parameter. For some reasons, the michael@0: // prototype of the handler doesn't declare this information as parameter, we michael@0: // will do it by hand. The stack layout for a signal handler frame is here: michael@0: // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libproc/common/Pstack.c#81 michael@0: // michael@0: // However, if we are being called by another signal handler passing the michael@0: // signal up the chain, then we may not have this random extra parameter, michael@0: // so we may have to walk the stack to find it. We do the actual work michael@0: // on another thread, where it's a little safer, but we want the ebp michael@0: // from this frame to find it. michael@0: uintptr_t current_ebp = (uintptr_t)_getfp(); michael@0: michael@0: pthread_mutex_lock(&handler_stack_mutex_); michael@0: ExceptionHandler *current_handler = michael@0: handler_stack_->at(handler_stack_->size() - ++handler_stack_index_); michael@0: pthread_mutex_unlock(&handler_stack_mutex_); michael@0: michael@0: // Restore original handler. michael@0: current_handler->TeardownHandler(signo); michael@0: michael@0: ucontext_t *sig_ctx = NULL; michael@0: if (current_handler->InternalWriteMinidump(signo, current_ebp, &sig_ctx)) { michael@0: // if (current_handler->InternalWriteMinidump(signo, &sig_ctx)) { michael@0: // Fully handled this exception, safe to exit. michael@0: exit(EXIT_FAILURE); michael@0: } else { michael@0: // Exception not fully handled, will call the next handler in stack to michael@0: // process it. michael@0: typedef void (*SignalHandler)(int signo); michael@0: SignalHandler old_handler = michael@0: reinterpret_cast(current_handler->old_handlers_[signo]); michael@0: if (old_handler != NULL) michael@0: old_handler(signo); michael@0: } michael@0: michael@0: pthread_mutex_lock(&handler_stack_mutex_); michael@0: current_handler->SetupHandler(signo); michael@0: --handler_stack_index_; michael@0: // All the handlers in stack have been invoked to handle the exception, michael@0: // normally the process should be terminated and should not reach here. michael@0: // In case we got here, ask the OS to handle it to avoid endless loop, michael@0: // normally the OS will generate a core and termiate the process. This michael@0: // may be desired to debug the program. michael@0: if (handler_stack_index_ == 0) michael@0: signal(signo, SIG_DFL); michael@0: pthread_mutex_unlock(&handler_stack_mutex_); michael@0: } michael@0: michael@0: bool ExceptionHandler::InternalWriteMinidump(int signo, michael@0: uintptr_t sighandler_ebp, michael@0: ucontext_t **sig_ctx) { michael@0: if (filter_ && !filter_(callback_context_)) michael@0: return false; michael@0: michael@0: bool success = false; michael@0: GUID guid; michael@0: char guid_str[kGUIDStringLength + 1]; michael@0: if (CreateGUID(&guid) && GUIDToString(&guid, guid_str, sizeof(guid_str))) { michael@0: char minidump_path[PATH_MAX]; michael@0: snprintf(minidump_path, sizeof(minidump_path), "%s/%s.dmp", michael@0: dump_path_c_, guid_str); michael@0: michael@0: // Block all the signals we want to process when writing minidump. michael@0: // We don't want it to be interrupted. michael@0: sigset_t sig_blocked, sig_old; michael@0: bool blocked = true; michael@0: sigfillset(&sig_blocked); michael@0: for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i) michael@0: sigdelset(&sig_blocked, kSigTable[i]); michael@0: if (sigprocmask(SIG_BLOCK, &sig_blocked, &sig_old) != 0) { michael@0: blocked = false; michael@0: print_message1(2, "HandleException: failed to block signals.\n"); michael@0: } michael@0: michael@0: success = minidump_generator_.WriteMinidumpToFile( michael@0: minidump_path, signo, sighandler_ebp, sig_ctx); michael@0: michael@0: // Unblock the signals. michael@0: if (blocked) michael@0: sigprocmask(SIG_SETMASK, &sig_old, &sig_old); michael@0: michael@0: if (callback_) michael@0: success = callback_(dump_path_c_, guid_str, callback_context_, success); michael@0: } michael@0: return success; michael@0: } michael@0: michael@0: } // namespace google_breakpad