Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove explicit conversion to std::string when it's not required.
[simgrid.git] / src / xbt / backtrace.cpp
1 /* Copyright (c) 2005-2022. 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 <xbt/backtrace.hpp>
9 #include <xbt/string.hpp>
10 #include <xbt/sysdep.h>
11 #include <xbt/virtu.h>
12
13 #include <cstdio>
14 #include <cstdlib>
15 #include <sstream>
16
17 #if HAVE_BOOST_STACKTRACE_BACKTRACE
18 #define BOOST_STACKTRACE_USE_BACKTRACE
19 #include <boost/stacktrace.hpp>
20 #include <boost/stacktrace/detail/frame_decl.hpp>
21 #elif HAVE_BOOST_STACKTRACE_ADDR2LINE
22 #define BOOST_STACKTRACE_USE_ADDR2LINE
23 #include <boost/stacktrace.hpp>
24 #include <boost/stacktrace/detail/frame_decl.hpp>
25 #endif
26
27 /** @brief show the backtrace of the current point (lovely while debugging) */
28 void xbt_backtrace_display_current()
29 {
30   simgrid::xbt::Backtrace().display();
31 }
32
33 namespace simgrid::xbt {
34
35 class BacktraceImpl {
36 #if HAVE_BOOST_STACKTRACE_BACKTRACE || HAVE_BOOST_STACKTRACE_ADDR2LINE
37   const boost::stacktrace::stacktrace st;
38
39 public:
40   std::string resolve() const
41   {
42     std::stringstream ss;
43
44     int frame_count = 0;
45     bool print      = false;
46
47     for (boost::stacktrace::frame const& frame : st) {
48       const std::string frame_name = frame.name();
49       if (print) {
50         if (frame_name.rfind("simgrid::xbt::MainFunction", 0) == 0 ||
51             frame_name.rfind("simgrid::kernel::context::Context::operator()()", 0) == 0)
52           break;
53         ss << "  ->  #" << frame_count++ << " ";
54         if (xbt_log_no_loc) // Don't display file source and line if so
55           ss << (frame_name.empty() ? "(debug info not found and log:no_loc activated)" : frame_name) << "\n";
56         else
57           ss << frame << "\n";
58         // If we are displaying the user side of a simcall, remove the crude details of context switching
59         if (frame_name.find("simgrid::kernel::actor::simcall_answered") != std::string::npos ||
60             frame_name.find("simgrid::kernel::actor::simcall_blocking") != std::string::npos ||
61             frame_name.find("simcall_run_answered") != std::string::npos ||
62             frame_name.find("simcall_run_blocking") != std::string::npos) {
63           frame_count = 0;
64           ss.str(""); // This is how you clear a stringstream in C++. clear() is something else :'(
65         }
66         if (frame_name == "main")
67           break;
68       } else {
69         if (frame_name == "simgrid::xbt::Backtrace::Backtrace()")
70           print = true;
71       }
72     }
73
74     return ss.str();
75   }
76 #else
77
78 public:
79   std::string resolve() const { return ""; } // fallback value
80 #endif
81 };
82
83 Backtrace::Backtrace() : impl_(std::make_shared<BacktraceImpl>()) {}
84
85 std::string Backtrace::resolve() const
86 {
87   return impl_->resolve();
88 }
89
90 void Backtrace::display() const
91 {
92   std::string backtrace = resolve();
93   std::fprintf(stderr, "Backtrace (displayed in actor %s%s):\n%s\n", xbt_procname(),
94                (xbt_log_no_loc ? " -- short trace because of --log=no_loc" : ""),
95                backtrace.empty() ? "(backtrace not set -- did you install Boost.Stacktrace?)" : backtrace.c_str());
96 }
97
98 } // namespace simgrid::xbt