Logo AND Algorithmique Numérique Distribuée

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