1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 typedef BOOL (WINAPI *MiniDumpWriteDumpFunc) (
27 IN CONST PMINIDUMP_EXCEPTION_INFORMATION, OPTIONAL
28 IN CONST PMINIDUMP_USER_STREAM_INFORMATION, OPTIONAL
29 IN CONST PMINIDUMP_CALLBACK_INFORMATION OPTIONAL
32 /// Creates a windows minidump in temp folder.
33 void CreateWin32MiniDump(_EXCEPTION_POINTERS* pep) {
34 char temp_path[MAX_PATH];
35 GetTempPath(sizeof(temp_path), temp_path);
36 char temp_file[MAX_PATH];
37 sprintf(temp_file, "%s\\ninja_crash_dump_%d.dmp",
38 temp_path, GetCurrentProcessId());
40 // Delete any previous minidump of the same name.
41 DeleteFile(temp_file);
43 // Load DbgHelp.dll dynamically, as library is not present on all
45 HMODULE dbghelp = LoadLibrary("dbghelp.dll");
46 if (dbghelp == NULL) {
47 Error("failed to create minidump: LoadLibrary('dbghelp.dll'): %s",
48 GetLastErrorString().c_str());
52 MiniDumpWriteDumpFunc mini_dump_write_dump =
53 (MiniDumpWriteDumpFunc)GetProcAddress(dbghelp, "MiniDumpWriteDump");
54 if (mini_dump_write_dump == NULL) {
55 Error("failed to create minidump: GetProcAddress('MiniDumpWriteDump'): %s",
56 GetLastErrorString().c_str());
60 HANDLE hFile = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
61 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
63 Error("failed to create minidump: CreateFileA(%s): %s",
64 temp_file, GetLastErrorString().c_str());
68 MINIDUMP_EXCEPTION_INFORMATION mdei;
69 mdei.ThreadId = GetCurrentThreadId();
70 mdei.ExceptionPointers = pep;
71 mdei.ClientPointers = FALSE;
72 MINIDUMP_TYPE mdt = (MINIDUMP_TYPE) (MiniDumpWithDataSegs |
73 MiniDumpWithHandleData);
75 BOOL rv = mini_dump_write_dump(GetCurrentProcess(), GetCurrentProcessId(),
76 hFile, mdt, (pep != 0) ? &mdei : 0, 0, 0);
80 Error("MiniDumpWriteDump failed: %s", GetLastErrorString().c_str());
84 Warning("minidump created: %s", temp_file);