blob: c2d55609d532ebb794985ed653f45c469ccc55f8 [file] [log] [blame]
bryner1217c1f2006-09-27 01:00:32 +00001// 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.
brynerf1400252006-09-28 19:35:08 +000055//
56// To use this library in your project, you will need to link against
mmentovai29401d22006-10-26 18:06:43 +000057// ole32.lib.
bryner1217c1f2006-09-27 01:00:32 +000058
59#ifndef CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__
60#define CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__
61
mmentovaibaff9382007-02-07 20:20:10 +000062#include <stdlib.h>
mmentovai29401d22006-10-26 18:06:43 +000063#include <Windows.h>
64#include <DbgHelp.h>
bryner1217c1f2006-09-27 01:00:32 +000065
mmentovai12a52452006-10-27 19:47:21 +000066#pragma warning( push )
67// Disable exception handler warnings.
68#pragma warning( disable : 4530 )
69
bryner1217c1f2006-09-27 01:00:32 +000070#include <string>
mmentovai283fd392006-12-07 20:46:54 +000071#include <vector>
bryner1217c1f2006-09-27 01:00:32 +000072
mmentovaie5dc6082007-02-14 19:51:05 +000073#include "google_breakpad/common/minidump_format.h"
mmentovaibaff9382007-02-07 20:20:10 +000074
mmentovaie5dc6082007-02-14 19:51:05 +000075namespace google_breakpad {
bryner1217c1f2006-09-27 01:00:32 +000076
mmentovai283fd392006-12-07 20:46:54 +000077using std::vector;
bryner1217c1f2006-09-27 01:00:32 +000078using std::wstring;
79
80class ExceptionHandler {
81 public:
mmentovaie5dc6082007-02-14 19:51:05 +000082 // A callback function to run before Breakpad performs any substantial
mmentovai283fd392006-12-07 20:46:54 +000083 // processing of an exception. A FilterCallback is called before writing
84 // a minidump. context is the parameter supplied by the user as
mmentovaif614cb92007-01-12 16:54:10 +000085 // callback_context when the handler was created. exinfo points to the
mmentovaibaff9382007-02-07 20:20:10 +000086 // exception record, if any; assertion points to assertion information,
87 // if any.
mmentovai283fd392006-12-07 20:46:54 +000088 //
mmentovaie5dc6082007-02-14 19:51:05 +000089 // If a FilterCallback returns true, Breakpad will continue processing,
90 // attempting to write a minidump. If a FilterCallback returns false, Breakpad
mmentovai283fd392006-12-07 20:46:54 +000091 // will immediately report the exception as unhandled without writing a
92 // minidump, allowing another handler the opportunity to handle it.
mmentovaibaff9382007-02-07 20:20:10 +000093 typedef bool (*FilterCallback)(void *context, EXCEPTION_POINTERS *exinfo,
94 MDRawAssertionInfo *assertion);
mmentovai283fd392006-12-07 20:46:54 +000095
bryner1217c1f2006-09-27 01:00:32 +000096 // A callback function to run after the minidump has been written.
97 // minidump_id is a unique id for the dump, so the minidump
mmentovai283fd392006-12-07 20:46:54 +000098 // file is <dump_path>\<minidump_id>.dmp. context is the parameter supplied
mmentovaif614cb92007-01-12 16:54:10 +000099 // by the user as callback_context when the handler was created. exinfo
100 // points to the exception record, or NULL if no exception occurred.
101 // succeeded indicates whether a minidump file was successfully written.
mmentovaibaff9382007-02-07 20:20:10 +0000102 // assertion points to information about an assertion if the handler was
103 // invoked by an assertion.
mmentovai283fd392006-12-07 20:46:54 +0000104 //
mmentovaie5dc6082007-02-14 19:51:05 +0000105 // If an exception occurred and the callback returns true, Breakpad will treat
mmentovai283fd392006-12-07 20:46:54 +0000106 // the exception as fully-handled, suppressing any other handlers from being
mmentovaie5dc6082007-02-14 19:51:05 +0000107 // notified of the exception. If the callback returns false, Breakpad will
mmentovai283fd392006-12-07 20:46:54 +0000108 // treat the exception as unhandled, and allow another handler to handle it.
mmentovaie5dc6082007-02-14 19:51:05 +0000109 // If there are no other handlers, Breakpad will report the exception to the
mmentovai283fd392006-12-07 20:46:54 +0000110 // system as unhandled, allowing a debugger or native crash dialog the
111 // opportunity to handle the exception. Most callback implementations
112 // should normally return the value of |succeeded|, or when they wish to
113 // not report an exception of handled, false. Callbacks will rarely want to
114 // return true directly (unless |succeeded| is true).
115 typedef bool (*MinidumpCallback)(const wchar_t *dump_path,
116 const wchar_t *minidump_id,
117 void *context,
mmentovaif614cb92007-01-12 16:54:10 +0000118 EXCEPTION_POINTERS *exinfo,
mmentovaibaff9382007-02-07 20:20:10 +0000119 MDRawAssertionInfo *assertion,
mmentovai283fd392006-12-07 20:46:54 +0000120 bool succeeded);
bryner1217c1f2006-09-27 01:00:32 +0000121
mmentovai6a844b12007-07-02 19:41:05 +0000122 // HandlerType specifies which types of handlers should be installed, if
123 // any. Use HANDLER_NONE for an ExceptionHandler that remains idle,
124 // without catching any failures on its own. This type of handler may
125 // still be triggered by calling WriteMinidump. Otherwise, use a
126 // combination of the other HANDLER_ values, or HANDLER_ALL to install
127 // all handlers.
128 enum HandlerType {
129 HANDLER_NONE = 0,
130 HANDLER_EXCEPTION = 1 << 0, // SetUnhandledExceptionFilter
131 HANDLER_INVALID_PARAMETER = 1 << 1, // _set_invalid_parameter_handler
132 HANDLER_PURECALL = 1 << 2, // _set_purecall_handler
133 HANDLER_ALL = HANDLER_EXCEPTION |
134 HANDLER_INVALID_PARAMETER |
135 HANDLER_PURECALL
136 };
137
bryner1217c1f2006-09-27 01:00:32 +0000138 // Creates a new ExceptionHandler instance to handle writing minidumps.
mmentovai283fd392006-12-07 20:46:54 +0000139 // Before writing a minidump, the optional filter callback will be called.
mmentovai6a844b12007-07-02 19:41:05 +0000140 // Its return value determines whether or not Breakpad should write a
141 // minidump. Minidump files will be written to dump_path, and the optional
142 // callback is called after writing the dump file, as described above.
143 // handler_types specifies the types of handlers that should be installed.
mmentovai283fd392006-12-07 20:46:54 +0000144 ExceptionHandler(const wstring &dump_path,
mmentovai6a844b12007-07-02 19:41:05 +0000145 FilterCallback filter,
146 MinidumpCallback callback,
147 void *callback_context,
148 HandlerType handler_types);
bryner1217c1f2006-09-27 01:00:32 +0000149 ~ExceptionHandler();
150
mmentovai1bff57e2006-10-27 16:10:55 +0000151 // Get and set the minidump path.
152 wstring dump_path() const { return dump_path_; }
mmentovaied61ae02006-11-28 19:47:44 +0000153 void set_dump_path(const wstring &dump_path) {
154 dump_path_ = dump_path;
mmentovai283fd392006-12-07 20:46:54 +0000155 dump_path_c_ = dump_path_.c_str();
mmentovaied61ae02006-11-28 19:47:44 +0000156 UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_.
157 }
mmentovai1bff57e2006-10-27 16:10:55 +0000158
bryner1217c1f2006-09-27 01:00:32 +0000159 // Writes a minidump immediately. This can be used to capture the
160 // execution state independently of a crash. Returns true on success.
161 bool WriteMinidump();
162
mmentovaifb6be7c2007-07-02 15:36:56 +0000163 // Writes a minidump immediately, with the user-supplied exception
164 // information.
165 bool WriteMinidumpForException(EXCEPTION_POINTERS *exinfo);
166
bryner1217c1f2006-09-27 01:00:32 +0000167 // Convenience form of WriteMinidump which does not require an
168 // ExceptionHandler instance.
169 static bool WriteMinidump(const wstring &dump_path,
170 MinidumpCallback callback, void *callback_context);
171
172 private:
mmentovaibaff9382007-02-07 20:20:10 +0000173 friend class AutoExceptionHandler;
174
bryner1217c1f2006-09-27 01:00:32 +0000175 // Function pointer type for MiniDumpWriteDump, which is looked up
176 // dynamically.
177 typedef BOOL (WINAPI *MiniDumpWriteDump_type)(
178 HANDLE hProcess,
179 DWORD dwPid,
180 HANDLE hFile,
181 MINIDUMP_TYPE DumpType,
182 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
183 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
184 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
185
mmentovaib2610192006-10-31 16:49:38 +0000186 // Runs the main loop for the exception handler thread.
187 static DWORD WINAPI ExceptionHandlerThreadMain(void *lpParameter);
bryner1217c1f2006-09-27 01:00:32 +0000188
mmentovaib2610192006-10-31 16:49:38 +0000189 // Called on the exception thread when an unhandled exception occurs.
190 // Signals the exception handler thread to handle the exception.
bryner1217c1f2006-09-27 01:00:32 +0000191 static LONG WINAPI HandleException(EXCEPTION_POINTERS *exinfo);
192
mmentovaibaff9382007-02-07 20:20:10 +0000193#if _MSC_VER >= 1400 // MSVC 2005/8
194 // This function will be called by some CRT functions when they detect
195 // that they were passed an invalid parameter. Note that in _DEBUG builds,
196 // the CRT may display an assertion dialog before calling this function,
197 // and the function will not be called unless the assertion dialog is
198 // dismissed by clicking "Ignore."
199 static void HandleInvalidParameter(const wchar_t *expression,
200 const wchar_t *function,
201 const wchar_t *file,
202 unsigned int line,
203 uintptr_t reserved);
204#endif // _MSC_VER >= 1400
205
ted.mielczarekb86e7ec2007-05-10 17:12:14 +0000206 // This function will be called by the CRT when a pure virtual
207 // function is called.
208 static void HandlePureVirtualCall();
209
mmentovaib2610192006-10-31 16:49:38 +0000210 // This is called on the exception thread or on another thread that
211 // the user wishes to produce a dump from. It calls
212 // WriteMinidumpWithException on the handler thread, avoiding stack
213 // overflows and inconsistent dumps due to writing the dump from
214 // the exception thread. If the dump is requested as a result of an
215 // exception, exinfo contains exception information, otherwise, it
mmentovaibaff9382007-02-07 20:20:10 +0000216 // is NULL. If the dump is requested as a result of an assertion
217 // (such as an invalid parameter being passed to a CRT function),
218 // assertion contains data about the assertion, otherwise, it is NULL.
219 bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS *exinfo,
220 MDRawAssertionInfo *assertion);
mmentovaib2610192006-10-31 16:49:38 +0000221
222 // This function does the actual writing of a minidump. It is called
223 // on the handler thread. requesting_thread_id is the ID of the thread
224 // that requested the dump. If the dump is requested as a result of
225 // an exception, exinfo contains exception information, otherwise,
226 // it is NULL.
227 bool WriteMinidumpWithException(DWORD requesting_thread_id,
mmentovaibaff9382007-02-07 20:20:10 +0000228 EXCEPTION_POINTERS *exinfo,
229 MDRawAssertionInfo *assertion);
mmentovaib2610192006-10-31 16:49:38 +0000230
mmentovaied61ae02006-11-28 19:47:44 +0000231 // Generates a new ID and stores it in next_minidump_id_, and stores the
232 // path of the next minidump to be written in next_minidump_path_.
bryner1217c1f2006-09-27 01:00:32 +0000233 void UpdateNextID();
234
mmentovai283fd392006-12-07 20:46:54 +0000235 FilterCallback filter_;
bryner1217c1f2006-09-27 01:00:32 +0000236 MinidumpCallback callback_;
237 void *callback_context_;
238
mmentovai283fd392006-12-07 20:46:54 +0000239 // The directory in which a minidump will be written, set by the dump_path
240 // argument to the constructor, or set_dump_path.
bryner1217c1f2006-09-27 01:00:32 +0000241 wstring dump_path_;
mmentovai283fd392006-12-07 20:46:54 +0000242
243 // The basename of the next minidump to be written, without the extension.
mmentovai29401d22006-10-26 18:06:43 +0000244 wstring next_minidump_id_;
mmentovai283fd392006-12-07 20:46:54 +0000245
246 // The full pathname of the next minidump to be written, including the file
247 // extension.
mmentovaied61ae02006-11-28 19:47:44 +0000248 wstring next_minidump_path_;
bryner1217c1f2006-09-27 01:00:32 +0000249
mmentovai283fd392006-12-07 20:46:54 +0000250 // Pointers to C-string representations of the above. These are set when
251 // the above wstring versions are set in order to avoid calling c_str during
252 // an exception, as c_str may attempt to allocate heap memory. These
253 // pointers are not owned by the ExceptionHandler object, but their lifetimes
254 // should be equivalent to the lifetimes of the associated wstring, provided
255 // that the wstrings are not altered.
256 const wchar_t *dump_path_c_;
257 const wchar_t *next_minidump_id_c_;
258 const wchar_t *next_minidump_path_c_;
259
bryner1217c1f2006-09-27 01:00:32 +0000260 HMODULE dbghelp_module_;
261 MiniDumpWriteDump_type minidump_write_dump_;
262
mmentovai6a844b12007-07-02 19:41:05 +0000263 // Tracks the handler types that were installed according to the
264 // handler_types constructor argument.
265 HandlerType handler_types_;
mmentovai65dbfcc2006-12-08 22:49:07 +0000266
267 // When installed_handler_ is true, previous_filter_ is the unhandled
268 // exception filter that was set prior to installing ExceptionHandler as
269 // the unhandled exception filter and pointing it to |this|. NULL indicates
270 // that there is no previous unhandled exception filter.
bryner1217c1f2006-09-27 01:00:32 +0000271 LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_;
mmentovai65dbfcc2006-12-08 22:49:07 +0000272
mmentovaibaff9382007-02-07 20:20:10 +0000273#if _MSC_VER >= 1400 // MSVC 2005/8
274 // Beginning in VC 8, the CRT provides an invalid parameter handler that will
275 // be called when some CRT functions are passed invalid parameters. In
276 // earlier CRTs, the same conditions would cause unexpected behavior or
277 // crashes.
278 _invalid_parameter_handler previous_iph_;
279#endif // _MSC_VER >= 1400
280
ted.mielczarekb86e7ec2007-05-10 17:12:14 +0000281 // The CRT allows you to override the default handler for pure
282 // virtual function calls.
283 _purecall_handler previous_pch_;
284
mmentovai283fd392006-12-07 20:46:54 +0000285 // The exception handler thread.
286 HANDLE handler_thread_;
mmentovaib2610192006-10-31 16:49:38 +0000287
288 // The critical section enforcing the requirement that only one exception be
mmentovai283fd392006-12-07 20:46:54 +0000289 // handled by a handler at a time.
290 CRITICAL_SECTION handler_critical_section_;
mmentovaib2610192006-10-31 16:49:38 +0000291
292 // Semaphores used to move exception handling between the exception thread
293 // and the handler thread. handler_start_semaphore_ is signalled by the
294 // exception thread to wake up the handler thread when an exception occurs.
295 // handler_finish_semaphore_ is signalled by the handler thread to wake up
296 // the exception thread when handling is complete.
mmentovai283fd392006-12-07 20:46:54 +0000297 HANDLE handler_start_semaphore_;
298 HANDLE handler_finish_semaphore_;
mmentovaib2610192006-10-31 16:49:38 +0000299
mmentovai283fd392006-12-07 20:46:54 +0000300 // The next 2 fields contain data passed from the requesting thread to
mmentovaib2610192006-10-31 16:49:38 +0000301 // the handler thread.
302
mmentovaib2610192006-10-31 16:49:38 +0000303 // The thread ID of the thread requesting the dump (either the exception
304 // thread or any other thread that called WriteMinidump directly).
mmentovai283fd392006-12-07 20:46:54 +0000305 DWORD requesting_thread_id_;
mmentovaib2610192006-10-31 16:49:38 +0000306
307 // The exception info passed to the exception handler on the exception
308 // thread, if an exception occurred. NULL for user-requested dumps.
mmentovai283fd392006-12-07 20:46:54 +0000309 EXCEPTION_POINTERS *exception_info_;
mmentovaib2610192006-10-31 16:49:38 +0000310
mmentovaibaff9382007-02-07 20:20:10 +0000311 // If the handler is invoked due to an assertion, this will contain a
312 // pointer to the assertion information. It is NULL at other times.
313 MDRawAssertionInfo *assertion_;
314
mmentovaib2610192006-10-31 16:49:38 +0000315 // The return value of the handler, passed from the handler thread back to
316 // the requesting thread.
mmentovai283fd392006-12-07 20:46:54 +0000317 bool handler_return_value_;
318
mmentovai65dbfcc2006-12-08 22:49:07 +0000319 // A stack of ExceptionHandler objects that have installed unhandled
320 // exception filters. This vector is used by HandleException to determine
321 // which ExceptionHandler object to route an exception to. When an
322 // ExceptionHandler is created with install_handler true, it will append
323 // itself to this list.
324 static vector<ExceptionHandler *> *handler_stack_;
325
326 // The index of the ExceptionHandler in handler_stack_ that will handle the
327 // next exception. Note that 0 means the last entry in handler_stack_, 1
328 // means the next-to-last entry, and so on. This is used by HandleException
mmentovaie5dc6082007-02-14 19:51:05 +0000329 // to support multiple stacked Breakpad handlers.
mmentovai65dbfcc2006-12-08 22:49:07 +0000330 static LONG handler_stack_index_;
331
332 // handler_stack_critical_section_ guards operations on handler_stack_ and
333 // handler_stack_index_.
mmentovai283fd392006-12-07 20:46:54 +0000334 static CRITICAL_SECTION handler_stack_critical_section_;
335
336 // True when handler_stack_critical_section_ has been initialized.
337 static bool handler_stack_critical_section_initialized_;
mmentovaib2610192006-10-31 16:49:38 +0000338
bryner1217c1f2006-09-27 01:00:32 +0000339 // disallow copy ctor and operator=
340 explicit ExceptionHandler(const ExceptionHandler &);
341 void operator=(const ExceptionHandler &);
342};
343
mmentovaie5dc6082007-02-14 19:51:05 +0000344} // namespace google_breakpad
bryner1217c1f2006-09-27 01:00:32 +0000345
mmentovai12a52452006-10-27 19:47:21 +0000346#pragma warning( pop )
347
bryner1217c1f2006-09-27 01:00:32 +0000348#endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__