2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <boost/thread/tss.hpp>
|
2012-04-03 13:01:00 +02:00
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
#ifdef HAVE_CXXABI_H
|
2014-04-20 14:09:01 +02:00
|
|
|
# include <cxxabi.h>
|
2014-09-02 13:02:22 +02:00
|
|
|
#endif /* HAVE_CXXABI_H */
|
2014-04-20 14:09:01 +02:00
|
|
|
|
2012-04-03 13:01:00 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
static boost::thread_specific_ptr<StackTrace> l_LastExceptionStack;
|
|
|
|
static boost::thread_specific_ptr<ContextTrace> l_LastExceptionContext;
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
#ifdef HAVE_CXXABI_H
|
2014-04-22 07:13:16 +02:00
|
|
|
# if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ > 3)
|
|
|
|
# define TYPEINFO_TYPE std::type_info
|
|
|
|
# else
|
|
|
|
# define TYPEINFO_TYPE void
|
|
|
|
# endif
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
# if !defined(__GLIBCXX__) && !defined(_WIN32)
|
2014-04-14 03:02:33 +02:00
|
|
|
static boost::thread_specific_ptr<void *> l_LastExceptionObj;
|
2014-04-22 07:13:16 +02:00
|
|
|
static boost::thread_specific_ptr<TYPEINFO_TYPE *> l_LastExceptionPvtInfo;
|
2014-04-14 03:02:33 +02:00
|
|
|
|
|
|
|
typedef void (*DestCallback)(void *);
|
|
|
|
static boost::thread_specific_ptr<DestCallback> l_LastExceptionDest;
|
|
|
|
|
2014-04-22 07:13:16 +02:00
|
|
|
extern "C" void __cxa_throw(void *obj, TYPEINFO_TYPE *pvtinfo, void (*dest)(void *));
|
2014-04-14 03:02:33 +02:00
|
|
|
extern "C" void __cxa_rethrow_primary_exception(void* thrown_object);
|
2014-09-02 13:02:22 +02:00
|
|
|
# endif /* !__GLIBCXX__ && !_WIN32 */
|
|
|
|
#endif /* HAVE_CXXABI_H */
|
2014-04-14 03:02:33 +02:00
|
|
|
|
|
|
|
void icinga::RethrowUncaughtException(void)
|
|
|
|
{
|
2014-09-02 13:02:22 +02:00
|
|
|
#if defined(__GLIBCXX__) || !defined(HAVE_CXXABI_H)
|
2014-04-14 03:02:33 +02:00
|
|
|
throw;
|
2014-04-14 09:50:54 +02:00
|
|
|
#else /* __GLIBCXX__ || _WIN32 */
|
2014-04-14 03:02:33 +02:00
|
|
|
__cxa_throw(*l_LastExceptionObj.get(), *l_LastExceptionPvtInfo.get(), *l_LastExceptionDest.get());
|
2014-04-14 09:50:54 +02:00
|
|
|
#endif /* __GLIBCXX__ || _WIN32 */
|
2014-04-14 03:02:33 +02:00
|
|
|
}
|
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
#ifdef HAVE_CXXABI_H
|
2013-03-07 19:44:39 +01:00
|
|
|
extern "C"
|
2014-04-20 14:09:01 +02:00
|
|
|
void __cxa_throw(void *obj, TYPEINFO_TYPE *pvtinfo, void (*dest)(void *))
|
2013-03-07 19:44:39 +01:00
|
|
|
{
|
2014-04-20 14:09:01 +02:00
|
|
|
std::type_info *tinfo = static_cast<std::type_info *>(pvtinfo);
|
|
|
|
|
|
|
|
typedef void (*cxa_throw_fn)(void *, std::type_info *, void (*)(void *)) __attribute__((noreturn));
|
2013-03-07 19:44:39 +01:00
|
|
|
static cxa_throw_fn real_cxa_throw;
|
|
|
|
|
2014-04-14 03:02:33 +02:00
|
|
|
#ifndef __GLIBCXX__
|
|
|
|
l_LastExceptionObj.reset(new void *(obj));
|
2014-04-22 07:13:16 +02:00
|
|
|
l_LastExceptionPvtInfo.reset(new TYPEINFO_TYPE *(pvtinfo));
|
2014-04-14 03:02:33 +02:00
|
|
|
l_LastExceptionDest.reset(new DestCallback(dest));
|
|
|
|
#endif /* __GLIBCXX__ */
|
|
|
|
|
2013-03-07 19:44:39 +01:00
|
|
|
if (real_cxa_throw == 0)
|
|
|
|
real_cxa_throw = (cxa_throw_fn)dlsym(RTLD_NEXT, "__cxa_throw");
|
|
|
|
|
2014-03-31 18:30:57 +02:00
|
|
|
#ifdef __GLIBCXX__
|
2013-03-07 19:44:39 +01:00
|
|
|
void *thrown_ptr = obj;
|
2013-03-17 22:14:40 +01:00
|
|
|
const std::type_info *boost_exc = &typeid(boost::exception);
|
2014-03-22 08:38:46 +01:00
|
|
|
const std::type_info *user_exc = &typeid(user_error);
|
2013-03-07 19:44:39 +01:00
|
|
|
|
|
|
|
/* Check if the exception is a pointer type. */
|
|
|
|
if (tinfo->__is_pointer_p())
|
|
|
|
thrown_ptr = *(void **)thrown_ptr;
|
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (!user_exc->__do_catch(tinfo, &thrown_ptr, 1)) {
|
2014-03-31 02:08:15 +02:00
|
|
|
#endif /* __GLIBCXX__ */
|
2014-03-22 08:38:46 +01:00
|
|
|
StackTrace stack;
|
|
|
|
SetLastExceptionStack(stack);
|
2013-11-19 07:49:41 +01:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
ContextTrace context;
|
|
|
|
SetLastExceptionContext(context);
|
2013-03-08 14:41:01 +01:00
|
|
|
|
2014-03-31 02:08:15 +02:00
|
|
|
#ifdef __GLIBCXX__
|
2014-03-22 08:38:46 +01:00
|
|
|
/* Check if thrown_ptr inherits from boost::exception. */
|
|
|
|
if (boost_exc->__do_catch(tinfo, &thrown_ptr, 1)) {
|
|
|
|
boost::exception *ex = (boost::exception *)thrown_ptr;
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (boost::get_error_info<StackTraceErrorInfo>(*ex) == NULL)
|
|
|
|
*ex << StackTraceErrorInfo(stack);
|
2013-11-19 07:49:41 +01:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (boost::get_error_info<ContextTraceErrorInfo>(*ex) == NULL)
|
|
|
|
*ex << ContextTraceErrorInfo(context);
|
|
|
|
}
|
2013-03-07 19:44:39 +01:00
|
|
|
}
|
2014-03-31 02:08:15 +02:00
|
|
|
#endif /* __GLIBCXX__ */
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2014-04-20 14:09:01 +02:00
|
|
|
real_cxa_throw(obj, tinfo, dest);
|
2013-03-07 19:44:39 +01:00
|
|
|
}
|
2014-09-02 13:02:22 +02:00
|
|
|
#endif /* HAVE_CXXABI_H */
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
StackTrace *icinga::GetLastExceptionStack(void)
|
2013-03-07 19:44:39 +01:00
|
|
|
{
|
2013-11-19 07:49:41 +01:00
|
|
|
return l_LastExceptionStack.get();
|
2013-03-07 19:44:39 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
void icinga::SetLastExceptionStack(const StackTrace& trace)
|
2013-03-07 19:44:39 +01:00
|
|
|
{
|
2013-11-19 07:49:41 +01:00
|
|
|
l_LastExceptionStack.reset(new StackTrace(trace));
|
|
|
|
}
|
|
|
|
|
|
|
|
ContextTrace *icinga::GetLastExceptionContext(void)
|
|
|
|
{
|
|
|
|
return l_LastExceptionContext.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void icinga::SetLastExceptionContext(const ContextTrace& context)
|
|
|
|
{
|
|
|
|
l_LastExceptionContext.reset(new ContextTrace(context));
|
2013-11-20 21:55:14 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 15:29:04 +01:00
|
|
|
String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, StackTrace *stack, ContextTrace *context)
|
|
|
|
{
|
|
|
|
std::ostringstream result;
|
|
|
|
|
|
|
|
String message = ex.what();
|
|
|
|
|
2014-12-20 15:41:31 +01:00
|
|
|
if (message.IsEmpty())
|
2014-12-20 15:29:04 +01:00
|
|
|
result << boost::diagnostic_information(ex);
|
|
|
|
else
|
|
|
|
result << "Error: " << message;
|
|
|
|
|
|
|
|
const ScriptError *dex = dynamic_cast<const ScriptError *>(&ex);
|
|
|
|
|
|
|
|
if (dex && !dex->GetDebugInfo().Path.IsEmpty()) {
|
|
|
|
result << "\nLocation:\n";
|
|
|
|
ShowCodeFragment(result, dex->GetDebugInfo());
|
|
|
|
}
|
|
|
|
|
2015-01-12 12:36:49 +01:00
|
|
|
const user_error *uex = dynamic_cast<const user_error *>(&ex);
|
2014-12-20 15:29:04 +01:00
|
|
|
const posix_error *pex = dynamic_cast<const posix_error *>(&ex);
|
|
|
|
|
2015-01-12 12:36:49 +01:00
|
|
|
if (!uex && !pex && verbose) {
|
2014-12-20 15:29:04 +01:00
|
|
|
const StackTrace *st = boost::get_error_info<StackTraceErrorInfo>(ex);
|
|
|
|
|
|
|
|
if (st) {
|
|
|
|
result << *st;
|
|
|
|
} else {
|
|
|
|
result << std::endl;
|
|
|
|
|
|
|
|
if (!stack)
|
|
|
|
stack = GetLastExceptionStack();
|
|
|
|
|
|
|
|
if (stack)
|
|
|
|
result << *stack;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boost::get_error_info<ContextTraceErrorInfo>(ex) == NULL) {
|
|
|
|
result << std::endl;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
context = GetLastExceptionContext();
|
|
|
|
|
|
|
|
if (context)
|
|
|
|
result << *context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
String icinga::DiagnosticInformation(boost::exception_ptr eptr, bool verbose)
|
2013-11-20 21:55:14 +01:00
|
|
|
{
|
|
|
|
StackTrace *pt = GetLastExceptionStack();
|
2013-11-19 07:49:41 +01:00
|
|
|
StackTrace stack;
|
|
|
|
|
|
|
|
ContextTrace *pc = GetLastExceptionContext();
|
|
|
|
ContextTrace context;
|
2013-11-20 21:55:14 +01:00
|
|
|
|
|
|
|
if (pt)
|
2013-11-19 07:49:41 +01:00
|
|
|
stack = *pt;
|
|
|
|
|
|
|
|
if (pc)
|
|
|
|
context = *pc;
|
2013-11-20 21:55:14 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
boost::rethrow_exception(eptr);
|
|
|
|
} catch (const std::exception& ex) {
|
2014-12-20 15:29:04 +01:00
|
|
|
return DiagnosticInformation(ex, verbose, pt ? &stack : NULL, pc ? &context : NULL);
|
2013-11-20 21:55:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return boost::diagnostic_information(eptr);
|
2013-03-07 19:44:39 +01:00
|
|
|
}
|
2014-05-22 14:41:27 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
ScriptError::ScriptError(const String& message)
|
|
|
|
: m_Message(message)
|
|
|
|
{ }
|
|
|
|
|
2015-02-10 13:27:02 +01:00
|
|
|
ScriptError::ScriptError(const String& message, const DebugInfo& di, bool incompleteExpr)
|
|
|
|
: m_Message(message), m_DebugInfo(di), m_IncompleteExpr(incompleteExpr)
|
2014-12-18 15:11:57 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
ScriptError::~ScriptError(void) throw()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
const char *ScriptError::what(void) const throw()
|
|
|
|
{
|
|
|
|
return m_Message.CStr();
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugInfo ScriptError::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2015-02-10 13:27:02 +01:00
|
|
|
bool ScriptError::IsIncompleteExpression(void) const
|
|
|
|
{
|
|
|
|
return m_IncompleteExpr;;
|
|
|
|
}
|
|
|
|
|
2014-12-20 15:29:04 +01:00
|
|
|
posix_error::posix_error(void)
|
|
|
|
: m_Message(NULL)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
posix_error::~posix_error(void) throw()
|
|
|
|
{
|
|
|
|
free(m_Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *posix_error::what(void) const throw()
|
|
|
|
{
|
|
|
|
if (!m_Message) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
|
|
|
|
const char * const *func = boost::get_error_info<boost::errinfo_api_function>(*this);
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
msgbuf << "Function call '" << *func << "'";
|
|
|
|
else
|
|
|
|
msgbuf << "Function call";
|
|
|
|
|
|
|
|
const std::string *fname = boost::get_error_info<boost::errinfo_file_name>(*this);
|
|
|
|
|
|
|
|
if (fname)
|
|
|
|
msgbuf << " for file '" << *fname << "'";
|
|
|
|
|
|
|
|
msgbuf << " failed";
|
|
|
|
|
|
|
|
const int *errnum = boost::get_error_info<boost::errinfo_errno>(*this);
|
|
|
|
|
|
|
|
if (errnum)
|
|
|
|
msgbuf << " with error code " << *errnum << ", '" << strerror(*errnum) << "'";
|
|
|
|
|
|
|
|
String str = msgbuf.str();
|
|
|
|
m_Message = strdup(str.CStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_Message;
|
|
|
|
}
|
|
|
|
|