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>
14 XBT_LOG_EXTERNAL_CATEGORY(xbt);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
17 void _xbt_throw(char* message, int value, const char* file, int line, const char* func)
20 simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
21 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 auto name = simgrid::xbt::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.get(), 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, " -> %s", backtrace.c_str());
49 XBT_LOG(prio, "%s %s: %s", context, name.get(), 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:");
77 XBT_CRITICAL(" -> %s", res.c_str());
80 static std::terminate_handler previous_terminate_handler = nullptr;
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 backtrace and exception
92 simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
94 std::rethrow_exception(std::current_exception());
97 // Parse error are handled differently, as the call stack does not matter, only the file location
98 catch (const simgrid::ParseError& e) {
99 XBT_ERROR("%s", e.what());
100 XBT_ERROR("Exiting now.");
104 // We manage C++ exception ourselves
105 catch (const std::exception& e) {
106 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
111 catch (const simgrid::ForcefulKillException&) {
112 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
113 "protected in a try/catch?");
117 // We don't know how to manage other exceptions
119 // If there was another handler let's delegate to it
120 if (previous_terminate_handler)
121 previous_terminate_handler();
123 XBT_ERROR("Unknown uncaught exception");
131 void install_exception_handler()
133 static std::once_flag handler_flag;
134 std::call_once(handler_flag, [] {
135 previous_terminate_handler = std::set_terminate(handler);
140 } // namespace simgrid
142 void xbt_throw_impossible(const char* file, int line, const char* func)
144 std::stringstream ss;
145 ss << file << ":" << line << ":" << func
146 << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
147 throw simgrid::xbt::ImpossibleError(ss.str());
149 void xbt_throw_unimplemented(const char* file, int line, const char* func)
151 std::stringstream ss;
152 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
153 throw simgrid::xbt::UnimplementedError(ss.str());