OpenWalnut  1.4.0
WSegmentationFault.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSEGMENTATIONFAULT_H
00026 #define WSEGMENTATIONFAULT_H
00027 
00028 #ifdef __linux__
00029 // This is highly platform dependent. Used for backtrace functionality.
00030 #include <signal.h>
00031 #endif  // __linux__
00032 
00033 #include <string>
00034 
00035 #include "WStringUtils.h"
00036 #include "WException.h"
00037 
00038 
00039 #ifdef __linux__
00040 // This is highly platform dependent. Used for backtrace functionality.
00041 
00042 /**
00043  * Template class for every signal which can be handled.
00044  */
00045 template <class SignalExceptionClass> class SignalTranslator
00046 {
00047 public:
00048     SignalTranslator()
00049     {
00050         static SingletonTranslator s_objTranslator;
00051     }
00052 
00053 protected:
00054 private:
00055     class SingletonTranslator
00056     {
00057     public:  // NOLINT
00058         SingletonTranslator()
00059         {
00060             signal( SignalExceptionClass::getSignalNumber(), SignalHandler );
00061         }
00062 
00063         static void SignalHandler( int signum )
00064         {
00065             throw SignalExceptionClass( std::string( "SIGNAL: " ) +
00066                                         string_utils::toString( signum ) );
00067         }
00068     };
00069 };
00070 #endif  // __linux__
00071 
00072 /**
00073  * Base exception class for handling segmentation faults.
00074  * It throws segmentation faults as exceptions. Please remember that SIGSEGV is not
00075  * recoverable, which means it can NOT be catched!
00076  * Also note that this will only work on Linux.
00077  */
00078 class WSegmentationFault: public WException
00079 {
00080 public:
00081     /**
00082      * Default constructor.
00083      * \param msg name of the exception. mostly the default "Segmentation Fault"
00084      */
00085     explicit WSegmentationFault( const std::string& msg = "Segmentation Fault" );
00086 
00087     /**
00088      * Destructor.
00089      */
00090     virtual ~WSegmentationFault() throw();
00091 
00092     /**
00093      * Defines signal type to handle.
00094      * @return The signal number.
00095      */
00096     static int getSignalNumber() throw();
00097 
00098     /**
00099      * Installs this exception as signal handler for SIGSEGV.
00100      * This will just work on Linux.
00101      */
00102     static void installSignalHandler() throw();
00103 
00104 protected:
00105 private:
00106 };
00107 
00108 #endif  // WSEGMENTATIONFAULT_H
00109