1 /* Copyright (c) 2005-2021. 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>
11 #include <boost/core/demangle.hpp>
15 XBT_LOG_EXTERNAL_CATEGORY(xbt);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
18 void _xbt_throw(char* message, const char* file, int line, const char* func)
21 simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
22 message ? message : "");
30 ImpossibleError::~ImpossibleError() = default;
31 InitializationError::~InitializationError() = default;
32 UnimplementedError::~UnimplementedError() = default;
34 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
37 std::string name = boost::core::demangle(typeid(exception).name());
39 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
40 if (with_context != nullptr) {
41 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.c_str(), with_context->throw_point().procname_.c_str(),
42 with_context->throw_point().pid_, exception.what());
43 // Do we have a backtrace?
44 if (not simgrid::config::get_value<bool>("exception/cutpath")) {
45 auto backtrace = with_context->resolve_backtrace();
46 XBT_LOG(prio, "Backtrace:\n%s", backtrace.c_str());
49 XBT_LOG(prio, "%s %s: %s", context, name.c_str(), exception.what());
52 // Don't log exceptions we got when trying to log exception
53 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
57 // Do we have a nested exception?
58 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
59 if (with_nested != nullptr && with_nested->nested_ptr() != nullptr)
60 with_nested->rethrow_nested();
61 } catch (const std::exception& nested_exception) {
62 log_exception(prio, "Caused by", nested_exception);
64 // We could catch nested_exception or WithContextException but we don't bother:
65 XBT_LOG(prio, "Caused by an unknown exception");
69 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
71 if (simgrid::config::get_value<bool>("exception/cutpath")) {
72 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
75 std::string res = bt.resolve();
76 XBT_CRITICAL("Current backtrace:\n%s", res.c_str());
79 static std::terminate_handler previous_terminate_handler = nullptr;
83 // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
84 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
85 if (lock.test_and_set()) {
86 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
90 // Get the current backtrace and exception
91 simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
93 std::rethrow_exception(std::current_exception());
96 // Parse error are handled differently, as the call stack does not matter, only the file location
97 catch (const simgrid::ParseError& e) {
98 XBT_ERROR("%s", e.what());
99 XBT_ERROR("Exiting now.");
103 // We manage C++ exception ourselves
104 catch (const std::exception& e) {
105 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
110 catch (const simgrid::ForcefulKillException&) {
111 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
112 "protected in a try/catch?");
116 // We don't know how to manage other exceptions
118 // If there was another handler let's delegate to it
119 if (previous_terminate_handler)
120 previous_terminate_handler();
122 XBT_ERROR("Unknown uncaught exception");
130 void install_exception_handler()
132 static std::once_flag handler_flag;
133 std::call_once(handler_flag, [] {
134 previous_terminate_handler = std::set_terminate(handler);
139 } // namespace simgrid
141 void xbt_throw_impossible(const char* file, int line, const char* func)
143 std::stringstream ss;
144 ss << file << ":" << line << ":" << func
145 << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
146 throw simgrid::xbt::ImpossibleError(ss.str());
148 void xbt_throw_unimplemented(const char* file, int line, const char* func)
150 std::stringstream ss;
151 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
152 throw simgrid::xbt::UnimplementedError(ss.str());