Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove parameter from XBT_EXPORT_NO_IMPORT/XBT_IMPORT_NO_EXPORT.
[simgrid.git] / src / xbt / backtrace.cpp
1 /* Copyright (c) 2005-2017. 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 <cstddef>
7 #include <cstdlib>
8 #include <cstring>
9
10 #include <vector>
11
12 // Try to detect and use the C++ intanium ABI for name demangling:
13 #ifdef __GXX_ABI_VERSION
14 #include <cxxabi.h>
15 #endif
16
17 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
18 #include <xbt/backtrace.h>
19 #include <xbt/backtrace.hpp>
20 #include <xbt/log.h>
21 #include <xbt/sysdep.h>
22
23 #include "src/internal_config.h"
24
25 extern "C" {
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
28
29 }
30
31 static bool startWith(std::string str, const char* prefix)
32 {
33   return strncmp(str.c_str(), prefix, strlen(prefix)) == 0;
34 }
35
36 void xbt_backtrace_display(xbt_backtrace_location_t* loc, std::size_t count)
37 {
38 #ifdef HAVE_BACKTRACE
39   std::vector<std::string> backtrace = simgrid::xbt::resolveBacktrace(loc, count);
40   if (backtrace.empty()) {
41     fprintf(stderr, "(backtrace not set)\n");
42     return;
43   }
44   fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name());
45   for (std::string const& s : backtrace) {
46     if (startWith(s, "xbt_backtrace_display_current"))
47       continue;
48
49     std::fprintf(stderr, "---> '%s'\n", s.c_str());
50     if (startWith(s, "SIMIX_simcall_handle") ||
51         startWith(s, "simgrid::xbt::MainFunction") /* main used with thread factory */)
52       break;
53   }
54 #else
55   XBT_ERROR("Cannot display backtrace when compiled without libunwind.");
56 #endif
57 }
58
59 /** @brief show the backtrace of the current point (lovely while debugging) */
60 void xbt_backtrace_display_current()
61 {
62   const std::size_t size = 10;
63   xbt_backtrace_location_t bt[size];
64   size_t used = xbt_backtrace_current(bt, size);
65   xbt_backtrace_display(bt, used);
66 }
67
68 namespace simgrid {
69 namespace xbt {
70
71 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
72 {
73 #ifdef __GXX_ABI_VERSION
74   int status;
75   auto res = std::unique_ptr<char, void(*)(void*)>(
76     abi::__cxa_demangle(name, nullptr, nullptr, &status),
77     std::free
78   );
79   if (res != nullptr)
80     return res;
81   // We did not manage to resolve this. Probably because this is not a mangled symbol:
82 #endif
83   // Return the symbol:
84   return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
85 }
86
87 std::vector<xbt_backtrace_location_t> backtrace()
88 {
89   const std::size_t size = 10;
90   xbt_backtrace_location_t loc[size];
91   size_t used = xbt_backtrace_current(loc, size);
92   return std::vector<xbt_backtrace_location_t>(loc, loc + used);
93 }
94
95 }
96 }
97
98 #if HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
99 # include "src/xbt/backtrace_linux.cpp"
100 #else
101 # include "src/xbt/backtrace_dummy.cpp"
102 #endif