Home | History | Annotate | Download | only in crash_generation
      1 // Copyright (c) 2008, 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 #ifndef CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
     31 #define CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
     32 
     33 #include <windows.h>
     34 #include <dbghelp.h>
     35 #include <rpc.h>
     36 #include <list>
     37 #include <string>
     38 #include "google_breakpad/common/minidump_format.h"
     39 
     40 namespace google_breakpad {
     41 
     42 // Abstraction for various objects and operations needed to generate
     43 // minidump on Windows. This abstraction is useful to hide all the gory
     44 // details for minidump generation and provide a clean interface to
     45 // the clients to generate minidumps.
     46 class MinidumpGenerator {
     47  public:
     48   // Creates an instance with the given parameters.
     49   // is_client_pointers specifies whether the exception_pointers and
     50   // assert_info point into the process that is being dumped.
     51   // Before calling WriteMinidump on the returned instance a dump file muct be
     52   // specified by a call to either SetDumpFile() or GenerateDumpFile().
     53   // If a full dump file will be requested via a subsequent call to either
     54   // SetFullDumpFile or GenerateFullDumpFile() dump_type must include
     55   // MiniDumpWithFullMemory.
     56   MinidumpGenerator(const std::wstring& dump_path,
     57                     const HANDLE process_handle,
     58                     const DWORD process_id,
     59                     const DWORD thread_id,
     60                     const DWORD requesting_thread_id,
     61                     EXCEPTION_POINTERS* exception_pointers,
     62                     MDRawAssertionInfo* assert_info,
     63                     const MINIDUMP_TYPE dump_type,
     64                     const bool is_client_pointers);
     65 
     66   ~MinidumpGenerator();
     67 
     68   void SetDumpFile(const HANDLE dump_file) { dump_file_ = dump_file; }
     69   void SetFullDumpFile(const HANDLE full_dump_file) {
     70     full_dump_file_ = full_dump_file;
     71   }
     72 
     73   // Generate the name for the dump file that will be written to once
     74   // WriteMinidump() is called. Can only be called once and cannot be called
     75   // if the dump file is set via SetDumpFile().
     76   bool GenerateDumpFile(std::wstring* dump_path);
     77 
     78   // Generate the name for the full dump file that will be written to once
     79   // WriteMinidump() is called. Cannot be called unless the minidump type
     80   // includes MiniDumpWithFullMemory. Can only be called once and cannot be
     81   // called if the dump file is set via SetFullDumpFile().
     82   bool GenerateFullDumpFile(std::wstring* full_dump_path);
     83 
     84   void SetAdditionalStreams(
     85       MINIDUMP_USER_STREAM_INFORMATION* additional_streams) {
     86     additional_streams_ = additional_streams;
     87   }
     88 
     89   void SetCallback(MINIDUMP_CALLBACK_INFORMATION* callback_info) {
     90     callback_info_ = callback_info;
     91   }
     92 
     93   // Writes the minidump with the given parameters. Stores the
     94   // dump file path in the dump_path parameter if dump generation
     95   // succeeds.
     96   bool WriteMinidump();
     97 
     98  private:
     99   // Function pointer type for MiniDumpWriteDump, which is looked up
    100   // dynamically.
    101   typedef BOOL (WINAPI* MiniDumpWriteDumpType)(
    102       HANDLE hProcess,
    103       DWORD ProcessId,
    104       HANDLE hFile,
    105       MINIDUMP_TYPE DumpType,
    106       CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
    107       CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
    108       CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
    109 
    110   // Function pointer type for UuidCreate, which is looked up dynamically.
    111   typedef RPC_STATUS (RPC_ENTRY* UuidCreateType)(UUID* Uuid);
    112 
    113   // Loads the appropriate DLL lazily in a thread safe way.
    114   HMODULE GetDbghelpModule();
    115 
    116   // Loads the appropriate DLL and gets a pointer to the MiniDumpWriteDump
    117   // function lazily and in a thread-safe manner.
    118   MiniDumpWriteDumpType GetWriteDump();
    119 
    120   // Loads the appropriate DLL lazily in a thread safe way.
    121   HMODULE GetRpcrt4Module();
    122 
    123   // Loads the appropriate DLL and gets a pointer to the UuidCreate
    124   // function lazily and in a thread-safe manner.
    125   UuidCreateType GetCreateUuid();
    126 
    127   // Returns the path for the file to write dump to.
    128   bool GenerateDumpFilePath(std::wstring* file_path);
    129 
    130   // Handle to dynamically loaded DbgHelp.dll.
    131   HMODULE dbghelp_module_;
    132 
    133   // Pointer to the MiniDumpWriteDump function.
    134   MiniDumpWriteDumpType write_dump_;
    135 
    136   // Handle to dynamically loaded rpcrt4.dll.
    137   HMODULE rpcrt4_module_;
    138 
    139   // Pointer to the UuidCreate function.
    140   UuidCreateType create_uuid_;
    141 
    142   // Handle for the process to dump.
    143   HANDLE process_handle_;
    144 
    145   // Process ID for the process to dump.
    146   DWORD process_id_;
    147 
    148   // The crashing thread ID.
    149   DWORD thread_id_;
    150 
    151   // The thread ID which is requesting the dump.
    152   DWORD requesting_thread_id_;
    153 
    154   // Pointer to the exception information for the crash. This may point to an
    155   // address in the crashing process so it should not be dereferenced.
    156   EXCEPTION_POINTERS* exception_pointers_;
    157 
    158   // Assertion info for the report.
    159   MDRawAssertionInfo* assert_info_;
    160 
    161   // Type of minidump to generate.
    162   MINIDUMP_TYPE dump_type_;
    163 
    164   // Specifies whether the exception_pointers_ reference memory in the crashing
    165   // process.
    166   bool is_client_pointers_;
    167 
    168   // Folder path to store dump files.
    169   std::wstring dump_path_;
    170 
    171   // The file where the dump will be written.
    172   HANDLE dump_file_;
    173 
    174   // The file where the full dump will be written.
    175   HANDLE full_dump_file_;
    176 
    177   // Tracks whether the dump file handle is managed externally.
    178   bool dump_file_is_internal_;
    179 
    180   // Tracks whether the full dump file handle is managed externally.
    181   bool full_dump_file_is_internal_;
    182 
    183   // Additional streams to be written to the dump.
    184   MINIDUMP_USER_STREAM_INFORMATION* additional_streams_;
    185 
    186   // The user defined callback for the various stages of the dump process.
    187   MINIDUMP_CALLBACK_INFORMATION* callback_info_;
    188 
    189   // Critical section to sychronize action of loading modules dynamically.
    190   CRITICAL_SECTION module_load_sync_;
    191 
    192   // Critical section to synchronize action of dynamically getting function
    193   // addresses from modules.
    194   CRITICAL_SECTION get_proc_address_sync_;
    195 };
    196 
    197 }  // namespace google_breakpad
    198 
    199 #endif  // CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
    200