Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix https://framagit.org/simgrid/simgrid/issues/28
[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 UnimplementedError::~UnimplementedError() = default;
32
33 void log_exception(e_xbt_log_priority_t prio, const char* context, std::exception const& exception)
34 {
35   try {
36     auto name = simgrid::xbt::demangle(typeid(exception).name());
37
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.get(), with_context->throw_point().procname_.c_str(),
41               with_context->throw_point().pid_, exception.what());
42     else
43       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
44
45     // Do we have a backtrace?
46     if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
47       auto backtrace = with_context->resolve_backtrace();
48       XBT_LOG(prio, "  -> %s", backtrace.c_str());
49     }
50
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)
54       return;
55     try {
56       with_nested->rethrow_nested();
57     }
58     catch (std::exception& nested_exception) {
59       log_exception(prio, "Caused by", nested_exception);
60     }
61     // We could catch nested_exception or WithContextException but we don't bother:
62     catch (...) {
63       XBT_LOG(prio, "Caused by an unknown exception");
64     }
65   }
66   catch (...) {
67     // Don't log exceptions we got when trying to log exception
68     XBT_LOG(prio, "Ignoring exception caught while while trying to log an exception!");
69   }
70 }
71
72 static void show_backtrace(const simgrid::xbt::Backtrace& bt)
73 {
74   if (simgrid::config::get_value<bool>("exception/cutpath")) {
75     XBT_CRITICAL("Display of current backtrace disabled by --cfg=exception/cutpath.");
76     return;
77   }
78   std::string res = bt.resolve();
79   XBT_CRITICAL("Current backtrace:");
80   XBT_CRITICAL("  -> %s", res.c_str());
81 }
82
83 static std::terminate_handler previous_terminate_handler = nullptr;
84
85 static void handler()
86 {
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.");
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   // We manage C++ exception ourselves
103   catch (std::exception& e) {
104     log_exception(xbt_log_priority_critical, "Uncaught exception", e);
105     show_backtrace(bt);
106     std::abort();
107   }
108
109   catch (const simgrid::ForcefulKillException&) {
110     XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
111               "protected "
112               "in a try/catch?");
113     show_backtrace(bt);
114   }
115
116   // We don't know how to manage other exceptions
117   catch (...) {
118     // If there was another handler let's delegate to it
119     if (previous_terminate_handler)
120       previous_terminate_handler();
121     else {
122       XBT_ERROR("Unknown uncaught exception");
123       show_backtrace(bt);
124       std::abort();
125     }
126   }
127 }
128
129 void install_exception_handler()
130 {
131   static std::once_flag handler_flag;
132   std::call_once(handler_flag, [] {
133     previous_terminate_handler = std::set_terminate(handler);
134   });
135 }
136
137 } // namespace xbt
138 } // namespace simgrid
139
140 void xbt_throw_impossible(const char* file, int line, const char* func)
141 {
142   std::stringstream ss;
143   ss << file << ":" << line << ":" << func << ": The Impossible Did Happen (yet again). Please report this bug.";
144   throw simgrid::xbt::ImpossibleError(ss.str());
145 }
146 void xbt_throw_unimplemented(const char* file, int line, const char* func)
147 {
148   std::stringstream ss;
149   ss << file << ":" << line << ":" << func << ": Feature unimplemented yet. Please report this bug.";
150   throw simgrid::xbt::UnimplementedError(ss.str());
151 }