diff options
-rw-r--r-- | src/ninja.cc | 35 | ||||
-rw-r--r-- | src/util.cc | 79 | ||||
-rw-r--r-- | src/util.h | 7 |
3 files changed, 121 insertions, 0 deletions
diff --git a/src/ninja.cc b/src/ninja.cc index 619c3bf..393f717 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -628,7 +628,42 @@ int RunBuild(Globals* globals, int argc, char** argv) { } // anonymous namespace + +#ifdef _MSC_VER + +/// 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 ca05292..be74c4d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -35,6 +35,7 @@ #include <vector> #ifdef _WIN32 +#include <DbgHelp.h> // for minidump
#include <direct.h> // _mkdir #endif @@ -311,3 +312,81 @@ double GetLoadAverage() return GetLoadAverage_unix(); #endif // _WIN32 } + +#ifdef _MSC_VER
+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 or MinGW, core dumps are created automatically, no code needed
+#endif
+
+ @@ -75,6 +75,13 @@ double GetLoadAverage(); #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 |