impr: Intercept glibc++ assertion handler

This commit is contained in:
WerWolv 2025-12-12 22:02:56 +01:00
parent ab95cdf3e5
commit 63e777c84c
2 changed files with 26 additions and 0 deletions

View File

@ -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()

View File

@ -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