From 25954d5503226e01c64904637dda17f9b3ba3a7e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 2 Jun 2023 20:29:58 -0500 Subject: [PATCH] [ox/std] Make stacktrace code more readable --- deps/ox/src/ox/std/stacktrace.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/deps/ox/src/ox/std/stacktrace.cpp b/deps/ox/src/ox/std/stacktrace.cpp index 9dc1f387..a19ff851 100644 --- a/deps/ox/src/ox/std/stacktrace.cpp +++ b/deps/ox/src/ox/std/stacktrace.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2022 gary@drinkingtea.net + * Copyright 2015 - 2023 gary@drinkingtea.net * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,7 +7,6 @@ */ #if defined(OX_USE_STDLIB) && __has_include() -#include #if __has_include() #include @@ -27,13 +26,14 @@ namespace ox { #if defined(OX_USE_STDLIB) && __has_include() [[nodiscard]] -static auto symbolicate([[maybe_unused]]char **frames, - [[maybe_unused]]std::size_t symbolsLen, +static auto symbolicate([[maybe_unused]]void **frames, + [[maybe_unused]]std::size_t frameCnt, [[maybe_unused]]const char *linePrefix) { using StrT = BasicString<100>; Vector out; #if __has_include() && __has_include() - for (auto i = 0u; i < symbolsLen; ++i) { + const auto mangledSymbols = backtrace_symbols(frames, static_cast(frameCnt)); + for (auto i = 0u; i < frameCnt; ++i) { Dl_info info; if (dladdr(frames[i], &info) && info.dli_sname) { int status = -1; @@ -43,8 +43,9 @@ static auto symbolicate([[maybe_unused]]char **frames, continue; } } - out.emplace_back(sfmt("{}", frames[i])); + out.emplace_back(sfmt("{}: {}", i, mangledSymbols[i])); } + free(mangledSymbols); #endif // __has_include() return out; } @@ -56,9 +57,7 @@ void printStackTrace([[maybe_unused]]unsigned shave) noexcept { Vector frames(FrameCnt); frames.resize(static_cast(backtrace(frames.data(), static_cast(frames.size())))); if (frames.size() - shave > 2) { - const auto symbols = backtrace_symbols(frames.data() + shave, static_cast(frames.size() - shave)); - const auto symbolicatedStacktrace = symbolicate(reinterpret_cast(frames.data() + shave), frames.size() - shave, "\t"); - free(symbols); + const auto symbolicatedStacktrace = symbolicate(frames.data() + shave, frames.size() - shave, "\t"); oxErr("Stacktrace:\n"); for (const auto &s : symbolicatedStacktrace) { oxErrf("\t{}\n", s);