Logo AND Algorithmique Numérique Distribuée

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