| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | *                    > www.orxonox.net < | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   License notice: | 
|---|
| 7 | * | 
|---|
| 8 | *   This program is free software; you can redistribute it and/or | 
|---|
| 9 | *   modify it under the terms of the GNU General Public License | 
|---|
| 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | *   of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *   GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *   You should have received a copy of the GNU General Public License | 
|---|
| 19 | *   along with this program; if not, write to the Free Software | 
|---|
| 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 | * | 
|---|
| 22 | *   Author: | 
|---|
| 23 | *      Christoph Renner (Linux implementation) | 
|---|
| 24 | *      Fabian 'x3n' Landau (Windows implementation) | 
|---|
| 25 | *   Co-authors: | 
|---|
| 26 | *      ... | 
|---|
| 27 | * | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | /** | 
|---|
| 31 | @file | 
|---|
| 32 | @ingroup Util | 
|---|
| 33 | @brief Declaration of the SignalHandler class. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #ifndef _SignalHandler_H__ | 
|---|
| 37 | #define _SignalHandler_H__ | 
|---|
| 38 |  | 
|---|
| 39 | #include "UtilPrereqs.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include <cassert> | 
|---|
| 42 | #include <string> | 
|---|
| 43 |  | 
|---|
| 44 | #include "Singleton.h" | 
|---|
| 45 | #include "SpecialConfig.h" | 
|---|
| 46 |  | 
|---|
| 47 | #if defined(ORXONOX_PLATFORM_LINUX) | 
|---|
| 48 |  | 
|---|
| 49 | #include <list> | 
|---|
| 50 | #include <signal.h> | 
|---|
| 51 |  | 
|---|
| 52 | namespace orxonox | 
|---|
| 53 | { | 
|---|
| 54 | typedef int (*SignalCallback)( void * someData ); | 
|---|
| 55 |  | 
|---|
| 56 | struct SignalRec | 
|---|
| 57 | { | 
|---|
| 58 | int signal; | 
|---|
| 59 | sig_t handler; | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 | struct SignalCallbackRec | 
|---|
| 63 | { | 
|---|
| 64 | SignalCallback cb; | 
|---|
| 65 | void * someData; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | typedef std::list<SignalRec> SignalRecList; | 
|---|
| 70 | typedef std::list<SignalCallbackRec> SignalCallbackList; | 
|---|
| 71 |  | 
|---|
| 72 | /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile. | 
|---|
| 73 | class _UtilExport SignalHandler : public Singleton<SignalHandler> | 
|---|
| 74 | { | 
|---|
| 75 | friend class Singleton<SignalHandler>; | 
|---|
| 76 |  | 
|---|
| 77 | public: | 
|---|
| 78 | void registerCallback( SignalCallback cb, void * someData ); | 
|---|
| 79 |  | 
|---|
| 80 | void doCatch( const std::string & appName, const std::string & filename ); | 
|---|
| 81 | void dontCatch(); | 
|---|
| 82 |  | 
|---|
| 83 | private: | 
|---|
| 84 | static void sigHandler( int sig ); | 
|---|
| 85 |  | 
|---|
| 86 | void catchSignal( int sig ); | 
|---|
| 87 | SignalRecList sigRecList; | 
|---|
| 88 |  | 
|---|
| 89 | SignalCallbackList callbackList; | 
|---|
| 90 |  | 
|---|
| 91 | static SignalHandler* singletonPtr_s; | 
|---|
| 92 |  | 
|---|
| 93 | std::string appName; | 
|---|
| 94 | std::string filename; | 
|---|
| 95 | }; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | #elif defined(ORXONOX_PLATFORM_WINDOWS) && defined(DBGHELP_FOUND) | 
|---|
| 99 |  | 
|---|
| 100 | #include <windows.h> | 
|---|
| 101 |  | 
|---|
| 102 | namespace orxonox | 
|---|
| 103 | { | 
|---|
| 104 | /// The SignalHandler is used to catch unhandled exceptions like access violations and write a backtrace to the logfile. | 
|---|
| 105 | class _UtilExport SignalHandler : public Singleton<SignalHandler> | 
|---|
| 106 | { | 
|---|
| 107 | friend class Singleton<SignalHandler>; | 
|---|
| 108 |  | 
|---|
| 109 | public: | 
|---|
| 110 | SignalHandler(); | 
|---|
| 111 | ~SignalHandler(); | 
|---|
| 112 |  | 
|---|
| 113 | void doCatch(const std::string& appName, const std::string& filename); | 
|---|
| 114 |  | 
|---|
| 115 | static std::string getStackTrace(PEXCEPTION_POINTERS pExceptionInfo = nullptr); | 
|---|
| 116 | static std::string getExceptionType(PEXCEPTION_POINTERS pExceptionInfo); | 
|---|
| 117 |  | 
|---|
| 118 | private: | 
|---|
| 119 | static LONG WINAPI exceptionFilter(PEXCEPTION_POINTERS pExceptionInfo); | 
|---|
| 120 |  | 
|---|
| 121 | static std::string getModuleName(const std::string& path); | 
|---|
| 122 | static DWORD getModuleBase(DWORD dwAddress); | 
|---|
| 123 |  | 
|---|
| 124 | template <typename T> | 
|---|
| 125 | static std::string pointerToString(T pointer, bool bFillZeros = true); | 
|---|
| 126 | template <typename T> | 
|---|
| 127 | static std::string pointerToString(T* pointer); | 
|---|
| 128 |  | 
|---|
| 129 | static SignalHandler* singletonPtr_s; | 
|---|
| 130 |  | 
|---|
| 131 | std::string filename_; | 
|---|
| 132 | LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter_; | 
|---|
| 133 | }; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | #else | 
|---|
| 137 |  | 
|---|
| 138 | namespace orxonox | 
|---|
| 139 | { | 
|---|
| 140 | /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile. Not implemented on systems except Linux and Windows. | 
|---|
| 141 | class _UtilExport SignalHandler : public Singleton<SignalHandler> | 
|---|
| 142 | { | 
|---|
| 143 | friend class Singleton<SignalHandler>; | 
|---|
| 144 |  | 
|---|
| 145 | public: | 
|---|
| 146 | void doCatch(const std::string& appName, const std::string& filename) {} | 
|---|
| 147 |  | 
|---|
| 148 | private: | 
|---|
| 149 | static SignalHandler* singletonPtr_s; | 
|---|
| 150 | }; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | #endif | 
|---|
| 154 |  | 
|---|
| 155 | #endif /* _SignalHandler_H__ */ | 
|---|