From 5d20ed31852c2075f08723a8c55e9db8240fb797 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 20 Aug 2021 15:57:54 -0600 Subject: [PATCH] Make sure that copying exceptions doesn't imply dropping the backtrace. --- include/deal.II/base/exceptions.h | 2 +- source/base/exceptions.cc | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/deal.II/base/exceptions.h b/include/deal.II/base/exceptions.h index d882028436..a30b345155 100644 --- a/include/deal.II/base/exceptions.h +++ b/include/deal.II/base/exceptions.h @@ -155,7 +155,7 @@ protected: #ifdef DEAL_II_HAVE_GLIBC_STACKTRACE /** - * array of pointers that contains the raw stack trace + * Array of pointers that contains the raw stack trace. */ void *raw_stacktrace[25]; #endif diff --git a/source/base/exceptions.cc b/source/base/exceptions.cc index f45a6f614f..b04ef35882 100644 --- a/source/base/exceptions.cc +++ b/source/base/exceptions.cc @@ -59,18 +59,24 @@ namespace deal_II_exceptions bool allow_abort_on_exception = true; } // namespace internals + + void set_additional_assert_output(const std::string &p) { internals::get_additional_assert_output() = p; } + + void suppress_stacktrace_in_exceptions() { internals::show_stacktrace = false; } + + void disable_abort_on_exception() { @@ -104,13 +110,17 @@ ExceptionBase::ExceptionBase(const ExceptionBase &exc) , cond(exc.cond) , exc(exc.exc) , stacktrace(nullptr) - , // don't copy stacktrace to avoid double de-allocation problem - n_stacktrace_frames(0) + , n_stacktrace_frames(exc.n_stacktrace_frames) , what_str("") // don't copy the error message, it gets generated dynamically // by what() { #ifdef DEAL_II_HAVE_GLIBC_STACKTRACE - std::fill(std::begin(raw_stacktrace), std::end(raw_stacktrace), nullptr); + // Copy the raw_stacktrace pointers. We don't own them, they just point to the + // addresses of symbols in the executable's/library's symbol tables -- and as + // a consequence, it is safe to copy these pointers + std::copy(std::begin(exc.raw_stacktrace), + std::end(exc.raw_stacktrace), + std::begin(raw_stacktrace)); #endif } @@ -147,6 +157,8 @@ ExceptionBase::set_fields(const char *f, #endif } + + const char * ExceptionBase::what() const noexcept { @@ -170,6 +182,7 @@ ExceptionBase::what() const noexcept } + const char * ExceptionBase::get_exc_name() const { -- 2.39.5