Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #193 from Takishipp/signals
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2016. The SimGrid Team.
2  * All rights reserved. */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdlib>
8
9 #include <atomic>
10 #include <exception>
11 #include <string>
12 #include <typeinfo>
13 #include <vector>
14 #include <memory>
15 #include <mutex>
16
17 #include <xbt/backtrace.hpp>
18 #include <xbt/exception.hpp>
19 #include <xbt/log.h>
20 #include <xbt/log.hpp>
21
22 extern "C" {
23 XBT_LOG_EXTERNAL_CATEGORY(xbt);
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
25 }
26
27 namespace simgrid {
28 namespace xbt {
29
30 WithContextException::~WithContextException() = default;
31
32 void logException(
33   e_xbt_log_priority_t prio,
34   const char* context, std::exception const& exception)
35 {
36   try {
37     auto name = simgrid::xbt::demangle(typeid(exception).name());
38
39     auto with_context =
40       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) {
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   std::vector<std::string> res = resolveBacktrace(&bt[0], bt.size());
80   XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
81   for (std::string const& s : res)
82     XBT_LOG(xbt_log_priority_critical, "  -> %s", s.c_str());
83 }
84
85 static std::terminate_handler previous_terminate_handler = nullptr;
86
87 static void handler()
88 {
89   // Avoid doing crazy things if we get an uncaught exception inside
90   // an uncaught exception
91   static std::atomic_flag lock = ATOMIC_FLAG_INIT;
92   if (lock.test_and_set()) {
93     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
94     std::abort();
95   }
96
97   // Get the current backtrace and exception
98   auto e = std::current_exception();
99   auto bt = backtrace();
100   try {
101     std::rethrow_exception(e);
102   }
103
104   // We manage C++ exception ourselves
105   catch (std::exception& e) {
106     logException(xbt_log_priority_critical, "Uncaught exception", e);
107     showBacktrace(bt);
108     std::abort();
109   }
110
111   // We don't know how to manage other exceptions
112   catch (...) {
113     // If there was another handler let's delegate to it
114     if (previous_terminate_handler)
115       previous_terminate_handler();
116     else {
117       XBT_ERROR("Unknown uncaught exception");
118       showBacktrace(bt);
119       std::abort();
120     }
121   }
122
123 }
124
125 void installExceptionHandler()
126 {
127   static std::once_flag handler_flag;
128   std::call_once(handler_flag, [] {
129     previous_terminate_handler = std::set_terminate(handler);
130   });
131 }
132
133 }
134 }