Logo AND Algorithmique Numérique Distribuée

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