1 /* Copyright (c) 2005-2022. 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>
16 XBT_LOG_EXTERNAL_CATEGORY(xbt);
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
19 void _xbt_throw(char* message, const char* file, int line, const char* func)
22 simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
23 message ? message : "");
31 ImpossibleError::~ImpossibleError() = default;
32 InitializationError::~InitializationError() = default;
33 UnimplementedError::~UnimplementedError() = default;
35 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
38 std::string name = boost::core::demangle(typeid(exception).name());
40 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
41 if (with_context != nullptr) {
42 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.c_str(), with_context->throw_point().procname_.c_str(),
43 with_context->throw_point().pid_, exception.what());
44 // Do we have a backtrace?
45 if (not simgrid::config::get_value<bool>("exception/cutpath")) {
46 auto backtrace = with_context->resolve_backtrace();
47 XBT_LOG(prio, "Backtrace:\n%s", backtrace.c_str());
50 XBT_LOG(prio, "%s %s: %s", context, name.c_str(), exception.what());
53 // Don't log exceptions we got when trying to log exception
54 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
58 // Do we have a nested exception?
59 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
60 if (with_nested != nullptr && with_nested->nested_ptr() != nullptr)
61 with_nested->rethrow_nested();
62 } catch (const std::exception& nested_exception) {
63 log_exception(prio, "Caused by", nested_exception);
65 // We could catch nested_exception or WithContextException but we don't bother:
66 XBT_LOG(prio, "Caused by an unknown exception");
70 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
72 if (simgrid::config::get_value<bool>("exception/cutpath")) {
73 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
76 std::string res = bt.resolve();
77 XBT_CRITICAL("Current backtrace:\n%s", res.c_str());
80 static std::terminate_handler previous_terminate_handler = nullptr;
82 XBT_ATTRIB_NORETURN static void handler()
84 // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
85 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
86 if (lock.test_and_set()) {
87 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
91 // Get the current exception and show 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);
108 catch (const simgrid::ForcefulKillException&) {
109 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
110 "protected in a try/catch?");
113 // We don't know how to manage other exceptions
115 // If there was another handler let's delegate to it
116 if (previous_terminate_handler) {
117 previous_terminate_handler();
118 XBT_ERROR("Unexpected return from delegated terminate handler");
120 XBT_ERROR("Unknown uncaught exception");
122 show_backtrace(simgrid::xbt::Backtrace());
126 void install_exception_handler()
128 static std::once_flag handler_flag;
129 std::call_once(handler_flag, [] {
130 previous_terminate_handler = std::set_terminate(handler);
135 } // namespace simgrid
137 void xbt_throw_impossible(const char* file, int line, const char* func)
139 std::stringstream ss;
140 ss << file << ":" << line << ":" << func
141 << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
142 throw simgrid::xbt::ImpossibleError(ss.str());
144 void xbt_throw_unimplemented(const char* file, int line, const char* func)
146 std::stringstream ss;
147 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
148 throw simgrid::xbt::UnimplementedError(ss.str());