Skip to content

Commit

Permalink
[xmake, Core] fallback stacktrace to cpptrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthapz committed Oct 26, 2024
1 parent e20939e commit 532aa72
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
24 changes: 4 additions & 20 deletions modules/stormkit/Core/Utils/ErrorHandling.mpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import std;
import :Meta.Concepts;
import :Meta.Traits;

import :Utils.Descriptors;
import :Utils.Stacktrace;

export namespace stormkit::core {
Expand Down Expand Up @@ -61,9 +60,8 @@ namespace stormkit::core {
-> decltype(auto) {
return [message = std::move(message),
location = std::move(location)] NORETURN_LAMBDA() -> std::optional<T> {
auto stderr = getSTDErr();

std::println(stderr,
std::println(std::cerr,
"[Expects] failed in \n"
" > file: {}\n"
" line: {}\n"
Expand All @@ -74,8 +72,6 @@ namespace stormkit::core {
location.function_name(),
message);

printStacktrace();

std::terminate();
};
}
Expand All @@ -85,9 +81,7 @@ namespace stormkit::core {
template<typename T>
constexpr auto expects(std::source_location location) noexcept -> decltype(auto) {
return [location = std::move(location)] NORETURN_LAMBDA(auto&& error) -> std::optional<T> {
auto stderr = getSTDErr();

std::println(stderr,
std::println(std::cerr,
"[Expects] failed in\n"
" > file: {}\n"
" line: {}\n"
Expand All @@ -98,8 +92,6 @@ namespace stormkit::core {
location.function_name(),
error);

printStacktrace();

std::terminate();
};
}
Expand All @@ -110,9 +102,7 @@ namespace stormkit::core {
-> decltype(auto) {
return [message = std::move(message), location = std::move(location)] NORETURN_LAMBDA(
auto&& error) -> std::expected<void, CanonicalType<decltype(error)>> {
auto stderr = getSTDErr();

std::println(stderr,
std::println(std::cerr,
"[Expects] failed in \n"
" > file: {}\n"
" line: {}\n"
Expand All @@ -125,8 +115,6 @@ namespace stormkit::core {
message,
error);

printStacktrace();

std::terminate();
};
}
Expand All @@ -136,9 +124,7 @@ namespace stormkit::core {
constexpr auto expects(std::source_location location) noexcept -> decltype(auto) {
return [location = std::move(location)] NORETURN_LAMBDA(
auto&& error) -> std::expected<void, CanonicalType<decltype(error)>> {
auto stderr = getSTDErr();

std::println(stderr,
std::println(std::cerr,
"[Expects] failed in\n"
" > file: {}\n"
" line: {}\n"
Expand All @@ -149,8 +135,6 @@ namespace stormkit::core {
location.function_name(),
std::forward<decltype(error)>(error));

printStacktrace();

std::terminate();
};
}
Expand Down
4 changes: 2 additions & 2 deletions modules/stormkit/Core/Utils/SignalHandler.mpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export namespace stormkit::core {

namespace stormkit::core {
extern "C" auto terminateHandler() noexcept -> void {
printStacktrace(1);
printStacktrace(3);
}

extern "C" auto signalHandler(int signum) noexcept -> void {
std::signal(signum, SIG_DFL);
printStacktrace(1);
printStacktrace(3);
std::raise(SIGABRT);
}

Expand Down
19 changes: 11 additions & 8 deletions src/Core/Stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

module;

#include <cstdio>
#if not defined(STD_STACKTRACE_SUPPORTED)
#include <cpptrace/cpptrace.hpp>
#endif

module stormkit.Core;

Expand All @@ -13,28 +15,29 @@ import std;
namespace stormkit::core {
/////////////////////////////////////
/////////////////////////////////////
auto printStacktrace([[maybe_unused]] int ignore_count) noexcept -> void {
auto printStacktrace(int ignore_count) noexcept -> void {
const auto thread_name = getCurrentThreadName();
if (not std::empty(thread_name))
std::println(stderr,
std::println(std::cerr,
"================= CALLSTACK (thread name: {}, id: {}) =================",
thread_name,
std::this_thread::get_id());
else
std::println(stderr,
std::println(std::cerr,
"================= CALLSTACK (thread id: {}) =================",
std::this_thread::get_id());
#if defined(STD_STACKTRACE_SUPPORTED)
const auto st = std::stacktrace::current();

#else
const auto st = cpptrace::stacktrace::current();
#endif
auto i = 0;
for (auto&& frame : st) {
if (i < ignore_count) {
i += 1;
continue;
}
std::println("{}# {}", (i++ - ignore_count), frame);
std::println(std::cerr, "{}# {}", (i++ - ignore_count), frame);
}
#endif
}
}
} // namespace stormkit::core
24 changes: 14 additions & 10 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ modules = {
add_includedirs("$(buildir)/.gens/include", { public = true })
add_cxflags("clang::-Wno-language-extension-token")

on_load(function(target)
local has_stacktrace = target:check_cxxsnippets({
test = [[
void test() {
std::stacktrace::current();
}
]],
}, { configs = { languages = "c++23" }, includes = { "stacktrace" } })

if not has_stacktrace then
print("No std C++23 stacktrace, falling back to cpptrace")
target:add("packages", "cpptrace", "libdwarf")
end
end)
on_config(function(target)
local output, errors = os.iorunv("git", { "rev-parse", "--abbrev-ref", "HEAD" })

Expand All @@ -29,14 +43,6 @@ modules = {
output, errors = os.iorunv("git", { "rev-parse", "--verify", "HEAD" })

target:set("configvar", "STORMKIT_GIT_COMMIT_HASH", output:trim())

local has_stacktrace = target:check_cxxsnippets({
test = [[
void test() {
std::stacktrace::current();
}
]],
}, { configs = { languages = "c++23" }, includes = { "stacktrace" } })
end)
end,
},
Expand Down Expand Up @@ -335,8 +341,6 @@ elseif is_mode("releasedbg") then
set_optimize("fast")
set_symbols("debug", "hidden")
add_cxflags("-fno-omit-frame-pointer", { tools = { "clang", "gcc" } })
add_mxflags("-fno-omit-frame-pointer", { tools = { "clang", "gcc" } })
add_cxflags("-ggdb3", { tools = { "clang", "gcc" } })
add_mxflags("-ggdb3", { tools = { "clang", "gcc" } })
end

Expand Down

0 comments on commit 532aa72

Please sign in to comment.