1 /* Copyright (c) 2005-2018. The SimGrid Team. All rights reserved. */
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. */
12 // Try to detect and use the C++ intanium ABI for name demangling:
13 #ifdef __GXX_ABI_VERSION
17 #include "simgrid/simix.h" /* SIMIX_process_self_get_name() */
18 #include <xbt/backtrace.h>
19 #include <xbt/backtrace.hpp>
21 #include <xbt/sysdep.h>
23 #include "src/internal_config.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_backtrace, xbt, "Backtrace");
27 static bool startWith(std::string str, const char* prefix)
29 return strncmp(str.c_str(), prefix, strlen(prefix)) == 0;
32 void xbt_backtrace_display(xbt_backtrace_location_t* loc, std::size_t count)
35 std::vector<std::string> backtrace = simgrid::xbt::resolve_backtrace(loc, count);
36 if (backtrace.empty()) {
37 fprintf(stderr, "(backtrace not set)\n");
40 fprintf(stderr, "Backtrace (displayed in process %s):\n", SIMIX_process_self_get_name());
41 for (std::string const& s : backtrace) {
42 if (startWith(s, "xbt_backtrace_display_current"))
45 std::fprintf(stderr, "---> '%s'\n", s.c_str());
46 if (startWith(s, "SIMIX_simcall_handle") ||
47 startWith(s, "simgrid::xbt::MainFunction") /* main used with thread factory */)
51 XBT_ERROR("Cannot display backtrace when compiled without libunwind.");
55 /** @brief show the backtrace of the current point (lovely while debugging) */
56 void xbt_backtrace_display_current()
58 const std::size_t size = 10;
59 xbt_backtrace_location_t bt[size];
60 size_t used = xbt_backtrace_current(bt, size);
61 xbt_backtrace_display(bt, used);
67 std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
69 #ifdef __GXX_ABI_VERSION
71 auto res = std::unique_ptr<char, void(*)(void*)>(
72 abi::__cxa_demangle(name, nullptr, nullptr, &status),
77 // We did not manage to resolve this. Probably because this is not a mangled symbol:
80 return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
83 std::vector<xbt_backtrace_location_t> backtrace()
85 const std::size_t size = 10;
86 xbt_backtrace_location_t loc[size];
87 size_t used = xbt_backtrace_current(loc, size);
88 return std::vector<xbt_backtrace_location_t>(loc, loc + used);
94 #if HAVE_BACKTRACE && HAVE_EXECINFO_H && HAVE_POPEN && defined(ADDR2LINE)
95 # include "src/xbt/backtrace_linux.cpp"
97 # include "src/xbt/backtrace_dummy.cpp"