From 9813fca3aa9b5834e903750ecb99d530938d440a Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Wed, 4 Dec 2024 15:29:30 -0800 Subject: [PATCH] Add --disable-exception-backtrace configure option --- Makefile.am | 9 +++++++-- Util/BacktraceException.hpp | 37 +++++++++++++++++++++++++++++++++---- configure.ac | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Makefile.am b/Makefile.am index 52dc8e5e2..1206f5070 100644 --- a/Makefile.am +++ b/Makefile.am @@ -571,8 +571,13 @@ EXTRA_DIST = \ generate_commit_hash.sh \ commit_hash.h -AM_CXXFLAGS = -Wall -Werror $(PTHREAD_CFLAGS) $(libev_CFLAGS) $(SQLITE3_CFLAGS) $(CURL_CFLAGS) $(CLBOSS_CXXFLAGS) $(LIBUNWIND_CFLAGS) -LDADD = libclboss.la $(PTHREAD_LIBS) $(libev_LIBS) $(SQLITE3_LIBS) $(CURL_LIBS) $(LIBUNWIND_LIBS) +AM_CXXFLAGS = -Wall -Werror $(PTHREAD_CFLAGS) $(libev_CFLAGS) $(SQLITE3_CFLAGS) $(CURL_CFLAGS) $(CLBOSS_CXXFLAGS) +LDADD = libclboss.la $(PTHREAD_LIBS) $(libev_LIBS) $(SQLITE3_LIBS) $(CURL_LIBS) + +if ENABLE_EXCEPTION_BACKTRACE +AM_CXXFLAGS += -DENABLE_EXCEPTION_BACKTRACE $(LIBUNWIND_CFLAGS) +LDADD += $(LIBUNWIND_LIBS) +endif ACLOCAL_AMFLAGS = -I m4 diff --git a/Util/BacktraceException.hpp b/Util/BacktraceException.hpp index a923fb4ca..523ef99d1 100644 --- a/Util/BacktraceException.hpp +++ b/Util/BacktraceException.hpp @@ -1,6 +1,37 @@ #ifndef UTIL_BACKTRACE_EXCEPTION_HPP #define UTIL_BACKTRACE_EXCEPTION_HPP +#ifdef HAVE_CONFIG_H +# include"config.h" +#endif + +#if !ENABLE_EXCEPTION_BACKTRACE // If not enabled or undefined + +#include + +namespace Util { + +/** class Util::BacktraceException + * + * @brief A do-nothing wrapper when backtraces are disabled. + */ + +template +class BacktraceException : public T { +public: + template + BacktraceException(Args&&... args) + : T(std::forward(args)...) {} + + const char* what() const noexcept override { + return T::what(); + } +}; + +} // namespace Util + +#else // ENABLE_EXCEPTION_BACKTRACE in force + #include #include #include @@ -11,10 +42,6 @@ #include #include -#ifdef HAVE_CONFIG_H -# include"config.h" -#endif - extern std::string g_argv0; #define UNW_LOCAL_ONLY @@ -150,4 +177,6 @@ class BacktraceException : public T { } // namespace Util +#endif // ENABLE_EXCEPTION_BACKTRACE + #endif /* UTIL_BACKTRACE_EXCEPTION_HPP */ diff --git a/configure.ac b/configure.ac index 36f3400ae..0eefceabc 100644 --- a/configure.ac +++ b/configure.ac @@ -12,6 +12,26 @@ LT_INIT([disable-shared]) m4_pattern_forbid([^PKG_]) +# Enable exception backtrace option +AC_ARG_ENABLE([exception-backtrace], + [AS_HELP_STRING([--disable-exception-backtrace], + [Disable exception backtrace in exceptions (enabled by default)])], + [case "${enable_exception_backtrace}" in + yes|no) ;; + *) AC_MSG_ERROR([invalid value for --enable-exception-backtrace: ${enable_exception_backtrace}]) ;; + esac], + [enable_exception_backtrace=yes]) # Default to "yes" + +AM_CONDITIONAL([ENABLE_EXCEPTION_BACKTRACE], [test "x$enable_exception_backtrace" = "xyes"]) +AC_SUBST([ENABLE_EXCEPTION_BACKTRACE], [$enable_exception_backtrace]) + +# Define a macro in config.h +if test "x$enable_exception_backtrace" = "xyes"; then + AC_DEFINE([ENABLE_EXCEPTION_BACKTRACE], [1], [Define if exception backtraces are enabled]) +else + AC_DEFINE([ENABLE_EXCEPTION_BACKTRACE], [0], [Define if exception backtraces are enabled]) +fi + # Checks for programs. # If CXXFLAGS is unset, remove the -g # that AC_PROG_CXX adds to it.