blob: 330911891f40635b47eb4f90a78b42e004f2f489 [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
mmentovaif614cb92007-01-12 16:54:10 +000082 // callback_context when the handler was created. exinfo points to the
83 // exception record.
mmentovai283fd392006-12-07 20:46:54 +000084 //
85 // If a FilterCallback returns true, Airbag will continue processing,
86 // attempting to write a minidump. If a FilterCallback returns false, Airbag
87 // will immediately report the exception as unhandled without writing a
88 // minidump, allowing another handler the opportunity to handle it.
mmentovaif614cb92007-01-12 16:54:10 +000089 typedef bool (*FilterCallback)(void *context, EXCEPTION_POINTERS *exinfo);
mmentovai283fd392006-12-07 20:46:54 +000090
bryner1217c1f2006-09-27 01:00:32 +000091 // A callback function to run after the minidump has been written.
92 // minidump_id is a unique id for the dump, so the minidump
mmentovai283fd392006-12-07 20:46:54 +000093 // file is <dump_path>\<minidump_id>.dmp. context is the parameter supplied
mmentovaif614cb92007-01-12 16:54:10 +000094 // by the user as callback_context when the handler was created. exinfo
95 // points to the exception record, or NULL if no exception occurred.
96 // succeeded indicates whether a minidump file was successfully written.
mmentovai283fd392006-12-07 20:46:54 +000097 //
98 // If an exception occurred and the callback returns true, Airbag will treat
99 // the exception as fully-handled, suppressing any other handlers from being
100 // notified of the exception. If the callback returns false, Airbag will
101 // treat the exception as unhandled, and allow another handler to handle it.
102 // If there are no other handlers, Airbag will report the exception to the
103 // system as unhandled, allowing a debugger or native crash dialog the
104 // opportunity to handle the exception. Most callback implementations
105 // should normally return the value of |succeeded|, or when they wish to
106 // not report an exception of handled, false. Callbacks will rarely want to
107 // return true directly (unless |succeeded| is true).
108 typedef bool (*MinidumpCallback)(const wchar_t *dump_path,
109 const wchar_t *minidump_id,
110 void *context,
mmentovaif614cb92007-01-12 16:54:10 +0000111 EXCEPTION_POINTERS *exinfo,
mmentovai283fd392006-12-07 20:46:54 +0000112 bool succeeded);
bryner1217c1f2006-09-27 01:00:32 +0000113
114 // Creates a new ExceptionHandler instance to handle writing minidumps.
mmentovai283fd392006-12-07 20:46:54 +0000115 // Before writing a minidump, the optional filter callback will be called.
116 // Its return value determines whether or not Airbag should write a minidump.
bryner1217c1f2006-09-27 01:00:32 +0000117 // Minidump files will be written to dump_path, and the optional callback
118 // is called after writing the dump file, as described above.
119 // If install_handler is true, then a minidump will be written whenever
120 // an unhandled exception occurs. If it is false, minidumps will only
121 // be written when WriteMinidump is called.
mmentovai283fd392006-12-07 20:46:54 +0000122 ExceptionHandler(const wstring &dump_path,
123 FilterCallback filter, MinidumpCallback callback,
bryner1217c1f2006-09-27 01:00:32 +0000124 void *callback_context, bool install_handler);
125 ~ExceptionHandler();
126
mmentovai1bff57e2006-10-27 16:10:55 +0000127 // Get and set the minidump path.
128 wstring dump_path() const { return dump_path_; }
mmentovaied61ae02006-11-28 19:47:44 +0000129 void set_dump_path(const wstring &dump_path) {
130 dump_path_ = dump_path;
mmentovai283fd392006-12-07 20:46:54 +0000131 dump_path_c_ = dump_path_.c_str();
mmentovaied61ae02006-11-28 19:47:44 +0000132 UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_.
133 }
mmentovai1bff57e2006-10-27 16:10:55 +0000134
bryner1217c1f2006-09-27 01:00:32 +0000135 // Writes a minidump immediately. This can be used to capture the
136 // execution state independently of a crash. Returns true on success.
137 bool WriteMinidump();
138
139 // Convenience form of WriteMinidump which does not require an
140 // ExceptionHandler instance.
141 static bool WriteMinidump(const wstring &dump_path,
142 MinidumpCallback callback, void *callback_context);
143
144 private:
145 // Function pointer type for MiniDumpWriteDump, which is looked up
146 // dynamically.
147 typedef BOOL (WINAPI *MiniDumpWriteDump_type)(
148 HANDLE hProcess,
149 DWORD dwPid,
150 HANDLE hFile,
151 MINIDUMP_TYPE DumpType,
152 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
153 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
154 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
155
mmentovaib2610192006-10-31 16:49:38 +0000156 // Runs the main loop for the exception handler thread.
157 static DWORD WINAPI ExceptionHandlerThreadMain(void *lpParameter);
bryner1217c1f2006-09-27 01:00:32 +0000158
mmentovaib2610192006-10-31 16:49:38 +0000159 // Called on the exception thread when an unhandled exception occurs.
160 // Signals the exception handler thread to handle the exception.
bryner1217c1f2006-09-27 01:00:32 +0000161 static LONG WINAPI HandleException(EXCEPTION_POINTERS *exinfo);
162
mmentovaib2610192006-10-31 16:49:38 +0000163 // This is called on the exception thread or on another thread that
164 // the user wishes to produce a dump from. It calls
165 // WriteMinidumpWithException on the handler thread, avoiding stack
166 // overflows and inconsistent dumps due to writing the dump from
167 // the exception thread. If the dump is requested as a result of an
168 // exception, exinfo contains exception information, otherwise, it
169 // is NULL.
170 bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS *exinfo);
171
172 // This function does the actual writing of a minidump. It is called
173 // on the handler thread. requesting_thread_id is the ID of the thread
174 // that requested the dump. If the dump is requested as a result of
175 // an exception, exinfo contains exception information, otherwise,
176 // it is NULL.
177 bool WriteMinidumpWithException(DWORD requesting_thread_id,
178 EXCEPTION_POINTERS *exinfo);
179
mmentovaied61ae02006-11-28 19:47:44 +0000180 // Generates a new ID and stores it in next_minidump_id_, and stores the
181 // path of the next minidump to be written in next_minidump_path_.
bryner1217c1f2006-09-27 01:00:32 +0000182 void UpdateNextID();
183
mmentovai283fd392006-12-07 20:46:54 +0000184 FilterCallback filter_;
bryner1217c1f2006-09-27 01:00:32 +0000185 MinidumpCallback callback_;
186 void *callback_context_;
187
mmentovai283fd392006-12-07 20:46:54 +0000188 // The directory in which a minidump will be written, set by the dump_path
189 // argument to the constructor, or set_dump_path.
bryner1217c1f2006-09-27 01:00:32 +0000190 wstring dump_path_;
mmentovai283fd392006-12-07 20:46:54 +0000191
192 // The basename of the next minidump to be written, without the extension.
mmentovai29401d22006-10-26 18:06:43 +0000193 wstring next_minidump_id_;
mmentovai283fd392006-12-07 20:46:54 +0000194
195 // The full pathname of the next minidump to be written, including the file
196 // extension.
mmentovaied61ae02006-11-28 19:47:44 +0000197 wstring next_minidump_path_;
bryner1217c1f2006-09-27 01:00:32 +0000198
mmentovai283fd392006-12-07 20:46:54 +0000199 // Pointers to C-string representations of the above. These are set when
200 // the above wstring versions are set in order to avoid calling c_str during
201 // an exception, as c_str may attempt to allocate heap memory. These
202 // pointers are not owned by the ExceptionHandler object, but their lifetimes
203 // should be equivalent to the lifetimes of the associated wstring, provided
204 // that the wstrings are not altered.
205 const wchar_t *dump_path_c_;
206 const wchar_t *next_minidump_id_c_;
207 const wchar_t *next_minidump_path_c_;
208
bryner1217c1f2006-09-27 01:00:32 +0000209 HMODULE dbghelp_module_;
210 MiniDumpWriteDump_type minidump_write_dump_;
211
mmentovai65dbfcc2006-12-08 22:49:07 +0000212 // True if the ExceptionHandler installed an unhandled exception filter
213 // when created (with an install_handler parameter set to true).
214 bool installed_handler_;
215
216 // When installed_handler_ is true, previous_filter_ is the unhandled
217 // exception filter that was set prior to installing ExceptionHandler as
218 // the unhandled exception filter and pointing it to |this|. NULL indicates
219 // that there is no previous unhandled exception filter.
bryner1217c1f2006-09-27 01:00:32 +0000220 LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_;
mmentovai65dbfcc2006-12-08 22:49:07 +0000221
mmentovai283fd392006-12-07 20:46:54 +0000222 // The exception handler thread.
223 HANDLE handler_thread_;
mmentovaib2610192006-10-31 16:49:38 +0000224
225 // The critical section enforcing the requirement that only one exception be
mmentovai283fd392006-12-07 20:46:54 +0000226 // handled by a handler at a time.
227 CRITICAL_SECTION handler_critical_section_;
mmentovaib2610192006-10-31 16:49:38 +0000228
229 // Semaphores used to move exception handling between the exception thread
230 // and the handler thread. handler_start_semaphore_ is signalled by the
231 // exception thread to wake up the handler thread when an exception occurs.
232 // handler_finish_semaphore_ is signalled by the handler thread to wake up
233 // the exception thread when handling is complete.
mmentovai283fd392006-12-07 20:46:54 +0000234 HANDLE handler_start_semaphore_;
235 HANDLE handler_finish_semaphore_;
mmentovaib2610192006-10-31 16:49:38 +0000236
mmentovai283fd392006-12-07 20:46:54 +0000237 // The next 2 fields contain data passed from the requesting thread to
mmentovaib2610192006-10-31 16:49:38 +0000238 // the handler thread.
239
mmentovaib2610192006-10-31 16:49:38 +0000240 // The thread ID of the thread requesting the dump (either the exception
241 // thread or any other thread that called WriteMinidump directly).
mmentovai283fd392006-12-07 20:46:54 +0000242 DWORD requesting_thread_id_;
mmentovaib2610192006-10-31 16:49:38 +0000243
244 // The exception info passed to the exception handler on the exception
245 // thread, if an exception occurred. NULL for user-requested dumps.
mmentovai283fd392006-12-07 20:46:54 +0000246 EXCEPTION_POINTERS *exception_info_;
mmentovaib2610192006-10-31 16:49:38 +0000247
248 // The return value of the handler, passed from the handler thread back to
249 // the requesting thread.
mmentovai283fd392006-12-07 20:46:54 +0000250 bool handler_return_value_;
251
mmentovai65dbfcc2006-12-08 22:49:07 +0000252 // A stack of ExceptionHandler objects that have installed unhandled
253 // exception filters. This vector is used by HandleException to determine
254 // which ExceptionHandler object to route an exception to. When an
255 // ExceptionHandler is created with install_handler true, it will append
256 // itself to this list.
257 static vector<ExceptionHandler *> *handler_stack_;
258
259 // The index of the ExceptionHandler in handler_stack_ that will handle the
260 // next exception. Note that 0 means the last entry in handler_stack_, 1
261 // means the next-to-last entry, and so on. This is used by HandleException
262 // to support multiple stacked Airbag handlers.
263 static LONG handler_stack_index_;
264
265 // handler_stack_critical_section_ guards operations on handler_stack_ and
266 // handler_stack_index_.
mmentovai283fd392006-12-07 20:46:54 +0000267 static CRITICAL_SECTION handler_stack_critical_section_;
268
269 // True when handler_stack_critical_section_ has been initialized.
270 static bool handler_stack_critical_section_initialized_;
mmentovaib2610192006-10-31 16:49:38 +0000271
bryner1217c1f2006-09-27 01:00:32 +0000272 // disallow copy ctor and operator=
273 explicit ExceptionHandler(const ExceptionHandler &);
274 void operator=(const ExceptionHandler &);
275};
276
277} // namespace google_airbag
278
mmentovai12a52452006-10-27 19:47:21 +0000279#pragma warning( pop )
280
bryner1217c1f2006-09-27 01:00:32 +0000281#endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__