Logo AND Algorithmique Numérique Distribuée

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