Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove 2 (out of 3) horrible hacks around Java contexts
[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 "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 // DO NOT define ~xbt_ex() in exception.hpp.
18 // Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java.
19 // Doing otherwise naturally breaks things (at least on freebsd with clang).
20
21 xbt_ex::~xbt_ex() = default;
22
23 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
24 {
25   xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
26            message);
27   xbt_free(message);
28   e.category = errcat;
29   e.value    = value;
30   throw e;
31 }
32
33 /** @brief returns a short name for the given exception category */
34 const char* xbt_ex_catname(xbt_errcat_t cat)
35 {
36   switch (cat) {
37     case unknown_error:
38       return "unknown error";
39     case arg_error:
40       return "invalid argument";
41     case bound_error:
42       return "out of bounds";
43     case mismatch_error:
44       return "mismatch";
45     case not_found_error:
46       return "not found";
47     case system_error:
48       return "system error";
49     case network_error:
50       return "network error";
51     case timeout_error:
52       return "timeout";
53     case cancel_error:
54       return "action canceled";
55     case thread_error:
56       return "thread error";
57     case host_error:
58       return "host failed";
59     case tracing_error:
60       return "tracing error";
61     case io_error:
62       return "io error";
63     case vm_error:
64       return "vm error";
65     default:
66       return "INVALID ERROR";
67   }
68   return "INVALID ERROR";
69 }
70
71 namespace simgrid {
72 namespace xbt {
73
74 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
75 {
76   try {
77     auto name = simgrid::xbt::demangle(typeid(exception).name());
78
79     auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
80     if (with_context != nullptr)
81       XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
82               with_context->throw_point().pid_, exception.what());
83     else
84       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
85
86     // Do we have a backtrace?
87     if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
88       auto backtrace = with_context->resolve_backtrace();
89       XBT_LOG(prio, "  -> %s", backtrace.c_str());
90     }
91
92     // Do we have a nested exception?
93     auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
94     if (with_nested == nullptr ||  with_nested->nested_ptr() == nullptr)
95       return;
96     try {
97       with_nested->rethrow_nested();
98     }
99     catch (std::exception& nested_exception) {
100       log_exception(prio, "Caused by", nested_exception);
101     }
102     // We could catch nested_exception or WithContextException but we don't bother:
103     catch (...) {
104       XBT_LOG(prio, "Caused by an unknown exception");
105     }
106   }
107   catch (...) {
108     // Don't log exceptions we got when trying to log exception
109   }
110 }
111
112 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
113 {
114   if (simgrid::config::get_value<bool>("exception/cutpath")) {
115     XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
116     return;
117   }
118   std::string res = bt.resolve();
119   XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
120   XBT_LOG(xbt_log_priority_critical, "  -> %s", res.c_str());
121 }
122
123 static std::terminate_handler previous_terminate_handler = nullptr;
124
125 static void handler()
126 {
127   // Avoid doing crazy things if we get an uncaught exception inside
128   // an uncaught exception
129   static std::atomic_flag lock = ATOMIC_FLAG_INIT;
130   if (lock.test_and_set()) {
131     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
132     std::abort();
133   }
134
135   // Get the current backtrace and exception
136   auto e = std::current_exception();
137   simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
138   try {
139     std::rethrow_exception(e);
140   }
141
142   // We manage C++ exception ourselves
143   catch (std::exception& e) {
144     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
145     show_backtrace(bt);
146     std::abort();
147   }
148
149   catch (simgrid::kernel::context::Context::StopRequest& e) {
150     XBT_ERROR("Received a StopRequest at the top-level exception handler. Maybe a Java->C++ call that is not protected "
151               "in a try/catch?");
152     show_backtrace(bt);
153   }
154
155   // We don't know how to manage other exceptions
156   catch (...) {
157     // If there was another handler let's delegate to it
158     if (previous_terminate_handler)
159       previous_terminate_handler();
160     else {
161       XBT_ERROR("Unknown uncaught exception");
162       show_backtrace(bt);
163       std::abort();
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 }