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 <xbt/config.hpp>
10 #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 : "");
27 namespace simgrid::xbt {
29 ImpossibleError::~ImpossibleError() = default;
30 InitializationError::~InitializationError() = default;
31 UnimplementedError::~UnimplementedError() = default;
33 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
36 std::string name = boost::core::demangle(typeid(exception).name());
38 auto* with_context = dynamic_cast<const simgrid::Exception*>(&exception);
39 if (with_context != nullptr) {
40 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.c_str(), with_context->throw_point().procname_.c_str(),
41 with_context->throw_point().pid_, exception.what());
42 // Do we have a backtrace?
43 if (not simgrid::config::get_value<bool>("exception/cutpath")) {
44 auto backtrace = with_context->resolve_backtrace();
45 XBT_LOG(prio, "Backtrace:\n%s", backtrace.c_str());
48 XBT_LOG(prio, "%s %s: %s", context, name.c_str(), exception.what());
51 // Don't log exceptions we got when trying to log exception
52 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
56 // Do we have a nested exception?
57 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
58 if (with_nested != nullptr && with_nested->nested_ptr() != nullptr)
59 with_nested->rethrow_nested();
60 } catch (const std::exception& nested_exception) {
61 log_exception(prio, "Caused by", nested_exception);
63 // We could catch nested_exception or WithContextException but we don't bother:
64 XBT_LOG(prio, "Caused by an unknown exception");
68 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
70 if (simgrid::config::get_value<bool>("exception/cutpath")) {
71 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
74 std::string res = bt.resolve();
75 XBT_CRITICAL("Current backtrace:\n%s", res.c_str());
78 static std::terminate_handler previous_terminate_handler = nullptr;
80 XBT_ATTRIB_NORETURN static void handler()
82 // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
83 if (static std::atomic_flag lock = ATOMIC_FLAG_INIT; lock.test_and_set()) {
84 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
88 // Get the current exception and show backtrace
90 std::rethrow_exception(std::current_exception());
93 // Parse error are handled differently, as the call stack does not matter, only the file location
94 catch (const simgrid::ParseError& e) {
95 XBT_ERROR("%s", e.what());
96 XBT_ERROR("Exiting now.");
100 // We manage C++ exception ourselves
101 catch (const std::exception& e) {
102 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
105 catch (const simgrid::ForcefulKillException&) {
106 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
107 "protected in a try/catch?");
110 // We don't know how to manage other exceptions
112 // If there was another handler let's delegate to it
113 if (previous_terminate_handler) {
114 previous_terminate_handler();
115 XBT_ERROR("Unexpected return from delegated terminate handler");
117 XBT_ERROR("Unknown uncaught exception");
119 show_backtrace(simgrid::xbt::Backtrace());
123 void install_exception_handler()
125 static std::once_flag handler_flag;
126 std::call_once(handler_flag, [] {
127 previous_terminate_handler = std::set_terminate(handler);
131 } // namespace simgrid::xbt
133 void xbt_throw_impossible(const char* file, int line, const char* func)
135 std::stringstream ss;
136 ss << file << ":" << line << ":" << func
137 << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
138 throw simgrid::xbt::ImpossibleError(ss.str());
140 void xbt_throw_unimplemented(const char* file, int line, const char* func)
142 std::stringstream ss;
143 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
144 throw simgrid::xbt::UnimplementedError(ss.str());