From 61ce88ba9b5cf404b4b7ac55c6741c9b48f46866 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 13 Feb 2023 08:27:08 +0100 Subject: [PATCH] build: Fix build on systems that have no backtrace or execinfo Fixes #932 --- lib/libimhex/source/helpers/stacktrace.cpp | 127 +++++++++++---------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/lib/libimhex/source/helpers/stacktrace.cpp b/lib/libimhex/source/helpers/stacktrace.cpp index b6ee42abb..1874184ca 100644 --- a/lib/libimhex/source/helpers/stacktrace.cpp +++ b/lib/libimhex/source/helpers/stacktrace.cpp @@ -94,75 +94,82 @@ } -#elif defined(HEX_HAS_EXECINFO) && __has_include(BACKTRACE_HEADER) +#elif defined(HEX_HAS_EXECINFO) - #include BACKTRACE_HEADER - #include - #include - #include + #if __has_include(BACKTRACE_HEADER) - namespace hex::stacktrace { + #include BACKTRACE_HEADER + #include + #include + #include - void initialize() { + namespace hex::stacktrace { - } - - std::vector getStackTrace() { - static std::vector result; - - std::array addresses; - auto count = backtrace(addresses.data(), addresses.size()); - auto functions = backtrace_symbols(addresses.data(), count); - - for (i32 i = 0; i < count; i++) - result.push_back(StackFrame { "", functions[i], 0 }); - - return result; - } - - } - -#elif defined(HEX_HAS_BACKTRACE) && __has_include(BACKTRACE_HEADER) - - #include BACKTRACE_HEADER - #include - #include - #include - - namespace hex::stacktrace { - - static struct backtrace_state *s_backtraceState; - - - void initialize() { - if (auto executablePath = fs::getExecutablePath(); executablePath.has_value()) { - static std::string path = executablePath->string(); - s_backtraceState = backtrace_create_state(path.c_str(), 1, [](void *, const char *msg, int) { log::error("{}", msg); }, nullptr); - } - } - - std::vector getStackTrace() { - static std::vector result; - - result.clear(); - if (s_backtraceState != nullptr) { - backtrace_full(s_backtraceState, 0, [](void *, uintptr_t, const char *fileName, int lineNumber, const char *function) -> int { - if (fileName == nullptr) - fileName = "??"; - if (function == nullptr) - function = "??"; - - result.push_back(StackFrame { std::fs::path(fileName).filename().string(), llvm::demangle(function), u32(lineNumber) }); - - return 0; - }, nullptr, nullptr); + void initialize() { } - return result; + std::vector getStackTrace() { + static std::vector result; + + std::array addresses; + auto count = backtrace(addresses.data(), addresses.size()); + auto functions = backtrace_symbols(addresses.data(), count); + + for (i32 i = 0; i < count; i++) + result.push_back(StackFrame { "", functions[i], 0 }); + + return result; + } + + } + #endif + +#elif defined(HEX_HAS_BACKTRACE) + + #if __has_include(BACKTRACE_HEADER) + + #include BACKTRACE_HEADER + #include + #include + #include + + namespace hex::stacktrace { + + static struct backtrace_state *s_backtraceState; + + + void initialize() { + if (auto executablePath = fs::getExecutablePath(); executablePath.has_value()) { + static std::string path = executablePath->string(); + s_backtraceState = backtrace_create_state(path.c_str(), 1, [](void *, const char *msg, int) { log::error("{}", msg); }, nullptr); + } + } + + std::vector getStackTrace() { + static std::vector result; + + result.clear(); + if (s_backtraceState != nullptr) { + backtrace_full(s_backtraceState, 0, [](void *, uintptr_t, const char *fileName, int lineNumber, const char *function) -> int { + if (fileName == nullptr) + fileName = "??"; + if (function == nullptr) + function = "??"; + + result.push_back(StackFrame { std::fs::path(fileName).filename().string(), llvm::demangle(function), u32(lineNumber) }); + + return 0; + }, nullptr, nullptr); + + } + + return result; + } + } - } + #endif #else