michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // ExceptionHandler can write a minidump file when an exception occurs, michael@0: // or when WriteMinidump() is called explicitly by your program. michael@0: // michael@0: // To have the exception handler write minidumps when an uncaught exception michael@0: // (crash) occurs, you should create an instance early in the execution michael@0: // of your program, and keep it around for the entire time you want to michael@0: // have crash handling active (typically, until shutdown). michael@0: // michael@0: // If you want to write minidumps without installing the exception handler, michael@0: // you can create an ExceptionHandler with install_handler set to false, michael@0: // then call WriteMinidump. You can also use this technique if you want to michael@0: // use different minidump callbacks for different call sites. michael@0: // michael@0: // In either case, a callback function is called when a minidump is written, michael@0: // which receives the unqiue id of the minidump. The caller can use this michael@0: // id to collect and write additional application state, and to launch an michael@0: // external crash-reporting application. michael@0: // michael@0: // It is important that creation and destruction of ExceptionHandler objects michael@0: // be nested cleanly, when using install_handler = true. michael@0: // Avoid the following pattern: michael@0: // ExceptionHandler *e = new ExceptionHandler(...); michael@0: // ExceptionHandler *f = new ExceptionHandler(...); michael@0: // delete e; michael@0: // This will put the exception filter stack into an inconsistent state. michael@0: michael@0: #ifndef CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__ michael@0: #define CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #pragma warning( push ) michael@0: // Disable exception handler warnings. michael@0: #pragma warning( disable : 4530 ) michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "client/windows/common/ipc_protocol.h" michael@0: #include "client/windows/crash_generation/crash_generation_client.h" michael@0: #include "common/scoped_ptr.h" michael@0: #include "google_breakpad/common/minidump_format.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using std::vector; michael@0: using std::wstring; michael@0: michael@0: // These entries store a list of memory regions that the client wants included michael@0: // in the minidump. michael@0: struct AppMemory { michael@0: ULONG64 ptr; michael@0: ULONG length; michael@0: michael@0: bool operator==(const struct AppMemory& other) const { michael@0: return ptr == other.ptr; michael@0: } michael@0: michael@0: bool operator==(const void* other) const { michael@0: return ptr == reinterpret_cast(other); michael@0: } michael@0: }; michael@0: typedef std::list AppMemoryList; michael@0: michael@0: class ExceptionHandler { michael@0: public: michael@0: // A callback function to run before Breakpad performs any substantial michael@0: // processing of an exception. A FilterCallback is called before writing michael@0: // a minidump. context is the parameter supplied by the user as michael@0: // callback_context when the handler was created. exinfo points to the michael@0: // exception record, if any; assertion points to assertion information, michael@0: // if any. michael@0: // michael@0: // If a FilterCallback returns true, Breakpad will continue processing, michael@0: // attempting to write a minidump. If a FilterCallback returns false, michael@0: // Breakpad will immediately report the exception as unhandled without michael@0: // writing a minidump, allowing another handler the opportunity to handle it. michael@0: typedef bool (*FilterCallback)(void* context, EXCEPTION_POINTERS* exinfo, michael@0: MDRawAssertionInfo* assertion); michael@0: michael@0: // A callback function to run after the minidump has been written. michael@0: // minidump_id is a unique id for the dump, so the minidump michael@0: // file is \.dmp. context is the parameter supplied michael@0: // by the user as callback_context when the handler was created. exinfo michael@0: // points to the exception record, or NULL if no exception occurred. michael@0: // succeeded indicates whether a minidump file was successfully written. michael@0: // assertion points to information about an assertion if the handler was michael@0: // invoked by an assertion. michael@0: // michael@0: // If an exception occurred and the callback returns true, Breakpad will treat michael@0: // the exception as fully-handled, suppressing any other handlers from being michael@0: // notified of the exception. If the callback returns false, Breakpad will michael@0: // treat the exception as unhandled, and allow another handler to handle it. michael@0: // If there are no other handlers, Breakpad will report the exception to the michael@0: // system as unhandled, allowing a debugger or native crash dialog the michael@0: // opportunity to handle the exception. Most callback implementations michael@0: // should normally return the value of |succeeded|, or when they wish to michael@0: // not report an exception of handled, false. Callbacks will rarely want to michael@0: // return true directly (unless |succeeded| is true). michael@0: // michael@0: // For out-of-process dump generation, dump path and minidump ID will always michael@0: // be NULL. In case of out-of-process dump generation, the dump path and michael@0: // minidump id are controlled by the server process and are not communicated michael@0: // back to the crashing process. michael@0: typedef bool (*MinidumpCallback)(const wchar_t* dump_path, michael@0: const wchar_t* minidump_id, michael@0: void* context, michael@0: EXCEPTION_POINTERS* exinfo, michael@0: MDRawAssertionInfo* assertion, michael@0: bool succeeded); michael@0: michael@0: // HandlerType specifies which types of handlers should be installed, if michael@0: // any. Use HANDLER_NONE for an ExceptionHandler that remains idle, michael@0: // without catching any failures on its own. This type of handler may michael@0: // still be triggered by calling WriteMinidump. Otherwise, use a michael@0: // combination of the other HANDLER_ values, or HANDLER_ALL to install michael@0: // all handlers. michael@0: enum HandlerType { michael@0: HANDLER_NONE = 0, michael@0: HANDLER_EXCEPTION = 1 << 0, // SetUnhandledExceptionFilter michael@0: HANDLER_INVALID_PARAMETER = 1 << 1, // _set_invalid_parameter_handler michael@0: HANDLER_PURECALL = 1 << 2, // _set_purecall_handler michael@0: HANDLER_ALL = HANDLER_EXCEPTION | michael@0: HANDLER_INVALID_PARAMETER | michael@0: HANDLER_PURECALL michael@0: }; michael@0: michael@0: // Creates a new ExceptionHandler instance to handle writing minidumps. michael@0: // Before writing a minidump, the optional filter callback will be called. michael@0: // Its return value determines whether or not Breakpad should write a michael@0: // minidump. Minidump files will be written to dump_path, and the optional michael@0: // callback is called after writing the dump file, as described above. michael@0: // handler_types specifies the types of handlers that should be installed. michael@0: ExceptionHandler(const wstring& dump_path, michael@0: FilterCallback filter, michael@0: MinidumpCallback callback, michael@0: void* callback_context, michael@0: int handler_types); michael@0: michael@0: // Creates a new ExceptionHandler instance that can attempt to perform michael@0: // out-of-process dump generation if pipe_name is not NULL. If pipe_name is michael@0: // NULL, or if out-of-process dump generation registration step fails, michael@0: // in-process dump generation will be used. This also allows specifying michael@0: // the dump type to generate. michael@0: ExceptionHandler(const wstring& dump_path, michael@0: FilterCallback filter, michael@0: MinidumpCallback callback, michael@0: void* callback_context, michael@0: int handler_types, michael@0: MINIDUMP_TYPE dump_type, michael@0: const wchar_t* pipe_name, michael@0: const CustomClientInfo* custom_info); michael@0: michael@0: // As above, creates a new ExceptionHandler instance to perform michael@0: // out-of-process dump generation if the given pipe_handle is not NULL. michael@0: ExceptionHandler(const wstring& dump_path, michael@0: FilterCallback filter, michael@0: MinidumpCallback callback, michael@0: void* callback_context, michael@0: int handler_types, michael@0: MINIDUMP_TYPE dump_type, michael@0: HANDLE pipe_handle, michael@0: const CustomClientInfo* custom_info); michael@0: michael@0: ~ExceptionHandler(); michael@0: michael@0: // Get and set the minidump path. michael@0: wstring dump_path() const { return dump_path_; } michael@0: void set_dump_path(const wstring &dump_path) { michael@0: dump_path_ = dump_path; michael@0: dump_path_c_ = dump_path_.c_str(); michael@0: UpdateNextID(); // Necessary to put dump_path_ in next_minidump_path_. michael@0: } michael@0: michael@0: // Requests that a previously reported crash be uploaded. michael@0: bool RequestUpload(DWORD crash_id); michael@0: michael@0: // Writes a minidump immediately. This can be used to capture the michael@0: // execution state independently of a crash. Returns true on success. michael@0: bool WriteMinidump(); michael@0: michael@0: // Writes a minidump immediately, with the user-supplied exception michael@0: // information. michael@0: bool WriteMinidumpForException(EXCEPTION_POINTERS* exinfo); michael@0: michael@0: // Convenience form of WriteMinidump which does not require an michael@0: // ExceptionHandler instance. michael@0: static bool WriteMinidump(const wstring &dump_path, michael@0: MinidumpCallback callback, void* callback_context); michael@0: michael@0: // Write a minidump of |child| immediately. This can be used to michael@0: // capture the execution state of |child| independently of a crash. michael@0: // Pass a meaningful |child_blamed_thread| to make that thread in michael@0: // the child process the one from which a crash signature is michael@0: // extracted. michael@0: static bool WriteMinidumpForChild(HANDLE child, michael@0: DWORD child_blamed_thread, michael@0: const wstring& dump_path, michael@0: MinidumpCallback callback, michael@0: void* callback_context); michael@0: michael@0: // Get the thread ID of the thread requesting the dump (either the exception michael@0: // thread or any other thread that called WriteMinidump directly). This michael@0: // may be useful if you want to include additional thread state in your michael@0: // dumps. michael@0: DWORD get_requesting_thread_id() const { return requesting_thread_id_; } michael@0: michael@0: // Controls behavior of EXCEPTION_BREAKPOINT and EXCEPTION_SINGLE_STEP. michael@0: bool get_handle_debug_exceptions() const { return handle_debug_exceptions_; } michael@0: void set_handle_debug_exceptions(bool handle_debug_exceptions) { michael@0: handle_debug_exceptions_ = handle_debug_exceptions; michael@0: } michael@0: michael@0: // Returns whether out-of-process dump generation is used or not. michael@0: bool IsOutOfProcess() const { return crash_generation_client_.get() != NULL; } michael@0: michael@0: // Calling RegisterAppMemory(p, len) causes len bytes starting michael@0: // at address p to be copied to the minidump when a crash happens. michael@0: void RegisterAppMemory(void* ptr, size_t length); michael@0: void UnregisterAppMemory(void* ptr); michael@0: michael@0: private: michael@0: friend class AutoExceptionHandler; michael@0: michael@0: // Initializes the instance with given values. michael@0: void Initialize(const wstring& dump_path, michael@0: FilterCallback filter, michael@0: MinidumpCallback callback, michael@0: void* callback_context, michael@0: int handler_types, michael@0: MINIDUMP_TYPE dump_type, michael@0: const wchar_t* pipe_name, michael@0: HANDLE pipe_handle, michael@0: const CustomClientInfo* custom_info); michael@0: michael@0: // Function pointer type for MiniDumpWriteDump, which is looked up michael@0: // dynamically. michael@0: typedef BOOL (WINAPI *MiniDumpWriteDump_type)( michael@0: HANDLE hProcess, michael@0: DWORD dwPid, michael@0: HANDLE hFile, michael@0: MINIDUMP_TYPE DumpType, michael@0: CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, michael@0: CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, michael@0: CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); michael@0: michael@0: // Function pointer type for UuidCreate, which is looked up dynamically. michael@0: typedef RPC_STATUS (RPC_ENTRY *UuidCreate_type)(UUID* Uuid); michael@0: michael@0: // Runs the main loop for the exception handler thread. michael@0: static DWORD WINAPI ExceptionHandlerThreadMain(void* lpParameter); michael@0: michael@0: // Called on the exception thread when an unhandled exception occurs. michael@0: // Signals the exception handler thread to handle the exception. michael@0: static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo); michael@0: michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: // This function will be called by some CRT functions when they detect michael@0: // that they were passed an invalid parameter. Note that in _DEBUG builds, michael@0: // the CRT may display an assertion dialog before calling this function, michael@0: // and the function will not be called unless the assertion dialog is michael@0: // dismissed by clicking "Ignore." michael@0: static void HandleInvalidParameter(const wchar_t* expression, michael@0: const wchar_t* function, michael@0: const wchar_t* file, michael@0: unsigned int line, michael@0: uintptr_t reserved); michael@0: #endif // _MSC_VER >= 1400 michael@0: michael@0: // This function will be called by the CRT when a pure virtual michael@0: // function is called. michael@0: static void HandlePureVirtualCall(); michael@0: michael@0: // This is called on the exception thread or on another thread that michael@0: // the user wishes to produce a dump from. It calls michael@0: // WriteMinidumpWithException on the handler thread, avoiding stack michael@0: // overflows and inconsistent dumps due to writing the dump from michael@0: // the exception thread. If the dump is requested as a result of an michael@0: // exception, exinfo contains exception information, otherwise, it michael@0: // is NULL. If the dump is requested as a result of an assertion michael@0: // (such as an invalid parameter being passed to a CRT function), michael@0: // assertion contains data about the assertion, otherwise, it is NULL. michael@0: bool WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS* exinfo, michael@0: MDRawAssertionInfo* assertion); michael@0: michael@0: // This function is called on the handler thread. It calls into michael@0: // WriteMinidumpWithExceptionForProcess() with a handle to the michael@0: // current process. requesting_thread_id is the ID of the thread michael@0: // that requested the dump. If the dump is requested as a result of michael@0: // an exception, exinfo contains exception information, otherwise, michael@0: // it is NULL. michael@0: bool WriteMinidumpWithException(DWORD requesting_thread_id, michael@0: EXCEPTION_POINTERS* exinfo, michael@0: MDRawAssertionInfo* assertion); michael@0: michael@0: // This function is used as a callback when calling MinidumpWriteDump, michael@0: // in order to add additional memory regions to the dump. michael@0: static BOOL CALLBACK MinidumpWriteDumpCallback( michael@0: PVOID context, michael@0: const PMINIDUMP_CALLBACK_INPUT callback_input, michael@0: PMINIDUMP_CALLBACK_OUTPUT callback_output); michael@0: michael@0: // This function does the actual writing of a minidump. It is michael@0: // called on the handler thread. requesting_thread_id is the ID of michael@0: // the thread that requested the dump, if that information is michael@0: // meaningful. If the dump is requested as a result of an michael@0: // exception, exinfo contains exception information, otherwise, it michael@0: // is NULL. process is the one that will be dumped. If michael@0: // requesting_thread_id is meaningful and should be added to the michael@0: // minidump, write_requester_stream is |true|. michael@0: bool WriteMinidumpWithExceptionForProcess(DWORD requesting_thread_id, michael@0: EXCEPTION_POINTERS* exinfo, michael@0: MDRawAssertionInfo* assertion, michael@0: HANDLE process, michael@0: bool write_requester_stream); michael@0: michael@0: // Generates a new ID and stores it in next_minidump_id_, and stores the michael@0: // path of the next minidump to be written in next_minidump_path_. michael@0: void UpdateNextID(); michael@0: michael@0: FilterCallback filter_; michael@0: MinidumpCallback callback_; michael@0: void* callback_context_; michael@0: michael@0: scoped_ptr crash_generation_client_; michael@0: michael@0: // The directory in which a minidump will be written, set by the dump_path michael@0: // argument to the constructor, or set_dump_path. michael@0: wstring dump_path_; michael@0: michael@0: // The basename of the next minidump to be written, without the extension. michael@0: wstring next_minidump_id_; michael@0: michael@0: // The full pathname of the next minidump to be written, including the file michael@0: // extension. michael@0: wstring next_minidump_path_; michael@0: michael@0: // Pointers to C-string representations of the above. These are set when michael@0: // the above wstring versions are set in order to avoid calling c_str during michael@0: // an exception, as c_str may attempt to allocate heap memory. These michael@0: // pointers are not owned by the ExceptionHandler object, but their lifetimes michael@0: // should be equivalent to the lifetimes of the associated wstring, provided michael@0: // that the wstrings are not altered. michael@0: const wchar_t* dump_path_c_; michael@0: const wchar_t* next_minidump_id_c_; michael@0: const wchar_t* next_minidump_path_c_; michael@0: michael@0: HMODULE dbghelp_module_; michael@0: MiniDumpWriteDump_type minidump_write_dump_; michael@0: MINIDUMP_TYPE dump_type_; michael@0: michael@0: HMODULE rpcrt4_module_; michael@0: UuidCreate_type uuid_create_; michael@0: michael@0: // Tracks the handler types that were installed according to the michael@0: // handler_types constructor argument. michael@0: int handler_types_; michael@0: michael@0: // When installed_handler_ is true, previous_filter_ is the unhandled michael@0: // exception filter that was set prior to installing ExceptionHandler as michael@0: // the unhandled exception filter and pointing it to |this|. NULL indicates michael@0: // that there is no previous unhandled exception filter. michael@0: LPTOP_LEVEL_EXCEPTION_FILTER previous_filter_; michael@0: michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: // Beginning in VC 8, the CRT provides an invalid parameter handler that will michael@0: // be called when some CRT functions are passed invalid parameters. In michael@0: // earlier CRTs, the same conditions would cause unexpected behavior or michael@0: // crashes. michael@0: _invalid_parameter_handler previous_iph_; michael@0: #endif // _MSC_VER >= 1400 michael@0: michael@0: // The CRT allows you to override the default handler for pure michael@0: // virtual function calls. michael@0: _purecall_handler previous_pch_; michael@0: michael@0: // The exception handler thread. michael@0: HANDLE handler_thread_; michael@0: michael@0: // True if the exception handler is being destroyed. michael@0: // Starting with MSVC 2005, Visual C has stronger guarantees on volatile vars. michael@0: // It has release semantics on write and acquire semantics on reads. michael@0: // See the msdn documentation. michael@0: volatile bool is_shutdown_; michael@0: michael@0: // The critical section enforcing the requirement that only one exception be michael@0: // handled by a handler at a time. michael@0: CRITICAL_SECTION handler_critical_section_; michael@0: michael@0: // Semaphores used to move exception handling between the exception thread michael@0: // and the handler thread. handler_start_semaphore_ is signalled by the michael@0: // exception thread to wake up the handler thread when an exception occurs. michael@0: // handler_finish_semaphore_ is signalled by the handler thread to wake up michael@0: // the exception thread when handling is complete. michael@0: HANDLE handler_start_semaphore_; michael@0: HANDLE handler_finish_semaphore_; michael@0: michael@0: // The next 2 fields contain data passed from the requesting thread to michael@0: // the handler thread. michael@0: michael@0: // The thread ID of the thread requesting the dump (either the exception michael@0: // thread or any other thread that called WriteMinidump directly). michael@0: DWORD requesting_thread_id_; michael@0: michael@0: // The exception info passed to the exception handler on the exception michael@0: // thread, if an exception occurred. NULL for user-requested dumps. michael@0: EXCEPTION_POINTERS* exception_info_; michael@0: michael@0: // If the handler is invoked due to an assertion, this will contain a michael@0: // pointer to the assertion information. It is NULL at other times. michael@0: MDRawAssertionInfo* assertion_; michael@0: michael@0: // The return value of the handler, passed from the handler thread back to michael@0: // the requesting thread. michael@0: bool handler_return_value_; michael@0: michael@0: // If true, the handler will intercept EXCEPTION_BREAKPOINT and michael@0: // EXCEPTION_SINGLE_STEP exceptions. Leave this false (the default) michael@0: // to not interfere with debuggers. michael@0: bool handle_debug_exceptions_; michael@0: michael@0: // Callers can request additional memory regions to be included in michael@0: // the dump. michael@0: AppMemoryList app_memory_info_; michael@0: michael@0: // A stack of ExceptionHandler objects that have installed unhandled michael@0: // exception filters. This vector is used by HandleException to determine michael@0: // which ExceptionHandler object to route an exception to. When an michael@0: // ExceptionHandler is created with install_handler true, it will append michael@0: // itself to this list. michael@0: static vector* handler_stack_; michael@0: michael@0: // The index of the ExceptionHandler in handler_stack_ that will handle the michael@0: // next exception. Note that 0 means the last entry in handler_stack_, 1 michael@0: // means the next-to-last entry, and so on. This is used by HandleException michael@0: // to support multiple stacked Breakpad handlers. michael@0: static LONG handler_stack_index_; michael@0: michael@0: // handler_stack_critical_section_ guards operations on handler_stack_ and michael@0: // handler_stack_index_. The critical section is initialized by the michael@0: // first instance of the class and destroyed by the last instance of it. michael@0: static CRITICAL_SECTION handler_stack_critical_section_; michael@0: michael@0: // The number of instances of this class. michael@0: volatile static LONG instance_count_; michael@0: michael@0: // disallow copy ctor and operator= michael@0: explicit ExceptionHandler(const ExceptionHandler &); michael@0: void operator=(const ExceptionHandler &); michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #pragma warning( pop ) michael@0: michael@0: #endif // CLIENT_WINDOWS_HANDLER_EXCEPTION_HANDLER_H__