- const std::size_t size = 10;
- xbt_backtrace_location_t bt[size];
- size_t used = xbt_backtrace_current(bt, size);
- xbt_backtrace_display(bt, used);
-}
-
-namespace simgrid {
-namespace xbt {
-
-std::unique_ptr<char, void(*)(void*)> demangle(const char* name)
-{
-#ifdef __GXX_ABI_VERSION
- int status;
- auto res = std::unique_ptr<char, void(*)(void*)>(
- abi::__cxa_demangle(name, nullptr, nullptr, &status),
- std::free
- );
- if (res != nullptr)
- return res;
- // We did not manage to resolve this. Probably because this is not a mangled symbol:
-#endif
- // Return the symbol:
- return std::unique_ptr<char, void(*)(void*)>(xbt_strdup(name), std::free);
-}
-
-Backtrace backtrace()
-{
- const std::size_t size = 10;
- xbt_backtrace_location_t loc[size];
- size_t used = xbt_backtrace_current(loc, size);
- return Backtrace(loc, loc + used);
-}
-
-}
-}
-
-size_t xbt_backtrace_current(xbt_backtrace_location_t* loc, std::size_t count)
-{
- std::size_t used = 0;
-#if HAVE_BACKTRACE
- used = backtrace(loc, count);
- if (used == 0) {
- std::fprintf(stderr, "The backtrace() function failed, which probably means that the memory is exhausted\n.");
- std::fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory\n.");
- std::fprintf(stderr, "Please go fix the memleaks\n");
- std::exit(1);
- }
-#endif
- return used;
-}
-
-namespace simgrid {
-namespace xbt {
-
-/** Find the path of the binary file from the name found in argv */
-static std::string get_binary_path()
-{
- struct stat stat_buf;
-
- if (xbt_binary_name == nullptr)
- return "";
-
- // We found it, we are happy:
- if (stat(xbt_binary_name, &stat_buf) == 0)
- return xbt_binary_name;
-
- // Not found, look in the PATH:
- char* path = getenv("PATH");
- if (path == nullptr)
- return "";
-
- XBT_DEBUG("Looking in the PATH: %s\n", path);
- std::vector<std::string> path_list;
- boost::split(path_list, path, boost::is_any_of(":;"));
-
- for (std::string const& path_item : path_list) {
- std::string binary_name = simgrid::xbt::string_printf("%s/%s", path_item.c_str(), xbt_binary_name);
- bool found = (stat(binary_name.c_str(), &stat_buf) == 0);
- XBT_DEBUG("Looked in the PATH for the binary. %s %s", found ? "Found" : "Not found", binary_name.c_str());
- if (found)
- return binary_name;
- }
-
- // Not found at all:
- return "";