1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : CASCatch_CatchSignals.cxx
24 // Author : Sergey RUIN, Open CASCADE S.A.S (sergey.ruin@opencascade.com)
26 #include "CASCatch_CatchSignals.hxx"
27 #include "CASCatch_Failure.hxx"
28 #include "CASCatch_ErrorHandler.hxx"
30 #include <TCollection_AsciiString.hxx>
32 #define MAX_HANDLER_NUMBER 6
34 //================================================================================
36 * \brief creates a CASCatch_CatchSignals
38 //================================================================================
39 CASCatch_CatchSignals::CASCatch_CatchSignals()
40 : myIsActivated( Standard_False )
42 for ( Standard_Integer i = 0; i <= MAX_HANDLER_NUMBER; i++ )
43 mySigStates[i] = NULL;
48 //================================ UNIX part ==================================================
51 #include <OSD_WhoAmI.hxx>
52 #include <OSD_SIGHUP.hxx>
53 #include <OSD_SIGINT.hxx>
54 #include <OSD_SIGQUIT.hxx>
55 #include <OSD_SIGILL.hxx>
56 #include <OSD_SIGKILL.hxx>
57 #include <OSD_SIGBUS.hxx>
58 #include <OSD_SIGSEGV.hxx>
59 #include <OSD_SIGSYS.hxx>
61 typedef void (ACT_SIGIO_HANDLER)(void) ;
62 ACT_SIGIO_HANDLER *ADR_ACT_SIGIO_HANDLER = NULL ;
64 typedef void (* SIG_PFV) (int);
67 # include <floatingpoint.h>
71 # include <floatingpoint.h>
72 # include <sys/machsig.h>
78 #include <sys/signal.h>
85 # include <sys/siginfo.h>
89 //================================================================================
91 * \brief universal handler for signals
93 //================================================================================
94 static void Handler(const int theSig)
98 sigaddset(&set, theSig);
99 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
101 TCollection_AsciiString aMessage(theSig);
102 aMessage+=" signal detected";
104 CASCatch_Failure::Raise(aMessage.ToCString());
109 //================================================================================
111 * \brief handler for SIGSEGV signal
113 //================================================================================
114 static void SegvHandler(const int, siginfo_t*, const Standard_Address)
118 sigaddset(&set, SIGSEGV);
119 sigprocmask (SIG_UNBLOCK, &set, NULL);
121 CASCatch_Failure::Raise("SIGSEGV detected");
126 //================================================================================
128 * \brief activates a signals handling
130 //================================================================================
131 void CASCatch_CatchSignals::Activate()
133 if(myIsActivated) return;
135 struct sigaction act;
137 Standard_Integer i = 0;
138 for(; i<=MAX_HANDLER_NUMBER; i++)
139 mySigStates[i] = new struct sigaction(); //Initialize structures
142 act.sa_handler = (SIG_PFV) &Handler ;
143 sigemptyset(&act.sa_mask) ;
145 stat = sigaction(SIGHUP,&act,(struct sigaction*)mySigStates[0]); // ...... hangup
146 stat = sigaction(SIGFPE,&act,(struct sigaction*) mySigStates[1]); // ...... floating point exception
147 stat = sigaction(SIGINT,&act,(struct sigaction*)mySigStates[2]); // ...... interrupt
148 stat = sigaction(SIGQUIT,&act,(struct sigaction*)mySigStates[3]); // ...... quit
149 stat = sigaction(SIGBUS,&act,(struct sigaction*)mySigStates[4]); // ...... bus error
150 stat = sigaction(SIGILL,&act,(struct sigaction*)mySigStates[5]); // ...... illegal instruction
153 act.sa_flags = SA_RESTART ;
157 act.sa_handler = (SIG_PFV) &SegvHandler ;
159 #ifdef SA_SIGINFO // OSF,SOLARIS,IRIX
160 act.sa_flags = act.sa_flags | SA_SIGINFO ;
162 act.sa_sigaction = (void(*)(int, siginfo_t *, void*)) &SegvHandler ;
166 stat = sigaction( SIGSEGV , &act , (struct sigaction*)mySigStates[6]); // ...... segmentation violation
168 myIsActivated = Standard_True;
172 //================================================================================
174 * \brief deactivates a signals handling
176 //================================================================================
177 void CASCatch_CatchSignals::Deactivate()
179 if(!myIsActivated) return;
181 struct sigaction oact;
184 stat = sigaction(SIGHUP,(struct sigaction*)mySigStates[0],&oact); // ...... hangup
185 stat = sigaction(SIGFPE,(struct sigaction*)mySigStates[1],&oact); // ...... floating point exception
186 stat = sigaction(SIGINT,(struct sigaction*)mySigStates[2],&oact); // ...... interrupt
187 stat = sigaction(SIGQUIT,(struct sigaction*)mySigStates[3],&oact); // ...... quit
188 stat = sigaction(SIGBUS,(struct sigaction*)mySigStates[4],&oact); // ...... bus error
189 stat = sigaction(SIGILL,(struct sigaction*)mySigStates[5],&oact); // ...... illegal instruction
190 stat = sigaction(SIGSEGV,(struct sigaction*)mySigStates[6],&oact); // ...... segmentation violation
193 Standard_Integer i = 0;
194 for(; i<=MAX_HANDLER_NUMBER; i++)
195 delete (struct sigaction*)mySigStates[i];
197 myIsActivated = Standard_False;
203 //====================================== WIN32 part ====================================================
204 #include <OSD_WNT_1.hxx>
210 #define _OSD_FPX ( _EM_DENORMAL | _EM_INEXACT | _EM_UNDERFLOW | _EM_ZERODIVIDE | _EM_OVERFLOW) //Mask these exceptions
212 //================================================================================
214 * \brief handler for unexpected exceptions
216 //================================================================================
217 static Standard_Integer WntHandler(const Standard_Address theExceptionInfo)
219 LPEXCEPTION_POINTERS lpXP = ( LPEXCEPTION_POINTERS )theExceptionInfo;
220 DWORD dwExceptionCode = lpXP -> ExceptionRecord -> ExceptionCode;
222 TCollection_AsciiString aMessage((Standard_Integer)dwExceptionCode);
223 aMessage+=" Exception code - unexpected exception";
225 CASCatch_Failure::Raise(aMessage.ToCString());
227 return EXCEPTION_EXECUTE_HANDLER;
230 void SIGWntHandler(int , int ) ;
231 static void (*SIGWNTHANDLER)(int) = ( void (*)(int) ) ( &SIGWntHandler ) ;
234 //================================================================================
236 * \brief handler for signals
238 //================================================================================
239 static void SIGWntHandler(const int signum , const int theCode)
242 void (*OLDSIGWNTHANDLER)(int) ;
247 _controlfp ( _OSD_FPX, _MCW_EM );
248 OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER );
250 if(theCode == _FPE_UNDERFLOW || theCode == _FPE_INEXACT) return;
251 CASCatch_Failure::Raise ("Floating point error");
254 OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER );
255 CASCatch_Failure::Raise("Access violation");
258 OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER );
259 CASCatch_Failure::Raise("Illegal instruction" );
265 //================================================================================
267 * \brief activates a signals handling
269 //================================================================================
270 void CASCatch_CatchSignals::Activate()
272 if(myIsActivated) return;
274 mySigStates[0] = SetUnhandledExceptionFilter (( LPTOP_LEVEL_EXCEPTION_FILTER )&WntHandler);
276 myFloatOpWord = _controlfp(0, 0);
277 _controlfp ( _OSD_FPX, _MCW_EM ); //Enable floating point exceptions
279 mySigStates[1] = signal( SIGSEGV , SIGWNTHANDLER );
280 mySigStates[2] = signal( SIGFPE , SIGWNTHANDLER );
281 mySigStates[3] = signal( SIGILL , SIGWNTHANDLER );
283 myIsActivated = Standard_True;
286 //================================================================================
288 * \brief deactivates a signals handling
290 //================================================================================
291 void CASCatch_CatchSignals::Deactivate()
293 if(!myIsActivated) return;
295 SetUnhandledExceptionFilter (( LPTOP_LEVEL_EXCEPTION_FILTER )mySigStates[0]);
297 _controlfp ( myFloatOpWord, _MCW_EM );
299 signal( SIGSEGV , ( void (*)(int) )mySigStates[1]);
300 signal( SIGFPE , ( void (*)(int) )mySigStates[2]);
301 signal( SIGILL , ( void (*)(int) )mySigStates[3]);
303 Standard_Integer i = 0;
304 for(; i<=MAX_HANDLER_NUMBER; i++)
305 mySigStates[i] = NULL;
307 myIsActivated = Standard_False;
312 //================================================================================
314 * \brief deactivates a signals handling
316 //================================================================================
317 void CASCatch_CatchSignals::Destroy()
319 if(myIsActivated) Deactivate();