From fc108523ca8df2043e483bf10e479471f8aa64a6 Mon Sep 17 00:00:00 2001 From: ravinsp <33562092+ravinsp@users.noreply.github.com> Date: Sun, 15 Dec 2019 23:28:49 +0530 Subject: [PATCH] Added stack trace logging for exceptions. --- CMakeLists.txt | 10 ++++++---- src/main.cpp | 29 ++++++++++++++++++++++++---- src/pchheader.hpp | 5 +++++ src/statefs/state_monitor/fusefs.cpp | 28 ++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c7fe8e1..de160e2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,8 @@ target_link_libraries(hpsupport libboost_log.a libboost_log_setup.a libboost_filesystem.a + libboost_stacktrace_backtrace.a + backtrace pthread crypto ssl @@ -124,10 +126,10 @@ target_link_libraries(hpstatemon add_dependencies(hpcore hpstatemon ) -add_custom_command(TARGET hpcore POST_BUILD - COMMAND strip ./build/hpcore - COMMAND strip ./build/hpstatemon -) +# add_custom_command(TARGET hpcore POST_BUILD +# COMMAND strip ./build/hpcore +# COMMAND strip ./build/hpstatemon +# ) target_precompile_headers(hpsupport PUBLIC src/pchheader.hpp) target_precompile_headers(hpcore REUSE_FROM hpsupport) diff --git a/src/main.cpp b/src/main.cpp index eeecadd8..b7a8458e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,15 +75,32 @@ void signal_handler(int signum) exit(signum); } +namespace boost +{ /** * Global exception handler for boost exceptions. */ -void boost::throw_exception(std::exception const &e) +void throw_exception(std::exception const &e) { - std::cerr << "Boost error:" << e.what() << "\n"; + LOG_ERR << "Boost error: " << e.what() << "\n" + << boost::stacktrace::stacktrace(); exit(1); } +inline void assertion_failed_msg(char const *expr, char const *msg, char const *function, char const * /*file*/, long /*line*/) +{ + LOG_ERR << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n" + << "Backtrace:\n" + << boost::stacktrace::stacktrace() << '\n'; + std::abort(); +} + +inline void assertion_failed(char const *expr, char const *function, char const *file, long line) +{ + ::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line); +} +} // namespace boost + /** * Global exception handler for std exceptions. */ @@ -102,14 +119,18 @@ void std_terminate() noexcept } catch (...) { - std::cerr << "std error: Terminated due to unknown exception" << "\n"; + LOG_ERR << "std error: Terminated due to unknown exception" + << "\n"; } } else { - std::cerr << "std error: Terminated due to unknown reason" << "\n"; + LOG_ERR << "std error: Terminated due to unknown reason" + << "\n"; } + LOG_ERR << boost::stacktrace::stacktrace(); + exit(1); } diff --git a/src/pchheader.hpp b/src/pchheader.hpp index 63165079..83bc627e 100644 --- a/src/pchheader.hpp +++ b/src/pchheader.hpp @@ -3,6 +3,10 @@ // This will direct all boost exceptions to our error handler. #define BOOST_NO_EXCEPTIONS +// Enable boost strack trace. +#define BOOST_STACKTRACE_USE_BACKTRACE +// Enable custom handlers for boost assertion failures. +#define BOOST_ENABLE_ASSERT_DEBUG_HANDLER #include #include @@ -25,6 +29,7 @@ #include #include #include +#include #include #include #include diff --git a/src/statefs/state_monitor/fusefs.cpp b/src/statefs/state_monitor/fusefs.cpp index 1f1fcd44..0220b7a4 100644 --- a/src/statefs/state_monitor/fusefs.cpp +++ b/src/statefs/state_monitor/fusefs.cpp @@ -1354,4 +1354,30 @@ int main(int argc, char *argv[]) } return fusefs::start(argv[0], argv[1], argv[2]); -} \ No newline at end of file +} + +namespace boost +{ +/** + * Global exception handler for boost exceptions. + */ +void throw_exception(std::exception const &e) +{ + std::cerr << "Boost error: " << e.what() << "\n" + << boost::stacktrace::stacktrace(); + exit(1); +} + +inline void assertion_failed_msg(char const *expr, char const *msg, char const *function, char const * /*file*/, long /*line*/) +{ + std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n" + << "Backtrace:\n" + << boost::stacktrace::stacktrace() << '\n'; + std::abort(); +} + +inline void assertion_failed(char const *expr, char const *function, char const *file, long line) +{ + ::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line); +} +} // namespace boost \ No newline at end of file