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 <xbt/backtrace.hpp>
9 #include <xbt/string.hpp>
10 #include <xbt/sysdep.h>
11 #include <xbt/virtu.h>
21 #include <boost/algorithm/string.hpp>
23 // Try to detect and use the C++ itanium ABI for name demangling:
24 #ifdef __GXX_ABI_VERSION
28 #if HAVE_BOOST_STACKTRACE_BACKTRACE
29 #define BOOST_STACKTRACE_USE_BACKTRACE
30 #include <boost/stacktrace.hpp>
31 #elif HAVE_BOOST_STACKTRACE_ADDR2LINE
32 #define BOOST_STACKTRACE_USE_ADDR2LINE
33 #include <boost/stacktrace.hpp>
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, std::function<void(char*)>> demangle(const char* name)
47 #ifdef __GXX_ABI_VERSION
49 std::unique_ptr<char, std::function<void(char*)>> res(abi::__cxa_demangle(name, nullptr, nullptr, &status),
53 // We did not manage to resolve this. Probably because this is not a mangled symbol:
56 return std::unique_ptr<char, std::function<void(char*)>>(xbt_strdup(name), &xbt_free_f);
63 void ref() { refcount_++; }
74 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
75 boost::stacktrace::stacktrace st;
79 Backtrace::Backtrace()
81 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
82 impl_ = new BacktraceImpl();
83 impl_->st = boost::stacktrace::stacktrace();
86 Backtrace::Backtrace(const Backtrace& bt)
93 Backtrace::Backtrace(Backtrace&& bt)
99 Backtrace& Backtrace::operator=(const Backtrace& rhs)
111 Backtrace& Backtrace::operator=(Backtrace&& rhs)
122 Backtrace::~Backtrace()
128 std::string const Backtrace::resolve() const
130 std::string result("");
132 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
133 std::stringstream ss;
135 result.append(ss.str());
140 void Backtrace::display() const
142 std::string backtrace = resolve();
143 if (backtrace.empty()) {
144 fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
147 fprintf(stderr, "Backtrace (displayed in actor %s):\n", xbt_procname());
148 std::fprintf(stderr, "%s\n", backtrace.c_str());
152 } // namespace simgrid