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