Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused typedefs.
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2018. 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 <cstdlib>
7
8 #include <atomic>
9 #include <exception>
10 #include <string>
11 #include <typeinfo>
12 #include <vector>
13 #include <memory>
14 #include <mutex>
15
16 #include <xbt/backtrace.hpp>
17 #include <xbt/config.hpp>
18 #include <xbt/ex.hpp>
19 #include <xbt/exception.hpp>
20 #include <xbt/log.h>
21 #include <xbt/log.hpp>
22
23 extern "C" {
24 XBT_LOG_EXTERNAL_CATEGORY(xbt);
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
26 }
27
28 namespace simgrid {
29 namespace xbt {
30
31 WithContextException::~WithContextException() = default;
32
33 void logException(
34   e_xbt_log_priority_t prio,
35   const char* context, std::exception const& exception)
36 {
37   try {
38     auto name = simgrid::xbt::demangle(typeid(exception).name());
39
40     auto* with_context = dynamic_cast<const simgrid::xbt::WithContextException*>(&exception);
41     if (with_context != nullptr)
42       XBT_LOG(prio, "%s %s by %s/%d: %s",
43         context, name.get(),
44         with_context->processName().c_str(), with_context->pid(),
45         exception.what());
46     else
47       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
48
49     // Do we have a backtrace?
50     if (with_context != nullptr && not xbt_cfg_get_boolean("exception/cutpath")) {
51       auto backtrace = simgrid::xbt::resolveBacktrace(
52         with_context->backtrace().data(), with_context->backtrace().size());
53       for (std::string const& s : backtrace)
54         XBT_LOG(prio, "  -> %s", s.c_str());
55     }
56
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       return;
61     try {
62       with_nested->rethrow_nested();
63     }
64     catch (std::exception& nested_exception) {
65       logException(prio, "Caused by", nested_exception);
66     }
67     // We could catch nested_exception or WithContextException but we don't bother:
68     catch (...) {
69       XBT_LOG(prio, "Caused by an unknown exception");
70     }
71   }
72   catch (...) {
73     // Don't log exceptions we got when trying to log exception
74   }
75 }
76
77 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
78 {
79   if (xbt_cfg_get_boolean("exception/cutpath")) {
80     XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
81     return;
82   }
83   std::vector<std::string> res = resolveBacktrace(&bt[0], bt.size());
84   XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
85   for (std::string const& s : res)
86     XBT_LOG(xbt_log_priority_critical, "  -> %s", s.c_str());
87 }
88
89 static std::terminate_handler previous_terminate_handler = nullptr;
90
91 static void handler()
92 {
93   // Avoid doing crazy things if we get an uncaught exception inside
94   // an uncaught exception
95   static std::atomic_flag lock = ATOMIC_FLAG_INIT;
96   if (lock.test_and_set()) {
97     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
98     std::abort();
99   }
100
101   // Get the current backtrace and exception
102   auto e = std::current_exception();
103   auto bt = backtrace();
104   try {
105     std::rethrow_exception(e);
106   }
107
108   // We manage C++ exception ourselves
109   catch (std::exception& e) {
110     logException(xbt_log_priority_critical, "Uncaught exception", e);
111     showBacktrace(bt);
112     std::abort();
113   }
114
115   // We don't know how to manage other exceptions
116   catch (...) {
117     // If there was another handler let's delegate to it
118     if (previous_terminate_handler)
119       previous_terminate_handler();
120     else {
121       XBT_ERROR("Unknown uncaught exception");
122       showBacktrace(bt);
123       std::abort();
124     }
125   }
126
127 }
128
129 void installExceptionHandler()
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 }
138 }