Salome HOME
Porting KERNEL on new XML reader.
[modules/kernel.git] / src / CASCatch / CASCatch_CatchSignals.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "CASCatch_CatchSignals.hxx"
21
22 #include "CASCatch_Failure.hxx"  
23 #include "CASCatch_ErrorHandler.hxx"
24 #include <TCollection_AsciiString.hxx>
25
26 #define MAX_HANDLER_NUMBER 6
27
28
29 //================================================================================
30 /*! Public -
31  * \brief creates a CASCatch_CatchSignals
32  */
33 //================================================================================ 
34 CASCatch_CatchSignals::CASCatch_CatchSignals() 
35      :myIsActivated(Standard_False)
36 {
37
38   Standard_Integer i = 0;
39   for(; i<=MAX_HANDLER_NUMBER; i++)
40     mySigStates[i] = NULL;
41 }
42
43 #ifndef WNT
44
45 //================================ UNIX part ==================================================
46
47 #include <OSD.hxx>
48 #include <OSD_WhoAmI.hxx>
49 #include <OSD_SIGHUP.hxx>
50 #include <OSD_SIGINT.hxx>
51 #include <OSD_SIGQUIT.hxx>
52 #include <OSD_SIGILL.hxx>
53 #include <OSD_SIGKILL.hxx>
54 #include <OSD_SIGBUS.hxx>
55 #include <OSD_SIGSEGV.hxx>
56 #include <OSD_SIGSYS.hxx>
57 #ifndef LIN
58 #include <exception.h>
59 #endif
60
61 //==============================
62 typedef void (ACT_SIGIO_HANDLER)(void) ;
63
64 ACT_SIGIO_HANDLER *ADR_ACT_SIGIO_HANDLER = NULL ;
65
66 typedef void (* SIG_PFV) (int);
67
68 #ifdef SUN
69 # include <floatingpoint.h>
70 #endif
71
72 #ifdef SOLARIS
73 # include <floatingpoint.h>
74 # include <sys/machsig.h>
75 # include <stdlib.h>
76 # include <stdio.h>
77 #endif
78
79 #include <signal.h>
80 #include <sys/signal.h>
81
82 #ifdef LIN
83 # include <stdlib.h>
84 # include <stdio.h>
85 #else
86 # ifdef SA_SIGINFO 
87 #   ifndef AIX
88 # include <sys/siginfo.h>
89 #    endif
90 # endif
91 #endif
92
93
94 #ifdef IRIX
95 # include <sigfpe.h>
96 # include <sys/siginfo.h>
97 #endif 
98
99
100 //================================================================================
101 /*! Private -
102  * \brief universal handler for signals
103  */
104 //================================================================================ 
105 static void Handler(const OSD_Signals theSig, const OSD_Signals)
106 {
107   sigset_t set;
108   sigemptyset(&set);
109   sigaddset(&set, theSig);
110   sigprocmask(SIG_UNBLOCK, &set, NULL) ;
111
112   TCollection_AsciiString aMessage(theSig);  
113   aMessage+=" signal detected";
114  
115   CASCatch_Failure::Raise(aMessage.ToCString());
116 }
117
118
119 #ifdef SA_SIGINFO
120 //================================================================================
121 /*! Private -
122  * \brief  handler for SIGSEGV signal
123  */
124 //================================================================================ 
125 static void SegvHandler(const OSD_Signals, const Standard_Address, const Standard_Address)
126 {
127   sigset_t set;
128   sigemptyset(&set);
129   sigaddset(&set, SIGSEGV);
130   sigprocmask (SIG_UNBLOCK, &set, NULL); 
131
132   CASCatch_Failure::Raise("SIGSEGV detected");
133 }
134 #endif
135
136
137 //================================================================================
138 /*! Public -
139  * \brief activates a signals handling
140  */
141 //================================================================================ 
142 void CASCatch_CatchSignals::Activate() 
143 {  
144   if(myIsActivated) return;
145
146   struct sigaction act;
147
148   Standard_Integer i = 0;
149   for(; i<=MAX_HANDLER_NUMBER; i++)  
150     mySigStates[i] = new struct sigaction(); //Initialize structures
151
152   int stat;
153   act.sa_handler =  (SIG_PFV) &Handler ;
154   sigemptyset(&act.sa_mask) ;
155
156
157   stat = sigaction(SIGHUP,&act,(struct sigaction*)mySigStates[0]);    // ...... hangup
158   stat = sigaction(SIGFPE,&act,(struct sigaction*) mySigStates[1]);   // ...... floating point exception
159   stat = sigaction(SIGINT,&act,(struct sigaction*)mySigStates[2]);   // ...... interrupt
160   stat = sigaction(SIGQUIT,&act,(struct sigaction*)mySigStates[3]);  // ...... quit
161   stat = sigaction(SIGBUS,&act,(struct sigaction*)mySigStates[4]);   // ...... bus error
162   stat = sigaction(SIGILL,&act,(struct sigaction*)mySigStates[5]);   // ...... illegal instruction
163
164 #ifdef SA_RESTART
165   act.sa_flags   = SA_RESTART ;
166 #else
167   act.sa_flags   = 0 ;
168 #endif
169   act.sa_handler = (SIG_PFV) &SegvHandler ;
170
171 #ifdef SA_SIGINFO       // OSF,SOLARIS,IRIX
172   act.sa_flags = act.sa_flags | SA_SIGINFO ;
173 # ifdef SOLARIS
174   act.sa_sigaction = (void(*)(int, siginfo_t *, void*)) &SegvHandler ;
175 # endif
176 #endif
177
178   stat = sigaction( SIGSEGV , &act , (struct sigaction*)mySigStates[6]);    // ...... segmentation violation
179
180   myIsActivated = Standard_True;
181 }
182
183
184 //================================================================================
185 /*! Public -
186  * \brief deactivates a signals handling
187  */
188 //================================================================================
189 void CASCatch_CatchSignals::Deactivate() 
190 {
191   if(!myIsActivated) return;
192
193   struct sigaction oact;
194   int stat;
195
196   stat = sigaction(SIGHUP,(struct sigaction*)mySigStates[0],&oact);   // ...... hangup
197   stat = sigaction(SIGFPE,(struct sigaction*)mySigStates[1],&oact);   // ...... floating point exception
198   stat = sigaction(SIGINT,(struct sigaction*)mySigStates[2],&oact);   // ...... interrupt
199   stat = sigaction(SIGQUIT,(struct sigaction*)mySigStates[3],&oact);  // ...... quit
200   stat = sigaction(SIGBUS,(struct sigaction*)mySigStates[4],&oact);   // ...... bus error
201   stat = sigaction(SIGILL,(struct sigaction*)mySigStates[5],&oact);   // ...... illegal instruction
202   stat = sigaction(SIGSEGV,(struct sigaction*)mySigStates[6],&oact);  // ...... segmentation violation
203
204
205   Standard_Integer i = 0;
206   for(; i<=MAX_HANDLER_NUMBER; i++)
207     delete (struct sigaction*)mySigStates[i];
208
209   myIsActivated = Standard_False;
210 }
211
212
213
214 #else
215 //====================================== WNT part ====================================================
216 #include <OSD_WNT_1.hxx>
217
218 #include <process.h>
219 #include <signal.h>
220 #include <float.h>
221
222 #define _OSD_FPX ( _EM_DENORMAL | _EM_INEXACT | _EM_UNDERFLOW | _EM_ZERODIVIDE | _EM_OVERFLOW) //Mask these exceptions
223
224 //================================================================================
225 /*! Private -
226  * \brief handler for unexpected exceptions
227  */
228 //================================================================================
229 static Standard_Integer WntHandler(const Standard_Address theExceptionInfo) 
230 {
231   LPEXCEPTION_POINTERS lpXP = ( LPEXCEPTION_POINTERS )theExceptionInfo;
232   DWORD                dwExceptionCode = lpXP -> ExceptionRecord -> ExceptionCode;
233
234   TCollection_AsciiString aMessage((Standard_Integer)dwExceptionCode);  
235   aMessage+=" Exception code - unexpected exception";
236
237   CASCatch_Failure::Raise(aMessage.ToCString());
238
239   return EXCEPTION_EXECUTE_HANDLER;
240 }
241
242 void SIGWntHandler(int , int ) ;
243 static void (*SIGWNTHANDLER)(int) = ( void (*)(int) ) ( &SIGWntHandler ) ;
244
245
246 //================================================================================
247 /*! Private -
248  * \brief handler for signals
249  */
250 //================================================================================
251 static void SIGWntHandler(const int signum , const int theCode)
252 {
253
254   void (*OLDSIGWNTHANDLER)(int) ;  
255   switch( signum ) { 
256   case SIGFPE : 
257     _fpreset() ;
258     _clearfp() ; 
259     _controlfp ( _OSD_FPX, _MCW_EM ); 
260     OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER ); 
261
262     if(theCode == _FPE_UNDERFLOW || theCode == _FPE_INEXACT) return;
263     CASCatch_Failure::Raise ("Floating point error"); 
264     break;
265   case SIGSEGV : 
266     OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER );
267     CASCatch_Failure::Raise("Access violation"); 
268     break; 
269   case SIGILL : 
270     OLDSIGWNTHANDLER = signal( signum , SIGWNTHANDLER );
271     CASCatch_Failure::Raise("Illegal instruction" ); 
272     break; 
273   }
274 }
275
276
277 //================================================================================
278 /*! Public -
279  * \brief activates a signals handling
280  */
281 //================================================================================ 
282 void CASCatch_CatchSignals::Activate() 
283 {
284   if(myIsActivated) return;
285
286   mySigStates[0] = SetUnhandledExceptionFilter (( LPTOP_LEVEL_EXCEPTION_FILTER )&WntHandler);
287
288   myFloatOpWord = _controlfp(0, 0);
289   _controlfp ( _OSD_FPX, _MCW_EM );  //Enable floating point exceptions
290
291   mySigStates[1] = signal( SIGSEGV , SIGWNTHANDLER );
292   mySigStates[2] = signal( SIGFPE , SIGWNTHANDLER );
293   mySigStates[3] = signal( SIGILL , SIGWNTHANDLER );
294
295   myIsActivated = Standard_True;
296 }
297
298 //================================================================================
299 /*! Public -
300  * \brief deactivates a signals handling
301  */
302 //================================================================================
303 void CASCatch_CatchSignals::Deactivate() 
304 {
305   if(!myIsActivated) return;
306
307   SetUnhandledExceptionFilter (( LPTOP_LEVEL_EXCEPTION_FILTER )mySigStates[0]);
308
309   _controlfp ( myFloatOpWord, _MCW_EM );
310
311   signal( SIGSEGV ,  ( void (*)(int) )mySigStates[1]);
312   signal( SIGFPE , ( void (*)(int) )mySigStates[2]);
313   signal( SIGILL , ( void (*)(int) )mySigStates[3]);
314
315   Standard_Integer i = 0;
316   for(; i<=MAX_HANDLER_NUMBER; i++)
317     mySigStates[i] = NULL;
318   
319   myIsActivated = Standard_False;
320 }
321
322 #endif
323
324 //================================================================================
325 /*! Private -
326  * \brief deactivates a signals handling
327  */
328 //================================================================================
329 void CASCatch_CatchSignals::Destroy() 
330 {
331   if(myIsActivated) Deactivate();
332 }
333