OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WSegmentationFault.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSEGMENTATIONFAULT_H
26 #define WSEGMENTATIONFAULT_H
27 
28 #ifdef __linux__
29 // This is highly platform dependent. Used for backtrace functionality.
30 #include <signal.h>
31 #endif // __linux__
32 
33 #include <string>
34 
35 #include <boost/lexical_cast.hpp>
36 
37 #include "WException.h"
38 #include "WExportCommon.h"
39 
40 #ifdef __linux__
41 // This is highly platform dependent. Used for backtrace functionality.
42 
43 /**
44  * Template class for every signal which can be handled.
45  */
46 template <class SignalExceptionClass> class SignalTranslator
47 {
48 public:
49  SignalTranslator()
50  {
51  static SingletonTranslator s_objTranslator;
52  }
53 
54 protected:
55 
56 private:
57  class SingletonTranslator
58  {
59  public: // NOLINT
60  SingletonTranslator()
61  {
62  signal( SignalExceptionClass::getSignalNumber(), SignalHandler );
63  }
64 
65  static void SignalHandler( int signum )
66  {
67  throw SignalExceptionClass( std::string( "SIGNAL: " ) +
68  boost::lexical_cast<std::string>( signum ) );
69  }
70  };
71 };
72 #endif // __linux__
73 
74 /**
75  * Base exception class for handling segmentation faults.
76  * It throws segmentation faults as exceptions. Please remember that SIGSEGV is not
77  * recoverable, which means it can NOT be catched!
78  * Also note that this will only work on Linux.
79  */
80 class OWCOMMON_EXPORT WSegmentationFault: public WException
81 {
82 public:
83 
84  /**
85  * Default constructor.
86  * \param msg name of the exception. mostly the default "Segmentation Fault"
87  */
88  explicit WSegmentationFault( const std::string& msg = "Segmentation Fault" );
89 
90  /**
91  * Destructor.
92  */
93  virtual ~WSegmentationFault() throw();
94 
95  /**
96  * Defines signal type to handle.
97  * @return The signal number.
98  */
99  static int getSignalNumber() throw();
100 
101  /**
102  * Installs this exception as signal handler for SIGSEGV.
103  * This will just work on Linux.
104  */
105  static void installSignalHandler() throw();
106 
107 protected:
108 
109 private:
110 };
111 
112 #endif // WSEGMENTATIONFAULT_H
113