+
+static std::terminate_handler previous_terminate_handler = nullptr;
+
+static void handler()
+{
+ // Avoid doing crazy things if we get an uncaught exception inside
+ // an uncaught exception
+ static std::atomic_flag lock = ATOMIC_FLAG_INIT;
+ if (lock.test_and_set()) {
+ XBT_ERROR("Handling an exception raised an exception. Bailing out.");
+ std::abort();
+ }
+
+ // Get the current backtrace and exception
+ auto e = std::current_exception();
+ simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
+ try {
+ std::rethrow_exception(e);
+ }
+
+ // We manage C++ exception ourselves
+ catch (std::exception& e) {
+ log_exception(xbt_log_priority_critical, "Uncaught exception", e);
+ show_backtrace(bt);
+ std::abort();
+ }
+
+ catch (simgrid::ForcefulKillException const& e) {
+ XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
+ "protected "
+ "in a try/catch?");
+ show_backtrace(bt);
+ }
+
+ // We don't know how to manage other exceptions
+ catch (...) {
+ // If there was another handler let's delegate to it
+ if (previous_terminate_handler)
+ previous_terminate_handler();
+ else {
+ XBT_ERROR("Unknown uncaught exception");
+ show_backtrace(bt);
+ std::abort();
+ }
+ }
+}
+
+void install_exception_handler()
+{
+ static std::once_flag handler_flag;
+ std::call_once(handler_flag, [] {
+ previous_terminate_handler = std::set_terminate(handler);
+ });
+}
+// deprecated
+void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
+{
+ log_exception(priority, context, exception);
+}
+void installExceptionHandler()
+{
+ install_exception_handler();
+}
+
+} // namespace xbt
+} // namespace simgrid
+
+void xbt_throw_impossible(const char* file, int line, const char* func)
+{
+ std::stringstream ss;
+ ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
+ throw std::runtime_error(ss.str());
+}
+void xbt_throw_unimplemented(const char* file, int line, const char* func)
+{
+ std::stringstream ss;
+ ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
+ throw std::runtime_error(ss.str());