diff options
author | Jiri Moudry <gmoudry@gmail.com> | 2012-03-27 15:39:01 (GMT) |
---|---|---|
committer | Jiri Moudry <gmoudry@gmail.com> | 2012-03-28 15:19:11 (GMT) |
commit | 755c501084412a0896312af05cda6d4227136174 (patch) | |
tree | c4b33ee3e2f08379835374b93cca75169929fe99 /src/ninja.cc | |
parent | 6a09b9653e5c6698dd687c6e9deb0b51eac39dca (diff) | |
download | Ninja-755c501084412a0896312af05cda6d4227136174.zip Ninja-755c501084412a0896312af05cda6d4227136174.tar.gz Ninja-755c501084412a0896312af05cda6d4227136174.tar.bz2 |
Improve handling of fatal errors on Windows, support creation of minidumps
Diffstat (limited to 'src/ninja.cc')
-rw-r--r-- | src/ninja.cc | 35 |
1 files changed, 35 insertions, 0 deletions
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"; |