toolkit/crashreporter/google-breakpad/src/client/solaris/handler/exception_handler.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2007, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // Author: Alfred Peng
michael@0 31
michael@0 32 #include <signal.h>
michael@0 33 #include <sys/stat.h>
michael@0 34 #include <sys/types.h>
michael@0 35 #include <unistd.h>
michael@0 36
michael@0 37 #include <cassert>
michael@0 38 #include <cstdlib>
michael@0 39 #include <ctime>
michael@0 40
michael@0 41 #include "client/solaris/handler/exception_handler.h"
michael@0 42 #include "common/solaris/guid_creator.h"
michael@0 43 #include "common/solaris/message_output.h"
michael@0 44 #include "google_breakpad/common/minidump_format.h"
michael@0 45
michael@0 46 namespace google_breakpad {
michael@0 47
michael@0 48 // Signals that we are interested.
michael@0 49 static const int kSigTable[] = {
michael@0 50 SIGSEGV,
michael@0 51 SIGABRT,
michael@0 52 SIGFPE,
michael@0 53 SIGILL,
michael@0 54 SIGBUS
michael@0 55 };
michael@0 56
michael@0 57 std::vector<ExceptionHandler*> *ExceptionHandler::handler_stack_ = NULL;
michael@0 58 int ExceptionHandler::handler_stack_index_ = 0;
michael@0 59 pthread_mutex_t ExceptionHandler::handler_stack_mutex_ =
michael@0 60 PTHREAD_MUTEX_INITIALIZER;
michael@0 61
michael@0 62 ExceptionHandler::ExceptionHandler(const string &dump_path,
michael@0 63 FilterCallback filter,
michael@0 64 MinidumpCallback callback,
michael@0 65 void *callback_context,
michael@0 66 bool install_handler)
michael@0 67 : filter_(filter),
michael@0 68 callback_(callback),
michael@0 69 callback_context_(callback_context),
michael@0 70 dump_path_(),
michael@0 71 installed_handler_(install_handler) {
michael@0 72 set_dump_path(dump_path);
michael@0 73
michael@0 74 if (install_handler) {
michael@0 75 SetupHandler();
michael@0 76 }
michael@0 77
michael@0 78 if (install_handler) {
michael@0 79 pthread_mutex_lock(&handler_stack_mutex_);
michael@0 80
michael@0 81 if (handler_stack_ == NULL)
michael@0 82 handler_stack_ = new std::vector<ExceptionHandler *>;
michael@0 83 handler_stack_->push_back(this);
michael@0 84 pthread_mutex_unlock(&handler_stack_mutex_);
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 ExceptionHandler::~ExceptionHandler() {
michael@0 89 TeardownAllHandlers();
michael@0 90 pthread_mutex_lock(&handler_stack_mutex_);
michael@0 91 if (handler_stack_->back() == this) {
michael@0 92 handler_stack_->pop_back();
michael@0 93 } else {
michael@0 94 print_message1(2, "warning: removing Breakpad handler out of order\n");
michael@0 95 for (std::vector<ExceptionHandler *>::iterator iterator =
michael@0 96 handler_stack_->begin();
michael@0 97 iterator != handler_stack_->end();
michael@0 98 ++iterator) {
michael@0 99 if (*iterator == this) {
michael@0 100 handler_stack_->erase(iterator);
michael@0 101 }
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 if (handler_stack_->empty()) {
michael@0 106 // When destroying the last ExceptionHandler that installed a handler,
michael@0 107 // clean up the handler stack.
michael@0 108 delete handler_stack_;
michael@0 109 handler_stack_ = NULL;
michael@0 110 }
michael@0 111 pthread_mutex_unlock(&handler_stack_mutex_);
michael@0 112 }
michael@0 113
michael@0 114 bool ExceptionHandler::WriteMinidump() {
michael@0 115 return InternalWriteMinidump(0, 0, NULL);
michael@0 116 }
michael@0 117
michael@0 118 // static
michael@0 119 bool ExceptionHandler::WriteMinidump(const string &dump_path,
michael@0 120 MinidumpCallback callback,
michael@0 121 void *callback_context) {
michael@0 122 ExceptionHandler handler(dump_path, NULL, callback,
michael@0 123 callback_context, false);
michael@0 124 return handler.InternalWriteMinidump(0, 0, NULL);
michael@0 125 }
michael@0 126
michael@0 127 void ExceptionHandler::SetupHandler() {
michael@0 128 // Signal on a different stack to avoid using the stack
michael@0 129 // of the crashing lwp.
michael@0 130 struct sigaltstack sig_stack;
michael@0 131 sig_stack.ss_sp = malloc(MINSIGSTKSZ);
michael@0 132 if (sig_stack.ss_sp == NULL)
michael@0 133 return;
michael@0 134 sig_stack.ss_size = MINSIGSTKSZ;
michael@0 135 sig_stack.ss_flags = 0;
michael@0 136
michael@0 137 if (sigaltstack(&sig_stack, NULL) < 0)
michael@0 138 return;
michael@0 139 for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i)
michael@0 140 SetupHandler(kSigTable[i]);
michael@0 141 }
michael@0 142
michael@0 143 void ExceptionHandler::SetupHandler(int signo) {
michael@0 144 struct sigaction act, old_act;
michael@0 145 act.sa_handler = HandleException;
michael@0 146 act.sa_flags = SA_ONSTACK;
michael@0 147 if (sigaction(signo, &act, &old_act) < 0)
michael@0 148 return;
michael@0 149 old_handlers_[signo] = old_act.sa_handler;
michael@0 150 }
michael@0 151
michael@0 152 void ExceptionHandler::TeardownHandler(int signo) {
michael@0 153 if (old_handlers_.find(signo) != old_handlers_.end()) {
michael@0 154 struct sigaction act;
michael@0 155 act.sa_handler = old_handlers_[signo];
michael@0 156 act.sa_flags = 0;
michael@0 157 sigaction(signo, &act, 0);
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 void ExceptionHandler::TeardownAllHandlers() {
michael@0 162 for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i) {
michael@0 163 TeardownHandler(kSigTable[i]);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 // static
michael@0 168 void ExceptionHandler::HandleException(int signo) {
michael@0 169 //void ExceptionHandler::HandleException(int signo, siginfo_t *sip, ucontext_t *sig_ctx) {
michael@0 170 // The context information about the signal is put on the stack of
michael@0 171 // the signal handler frame as value parameter. For some reasons, the
michael@0 172 // prototype of the handler doesn't declare this information as parameter, we
michael@0 173 // will do it by hand. The stack layout for a signal handler frame is here:
michael@0 174 // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libproc/common/Pstack.c#81
michael@0 175 //
michael@0 176 // However, if we are being called by another signal handler passing the
michael@0 177 // signal up the chain, then we may not have this random extra parameter,
michael@0 178 // so we may have to walk the stack to find it. We do the actual work
michael@0 179 // on another thread, where it's a little safer, but we want the ebp
michael@0 180 // from this frame to find it.
michael@0 181 uintptr_t current_ebp = (uintptr_t)_getfp();
michael@0 182
michael@0 183 pthread_mutex_lock(&handler_stack_mutex_);
michael@0 184 ExceptionHandler *current_handler =
michael@0 185 handler_stack_->at(handler_stack_->size() - ++handler_stack_index_);
michael@0 186 pthread_mutex_unlock(&handler_stack_mutex_);
michael@0 187
michael@0 188 // Restore original handler.
michael@0 189 current_handler->TeardownHandler(signo);
michael@0 190
michael@0 191 ucontext_t *sig_ctx = NULL;
michael@0 192 if (current_handler->InternalWriteMinidump(signo, current_ebp, &sig_ctx)) {
michael@0 193 // if (current_handler->InternalWriteMinidump(signo, &sig_ctx)) {
michael@0 194 // Fully handled this exception, safe to exit.
michael@0 195 exit(EXIT_FAILURE);
michael@0 196 } else {
michael@0 197 // Exception not fully handled, will call the next handler in stack to
michael@0 198 // process it.
michael@0 199 typedef void (*SignalHandler)(int signo);
michael@0 200 SignalHandler old_handler =
michael@0 201 reinterpret_cast<SignalHandler>(current_handler->old_handlers_[signo]);
michael@0 202 if (old_handler != NULL)
michael@0 203 old_handler(signo);
michael@0 204 }
michael@0 205
michael@0 206 pthread_mutex_lock(&handler_stack_mutex_);
michael@0 207 current_handler->SetupHandler(signo);
michael@0 208 --handler_stack_index_;
michael@0 209 // All the handlers in stack have been invoked to handle the exception,
michael@0 210 // normally the process should be terminated and should not reach here.
michael@0 211 // In case we got here, ask the OS to handle it to avoid endless loop,
michael@0 212 // normally the OS will generate a core and termiate the process. This
michael@0 213 // may be desired to debug the program.
michael@0 214 if (handler_stack_index_ == 0)
michael@0 215 signal(signo, SIG_DFL);
michael@0 216 pthread_mutex_unlock(&handler_stack_mutex_);
michael@0 217 }
michael@0 218
michael@0 219 bool ExceptionHandler::InternalWriteMinidump(int signo,
michael@0 220 uintptr_t sighandler_ebp,
michael@0 221 ucontext_t **sig_ctx) {
michael@0 222 if (filter_ && !filter_(callback_context_))
michael@0 223 return false;
michael@0 224
michael@0 225 bool success = false;
michael@0 226 GUID guid;
michael@0 227 char guid_str[kGUIDStringLength + 1];
michael@0 228 if (CreateGUID(&guid) && GUIDToString(&guid, guid_str, sizeof(guid_str))) {
michael@0 229 char minidump_path[PATH_MAX];
michael@0 230 snprintf(minidump_path, sizeof(minidump_path), "%s/%s.dmp",
michael@0 231 dump_path_c_, guid_str);
michael@0 232
michael@0 233 // Block all the signals we want to process when writing minidump.
michael@0 234 // We don't want it to be interrupted.
michael@0 235 sigset_t sig_blocked, sig_old;
michael@0 236 bool blocked = true;
michael@0 237 sigfillset(&sig_blocked);
michael@0 238 for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i)
michael@0 239 sigdelset(&sig_blocked, kSigTable[i]);
michael@0 240 if (sigprocmask(SIG_BLOCK, &sig_blocked, &sig_old) != 0) {
michael@0 241 blocked = false;
michael@0 242 print_message1(2, "HandleException: failed to block signals.\n");
michael@0 243 }
michael@0 244
michael@0 245 success = minidump_generator_.WriteMinidumpToFile(
michael@0 246 minidump_path, signo, sighandler_ebp, sig_ctx);
michael@0 247
michael@0 248 // Unblock the signals.
michael@0 249 if (blocked)
michael@0 250 sigprocmask(SIG_SETMASK, &sig_old, &sig_old);
michael@0 251
michael@0 252 if (callback_)
michael@0 253 success = callback_(dump_path_c_, guid_str, callback_context_, success);
michael@0 254 }
michael@0 255 return success;
michael@0 256 }
michael@0 257
michael@0 258 } // namespace google_breakpad

mercurial