Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
db93b541f1f2c98fb7b4d1fd155082e96ce62b97
[simgrid.git] / src / xbt / backtrace.cpp
1 /* Copyright (c) 2005-2018. 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 void xbt_backtrace_display(const simgrid::xbt::Backtrace& bt)
37 {
38   std::string backtrace = simgrid::xbt::resolve_backtrace(bt);
39   if (backtrace.empty()) {
40     fprintf(stderr, "(backtrace not set -- did you install Boost.Stacktrace?)\n");
41     return;
42   }
43   fprintf(stderr, "Backtrace (displayed in actor %s):\n", SIMIX_process_self_get_name());
44   std::fprintf(stderr, "%s\n", backtrace.c_str());
45 }
46
47 /** @brief show the backtrace of the current point (lovely while debugging) */
48 void xbt_backtrace_display_current()
49 {
50   simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
51   xbt_backtrace_display(bt);
52 }
53
54 namespace simgrid {
55 namespace xbt {
56
57 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
58 {
59 #ifdef __GXX_ABI_VERSION
60   int status;
61   auto res = std::unique_ptr<char, void(*)(void*)>(
62     abi::__cxa_demangle(name, nullptr, nullptr, &status),
63     std::free
64   );
65   if (res != nullptr)
66     return res;
67   // We did not manage to resolve this. Probably because this is not a mangled symbol:
68 #endif
69   // Return the symbol:
70   return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
71 }
72
73 class BacktraceImpl {
74   short refcount_ = 1;
75
76 public:
77   void ref() { refcount_++; }
78   bool unref()
79   {
80     refcount_--;
81     return refcount_ == 0;
82   }
83 #if HAVE_BOOST_STACKTRACE
84   boost::stacktrace::stacktrace st;
85 #endif
86 };
87
88 Backtrace::Backtrace()
89 {
90 #if HAVE_BOOST_STACKTRACE
91   impl_     = new BacktraceImpl();
92   impl_->st = boost::stacktrace::stacktrace();
93 #endif
94 }
95 Backtrace::Backtrace(const Backtrace& bt)
96 {
97   impl_ = bt.impl_;
98   if (impl_)
99     impl_->ref();
100 }
101
102 Backtrace::~Backtrace()
103 {
104   if (impl_ != nullptr && impl_->unref()) {
105     delete impl_;
106   }
107 }
108 } // namespace xbt
109 } // namespace simgrid
110
111 namespace simgrid {
112 namespace xbt {
113
114 std::string resolve_backtrace(const Backtrace& bt)
115 {
116   std::string result("");
117
118 #if HAVE_BOOST_STACKTRACE
119   std::stringstream ss;
120   ss << bt.impl_->st;
121   result.append(ss.str());
122 #endif
123   return result;
124 }
125
126 } // namespace xbt
127 } // namespace simgrid