1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/solaris/handler/exception_handler.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,201 @@ 1.4 +// Copyright (c) 2007, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: Alfred Peng 1.34 + 1.35 +#ifndef CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__ 1.36 +#define CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__ 1.37 + 1.38 +#include <map> 1.39 +#include <string> 1.40 +#include <vector> 1.41 + 1.42 +#include "client/solaris/handler/minidump_generator.h" 1.43 + 1.44 +namespace google_breakpad { 1.45 + 1.46 +using std::string; 1.47 + 1.48 +// 1.49 +// ExceptionHandler 1.50 +// 1.51 +// ExceptionHandler can write a minidump file when an exception occurs, 1.52 +// or when WriteMinidump() is called explicitly by your program. 1.53 +// 1.54 +// To have the exception handler write minidumps when an uncaught exception 1.55 +// (crash) occurs, you should create an instance early in the execution 1.56 +// of your program, and keep it around for the entire time you want to 1.57 +// have crash handling active (typically, until shutdown). 1.58 +// (NOTE): There should be only one this kind of exception handler 1.59 +// object per process. 1.60 +// 1.61 +// If you want to write minidumps without installing the exception handler, 1.62 +// you can create an ExceptionHandler with install_handler set to false, 1.63 +// then call WriteMinidump. You can also use this technique if you want to 1.64 +// use different minidump callbacks for different call sites. 1.65 +// 1.66 +// In either case, a callback function is called when a minidump is written, 1.67 +// which receives the unqiue id of the minidump. The caller can use this 1.68 +// id to collect and write additional application state, and to launch an 1.69 +// external crash-reporting application. 1.70 +// 1.71 +// Caller should try to make the callbacks as crash-friendly as possible, 1.72 +// it should avoid use heap memory allocation as much as possible. 1.73 +// 1.74 +class ExceptionHandler { 1.75 + public: 1.76 + // A callback function to run before Breakpad performs any substantial 1.77 + // processing of an exception. A FilterCallback is called before writing 1.78 + // a minidump. context is the parameter supplied by the user as 1.79 + // callback_context when the handler was created. 1.80 + // 1.81 + // If a FilterCallback returns true, Breakpad will continue processing, 1.82 + // attempting to write a minidump. If a FilterCallback returns false, 1.83 + // Breakpad will immediately report the exception as unhandled without 1.84 + // writing a minidump, allowing another handler the opportunity to handle it. 1.85 + typedef bool (*FilterCallback)(void *context); 1.86 + 1.87 + // A callback function to run after the minidump has been written. 1.88 + // minidump_id is a unique id for the dump, so the minidump 1.89 + // file is <dump_path>/<minidump_id>.dmp. context is the parameter supplied 1.90 + // by the user as callback_context when the handler was created. succeeded 1.91 + // indicates whether a minidump file was successfully written. 1.92 + // 1.93 + // If an exception occurred and the callback returns true, Breakpad will 1.94 + // treat the exception as fully-handled, suppressing any other handlers from 1.95 + // being notified of the exception. If the callback returns false, Breakpad 1.96 + // will treat the exception as unhandled, and allow another handler to handle 1.97 + // it. If there are no other handlers, Breakpad will report the exception to 1.98 + // the system as unhandled, allowing a debugger or native crash dialog the 1.99 + // opportunity to handle the exception. Most callback implementations 1.100 + // should normally return the value of |succeeded|, or when they wish to 1.101 + // not report an exception of handled, false. Callbacks will rarely want to 1.102 + // return true directly (unless |succeeded| is true). 1.103 + typedef bool (*MinidumpCallback)(const char *dump_path, 1.104 + const char *minidump_id, 1.105 + void *context, 1.106 + bool succeeded); 1.107 + 1.108 + // Creates a new ExceptionHandler instance to handle writing minidumps. 1.109 + // Before writing a minidump, the optional filter callback will be called. 1.110 + // Its return value determines whether or not Breakpad should write a 1.111 + // minidump. Minidump files will be written to dump_path, and the optional 1.112 + // callback is called after writing the dump file, as described above. 1.113 + // If install_handler is true, then a minidump will be written whenever 1.114 + // an unhandled exception occurs. If it is false, minidumps will only 1.115 + // be written when WriteMinidump is called. 1.116 + ExceptionHandler(const string &dump_path, 1.117 + FilterCallback filter, MinidumpCallback callback, 1.118 + void *callback_context, 1.119 + bool install_handler); 1.120 + ~ExceptionHandler(); 1.121 + 1.122 + // Get and Set the minidump path. 1.123 + string dump_path() const { return dump_path_; } 1.124 + void set_dump_path(const string &dump_path) { 1.125 + dump_path_ = dump_path; 1.126 + dump_path_c_ = dump_path_.c_str(); 1.127 + } 1.128 + 1.129 + // Writes a minidump immediately. This can be used to capture the 1.130 + // execution state independently of a crash. Returns true on success. 1.131 + bool WriteMinidump(); 1.132 + 1.133 + // Convenience form of WriteMinidump which does not require an 1.134 + // ExceptionHandler instance. 1.135 + static bool WriteMinidump(const string &dump_path, 1.136 + MinidumpCallback callback, 1.137 + void *callback_context); 1.138 + 1.139 + private: 1.140 + // Setup crash handler. 1.141 + void SetupHandler(); 1.142 + // Setup signal handler for a signal. 1.143 + void SetupHandler(int signo); 1.144 + // Teardown the handler for a signal. 1.145 + void TeardownHandler(int signo); 1.146 + // Teardown all handlers. 1.147 + void TeardownAllHandlers(); 1.148 + 1.149 + // Runs the main loop for the exception handler thread. 1.150 + static void* ExceptionHandlerThreadMain(void *lpParameter); 1.151 + 1.152 + // Signal handler. 1.153 + static void HandleException(int signo); 1.154 + 1.155 + // Write all the information to the dump file. 1.156 + // If called from a signal handler, sighandler_ebp is the ebp of 1.157 + // that signal handler's frame, and sig_ctx is an out parameter 1.158 + // that will be set to point at the ucontext_t that was placed 1.159 + // on the stack by the kernel. You can pass zero and NULL 1.160 + // for the second and third parameters if you are not calling 1.161 + // this from a signal handler. 1.162 + bool InternalWriteMinidump(int signo, uintptr_t sighandler_ebp, 1.163 + ucontext_t **sig_ctx); 1.164 + 1.165 + private: 1.166 + // The callbacks before and after writing the dump file. 1.167 + FilterCallback filter_; 1.168 + MinidumpCallback callback_; 1.169 + void *callback_context_; 1.170 + 1.171 + // The directory in which a minidump will be written, set by the dump_path 1.172 + // argument to the constructor, or set_dump_path. 1.173 + string dump_path_; 1.174 + // C style dump path. Keep this when setting dump path, since calling 1.175 + // c_str() of std::string when crashing may not be safe. 1.176 + const char *dump_path_c_; 1.177 + 1.178 + // True if the ExceptionHandler installed an unhandled exception filter 1.179 + // when created (with an install_handler parameter set to true). 1.180 + bool installed_handler_; 1.181 + 1.182 + // Keep the previous handlers for the signal. 1.183 + typedef void (*sighandler_t)(int); 1.184 + std::map<int, sighandler_t> old_handlers_; 1.185 + 1.186 + // The global exception handler stack. This is need becuase there may exist 1.187 + // multiple ExceptionHandler instances in a process. Each will have itself 1.188 + // registered in this stack. 1.189 + static std::vector<ExceptionHandler *> *handler_stack_; 1.190 + // The index of the handler that should handle the next exception. 1.191 + static int handler_stack_index_; 1.192 + static pthread_mutex_t handler_stack_mutex_; 1.193 + 1.194 + // The minidump generator. 1.195 + MinidumpGenerator minidump_generator_; 1.196 + 1.197 + // disallow copy ctor and operator= 1.198 + explicit ExceptionHandler(const ExceptionHandler &); 1.199 + void operator=(const ExceptionHandler &); 1.200 +}; 1.201 + 1.202 +} // namespace google_breakpad 1.203 + 1.204 +#endif // CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__