Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clang-tidy: readability-qualified-auto.
[simgrid.git] / src / xbt / exception.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 "simgrid/Exception.hpp"
7 #include <xbt/config.hpp>
8 #include <xbt/log.hpp>
9
10 #include <boost/core/demangle.hpp>
11 #include <mutex>
12 #include <sstream>
13 #include <typeinfo>
14
15 XBT_LOG_EXTERNAL_CATEGORY(xbt);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
17
18 void _xbt_throw(char* message, const char* file, int line, const char* func)
19 {
20   simgrid::Exception e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), sg_actor_self_get_name(),
21                                                 sg_actor_self_get_pid()),
22                        message ? message : "");
23   xbt_free(message);
24   throw e;
25 }
26
27 namespace simgrid::xbt {
28
29 ImpossibleError::~ImpossibleError()         = default;
30 InitializationError::~InitializationError() = default;
31 UnimplementedError::~UnimplementedError()   = default;
32
33 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
34 {
35   try {
36     std::string name = boost::core::demangle(typeid(exception).name());
37
38     const auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
39     if (with_context != nullptr) {
40       XBT_LOG(prio, "%s %s by %s/%ld: %s", context, name.c_str(), with_context->throw_point().procname_.c_str(),
41               with_context->throw_point().pid_, exception.what());
42       // Do we have a backtrace?
43       if (not simgrid::config::get_value<bool>("exception/cutpath")) {
44         auto backtrace = with_context->resolve_backtrace();
45         XBT_LOG(prio, "Backtrace:\n%s", backtrace.c_str());
46       }
47     } else {
48       XBT_LOG(prio, "%s %s: %s", context, name.c_str(), exception.what());
49     }
50   } catch (...) {
51     // Don't log exceptions we got when trying to log exception
52     XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
53   }
54
55   try {
56     // Do we have a nested exception?
57     const auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
58     if (with_nested != nullptr && with_nested->nested_ptr() != nullptr)
59       with_nested->rethrow_nested();
60   } catch (const std::exception& nested_exception) {
61     log_exception(prio, "Caused by", nested_exception);
62   } catch (...) {
63     // We could catch nested_exception or WithContextException but we don't bother:
64     XBT_LOG(prio, "Caused by an unknown exception");
65   }
66 }
67
68 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
69 {
70   if (simgrid::config::get_value<bool>("exception/cutpath")) {
71     XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
72     return;
73   }
74   std::string res = bt.resolve();
75   XBT_CRITICAL("Current backtrace:\n%s", res.c_str());
76 }
77
78 static std::terminate_handler previous_terminate_handler = nullptr;
79
80 XBT_ATTRIB_NORETURN static void handler()
81 {
82   // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
83   if (static std::atomic_flag lock = ATOMIC_FLAG_INIT; lock.test_and_set()) {
84     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
85     std::abort();
86   }
87
88   // Get the current exception and show backtrace
89   try {
90     std::rethrow_exception(std::current_exception());
91   }
92
93   // Parse error are handled differently, as the call stack does not matter, only the file location
94   catch (const simgrid::ParseError& e) {
95     XBT_ERROR("%s", e.what());
96     XBT_ERROR("Exiting now.");
97     std::abort();
98   }
99
100   // We manage C++ exception ourselves
101   catch (const std::exception& e) {
102     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
103   }
104
105   catch (const simgrid::ForcefulKillException&) {
106     XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Please help fix this bug.");
107   }
108
109   // We don't know how to manage other exceptions
110   catch (...) {
111     // If there was another handler let's delegate to it
112     if (previous_terminate_handler) {
113       previous_terminate_handler();
114       XBT_ERROR("Unexpected return from delegated terminate handler");
115     }
116     XBT_ERROR("Unknown uncaught exception");
117   }
118   show_backtrace(simgrid::xbt::Backtrace());
119   std::abort();
120 }
121
122 void install_exception_handler()
123 {
124   static std::once_flag handler_flag;
125   std::call_once(handler_flag, [] {
126     previous_terminate_handler = std::set_terminate(handler);
127   });
128 }
129
130 } // namespace simgrid::xbt
131
132 void xbt_throw_impossible(const char* file, int line, const char* func)
133 {
134   std::stringstream ss;
135   ss << file << ":" << line << ":" << func
136      << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
137   throw simgrid::xbt::ImpossibleError(ss.str());
138 }
139 void xbt_throw_unimplemented(const char* file, int line, const char* func)
140 {
141   std::stringstream ss;
142   ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
143   throw simgrid::xbt::UnimplementedError(ss.str());
144 }