From 60d3b5e31951ad81d048716adeb3223ff3aa4469 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Mon, 29 Jul 2019 17:50:16 +0200 Subject: [PATCH] Add a demangle facility Code was previously inlined into BacktraceManager. --- src/utils/BacktraceManager.cpp | 12 +++--------- src/utils/CMakeLists.txt | 1 + src/utils/Demangle.cpp | 18 ++++++++++++++++++ src/utils/Demangle.hpp | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/utils/Demangle.cpp create mode 100644 src/utils/Demangle.hpp diff --git a/src/utils/BacktraceManager.cpp b/src/utils/BacktraceManager.cpp index 80d947344..32bd1e912 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 59def1c2d..81ceee332 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 000000000..be8553a10 --- /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 000000000..f9b7d25b4 --- /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 -- GitLab