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(XBT_THROW_POINT), message);
40 /** @brief shows an exception content and the associated stack if available */
41 void xbt_ex_display(xbt_ex_t* e)
43 simgrid::xbt::log_exception(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
46 /** @brief returns a short name for the given exception category */
47 const char* xbt_ex_catname(xbt_errcat_t cat)
51 return "unknown error";
53 return "invalid argument";
55 return "out of bounds";
61 return "system error";
63 return "network error";
67 return "action canceled";
69 return "thread error";
73 return "tracing error";
79 return "INVALID ERROR";
81 return "INVALID ERROR";
87 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
90 auto name = simgrid::xbt::demangle(typeid(exception).name());
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());
97 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
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());
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)
112 with_nested->rethrow_nested();
114 catch (std::exception& nested_exception) {
115 log_exception(prio, "Caused by", nested_exception);
117 // We could catch nested_exception or WithContextException but we don't bother:
119 XBT_LOG(prio, "Caused by an unknown exception");
123 // Don't log exceptions we got when trying to log exception
127 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
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.");
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());
139 static std::terminate_handler previous_terminate_handler = nullptr;
141 static void handler()
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.");
151 // Get the current backtrace and exception
152 auto e = std::current_exception();
153 auto bt = backtrace();
155 std::rethrow_exception(e);
158 // We manage C++ exception ourselves
159 catch (std::exception& e) {
160 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
165 // We don't know how to manage other exceptions
167 // If there was another handler let's delegate to it
168 if (previous_terminate_handler)
169 previous_terminate_handler();
171 XBT_ERROR("Unknown uncaught exception");
179 void install_exception_handler()
181 static std::once_flag handler_flag;
182 std::call_once(handler_flag, [] {
183 previous_terminate_handler = std::set_terminate(handler);
187 void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
189 log_exception(priority, context, exception);
191 void installExceptionHandler()
193 install_exception_handler();