1 /* Copyright (c) 2005-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/internal_config.h"
8 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
9 #include <xbt/backtrace.hpp>
11 #include <xbt/string.hpp>
12 #include <xbt/sysdep.h>
22 #include <boost/algorithm/string.hpp>
24 // Try to detect and use the C++ itanium ABI for name demangling:
25 #ifdef __GXX_ABI_VERSION
29 #if HAVE_BOOST_STACKTRACE
30 #define BOOST_STACKTRACE_USE_BACKTRACE
31 #include <boost/stacktrace.hpp>
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
36 /** @brief show the backtrace of the current point (lovely while debugging) */
37 void xbt_backtrace_display_current()
39 simgrid::xbt::Backtrace().display();
45 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
47 #ifdef __GXX_ABI_VERSION
49 auto res = std::unique_ptr<char, void (*)(void*)>(abi::__cxa_demangle(name, nullptr, nullptr, &status), &std::free);
52 // We did not manage to resolve this. Probably because this is not a mangled symbol:
55 return std::unique_ptr<char, void (*)(void*)>(xbt_strdup(name), &std::free);
62 void ref() { refcount_++; }
73 #if HAVE_BOOST_STACKTRACE
74 boost::stacktrace::stacktrace st;
78 Backtrace::Backtrace()
80 #if HAVE_BOOST_STACKTRACE
81 impl_ = new BacktraceImpl();
82 impl_->st = boost::stacktrace::stacktrace();
85 Backtrace::Backtrace(const Backtrace& bt)
92 Backtrace::Backtrace(Backtrace&& bt)
98 Backtrace& Backtrace::operator=(const Backtrace& rhs)
110 Backtrace& Backtrace::operator=(Backtrace&& rhs)
121 Backtrace::~Backtrace()
127 std::string const Backtrace::resolve() const
129 std::string result("");
131 #if HAVE_BOOST_STACKTRACE
132 std::stringstream ss;
134 result.append(ss.str());
139 void Backtrace::display() const
141 std::string backtrace = resolve();
142 if (backtrace.empty()) {
143 fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
146 fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
147 std::fprintf(stderr, "%s\n", backtrace.c_str());
151 } // namespace simgrid