1 /* Copyright (c) 2005-2019. 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());
44 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
46 // Do we have a backtrace?
47 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
48 auto backtrace = with_context->resolve_backtrace();
49 XBT_LOG(prio, " -> %s", backtrace.c_str());
52 // Do we have a nested exception?
53 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
54 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
57 with_nested->rethrow_nested();
59 catch (std::exception& nested_exception) {
60 log_exception(prio, "Caused by", nested_exception);
62 // We could catch nested_exception or WithContextException but we don't bother:
64 XBT_LOG(prio, "Caused by an unknown exception");
68 // Don't log exceptions we got when trying to log exception
69 XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
73 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
75 if (simgrid::config::get_value<bool>("exception/cutpath")) {
76 XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
79 std::string res = bt.resolve();
80 XBT_CRITICAL("Current backtrace:");
81 XBT_CRITICAL(" -> %s", res.c_str());
84 static std::terminate_handler previous_terminate_handler = nullptr;
88 // Avoid doing crazy things if we get an uncaught exception inside an uncaught exception
89 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
90 if (lock.test_and_set()) {
91 XBT_ERROR("Handling an exception raised an exception. Bailing out.");
95 // Get the current backtrace and exception
96 auto e = std::current_exception();
97 simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
99 std::rethrow_exception(e);
102 // Parse error are handled differently, as the call stack does not matter, only the file location
103 catch (simgrid::ParseError& e) {
104 XBT_ERROR("%s", e.what());
105 XBT_ERROR("Exiting now.");
109 // We manage C++ exception ourselves
110 catch (std::exception& e) {
111 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
116 catch (const simgrid::ForcefulKillException&) {
117 XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
118 "protected in a try/catch?");
122 // We don't know how to manage other exceptions
124 // If there was another handler let's delegate to it
125 if (previous_terminate_handler)
126 previous_terminate_handler();
128 XBT_ERROR("Unknown uncaught exception");
136 void install_exception_handler()
138 static std::once_flag handler_flag;
139 std::call_once(handler_flag, [] {
140 previous_terminate_handler = std::set_terminate(handler);
145 } // namespace simgrid
147 void xbt_throw_impossible(const char* file, int line, const char* func)
149 std::stringstream ss;
150 ss << file << ":" << line << ":" << func
151 << ": The Impossible Did Happen (yet again). Please report this bug (after reading https://xkcd.com/2200 :)";
152 throw simgrid::xbt::ImpossibleError(ss.str());
154 void xbt_throw_unimplemented(const char* file, int line, const char* func)
156 std::stringstream ss;
157 ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
158 throw simgrid::xbt::UnimplementedError(ss.str());