From af635957efca0dfcd79e7c297a69db36e4971e98 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 28 May 2019 12:09:42 +0300 Subject: Print backtrace to stderr when terminating due to unhandled exception --- build2/b.cxx | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'build2/b.cxx') 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 -#include // strcmp(), strchr() +#include // strcmp(), strchr() #include -#include // cout +#include // cout +#include // set_terminate(), terminate_handler #include -#include // stderr_fd(), fdterm() +#include // stderr_fd(), fdterm() +#include // backtrace() #include #include @@ -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); -- cgit v1.1