diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2019-05-28 12:09:42 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2019-05-28 16:03:05 +0300 |
commit | af635957efca0dfcd79e7c297a69db36e4971e98 (patch) | |
tree | 51549d5d26e998f3b533a5dfa273a8227aa1690b /build2/b.cxx | |
parent | 9996a730b686399d385bf2696d3b9cd681b0fcec (diff) |
Print backtrace to stderr when terminating due to unhandled exception
Diffstat (limited to 'build2/b.cxx')
-rw-r--r-- | build2/b.cxx | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/build2/b.cxx b/build2/b.cxx index 882b469..022df9d 100644 --- a/build2/b.cxx +++ b/build2/b.cxx @@ -13,12 +13,14 @@ #endif #include <sstream> -#include <cstring> // strcmp(), strchr() +#include <cstring> // strcmp(), strchr() #include <typeinfo> -#include <iostream> // cout +#include <iostream> // cout +#include <exception> // set_terminate(), terminate_handler #include <libbutl/pager.mxx> -#include <libbutl/fdstream.mxx> // stderr_fd(), fdterm() +#include <libbutl/fdstream.mxx> // stderr_fd(), fdterm() +#include <libbutl/backtrace.mxx> // backtrace() #include <build2/types.hxx> #include <build2/utility.hxx> @@ -131,9 +133,25 @@ namespace build2 } } +// Print backtrace if terminating due to an unhandled exception. Note that +// custom_terminate is non-static and not a lambda to reduce the noise. +// +static terminate_handler default_terminate; + +void +custom_terminate () +{ + *diag_stream << backtrace (); + + if (default_terminate != nullptr) + default_terminate (); +} + int build2:: main (int argc, char* argv[]) { + default_terminate = set_terminate (custom_terminate); + tracer trace ("main"); int r (0); |