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 #include "client/windows/crash_generation/minidump_generator.h"
31 #include <cassert>
32 #include "client/windows/common/auto_critical_section.h"
33 #include "common/windows/guid_string.h"
34
35 using std::wstring;
36
37 namespace google_breakpad {
38
MinidumpGenerator(const wstring & dump_path)39 MinidumpGenerator::MinidumpGenerator(const wstring& dump_path)
40 : dbghelp_module_(NULL),
41 rpcrt4_module_(NULL),
42 dump_path_(dump_path),
43 write_dump_(NULL),
44 create_uuid_(NULL) {
45 InitializeCriticalSection(&module_load_sync_);
46 InitializeCriticalSection(&get_proc_address_sync_);
47 }
48
~MinidumpGenerator()49 MinidumpGenerator::~MinidumpGenerator() {
50 if (dbghelp_module_) {
51 FreeLibrary(dbghelp_module_);
52 }
53
54 if (rpcrt4_module_) {
55 FreeLibrary(rpcrt4_module_);
56 }
57
58 DeleteCriticalSection(&get_proc_address_sync_);
59 DeleteCriticalSection(&module_load_sync_);
60 }
61
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,wstring * dump_path)62 bool MinidumpGenerator::WriteMinidump(HANDLE process_handle,
63 DWORD process_id,
64 DWORD thread_id,
65 DWORD requesting_thread_id,
66 EXCEPTION_POINTERS* exception_pointers,
67 MDRawAssertionInfo* assert_info,
68 MINIDUMP_TYPE dump_type,
69 bool is_client_pointers,
70 wstring* dump_path) {
71 MiniDumpWriteDumpType write_dump = GetWriteDump();
72 if (!write_dump) {
73 return false;
74 }
75
76 wstring dump_file_path;
77 if (!GenerateDumpFilePath(&dump_file_path)) {
78 return false;
79 }
80
81 // If the client requests a full memory dump, we will write a normal mini
82 // dump and a full memory dump. Both dump files use the same uuid as file
83 // name prefix.
84 bool full_memory_dump = (dump_type & MiniDumpWithFullMemory) != 0;
85 wstring full_dump_file_path;
86 if (full_memory_dump) {
87 full_dump_file_path.assign(dump_file_path);
88 full_dump_file_path.resize(full_dump_file_path.size() - 4); // strip .dmp
89 full_dump_file_path.append(TEXT("-full.dmp"));
90 }
91
92 HANDLE dump_file = CreateFile(dump_file_path.c_str(),
93 GENERIC_WRITE,
94 0,
95 NULL,
96 CREATE_NEW,
97 FILE_ATTRIBUTE_NORMAL,
98 NULL);
99
100 if (dump_file == INVALID_HANDLE_VALUE) {
101 return false;
102 }
103
104 HANDLE full_dump_file = INVALID_HANDLE_VALUE;
105 if (full_memory_dump) {
106 full_dump_file = CreateFile(full_dump_file_path.c_str(),
107 GENERIC_WRITE,
108 0,
109 NULL,
110 CREATE_NEW,
111 FILE_ATTRIBUTE_NORMAL,
112 NULL);
113
114 if (full_dump_file == INVALID_HANDLE_VALUE) {
115 CloseHandle(dump_file);
116 return false;
117 }
118 }
119
120 MINIDUMP_EXCEPTION_INFORMATION* dump_exception_pointers = NULL;
121 MINIDUMP_EXCEPTION_INFORMATION dump_exception_info;
122
123 // Setup the exception information object only if it's a dump
124 // due to an exception.
125 if (exception_pointers) {
126 dump_exception_pointers = &dump_exception_info;
127 dump_exception_info.ThreadId = thread_id;
128 dump_exception_info.ExceptionPointers = exception_pointers;
129 dump_exception_info.ClientPointers = is_client_pointers;
130 }
131
132 // Add an MDRawBreakpadInfo stream to the minidump, to provide additional
133 // information about the exception handler to the Breakpad processor.
134 // The information will help the processor determine which threads are
135 // relevant. The Breakpad processor does not require this information but
136 // can function better with Breakpad-generated dumps when it is present.
137 // The native debugger is not harmed by the presence of this information.
138 MDRawBreakpadInfo breakpad_info = {0};
139 if (!is_client_pointers) {
140 // Set the dump thread id and requesting thread id only in case of
141 // in-process dump generation.
142 breakpad_info.validity = MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID |
143 MD_BREAKPAD_INFO_VALID_REQUESTING_THREAD_ID;
144 breakpad_info.dump_thread_id = thread_id;
145 breakpad_info.requesting_thread_id = requesting_thread_id;
146 }
147
148 // Leave room in user_stream_array for a possible assertion info stream.
149 MINIDUMP_USER_STREAM user_stream_array[2];
150 user_stream_array[0].Type = MD_BREAKPAD_INFO_STREAM;
151 user_stream_array[0].BufferSize = sizeof(breakpad_info);
152 user_stream_array[0].Buffer = &breakpad_info;
153
154 MINIDUMP_USER_STREAM_INFORMATION user_streams;
155 user_streams.UserStreamCount = 1;
156 user_streams.UserStreamArray = user_stream_array;
157
158 MDRawAssertionInfo* actual_assert_info = assert_info;
159 MDRawAssertionInfo client_assert_info = {0};
160
161 if (assert_info) {
162 // If the assertion info object lives in the client process,
163 // read the memory of the client process.
164 if (is_client_pointers) {
165 SIZE_T bytes_read = 0;
166 if (!ReadProcessMemory(process_handle,
167 assert_info,
168 &client_assert_info,
169 sizeof(client_assert_info),
170 &bytes_read)) {
171 CloseHandle(dump_file);
172 if (full_dump_file != INVALID_HANDLE_VALUE)
173 CloseHandle(full_dump_file);
174 return false;
175 }
176
177 if (bytes_read != sizeof(client_assert_info)) {
178 CloseHandle(dump_file);
179 if (full_dump_file != INVALID_HANDLE_VALUE)
180 CloseHandle(full_dump_file);
181 return false;
182 }
183
184 actual_assert_info = &client_assert_info;
185 }
186
187 user_stream_array[1].Type = MD_ASSERTION_INFO_STREAM;
188 user_stream_array[1].BufferSize = sizeof(MDRawAssertionInfo);
189 user_stream_array[1].Buffer = actual_assert_info;
190 ++user_streams.UserStreamCount;
191 }
192
193 bool result_minidump = write_dump(
194 process_handle,
195 process_id,
196 dump_file,
197 static_cast<MINIDUMP_TYPE>((dump_type & (~MiniDumpWithFullMemory))
198 | MiniDumpNormal),
199 exception_pointers ? &dump_exception_info : NULL,
200 &user_streams,
201 NULL) != FALSE;
202
203 bool result_full_memory = true;
204 if (full_memory_dump) {
205 result_full_memory = write_dump(
206 process_handle,
207 process_id,
208 full_dump_file,
209 static_cast<MINIDUMP_TYPE>(dump_type & (~MiniDumpNormal)),
210 exception_pointers ? &dump_exception_info : NULL,
211 &user_streams,
212 NULL) != FALSE;
213 }
214
215 bool result = result_minidump && result_full_memory;
216
217 CloseHandle(dump_file);
218 if (full_dump_file != INVALID_HANDLE_VALUE)
219 CloseHandle(full_dump_file);
220
221 // Store the path of the dump file in the out parameter if dump generation
222 // succeeded.
223 if (result && dump_path) {
224 *dump_path = dump_file_path;
225 }
226
227 return result;
228 }
229
GetDbghelpModule()230 HMODULE MinidumpGenerator::GetDbghelpModule() {
231 AutoCriticalSection lock(&module_load_sync_);
232 if (!dbghelp_module_) {
233 dbghelp_module_ = LoadLibrary(TEXT("dbghelp.dll"));
234 }
235
236 return dbghelp_module_;
237 }
238
GetWriteDump()239 MinidumpGenerator::MiniDumpWriteDumpType MinidumpGenerator::GetWriteDump() {
240 AutoCriticalSection lock(&get_proc_address_sync_);
241 if (!write_dump_) {
242 HMODULE module = GetDbghelpModule();
243 if (module) {
244 FARPROC proc = GetProcAddress(module, "MiniDumpWriteDump");
245 write_dump_ = reinterpret_cast<MiniDumpWriteDumpType>(proc);
246 }
247 }
248
249 return write_dump_;
250 }
251
GetRpcrt4Module()252 HMODULE MinidumpGenerator::GetRpcrt4Module() {
253 AutoCriticalSection lock(&module_load_sync_);
254 if (!rpcrt4_module_) {
255 rpcrt4_module_ = LoadLibrary(TEXT("rpcrt4.dll"));
256 }
257
258 return rpcrt4_module_;
259 }
260
GetCreateUuid()261 MinidumpGenerator::UuidCreateType MinidumpGenerator::GetCreateUuid() {
262 AutoCriticalSection lock(&module_load_sync_);
263 if (!create_uuid_) {
264 HMODULE module = GetRpcrt4Module();
265 if (module) {
266 FARPROC proc = GetProcAddress(module, "UuidCreate");
267 create_uuid_ = reinterpret_cast<UuidCreateType>(proc);
268 }
269 }
270
271 return create_uuid_;
272 }
273
GenerateDumpFilePath(wstring * file_path)274 bool MinidumpGenerator::GenerateDumpFilePath(wstring* file_path) {
275 UUID id = {0};
276
277 UuidCreateType create_uuid = GetCreateUuid();
278 if(!create_uuid) {
279 return false;
280 }
281
282 create_uuid(&id);
283 wstring id_str = GUIDString::GUIDToWString(&id);
284
285 *file_path = dump_path_ + TEXT("\\") + id_str + TEXT(".dmp");
286 return true;
287 }
288
289 } // namespace google_breakpad
290