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_BACKTRACE
30 #define BOOST_STACKTRACE_USE_BACKTRACE
31 #include <boost/stacktrace.hpp>
32 #elif HAVE_BOOST_STACKTRACE_ADDR2LINE
33 #define BOOST_STACKTRACE_USE_ADDR2LINE
34 #include <boost/stacktrace.hpp>
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
39 /** @brief show the backtrace of the current point (lovely while debugging) */
40 void xbt_backtrace_display_current()
42 simgrid::xbt::Backtrace().display();
48 std::unique_ptr<char, std::function<void(char*)>> demangle(const char* name)
50 #ifdef __GXX_ABI_VERSION
52 std::unique_ptr<char, std::function<void(char*)>> res(abi::__cxa_demangle(name, nullptr, nullptr, &status),
56 // We did not manage to resolve this. Probably because this is not a mangled symbol:
59 return std::unique_ptr<char, std::function<void(char*)>>(xbt_strdup(name), &xbt_free_f);
66 void ref() { refcount_++; }
77 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
78 boost::stacktrace::stacktrace st;
82 Backtrace::Backtrace()
84 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
85 impl_ = new BacktraceImpl();
86 impl_->st = boost::stacktrace::stacktrace();
89 Backtrace::Backtrace(const Backtrace& bt)
96 Backtrace::Backtrace(Backtrace&& bt)
102 Backtrace& Backtrace::operator=(const Backtrace& rhs)
114 Backtrace& Backtrace::operator=(Backtrace&& rhs)
125 Backtrace::~Backtrace()
131 std::string const Backtrace::resolve() const
133 std::string result("");
135 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
136 std::stringstream ss;
138 result.append(ss.str());
143 void Backtrace::display() const
145 std::string backtrace = resolve();
146 if (backtrace.empty()) {
147 fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
150 fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
151 std::fprintf(stderr, "%s\n", backtrace.c_str());
155 } // namespace simgrid