From 755c501084412a0896312af05cda6d4227136174 Mon Sep 17 00:00:00 2001 From: Jiri Moudry Date: Tue, 27 Mar 2012 17:39:01 +0200 Subject: Improve handling of fatal errors on Windows, support creation of minidumps --- src/ninja.cc | 35 +++++++++++++++++++++++++++ src/util.cc | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 7 ++++++ 3 files changed, 120 insertions(+) diff --git a/src/ninja.cc b/src/ninja.cc index 04cd771..5807dcd 100644 --- a/src/ninja.cc +++ b/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"; diff --git a/src/util.cc b/src/util.cc index 4d9adf3..d240e1c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -34,6 +34,7 @@ #include #ifdef _WIN32 +#include // for minidump #include // _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 + diff --git a/src/util.h b/src/util.h index 9b4e745..016e6cf 100644 --- a/src/util.h +++ b/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 -- cgit v0.12