build: Move all demangler uses into the trace library

This commit is contained in:
WerWolv
2025-07-30 20:57:53 +02:00
parent ec9461741c
commit 6fcc3e8cb3
12 changed files with 28 additions and 59 deletions

View File

@@ -21,6 +21,7 @@ namespace hex::trace {
std::string implementationName;
};
StackTraceResult getStackTrace();
StackTraceResult getStackTrace();
[[nodiscard]] std::string demangle(const std::string &mangledName);
}

View File

@@ -3,21 +3,23 @@
#include <llvm/Demangle/Demangle.h>
namespace {
namespace hex::trace {
[[maybe_unused]] std::string tryDemangle(const std::string &symbolName) {
if (auto variant1 = llvm::demangle(symbolName); variant1 != symbolName)
return variant1;
std::string demangle(const std::string &symbolName) {
if (auto result = llvm::demangle(symbolName); result != symbolName)
return result;
if (auto variant2 = llvm::demangle(std::string("_") + symbolName); variant2 != std::string("_") + symbolName)
return variant2;
if (auto result = llvm::demangle(std::string("_") + symbolName); result != std::string("_") + symbolName)
return result;
if (auto result = llvm::demangle(std::string("_Z") + symbolName); result != std::string("_Z") + symbolName)
return result;
return symbolName;
}
}
#if defined(HEX_HAS_STD_STACKTRACE) && __has_include(<stacktrace>)
#include <stacktrace>
@@ -143,7 +145,7 @@ namespace {
fileName = "??";
}
auto demangledName = tryDemangle(symbolName);
auto demangledName = demangle(symbolName);
stackTrace.push_back(StackFrame { fileName, demangledName, lineNumber });
}
@@ -180,7 +182,7 @@ namespace {
dladdr(addresses[i], &info);
auto fileName = info.dli_fname != nullptr ? std::filesystem::path(info.dli_fname).filename().string() : "??";
auto demangledName = info.dli_sname != nullptr ? tryDemangle(info.dli_sname) : "??";
auto demangledName = info.dli_sname != nullptr ? demangle(info.dli_sname) : "??";
result.push_back(StackFrame { std::move(fileName), std::move(demangledName), 0 });
}
@@ -222,7 +224,7 @@ namespace {
if (function == nullptr)
function = "??";
result.push_back(StackFrame { std::filesystem::path(fileName).filename().string(), tryDemangle(function), std::uint32_t(lineNumber) });
result.push_back(StackFrame { std::filesystem::path(fileName).filename().string(), demangle(function), std::uint32_t(lineNumber) });
return 0;
}, nullptr, nullptr);

View File

