From 736a4c2fb742b92527ccb0e502829567fc55fad1 Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Thu, 19 Oct 2023 17:23:21 +0200 Subject: [PATCH] Disable capturing backtraces with HILTI exceptions in non-debug builds. They can be expensive to capture, and aren't used anywhere by default unless explicitly requested. Closes #1565. --- hilti/runtime/include/exception.h | 7 ++++++- hilti/runtime/src/configuration.cc | 5 +++++ hilti/runtime/src/exception.cc | 2 ++ hilti/toolchain/src/compiler/driver.cc | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/hilti/runtime/include/exception.h b/hilti/runtime/include/exception.h index 433e82a3b..08b06a256 100644 --- a/hilti/runtime/include/exception.h +++ b/hilti/runtime/include/exception.h @@ -49,11 +49,13 @@ class Exception : public std::runtime_error { /** Returns the location associated with the exception. */ auto location() const { return _location; } +#ifndef NDEBUG /** * Returns a stack backtrace captured at the time the exception was * thrown. */ auto backtrace() const { return _backtrace.backtrace(); } +#endif protected: enum Internal {}; @@ -66,7 +68,10 @@ class Exception : public std::runtime_error { std::string _description; std::string _location; - Backtrace _backtrace; + +#ifndef NDEBUG + Backtrace _backtrace; // capturing the backtrace can be pretty expensive, so limit to debug builds +#endif }; inline std::ostream& operator<<(std::ostream& stream, const Exception& e) { return stream << e.what(); } diff --git a/hilti/runtime/src/configuration.cc b/hilti/runtime/src/configuration.cc index 9633cb4eb..2abc400c6 100644 --- a/hilti/runtime/src/configuration.cc +++ b/hilti/runtime/src/configuration.cc @@ -24,5 +24,10 @@ void configuration::set(Configuration cfg) { if ( isInitialized() ) hilti::rt::fatalError("attempt to change configuration after library has already been initialized"); +#ifndef NDEBUG + if ( cfg.show_backtraces ) + hilti::rt::warning("printing of exception backtraces enabled, but not supported in release builds"); +#endif + *detail::__configuration = std::move(cfg); } diff --git a/hilti/runtime/src/exception.cc b/hilti/runtime/src/exception.cc index 1a30eee0f..e1efa9c51 100644 --- a/hilti/runtime/src/exception.cc +++ b/hilti/runtime/src/exception.cc @@ -41,6 +41,7 @@ HILTI_EXCEPTION_IMPL(StackSizeExceeded) static void printException(const std::string& msg, const Exception& e, std::ostream& out) { out << "[libhilti] " << msg << " " << demangle(typeid(e).name()) << ": " << e.what() << std::endl; +#ifndef NDEBUG if ( ! configuration::get().show_backtraces ) return; @@ -52,6 +53,7 @@ static void printException(const std::string& msg, const Exception& e, std::ostr for ( const auto& s : *bt ) out << "[libhilti] " << s << "\n"; +#endif } Exception::Exception(Internal, const char* type, const std::string& what, std::string_view desc, diff --git a/hilti/toolchain/src/compiler/driver.cc b/hilti/toolchain/src/compiler/driver.cc index 2c95a5792..44448610d 100644 --- a/hilti/toolchain/src/compiler/driver.cc +++ b/hilti/toolchain/src/compiler/driver.cc @@ -1167,6 +1167,7 @@ Result Driver::jitUnits() { void Driver::printHiltiException(const hilti::rt::Exception& e) { std::cerr << fmt("uncaught exception %s: %s", util::demangle(typeid(e).name()), e.what()) << std::endl; +#ifndef NDEBUG if ( _driver_options.show_backtraces ) { if ( auto bt = e.backtrace(); ! bt->empty() ) { std::cerr << "backtrace:\n"; @@ -1175,6 +1176,7 @@ void Driver::printHiltiException(const hilti::rt::Exception& e) { std::cerr << " " << s << "\n"; } } +#endif } Result Driver::initRuntime() {