1 /* Copyright (c) 2005-2018. The SimGrid Team. All rights reserved. */
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. */
16 #include "simgrid/Exception.hpp"
17 #include <xbt/backtrace.hpp>
18 #include <xbt/config.hpp>
20 #include <xbt/log.hpp>
22 XBT_LOG_EXTERNAL_CATEGORY(xbt);
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
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).
29 xbt_ex::~xbt_ex() = default;
31 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
33 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::backtrace(), xbt_procname(), xbt_getpid()),
41 /** @brief shows an exception content and the associated stack if available */
42 void xbt_ex_display(xbt_ex_t* e)
44 simgrid::xbt::log_exception(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
47 /** @brief returns a short name for the given exception category */
48 const char* xbt_ex_catname(xbt_errcat_t cat)
52 return "unknown error";
54 return "invalid argument";
56 return "out of bounds";
62 return "system error";
64 return "network error";
68 return "action canceled";
70 return "thread error";
74 return "tracing error";
80 return "INVALID ERROR";
82 return "INVALID ERROR";
88 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
91 auto name = simgrid::xbt::demangle(typeid(exception).name());
93 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
94 if (with_context != nullptr)
95 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
96 with_context->throw_point().pid_, exception.what());
98 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
100 // Do we have a backtrace?
101 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
102 auto backtrace = simgrid::xbt::resolve_backtrace(with_context->throw_point().backtrace_.data(),
103 with_context->throw_point().backtrace_.size());
104 for (std::string const& s : backtrace)
105 XBT_LOG(prio, " -> %s", s.c_str());
108 // Do we have a nested exception?
109 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
110 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
113 with_nested->rethrow_nested();
115 catch (std::exception& nested_exception) {
116 log_exception(prio, "Caused by", nested_exception);
118 // We could catch nested_exception or WithContextException but we don't bother:
120 XBT_LOG(prio, "Caused by an unknown exception");
124 // Don't log exceptions we got when trying to log exception
128 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
130 if (simgrid::config::get_value<bool>("exception/cutpath")) {
131 XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
134 std::vector<std::string> res = resolve_backtrace(&bt[0], bt.size());
135 XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
136 for (std::string const& s : res)
137 XBT_LOG(xbt_log_priority_critical, " -> %s", s.c_str());
140 static std::terminate_handler previous_terminate_handler = nullptr;
142 static void handler()
144 // Avoid doing crazy things if we get an uncaught exception inside
145 // an uncaught exception
146 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
147 if (lock.test_and_set()) {
148 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
152 // Get the current backtrace and exception
153 auto e = std::current_exception();
154 auto bt = backtrace();
156 std::rethrow_exception(e);
159 // We manage C++ exception ourselves
160 catch (std::exception& e) {
161 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
166 // We don't know how to manage other exceptions
168 // If there was another handler let's delegate to it
169 if (previous_terminate_handler)
170 previous_terminate_handler();
172 XBT_ERROR("Unknown uncaught exception");
180 void install_exception_handler()
182 static std::once_flag handler_flag;
183 std::call_once(handler_flag, [] {
184 previous_terminate_handler = std::set_terminate(handler);
188 void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
190 log_exception(priority, context, exception);
192 void installExceptionHandler()
194 install_exception_handler();