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. */
6 #include "simgrid/Exception.hpp"
7 #include <xbt/config.hpp>
13 XBT_LOG_EXTERNAL_CATEGORY(xbt);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
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).
20 xbt_ex::~xbt_ex() = default;
22 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
24 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::backtrace(), xbt_procname(), xbt_getpid()),
32 /** @brief shows an exception content and the associated stack if available */
33 void xbt_ex_display(xbt_ex_t* e)
35 simgrid::xbt::log_exception(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
38 /** @brief returns a short name for the given exception category */
39 const char* xbt_ex_catname(xbt_errcat_t cat)
43 return "unknown error";
45 return "invalid argument";
47 return "out of bounds";
53 return "system error";
55 return "network error";
59 return "action canceled";
61 return "thread error";
65 return "tracing error";
71 return "INVALID ERROR";
73 return "INVALID ERROR";
79 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
82 auto name = simgrid::xbt::demangle(typeid(exception).name());
84 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
85 if (with_context != nullptr)
86 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
87 with_context->throw_point().pid_, exception.what());
89 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
91 // Do we have a backtrace?
92 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
93 auto backtrace = simgrid::xbt::resolve_backtrace(with_context->throw_point().backtrace_.data(),
94 with_context->throw_point().backtrace_.size());
95 for (std::string const& s : backtrace)
96 XBT_LOG(prio, " -> %s", s.c_str());
99 // Do we have a nested exception?
100 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
101 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
104 with_nested->rethrow_nested();
106 catch (std::exception& nested_exception) {
107 log_exception(prio, "Caused by", nested_exception);
109 // We could catch nested_exception or WithContextException but we don't bother:
111 XBT_LOG(prio, "Caused by an unknown exception");
115 // Don't log exceptions we got when trying to log exception
119 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
121 if (simgrid::config::get_value<bool>("exception/cutpath")) {
122 XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
125 std::vector<std::string> res = resolve_backtrace(&bt[0], bt.size());
126 XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
127 for (std::string const& s : res)
128 XBT_LOG(xbt_log_priority_critical, " -> %s", s.c_str());
131 static std::terminate_handler previous_terminate_handler = nullptr;
133 static void handler()
135 // Avoid doing crazy things if we get an uncaught exception inside
136 // an uncaught exception
137 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
138 if (lock.test_and_set()) {
139 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
143 // Get the current backtrace and exception
144 auto e = std::current_exception();
145 auto bt = backtrace();
147 std::rethrow_exception(e);
150 // We manage C++ exception ourselves
151 catch (std::exception& e) {
152 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
157 // We don't know how to manage other exceptions
159 // If there was another handler let's delegate to it
160 if (previous_terminate_handler)
161 previous_terminate_handler();
163 XBT_ERROR("Unknown uncaught exception");
171 void install_exception_handler()
173 static std::once_flag handler_flag;
174 std::call_once(handler_flag, [] {
175 previous_terminate_handler = std::set_terminate(handler);
179 void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
181 log_exception(priority, context, exception);
183 void installExceptionHandler()
185 install_exception_handler();
189 } // namespace simgrid
191 void xbt_throw_impossible(const char* file, int line, const char* func)
193 std::stringstream ss;
194 ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
195 throw std::runtime_error(ss.str());
197 void xbt_throw_unimplemented(const char* file, int line, const char* func)
199 std::stringstream ss;
200 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
201 throw std::runtime_error(ss.str());