Logo AND Algorithmique Numérique Distribuée

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