Logo AND Algorithmique Numérique Distribuée

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