diff --git a/lib/trace/CMakeLists.txt b/lib/trace/CMakeLists.txt index 01044b2b6..4d0f3eebe 100644 --- a/lib/trace/CMakeLists.txt +++ b/lib/trace/CMakeLists.txt @@ -46,7 +46,7 @@ if (NOT IMHEX_DISABLE_STACKTRACE) if (HAVE_STDCPPEXP) message(STATUS " enabled!") - target_link_libraries(tracing ${LIBIMHEX_LIBRARY_TYPE_PRIVATE} stdc++exp) + target_link_libraries(tracing ${LIBIMHEX_LIBRARY_TYPE_PRIVATE} stdc++exp dl) if (HAVE_STDCPP_LIBBACKTRACE) target_link_libraries(tracing ${LIBIMHEX_LIBRARY_TYPE_PRIVATE} stdc++_libbacktrace) endif() diff --git a/lib/trace/source/stacktrace.cpp b/lib/trace/source/stacktrace.cpp index 3286f969f..30b88ba4a 100644 --- a/lib/trace/source/stacktrace.cpp +++ b/lib/trace/source/stacktrace.cpp @@ -28,11 +28,18 @@ namespace hex::trace { #include #include + #include #endif namespace hex::trace { + static std::string toUTF8String(const auto &value) { + auto result = value.generic_u8string(); + + return { result.begin(), result.end() }; + } + void initialize() { } @@ -44,31 +51,31 @@ namespace hex::trace { for (const auto &entry : stackTrace) { if (entry.source_line() == 0 && entry.source_file().empty()) { - #if __has_include() - Dl_info info = {}; - dladdr(reinterpret_cast(entry.native_handle()), &info); + #if __has_include() + Dl_info info = {}; + dladdr(reinterpret_cast(entry.native_handle()), &info); - std::string description; + 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() : ""; + auto path = info.dli_fname != nullptr ? std::optional{info.dli_fname} : std::nullopt; + auto filePath = path ? toUTF8String(*path) : "??"; + auto fileName = path ? toUTF8String(path->filename()) : ""; - 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); + if (info.dli_sname != nullptr) { + description = demangle(info.dli_sname); + if (info.dli_saddr != reinterpret_cast(entry.native_handle())) { + auto symOffset = entry.native_handle() - reinterpret_cast(info.dli_saddr); + description += fmt::format("+0x{:x}", symOffset); + } + } else { + auto rvaOffset = entry.native_handle() - reinterpret_cast(info.dli_fbase); + description = fmt::format("{}+0x{:08x}", fileName, rvaOffset); } - } 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); - #endif + result.emplace_back(filePath, description, 0); + #else + result.emplace_back("", "??", 0); + #endif } else { result.emplace_back(entry.source_file(), entry.description(), entry.source_line()); }