Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Initialize data members with class initializers, or initialization lists.
[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 <xbt/backtrace.hpp>
9 #include <xbt/string.hpp>
10 #include <xbt/sysdep.h>
11 #include <xbt/virtu.h>
12
13 #include <cstddef>
14 #include <cstdlib>
15 #include <cstring>
16 #include <fstream>
17 #include <sstream>
18 #include <sys/stat.h>
19 #include <vector>
20
21 #include <boost/algorithm/string.hpp>
22
23 // Try to detect and use the C++ itanium ABI for name demangling:
24 #ifdef __GXX_ABI_VERSION
25 #include <cxxabi.h>
26 #endif
27
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>
34 #endif
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, std::function<void(char*)>> demangle(const char* name)
46 {
47 #ifdef __GXX_ABI_VERSION
48   int status;
49   std::unique_ptr<char, std::function<void(char*)>> res(abi::__cxa_demangle(name, nullptr, nullptr, &status),
50                                                         &std::free);
51   if (res != nullptr)
52     return res;
53   // We did not manage to resolve this. Probably because this is not a mangled symbol:
54 #endif
55   // Return the symbol:
56   return std::unique_ptr<char, std::function<void(char*)>>(xbt_strdup(name), &xbt_free_f);
57 }
58
59 class BacktraceImpl {
60   short refcount_ = 1;
61
62 public:
63   void ref() { refcount_++; }
64   bool unref()
65   {
66     refcount_--;
67     if (refcount_ == 0) {
68       delete this;
69       return true;
70     } else {
71       return false;
72     }
73   }
74 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
75   boost::stacktrace::stacktrace st;
76 #endif
77 };
78
79 Backtrace::Backtrace()
80 {
81 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
82   impl_     = new BacktraceImpl();
83   impl_->st = boost::stacktrace::stacktrace();
84 #endif
85 }
86
87 Backtrace::Backtrace(const Backtrace& bt) : impl_(bt.impl_)
88 {
89   if (impl_)
90     impl_->ref();
91 }
92
93 Backtrace::Backtrace(Backtrace&& bt)
94 {
95   std::swap(impl_, bt.impl_);
96 }
97
98 Backtrace& Backtrace::operator=(const Backtrace& rhs)
99 {
100   if (this != &rhs) {
101     if (impl_)
102       impl_->unref();
103     impl_ = rhs.impl_;
104     if (impl_)
105       impl_->ref();
106   }
107   return *this;
108 }
109
110 Backtrace& Backtrace::operator=(Backtrace&& rhs)
111 {
112   if (this != &rhs) {
113     if (impl_)
114       impl_->unref();
115     impl_     = rhs.impl_;
116     rhs.impl_ = nullptr;
117   }
118   return *this;
119 }
120
121 Backtrace::~Backtrace()
122 {
123   if (impl_)
124     impl_->unref();
125 }
126
127 std::string const Backtrace::resolve() const
128 {
129   std::string result("");
130
131 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
132   std::stringstream ss;
133   ss << impl_->st;
134   result.append(ss.str());
135 #endif
136   return result;
137 }
138
139 void Backtrace::display() const
140 {
141   std::string backtrace = resolve();
142   if (backtrace.empty()) {
143     fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
144     return;
145   }
146   fprintf(stderr, "Backtrace (displayed in actor %s):\n", xbt_procname());
147   std::fprintf(stderr, "%s\n", backtrace.c_str());
148 }
149
150 } // namespace xbt
151 } // namespace simgrid