1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/exception.hpp"
4 #include <boost/thread/tss.hpp>
8 #endif /* HAVE_CXXABI_H */
10 using namespace icinga;
12 static boost::thread_specific_ptr<StackTrace> l_LastExceptionStack;
13 static boost::thread_specific_ptr<ContextTrace> l_LastExceptionContext;
17 #ifdef _LIBCPPABI_VERSION
18 class libcxx_type_info : public std::type_info
21 ~libcxx_type_info() override;
23 virtual void noop1() const;
24 virtual void noop2() const;
25 virtual bool can_catch(const libcxx_type_info *thrown_type, void *&adjustedPtr) const = 0;
27 #endif /* _LIBCPPABI_VERSION */
30 #if defined(__GLIBCXX__) || defined(_LIBCPPABI_VERSION)
31 inline void *cast_exception(void *obj, const std::type_info *src, const std::type_info *dst)
34 void *thrown_ptr = obj;
36 /* Check if the exception is a pointer type. */
37 if (src->__is_pointer_p())
38 thrown_ptr = *(void **)thrown_ptr;
40 if (dst->__do_catch(src, &thrown_ptr, 1))
44 #else /* __GLIBCXX__ */
45 const auto *srcInfo = static_cast<const libcxx_type_info *>(src);
46 const auto *dstInfo = static_cast<const libcxx_type_info *>(dst);
50 if (dstInfo->can_catch(srcInfo, adj))
54 #endif /* __GLIBCXX__ */
57 #else /* defined(__GLIBCXX__) || defined(_LIBCPPABI_VERSION) */
58 #define NO_CAST_EXCEPTION
59 #endif /* defined(__GLIBCXX__) || defined(_LIBCPPABI_VERSION) */
61 # if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ > 3)
62 # define TYPEINFO_TYPE std::type_info
64 # define TYPEINFO_TYPE void
67 # if !defined(__GLIBCXX__) && !defined(_WIN32)
68 static boost::thread_specific_ptr<void *> l_LastExceptionObj;
69 static boost::thread_specific_ptr<TYPEINFO_TYPE *> l_LastExceptionPvtInfo;
71 typedef void (*DestCallback)(void *);
72 static boost::thread_specific_ptr<DestCallback> l_LastExceptionDest;
73 # endif /* !__GLIBCXX__ && !_WIN32 */
75 extern "C" void __cxa_throw(void *obj, TYPEINFO_TYPE *pvtinfo, void (*dest)(void *));
76 #endif /* HAVE_CXXABI_H */
78 void icinga::RethrowUncaughtException()
80 #if defined(__GLIBCXX__) || !defined(HAVE_CXXABI_H)
82 #else /* __GLIBCXX__ || !HAVE_CXXABI_H */
83 __cxa_throw(*l_LastExceptionObj.get(), *l_LastExceptionPvtInfo.get(), *l_LastExceptionDest.get());
84 #endif /* __GLIBCXX__ || !HAVE_CXXABI_H */
89 void __cxa_throw(void *obj, TYPEINFO_TYPE *pvtinfo, void (*dest)(void *))
91 auto *tinfo = static_cast<std::type_info *>(pvtinfo);
93 typedef void (*cxa_throw_fn)(void *, std::type_info *, void (*)(void *)) __attribute__((noreturn));
94 static cxa_throw_fn real_cxa_throw;
96 #if !defined(__GLIBCXX__) && !defined(_WIN32)
97 l_LastExceptionObj.reset(new void *(obj));
98 l_LastExceptionPvtInfo.reset(new TYPEINFO_TYPE *(pvtinfo));
99 l_LastExceptionDest.reset(new DestCallback(dest));
100 #endif /* !defined(__GLIBCXX__) && !defined(_WIN32) */
102 if (real_cxa_throw == nullptr)
103 real_cxa_throw = (cxa_throw_fn)dlsym(RTLD_NEXT, "__cxa_throw");
105 #ifndef NO_CAST_EXCEPTION
106 void *uex = cast_exception(obj, tinfo, &typeid(user_error));
107 auto *ex = reinterpret_cast<boost::exception *>(cast_exception(obj, tinfo, &typeid(boost::exception)));
110 #endif /* NO_CAST_EXCEPTION */
112 SetLastExceptionStack(stack);
114 #ifndef NO_CAST_EXCEPTION
115 if (ex && !boost::get_error_info<StackTraceErrorInfo>(*ex))
116 *ex << StackTraceErrorInfo(stack);
118 #endif /* NO_CAST_EXCEPTION */
120 ContextTrace context;
121 SetLastExceptionContext(context);
123 #ifndef NO_CAST_EXCEPTION
124 if (ex && !boost::get_error_info<ContextTraceErrorInfo>(*ex))
125 *ex << ContextTraceErrorInfo(context);
126 #endif /* NO_CAST_EXCEPTION */
128 real_cxa_throw(obj, tinfo, dest);
130 #endif /* HAVE_CXXABI_H */
132 StackTrace *icinga::GetLastExceptionStack()
134 return l_LastExceptionStack.get();
137 void icinga::SetLastExceptionStack(const StackTrace& trace)
139 l_LastExceptionStack.reset(new StackTrace(trace));
142 ContextTrace *icinga::GetLastExceptionContext()
144 return l_LastExceptionContext.get();
147 void icinga::SetLastExceptionContext(const ContextTrace& context)
149 l_LastExceptionContext.reset(new ContextTrace(context));
152 String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, StackTrace *stack, ContextTrace *context)
154 std::ostringstream result;
156 String message = ex.what();
158 const auto *vex = dynamic_cast<const ValidationError *>(&ex);
160 if (message.IsEmpty())
161 result << boost::diagnostic_information(ex) << "\n";
163 result << "Error: " << message << "\n";
165 const auto *dex = dynamic_cast<const ScriptError *>(&ex);
167 if (dex && !dex->GetDebugInfo().Path.IsEmpty())
168 ShowCodeLocation(result, dex->GetDebugInfo());
173 ConfigObject::Ptr dobj = vex->GetObject();
175 di = dobj->GetDebugInfo();
177 Dictionary::Ptr currentHint = vex->GetDebugHint();
181 for (const String& attr : vex->GetAttributePath()) {
182 Dictionary::Ptr props = currentHint->Get("properties");
187 currentHint = props->Get(attr);
192 messages = currentHint->Get("messages");
196 if (messages && messages->GetLength() > 0) {
197 Array::Ptr message = messages->Get(messages->GetLength() - 1);
199 di.Path = message->Get(1);
200 di.FirstLine = message->Get(2);
201 di.FirstColumn = message->Get(3);
202 di.LastLine = message->Get(4);
203 di.LastColumn = message->Get(5);
206 if (!di.Path.IsEmpty())
207 ShowCodeLocation(result, di);
210 const auto *uex = dynamic_cast<const user_error *>(&ex);
211 const auto *pex = dynamic_cast<const posix_error *>(&ex);
213 if (!uex && !pex && verbose) {
214 const StackTrace *st = boost::get_error_info<StackTraceErrorInfo>(ex);
222 stack = GetLastExceptionStack();
230 const ContextTrace *ct = boost::get_error_info<ContextTraceErrorInfo>(ex);
238 context = GetLastExceptionContext();
247 String icinga::DiagnosticInformation(const boost::exception_ptr& eptr, bool verbose)
249 StackTrace *pt = GetLastExceptionStack();
252 ContextTrace *pc = GetLastExceptionContext();
253 ContextTrace context;
262 boost::rethrow_exception(eptr);
263 } catch (const std::exception& ex) {
264 return DiagnosticInformation(ex, verbose, pt ? &stack : nullptr, pc ? &context : nullptr);
267 return boost::diagnostic_information(eptr);
270 ScriptError::ScriptError(String message)
271 : m_Message(std::move(message)), m_IncompleteExpr(false)
274 ScriptError::ScriptError(String message, DebugInfo di, bool incompleteExpr)
275 : m_Message(std::move(message)), m_DebugInfo(std::move(di)), m_IncompleteExpr(incompleteExpr), m_HandledByDebugger(false)
278 const char *ScriptError::what() const throw()
280 return m_Message.CStr();
283 DebugInfo ScriptError::GetDebugInfo() const
288 bool ScriptError::IsIncompleteExpression() const
290 return m_IncompleteExpr;
293 bool ScriptError::IsHandledByDebugger() const
295 return m_HandledByDebugger;
298 void ScriptError::SetHandledByDebugger(bool handled)
300 m_HandledByDebugger = handled;
303 posix_error::~posix_error() throw()
308 const char *posix_error::what() const throw()
311 std::ostringstream msgbuf;
313 const char * const *func = boost::get_error_info<boost::errinfo_api_function>(*this);
316 msgbuf << "Function call '" << *func << "'";
318 msgbuf << "Function call";
320 const std::string *fname = boost::get_error_info<boost::errinfo_file_name>(*this);
323 msgbuf << " for file '" << *fname << "'";
327 const int *errnum = boost::get_error_info<boost::errinfo_errno>(*this);
330 msgbuf << " with error code " << *errnum << ", '" << strerror(*errnum) << "'";
332 String str = msgbuf.str();
333 m_Message = strdup(str.CStr());
339 ValidationError::ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message)
340 : m_Object(object), m_AttributePath(attributePath), m_Message(message)
344 for (const String& attribute : attributePath) {
348 path += "'" + attribute + "'";
351 Type::Ptr type = object->GetReflectionType();
352 m_What = "Validation failed for object '" + object->GetName() + "' of type '" + type->GetName() + "'";
355 m_What += "; Attribute " + path;
357 m_What += ": " + message;
360 ValidationError::~ValidationError() throw()
363 const char *ValidationError::what() const throw()
365 return m_What.CStr();
368 ConfigObject::Ptr ValidationError::GetObject() const
373 std::vector<String> ValidationError::GetAttributePath() const
375 return m_AttributePath;
378 String ValidationError::GetMessage() const
383 void ValidationError::SetDebugHint(const Dictionary::Ptr& dhint)
388 Dictionary::Ptr ValidationError::GetDebugHint() const
393 std::string icinga::to_string(const StackTraceErrorInfo&)
399 std::string icinga::to_string(const errinfo_win32_error& e)
401 return "[errinfo_win32_error] = " + Utility::FormatErrorNumber(e.value()) + "\n";
405 std::string icinga::to_string(const errinfo_getaddrinfo_error& e)
410 msg = gai_strerrorA(e.value());
412 msg = gai_strerror(e.value());
415 return "[errinfo_getaddrinfo_error] = " + String(msg) + "\n";
418 std::string icinga::to_string(const ContextTraceErrorInfo& e)
420 std::ostringstream msgbuf;
421 msgbuf << "[Context] = " << e.value();