Logo AND Algorithmique Numérique Distribuée

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