| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009, Google Inc. | |
| 2 // All rights reserved. | |
| 3 // | |
| 4 // Redistribution and use in source and binary forms, with or without | |
| 5 // modification, are permitted provided that the following conditions are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_ | |
| 31 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_ | |
| 32 | |
| 33 #include <vector> | |
| 34 #include <string> | |
| 35 | |
| 36 #include <signal.h> | |
| 37 | |
| 38 namespace google_breakpad { | |
| 39 | |
| 40 // ExceptionHandler | |
| 41 // | |
| 42 // ExceptionHandler can write a minidump file when an exception occurs, | |
| 43 // or when WriteMinidump() is called explicitly by your program. | |
| 44 // | |
| 45 // To have the exception handler write minidumps when an uncaught exception | |
| 46 // (crash) occurs, you should create an instance early in the execution | |
| 47 // of your program, and keep it around for the entire time you want to | |
| 48 // have crash handling active (typically, until shutdown). | |
| 49 // (NOTE): There should be only be one this kind of exception handler | |
| 50 // object per process. | |
| 51 // | |
| 52 // If you want to write minidumps without installing the exception handler, | |
| 53 // you can create an ExceptionHandler with install_handler set to false, | |
| 54 // then call WriteMinidump. You can also use this technique if you want to | |
| 55 // use different minidump callbacks for different call sites. | |
| 56 // | |
| 57 // In either case, a callback function is called when a minidump is written, | |
| 58 // which receives the unqiue id of the minidump. The caller can use this | |
| 59 // id to collect and write additional application state, and to launch an | |
| 60 // external crash-reporting application. | |
| 61 // | |
| 62 // Caller should try to make the callbacks as crash-friendly as possible, | |
| 63 // it should avoid use heap memory allocation as much as possible. | |
| 64 class ExceptionHandler { | |
| 65 public: | |
| 66 // A callback function to run before Breakpad performs any substantial | |
| 67 // processing of an exception. A FilterCallback is called before writing | |
| 68 // a minidump. context is the parameter supplied by the user as | |
| 69 // callback_context when the handler was created. | |
| 70 // | |
| 71 // If a FilterCallback returns true, Breakpad will continue processing, | |
| 72 // attempting to write a minidump. If a FilterCallback returns false, | |
| 73 // Breakpad will immediately report the exception as unhandled without | |
| 74 // writing a minidump, allowing another handler the opportunity to handle it. | |
| 75 typedef bool (*FilterCallback)(void *context); | |
| 76 | |
| 77 // A callback function to run after the minidump has been written. | |
| 78 // minidump_id is a unique id for the dump, so the minidump | |
| 79 // file is <dump_path>\<minidump_id>.dmp. context is the parameter supplied | |
| 80 // by the user as callback_context when the handler was created. succeeded | |
| 81 // indicates whether a minidump file was successfully written. | |
| 82 // | |
| 83 // If an exception occurred and the callback returns true, Breakpad will | |
| 84 // treat the exception as fully-handled, suppressing any other handlers from | |
| 85 // being notified of the exception. If the callback returns false, Breakpad | |
| 86 // will treat the exception as unhandled, and allow another handler to handle | |
| 87 // it. If there are no other handlers, Breakpad will report the exception to | |
| 88 // the system as unhandled, allowing a debugger or native crash dialog the | |
| 89 // opportunity to handle the exception. Most callback implementations | |
| 90 // should normally return the value of |succeeded|, or when they wish to | |
| 91 // not report an exception of handled, false. Callbacks will rarely want to | |
| 92 // return true directly (unless |succeeded| is true). | |
| 93 typedef bool (*MinidumpCallback)(const char *dump_path, | |
| 94 const char *minidump_id, | |
| 95 void *context, | |
| 96 bool succeeded); | |
| 97 | |
| 98 // In certain cases, a user may wish to handle the generation of the minidump | |
| 99 // themselves. In this case, they can install a handler callback which is | |
| 100 // called when a crash has occured. If this function returns true, no other | |
| 101 // processing of occurs and the process will shortly be crashed. If this | |
| 102 // returns false, the normal processing continues. | |
| 103 typedef bool (*HandlerCallback)(const void* crash_context, | |
| 104 size_t crash_context_size, | |
| 105 void* context); | |
| 106 | |
| 107 // Creates a new ExceptionHandler instance to handle writing minidumps. | |
| 108 // Before writing a minidump, the optional filter callback will be called. | |
| 109 // Its return value determines whether or not Breakpad should write a | |
| 110 // minidump. Minidump files will be written to dump_path, and the optional | |
| 111 // callback is called after writing the dump file, as described above. | |
| 112 // If install_handler is true, then a minidump will be written whenever | |
| 113 // an unhandled exception occurs. If it is false, minidumps will only | |
| 114 // be written when WriteMinidump is called. | |
| 115 ExceptionHandler(const std::string &dump_path, | |
| 116 FilterCallback filter, MinidumpCallback callback, | |
| 117 void *callback_context, | |
| 118 bool install_handler); | |
| 119 ~ExceptionHandler(); | |
| 120 | |
| 121 // Get and set the minidump path. | |
| 122 std::string dump_path() const { return dump_path_; } | |
| 123 void set_dump_path(const std::string &dump_path) { | |
| 124 dump_path_ = dump_path; | |
| 125 dump_path_c_ = dump_path_.c_str(); | |
| 126 UpdateNextID(); | |
| 127 } | |
| 128 | |
| 129 void set_crash_handler(HandlerCallback callback) { | |
| 130 crash_handler_ = callback; | |
| 131 } | |
| 132 | |
| 133 // Writes a minidump immediately. This can be used to capture the | |
| 134 // execution state independently of a crash. Returns true on success. | |
| 135 bool WriteMinidump(); | |
| 136 | |
| 137 // Convenience form of WriteMinidump which does not require an | |
| 138 // ExceptionHandler instance. | |
| 139 static bool WriteMinidump(const std::string &dump_path, | |
| 140 MinidumpCallback callback, | |
| 141 void *callback_context); | |
| 142 | |
| 143 // This structure is passed to minidump_writer.h:WriteMinidump via an opaque | |
| 144 // blob. It shouldn't be needed in any user code. | |
| 145 struct CrashContext { | |
| 146 siginfo_t siginfo; | |
| 147 pid_t tid; // the crashing thread. | |
| 148 struct ucontext context; | |
| 149 struct _libc_fpstate float_state; | |
| 150 }; | |
| 151 | |
| 152 private: | |
| 153 bool InstallHandlers(); | |
| 154 void UninstallHandlers(); | |
| 155 void PreresolveSymbols(); | |
| 156 | |
| 157 void UpdateNextID(); | |
| 158 static void SignalHandler(int sig, siginfo_t* info, void* uc); | |
| 159 bool HandleSignal(int sig, siginfo_t* info, void* uc); | |
| 160 static int ThreadEntry(void* arg); | |
| 161 bool DoDump(pid_t crashing_process, const void* context, | |
| 162 size_t context_size); | |
| 163 | |
| 164 const FilterCallback filter_; | |
| 165 const MinidumpCallback callback_; | |
| 166 void* const callback_context_; | |
| 167 | |
| 168 std::string dump_path_; | |
| 169 std::string next_minidump_path_; | |
| 170 std::string next_minidump_id_; | |
| 171 | |
| 172 // Pointers to C-string representations of the above. These are set | |
| 173 // when the above are set so we can avoid calling c_str during | |
| 174 // an exception. | |
| 175 const char* dump_path_c_; | |
| 176 const char* next_minidump_path_c_; | |
| 177 const char* next_minidump_id_c_; | |
| 178 | |
| 179 const bool handler_installed_; | |
| 180 void* signal_stack; // the handler stack. | |
| 181 HandlerCallback crash_handler_; | |
| 182 | |
| 183 // The global exception handler stack. This is need becuase there may exist | |
| 184 // multiple ExceptionHandler instances in a process. Each will have itself | |
| 185 // registered in this stack. | |
| 186 static std::vector<ExceptionHandler*> *handler_stack_; | |
| 187 // The index of the handler that should handle the next exception. | |
| 188 static unsigned handler_stack_index_; | |
| 189 static pthread_mutex_t handler_stack_mutex_; | |
| 190 | |
| 191 // A vector of the old signal handlers. The void* is a pointer to a newly | |
| 192 // allocated sigaction structure to avoid pulling in too many includes. | |
| 193 std::vector<std::pair<int, void *> > old_handlers_; | |
| 194 }; | |
| 195 | |
| 196 } // namespace google_breakpad | |
| 197 | |
| 198 #endif // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_ | |
| OLD | NEW |