blob: 8f4f50a2e8727b247e5646f5ab8f575dcfb66f23 [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
mmentovai29401d22006-10-26 18:06:43 +000062#include <Windows.h>
63#include <DbgHelp.h>
bryner1217c1f2006-09-27 01:00:32 +000064
mmentovai12a52452006-10-27 19:47:21 +000065#pragma warning( push )
66// Disable exception handler warnings.
67#pragma warning( disable : 4530 )
68
bryner1217c1f2006-09-27 01:00:32 +000069#include <string>
mmentovai283fd392006-12-07 20:46:54 +000070#include <vector>
bryner1217c1f2006-09-27 01:00:32 +000071
72namespace google_airbag {
73
mmentovai283fd392006-12-07 20:46:54 +000074using std::vector;
bryner1217c1f2006-09-27 01:00:32 +000075using std::wstring;
76
77class ExceptionHandler {
78 public:
mmentovai283fd392006-12-07 20:46:54 +000079 // A callback function to run before Airbag performs any substantial
80 // processing of an exception. A FilterCallback is called before writing
81 // a minidump. context is the parameter supplied by the user as
82 // callback_context when the handler was created.
83 //
84 // If a FilterCallback returns true, Airbag will continue processing,
85 // attempting to write a minidump. If a FilterCallback returns false, Airbag
86 // will immediately report the exception as unhandled without writing a
87 // minidump, allowing another handler the opportunity to handle it.
88 typedef bool (*FilterCallback)(void *context);
89
bryner1217c1f2006-09-27 01:00:32 +000090 // A callback function to run after the minidump has been written.
91 // minidump_id is a unique id for the dump, so the minidump
mmentovai283fd392006-12-07 20:46:54 +000092 // file is <dump_path>\<minidump_id>.dmp. context is the parameter supplied
93 // by the user as callback_context when the handler was created. succeeded
94 // indicates whether a minidump file was successfully written.
95 //
96 // If an exception occurred and the callback returns true, Airbag will treat
97 // the exception as fully-handled, suppressing any other handlers from being
98 // notified of the exception. If the callback returns false, Airbag will
99 // treat the exception as unhandled, and allow another handler to handle it.
100 // If there are no other handlers, Airbag will report the exception to the
101 // system as unhandled, allowing a debugger or native crash dialog the
102 // opportunity to handle the exception. Most callback implementations
103 // should normally return the value of |succeeded|, or when they wish to
104 // not report an exception of handled, false. Callbacks will rarely want to
105 // return true directly (unless |succeeded| is true).
106 typedef bool (*MinidumpCallback)(const wchar_t *dump_path,
107 const wchar_t *minidump_id,
108 void *context,
109 bool succeeded);
bryner1217c1f2006-09-27 01:00:32 +0000110
111 // Creates a new ExceptionHandler instance to handle writing minidumps.
mmentovai283fd392006-12-07 20:46:54 +0000112 // Before writing a minidump, the optional filter callback will be called.
113 // Its return value determines whether or not Airbag should write a minidump.
bryner1217c1f2006-09-27 01:00:32 +0000114 // Minidump files will be written to dump_path, and the optional callback
115 // is called after writing the dump file, as described above.
116 // If install_handler is true, then a minidump will be written whenever
117 // an unhandled exception occurs. If it is false, minidumps will only
118 // be written when WriteMinidump is called.
mmentovai283fd392006-12-07 20:46:54 +0000119 ExceptionHandler(const wstring &dump_path,
120 FilterCallback filter, MinidumpCallback callback,
bryner1217c1f2006-09-27 01:00:32 +0000121 void *callback_context, bool install_handler);
122 ~ExceptionHandler();
123
mmentovai1bff57e2006-10-27 16:10:55 +0000124 // Get and set the minidump path.
125 wstring dump_path() const { return dump_path_; }
mmentovaied61ae02006-11-28 19:47:44 +0000126 void set_dump_path(const wstring &dump_path) {
127 dump_path_ = dump_path;
mmentovai283fd392006-12-07 20:46:54 +0000128 dump_path_c_ = dump_path_.c_str();
mmentovaied61ae02006-11-28 19:47:44 +0000129 UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_.
130 }
mmentovai1bff57e2006-10-27 16:10:55 +0000131
bryner1217c1f2006-09-27 01:00:32 +0000132 // Writes a minidump immediately. This can be used to capture the
133 // execution state independently of a crash. Returns true on success.
134 bool WriteMinidump();
135
136 // Convenience form of WriteMinidump which does not require an
137 // ExceptionHandler instance.
138 static bool WriteMinidump(const wstring &dump_path,
139 MinidumpCallback callback, void *callback_context);
140
141 private:
142 // Function pointer type for MiniDumpWriteDump, which is looked up
143 // dynamically.
144 typedef BOOL (WINAPI *MiniDumpWriteDump_type)(
145 HANDLE hProcess,
146 DWORD dwPid,
147 HANDLE hFile,
148 MINIDUMP_TYPE DumpType,
149 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
150 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
151 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
152
mmentovaib2610192006-10-31 16:49:38 +0000153 // Runs the main loop for the exception handler thread.
154 static DWORD WINAPI ExceptionHandlerThreadMain(void *lpParameter);
bryner1217c1f2006-09-27 01:00:32 +0000155
mmentovaib2610192006-10-31 16:49:38 +0000156 // Called on the exception thread when an unhandled exception occurs.
157 // Signals the exception handler thread to handle the exception.
bryner1217c1f2006-09-27 01:00:32 +0000158 static LONG WINAPI HandleException(EXCEPTION_POINTERS *exinfo);
159
mmentovaib2610192006-10-31 16:49:38 +0000160 // This is called on the exception thread or on another thread that
161 // the user wishes to produce a dump from. It calls
162 // WriteMinidumpWithException on the handler thread, avoiding stack
163 // overflows and inconsistent dumps due to writing the dump from
164 // the exception thread. If the dump is requested as a result of an
165 // exception, exinfo contains exception information, otherwise, it
166 // is NULL.
167 bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS *exinfo);
168
169 // This function does the actual writing of a minidump. It is called
170 // on the handler thread. requesting_thread_id is the ID of the thread
171 // that requested the dump. If the dump is requested as a result of
172 // an exception, exinfo contains exception information, otherwise,
173 // it is NULL.
174 bool WriteMinidumpWithException(DWORD requesting_thread_id,
175 EXCEPTION_POINTERS *exinfo);
176
mmentovaied61ae02006-11-28 19:47:44 +0000177 // Generates a new ID and stores it in next_minidump_id_, and stores the
178 // path of the next minidump to be written in next_minidump_path_.
bryner1217c1f2006-09-27 01:00:32 +0000179 void UpdateNextID();
180
mmentovai283fd392006-12-07 20:46:54 +0000181 FilterCallback filter_;
bryner1217c1f2006-09-27 01:00:32 +0000182 MinidumpCallback callback_;
183 void *callback_context_;
184
mmentovai283fd392006-12-07 20:46:54 +0000185 // The directory in which a minidump will be written, set by the dump_path
186 // argument to the constructor, or set_dump_path.
bryner1217c1f2006-09-27 01:00:32 +0000187 wstring dump_path_;
mmentovai283fd392006-12-07 20:46:54 +0000188
189 // The basename of the next minidump to be written, without the extension.
mmentovai29401d22006-10-26 18:06:43 +0000190 wstring next_minidump_id_;
mmentovai283fd392006-12-07 20:46:54 +0000191
192 // The full pathname of the next minidump to be written, including the file
193 // extension.
mmentovaied61ae02006-11-28 19:47:44 +0000194 wstring next_minidump_path_;
bryner1217c1f2006-09-27 01:00:32 +0000195
mmentovai283fd392006-12-07 20:46:54 +0000196 // Pointers to C-string representations of the above. These are set when
197 // the above wstring versions are set in order to avoid calling c_str during
198 // an exception, as c_str may attempt to allocate heap memory. These
199 // pointers are not owned by the ExceptionHandler object, but their lifetimes
200 // should be equivalent to the lifetimes of the associated wstring, provided
201 // that the wstrings are not altered.
202 const wchar_t *dump_path_c_;
203 const wchar_t *next_minidump_id_c_;
204 const wchar_t *next_minidump_path_c_;
205
bryner1217c1f2006-09-27 01:00:32 +0000206 HMODULE dbghelp_module_;
207 MiniDumpWriteDump_type minidump_write_dump_;
208
mmentovai65dbfcc2006-12-08 22:49:07 +0000209 // True if the ExceptionHandler installed an unhandled exception filter
210 // when created (with an install_handler parameter set to true).
211 bool installed_handler_;
212
213 // When installed_handler_ is true, previous_filter_ is the unhandled
214 // exception filter that was set prior to installing ExceptionHandler as
215 // the unhandled exception filter and pointing it to |this|. NULL indicates
216 // that there is no previous unhandled exception filter.
bryner1217c1f2006-09-27 01:00:32 +0000217 LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_;
mmentovai65dbfcc2006-12-08 22:49:07 +0000218
mmentovai283fd392006-12-07 20:46:54 +0000219 // The exception handler thread.
220 HANDLE handler_thread_;
mmentovaib2610192006-10-31 16:49:38 +0000221
222 // The critical section enforcing the requirement that only one exception be
mmentovai283fd392006-12-07 20:46:54 +0000223 // handled by a handler at a time.
224 CRITICAL_SECTION handler_critical_section_;
mmentovaib2610192006-10-31 16:49:38 +0000225
226 // Semaphores used to move exception handling between the exception thread
227 // and the handler thread. handler_start_semaphore_ is signalled by the
228 // exception thread to wake up the handler thread when an exception occurs.
229 // handler_finish_semaphore_ is signalled by the handler thread to wake up
230 // the exception thread when handling is complete.
mmentovai283fd392006-12-07 20:46:54 +0000231 HANDLE handler_start_semaphore_;
232 HANDLE handler_finish_semaphore_;
mmentovaib2610192006-10-31 16:49:38 +0000233
mmentovai283fd392006-12-07 20:46:54 +0000234 // The next 2 fields contain data passed from the requesting thread to
mmentovaib2610192006-10-31 16:49:38 +0000235 // the handler thread.
236
mmentovaib2610192006-10-31 16:49:38 +0000237 // The thread ID of the thread requesting the dump (either the exception
238 // thread or any other thread that called WriteMinidump directly).
mmentovai283fd392006-12-07 20:46:54 +0000239 DWORD requesting_thread_id_;
mmentovaib2610192006-10-31 16:49:38 +0000240
241 // The exception info passed to the exception handler on the exception
242 // thread, if an exception occurred. NULL for user-requested dumps.
mmentovai283fd392006-12-07 20:46:54 +0000243 EXCEPTION_POINTERS *exception_info_;
mmentovaib2610192006-10-31 16:49:38 +0000244
245 // The return value of the handler, passed from the handler thread back to
246 // the requesting thread.
mmentovai283fd392006-12-07 20:46:54 +0000247 bool handler_return_value_;
248
mmentovai65dbfcc2006-12-08 22:49:07 +0000249 // A stack of ExceptionHandler objects that have installed unhandled
250 // exception filters. This vector is used by HandleException to determine
251 // which ExceptionHandler object to route an exception to. When an
252 // ExceptionHandler is created with install_handler true, it will append
253 // itself to this list.
254 static vector<ExceptionHandler *> *handler_stack_;
255
256 // The index of the ExceptionHandler in handler_stack_ that will handle the
257 // next exception. Note that 0 means the last entry in handler_stack_, 1
258 // means the next-to-last entry, and so on. This is used by HandleException
259 // to support multiple stacked Airbag handlers.
260 static LONG handler_stack_index_;
261
262 // handler_stack_critical_section_ guards operations on handler_stack_ and
263 // handler_stack_index_.
mmentovai283fd392006-12-07 20:46:54 +0000264 static CRITICAL_SECTION handler_stack_critical_section_;
265
266 // True when handler_stack_critical_section_ has been initialized.
267 static bool handler_stack_critical_section_initialized_;
mmentovaib2610192006-10-31 16:49:38 +0000268
bryner1217c1f2006-09-27 01:00:32 +0000269 // disallow copy ctor and operator=
270 explicit ExceptionHandler(const ExceptionHandler &);
271 void operator=(const ExceptionHandler &);
272};
273
274} // namespace google_airbag
275
mmentovai12a52452006-10-27 19:47:21 +0000276#pragma warning( pop )
277
bryner1217c1f2006-09-27 01:00:32 +0000278#endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__