blob: e4b3f3bb2a374fe485ecab17cb307de168d52772 [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
122 // Creates a new ExceptionHandler instance to handle writing minidumps.
mmentovai283fd392006-12-07 20:46:54 +0000123 // Before writing a minidump, the optional filter callback will be called.
mmentovaie5dc6082007-02-14 19:51:05 +0000124 // Its return value determines whether or not Breakpad should write a minidump.
bryner1217c1f2006-09-27 01:00:32 +0000125 // Minidump files will be written to dump_path, and the optional callback
126 // is called after writing the dump file, as described above.
127 // If install_handler is true, then a minidump will be written whenever
128 // an unhandled exception occurs. If it is false, minidumps will only
129 // be written when WriteMinidump is called.
mmentovai283fd392006-12-07 20:46:54 +0000130 ExceptionHandler(const wstring &dump_path,
131 FilterCallback filter, MinidumpCallback callback,
bryner1217c1f2006-09-27 01:00:32 +0000132 void *callback_context, bool install_handler);
133 ~ExceptionHandler();
134
mmentovai1bff57e2006-10-27 16:10:55 +0000135 // Get and set the minidump path.
136 wstring dump_path() const { return dump_path_; }
mmentovaied61ae02006-11-28 19:47:44 +0000137 void set_dump_path(const wstring &dump_path) {
138 dump_path_ = dump_path;
mmentovai283fd392006-12-07 20:46:54 +0000139 dump_path_c_ = dump_path_.c_str();
mmentovaied61ae02006-11-28 19:47:44 +0000140 UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_.
141 }
mmentovai1bff57e2006-10-27 16:10:55 +0000142
bryner1217c1f2006-09-27 01:00:32 +0000143 // Writes a minidump immediately. This can be used to capture the
144 // execution state independently of a crash. Returns true on success.
145 bool WriteMinidump();
146
147 // Convenience form of WriteMinidump which does not require an
148 // ExceptionHandler instance.
149 static bool WriteMinidump(const wstring &dump_path,
150 MinidumpCallback callback, void *callback_context);
151
152 private:
mmentovaibaff9382007-02-07 20:20:10 +0000153 friend class AutoExceptionHandler;
154
bryner1217c1f2006-09-27 01:00:32 +0000155 // Function pointer type for MiniDumpWriteDump, which is looked up
156 // dynamically.
157 typedef BOOL (WINAPI *MiniDumpWriteDump_type)(
158 HANDLE hProcess,
159 DWORD dwPid,
160 HANDLE hFile,
161 MINIDUMP_TYPE DumpType,
162 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
163 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
164 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
165
mmentovaib2610192006-10-31 16:49:38 +0000166 // Runs the main loop for the exception handler thread.
167 static DWORD WINAPI ExceptionHandlerThreadMain(void *lpParameter);
bryner1217c1f2006-09-27 01:00:32 +0000168
mmentovaib2610192006-10-31 16:49:38 +0000169 // Called on the exception thread when an unhandled exception occurs.
170 // Signals the exception handler thread to handle the exception.
bryner1217c1f2006-09-27 01:00:32 +0000171 static LONG WINAPI HandleException(EXCEPTION_POINTERS *exinfo);
172
mmentovaibaff9382007-02-07 20:20:10 +0000173#if _MSC_VER >= 1400 // MSVC 2005/8
174 // This function will be called by some CRT functions when they detect
175 // that they were passed an invalid parameter. Note that in _DEBUG builds,
176 // the CRT may display an assertion dialog before calling this function,
177 // and the function will not be called unless the assertion dialog is
178 // dismissed by clicking "Ignore."
179 static void HandleInvalidParameter(const wchar_t *expression,
180 const wchar_t *function,
181 const wchar_t *file,
182 unsigned int line,
183 uintptr_t reserved);
184#endif // _MSC_VER >= 1400
185
ted.mielczarekb86e7ec2007-05-10 17:12:14 +0000186 // This function will be called by the CRT when a pure virtual
187 // function is called.
188 static void HandlePureVirtualCall();
189
mmentovaib2610192006-10-31 16:49:38 +0000190 // This is called on the exception thread or on another thread that
191 // the user wishes to produce a dump from. It calls
192 // WriteMinidumpWithException on the handler thread, avoiding stack
193 // overflows and inconsistent dumps due to writing the dump from
194 // the exception thread. If the dump is requested as a result of an
195 // exception, exinfo contains exception information, otherwise, it
mmentovaibaff9382007-02-07 20:20:10 +0000196 // is NULL. If the dump is requested as a result of an assertion
197 // (such as an invalid parameter being passed to a CRT function),
198 // assertion contains data about the assertion, otherwise, it is NULL.
199 bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS *exinfo,
200 MDRawAssertionInfo *assertion);
mmentovaib2610192006-10-31 16:49:38 +0000201
202 // This function does the actual writing of a minidump. It is called
203 // on the handler thread. requesting_thread_id is the ID of the thread
204 // that requested the dump. If the dump is requested as a result of
205 // an exception, exinfo contains exception information, otherwise,
206 // it is NULL.
207 bool WriteMinidumpWithException(DWORD requesting_thread_id,
mmentovaibaff9382007-02-07 20:20:10 +0000208 EXCEPTION_POINTERS *exinfo,
209 MDRawAssertionInfo *assertion);
mmentovaib2610192006-10-31 16:49:38 +0000210
mmentovaied61ae02006-11-28 19:47:44 +0000211 // Generates a new ID and stores it in next_minidump_id_, and stores the
212 // path of the next minidump to be written in next_minidump_path_.
bryner1217c1f2006-09-27 01:00:32 +0000213 void UpdateNextID();
214
mmentovai283fd392006-12-07 20:46:54 +0000215 FilterCallback filter_;
bryner1217c1f2006-09-27 01:00:32 +0000216 MinidumpCallback callback_;
217 void *callback_context_;
218
mmentovai283fd392006-12-07 20:46:54 +0000219 // The directory in which a minidump will be written, set by the dump_path
220 // argument to the constructor, or set_dump_path.
bryner1217c1f2006-09-27 01:00:32 +0000221 wstring dump_path_;
mmentovai283fd392006-12-07 20:46:54 +0000222
223 // The basename of the next minidump to be written, without the extension.
mmentovai29401d22006-10-26 18:06:43 +0000224 wstring next_minidump_id_;
mmentovai283fd392006-12-07 20:46:54 +0000225
226 // The full pathname of the next minidump to be written, including the file
227 // extension.
mmentovaied61ae02006-11-28 19:47:44 +0000228 wstring next_minidump_path_;
bryner1217c1f2006-09-27 01:00:32 +0000229
mmentovai283fd392006-12-07 20:46:54 +0000230 // Pointers to C-string representations of the above. These are set when
231 // the above wstring versions are set in order to avoid calling c_str during
232 // an exception, as c_str may attempt to allocate heap memory. These
233 // pointers are not owned by the ExceptionHandler object, but their lifetimes
234 // should be equivalent to the lifetimes of the associated wstring, provided
235 // that the wstrings are not altered.
236 const wchar_t *dump_path_c_;
237 const wchar_t *next_minidump_id_c_;
238 const wchar_t *next_minidump_path_c_;
239
bryner1217c1f2006-09-27 01:00:32 +0000240 HMODULE dbghelp_module_;
241 MiniDumpWriteDump_type minidump_write_dump_;
242
mmentovai65dbfcc2006-12-08 22:49:07 +0000243 // True if the ExceptionHandler installed an unhandled exception filter
244 // when created (with an install_handler parameter set to true).
245 bool installed_handler_;
246
247 // When installed_handler_ is true, previous_filter_ is the unhandled
248 // exception filter that was set prior to installing ExceptionHandler as
249 // the unhandled exception filter and pointing it to |this|. NULL indicates
250 // that there is no previous unhandled exception filter.
bryner1217c1f2006-09-27 01:00:32 +0000251 LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_;
mmentovai65dbfcc2006-12-08 22:49:07 +0000252
mmentovaibaff9382007-02-07 20:20:10 +0000253#if _MSC_VER >= 1400 // MSVC 2005/8
254 // Beginning in VC 8, the CRT provides an invalid parameter handler that will
255 // be called when some CRT functions are passed invalid parameters. In
256 // earlier CRTs, the same conditions would cause unexpected behavior or
257 // crashes.
258 _invalid_parameter_handler previous_iph_;
259#endif // _MSC_VER >= 1400
260
ted.mielczarekb86e7ec2007-05-10 17:12:14 +0000261 // The CRT allows you to override the default handler for pure
262 // virtual function calls.
263 _purecall_handler previous_pch_;
264
mmentovai283fd392006-12-07 20:46:54 +0000265 // The exception handler thread.
266 HANDLE handler_thread_;
mmentovaib2610192006-10-31 16:49:38 +0000267
268 // The critical section enforcing the requirement that only one exception be
mmentovai283fd392006-12-07 20:46:54 +0000269 // handled by a handler at a time.
270 CRITICAL_SECTION handler_critical_section_;
mmentovaib2610192006-10-31 16:49:38 +0000271
272 // Semaphores used to move exception handling between the exception thread
273 // and the handler thread. handler_start_semaphore_ is signalled by the
274 // exception thread to wake up the handler thread when an exception occurs.
275 // handler_finish_semaphore_ is signalled by the handler thread to wake up
276 // the exception thread when handling is complete.
mmentovai283fd392006-12-07 20:46:54 +0000277 HANDLE handler_start_semaphore_;
278 HANDLE handler_finish_semaphore_;
mmentovaib2610192006-10-31 16:49:38 +0000279
mmentovai283fd392006-12-07 20:46:54 +0000280 // The next 2 fields contain data passed from the requesting thread to
mmentovaib2610192006-10-31 16:49:38 +0000281 // the handler thread.
282
mmentovaib2610192006-10-31 16:49:38 +0000283 // The thread ID of the thread requesting the dump (either the exception
284 // thread or any other thread that called WriteMinidump directly).
mmentovai283fd392006-12-07 20:46:54 +0000285 DWORD requesting_thread_id_;
mmentovaib2610192006-10-31 16:49:38 +0000286
287 // The exception info passed to the exception handler on the exception
288 // thread, if an exception occurred. NULL for user-requested dumps.
mmentovai283fd392006-12-07 20:46:54 +0000289 EXCEPTION_POINTERS *exception_info_;
mmentovaib2610192006-10-31 16:49:38 +0000290
mmentovaibaff9382007-02-07 20:20:10 +0000291 // If the handler is invoked due to an assertion, this will contain a
292 // pointer to the assertion information. It is NULL at other times.
293 MDRawAssertionInfo *assertion_;
294
mmentovaib2610192006-10-31 16:49:38 +0000295 // The return value of the handler, passed from the handler thread back to
296 // the requesting thread.
mmentovai283fd392006-12-07 20:46:54 +0000297 bool handler_return_value_;
298
mmentovai65dbfcc2006-12-08 22:49:07 +0000299 // A stack of ExceptionHandler objects that have installed unhandled
300 // exception filters. This vector is used by HandleException to determine
301 // which ExceptionHandler object to route an exception to. When an
302 // ExceptionHandler is created with install_handler true, it will append
303 // itself to this list.
304 static vector<ExceptionHandler *> *handler_stack_;
305
306 // The index of the ExceptionHandler in handler_stack_ that will handle the
307 // next exception. Note that 0 means the last entry in handler_stack_, 1
308 // means the next-to-last entry, and so on. This is used by HandleException
mmentovaie5dc6082007-02-14 19:51:05 +0000309 // to support multiple stacked Breakpad handlers.
mmentovai65dbfcc2006-12-08 22:49:07 +0000310 static LONG handler_stack_index_;
311
312 // handler_stack_critical_section_ guards operations on handler_stack_ and
313 // handler_stack_index_.
mmentovai283fd392006-12-07 20:46:54 +0000314 static CRITICAL_SECTION handler_stack_critical_section_;
315
316 // True when handler_stack_critical_section_ has been initialized.
317 static bool handler_stack_critical_section_initialized_;
mmentovaib2610192006-10-31 16:49:38 +0000318
bryner1217c1f2006-09-27 01:00:32 +0000319 // disallow copy ctor and operator=
320 explicit ExceptionHandler(const ExceptionHandler &);
321 void operator=(const ExceptionHandler &);
322};
323
mmentovaie5dc6082007-02-14 19:51:05 +0000324} // namespace google_breakpad
bryner1217c1f2006-09-27 01:00:32 +0000325
mmentovai12a52452006-10-27 19:47:21 +0000326#pragma warning( pop )
327
bryner1217c1f2006-09-27 01:00:32 +0000328#endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__