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
- cd build-debug
- ./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
image: deukhoofd/windowsbuilder
commands:
@ -63,6 +63,6 @@ volumes:
path: /home/docs/Arbutils
---
kind: signature
hmac: a85bf6d81ed3a176b8761dc084080dc0234b052ef3c60f07bb6b932dac4811d3
hmac: 38b3e0329693c61f83bcee5a596e6c560a591dc9f8ac8362e60387a99acb3626
...

View File

@ -10,9 +10,9 @@ set(CMAKE_CXX_STANDARD 20)
if (NOT WINDOWS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
else()
else ()
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
endif()
endif ()
option(WINDOWS "Whether the build target is Windows or not." OFF)
option(SHARED "Whether we should build a shared library, instead of a static one." OFF)
@ -31,7 +31,7 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options(-fuse-ld=lld)
endif ()
if (CMAKE_BUILD_TYPE MATCHES Release AND NOT WINDOWS)
if (CMAKE_BUILD_TYPE MATCHES Release)
# Include debug symbols in all linux builds
message("Including debug symbols")
add_compile_options(-g -gline-tables-only)
@ -66,32 +66,36 @@ if (WINDOWS)
add_compile_options(-m64)
if (SHARED)
set_target_properties(Arbutils PROPERTIES SUFFIX ".dll")
endif()
endif ()
endif (WINDOWS)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
ADD_DEFINITIONS(-DDEBUG)
endif()
endif ()
set(LINKS)
if (NOT WINDOWS AND PRETTYTRACES)
set(LINKS ${LINKS} -lbfd -ldl)
if (PRETTYTRACES)
if (NOT WINDOWS)
set(LINKS ${LINKS} -ldw)
else ()
set(LINKS ${LINKS} -ldbghelp -lpsapi)
endif ()
ADD_DEFINITIONS(-DPRETTYTRACES=1)
endif()
endif ()
if (SIGNAL_HANDLING)
ADD_DEFINITIONS(-DSIGNAL_HANDLING=1)
endif()
endif ()
# If we want to link the C and C++ library statically, link those as well.
if (STATICC)
message("Linking dependencies statically.")
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
set(LINKS -static-libgcc -static-libstdc++ ${LINKS})
endif(STATICC)
endif (STATICC)
if (WINDOWS)
set(LINKS ${LINKS} -Wl,-Bstatic -lm -lstdc++ -lpthread -Wl,-Bdynamic)
endif()
endif ()
target_link_libraries(Arbutils ${LINKS})
@ -120,4 +124,4 @@ if (NOT WINDOWS)
install(TARGETS Arbutils
CONFIGURATIONS Release
RUNTIME DESTINATION lib)
endif()
endif ()

View File

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