OpenWalnut  1.5.0dev
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 "WStringUtils.h"
36 #include "WException.h"
37 
38 
39 #ifdef __linux__
40 // This is highly platform dependent. Used for backtrace functionality.
41 
42 /**
43  * Template class for every signal which can be handled.
44  */
45 template <class SignalExceptionClass> class SignalTranslator
46 {
47 public:
48  SignalTranslator()
49  {
50  static SingletonTranslator s_objTranslator;
51  }
52 
53 protected:
54 private:
55  class SingletonTranslator
56  {
57  public: // NOLINT
58  SingletonTranslator()
59  {
60  signal( SignalExceptionClass::getSignalNumber(), SignalHandler );
61  }
62 
63  static void SignalHandler( int signum )
64  {
65  throw SignalExceptionClass( std::string( "SIGNAL: " ) +
66  string_utils::toString( signum ) );
67  }
68  };
69 };
70 #endif // __linux__
71 
72 /**
73  * Base exception class for handling segmentation faults.
74  * It throws segmentation faults as exceptions. Please remember that SIGSEGV is not
75  * recoverable, which means it can NOT be catched!
76  * Also note that this will only work on Linux.
77  */
79 {
80 public:
81  /**
82  * Default constructor.
83  * \param msg name of the exception. mostly the default "Segmentation Fault"
84  */
85  explicit WSegmentationFault( const std::string& msg = "Segmentation Fault" );
86 
87  /**
88  * Destructor.
89  */
90  virtual ~WSegmentationFault() throw();
91 
92  /**
93  * Defines signal type to handle.
94  * @return The signal number.
95  */
96  static int getSignalNumber() throw();
97 
98  /**
99  * Installs this exception as signal handler for SIGSEGV.
100  * This will just work on Linux.
101  */
102  static void installSignalHandler() throw();
103 
104 protected:
105 private:
106 };
107 
108 #endif // WSEGMENTATIONFAULT_H
109 
Basic exception handler.
Definition: WException.h:39
Base exception class for handling segmentation faults.
static void installSignalHandler()
Installs this exception as signal handler for SIGSEGV.
WSegmentationFault(const std::string &msg="Segmentation Fault")
Default constructor.
static int getSignalNumber()
Defines signal type to handle.
virtual ~WSegmentationFault()
Destructor.
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120