toolkit/crashreporter/google-breakpad/src/client/mac/handler/exception_handler.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/mac/handler/exception_handler.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,280 @@
     1.4 +// Copyright (c) 2006, 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 +// exception_handler.h:  MacOS exception handler
    1.34 +// This class can install a Mach exception port handler to trap most common
    1.35 +// programming errors.  If an exception occurs, a minidump file will be
    1.36 +// generated which contains detailed information about the process and the
    1.37 +// exception.
    1.38 +
    1.39 +#ifndef CLIENT_MAC_HANDLER_EXCEPTION_HANDLER_H__
    1.40 +#define CLIENT_MAC_HANDLER_EXCEPTION_HANDLER_H__
    1.41 +
    1.42 +#include <mach/mach.h>
    1.43 +#include <TargetConditionals.h>
    1.44 +
    1.45 +#include <string>
    1.46 +
    1.47 +#include "common/scoped_ptr.h"
    1.48 +
    1.49 +#if !TARGET_OS_IPHONE
    1.50 +#include "client/mac/crash_generation/crash_generation_client.h"
    1.51 +#endif
    1.52 +
    1.53 +namespace google_breakpad {
    1.54 +
    1.55 +using std::string;
    1.56 +
    1.57 +struct ExceptionParameters;
    1.58 +
    1.59 +enum HandlerThreadMessage {
    1.60 +  // Message ID telling the handler thread to write a dump.
    1.61 +  kWriteDumpMessage = 0,
    1.62 +  // Message ID telling the handler thread to write a dump and include
    1.63 +  // an exception stream.
    1.64 +  kWriteDumpWithExceptionMessage = 1,
    1.65 +  // Message ID telling the handler thread to quit.
    1.66 +  kShutdownMessage = 2
    1.67 +};
    1.68 +
    1.69 +class ExceptionHandler {
    1.70 + public:
    1.71 +  // A callback function to run before Breakpad performs any substantial
    1.72 +  // processing of an exception.  A FilterCallback is called before writing
    1.73 +  // a minidump.  context is the parameter supplied by the user as
    1.74 +  // callback_context when the handler was created.
    1.75 +  //
    1.76 +  // If a FilterCallback returns true, Breakpad will continue processing,
    1.77 +  // attempting to write a minidump.  If a FilterCallback returns false, Breakpad
    1.78 +  // will immediately report the exception as unhandled without writing a
    1.79 +  // minidump, allowing another handler the opportunity to handle it.
    1.80 +  typedef bool (*FilterCallback)(void *context);
    1.81 +
    1.82 +  // A callback function to run after the minidump has been written.
    1.83 +  // |minidump_id| is a unique id for the dump, so the minidump
    1.84 +  // file is <dump_dir>/<minidump_id>.dmp.
    1.85 +  // |context| is the value passed into the constructor.
    1.86 +  // |succeeded| indicates whether a minidump file was successfully written.
    1.87 +  // Return true if the exception was fully handled and breakpad should exit.
    1.88 +  // Return false to allow any other exception handlers to process the
    1.89 +  // exception.
    1.90 +  typedef bool (*MinidumpCallback)(const char *dump_dir,
    1.91 +                                   const char *minidump_id,
    1.92 +                                   void *context, bool succeeded);
    1.93 +
    1.94 +  // A callback function which will be called directly if an exception occurs.
    1.95 +  // This bypasses the minidump file writing and simply gives the client
    1.96 +  // the exception information.
    1.97 +  typedef bool (*DirectCallback)( void *context,
    1.98 +                                  int exception_type,
    1.99 +                                  int exception_code,
   1.100 +                                  int exception_subcode,
   1.101 +                                  mach_port_t thread_name);
   1.102 +
   1.103 +  // Creates a new ExceptionHandler instance to handle writing minidumps.
   1.104 +  // Minidump files will be written to dump_path, and the optional callback
   1.105 +  // is called after writing the dump file, as described above.
   1.106 +  // If install_handler is true, then a minidump will be written whenever
   1.107 +  // an unhandled exception occurs.  If it is false, minidumps will only
   1.108 +  // be written when WriteMinidump is called.
   1.109 +  // If port_name is non-NULL, attempt to perform out-of-process dump generation
   1.110 +  // If port_name is NULL, in-process dump generation will be used.
   1.111 +  ExceptionHandler(const string &dump_path,
   1.112 +                   FilterCallback filter, MinidumpCallback callback,
   1.113 +                   void *callback_context, bool install_handler,
   1.114 +		   const char *port_name);
   1.115 +
   1.116 +  // A special constructor if we want to bypass minidump writing and
   1.117 +  // simply get a callback with the exception information.
   1.118 +  ExceptionHandler(DirectCallback callback,
   1.119 +                   void *callback_context,
   1.120 +                   bool install_handler);
   1.121 +
   1.122 +  ~ExceptionHandler();
   1.123 +
   1.124 +  // Get and set the minidump path.
   1.125 +  string dump_path() const { return dump_path_; }
   1.126 +  void set_dump_path(const string &dump_path) {
   1.127 +    dump_path_ = dump_path;
   1.128 +    dump_path_c_ = dump_path_.c_str();
   1.129 +    UpdateNextID();  // Necessary to put dump_path_ in next_minidump_path_.
   1.130 +  }
   1.131 +
   1.132 +  // Writes a minidump immediately.  This can be used to capture the
   1.133 +  // execution state independently of a crash.  Returns true on success.
   1.134 +  bool WriteMinidump() {
   1.135 +    return WriteMinidump(false);
   1.136 +  }
   1.137 +
   1.138 +  bool WriteMinidump(bool write_exception_stream);
   1.139 +
   1.140 +  // Convenience form of WriteMinidump which does not require an
   1.141 +  // ExceptionHandler instance.
   1.142 +  static bool WriteMinidump(const string &dump_path, MinidumpCallback callback,
   1.143 +                            void *callback_context) {
   1.144 +    return WriteMinidump(dump_path, false, callback, callback_context);
   1.145 +  }
   1.146 +
   1.147 +  static bool WriteMinidump(const string &dump_path,
   1.148 +                            bool write_exception_stream,
   1.149 +                            MinidumpCallback callback,
   1.150 +                            void *callback_context);
   1.151 +
   1.152 +  // Write a minidump of child immediately. This can be used to capture
   1.153 +  // the execution state of a child process independently of a crash.
   1.154 +  static bool WriteMinidumpForChild(mach_port_t child,
   1.155 +				    mach_port_t child_blamed_thread,
   1.156 +				    const std::string &dump_path,
   1.157 +				    MinidumpCallback callback,
   1.158 +				    void *callback_context);
   1.159 +
   1.160 +  // Returns whether out-of-process dump generation is used or not.
   1.161 +  bool IsOutOfProcess() const {
   1.162 +#if TARGET_OS_IPHONE
   1.163 +    return false;
   1.164 +#else
   1.165 +    return crash_generation_client_.get() != NULL;
   1.166 +#endif
   1.167 +  }
   1.168 +
   1.169 + private:
   1.170 +  // Install the mach exception handler
   1.171 +  bool InstallHandler();
   1.172 +
   1.173 +  // Uninstall the mach exception handler (if any)
   1.174 +  bool UninstallHandler(bool in_exception);
   1.175 +
   1.176 +  // Setup the handler thread, and if |install_handler| is true, install the
   1.177 +  // mach exception port handler
   1.178 +  bool Setup(bool install_handler);
   1.179 +
   1.180 +  // Uninstall the mach exception handler (if any) and terminate the helper
   1.181 +  // thread
   1.182 +  bool Teardown();
   1.183 +
   1.184 +  // Send a mach message to the exception handler.  Return true on
   1.185 +  // success, false otherwise.
   1.186 +  bool SendMessageToHandlerThread(HandlerThreadMessage message_id);
   1.187 +
   1.188 +  // All minidump writing goes through this one routine.
   1.189 +  // |task_context| can be NULL. If not, it will be used to retrieve the
   1.190 +  // context of the current thread, instead of using |thread_get_state|.
   1.191 +  bool WriteMinidumpWithException(int exception_type,
   1.192 +                                  int exception_code,
   1.193 +                                  int exception_subcode,
   1.194 +                                  ucontext_t *task_context,
   1.195 +                                  mach_port_t thread_name,
   1.196 +                                  bool exit_after_write,
   1.197 +                                  bool report_current_thread);
   1.198 +
   1.199 +  // When installed, this static function will be call from a newly created
   1.200 +  // pthread with |this| as the argument
   1.201 +  static void *WaitForMessage(void *exception_handler_class);
   1.202 +
   1.203 +  // Signal handler for SIGABRT.
   1.204 +  static void SignalHandler(int sig, siginfo_t* info, void* uc);
   1.205 +
   1.206 +  // disallow copy ctor and operator=
   1.207 +  explicit ExceptionHandler(const ExceptionHandler &);
   1.208 +  void operator=(const ExceptionHandler &);
   1.209 +
   1.210 +  // Generates a new ID and stores it in next_minidump_id_, and stores the
   1.211 +  // path of the next minidump to be written in next_minidump_path_.
   1.212 +  void UpdateNextID();
   1.213 +
   1.214 +  // These functions will suspend/resume all threads except for the
   1.215 +  // reporting thread
   1.216 +  bool SuspendThreads();
   1.217 +  bool ResumeThreads();
   1.218 +
   1.219 +  // The destination directory for the minidump
   1.220 +  string dump_path_;
   1.221 +
   1.222 +  // The basename of the next minidump w/o extension
   1.223 +  string next_minidump_id_;
   1.224 +
   1.225 +  // The full path to the next minidump to be written, including extension
   1.226 +  string next_minidump_path_;
   1.227 +
   1.228 +  // Pointers to the UTF-8 versions of above
   1.229 +  const char *dump_path_c_;
   1.230 +  const char *next_minidump_id_c_;
   1.231 +  const char *next_minidump_path_c_;
   1.232 +
   1.233 +  // The callback function and pointer to be passed back after the minidump
   1.234 +  // has been written
   1.235 +  FilterCallback filter_;
   1.236 +  MinidumpCallback callback_;
   1.237 +  void *callback_context_;
   1.238 +
   1.239 +  // The callback function to be passed back when we don't want a minidump
   1.240 +  // file to be written
   1.241 +  DirectCallback directCallback_;
   1.242 +
   1.243 +  // The thread that is created for the handler
   1.244 +  pthread_t handler_thread_;
   1.245 +
   1.246 +  // The port that is waiting on an exception message to be sent, if the
   1.247 +  // handler is installed
   1.248 +  mach_port_t handler_port_;
   1.249 +
   1.250 +  // These variables save the previous exception handler's data so that it
   1.251 +  // can be re-installed when this handler is uninstalled
   1.252 +  ExceptionParameters *previous_;
   1.253 +
   1.254 +  // True, if we've installed the exception handler
   1.255 +  bool installed_exception_handler_;
   1.256 +
   1.257 +  // True, if we're in the process of uninstalling the exception handler and
   1.258 +  // the thread.
   1.259 +  bool is_in_teardown_;
   1.260 +
   1.261 +  // Save the last result of the last minidump
   1.262 +  bool last_minidump_write_result_;
   1.263 +
   1.264 +  // A mutex for use when writing out a minidump that was requested on a
   1.265 +  // thread other than the exception handler.
   1.266 +  pthread_mutex_t minidump_write_mutex_;
   1.267 +
   1.268 +  // True, if we're using the mutext to indicate when mindump writing occurs
   1.269 +  bool use_minidump_write_mutex_;
   1.270 +
   1.271 +  // Old signal handler for SIGABRT. Used to be able to restore it when
   1.272 +  // uninstalling.
   1.273 +  scoped_ptr<struct sigaction> old_handler_;
   1.274 +
   1.275 +#if !TARGET_OS_IPHONE
   1.276 +  // Client for out-of-process dump generation.
   1.277 +  scoped_ptr<CrashGenerationClient> crash_generation_client_;
   1.278 +#endif
   1.279 +};
   1.280 +
   1.281 +}  // namespace google_breakpad
   1.282 +
   1.283 +#endif  // CLIENT_MAC_HANDLER_EXCEPTION_HANDLER_H__

mercurial