Browse files

Improve handling of fatal errors on Windows, support creation of mini…

…dumps
  • Loading branch information...
1 parent 6a09b96 commit 755c501084412a0896312af05cda6d4227136174 @gmoudry gmoudry committed Mar 27, 2012
Showing with 120 additions and 0 deletions.
  1. +35 −0 src/ninja.cc
  2. +78 −0 src/util.cc
  3. +7 −0 src/util.h
View
35 src/ninja.cc
@@ -614,7 +614,42 @@ int RunBuild(Globals* globals, int argc, char** argv) {
} // anonymous namespace
+
+#ifdef _WIN32
+
+/// This handler processes fatal crashes that you can't catch
+/// Test example: C++ exception in a stack-unwind-block
+/// Real-world example: ninja launched a compiler to process a tricky C++ input file.
+/// The compiler got itself into a state where it generated 3 GB of output and caused ninja to crash
+void ninja_terminate_fct() {
+ Create_Win32_MiniDump(NULL);
+ Fatal("terminate handler called");
+}
+
+/// main_unsafe is called from within an exception handling block
+int main_unsafe(int argc, char** argv);
+
+/// Windows main() uses SEH (Structured exception handling)
+int main(int argc, char** argv) {
+ // set a handler to catch crashes not caught by the __try..__except block (e.g. an exception in a stack-unwind-block)
+ set_terminate(ninja_terminate_fct);
+ __try {
+ // running inside __try ... __except suppresses any Windows error dialogs for errors such as bad_alloc
+ return main_unsafe(argc, argv);
+ }
+ __except(exception_filter(GetExceptionCode(), GetExceptionInformation())) {
+ // you will land here e.g. if you run out of memory, or run inside a distribution environment that fails
+ fprintf(stderr, "ninja: exception, exiting with error code 2\n");
+ // common error situations below return exitCode=1, 2 was chosen to indicate a more serious problem
+ return 2;
+ }
+}
+
+int main_unsafe (int argc, char** argv) {
+#else
+//on Linux, we have no exception handling
int main(int argc, char** argv) {
+#endif
Globals globals;
globals.ninja_command = argv[0];
const char* input_file = "build.ninja";
View
78 src/util.cc
@@ -34,6 +34,7 @@
#include <vector>
#ifdef _WIN32
+#include <DbgHelp.h> // for minidump
#include <direct.h> // _mkdir
#endif
@@ -292,3 +293,80 @@ string StripAnsiEscapeCodes(const string& in) {
}
return stripped;
}
+
+#ifdef _WIN32
+typedef BOOL (WINAPI *MiniDumpWriteDumpFunc) (
+ IN HANDLE,
+ IN DWORD,
+ IN HANDLE,
+ IN MINIDUMP_TYPE,
+ IN CONST PMINIDUMP_EXCEPTION_INFORMATION, OPTIONAL
+ IN CONST PMINIDUMP_USER_STREAM_INFORMATION, OPTIONAL
+ IN CONST PMINIDUMP_CALLBACK_INFORMATION OPTIONAL
+ );
+
+/// this function creates a windows minidump in temp folder.
+void Create_Win32_MiniDump( _EXCEPTION_POINTERS* pep ) {
+ char tempPathBuff[MAX_PATH];
+ GetTempPath(sizeof(tempPathBuff), tempPathBuff);
+ char tempFileName[MAX_PATH];
+ sprintf(tempFileName, "%s\\ninja_crash_dump_%d.dmp", tempPathBuff, GetCurrentProcessId());
+
+ //delete any previous minidump of the same name
+ DeleteFile(tempFileName);
+
+ // load DbgHelp.dll dynamically, as library is not present on all windows versions
+ HMODULE hModDbgHelp = LoadLibrary("dbghelp.dll");
+ if (hModDbgHelp == NULL) {
+ fprintf(stderr, "ninja: failed to create minidump, failed to load dbghelp.dll, error %s \n",
+ GetLastErrorString().c_str());
+ return;
+ }
+
+ MiniDumpWriteDumpFunc pfnMiniDumpWriteDump = (MiniDumpWriteDumpFunc)GetProcAddress(hModDbgHelp, "MiniDumpWriteDump");
+ if (pfnMiniDumpWriteDump == NULL) {
+ fprintf(stderr, "ninja: failed to create minidump, failed on GetProcAddress('MiniDumpWriteDump'), error %s \n",
+ GetLastErrorString().c_str());
+ return;
+ }
+
+ // Open the file
+ HANDLE hFile = CreateFileA( tempFileName, GENERIC_READ | GENERIC_WRITE,
+ 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
+ if (hFile == NULL) {
+ fprintf(stderr, "ninja: failed to create minidump, failed on CreateFileA(%s), error %s \n",
+ tempFileName, GetLastErrorString().c_str());
+ return;
+ }
+
+ // Create the mini dump
+ MINIDUMP_EXCEPTION_INFORMATION mdei;
+ mdei.ThreadId = GetCurrentThreadId();
+ mdei.ExceptionPointers = pep;
+ mdei.ClientPointers = FALSE;
+ MINIDUMP_TYPE mdt = (MINIDUMP_TYPE) (MiniDumpWithDataSegs | MiniDumpWithHandleData);
+
+ BOOL rv = pfnMiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
+ hFile, mdt, (pep != 0) ? &mdei : 0, 0, 0 );
+
+ if ( !rv )
+ fprintf(stderr, "ninja: MiniDumpWriteDump failed. Error: %s \n", GetLastErrorString().c_str() );
+ else
+ fprintf(stderr, "ninja: Minidump created: %s\n", tempFileName );
+
+ // Close the file
+ CloseHandle( hFile );
+}
+
+/// On Windows, we want to prevent error dialogs in case of exceptions.
+/// This function handles the exception, and writes a minidump.
+int exception_filter(unsigned int code, struct _EXCEPTION_POINTERS *ep) {
+ fprintf(stderr, "ninja.exe fatal error: 0x%X \n", code); //e.g. EXCEPTION_ACCESS_VIOLATION
+ fflush(stderr);
+ Create_Win32_MiniDump(ep);
+ return EXCEPTION_EXECUTE_HANDLER;
+}
+#else
+ //on Linux, core dumps are created automatically, no code needed
+#endif
+
View
7 src/util.h
@@ -76,6 +76,13 @@ string StripAnsiEscapeCodes(const string& in);
#endif
#ifdef _WIN32
+
+/// Handler for __except block
+int exception_filter(unsigned int code, struct _EXCEPTION_POINTERS *ep);
+
+/// Write a windows minidump file in temp directory. User can specify null 'pep' argument.
+void Create_Win32_MiniDump( struct _EXCEPTION_POINTERS* pep );
+
/// Convert the value returned by GetLastError() into a string.
string GetLastErrorString();
#endif

0 comments on commit 755c501

Please sign in to comment.