Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define simgrid::xbt::InitializationError (please Sonar).
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/Exception.hpp"
7 #include "src/kernel/context/Context.hpp"
8 #include <xbt/config.hpp>
9 #include <xbt/log.hpp>
10
11 #include <mutex>
12 #include <sstream>
13
14 XBT_LOG_EXTERNAL_CATEGORY(xbt);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
16
17 void _xbt_throw(char* message, int value, const char* file, int line, const char* func)
18 {
19   simgrid::Exception e(
20       simgrid::xbt::ThrowPoint(file, line, func, simgrid::xbt::Backtrace(), xbt_procname(), xbt_getpid()),
21       message ? message : "");
22   xbt_free(message);
23   e.value    = value;
24   throw e;
25 }
26
27 namespace simgrid {
28 namespace xbt {
29
30 ImpossibleError::~ImpossibleError()         = default;
31 InitializationError::~InitializationError() = default;
32 UnimplementedError::~UnimplementedError()   = default;
33
34 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
35 {
36   try {
37     auto name = simgrid::xbt::demangle(typeid(exception).name());
38
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     else
44       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
45
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());
50     }
51
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)
55       return;
56     try {
57       with_nested->rethrow_nested();
58     }
59     catch (std::exception& nested_exception) {
60       log_exception(prio, "Caused by", nested_exception);
61     }
62     // We could catch nested_exception or WithContextException but we don't bother:
63     catch (...) {
64       XBT_LOG(prio, "Caused by an unknown exception");
65     }
66   }
67   catch (...) {
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!");
70   }
71 }
72
73 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
74 {
75   if (simgrid::config::get_value<bool>("exception/cutpath")) {
76     XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
77     return;
78   }
79   std::string res = bt.resolve();
80   XBT_CRITICAL("Current backtrace:");
81   XBT_CRITICAL("  -> %s", res.c_str());
82 }
83
84 static std::terminate_handler previous_terminate_handler = nullptr;
85
86 static void handler()
87 {
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.");
92     std::abort();
93   }
94
95   // Get the current backtrace and exception
96   auto e = std::current_exception();
97   simgrid::xbt::Backtrace bt = simgrid::xbt::Backtrace();
98   try {
99     std::rethrow_exception(e);
100   }
101
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.");
106     std::abort();
107   }
108
109   // We manage C++ exception ourselves
110   catch (std::exception& e) {
111     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
112     show_backtrace(bt);
113     std::abort();
114   }
115
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?");
119     show_backtrace(bt);
120   }
121
122   // We don't know how to manage other exceptions
123   catch (...) {
124     // If there was another handler let's delegate to it
125     if (previous_terminate_handler)
126       previous_terminate_handler();
127     else {
128       XBT_ERROR("Unknown uncaught exception");
129       show_backtrace(bt);
130       std::abort();
131     }
132   }
133   XBT_INFO("BAM");
134 }
135
136 void install_exception_handler()
137 {
138   static std::once_flag handler_flag;
139   std::call_once(handler_flag, [] {
140     previous_terminate_handler = std::set_terminate(handler);
141   });
142 }
143
144 } // namespace xbt
145 } // namespace simgrid
146
147 void xbt_throw_impossible(const char* file, int line, const char* func)
148 {
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());
153 }
154 void xbt_throw_unimplemented(const char* file, int line, const char* func)
155 {
156   std::stringstream ss;
157   ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
158   throw simgrid::xbt::UnimplementedError(ss.str());
159 }