1 // Copyright 2008 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #ifndef CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_ 30 #define CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_ 31 32 #include <windows.h> 33 #include <dbghelp.h> 34 #include <rpc.h> 35 #include <list> 36 #include <string> 37 #include "google_breakpad/common/minidump_format.h" 38 39 namespace google_breakpad { 40 41 // Abstraction for various objects and operations needed to generate 42 // minidump on Windows. This abstraction is useful to hide all the gory 43 // details for minidump generation and provide a clean interface to 44 // the clients to generate minidumps. 45 class MinidumpGenerator { 46 public: 47 // Creates an instance with the given parameters. 48 // is_client_pointers specifies whether the exception_pointers and 49 // assert_info point into the process that is being dumped. 50 // Before calling WriteMinidump on the returned instance a dump file muct be 51 // specified by a call to either SetDumpFile() or GenerateDumpFile(). 52 // If a full dump file will be requested via a subsequent call to either 53 // SetFullDumpFile or GenerateFullDumpFile() dump_type must include 54 // MiniDumpWithFullMemory. 55 MinidumpGenerator(const std::wstring& dump_path, 56 const HANDLE process_handle, 57 const DWORD process_id, 58 const DWORD thread_id, 59 const DWORD requesting_thread_id, 60 EXCEPTION_POINTERS* exception_pointers, 61 MDRawAssertionInfo* assert_info, 62 const MINIDUMP_TYPE dump_type, 63 const bool is_client_pointers); 64 65 ~MinidumpGenerator(); 66 SetDumpFile(const HANDLE dump_file)67 void SetDumpFile(const HANDLE dump_file) { dump_file_ = dump_file; } SetFullDumpFile(const HANDLE full_dump_file)68 void SetFullDumpFile(const HANDLE full_dump_file) { 69 full_dump_file_ = full_dump_file; 70 } 71 72 // Generate the name for the dump file that will be written to once 73 // WriteMinidump() is called. Can only be called once and cannot be called 74 // if the dump file is set via SetDumpFile(). 75 bool GenerateDumpFile(std::wstring* dump_path); 76 77 // Generate the name for the full dump file that will be written to once 78 // WriteMinidump() is called. Cannot be called unless the minidump type 79 // includes MiniDumpWithFullMemory. Can only be called once and cannot be 80 // called if the dump file is set via SetFullDumpFile(). 81 bool GenerateFullDumpFile(std::wstring* full_dump_path); 82 SetAdditionalStreams(MINIDUMP_USER_STREAM_INFORMATION * additional_streams)83 void SetAdditionalStreams( 84 MINIDUMP_USER_STREAM_INFORMATION* additional_streams) { 85 additional_streams_ = additional_streams; 86 } 87 SetCallback(MINIDUMP_CALLBACK_INFORMATION * callback_info)88 void SetCallback(MINIDUMP_CALLBACK_INFORMATION* callback_info) { 89 callback_info_ = callback_info; 90 } 91 92 // Writes the minidump with the given parameters. Stores the 93 // dump file path in the dump_path parameter if dump generation 94 // succeeds. 95 bool WriteMinidump(); 96 97 private: 98 // Function pointer type for MiniDumpWriteDump, which is looked up 99 // dynamically. 100 typedef BOOL (WINAPI* MiniDumpWriteDumpType)( 101 HANDLE hProcess, 102 DWORD ProcessId, 103 HANDLE hFile, 104 MINIDUMP_TYPE DumpType, 105 CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, 106 CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, 107 CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); 108 109 // Function pointer type for UuidCreate, which is looked up dynamically. 110 typedef RPC_STATUS (RPC_ENTRY* UuidCreateType)(UUID* Uuid); 111 112 // Loads the appropriate DLL lazily in a thread safe way. 113 HMODULE GetDbghelpModule(); 114 115 // Loads the appropriate DLL and gets a pointer to the MiniDumpWriteDump 116 // function lazily and in a thread-safe manner. 117 MiniDumpWriteDumpType GetWriteDump(); 118 119 // Loads the appropriate DLL lazily in a thread safe way. 120 HMODULE GetRpcrt4Module(); 121 122 // Loads the appropriate DLL and gets a pointer to the UuidCreate 123 // function lazily and in a thread-safe manner. 124 UuidCreateType GetCreateUuid(); 125 126 // Returns the path for the file to write dump to. 127 bool GenerateDumpFilePath(std::wstring* file_path); 128 129 // Handle to dynamically loaded DbgHelp.dll. 130 HMODULE dbghelp_module_; 131 132 // Pointer to the MiniDumpWriteDump function. 133 MiniDumpWriteDumpType write_dump_; 134 135 // Handle to dynamically loaded rpcrt4.dll. 136 HMODULE rpcrt4_module_; 137 138 // Pointer to the UuidCreate function. 139 UuidCreateType create_uuid_; 140 141 // Handle for the process to dump. 142 HANDLE process_handle_; 143 144 // Process ID for the process to dump. 145 DWORD process_id_; 146 147 // The crashing thread ID. 148 DWORD thread_id_; 149 150 // The thread ID which is requesting the dump. 151 DWORD requesting_thread_id_; 152 153 // Pointer to the exception information for the crash. This may point to an 154 // address in the crashing process so it should not be dereferenced. 155 EXCEPTION_POINTERS* exception_pointers_; 156 157 // Assertion info for the report. 158 MDRawAssertionInfo* assert_info_; 159 160 // Type of minidump to generate. 161 MINIDUMP_TYPE dump_type_; 162 163 // Specifies whether the exception_pointers_ reference memory in the crashing 164 // process. 165 bool is_client_pointers_; 166 167 // Folder path to store dump files. 168 std::wstring dump_path_; 169 170 // UUID used to make dump file names. 171 UUID uuid_; 172 bool uuid_generated_; 173 174 // The file where the dump will be written. 175 HANDLE dump_file_; 176 177 // The file where the full dump will be written. 178 HANDLE full_dump_file_; 179 180 // Tracks whether the dump file handle is managed externally. 181 bool dump_file_is_internal_; 182 183 // Tracks whether the full dump file handle is managed externally. 184 bool full_dump_file_is_internal_; 185 186 // Additional streams to be written to the dump. 187 MINIDUMP_USER_STREAM_INFORMATION* additional_streams_; 188 189 // The user defined callback for the various stages of the dump process. 190 MINIDUMP_CALLBACK_INFORMATION* callback_info_; 191 192 // Critical section to sychronize action of loading modules dynamically. 193 CRITICAL_SECTION module_load_sync_; 194 195 // Critical section to synchronize action of dynamically getting function 196 // addresses from modules. 197 CRITICAL_SECTION get_proc_address_sync_; 198 }; 199 200 } // namespace google_breakpad 201 202 #endif // CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_ 203