From bb6da9afc166e61ea60dacfc4da94b9181ff887b Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Fri, 27 Jul 2012 12:02:59 -0700 Subject: rearrange minidump patch to match ninja code style --- configure.py | 4 ++- src/minidump-win32.cc | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ninja.cc | 79 ++++++++++++++++++++++++++-------------------- src/util.cc | 79 ---------------------------------------------- 4 files changed, 135 insertions(+), 114 deletions(-) create mode 100644 src/minidump-win32.cc diff --git a/configure.py b/configure.py index e52262d..41b3cf7 100755 --- a/configure.py +++ b/configure.py @@ -243,8 +243,10 @@ for name in ['build', 'state', 'util']: objs += cxx(name) -if platform == 'mingw' or platform == 'windows': +if platform in ('mingw', 'windows'): objs += cxx('subprocess-win32') + if platform == 'windows': + objs += cxx('minidump-win32') objs += cc('getopt') else: objs += cxx('subprocess') diff --git a/src/minidump-win32.cc b/src/minidump-win32.cc new file mode 100644 index 0000000..f7067f3 --- /dev/null +++ b/src/minidump-win32.cc @@ -0,0 +1,87 @@ +// Copyright 2012 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef NINJA_BOOTSTRAP + +#include +#include + +#include "util.h" + +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 + ); + +/// Creates a windows minidump in temp folder. +void CreateWin32MiniDump(_EXCEPTION_POINTERS* pep) { + char temp_path[MAX_PATH]; + GetTempPath(sizeof(temp_path), temp_path); + char temp_file[MAX_PATH]; + sprintf(temp_file, "%s\\ninja_crash_dump_%d.dmp", + temp_path, GetCurrentProcessId()); + + // Delete any previous minidump of the same name. + DeleteFile(temp_file); + + // Load DbgHelp.dll dynamically, as library is not present on all + // Windows versions. + HMODULE dbghelp = LoadLibrary("dbghelp.dll"); + if (dbghelp == NULL) { + Error("failed to create minidump: LoadLibrary('dbghelp.dll'): %s", + GetLastErrorString().c_str()); + return; + } + + MiniDumpWriteDumpFunc mini_dump_write_dump = + (MiniDumpWriteDumpFunc)GetProcAddress(dbghelp, "MiniDumpWriteDump"); + if (mini_dump_write_dump == NULL) { + Error("failed to create minidump: GetProcAddress('MiniDumpWriteDump'): %s", + GetLastErrorString().c_str()); + return; + } + + HANDLE hFile = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (hFile == NULL) { + Error("failed to create minidump: CreateFileA(%s): %s", + temp_file, GetLastErrorString().c_str()); + return; + } + + MINIDUMP_EXCEPTION_INFORMATION mdei; + mdei.ThreadId = GetCurrentThreadId(); + mdei.ExceptionPointers = pep; + mdei.ClientPointers = FALSE; + MINIDUMP_TYPE mdt = (MINIDUMP_TYPE) (MiniDumpWithDataSegs | + MiniDumpWithHandleData); + + BOOL rv = mini_dump_write_dump(GetCurrentProcess(), GetCurrentProcessId(), + hFile, mdt, (pep != 0) ? &mdei : 0, 0, 0); + CloseHandle(hFile); + + if (!rv) { + Error("MiniDumpWriteDump failed: %s", GetLastErrorString().c_str()); + return; + } + + Warning("minidump created: %s", temp_file); +} + +#endif // NINJA_BOOTSTRAP diff --git a/src/ninja.cc b/src/ninja.cc index 393f717..c1caf37 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -626,44 +626,33 @@ int RunBuild(Globals* globals, int argc, char** argv) { return 0; } -} // 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"); +// Defined in minidump-win32.cc. +void CreateWin32MiniDump(_EXCEPTION_POINTERS* pep); + +/// 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 TerminateHandler() { + CreateWin32MiniDump(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 +/// On Windows, we want to prevent error dialogs in case of exceptions. +/// This function handles the exception, and writes a minidump. +int ExceptionFilter(unsigned int code, struct _EXCEPTION_POINTERS *ep) { + Error("exception: 0x%X", code); // e.g. EXCEPTION_ACCESS_VIOLATION + fflush(stderr); + CreateWin32MiniDump(ep); + return EXCEPTION_EXECUTE_HANDLER; +} + +#endif // _MSC_VER + +int NinjaMain(int argc, char** argv) { Globals globals; globals.ninja_command = argv[0]; const char* input_file = "build.ninja"; @@ -824,3 +813,25 @@ reload: } return result; } + +} // anonymous namespace + +int main(int argc, char** argv) { +#if !defined(NINJA_BOOTSTRAP) && defined(_MSC_VER) + // 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 NinjaMain(argc, argv); + } + __except(ExceptionFilter(GetExceptionCode(), GetExceptionInformation())) { + // Common error situations return exitCode=1. 2 was chosen to + // indicate a more serious problem. + return 2; + } +#else + return NinjaMain(argc, argv); +#endif +} diff --git a/src/util.cc b/src/util.cc index be74c4d..ca05292 100644 --- a/src/util.cc +++ b/src/util.cc @@ -35,7 +35,6 @@ #include #ifdef _WIN32 -#include // for minidump #include // _mkdir #endif @@ -312,81 +311,3 @@ 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 - - -- cgit v0.12