Planeshift

exception_handler.h

Go to the documentation of this file.
00001 // Copyright (c) 2010 Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
00031 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
00032 
00033 #include <string>
00034 #include <vector>
00035 
00036 #include <pthread.h>
00037 #include <signal.h>
00038 #include <stdint.h>
00039 #include <stdio.h>
00040 #include <sys/ucontext.h>
00041 
00042 #include "client/linux/crash_generation/crash_generation_client.h"
00043 #include "client/linux/handler/minidump_descriptor.h"
00044 #include "client/linux/minidump_writer/minidump_writer.h"
00045 #include "common/scoped_ptr.h"
00046 #include "common/using_std_string.h"
00047 #include "google_breakpad/common/minidump_format.h"
00048 
00049 namespace google_breakpad {
00050 
00051 // ExceptionHandler
00052 //
00053 // ExceptionHandler can write a minidump file when an exception occurs,
00054 // or when WriteMinidump() is called explicitly by your program.
00055 //
00056 // To have the exception handler write minidumps when an uncaught exception
00057 // (crash) occurs, you should create an instance early in the execution
00058 // of your program, and keep it around for the entire time you want to
00059 // have crash handling active (typically, until shutdown).
00060 // (NOTE): There should be only be one this kind of exception handler
00061 // object per process.
00062 //
00063 // If you want to write minidumps without installing the exception handler,
00064 // you can create an ExceptionHandler with install_handler set to false,
00065 // then call WriteMinidump.  You can also use this technique if you want to
00066 // use different minidump callbacks for different call sites.
00067 //
00068 // In either case, a callback function is called when a minidump is written,
00069 // which receives the full path or file descriptor of the minidump.  The
00070 // caller can collect and write additional application state to that minidump,
00071 // and launch an external crash-reporting application.
00072 //
00073 // Caller should try to make the callbacks as crash-friendly as possible,
00074 // it should avoid use heap memory allocation as much as possible.
00075 
00076 class ExceptionHandler {
00077  public:
00078   // A callback function to run before Breakpad performs any substantial
00079   // processing of an exception.  A FilterCallback is called before writing
00080   // a minidump.  |context| is the parameter supplied by the user as
00081   // callback_context when the handler was created.
00082   //
00083   // If a FilterCallback returns true, Breakpad will continue processing,
00084   // attempting to write a minidump.  If a FilterCallback returns false,
00085   // Breakpad  will immediately report the exception as unhandled without
00086   // writing a minidump, allowing another handler the opportunity to handle it.
00087   typedef bool (*FilterCallback)(void *context);
00088 
00089   // A callback function to run after the minidump has been written.
00090   // |descriptor| contains the file descriptor or file path containing the
00091   // minidump. |context| is the parameter supplied by the user as
00092   // callback_context when the handler was created.  |succeeded| indicates
00093   // whether a minidump file was successfully written.
00094   //
00095   // If an exception occurred and the callback returns true, Breakpad will
00096   // treat the exception as fully-handled, suppressing any other handlers from
00097   // being notified of the exception.  If the callback returns false, Breakpad
00098   // will treat the exception as unhandled, and allow another handler to handle
00099   // it. If there are no other handlers, Breakpad will report the exception to
00100   // the system as unhandled, allowing a debugger or native crash dialog the
00101   // opportunity to handle the exception.  Most callback implementations
00102   // should normally return the value of |succeeded|, or when they wish to
00103   // not report an exception of handled, false.  Callbacks will rarely want to
00104   // return true directly (unless |succeeded| is true).
00105   typedef bool (*MinidumpCallback)(const MinidumpDescriptor& descriptor,
00106                                    void* context,
00107                                    bool succeeded);
00108 
00109   // In certain cases, a user may wish to handle the generation of the minidump
00110   // themselves. In this case, they can install a handler callback which is
00111   // called when a crash has occurred. If this function returns true, no other
00112   // processing of occurs and the process will shortly be crashed. If this
00113   // returns false, the normal processing continues.
00114   typedef bool (*HandlerCallback)(const void* crash_context,
00115                                   size_t crash_context_size,
00116                                   void* context);
00117 
00118   // Creates a new ExceptionHandler instance to handle writing minidumps.
00119   // Before writing a minidump, the optional |filter| callback will be called.
00120   // Its return value determines whether or not Breakpad should write a
00121   // minidump.  The minidump content will be written to the file path or file
00122   // descriptor from |descriptor|, and the optional |callback| is called after
00123   // writing the dump file, as described above.
00124   // If install_handler is true, then a minidump will be written whenever
00125   // an unhandled exception occurs.  If it is false, minidumps will only
00126   // be written when WriteMinidump is called.
00127   // If |server_fd| is valid, the minidump is generated out-of-process.  If it
00128   // is -1, in-process generation will always be used.
00129   ExceptionHandler(const MinidumpDescriptor& descriptor,
00130                    FilterCallback filter,
00131                    MinidumpCallback callback,
00132                    void *callback_context,
00133                    bool install_handler,
00134                    const int server_fd);
00135   ~ExceptionHandler();
00136 
00137   const MinidumpDescriptor& minidump_descriptor() const {
00138     return minidump_descriptor_;
00139   }
00140 
00141   void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
00142     minidump_descriptor_ = descriptor;
00143   }
00144 
00145   void set_crash_handler(HandlerCallback callback) {
00146     crash_handler_ = callback;
00147   }
00148 
00149   // Writes a minidump immediately.  This can be used to capture the execution
00150   // state independently of a crash.
00151   // Returns true on success.
00152   // If the ExceptionHandler has been created with a path, a new file is
00153   // generated for each minidump.  The file path can be retrieved in the
00154   // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
00155   // MinidumpDescriptor directly from the ExceptionHandler (with
00156   // minidump_descriptor()).
00157   // If the ExceptionHandler has been created with a file descriptor, the file
00158   // descriptor is repositioned to its beginning and the previous generated
00159   // minidump is overwritten.
00160   // Note that this method is not supposed to be called from a compromised
00161   // context as it uses the heap.
00162   bool WriteMinidump();
00163 
00164   // Convenience form of WriteMinidump which does not require an
00165   // ExceptionHandler instance.
00166   static bool WriteMinidump(const string& dump_path,
00167                             MinidumpCallback callback,
00168                             void* callback_context);
00169 
00170   // Write a minidump of |child| immediately.  This can be used to
00171   // capture the execution state of |child| independently of a crash.
00172   // Pass a meaningful |child_blamed_thread| to make that thread in
00173   // the child process the one from which a crash signature is
00174   // extracted.
00175   //
00176   // WARNING: the return of this function *must* happen before
00177   // the code that will eventually reap |child| executes.
00178   // Otherwise there's a pernicious race condition in which |child|
00179   // exits, is reaped, another process created with its pid, then that
00180   // new process dumped.
00181   static bool WriteMinidumpForChild(pid_t child,
00182                                     pid_t child_blamed_thread,
00183                                     const string& dump_path,
00184                                     MinidumpCallback callback,
00185                                     void* callback_context);
00186 
00187   // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
00188   // blob. It shouldn't be needed in any user code.
00189   struct CrashContext {
00190     siginfo_t siginfo;
00191     pid_t tid;  // the crashing thread.
00192     struct ucontext context;
00193 #if !defined(__ARM_EABI__)
00194     // #ifdef this out because FP state is not part of user ABI for Linux ARM.
00195     struct _libc_fpstate float_state;
00196 #endif
00197   };
00198 
00199   // Returns whether out-of-process dump generation is used or not.
00200   bool IsOutOfProcess() const {
00201       return crash_generation_client_.get() != NULL;
00202   }
00203 
00204   // Add information about a memory mapping. This can be used if
00205   // a custom library loader is used that maps things in a way
00206   // that the linux dumper can't handle by reading the maps file.
00207   void AddMappingInfo(const string& name,
00208                       const uint8_t identifier[sizeof(MDGUID)],
00209                       uintptr_t start_address,
00210                       size_t mapping_size,
00211                       size_t file_offset);
00212 
00213   // Register a block of memory of length bytes starting at address ptr
00214   // to be copied to the minidump when a crash happens.
00215   void RegisterAppMemory(void* ptr, size_t length);
00216 
00217   // Unregister a block of memory that was registered with RegisterAppMemory.
00218   void UnregisterAppMemory(void* ptr);
00219 
00220   // Force signal handling for the specified signal.
00221   bool SimulateSignalDelivery(int sig);
00222  private:
00223   // Save the old signal handlers and install new ones.
00224   static bool InstallHandlersLocked();
00225   // Restore the old signal handlers.
00226   static void RestoreHandlersLocked();
00227 
00228   void PreresolveSymbols();
00229   bool GenerateDump(CrashContext *context);
00230   void SendContinueSignalToChild();
00231   void WaitForContinueSignal();
00232 
00233   static void SignalHandler(int sig, siginfo_t* info, void* uc);
00234   bool HandleSignal(int sig, siginfo_t* info, void* uc);
00235   static int ThreadEntry(void* arg);
00236   bool DoDump(pid_t crashing_process, const void* context,
00237               size_t context_size);
00238 
00239   const FilterCallback filter_;
00240   const MinidumpCallback callback_;
00241   void* const callback_context_;
00242 
00243   scoped_ptr<CrashGenerationClient> crash_generation_client_;
00244 
00245   MinidumpDescriptor minidump_descriptor_;
00246 
00247   HandlerCallback crash_handler_;
00248 
00249   // The global exception handler stack. This is need becuase there may exist
00250   // multiple ExceptionHandler instances in a process. Each will have itself
00251   // registered in this stack.
00252   static std::vector<ExceptionHandler*> *handler_stack_;
00253   static pthread_mutex_t handler_stack_mutex_;
00254 
00255   // We need to explicitly enable ptrace of parent processes on some
00256   // kernels, but we need to know the PID of the cloned process before we
00257   // can do this. We create a pipe which we can use to block the
00258   // cloned process after creating it, until we have explicitly enabled 
00259   // ptrace. This is used to store the file descriptors for the pipe
00260   int fdes[2];
00261 
00262   // Callers can add extra info about mappings for cases where the
00263   // dumper code cannot extract enough information from /proc/<pid>/maps.
00264   MappingList mapping_list_;
00265 
00266   // Callers can request additional memory regions to be included in
00267   // the dump.
00268   AppMemoryList app_memory_list_;
00269 };
00270 
00271 }  // namespace google_breakpad
00272 
00273 #endif  // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_