1 /* Copyright (c) 2005-2018. 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. */
16 #include <xbt/backtrace.hpp>
17 #include <xbt/config.hpp>
19 #include <xbt/exception.hpp>
21 #include <xbt/log.hpp>
23 XBT_LOG_EXTERNAL_CATEGORY(xbt);
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
29 WithContextException::~WithContextException() = default;
31 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
34 auto name = simgrid::xbt::demangle(typeid(exception).name());
36 auto* with_context = dynamic_cast<const simgrid::xbt::WithContextException*>(&exception);
37 if (with_context != nullptr)
38 XBT_LOG(prio, "%s %s by %s/%d: %s", context, name.get(), with_context->process_name().c_str(),
39 with_context->pid(), exception.what());
41 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
43 // Do we have a backtrace?
44 if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
46 simgrid::xbt::resolve_backtrace(with_context->backtrace().data(), with_context->backtrace().size());
47 for (std::string const& s : backtrace)
48 XBT_LOG(prio, " -> %s", s.c_str());
51 // Do we have a nested exception?
52 auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
53 if (with_nested == nullptr || with_nested->nested_ptr() == nullptr)
56 with_nested->rethrow_nested();
58 catch (std::exception& nested_exception) {
59 log_exception(prio, "Caused by", nested_exception);
61 // We could catch nested_exception or WithContextException but we don't bother:
63 XBT_LOG(prio, "Caused by an unknown exception");
67 // Don't log exceptions we got when trying to log exception
71 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
73 if (simgrid::config::get_value<bool>("exception/cutpath")) {
74 XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
77 std::vector<std::string> res = resolve_backtrace(&bt[0], bt.size());
78 XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
79 for (std::string const& s : res)
80 XBT_LOG(xbt_log_priority_critical, " -> %s", s.c_str());
83 static std::terminate_handler previous_terminate_handler = nullptr;
87 // Avoid doing crazy things if we get an uncaught exception inside
88 // 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 auto bt = backtrace();
99 std::rethrow_exception(e);
102 // We manage C++ exception ourselves
103 catch (std::exception& e) {
104 log_exception(xbt_log_priority_critical, "Uncaught exception", e);
109 // We don't know how to manage other exceptions
111 // If there was another handler let's delegate to it
112 if (previous_terminate_handler)
113 previous_terminate_handler();
115 XBT_ERROR("Unknown uncaught exception");
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 void logException(e_xbt_log_priority_t priority, const char* context, std::exception const& exception)
133 log_exception(priority, context, exception);
135 void installExceptionHandler()
137 install_exception_handler();