Planeshift
minidump_generator.h
Go to the documentation of this file.
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>
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 dump path.
48  explicit MinidumpGenerator(const std::wstring& dump_path);
49 
51 
52  // Writes the minidump with the given parameters. Stores the
53  // dump file path in the dump_path parameter if dump generation
54  // succeeds.
55  bool WriteMinidump(HANDLE process_handle,
56  DWORD process_id,
57  DWORD thread_id,
58  DWORD requesting_thread_id,
59  EXCEPTION_POINTERS* exception_pointers,
60  MDRawAssertionInfo* assert_info,
61  MINIDUMP_TYPE dump_type,
62  bool is_client_pointers,
63  std::wstring* dump_path);
64 
65  // Writes the minidump with the given parameters. Stores the dump file
66  // path in the dump_path (and full_dump_path) parameter if dump
67  // generation succeeds. full_dump_path and dump_path can be NULL.
68  bool WriteMinidump(HANDLE process_handle,
69  DWORD process_id,
70  DWORD thread_id,
71  DWORD requesting_thread_id,
72  EXCEPTION_POINTERS* exception_pointers,
73  MDRawAssertionInfo* assert_info,
74  MINIDUMP_TYPE dump_type,
75  bool is_client_pointers,
76  std::wstring* dump_path,
77  std::wstring* full_dump_path);
78 
79  // Writes the minidump with the given parameters. Writes the minidump and
80  // full dump to the file handles supplied. This allows the caller to handle
81  // the creation of the files for the dump. The file handles are not closed
82  // by this function.
83  bool WriteMinidump(HANDLE process_handle,
84  DWORD process_id,
85  DWORD thread_id,
86  DWORD requesting_thread_id,
87  EXCEPTION_POINTERS* exception_pointers,
88  MDRawAssertionInfo* assert_info,
89  MINIDUMP_TYPE dump_type,
90  bool is_client_pointers,
91  HANDLE dump_file,
92  HANDLE full_dump_file);
93 
94  private:
95  // Function pointer type for MiniDumpWriteDump, which is looked up
96  // dynamically.
97  typedef BOOL (WINAPI* MiniDumpWriteDumpType)(
98  HANDLE hProcess,
99  DWORD ProcessId,
100  HANDLE hFile,
101  MINIDUMP_TYPE DumpType,
102  CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
103  CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
104  CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
105 
106  // Function pointer type for UuidCreate, which is looked up dynamically.
107  typedef RPC_STATUS (RPC_ENTRY* UuidCreateType)(UUID* Uuid);
108 
109  // Loads the appropriate DLL lazily in a thread safe way.
110  HMODULE GetDbghelpModule();
111 
112  // Loads the appropriate DLL and gets a pointer to the MiniDumpWriteDump
113  // function lazily and in a thread-safe manner.
114  MiniDumpWriteDumpType GetWriteDump();
115 
116  // Loads the appropriate DLL lazily in a thread safe way.
117  HMODULE GetRpcrt4Module();
118 
119  // Loads the appropriate DLL and gets a pointer to the UuidCreate
120  // function lazily and in a thread-safe manner.
121  UuidCreateType GetCreateUuid();
122 
123  // Returns the path for the file to write dump to.
124  bool GenerateDumpFilePath(std::wstring* file_path);
125 
126  // Handle to dynamically loaded DbgHelp.dll.
127  HMODULE dbghelp_module_;
128 
129  // Pointer to the MiniDumpWriteDump function.
130  MiniDumpWriteDumpType write_dump_;
131 
132  // Handle to dynamically loaded rpcrt4.dll.
133  HMODULE rpcrt4_module_;
134 
135  // Pointer to the UuidCreate function.
136  UuidCreateType create_uuid_;
137 
138  // Folder path to store dump files.
139  std::wstring dump_path_;
140 
141  // Critical section to sychronize action of loading modules dynamically.
142  CRITICAL_SECTION module_load_sync_;
143 
144  // Critical section to synchronize action of dynamically getting function
145  // addresses from modules.
146  CRITICAL_SECTION get_proc_address_sync_;
147 };
148 
149 } // namespace google_breakpad
150 
151 #endif // CLIENT_WINDOWS_CRASH_GENERATION_MINIDUMP_GENERATOR_H_
bool WriteMinidump(HANDLE process_handle, DWORD process_id, DWORD thread_id, DWORD requesting_thread_id, EXCEPTION_POINTERS *exception_pointers, MDRawAssertionInfo *assert_info, MINIDUMP_TYPE dump_type, bool is_client_pointers, std::wstring *dump_path)