Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b9f4009f053812d554636bf05125628a99f37689
[simgrid.git] / src / xbt / backtrace.cpp
1 /* Copyright (c) 2005-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/internal_config.h"
7
8 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
9 #include <xbt/backtrace.hpp>
10 #include <xbt/log.h>
11 #include <xbt/string.hpp>
12 #include <xbt/sysdep.h>
13
14 #include <cstddef>
15 #include <cstdlib>
16 #include <cstring>
17 #include <fstream>
18 #include <sstream>
19 #include <sys/stat.h>
20 #include <vector>
21
22 #include <boost/algorithm/string.hpp>
23
24 // Try to detect and use the C++ itanium ABI for name demangling:
25 #ifdef __GXX_ABI_VERSION
26 #include <cxxabi.h>
27 #endif
28
29 #if HAVE_BOOST_STACKTRACE
30 #define BOOST_STACKTRACE_USE_BACKTRACE
31 #include <boost/stacktrace.hpp>
32 #endif
33
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
35
36 /** @brief show the backtrace of the current point (lovely while debugging) */
37 void xbt_backtrace_display_current()
38 {
39   simgrid::xbt::Backtrace().display();
40 }
41
42 namespace simgrid {
43 namespace xbt {
44
45 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
46 {
47 #ifdef __GXX_ABI_VERSION
48   int status;
49   auto res = std::unique_ptr<char, void (*)(void*)>(abi::__cxa_demangle(name, nullptr, nullptr, &status), &std::free);
50   if (res != nullptr)
51     return res;
52   // We did not manage to resolve this. Probably because this is not a mangled symbol:
53 #endif
54   // Return the symbol:
55   return std::unique_ptr<char, void (*)(void*)>(xbt_strdup(name), &std::free);
56 }
57
58 class BacktraceImpl {
59   short refcount_ = 1;
60
61 public:
62   void ref() { refcount_++; }
63   bool unref()
64   {
65     refcount_--;
66     return refcount_ == 0;
67   }
68 #if HAVE_BOOST_STACKTRACE
69   boost::stacktrace::stacktrace st;
70 #endif
71 };
72
73 Backtrace::Backtrace()
74 {
75 #if HAVE_BOOST_STACKTRACE
76   impl_     = new BacktraceImpl();
77   impl_->st = boost::stacktrace::stacktrace();
78 #endif
79 }
80 Backtrace::Backtrace(const Backtrace& bt)
81 {
82   impl_ = bt.impl_;
83   if (impl_)
84     impl_->ref();
85 }
86
87 Backtrace::Backtrace(Backtrace&& bt)
88 {
89   impl_    = bt.impl_;
90   bt.impl_ = nullptr;
91 }
92
93 Backtrace& Backtrace::operator=(const Backtrace& rhs)
94 {
95   impl_ = rhs.impl_;
96   if (impl_)
97     impl_->ref();
98   return *this;
99 }
100
101 Backtrace& Backtrace::operator=(Backtrace&& rhs)
102 {
103   impl_     = rhs.impl_;
104   rhs.impl_ = nullptr;
105   return *this;
106 }
107
108 Backtrace::~Backtrace()
109 {
110   if (impl_ != nullptr && impl_->unref()) {
111     delete impl_;
112   }
113 }
114
115 std::string const Backtrace::resolve() const
116 {
117   std::string result("");
118
119 #if HAVE_BOOST_STACKTRACE
120   std::stringstream ss;
121   ss << impl_->st;
122   result.append(ss.str());
123 #endif
124   return result;
125 }
126
127 void Backtrace::display() const
128 {
129   std::string backtrace = resolve();
130   if (backtrace.empty()) {
131     fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
132     return;
133   }
134   fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
135   std::fprintf(stderr, "%s\n", backtrace.c_str());
136 }
137
138 } // namespace xbt
139 } // namespace simgrid