Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/SignalHandler.h @ 7455

Last change on this file since 7455 was 7455, checked in by landauf, 14 years ago

added special implementation of abort() and _assert() for mingw which contain a break point.
also added displacement to the callstack

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
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
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @ingroup Util
32    @brief Declaration of the SignalHandler class.
33*/
34
35#ifndef _SignalHandler_H__
36#define _SignalHandler_H__
37
38#include "UtilPrereqs.h"
39
40#include <cassert>
41#include <string>
42#include "Singleton.h"
43#include "SpecialConfig.h"
44
45#if defined(ORXONOX_PLATFORM_LINUX)
46
47#include <list>
48#include <signal.h>
49
50namespace orxonox
51{
52    typedef int (*SignalCallback)( void * someData );
53
54    struct SignalRec
55    {
56        int signal;
57        sig_t handler;
58    };
59
60    struct SignalCallbackRec
61    {
62        SignalCallback cb;
63        void * someData;
64    };
65
66
67    typedef std::list<SignalRec> SignalRecList;
68    typedef std::list<SignalCallbackRec> SignalCallbackList;
69
70    /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile.
71    class SignalHandler : public Singleton<SignalHandler>
72    {
73        friend class Singleton<SignalHandler>;
74    public:
75        SignalHandler()  { }
76        ~SignalHandler() { }
77
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
102namespace 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 = NULL);
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
138namespace 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__ */
Note: See TracBrowser for help on using the repository browser.