bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006, 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 | // ExceptionHandler can write a minidump file when an exception occurs, |
| 31 | // or when WriteMinidump() is called explicitly by your program. |
| 32 | // |
| 33 | // To have the exception handler write minidumps when an uncaught exception |
| 34 | // (crash) occurs, you should create an instance early in the execution |
| 35 | // of your program, and keep it around for the entire time you want to |
| 36 | // have crash handling active (typically, until shutdown). |
| 37 | // |
| 38 | // If you want to write minidumps without installing the exception handler, |
| 39 | // you can create an ExceptionHandler with install_handler set to false, |
| 40 | // then call WriteMinidump. You can also use this technique if you want to |
| 41 | // use different minidump callbacks for different call sites. |
| 42 | // |
| 43 | // In either case, a callback function is called when a minidump is written, |
| 44 | // which receives the unqiue id of the minidump. The caller can use this |
| 45 | // id to collect and write additional application state, and to launch an |
| 46 | // external crash-reporting application. |
| 47 | // |
| 48 | // It is important that creation and destruction of ExceptionHandler objects |
| 49 | // be nested cleanly, when using install_handler = true. |
| 50 | // Avoid the following pattern: |
| 51 | // ExceptionHandler *e = new ExceptionHandler(...); |
| 52 | // ExceptionHandler *f = new ExceptionHandler(...); |
| 53 | // delete e; |
| 54 | // This will put the exception filter stack into an inconsistent state. |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 55 | |
| 56 | #ifndef CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__ |
| 57 | #define CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__ |
| 58 | |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
mmentovai | 29401d2 | 2006-10-26 18:06:43 +0000 | [diff] [blame] | 60 | #include <Windows.h> |
| 61 | #include <DbgHelp.h> |
mmentovai | 469580e | 2008-01-28 20:02:01 +0000 | [diff] [blame] | 62 | #include <rpc.h> |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 63 | |
mmentovai | 12a5245 | 2006-10-27 19:47:21 +0000 | [diff] [blame] | 64 | #pragma warning( push ) |
| 65 | // Disable exception handler warnings. |
mmentovai | 469580e | 2008-01-28 20:02:01 +0000 | [diff] [blame] | 66 | #pragma warning( disable : 4530 ) |
mmentovai | 12a5245 | 2006-10-27 19:47:21 +0000 | [diff] [blame] | 67 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 68 | #include <string> |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 69 | #include <vector> |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 70 | |
doshimun | 0ded3d7 | 2008-05-05 20:03:56 +0000 | [diff] [blame] | 71 | #include "client/windows/common/ipc_protocol.h" |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 72 | #include "client/windows/crash_generation/crash_generation_client.h" |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 73 | #include "google_breakpad/common/minidump_format.h" |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 74 | #include "processor/scoped_ptr.h" |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 75 | |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 76 | namespace google_breakpad { |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 77 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 78 | using std::vector; |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 79 | using std::wstring; |
| 80 | |
| 81 | class ExceptionHandler { |
| 82 | public: |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 83 | // A callback function to run before Breakpad performs any substantial |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 84 | // processing of an exception. A FilterCallback is called before writing |
| 85 | // a minidump. context is the parameter supplied by the user as |
mmentovai | f614cb9 | 2007-01-12 16:54:10 +0000 | [diff] [blame] | 86 | // callback_context when the handler was created. exinfo points to the |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 87 | // exception record, if any; assertion points to assertion information, |
| 88 | // if any. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 89 | // |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 90 | // If a FilterCallback returns true, Breakpad will continue processing, |
| 91 | // attempting to write a minidump. If a FilterCallback returns false, Breakpad |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 92 | // will immediately report the exception as unhandled without writing a |
| 93 | // minidump, allowing another handler the opportunity to handle it. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 94 | typedef bool (*FilterCallback)(void* context, EXCEPTION_POINTERS* exinfo, |
| 95 | MDRawAssertionInfo* assertion); |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 96 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 97 | // A callback function to run after the minidump has been written. |
| 98 | // minidump_id is a unique id for the dump, so the minidump |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 99 | // file is <dump_path>\<minidump_id>.dmp. context is the parameter supplied |
mmentovai | f614cb9 | 2007-01-12 16:54:10 +0000 | [diff] [blame] | 100 | // by the user as callback_context when the handler was created. exinfo |
| 101 | // points to the exception record, or NULL if no exception occurred. |
| 102 | // succeeded indicates whether a minidump file was successfully written. |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 103 | // assertion points to information about an assertion if the handler was |
| 104 | // invoked by an assertion. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 105 | // |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 106 | // If an exception occurred and the callback returns true, Breakpad will treat |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 107 | // the exception as fully-handled, suppressing any other handlers from being |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 108 | // notified of the exception. If the callback returns false, Breakpad will |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 109 | // treat the exception as unhandled, and allow another handler to handle it. |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 110 | // If there are no other handlers, Breakpad will report the exception to the |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 111 | // system as unhandled, allowing a debugger or native crash dialog the |
| 112 | // opportunity to handle the exception. Most callback implementations |
| 113 | // should normally return the value of |succeeded|, or when they wish to |
| 114 | // not report an exception of handled, false. Callbacks will rarely want to |
| 115 | // return true directly (unless |succeeded| is true). |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 116 | // |
| 117 | // For out-of-process dump generation, dump path and minidump ID will always |
| 118 | // be NULL. In case of out-of-process dump generation, the dump path and |
| 119 | // minidump id are controlled by the server process and are not communicated |
| 120 | // back to the crashing process. |
| 121 | typedef bool (*MinidumpCallback)(const wchar_t* dump_path, |
| 122 | const wchar_t* minidump_id, |
| 123 | void* context, |
| 124 | EXCEPTION_POINTERS* exinfo, |
| 125 | MDRawAssertionInfo* assertion, |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 126 | bool succeeded); |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 127 | |
mmentovai | 6a844b1 | 2007-07-02 19:41:05 +0000 | [diff] [blame] | 128 | // HandlerType specifies which types of handlers should be installed, if |
| 129 | // any. Use HANDLER_NONE for an ExceptionHandler that remains idle, |
| 130 | // without catching any failures on its own. This type of handler may |
| 131 | // still be triggered by calling WriteMinidump. Otherwise, use a |
| 132 | // combination of the other HANDLER_ values, or HANDLER_ALL to install |
| 133 | // all handlers. |
| 134 | enum HandlerType { |
| 135 | HANDLER_NONE = 0, |
| 136 | HANDLER_EXCEPTION = 1 << 0, // SetUnhandledExceptionFilter |
| 137 | HANDLER_INVALID_PARAMETER = 1 << 1, // _set_invalid_parameter_handler |
| 138 | HANDLER_PURECALL = 1 << 2, // _set_purecall_handler |
| 139 | HANDLER_ALL = HANDLER_EXCEPTION | |
| 140 | HANDLER_INVALID_PARAMETER | |
| 141 | HANDLER_PURECALL |
| 142 | }; |
| 143 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 144 | // Creates a new ExceptionHandler instance to handle writing minidumps. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 145 | // Before writing a minidump, the optional filter callback will be called. |
mmentovai | 6a844b1 | 2007-07-02 19:41:05 +0000 | [diff] [blame] | 146 | // Its return value determines whether or not Breakpad should write a |
| 147 | // minidump. Minidump files will be written to dump_path, and the optional |
| 148 | // callback is called after writing the dump file, as described above. |
| 149 | // handler_types specifies the types of handlers that should be installed. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 150 | ExceptionHandler(const wstring& dump_path, |
mmentovai | 6a844b1 | 2007-07-02 19:41:05 +0000 | [diff] [blame] | 151 | FilterCallback filter, |
| 152 | MinidumpCallback callback, |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 153 | void* callback_context, |
mmentovai | 3c07b28 | 2007-07-02 20:41:30 +0000 | [diff] [blame] | 154 | int handler_types); |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 155 | |
| 156 | // Creates a new ExcetpionHandler instance that can attempt to perform |
| 157 | // out-of-process dump generation if pipe_name is not NULL. If pipe_name is |
| 158 | // NULL, or if out-of-process dump generation registration step fails, |
| 159 | // in-process dump generation will be used. This also allows specifying |
| 160 | // the dump type to generate. |
| 161 | ExceptionHandler(const wstring& dump_path, |
| 162 | FilterCallback filter, |
| 163 | MinidumpCallback callback, |
| 164 | void* callback_context, |
| 165 | int handler_types, |
| 166 | MINIDUMP_TYPE dump_type, |
doshimun | 0ded3d7 | 2008-05-05 20:03:56 +0000 | [diff] [blame] | 167 | const wchar_t* pipe_name, |
| 168 | const CustomClientInfo* custom_info); |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 169 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 170 | ~ExceptionHandler(); |
| 171 | |
mmentovai | 1bff57e | 2006-10-27 16:10:55 +0000 | [diff] [blame] | 172 | // Get and set the minidump path. |
| 173 | wstring dump_path() const { return dump_path_; } |
mmentovai | ed61ae0 | 2006-11-28 19:47:44 +0000 | [diff] [blame] | 174 | void set_dump_path(const wstring &dump_path) { |
| 175 | dump_path_ = dump_path; |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 176 | dump_path_c_ = dump_path_.c_str(); |
mmentovai | ed61ae0 | 2006-11-28 19:47:44 +0000 | [diff] [blame] | 177 | UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_. |
| 178 | } |
mmentovai | 1bff57e | 2006-10-27 16:10:55 +0000 | [diff] [blame] | 179 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 180 | // Writes a minidump immediately. This can be used to capture the |
| 181 | // execution state independently of a crash. Returns true on success. |
| 182 | bool WriteMinidump(); |
| 183 | |
mmentovai | fb6be7c | 2007-07-02 15:36:56 +0000 | [diff] [blame] | 184 | // Writes a minidump immediately, with the user-supplied exception |
| 185 | // information. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 186 | bool WriteMinidumpForException(EXCEPTION_POINTERS* exinfo); |
mmentovai | fb6be7c | 2007-07-02 15:36:56 +0000 | [diff] [blame] | 187 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 188 | // Convenience form of WriteMinidump which does not require an |
| 189 | // ExceptionHandler instance. |
| 190 | static bool WriteMinidump(const wstring &dump_path, |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 191 | MinidumpCallback callback, void* callback_context); |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 192 | |
ted.mielczarek | 27e54b4 | 2011-11-11 19:05:51 +0000 | [diff] [blame^] | 193 | // Requests a minidump of the server be generated and waits for completion. |
| 194 | // This can be used to capture the execution state of parent process |
| 195 | // independent of a crash on either side of the connection. Only valid for |
| 196 | // out-of-process clients. |
| 197 | bool RequestMinidumpForParent(); |
| 198 | |
mmentovai | 6a3f879 | 2007-08-17 19:42:18 +0000 | [diff] [blame] | 199 | // Get the thread ID of the thread requesting the dump (either the exception |
| 200 | // thread or any other thread that called WriteMinidump directly). This |
| 201 | // may be useful if you want to include additional thread state in your |
| 202 | // dumps. |
| 203 | DWORD get_requesting_thread_id() const { return requesting_thread_id_; } |
| 204 | |
mmentovai | f4021f0 | 2007-10-18 20:54:20 +0000 | [diff] [blame] | 205 | // Controls behavior of EXCEPTION_BREAKPOINT and EXCEPTION_SINGLE_STEP. |
| 206 | bool get_handle_debug_exceptions() const { return handle_debug_exceptions_; } |
| 207 | void set_handle_debug_exceptions(bool handle_debug_exceptions) { |
| 208 | handle_debug_exceptions_ = handle_debug_exceptions; |
| 209 | } |
| 210 | |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 211 | // Returns whether out-of-process dump generation is used or not. |
| 212 | bool IsOutOfProcess() const { return crash_generation_client_.get() != NULL; } |
| 213 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 214 | private: |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 215 | friend class AutoExceptionHandler; |
| 216 | |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 217 | // Initializes the instance with given values. |
| 218 | void Initialize(const wstring& dump_path, |
| 219 | FilterCallback filter, |
| 220 | MinidumpCallback callback, |
| 221 | void* callback_context, |
| 222 | int handler_types, |
| 223 | MINIDUMP_TYPE dump_type, |
doshimun | 0ded3d7 | 2008-05-05 20:03:56 +0000 | [diff] [blame] | 224 | const wchar_t* pipe_name, |
| 225 | const CustomClientInfo* custom_info); |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 226 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 227 | // Function pointer type for MiniDumpWriteDump, which is looked up |
| 228 | // dynamically. |
| 229 | typedef BOOL (WINAPI *MiniDumpWriteDump_type)( |
| 230 | HANDLE hProcess, |
| 231 | DWORD dwPid, |
| 232 | HANDLE hFile, |
| 233 | MINIDUMP_TYPE DumpType, |
| 234 | CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, |
| 235 | CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, |
| 236 | CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); |
| 237 | |
mmentovai | 469580e | 2008-01-28 20:02:01 +0000 | [diff] [blame] | 238 | // Function pointer type for UuidCreate, which is looked up dynamically. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 239 | typedef RPC_STATUS (RPC_ENTRY *UuidCreate_type)(UUID* Uuid); |
mmentovai | 469580e | 2008-01-28 20:02:01 +0000 | [diff] [blame] | 240 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 241 | // Runs the main loop for the exception handler thread. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 242 | static DWORD WINAPI ExceptionHandlerThreadMain(void* lpParameter); |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 243 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 244 | // Called on the exception thread when an unhandled exception occurs. |
| 245 | // Signals the exception handler thread to handle the exception. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 246 | static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo); |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 247 | |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 248 | #if _MSC_VER >= 1400 // MSVC 2005/8 |
| 249 | // This function will be called by some CRT functions when they detect |
| 250 | // that they were passed an invalid parameter. Note that in _DEBUG builds, |
| 251 | // the CRT may display an assertion dialog before calling this function, |
| 252 | // and the function will not be called unless the assertion dialog is |
| 253 | // dismissed by clicking "Ignore." |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 254 | static void HandleInvalidParameter(const wchar_t* expression, |
| 255 | const wchar_t* function, |
| 256 | const wchar_t* file, |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 257 | unsigned int line, |
| 258 | uintptr_t reserved); |
| 259 | #endif // _MSC_VER >= 1400 |
| 260 | |
ted.mielczarek | b86e7ec | 2007-05-10 17:12:14 +0000 | [diff] [blame] | 261 | // This function will be called by the CRT when a pure virtual |
| 262 | // function is called. |
| 263 | static void HandlePureVirtualCall(); |
| 264 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 265 | // This is called on the exception thread or on another thread that |
| 266 | // the user wishes to produce a dump from. It calls |
| 267 | // WriteMinidumpWithException on the handler thread, avoiding stack |
| 268 | // overflows and inconsistent dumps due to writing the dump from |
| 269 | // the exception thread. If the dump is requested as a result of an |
| 270 | // exception, exinfo contains exception information, otherwise, it |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 271 | // is NULL. If the dump is requested as a result of an assertion |
| 272 | // (such as an invalid parameter being passed to a CRT function), |
| 273 | // assertion contains data about the assertion, otherwise, it is NULL. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 274 | bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS* exinfo, |
| 275 | MDRawAssertionInfo* assertion); |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 276 | |
| 277 | // This function does the actual writing of a minidump. It is called |
| 278 | // on the handler thread. requesting_thread_id is the ID of the thread |
| 279 | // that requested the dump. If the dump is requested as a result of |
| 280 | // an exception, exinfo contains exception information, otherwise, |
| 281 | // it is NULL. |
| 282 | bool WriteMinidumpWithException(DWORD requesting_thread_id, |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 283 | EXCEPTION_POINTERS* exinfo, |
| 284 | MDRawAssertionInfo* assertion); |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 285 | |
ted.mielczarek | 0df0555 | 2011-01-25 19:19:19 +0000 | [diff] [blame] | 286 | // This function is used as a callback when calling MinidumpWriteDump, |
| 287 | // in order to add additional memory regions to the dump. |
| 288 | static BOOL CALLBACK MinidumpWriteDumpCallback( |
| 289 | PVOID context, |
| 290 | const PMINIDUMP_CALLBACK_INPUT callback_input, |
| 291 | PMINIDUMP_CALLBACK_OUTPUT callback_output); |
| 292 | |
mmentovai | ed61ae0 | 2006-11-28 19:47:44 +0000 | [diff] [blame] | 293 | // Generates a new ID and stores it in next_minidump_id_, and stores the |
| 294 | // path of the next minidump to be written in next_minidump_path_. |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 295 | void UpdateNextID(); |
| 296 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 297 | FilterCallback filter_; |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 298 | MinidumpCallback callback_; |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 299 | void* callback_context_; |
| 300 | |
| 301 | scoped_ptr<CrashGenerationClient> crash_generation_client_; |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 302 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 303 | // The directory in which a minidump will be written, set by the dump_path |
| 304 | // argument to the constructor, or set_dump_path. |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 305 | wstring dump_path_; |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 306 | |
| 307 | // The basename of the next minidump to be written, without the extension. |
mmentovai | 29401d2 | 2006-10-26 18:06:43 +0000 | [diff] [blame] | 308 | wstring next_minidump_id_; |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 309 | |
| 310 | // The full pathname of the next minidump to be written, including the file |
| 311 | // extension. |
mmentovai | ed61ae0 | 2006-11-28 19:47:44 +0000 | [diff] [blame] | 312 | wstring next_minidump_path_; |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 313 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 314 | // Pointers to C-string representations of the above. These are set when |
| 315 | // the above wstring versions are set in order to avoid calling c_str during |
| 316 | // an exception, as c_str may attempt to allocate heap memory. These |
| 317 | // pointers are not owned by the ExceptionHandler object, but their lifetimes |
| 318 | // should be equivalent to the lifetimes of the associated wstring, provided |
| 319 | // that the wstrings are not altered. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 320 | const wchar_t* dump_path_c_; |
| 321 | const wchar_t* next_minidump_id_c_; |
| 322 | const wchar_t* next_minidump_path_c_; |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 323 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 324 | HMODULE dbghelp_module_; |
| 325 | MiniDumpWriteDump_type minidump_write_dump_; |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 326 | MINIDUMP_TYPE dump_type_; |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 327 | |
mmentovai | 469580e | 2008-01-28 20:02:01 +0000 | [diff] [blame] | 328 | HMODULE rpcrt4_module_; |
| 329 | UuidCreate_type uuid_create_; |
| 330 | |
mmentovai | 6a844b1 | 2007-07-02 19:41:05 +0000 | [diff] [blame] | 331 | // Tracks the handler types that were installed according to the |
| 332 | // handler_types constructor argument. |
mmentovai | 3c07b28 | 2007-07-02 20:41:30 +0000 | [diff] [blame] | 333 | int handler_types_; |
mmentovai | 65dbfcc | 2006-12-08 22:49:07 +0000 | [diff] [blame] | 334 | |
| 335 | // When installed_handler_ is true, previous_filter_ is the unhandled |
| 336 | // exception filter that was set prior to installing ExceptionHandler as |
| 337 | // the unhandled exception filter and pointing it to |this|. NULL indicates |
| 338 | // that there is no previous unhandled exception filter. |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 339 | LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_; |
mmentovai | 65dbfcc | 2006-12-08 22:49:07 +0000 | [diff] [blame] | 340 | |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 341 | #if _MSC_VER >= 1400 // MSVC 2005/8 |
| 342 | // Beginning in VC 8, the CRT provides an invalid parameter handler that will |
| 343 | // be called when some CRT functions are passed invalid parameters. In |
| 344 | // earlier CRTs, the same conditions would cause unexpected behavior or |
| 345 | // crashes. |
| 346 | _invalid_parameter_handler previous_iph_; |
| 347 | #endif // _MSC_VER >= 1400 |
| 348 | |
ted.mielczarek | b86e7ec | 2007-05-10 17:12:14 +0000 | [diff] [blame] | 349 | // The CRT allows you to override the default handler for pure |
| 350 | // virtual function calls. |
| 351 | _purecall_handler previous_pch_; |
| 352 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 353 | // The exception handler thread. |
| 354 | HANDLE handler_thread_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 355 | |
doshimun | a6f58a1 | 2009-01-14 20:51:51 +0000 | [diff] [blame] | 356 | // True if the exception handler is being destroyed. |
| 357 | // Starting with MSVC 2005, Visual C has stronger guarantees on volatile vars. |
| 358 | // It has release semantics on write and acquire semantics on reads. |
| 359 | // See the msdn documentation. |
| 360 | volatile bool is_shutdown_; |
| 361 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 362 | // The critical section enforcing the requirement that only one exception be |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 363 | // handled by a handler at a time. |
| 364 | CRITICAL_SECTION handler_critical_section_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 365 | |
| 366 | // Semaphores used to move exception handling between the exception thread |
| 367 | // and the handler thread. handler_start_semaphore_ is signalled by the |
| 368 | // exception thread to wake up the handler thread when an exception occurs. |
| 369 | // handler_finish_semaphore_ is signalled by the handler thread to wake up |
| 370 | // the exception thread when handling is complete. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 371 | HANDLE handler_start_semaphore_; |
| 372 | HANDLE handler_finish_semaphore_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 373 | |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 374 | // The next 2 fields contain data passed from the requesting thread to |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 375 | // the handler thread. |
| 376 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 377 | // The thread ID of the thread requesting the dump (either the exception |
| 378 | // thread or any other thread that called WriteMinidump directly). |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 379 | DWORD requesting_thread_id_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 380 | |
| 381 | // The exception info passed to the exception handler on the exception |
| 382 | // thread, if an exception occurred. NULL for user-requested dumps. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 383 | EXCEPTION_POINTERS* exception_info_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 384 | |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 385 | // If the handler is invoked due to an assertion, this will contain a |
| 386 | // pointer to the assertion information. It is NULL at other times. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 387 | MDRawAssertionInfo* assertion_; |
mmentovai | baff938 | 2007-02-07 20:20:10 +0000 | [diff] [blame] | 388 | |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 389 | // The return value of the handler, passed from the handler thread back to |
| 390 | // the requesting thread. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 391 | bool handler_return_value_; |
| 392 | |
mmentovai | f4021f0 | 2007-10-18 20:54:20 +0000 | [diff] [blame] | 393 | // If true, the handler will intercept EXCEPTION_BREAKPOINT and |
| 394 | // EXCEPTION_SINGLE_STEP exceptions. Leave this false (the default) |
| 395 | // to not interfere with debuggers. |
| 396 | bool handle_debug_exceptions_; |
| 397 | |
mmentovai | 65dbfcc | 2006-12-08 22:49:07 +0000 | [diff] [blame] | 398 | // A stack of ExceptionHandler objects that have installed unhandled |
| 399 | // exception filters. This vector is used by HandleException to determine |
| 400 | // which ExceptionHandler object to route an exception to. When an |
| 401 | // ExceptionHandler is created with install_handler true, it will append |
| 402 | // itself to this list. |
doshimun@gmail.com | c79141e | 2008-03-08 00:02:40 +0000 | [diff] [blame] | 403 | static vector<ExceptionHandler*>* handler_stack_; |
mmentovai | 65dbfcc | 2006-12-08 22:49:07 +0000 | [diff] [blame] | 404 | |
| 405 | // The index of the ExceptionHandler in handler_stack_ that will handle the |
| 406 | // next exception. Note that 0 means the last entry in handler_stack_, 1 |
| 407 | // means the next-to-last entry, and so on. This is used by HandleException |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 408 | // to support multiple stacked Breakpad handlers. |
mmentovai | 65dbfcc | 2006-12-08 22:49:07 +0000 | [diff] [blame] | 409 | static LONG handler_stack_index_; |
| 410 | |
| 411 | // handler_stack_critical_section_ guards operations on handler_stack_ and |
doshimun | a6f58a1 | 2009-01-14 20:51:51 +0000 | [diff] [blame] | 412 | // handler_stack_index_. The critical section is initialized by the |
| 413 | // first instance of the class and destroyed by the last instance of it. |
mmentovai | 283fd39 | 2006-12-07 20:46:54 +0000 | [diff] [blame] | 414 | static CRITICAL_SECTION handler_stack_critical_section_; |
| 415 | |
doshimun | a6f58a1 | 2009-01-14 20:51:51 +0000 | [diff] [blame] | 416 | // The number of instances of this class. |
| 417 | volatile static LONG instance_count_; |
mmentovai | b261019 | 2006-10-31 16:49:38 +0000 | [diff] [blame] | 418 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 419 | // disallow copy ctor and operator= |
| 420 | explicit ExceptionHandler(const ExceptionHandler &); |
| 421 | void operator=(const ExceptionHandler &); |
| 422 | }; |
| 423 | |
mmentovai | e5dc608 | 2007-02-14 19:51:05 +0000 | [diff] [blame] | 424 | } // namespace google_breakpad |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 425 | |
mmentovai | 12a5245 | 2006-10-27 19:47:21 +0000 | [diff] [blame] | 426 | #pragma warning( pop ) |
| 427 | |
bryner | 1217c1f | 2006-09-27 01:00:32 +0000 | [diff] [blame] | 428 | #endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__ |