@@ -58,7 +58,7 @@ set_target_properties(main PROPERTIES
target_compile_definitions(main PRIVATE IMHEX_PROJECT_NAME="${PROJECT_NAME}")
target_link_libraries(main PRIVATE libromfs-imhex libimhex libwolv ${LIBBACKTRACE_LIBRARIES} LLVMDemangle)
target_link_libraries(main PRIVATE libromfs-imhex libimhex libwolv ${LIBBACKTRACE_LIBRARIES})
if (WIN32)
target_link_libraries(main PRIVATE usp10 wsock32 ws2_32 Dwmapi.lib Winmm.lib)

View File

@@ -13,11 +13,9 @@
#include <init/tasks.hpp>
#include <hex/trace/stacktrace.hpp>
#include <llvm/Demangle/Demangle.h>
#include <nlohmann/json.hpp>
#include <hex/trace/stacktrace.hpp>
#include <llvm/Demangle/Demangle.h>
#include <csignal>
#include <exception>
@@ -164,7 +162,7 @@ namespace hex::crash {
try {
std::rethrow_exception(std::current_exception());
} catch (std::exception &ex) {
std::string exceptionStr = hex::format("{}()::what() -> {}", llvm::demangle(std::string("_Z") + typeid(ex).name()), ex.what());
std::string exceptionStr = hex::format("{}()::what() -> {}", trace::demangle(typeid(ex).name()), ex.what());
handleCrash(exceptionStr);
log::fatal("Program terminated with uncaught exception: {}", exceptionStr);

View File

@@ -49,8 +49,6 @@ add_imhex_plugin(
source/content/window_decoration.cpp
source/content/data_information_sections.cpp
source/content/helpers/demangle.cpp
source/content/data_processor_nodes/basic_nodes.cpp
source/content/data_processor_nodes/control_nodes.cpp
source/content/data_processor_nodes/decode_nodes.cpp
@@ -130,7 +128,6 @@ add_imhex_plugin(
${JTHREAD_LIBRARIES}
plcli
libpl-gen
LLVMDemangle
)
if (WIN32)

View File

@@ -1,9 +0,0 @@
#pragma once
#include <string>
namespace hex::plugin::builtin {
std::string demangle(const std::string &mangled);
}

View File

@@ -3,8 +3,7 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization_manager.hpp>
#include <llvm/Demangle/Demangle.h>
#include <hex/trace/stacktrace.hpp>
#include <string>
@@ -20,7 +19,7 @@ namespace hex::plugin::builtin {
void drawContent() override {
ImGuiExt::TextFormattedWrapped("hex.builtin.popup.crash_recover.message"_lang);
ImGuiExt::TextFormattedWrapped(hex::format("Error: {}: {}", llvm::demangle(this->m_errorType), this->m_errorMessage));
ImGuiExt::TextFormattedWrapped(hex::format("Error: {}: {}", trace::demangle(this->m_errorType), this->m_errorMessage));
if (ImGui::Button("hex.ui.common.okay"_lang)) {
this->close();

View File

@@ -1,6 +1,5 @@
#include <content/command_line_interface.hpp>
#include <content/providers/file_provider.hpp>
#include <content/helpers/demangle.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
@@ -18,13 +17,13 @@
#include <hex/helpers/debugging.hpp>
#include <hex/subcommands/subcommands.hpp>
#include <hex/trace/stacktrace.hpp>
#include <romfs/romfs.hpp>
#include <wolv/utils/string.hpp>
#include <wolv/math_eval/math_evaluator.hpp>
#include <pl/cli/cli.hpp>
#include <llvm/Demangle/Demangle.h>
namespace hex::plugin::builtin {
using namespace hex::literals;
@@ -383,7 +382,7 @@ namespace hex::plugin::builtin {
std::exit(EXIT_FAILURE);
}
log::println("{}", hex::plugin::builtin::demangle(args[0]));
log::println("{}", trace::demangle(args[0]));
std::exit(EXIT_SUCCESS);
}

View File

@@ -1,17 +0,0 @@
#include <content/helpers/demangle.hpp>
#include <llvm/Demangle/Demangle.h>
namespace hex::plugin::builtin {
std::string demangle(const std::string &mangled) {
std::string result = llvm::demangle(mangled);
if (result.empty() || result == mangled)
result = llvm::demangle("_" + mangled);
if (result.empty() || result == ("_" + mangled))
result = mangled;
return result;
}
}

View File

@@ -3,11 +3,11 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/trace/stacktrace.hpp>
#include <pl/core/token.hpp>
#include <pl/core/evaluator.hpp>
#include <content/helpers/demangle.hpp>
#include <pl/patterns/pattern.hpp>
namespace hex::plugin::builtin {
@@ -75,7 +75,7 @@ namespace hex::plugin::builtin {
ContentRegistry::PatternLanguage::addFunction(nsHexDec, "demangle", FunctionParameterCount::exactly(1), [](Evaluator *, auto params) -> std::optional<Token::Literal> {
const auto mangledString = params[0].toString(false);
return hex::plugin::builtin::demangle(mangledString);
return trace::demangle(mangledString);
});
}

View File

@@ -1,10 +1,9 @@
#include <hex/helpers/utils.hpp>
#include <hex/api/localization_manager.hpp>
#include <content/helpers/demangle.hpp>
#include <hex/trace/stacktrace.hpp>
#include <hex/ui/imgui_imhex_extensions.h>
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <TextEditor.h>
@@ -31,7 +30,7 @@ namespace hex::plugin::builtin {
static float prevWindowWidth;
if (ImGui::InputTextWithHint("hex.builtin.tools.demangler.mangled"_lang, "Itanium, MSVC, Dlang & Rust", mangledName)) {
demangledName = hex::plugin::builtin::demangle(mangledName);
demangledName = trace::demangle(mangledName);
if (demangledName == mangledName) {
demangledName = "???";

View File

@@ -3,6 +3,7 @@
#include <hex/api/imhex_api.hpp>
#include <hex/api/achievement_manager.hpp>
#include <hex/api/events/events_interaction.hpp>
#include <hex/trace/stacktrace.hpp>
#include <hex/providers/buffered_reader.hpp>
@@ -13,7 +14,6 @@
#include <string>
#include <utility>
#include <content/helpers/demangle.hpp>
#include <boost/regex.hpp>
namespace hex::plugin::builtin {
@@ -71,7 +71,7 @@ namespace hex::plugin::builtin {
ImGui::TableNextColumn();
ImGuiExt::TextFormatted("[ 0x{:08X} - 0x{:08X} ]", region.getStartAddress(), region.getEndAddress());
auto demangledValue = hex::plugin::builtin::demangle(value);
auto demangledValue = trace::demangle(value);
if (value != demangledValue) {
ImGui::TableNextRow();
@@ -663,7 +663,7 @@ namespace hex::plugin::builtin {
if (ImGui::MenuItemEx("hex.builtin.view.find.context.copy"_lang, ICON_VS_COPY))
ImGui::SetClipboardText(value.c_str());
if (ImGui::MenuItemEx("hex.builtin.view.find.context.copy_demangle"_lang, ICON_VS_FILES))
ImGui::SetClipboardText(hex::plugin::builtin::demangle(value).c_str());
ImGui::SetClipboardText(trace::demangle(value).c_str());
if (ImGui::BeginMenuEx("hex.builtin.view.find.context.replace"_lang, ICON_VS_REPLACE)) {
if (ImGui::BeginTabBar("##replace_tabs")) {
if (ImGui::BeginTabItem("hex.builtin.view.find.context.replace.hex"_lang)) {