Logo AND Algorithmique Numérique Distribuée

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