Commit c472ba28 authored by ebaum's avatar ebaum
Browse files

[ADD]

- added segmentation fault handler
parent 12287e22
......@@ -26,6 +26,9 @@
#include "gui/qt4/WMainApplication.h"
#include "utils/WOptionHandler.h"
#include "common/WException.h"
#include "common/WSegmentationFault.h"
/**
* The main routine starting up the whole application.
*
......@@ -43,6 +46,10 @@ int main( int argc, char* argv[] )
"along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>." << std::endl;
std::cout << std::endl; // Create new line after message for clarity.
// install signal handler as early as possible
WSegmentationFault::installSignalHandler();
// start application
WOptionHandler h( argc, argv );
return h.takeActions();
}
......@@ -40,7 +40,7 @@
WException::WException( const std::string& msg ): exception()
{
// initialize members
m_Msg = msg;
m_msg = msg;
// print stacktrace and message
std::cerr << "Exception thrown! Backtrace:" << std::endl << getBacktrace() << std::endl;
......@@ -55,7 +55,7 @@ WException::~WException() throw()
const char* WException::what() const throw()
{
// return it
return m_Msg.c_str();
return m_msg.c_str();
}
std::string WException::getTrace() const
......
......@@ -28,7 +28,6 @@
#include <list>
#include <string>
/**
* \par Description:
* Basic exception handler.
......@@ -80,7 +79,7 @@ protected:
/**
* Message given during throw.
*/
std::string m_Msg;
std::string m_msg;
/**
* Stack trace for identifying the source where this exception came from.
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WSegmentationFault.h"
WSegmentationFault::WSegmentationFault( const std::string& msg )
{
// initialize members
// will the user ever have the chance to set msg?
m_msg="Segmentation Fault.";
}
WSegmentationFault::~WSegmentationFault() throw()
{
// cleanup
}
int WSegmentationFault::getSignalNumber() throw()
{
#ifdef __linux__
return SIGSEGV;
#else
return 0;
#endif // __linux__
}
void WSegmentationFault::installSignalHandler() throw()
{
#ifdef __linux__
SignalTranslator<WSegmentationFault> segmentationFaultTranslator;
#endif // __linux__
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WSEGMENTATIONFAULT_H
#define WSEGMENTATIONFAULT_H
#ifdef __linux__
// This is highly platform dependent. Used for backtrace functionality.
#include <signal.h>
#endif // __linux__
#include <string>
#include "WException.h"
#ifdef __linux__
// This is highly platform dependent. Used for backtrace functionality.
/**
* \par Description
* Template class for every signal which can be handled.
*/
template <class SignalExceptionClass> class SignalTranslator
{
public:
SignalTranslator()
{
static SingleTonTranslator s_objTranslator;
}
protected:
private:
class SingleTonTranslator
{
public: // NOLINT
SingleTonTranslator()
{
signal( SignalExceptionClass::getSignalNumber(), SignalHandler );
}
static void SignalHandler( int signum )
{
throw SignalExceptionClass();
}
};
};
#endif // __linux__
/**
* \par Description:
* Base exception class for handling segmentation faults.
* It throws segmentation faults as exceptions. Please remember that SIGSEGV is not
* recoverable, which means it can NOT be catched!
* Also note that this will only work on Linux.
*/
class WSegmentationFault: public WException
{
public:
/**
* \par Description
* Default constructor.
*/
explicit WSegmentationFault( const std::string& msg = std::string() );
/**
* \par Description
* Destructor.
*/
virtual ~WSegmentationFault() throw();
/**
* \par Description
* Defines signal type to handle.
* @return The signal number.
*/
static int getSignalNumber() throw();
/**
* \par Description
* Installs this exception as signal handler for SIGSEGV.
* This will just work on Linux.
*/
static void installSignalHandler() throw();
protected:
private:
};
#endif // WSEGMENTATIONFAULT_H
......@@ -79,9 +79,6 @@ public:
std::string bt = e.getBacktrace();
// how to test this? Since the trace is different in release and debug mode, we simply test for
// non empty string here.
// TODO(ebaum): better test! (maybe use find to search for some texts like CxxTest::TestRunner or
// something similar which only will work in debug mode)
TS_ASSERT( bt.length() );
}
#endif // __linux__
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment