Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2021. 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 <mutex>
12 #include <sstream>
13
14 XBT_LOG_EXTERNAL_CATEGORY(xbt);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
16
17 void _xbt_throw(char* message, int value, const char* file, int line, const char* func)
18 {
19   simgrid::Exception e(
20       simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
21       message ? message : "");
22   xbt_free(message);
23   e.value    = value;
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     auto name = simgrid::xbt::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.get(), 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, "  -> %s", backtrace.c_str());
47       }
48     } else {
49       XBT_LOG(prio, "%s %s: %s", context, name.get(), 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:");
77   XBT_CRITICAL("  -> %s", res.c_str());
78 }
79
80 static std::terminate_handler previous_terminate_handler = nullptr;
81
82 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 backtrace and exception
92   auto e = std::current_exception();
93   simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
94   try {
95     std::rethrow_exception(e);
96   }
97
98   // Parse error are handled differently, as the call stack does not matter, only the file location
99   catch (const simgrid::ParseError& e) {
100     XBT_ERROR("%s", e.what());
101     XBT_ERROR("Exiting now.");
102     std::abort();
103   }
104
105   // We manage C++ exception ourselves
106   catch (const std::exception& e) {
107     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
108     show_backtrace(bt);
109     std::abort();
110   }
111
112   catch (const simgrid::ForcefulKillException&) {
113     XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
114               "protected in a try/catch?");
115     show_backtrace(bt);
116   }
117
118   // We don't know how to manage other exceptions
119   catch (...) {
120     // If there was another handler let's delegate to it
121     if (previous_terminate_handler)
122       previous_terminate_handler();
123     else {
124       XBT_ERROR("Unknown uncaught exception");
125       show_backtrace(bt);
126       std::abort();
127     }
128   }
129   XBT_INFO("BAM");
130 }
131
132 void install_exception_handler()
133 {
134   static std::once_flag handler_flag;
135   std::call_once(handler_flag, [] {
136     previous_terminate_handler = std::set_terminate(handler);
137   });
138 }
139
140 } // namespace xbt
141 } // namespace simgrid
142
143 void xbt_throw_impossible(const char* file, int line, const char* func)
144 {
145   std::stringstream ss;
146   ss << file << ":" << line << ":" << func
147      << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
148   throw simgrid::xbt::ImpossibleError(ss.str());
149 }
150 void xbt_throw_unimplemented(const char* file, int line, const char* func)
151 {
152   std::stringstream ss;
153   ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
154   throw simgrid::xbt::UnimplementedError(ss.str());
155 }