Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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*)>(
50     abi::__cxa_demangle(name, nullptr, nullptr, &status),
51     std::free
52   );
53   if (res != nullptr)
54     return res;
55   // We did not manage to resolve this. Probably because this is not a mangled symbol:
56 #endif
57   // Return the symbol:
58   return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
59 }
60
61 class BacktraceImpl {
62   short refcount_ = 1;
63
64 public:
65   void ref() { refcount_++; }
66   bool unref()
67   {
68     refcount_--;
69     return refcount_ == 0;
70   }
71 #if HAVE_BOOST_STACKTRACE
72   boost::stacktrace::stacktrace st;
73 #endif
74 };
75
76 Backtrace::Backtrace()
77 {
78 #if HAVE_BOOST_STACKTRACE
79   impl_     = new BacktraceImpl();
80   impl_->st = boost::stacktrace::stacktrace();
81 #endif
82 }
83 Backtrace::Backtrace(const Backtrace& bt)
84 {
85   impl_ = bt.impl_;
86   if (impl_)
87     impl_->ref();
88 }
89
90 Backtrace::~Backtrace()
91 {
92   if (impl_ != nullptr && impl_->unref()) {
93     delete impl_;
94   }
95 }
96
97 std::string const Backtrace::resolve() const
98 {
99   std::string result("");
100
101 #if HAVE_BOOST_STACKTRACE
102   std::stringstream ss;
103   ss << impl_->st;
104   result.append(ss.str());
105 #endif
106   return result;
107 }
108
109 void Backtrace::display() const
110 {
111   std::string backtrace = resolve();
112   if (backtrace.empty()) {
113     fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
114     return;
115   }
116   fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
117   std::fprintf(stderr, "%s\n", backtrace.c_str());
118 }
119
120 } // namespace xbt
121 } // namespace simgrid