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 ImpossibleError::~ImpossibleError() = default;
74 UnimplementedError::~UnimplementedError() = default;
76 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
79 auto name = simgrid::xbt::demangle(typeid(exception).name());
81 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
82 if (with_context != nullptr)
83 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->throw_point().procname_.c_str(),
84 with_context->throw_point().pid_, exception.what());
86 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
88 // Do we have a backtrace?
89 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
90 auto backtrace = with_context->resolve_backtrace();
91 XBT_LOG(prio, " -> %s", backtrace.c_str());
94 // Do we have a nested exception?
95 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
96 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
99 with_nested->rethrow_nested();
101 catch (std::exception& nested_exception) {
102 log_exception(prio, "Caused by", nested_exception);
104 // We could catch nested_exception or WithContextException but we don't bother:
106 XBT_LOG(prio, "Caused by an unknown exception");
110 // Don't log exceptions we got when trying to log exception
111 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
115 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
117 if (simgrid::config::get_value<bool>("exception/cutpath")) {
118 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
121 std::string res = bt.resolve();
122 XBT_CRITICAL("Current backtrace:");
123 XBT_CRITICAL(" -> %s", res.c_str());
126 static std::terminate_handler previous_terminate_handler = nullptr;
128 static void handler()
130 // Avoid doing crazy things if we get an uncaught exception inside
131 // an uncaught exception
132 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
133 if (lock.test_and_set()) {
134 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
138 // Get the current backtrace and exception
139 auto e = std::current_exception();
140 simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
142 std::rethrow_exception(e);
145 // We manage C++ exception ourselves
146 catch (std::exception& e) {
147 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
152 catch (const simgrid::ForcefulKillException&) {
153 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
159 // We don't know how to manage other exceptions
161 // If there was another handler let's delegate to it
162 if (previous_terminate_handler)
163 previous_terminate_handler();
165 XBT_ERROR("Unknown uncaught exception");
172 void install_exception_handler()
174 static std::once_flag handler_flag;
175 std::call_once(handler_flag, [] {
176 previous_terminate_handler = std::set_terminate(handler);
181 } // namespace simgrid
183 void xbt_throw_impossible(const char* file, int line, const char* func)
185 std::stringstream ss;
186 ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
187 throw simgrid::xbt::ImpossibleError(ss.str());
189 void xbt_throw_unimplemented(const char* file, int line, const char* func)
191 std::stringstream ss;
192 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
193 throw simgrid::xbt::UnimplementedError(ss.str());