Cleanup exception stacktraces, so the compile the same on Windows.
continuous-integration/drone/push Build is failing Details

Stacktraces still appear to be empty on Windows, but that's an issue for a later day.
This commit is contained in:
Deukhoofd 2022-02-26 14:55:28 +01:00
parent eb8356145f
commit ba411d011b
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 27 additions and 42 deletions

View File

@ -28,7 +28,7 @@ steps:
- cmake --build build-release --target all -- -j 4 - cmake --build build-release --target all -- -j 4
- cd build-debug - cd build-debug
- ./ArbutilsTests -s --duration=true --force-colors=true - ./ArbutilsTests -s --duration=true --force-colors=true
- valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --leak-resolution=med --track-origins=yes --vgdb=no --error-exitcode=1 ./ArbutilsTests --test-case-exclude="Throw exception get stack trace" - valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --leak-resolution=med --track-origins=yes --vgdb=no --error-exitcode=1 ./ArbutilsTests
- name: test-release-windows - name: test-release-windows
image: deukhoofd/windowsbuilder image: deukhoofd/windowsbuilder
commands: commands:
@ -63,6 +63,6 @@ volumes:
path: /home/docs/Arbutils path: /home/docs/Arbutils
--- ---
kind: signature kind: signature
hmac: a85bf6d81ed3a176b8761dc084080dc0234b052ef3c60f07bb6b932dac4811d3 hmac: 38b3e0329693c61f83bcee5a596e6c560a591dc9f8ac8362e60387a99acb3626
... ...

View File

@ -31,7 +31,7 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options(-fuse-ld=lld) add_link_options(-fuse-ld=lld)
endif () endif ()
if (CMAKE_BUILD_TYPE MATCHES Release AND NOT WINDOWS) if (CMAKE_BUILD_TYPE MATCHES Release)
# Include debug symbols in all linux builds # Include debug symbols in all linux builds
message("Including debug symbols") message("Including debug symbols")
add_compile_options(-g -gline-tables-only) add_compile_options(-g -gline-tables-only)
@ -74,8 +74,12 @@ if (CMAKE_BUILD_TYPE MATCHES "Debug")
endif () endif ()
set(LINKS) set(LINKS)
if (NOT WINDOWS AND PRETTYTRACES) if (PRETTYTRACES)
set(LINKS ${LINKS} -lbfd -ldl) if (NOT WINDOWS)
set(LINKS ${LINKS} -ldw)
else ()
set(LINKS ${LINKS} -ldbghelp -lpsapi)
endif ()
ADD_DEFINITIONS(-DPRETTYTRACES=1) ADD_DEFINITIONS(-DPRETTYTRACES=1)
endif () endif ()
if (SIGNAL_HANDLING) if (SIGNAL_HANDLING)

View File

@ -9,12 +9,10 @@
#include <source_location> #include <source_location>
#endif #endif
#if !WINDOWS #if PRETTYTRACES && !WINDOWS
#if PRETTYTRACES #define BACKWARD_HAS_DW 1
#define BACKWARD_HAS_BFD 1
#endif #endif
#include <backward.hpp> #include <backward.hpp>
#endif
#if defined(__clang__) #if defined(__clang__)
namespace std { namespace std {
@ -35,35 +33,24 @@ static constexpr const char* file_name(const char* path) {
namespace ArbUt { namespace ArbUt {
/// @brief Implementation of std::logic_error that gives a stack trace when thrown. /// @brief Implementation of std::logic_error that gives a stack trace when thrown.
class Exception : std::logic_error { class Exception : std::logic_error {
#if !WINDOWS
backward::StackTrace _stack; backward::StackTrace _stack;
#endif
public: public:
/// @brief Throw an exception with specified message. /// @brief Throw an exception with specified message.
explicit Exception(const std::string& msg) : std::logic_error(msg) { explicit Exception(const std::string& msg) : std::logic_error(msg), _stack({}) {
#if !WINDOWS
_stack.load_here(32); _stack.load_here(32);
#if DEBUG #if DEBUG
_stack.skip_n_firsts(2); _stack.skip_n_firsts(2);
#else #else
_stack.skip_n_firsts(0); _stack.skip_n_firsts(0);
#endif
#endif #endif
} }
/// @brief Copy operator. /// @brief Copy operator.
Exception(const Exception& e) noexcept Exception(const Exception& e) noexcept : std::logic_error(e.what()), _stack(e._stack) {}
: std::logic_error(e.what())
#if !WINDOWS
,
_stack(e._stack)
#endif
{
}
template <typename... Args> template <typename... Args>
/// @brief Throw a nicely formatted exception. The exception message will have the file name, line number, message, /// @brief Throw a nicely formatted exception. The exception message will have the file name, line number,
/// along with any optional parameters passed. /// message, along with any optional parameters passed.
[[noreturn]] static void Throw(const std::string& expression, const std::source_location& location, [[noreturn]] static void Throw(const std::string& expression, const std::source_location& location,
const Args... args) { const Args... args) {
std::stringstream error; std::stringstream error;
@ -86,14 +73,9 @@ namespace ArbUt {
/// retrieved. /// retrieved.
[[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6, [[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6,
[[maybe_unused]] bool include_addr = true) const { [[maybe_unused]] bool include_addr = true) const {
#if !WINDOWS
return BuildStacktraceFromStack(_stack, depth, include_addr); return BuildStacktraceFromStack(_stack, depth, include_addr);
#else
return "Stack trace not available on Windows.";
#endif
} }
#if !WINDOWS
/// @brief Builds a string from a given stack. /// @brief Builds a string from a given stack.
/// @param stack The stack to build the string from. /// @param stack The stack to build the string from.
/// @param depth The max number of stacks it should retrieve. /// @param depth The max number of stacks it should retrieve.
@ -170,7 +152,6 @@ namespace ArbUt {
ss << fileName << "[" << source.line << "] " << snippet << std::endl; ss << fileName << "[" << source.line << "] " << snippet << std::endl;
} }
#endif
}; };
} }