Logo AND Algorithmique Numérique Distribuée

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