Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't use pass-by-value for large parameters.
[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, 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
75   boost::stacktrace::stacktrace st;
76 #endif
77 };
78
79 Backtrace::Backtrace()
80 {
81 #if HAVE_BOOST_STACKTRACE
82   impl_     = new BacktraceImpl();
83   impl_->st = boost::stacktrace::stacktrace();
84 #endif
85 }
86 Backtrace::Backtrace(const Backtrace& bt)
87 {
88   impl_ = bt.impl_;
89   if (impl_)
90     impl_->ref();
91 }
92
93 Backtrace::Backtrace(Backtrace&& bt)
94 {
95   impl_    = bt.impl_;
96   bt.impl_ = nullptr;
97 }
98
99 Backtrace& Backtrace::operator=(const Backtrace& rhs)
100 {
101   if (this != &rhs) {
102     if (impl_)
103       impl_->unref();
104     impl_ = rhs.impl_;
105     if (impl_)
106       impl_->ref();
107   }
108   return *this;
109 }
110
111 Backtrace& Backtrace::operator=(Backtrace&& rhs)
112 {
113   if (this != &rhs) {
114     if (impl_)
115       impl_->unref();
116     impl_     = rhs.impl_;
117     rhs.impl_ = nullptr;
118   }
119   return *this;
120 }
121
122 Backtrace::~Backtrace()
123 {
124   if (impl_)
125     impl_->unref();
126 }
127
128 std::string const Backtrace::resolve() const
129 {
130   std::string result("");
131
132 #if HAVE_BOOST_STACKTRACE
133   std::stringstream ss;
134   ss << impl_->st;
135   result.append(ss.str());
136 #endif
137   return result;
138 }
139
140 void Backtrace::display() const
141 {
142   std::string backtrace = resolve();
143   if (backtrace.empty()) {
144     fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
145     return;
146   }
147   fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
148   std::fprintf(stderr, "%s\n", backtrace.c_str());
149 }
150
151 } // namespace xbt
152 } // namespace simgrid