From 52f1e90df5a8c98c5dc9ae3d50f7c24928c7b69b Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 28 May 2019 12:28:05 +0300 Subject: Print backtrace to stderr when terminating due to unhandled exception --- bdep/bdep.cxx | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'bdep/bdep.cxx') diff --git a/bdep/bdep.cxx b/bdep/bdep.cxx index e4146e5..e02687d 100644 --- a/bdep/bdep.cxx +++ b/bdep/bdep.cxx @@ -6,8 +6,11 @@ # include // signal() #endif -#include // strcmp() +#include // strcmp() #include +#include // set_terminate(), terminate_handler + +#include // backtrace() #include #include @@ -152,12 +155,28 @@ init (const common_options& co, return o; } +// 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 bdep:: main (int argc, char* argv[]) try { using namespace cli; + default_terminate = set_terminate (custom_terminate); + stderr_term = fdterm (stderr_fd ()); argv0 = argv[0]; -- cgit v1.1