Ninja
minidump-win32.cc
Go to the documentation of this file.
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #ifndef NINJA_BOOTSTRAP
16 
17 #include <windows.h>
18 #include <DbgHelp.h>
19 
20 
21 #include "util.h"
22 
23 typedef BOOL (WINAPI *MiniDumpWriteDumpFunc) (
24  IN HANDLE,
25  IN DWORD,
26  IN HANDLE,
27  IN MINIDUMP_TYPE,
30  IN CONST PMINIDUMP_CALLBACK_INFORMATION OPTIONAL
31  );
32 
33 /// Creates a windows minidump in temp folder.
34 void CreateWin32MiniDump(_EXCEPTION_POINTERS* pep) {
35  char temp_path[MAX_PATH];
36  GetTempPath(sizeof(temp_path), temp_path);
37  char temp_file[MAX_PATH];
38  sprintf(temp_file, "%s\\ninja_crash_dump_%d.dmp",
39  temp_path, GetCurrentProcessId());
40 
41  // Delete any previous minidump of the same name.
42  DeleteFile(temp_file);
43 
44  // Load DbgHelp.dll dynamically, as library is not present on all
45  // Windows versions.
46  HMODULE dbghelp = LoadLibrary("dbghelp.dll");
47  if (dbghelp == NULL) {
48  Error("failed to create minidump: LoadLibrary('dbghelp.dll'): %s",
49  GetLastErrorString().c_str());
50  return;
51  }
52 
53  MiniDumpWriteDumpFunc mini_dump_write_dump =
54  (MiniDumpWriteDumpFunc)GetProcAddress(dbghelp, "MiniDumpWriteDump");
55  if (mini_dump_write_dump == NULL) {
56  Error("failed to create minidump: GetProcAddress('MiniDumpWriteDump'): %s",
57  GetLastErrorString().c_str());
58  return;
59  }
60 
61  HANDLE hFile = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
62  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
63  if (hFile == NULL) {
64  Error("failed to create minidump: CreateFileA(%s): %s",
65  temp_file, GetLastErrorString().c_str());
66  return;
67  }
68 
69  MINIDUMP_EXCEPTION_INFORMATION mdei;
70  mdei.ThreadId = GetCurrentThreadId();
71  mdei.ExceptionPointers = pep;
72  mdei.ClientPointers = FALSE;
73  MINIDUMP_TYPE mdt = (MINIDUMP_TYPE) (MiniDumpWithDataSegs |
74  MiniDumpWithHandleData);
75 
76  BOOL rv = mini_dump_write_dump(GetCurrentProcess(), GetCurrentProcessId(),
77  hFile, mdt, (pep != 0) ? &mdei : 0, 0, 0);
78  CloseHandle(hFile);
79 
80  if (!rv) {
81  Error("MiniDumpWriteDump failed: %s", GetLastErrorString().c_str());
82  return;
83  }
84 
85  Warning("minidump created: %s", temp_file);
86 }
87 
88 #endif // NINJA_BOOTSTRAP
IN IN IN IN CONST PMINIDUMP_EXCEPTION_INFORMATION
IN IN IN IN CONST OPTIONAL IN CONST OPTIONAL IN CONST PMINIDUMP_CALLBACK_INFORMATION OPTIONAL
IN IN IN MINIDUMP_TYPE
IN IN HANDLE
void CreateWin32MiniDump(_EXCEPTION_POINTERS *pep)
Creates a windows minidump in temp folder.
IN IN IN IN CONST OPTIONAL IN CONST PMINIDUMP_USER_STREAM_INFORMATION
typedef BOOL(WINAPI *MiniDumpWriteDumpFunc)(IN HANDLE
IN DWORD
void Warning(const char *msg,...)
Log a warning message.
Definition: util.cc:69
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:78