Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the plugins from the command line, and document it
[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 XBT_LOG_EXTERNAL_CATEGORY(xbt);
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
25
26 namespace simgrid {
27 namespace xbt {
28
29 WithContextException::~WithContextException() = default;
30
31 void logException(
32   e_xbt_log_priority_t prio,
33   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::xbt::WithContextException*>(&exception);
39     if (with_context != nullptr)
40       XBT_LOG(prio, "%s %s by %s/%d: %s",
41         context, name.get(),
42         with_context->processName().c_str(), with_context->pid(),
43         exception.what());
44     else
45       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
46
47     // Do we have a backtrace?
48     if (with_context != nullptr && not simgrid::config::get_value<bool>("exception/cutpath")) {
49       auto backtrace = simgrid::xbt::resolveBacktrace(
50         with_context->backtrace().data(), with_context->backtrace().size());
51       for (std::string const& s : backtrace)
52         XBT_LOG(prio, "  -> %s", s.c_str());
53     }
54
55     // Do we have a nested exception?
56     auto* with_nested = dynamic_cast<const std::nested_exception*>(&exception);
57     if (with_nested == nullptr ||  with_nested->nested_ptr() == nullptr)
58       return;
59     try {
60       with_nested->rethrow_nested();
61     }
62     catch (std::exception& nested_exception) {
63       logException(prio, "Caused by", nested_exception);
64     }
65     // We could catch nested_exception or WithContextException but we don't bother:
66     catch (...) {
67       XBT_LOG(prio, "Caused by an unknown exception");
68     }
69   }
70   catch (...) {
71     // Don't log exceptions we got when trying to log exception
72   }
73 }
74
75 static void showBacktrace(std::vector<xbt_backtrace_location_t>& bt)
76 {
77   if (simgrid::config::get_value<bool>("exception/cutpath")) {
78     XBT_LOG(xbt_log_priority_critical, "Display of current backtrace disabled by --cfg=exception/cutpath.");
79     return;
80   }
81   std::vector<std::string> res = resolveBacktrace(&bt[0], bt.size());
82   XBT_LOG(xbt_log_priority_critical, "Current backtrace:");
83   for (std::string const& s : res)
84     XBT_LOG(xbt_log_priority_critical, "  -> %s", s.c_str());
85 }
86
87 static std::terminate_handler previous_terminate_handler = nullptr;
88
89 static void handler()
90 {
91   // Avoid doing crazy things if we get an uncaught exception inside
92   // an uncaught exception
93   static std::atomic_flag lock = ATOMIC_FLAG_INIT;
94   if (lock.test_and_set()) {
95     XBT_ERROR("Handling an exception raised an exception. Bailing out.");
96     std::abort();
97   }
98
99   // Get the current backtrace and exception
100   auto e = std::current_exception();
101   auto bt = backtrace();
102   try {
103     std::rethrow_exception(e);
104   }
105
106   // We manage C++ exception ourselves
107   catch (std::exception& e) {
108     logException(xbt_log_priority_critical, "Uncaught exception", e);
109     showBacktrace(bt);
110     std::abort();
111   }
112
113   // We don't know how to manage other exceptions
114   catch (...) {
115     // If there was another handler let's delegate to it
116     if (previous_terminate_handler)
117       previous_terminate_handler();
118     else {
119       XBT_ERROR("Unknown uncaught exception");
120       showBacktrace(bt);
121       std::abort();
122     }
123   }
124
125 }
126
127 void installExceptionHandler()
128 {
129   static std::once_flag handler_flag;
130   std::call_once(handler_flag, [] {
131     previous_terminate_handler = std::set_terminate(handler);
132   });
133 }
134
135 }
136 }