Skip to content

Commit

Permalink
Disable capturing backtraces with HILTI exceptions in non-debug builds.
Browse files Browse the repository at this point in the history
They can be expensive to capture, and aren't used anywhere by default
unless explicitly requested.

Closes #1565.
  • Loading branch information
rsmmr committed Oct 19, 2023
1 parent 80cdec5 commit 736a4c2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion hilti/runtime/include/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand All @@ -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(); }
Expand Down
5 changes: 5 additions & 0 deletions hilti/runtime/src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions hilti/runtime/src/exception.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/src/compiler/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ Result<Nothing> 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";
Expand All @@ -1175,6 +1176,7 @@ void Driver::printHiltiException(const hilti::rt::Exception& e) {
std::cerr << " " << s << "\n";
}
}
#endif
}

Result<Nothing> Driver::initRuntime() {
Expand Down

0 comments on commit 736a4c2

Please sign in to comment.