diff --git a/lib/trace/CMakeLists.txt b/lib/trace/CMakeLists.txt index 2551b671e..28e93ad9d 100644 --- a/lib/trace/CMakeLists.txt +++ b/lib/trace/CMakeLists.txt @@ -81,6 +81,9 @@ if (IMHEX_TRACE_EXCEPTIONS) if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") target_link_options(tracing ${LIBIMHEX_LIBRARY_TYPE_PUBLIC} "-Wl,--wrap=__cxa_throw") target_compile_definitions(tracing ${LIBIMHEX_LIBRARY_TYPE_PRIVATE} HEX_WRAP_CXA_THROW) + + target_link_options(tracing ${LIBIMHEX_LIBRARY_TYPE_PUBLIC} "-Wl,--wrap=_ZSt21__glibcxx_assert_failPKciS0_S0_") + target_compile_definitions(tracing ${LIBIMHEX_LIBRARY_TYPE_PRIVATE} HEX_WRAP_GLIBCXX_ASSERT_FAIL) elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Not supported currently endif() diff --git a/lib/trace/source/exceptions.cpp b/lib/trace/source/exceptions.cpp index d963abc92..1c49b06ca 100644 --- a/lib/trace/source/exceptions.cpp +++ b/lib/trace/source/exceptions.cpp @@ -35,4 +35,27 @@ namespace hex::trace { } +#endif + +#if defined(HEX_WRAP_GLIBCXX_ASSERT_FAIL) + +extern "C" { + + [[noreturn]] void __wrap__ZSt21__glibcxx_assert_failPKciS0_S0_(const char* file, int line, const char* function, const char* condition) { + if (file != nullptr && function != nullptr && condition != nullptr) { + fprintf(stderr, "Assertion failed (glibc++): (%s), function %s, file %s, line %d.\n", condition, function, file, line); + } else if (function != nullptr) { + fprintf(stderr, "%s: Undefined behavior detected (glibc++).\n", function); + } + + auto stackTrace = hex::trace::getStackTrace(); + for (const auto &entry : stackTrace.stackFrames) { + fprintf(stderr, " %s at %s:%d\n", entry.function.c_str(), entry.file.c_str(), entry.line); + } + + std::terminate(); + } + +} + #endif \ No newline at end of file