Planeshift

minidump_generator.h

Go to the documentation of this file.
00001 // Copyright (c) 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 #ifndef CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
00031 #define CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
00032 
00033 #include <windows.h>
00034 #include <dbghelp.h>
00035 #include <rpc.h>
00036 #include <list>
00037 #include "google_breakpad/common/minidump_format.h"
00038 
00039 namespace google_breakpad {
00040 
00041 // Abstraction for various objects and operations needed to generate
00042 // minidump on Windows. This abstraction is useful to hide all the gory
00043 // details for minidump generation and provide a clean interface to
00044 // the clients to generate minidumps.
00045 class MinidumpGenerator {
00046  public:
00047   // Creates an instance with the given dump path.
00048   explicit MinidumpGenerator(const std::wstring& dump_path);
00049 
00050   ~MinidumpGenerator();
00051 
00052   // Writes the minidump with the given parameters. Stores the
00053   // dump file path in the dump_path parameter if dump generation
00054   // succeeds.
00055   bool WriteMinidump(HANDLE process_handle,
00056                      DWORD process_id,
00057                      DWORD thread_id,
00058                      DWORD requesting_thread_id,
00059                      EXCEPTION_POINTERS* exception_pointers,
00060                      MDRawAssertionInfo* assert_info,
00061                      MINIDUMP_TYPE dump_type,
00062                      bool is_client_pointers,
00063                      std::wstring* dump_path);
00064 
00065   // Writes the minidump with the given parameters. Stores the dump file
00066   // path in the dump_path (and full_dump_path) parameter if dump
00067   // generation succeeds. full_dump_path and dump_path can be NULL.
00068   bool WriteMinidump(HANDLE process_handle,
00069                      DWORD process_id,
00070                      DWORD thread_id,
00071                      DWORD requesting_thread_id,
00072                      EXCEPTION_POINTERS* exception_pointers,
00073                      MDRawAssertionInfo* assert_info,
00074                      MINIDUMP_TYPE dump_type,
00075                      bool is_client_pointers,
00076                      std::wstring* dump_path,
00077                      std::wstring* full_dump_path);
00078 
00079   // Writes the minidump with the given parameters. Writes the minidump and
00080   // full dump to the file handles supplied. This allows the caller to handle
00081   // the creation of the files for the dump. The file handles are not closed
00082   // by this function.
00083   bool WriteMinidump(HANDLE process_handle,
00084                      DWORD process_id,
00085                      DWORD thread_id,
00086                      DWORD requesting_thread_id,
00087                      EXCEPTION_POINTERS* exception_pointers,
00088                      MDRawAssertionInfo* assert_info,
00089                      MINIDUMP_TYPE dump_type,
00090                      bool is_client_pointers,
00091                      HANDLE dump_file,
00092                      HANDLE full_dump_file);
00093 
00094  private:
00095   // Function pointer type for MiniDumpWriteDump, which is looked up
00096   // dynamically.
00097   typedef BOOL (WINAPI* MiniDumpWriteDumpType)(
00098       HANDLE hProcess,
00099       DWORD ProcessId,
00100       HANDLE hFile,
00101       MINIDUMP_TYPE DumpType,
00102       CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
00103       CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
00104       CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
00105 
00106   // Function pointer type for UuidCreate, which is looked up dynamically.
00107   typedef RPC_STATUS (RPC_ENTRY* UuidCreateType)(UUID* Uuid);
00108 
00109   // Loads the appropriate DLL lazily in a thread safe way.
00110   HMODULE GetDbghelpModule();
00111 
00112   // Loads the appropriate DLL and gets a pointer to the MiniDumpWriteDump
00113   // function lazily and in a thread-safe manner.
00114   MiniDumpWriteDumpType GetWriteDump();
00115 
00116   // Loads the appropriate DLL lazily in a thread safe way.
00117   HMODULE GetRpcrt4Module();
00118 
00119   // Loads the appropriate DLL and gets a pointer to the UuidCreate
00120   // function lazily and in a thread-safe manner.
00121   UuidCreateType GetCreateUuid();
00122 
00123   // Returns the path for the file to write dump to.
00124   bool GenerateDumpFilePath(std::wstring* file_path);
00125 
00126   // Handle to dynamically loaded DbgHelp.dll.
00127   HMODULE dbghelp_module_;
00128 
00129   // Pointer to the MiniDumpWriteDump function.
00130   MiniDumpWriteDumpType write_dump_;
00131 
00132   // Handle to dynamically loaded rpcrt4.dll.
00133   HMODULE rpcrt4_module_;
00134 
00135   // Pointer to the UuidCreate function.
00136   UuidCreateType create_uuid_;
00137 
00138   // Folder path to store dump files.
00139   std::wstring dump_path_;
00140 
00141   // Critical section to sychronize action of loading modules dynamically.
00142   CRITICAL_SECTION module_load_sync_;
00143 
00144   // Critical section to synchronize action of dynamically getting function
00145   // addresses from modules.
00146   CRITICAL_SECTION get_proc_address_sync_;
00147 };
00148 
00149 }  // namespace google_breakpad
00150 
00151 #endif  // CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_