2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 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
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/exception.h"
|
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
|
|
|
|
2013-03-15 13:44:45 +01:00
|
|
|
#ifndef _WIN32
|
2013-03-07 19:44:39 +01:00
|
|
|
extern "C"
|
|
|
|
void __cxa_throw(void *obj, void *pvtinfo, void (*dest)(void *))
|
|
|
|
{
|
|
|
|
typedef void (*cxa_throw_fn)(void *, void *, void (*) (void *)) __attribute__((noreturn));
|
|
|
|
static cxa_throw_fn real_cxa_throw;
|
|
|
|
|
|
|
|
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 *tinfo = static_cast<std::type_info *>(pvtinfo);
|
|
|
|
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
|
|
|
|
|
|
|
real_cxa_throw(obj, pvtinfo, dest);
|
|
|
|
}
|
2013-03-15 13:44:45 +01:00
|
|
|
#endif /* _WIN32 */
|
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
|
|
|
}
|
|
|
|
|
|
|
|
String icinga::DiagnosticInformation(boost::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
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) {
|
2013-11-19 07:49:41 +01:00
|
|
|
return DiagnosticInformation(ex, 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
|
|
|
}
|