toolkit/crashreporter/google-breakpad/src/client/linux/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/linux/handler/exception_handler.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,275 @@
     1.4 +// Copyright (c) 2010 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 +#ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
    1.34 +#define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
    1.35 +
    1.36 +#include <string>
    1.37 +#include <vector>
    1.38 +
    1.39 +#include <pthread.h>
    1.40 +#include <signal.h>
    1.41 +#include <stdint.h>
    1.42 +#include <stdio.h>
    1.43 +#include <sys/ucontext.h>
    1.44 +
    1.45 +#include "client/linux/crash_generation/crash_generation_client.h"
    1.46 +#include "client/linux/handler/minidump_descriptor.h"
    1.47 +#include "client/linux/minidump_writer/minidump_writer.h"
    1.48 +#include "common/scoped_ptr.h"
    1.49 +#include "common/using_std_string.h"
    1.50 +#include "google_breakpad/common/minidump_format.h"
    1.51 +
    1.52 +namespace google_breakpad {
    1.53 +
    1.54 +// ExceptionHandler
    1.55 +//
    1.56 +// ExceptionHandler can write a minidump file when an exception occurs,
    1.57 +// or when WriteMinidump() is called explicitly by your program.
    1.58 +//
    1.59 +// To have the exception handler write minidumps when an uncaught exception
    1.60 +// (crash) occurs, you should create an instance early in the execution
    1.61 +// of your program, and keep it around for the entire time you want to
    1.62 +// have crash handling active (typically, until shutdown).
    1.63 +// (NOTE): There should be only be one this kind of exception handler
    1.64 +// object per process.
    1.65 +//
    1.66 +// If you want to write minidumps without installing the exception handler,
    1.67 +// you can create an ExceptionHandler with install_handler set to false,
    1.68 +// then call WriteMinidump.  You can also use this technique if you want to
    1.69 +// use different minidump callbacks for different call sites.
    1.70 +//
    1.71 +// In either case, a callback function is called when a minidump is written,
    1.72 +// which receives the full path or file descriptor of the minidump.  The
    1.73 +// caller can collect and write additional application state to that minidump,
    1.74 +// and launch an external crash-reporting application.
    1.75 +//
    1.76 +// Caller should try to make the callbacks as crash-friendly as possible,
    1.77 +// it should avoid use heap memory allocation as much as possible.
    1.78 +
    1.79 +class ExceptionHandler {
    1.80 + public:
    1.81 +  // A callback function to run before Breakpad performs any substantial
    1.82 +  // processing of an exception.  A FilterCallback is called before writing
    1.83 +  // a minidump.  |context| is the parameter supplied by the user as
    1.84 +  // callback_context when the handler was created.
    1.85 +  //
    1.86 +  // If a FilterCallback returns true, Breakpad will continue processing,
    1.87 +  // attempting to write a minidump.  If a FilterCallback returns false,
    1.88 +  // Breakpad  will immediately report the exception as unhandled without
    1.89 +  // writing a minidump, allowing another handler the opportunity to handle it.
    1.90 +  typedef bool (*FilterCallback)(void *context);
    1.91 +
    1.92 +  // A callback function to run after the minidump has been written.
    1.93 +  // |descriptor| contains the file descriptor or file path containing the
    1.94 +  // minidump. |context| is the parameter supplied by the user as
    1.95 +  // callback_context when the handler was created.  |succeeded| indicates
    1.96 +  // whether a minidump file was successfully written.
    1.97 +  //
    1.98 +  // If an exception occurred and the callback returns true, Breakpad will
    1.99 +  // treat the exception as fully-handled, suppressing any other handlers from
   1.100 +  // being notified of the exception.  If the callback returns false, Breakpad
   1.101 +  // will treat the exception as unhandled, and allow another handler to handle
   1.102 +  // it. If there are no other handlers, Breakpad will report the exception to
   1.103 +  // the system as unhandled, allowing a debugger or native crash dialog the
   1.104 +  // opportunity to handle the exception.  Most callback implementations
   1.105 +  // should normally return the value of |succeeded|, or when they wish to
   1.106 +  // not report an exception of handled, false.  Callbacks will rarely want to
   1.107 +  // return true directly (unless |succeeded| is true).
   1.108 +  typedef bool (*MinidumpCallback)(const MinidumpDescriptor& descriptor,
   1.109 +                                   void* context,
   1.110 +                                   bool succeeded);
   1.111 +
   1.112 +  // In certain cases, a user may wish to handle the generation of the minidump
   1.113 +  // themselves. In this case, they can install a handler callback which is
   1.114 +  // called when a crash has occurred. If this function returns true, no other
   1.115 +  // processing of occurs and the process will shortly be crashed. If this
   1.116 +  // returns false, the normal processing continues.
   1.117 +  typedef bool (*HandlerCallback)(const void* crash_context,
   1.118 +                                  size_t crash_context_size,
   1.119 +                                  void* context);
   1.120 +
   1.121 +  // Creates a new ExceptionHandler instance to handle writing minidumps.
   1.122 +  // Before writing a minidump, the optional |filter| callback will be called.
   1.123 +  // Its return value determines whether or not Breakpad should write a
   1.124 +  // minidump.  The minidump content will be written to the file path or file
   1.125 +  // descriptor from |descriptor|, and the optional |callback| is called after
   1.126 +  // writing the dump file, as described above.
   1.127 +  // If install_handler is true, then a minidump will be written whenever
   1.128 +  // an unhandled exception occurs.  If it is false, minidumps will only
   1.129 +  // be written when WriteMinidump is called.
   1.130 +  // If |server_fd| is valid, the minidump is generated out-of-process.  If it
   1.131 +  // is -1, in-process generation will always be used.
   1.132 +  ExceptionHandler(const MinidumpDescriptor& descriptor,
   1.133 +                   FilterCallback filter,
   1.134 +                   MinidumpCallback callback,
   1.135 +                   void *callback_context,
   1.136 +                   bool install_handler,
   1.137 +                   const int server_fd);
   1.138 +  ~ExceptionHandler();
   1.139 +
   1.140 +  const MinidumpDescriptor& minidump_descriptor() const {
   1.141 +    return minidump_descriptor_;
   1.142 +  }
   1.143 +
   1.144 +  void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
   1.145 +    minidump_descriptor_ = descriptor;
   1.146 +  }
   1.147 +
   1.148 +  void set_crash_handler(HandlerCallback callback) {
   1.149 +    crash_handler_ = callback;
   1.150 +  }
   1.151 +
   1.152 +  // Writes a minidump immediately.  This can be used to capture the execution
   1.153 +  // state independently of a crash.
   1.154 +  // Returns true on success.
   1.155 +  // If the ExceptionHandler has been created with a path, a new file is
   1.156 +  // generated for each minidump.  The file path can be retrieved in the
   1.157 +  // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
   1.158 +  // MinidumpDescriptor directly from the ExceptionHandler (with
   1.159 +  // minidump_descriptor()).
   1.160 +  // If the ExceptionHandler has been created with a file descriptor, the file
   1.161 +  // descriptor is repositioned to its beginning and the previous generated
   1.162 +  // minidump is overwritten.
   1.163 +  // Note that this method is not supposed to be called from a compromised
   1.164 +  // context as it uses the heap.
   1.165 +  bool WriteMinidump();
   1.166 +
   1.167 +  // Convenience form of WriteMinidump which does not require an
   1.168 +  // ExceptionHandler instance.
   1.169 +  static bool WriteMinidump(const string& dump_path,
   1.170 +                            MinidumpCallback callback,
   1.171 +                            void* callback_context);
   1.172 +
   1.173 +  // Write a minidump of |child| immediately.  This can be used to
   1.174 +  // capture the execution state of |child| independently of a crash.
   1.175 +  // Pass a meaningful |child_blamed_thread| to make that thread in
   1.176 +  // the child process the one from which a crash signature is
   1.177 +  // extracted.
   1.178 +  //
   1.179 +  // WARNING: the return of this function *must* happen before
   1.180 +  // the code that will eventually reap |child| executes.
   1.181 +  // Otherwise there's a pernicious race condition in which |child|
   1.182 +  // exits, is reaped, another process created with its pid, then that
   1.183 +  // new process dumped.
   1.184 +  static bool WriteMinidumpForChild(pid_t child,
   1.185 +                                    pid_t child_blamed_thread,
   1.186 +                                    const string& dump_path,
   1.187 +                                    MinidumpCallback callback,
   1.188 +                                    void* callback_context);
   1.189 +
   1.190 +  // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
   1.191 +  // blob. It shouldn't be needed in any user code.
   1.192 +  struct CrashContext {
   1.193 +    siginfo_t siginfo;
   1.194 +    pid_t tid;  // the crashing thread.
   1.195 +    struct ucontext context;
   1.196 +#if !defined(__ARM_EABI__)
   1.197 +    // #ifdef this out because FP state is not part of user ABI for Linux ARM.
   1.198 +    struct _libc_fpstate float_state;
   1.199 +#endif
   1.200 +  };
   1.201 +
   1.202 +  // Returns whether out-of-process dump generation is used or not.
   1.203 +  bool IsOutOfProcess() const {
   1.204 +      return crash_generation_client_.get() != NULL;
   1.205 +  }
   1.206 +
   1.207 +  // Add information about a memory mapping. This can be used if
   1.208 +  // a custom library loader is used that maps things in a way
   1.209 +  // that the linux dumper can't handle by reading the maps file.
   1.210 +  void AddMappingInfo(const string& name,
   1.211 +                      const uint8_t identifier[sizeof(MDGUID)],
   1.212 +                      uintptr_t start_address,
   1.213 +                      size_t mapping_size,
   1.214 +                      size_t file_offset);
   1.215 +
   1.216 +  // Register a block of memory of length bytes starting at address ptr
   1.217 +  // to be copied to the minidump when a crash happens.
   1.218 +  void RegisterAppMemory(void* ptr, size_t length);
   1.219 +
   1.220 +  // Unregister a block of memory that was registered with RegisterAppMemory.
   1.221 +  void UnregisterAppMemory(void* ptr);
   1.222 +
   1.223 +  // Force signal handling for the specified signal.
   1.224 +  bool SimulateSignalDelivery(int sig);
   1.225 +
   1.226 +  // Report a crash signal from an SA_SIGINFO signal handler.
   1.227 +  bool HandleSignal(int sig, siginfo_t* info, void* uc);
   1.228 + private:
   1.229 +  // Save the old signal handlers and install new ones.
   1.230 +  static bool InstallHandlersLocked();
   1.231 +  // Restore the old signal handlers.
   1.232 +  static void RestoreHandlersLocked();
   1.233 +
   1.234 +  void PreresolveSymbols();
   1.235 +  bool GenerateDump(CrashContext *context);
   1.236 +  void SendContinueSignalToChild();
   1.237 +  void WaitForContinueSignal();
   1.238 +
   1.239 +  static void SignalHandler(int sig, siginfo_t* info, void* uc);
   1.240 +  static int ThreadEntry(void* arg);
   1.241 +  bool DoDump(pid_t crashing_process, const void* context,
   1.242 +              size_t context_size);
   1.243 +
   1.244 +  const FilterCallback filter_;
   1.245 +  const MinidumpCallback callback_;
   1.246 +  void* const callback_context_;
   1.247 +
   1.248 +  scoped_ptr<CrashGenerationClient> crash_generation_client_;
   1.249 +
   1.250 +  MinidumpDescriptor minidump_descriptor_;
   1.251 +
   1.252 +  HandlerCallback crash_handler_;
   1.253 +
   1.254 +  // The global exception handler stack. This is need becuase there may exist
   1.255 +  // multiple ExceptionHandler instances in a process. Each will have itself
   1.256 +  // registered in this stack.
   1.257 +  static std::vector<ExceptionHandler*> *handler_stack_;
   1.258 +  static pthread_mutex_t handler_stack_mutex_;
   1.259 +
   1.260 +  // We need to explicitly enable ptrace of parent processes on some
   1.261 +  // kernels, but we need to know the PID of the cloned process before we
   1.262 +  // can do this. We create a pipe which we can use to block the
   1.263 +  // cloned process after creating it, until we have explicitly enabled 
   1.264 +  // ptrace. This is used to store the file descriptors for the pipe
   1.265 +  int fdes[2];
   1.266 +
   1.267 +  // Callers can add extra info about mappings for cases where the
   1.268 +  // dumper code cannot extract enough information from /proc/<pid>/maps.
   1.269 +  MappingList mapping_list_;
   1.270 +
   1.271 +  // Callers can request additional memory regions to be included in
   1.272 +  // the dump.
   1.273 +  AppMemoryList app_memory_list_;
   1.274 +};
   1.275 +
   1.276 +}  // namespace google_breakpad
   1.277 +
   1.278 +#endif  // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_

mercurial