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 <boost/lexical_cast.hpp> 00036 00037 #include "WException.h" 00038 #include "WExportCommon.h" 00039 00040 #ifdef __linux__ 00041 // This is highly platform dependent. Used for backtrace functionality. 00042 00043 /** 00044 * Template class for every signal which can be handled. 00045 */ 00046 template <class SignalExceptionClass> class SignalTranslator 00047 { 00048 public: 00049 SignalTranslator() 00050 { 00051 static SingletonTranslator s_objTranslator; 00052 } 00053 00054 protected: 00055 00056 private: 00057 class SingletonTranslator 00058 { 00059 public: // NOLINT 00060 SingletonTranslator() 00061 { 00062 signal( SignalExceptionClass::getSignalNumber(), SignalHandler ); 00063 } 00064 00065 static void SignalHandler( int signum ) 00066 { 00067 throw SignalExceptionClass( std::string( "SIGNAL: " ) + 00068 boost::lexical_cast<std::string>( signum ) ); 00069 } 00070 }; 00071 }; 00072 #endif // __linux__ 00073 00074 /** 00075 * Base exception class for handling segmentation faults. 00076 * It throws segmentation faults as exceptions. Please remember that SIGSEGV is not 00077 * recoverable, which means it can NOT be catched! 00078 * Also note that this will only work on Linux. 00079 */ 00080 class OWCOMMON_EXPORT WSegmentationFault: public WException 00081 { 00082 public: 00083 00084 /** 00085 * Default constructor. 00086 * \param msg name of the exception. mostly the default "Segmentation Fault" 00087 */ 00088 explicit WSegmentationFault( const std::string& msg = "Segmentation Fault" ); 00089 00090 /** 00091 * Destructor. 00092 */ 00093 virtual ~WSegmentationFault() throw(); 00094 00095 /** 00096 * Defines signal type to handle. 00097 * @return The signal number. 00098 */ 00099 static int getSignalNumber() throw(); 00100 00101 /** 00102 * Installs this exception as signal handler for SIGSEGV. 00103 * This will just work on Linux. 00104 */ 00105 static void installSignalHandler() throw(); 00106 00107 protected: 00108 00109 private: 00110 }; 00111 00112 #endif // WSEGMENTATIONFAULT_H 00113