1 /* Copyright (c) 2005-2019. 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 "src/kernel/context/Context.hpp"
8 #include <xbt/config.hpp>
14 XBT_LOG_EXTERNAL_CATEGORY(xbt);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
17 // DO NOT define ~xbt_ex() in exception.hpp.
18 // Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java.
19 // Doing otherwise naturally breaks things (at least on freebsd with clang).
21 xbt_ex::~xbt_ex() = default;
23 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
25 xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
33 /** @brief returns a short name for the given exception category */
34 const char* xbt_ex_catname(xbt_errcat_t cat)
38 return "unknown error";
40 return "invalid argument";
42 return "out of bounds";
48 return "system error";
50 return "network error";
54 return "action canceled";
56 return "thread error";
60 return "tracing error";
66 return "INVALID ERROR";
73 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
76 auto name = simgrid::xbt::demangle(typeid(exception).name());
78 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
79 if (with_context != nullptr)
80 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
81 with_context->throw_point().pid_, exception.what());
83 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
85 // Do we have a backtrace?
86 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
87 auto backtrace = with_context->resolve_backtrace();
88 XBT_LOG(prio, " -> %s", backtrace.c_str());
91 // Do we have a nested exception?
92 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
93 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
96 with_nested->rethrow_nested();
98 catch (std::exception& nested_exception) {
99 log_exception(prio, "Caused by", nested_exception);
101 // We could catch nested_exception or WithContextException but we don't bother:
103 XBT_LOG(prio, "Caused by an unknown exception");
107 // Don't log exceptions we got when trying to log exception
108 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
112 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
114 if (simgrid::config::get_value<bool>("exception/cutpath")) {
115 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
118 std::string res = bt.resolve();
119 XBT_CRITICAL("Current backtrace:");
120 XBT_CRITICAL(" -> %s", res.c_str());
123 static std::terminate_handler previous_terminate_handler = nullptr;
125 static void handler()
127 // Avoid doing crazy things if we get an uncaught exception inside
128 // an uncaught exception
129 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
130 if (lock.test_and_set()) {
131 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
135 // Get the current backtrace and exception
136 auto e = std::current_exception();
137 simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
139 std::rethrow_exception(e);
142 // We manage C++ exception ourselves
143 catch (std::exception& e) {
144 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
149 catch (const simgrid::ForcefulKillException&) {
150 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
156 // We don't know how to manage other exceptions
158 // If there was another handler let's delegate to it
159 if (previous_terminate_handler)
160 previous_terminate_handler();
162 XBT_ERROR("Unknown uncaught exception");
169 void install_exception_handler()
171 static std::once_flag handler_flag;
172 std::call_once(handler_flag, [] {
173 previous_terminate_handler = std::set_terminate(handler);
178 } // namespace simgrid
180 void xbt_throw_impossible(const char* file, int line, const char* func)
182 std::stringstream ss;
183 ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
184 throw std::runtime_error(ss.str());
186 void xbt_throw_unimplemented(const char* file, int line, const char* func)
188 std::stringstream ss;
189 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
190 throw std::runtime_error(ss.str());