Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
efb2c6019c8f32ceddf9c865959697adb4ccd845
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2019. 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 <mutex>
11 #include <sstream>
12
13 XBT_LOG_EXTERNAL_CATEGORY(xbt);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
15
16 // DO NOT define ~xbt_ex() in exception.hpp.
17 // Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java.
18 // Doing otherwise naturally breaks things (at least on freebsd with clang).
19
20 xbt_ex::~xbt_ex() = default;
21
22 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
23 {
24   xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
25            message);
26   xbt_free(message);
27   e.category = errcat;
28   e.value    = value;
29   throw e;
30 }
31
32 /** @brief returns a short name for the given exception category */
33 const char* xbt_ex_catname(xbt_errcat_t cat)
34 {
35   switch (cat) {
36     case unknown_error:
37       return "unknown error";
38     case arg_error:
39       return "invalid argument";
40     case bound_error:
41       return "out of bounds";
42     case mismatch_error:
43       return "mismatch";
44     case not_found_error:
45       return "not found";
46     case system_error:
47       return "system error";
48     case network_error:
49       return "network error";
50     case timeout_error:
51       return "timeout";
52     case cancel_error:
53       return "action canceled";
54     case thread_error:
55       return "thread error";
56     case host_error:
57       return "host failed";
58     case tracing_error:
59       return "tracing error";
60     case io_error:
61       return "io error";
62     case vm_error:
63       return "vm error";
64     default:
65       return "INVALID ERROR";
66   }
67   return "INVALID ERROR";
68 }
69
70 namespace simgrid {
71 namespace xbt {
72
73 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
74 {
75   try {
76     auto name = simgrid::xbt::demangle(typeid(exception).name());
77
78     auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
79     if (with_context != nullptr)
80       XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
81               with_context->throw_point().pid_, exception.what());
82     else
83       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
84
85     // Do we have a backtrace?
86     if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
87       auto backtrace = with_context->resolve_backtrace();
88       XBT_LOG(prio, "  -> %s", backtrace.c_str());
89     }
90
91     // Do we have a nested exception?
92     auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
93     if (with_nested == nullptr ||  with_nested->nested_ptr() == nullptr)
94       return;
95     try {
96       with_nested->rethrow_nested();
97     }
98     catch (std::exception& nested_exception) {
99       log_exception(prio, "Caused by", nested_exception);
100     }
101     // We could catch nested_exception or WithContextException but we don't bother:
102     catch (...) {
103       XBT_LOG(prio, "Caused by an unknown exception");
104     }
105   }
106   catch (...) {
107     // Don't log exceptions we got when trying to log exception
108   }
109 }
110
111 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
112 {
113   if (simgrid::config::get_value<bool>("exception/cutpath")) {
114     XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
115     return;
116   }
117   std::string res = bt.resolve();
118   XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
119   XBT_LOG(xbt_log_priority_critical, "  -> %s", res.c_str());
120 }
121
122 static std::terminate_handler previous_terminate_handler = nullptr;
123
124 static void handler()
125 {
126   // Avoid doing crazy things if we get an uncaught exception inside
127   // an uncaught exception
128   static std::atomic_flag lock = ATOMIC_FLAG_INIT;
129   if (lock.test_and_set()) {
130     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
131     std::abort();
132   }
133
134   // Get the current backtrace and exception
135   auto e = std::current_exception();
136   simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
137   try {
138     std::rethrow_exception(e);
139   }
140
141   // We manage C++ exception ourselves
142   catch (std::exception& e) {
143     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
144     show_backtrace(bt);
145     std::abort();
146   }
147
148   // We don't know how to manage other exceptions
149   catch (...) {
150     // If there was another handler let's delegate to it
151     if (previous_terminate_handler)
152       previous_terminate_handler();
153     else {
154       XBT_ERROR("Unknown uncaught exception");
155       show_backtrace(bt);
156       std::abort();
157     }
158   }
159
160 }
161
162 void install_exception_handler()
163 {
164   static std::once_flag handler_flag;
165   std::call_once(handler_flag, [] {
166     previous_terminate_handler = std::set_terminate(handler);
167   });
168 }
169 // deprecated
170 void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
171 {
172   log_exception(priority, context, exception);
173 }
174 void installExceptionHandler()
175 {
176   install_exception_handler();
177 }
178
179 } // namespace xbt
180 } // namespace simgrid
181
182 void xbt_throw_impossible(const char* file, int line, const char* func)
183 {
184   std::stringstream ss;
185   ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
186   throw std::runtime_error(ss.str());
187 }
188 void xbt_throw_unimplemented(const char* file, int line, const char* func)
189 {
190   std::stringstream ss;
191   ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
192   throw std::runtime_error(ss.str());
193 }