diff --git a/src/utils/BacktraceManager.cpp b/src/utils/BacktraceManager.cpp index 80d9473444436e6c20ef9e47fbc81b733806047e..32bd1e9124aebba6ecf72befc2ffd0cb1e6b99a3 100644 --- a/src/utils/BacktraceManager.cpp +++ b/src/utils/BacktraceManager.cpp @@ -4,10 +4,11 @@ #include <rang.hpp> #include <cmath> -#include <cxxabi.h> #include <execinfo.h> #include <regex> +#include <Demangle.hpp> + BacktraceManager::BacktraceManager() { const int size = 100; @@ -35,7 +36,6 @@ operator<<(std::ostream& os, const BacktraceManager& btm) const auto& line = lines[i_line]; os << rang::fg::green << "[" << std::setw(width) << i_line + 1 << '/' << lines.size() << "] " << rang::fg::reset; std::smatch matchex; - int status = -1; if (std::regex_search(line, matchex, mangled_function)) { std::string prefix = matchex.prefix().str(); std::string function = line.substr(matchex.position() + 1, matchex.length() - 2); @@ -43,13 +43,7 @@ operator<<(std::ostream& os, const BacktraceManager& btm) os << prefix << '('; if (function.size() > 0) { - char* demangled = abi::__cxa_demangle(function.c_str(), NULL, NULL, &status); - if (status == 0) { - os << rang::style::bold << demangled << rang::style::reset; - free(demangled); - } else { - os << rang::style::bold << function << rang::style::reset; - } + os << rang::style::bold << demangle(function) << rang::style::reset; } os << '+' << suffix << '\n'; } else { diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 59def1c2dd746c70799b67953ec260abc051d87a..81ceee33231974b031d31ae3e3010d0e72244a56 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -8,6 +8,7 @@ add_library( BuildInfo.cpp BacktraceManager.cpp ConsoleManager.cpp + Demangle.cpp FPEManager.cpp Messenger.cpp Partitioner.cpp diff --git a/src/utils/Demangle.cpp b/src/utils/Demangle.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be8553a10b24521dcfab9299d18ee6c301d34e8b --- /dev/null +++ b/src/utils/Demangle.cpp @@ -0,0 +1,18 @@ +#include <Demangle.hpp> +#include <cxxabi.h> +#include <memory> + +std::string +demangle(const std::string_view mangled) +{ + int status = -1; + + char* cxa_demangled = abi::__cxa_demangle(mangled.data(), NULL, NULL, &status); + if (status == 0) { + std::string demangled{cxa_demangled}; + free(cxa_demangled); + return demangled; + } else { + return std::string{mangled}; + } +} diff --git a/src/utils/Demangle.hpp b/src/utils/Demangle.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f9b7d25b4eb9405ee0278618fd25767212c0a159 --- /dev/null +++ b/src/utils/Demangle.hpp @@ -0,0 +1,16 @@ +#ifndef DEMANGLE_HPP +#define DEMANGLE_HPP + +#include <string> +#include <typeinfo> + +std::string demangle(const std::string_view mangled); + +template <typename T> +inline std::string +demangle() +{ + return demangle(typeid(T).name()); +} + +#endif // DEMANGLE_HPP