diff --git a/lib/trace/source/stacktrace.cpp b/lib/trace/source/stacktrace.cpp index aff6e4340..3286f969f 100644 --- a/lib/trace/source/stacktrace.cpp +++ b/lib/trace/source/stacktrace.cpp @@ -23,6 +23,13 @@ namespace hex::trace { #if defined(HEX_HAS_STD_STACKTRACE) && __has_include() #include + + #if __has_include() + + #include + #include + + #endif namespace hex::trace { @@ -36,10 +43,35 @@ namespace hex::trace { auto stackTrace = std::stacktrace::current(); for (const auto &entry : stackTrace) { - if (entry.source_line() == 0 && entry.source_file().empty()) + if (entry.source_line() == 0 && entry.source_file().empty()) { + #if __has_include() + Dl_info info = {}; + dladdr(reinterpret_cast(entry.native_handle()), &info); + + std::string description; + + auto path = info.dli_fname != nullptr ? std::optional{info.dli_fname} : std::nullopt; + auto filePath = path ? path->string() : "??"; + auto fileName = path ? path->filename().string() : ""; + + if (info.dli_sname != nullptr) { + description = tryDemangle(info.dli_sname); + if (info.dli_saddr != reinterpret_cast(entry.native_handle())) { + auto symOffset = entry.native_handle() - reinterpret_cast(info.dli_saddr); + description += std::format("+0x{:x}", symOffset); + } + } else { + auto rvaOffset = entry.native_handle() - reinterpret_cast(info.dli_fbase); + description = std::format("{}+0x{:08x}", fileName, rvaOffset); + } + + result.emplace_back(filePath, description, 0); + #else result.emplace_back("", "??", 0); - else + #endif + } else { result.emplace_back(entry.source_file(), entry.description(), entry.source_line()); + } } return { result, "std::stacktrace" }; @@ -251,4 +283,4 @@ namespace hex::trace { } } -#endif \ No newline at end of file +#endif