1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
19 //---------- All Systems except Windows NT : ----------------------------------
23 #include <OSD_WhoAmI.hxx>
24 #include <OSD_SIGHUP.hxx>
25 #include <OSD_SIGINT.hxx>
26 #include <OSD_SIGQUIT.hxx>
27 #include <OSD_SIGILL.hxx>
28 #include <OSD_SIGKILL.hxx>
29 #include <OSD_SIGBUS.hxx>
30 #include <OSD_SIGSEGV.hxx>
31 #include <OSD_SIGSYS.hxx>
32 #include <OSD_Exception_CTRL_BREAK.hxx>
33 #include <Standard_NumericError.hxx>
34 #include <Standard_DivideByZero.hxx>
35 #include <Standard_Overflow.hxx>
37 #include <Standard_ErrorHandler.hxx>
44 static Standard_Boolean fFltExceptions = Standard_False;
47 // variable signalling that Control-C has been pressed (SIGINT signal)
48 static Standard_Boolean fCtrlBrk;
50 //const OSD_WhoAmI Iam = OSD_WPackage;
52 typedef void (ACT_SIGIO_HANDLER)(void) ;
53 ACT_SIGIO_HANDLER *ADR_ACT_SIGIO_HANDLER = NULL ;
56 typedef void (* SIG_PFV) (int);
65 # include <sys/siginfo.h>
69 typedef void (* SIG_PFV) (int);
73 #if !defined(__ANDROID__) && !defined(__QNX__)
74 #include <sys/signal.h>
77 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
78 //============================================================================
79 //==== GetOldSigAction
81 //============================================================================
83 static struct sigaction *GetOldSigAction()
85 static struct sigaction oldSignals[NSIG];
90 static sigfpe_handler_type *GetOldFPE()
92 static sigfpe_handler_type aIEEEHandler[5] = { NULL, NULL, NULL, NULL, NULL } ;
98 //============================================================================
100 //==== Catche the differents signals:
101 //==== 1- The Fatal signals, which cause the end of process:
102 //==== 2- The exceptions which are "signaled" by Raise.
103 //==== The Fatal Signals:
104 //==== SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGKILL, SIGBUS, SIGSYS
105 //==== The Exceptions:
107 //==== (SUN versions)
108 //==== FPE_INTOVF_TRAP // ..... integer overflow
109 //==== FPE_INTDIV_TRAP // ..... integer divide by zero
110 //==== FPE_FLTINEX_TRAP // ..... [floating inexact result]
111 //==== FPE_FLTDIV_TRAP // ..... [floating divide by zero]
112 //==== FPE_FLTUND_TRAP // ..... [floating underflow]
113 //==== FPE_FLTOPER_TRAP // ..... [floating inexact result]
114 //==== FPE_FLTOVF_TRAP // ..... [floating overflow]
115 //==== SIGSEGV is handled by "SegvHandler()"
116 //============================================================================
118 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
119 static void Handler (const int theSignal, siginfo_t *theSigInfo, const Standard_Address theContext)
121 static void Handler (const int theSignal, siginfo_t */*theSigInfo*/, const Standard_Address /*theContext*/)
124 static void Handler (const int theSignal)
127 struct sigaction oldact, act;
128 // re-install the signal
129 if ( ! sigaction (theSignal, NULL, &oldact) ) {
130 // cout << " signal is " << theSignal << " handler is " << oldact.sa_handler << endl;
131 if (sigaction (theSignal, &oldact, &act)) perror ("sigaction");
134 perror ("sigaction");
137 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
138 if (pthread_self() != getOCCThread() || !Standard_ErrorHandler::IsInTryBlock()) {
139 // use the previous signal handler
140 // cout << "OSD::Handler: signal " << (int) theSignal << " occured outside a try block " << endl ;
141 struct sigaction *oldSignals = GetOldSigAction();
142 struct sigaction asigacthandler = oldSignals[theSignal >= 0 && theSignal < NSIG ? theSignal : 0];
144 if (asigacthandler.sa_flags & SA_SIGINFO) {
145 void (*aCurInfoHandle)(int, siginfo_t *, void *) = asigacthandler.sa_sigaction;
147 siginfo_t * aSigInfo = NULL;
149 aSigInfo = theSigInfo;
153 switch (aSigInfo->si_signo) {
157 sigfpe_handler_type *aIEEEHandlerTab = GetOldFPE();
158 sigfpe_handler_type aIEEEHandler = NULL;
159 switch (aSigInfo->si_code) {
160 case FPE_INTDIV_TRAP :
161 case FPE_FLTDIV_TRAP :
162 aIEEEHandler = aIEEEHandlerTab[1];
164 case FPE_INTOVF_TRAP :
165 case FPE_FLTOVF_TRAP :
166 aIEEEHandler = aIEEEHandlerTab[2];
168 case FPE_FLTUND_TRAP :
169 aIEEEHandler = aIEEEHandlerTab[4];
171 case FPE_FLTRES_TRAP :
172 aIEEEHandler = aIEEEHandlerTab[3];
174 case FPE_FLTINV_TRAP :
175 aIEEEHandler = aIEEEHandlerTab[0];
177 case FPE_FLTSUB_TRAP :
182 // cout << "OSD::Handler: calling previous IEEE signal handler with info" << endl ;
183 (*aIEEEHandler) (theSignal, aSigInfo, theContext);
193 if (aCurInfoHandle) {
194 // cout << "OSD::Handler: calling previous signal handler with info " << aCurInfoHandle << endl ;
195 (*aCurInfoHandle) (theSignal, aSigInfo, theContext);
197 cerr << " previous signal handler return" << endl ;
202 // cout << "OSD::Handler: no handler with info for the signal" << endl;
206 // no siginfo needed for the signal
207 void (*aCurHandler) (int) = asigacthandler.sa_handler;
209 // cout << "OSD::Handler: calling previous signal handler" << endl ;
210 (*aCurHandler) (theSignal);
212 cerr << " previous signal handler return" << endl ;
217 // cout << " Signal occured outside a try block, but no handler for it" <<endl;
222 // cout << "OSD::Handler: signal " << (int) theSignal << " occured inside a try block " << endl ;
223 if ( ADR_ACT_SIGIO_HANDLER != NULL )
224 (*ADR_ACT_SIGIO_HANDLER)() ;
227 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
228 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
234 OSD_SIGHUP::NewInstance("SIGHUP 'hangup' detected.")->Jump();
238 // For safe handling of Control-C as stop event, arm a variable but do not
239 // generate longjump (we are out of context anyway)
240 fCtrlBrk = Standard_True;
241 // OSD_SIGINT::NewInstance("SIGINT 'interrupt' detected.")->Jump();
245 OSD_SIGQUIT::NewInstance("SIGQUIT 'quit' detected.")->Jump();
249 OSD_SIGILL::NewInstance("SIGILL 'illegal instruction' detected.")->Jump();
253 OSD_SIGKILL::NewInstance("SIGKILL 'kill' detected.")->Jump();
257 sigaddset(&set, SIGBUS);
258 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
259 OSD_SIGBUS::NewInstance("SIGBUS 'bus error' detected.")->Jump();
263 OSD_SIGSEGV::NewInstance("SIGSEGV 'segmentation violation' detected.")->Jump();
268 OSD_SIGSYS::NewInstance("SIGSYS 'bad argument to system call' detected.")->Jump();
273 sigaddset(&set, SIGFPE);
274 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
276 // Pour DEC/OSF1 SIGFPE = Division par zero.
277 Standard_DivideByZero::NewInstance('')->Jump;
280 #if (!defined (__sun)) && (!defined(SOLARIS))
281 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
286 switch(aSigInfo->si_code) {
287 case FPE_FLTDIV_TRAP :
288 Standard_DivideByZero::NewInstance("Floating Divide By Zero")->Jump();
290 case FPE_INTDIV_TRAP :
291 Standard_DivideByZero::NewInstance("Integer Divide By Zero")->Jump();
293 case FPE_FLTOVF_TRAP :
294 Standard_Overflow::NewInstance("Floating Overflow")->Jump();
296 case FPE_INTOVF_TRAP :
297 Standard_Overflow::NewInstance("Integer Overflow")->Jump();
299 case FPE_FLTUND_TRAP :
300 Standard_NumericError::NewInstance("Floating Underflow")->Jump();
302 case FPE_FLTRES_TRAP:
303 Standard_NumericError::NewInstance("Floating Point Inexact Result")->Jump();
305 case FPE_FLTINV_TRAP :
306 Standard_NumericError::NewInstance("Invalid Floating Point Operation")->Jump();
309 Standard_NumericError::NewInstance("Numeric Error")->Jump();
313 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
317 #if defined (__sgi) || defined(IRIX)
319 sigaddset(&set, SIGTRAP);
320 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
321 Standard_DivideByZero::NewInstance("SIGTRAP IntegerDivideByZero")->Jump(); break;
325 cout << "Unexpected signal " << theSignal << endl ;
331 //============================================================================
333 //============================================================================
336 static void SegvHandler(const int theSignal,
338 const Standard_Address theContext)
340 #ifdef NO_CXX_EXCEPTION
341 if (!Standard_ErrorHandler::IsInTryBlock()) {
342 Handler(theSignal, ip, theContext);
350 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
351 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
353 // cout << "OSD::SegvHandler activated(SA_SIGINFO)" << endl ;
357 sigaddset(&set, SIGSEGV);
358 sigprocmask (SIG_UNBLOCK, &set, NULL) ;
359 void *address = ip->si_addr ;
362 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",
364 OSD_SIGSEGV::NewInstance(Msg)->Jump();
369 cout << "Wrong undefined address." << endl ;
375 #elif defined (_hpux) || defined(HPUX)
376 // Not ACTIVE ? SA_SIGINFO is defined on SUN, OSF, SGI and HP (and Linux) !
377 // pour version 09.07
379 static void SegvHandler(const int theSignal,
381 const Standard_Address theContext)
383 unsigned long Space ;
384 unsigned long Offset ;
387 if ( theContext != NULL ) {
388 Space = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr20 ;
389 Offset = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr21 ;
390 // cout << "Wrong address = " << hex(Offset) << endl ;
392 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",Offset) ;
393 OSD_SIGSEGV::Jump(Msg);
394 // scp->sc_pcoq_head = scp->sc_pcoq_tail ; Permettrait de continuer a
395 // scp->sc_pcoq_tail = scp->sc_pcoq_tail + 0x4 ; l'intruction suivant le segv.
400 cout << "Wrong undefined address." << endl ;
408 //============================================================================
410 //==== Set the differents signals:
411 //============================================================================
413 void OSD::SetSignal(const Standard_Boolean aFloatingSignal)
415 static int first_time = 3 ;
416 struct sigaction act, oact;
419 if( aFloatingSignal ) {
420 //==== Enable the floating point exceptions ===============
421 #if defined (__sun) || defined (SOLARIS)
422 sigfpe_handler_type PHandler = (sigfpe_handler_type) Handler ;
423 stat = ieee_handler("set", "invalid", PHandler);
424 stat = ieee_handler("set", "division", PHandler) || stat;
425 stat = ieee_handler("set", "overflow", PHandler) || stat;
427 //stat = ieee_handler("set", "underflow", PHandler) || stat;
428 //stat = ieee_handler("set", "inexact", PHandler) || stat;
432 cerr << "ieee_handler does not work !!! KO " << endl;
435 #elif defined (linux)
436 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
437 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
438 fFltExceptions = Standard_True;
441 else if ( first_time & 1 ) {
442 // cout << "SetSignal( Standard_False ) is not implemented..." << endl ;
443 first_time = first_time & (~ 1) ;
446 #if defined (sgi) || defined (IRIX )
447 if ( first_time & 2 ) {
448 char *TRAP_FPE = getenv("TRAP_FPE") ;
449 if ( TRAP_FPE == NULL ) {
451 cout << "On SGI you must set TRAP_FPE environment variable : " << endl ;
452 cout << "set env(TRAP_FPE) \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\" or" << endl ;
453 cout << "setenv TRAP_FPE \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\"" << endl ;
456 first_time = first_time & (~ 2) ;
461 //==== Save the old Signal Handler, and set the new one ===================
463 sigemptyset(&act.sa_mask) ;
466 act.sa_flags = SA_RESTART ;
471 act.sa_flags = act.sa_flags | SA_SIGINFO ;
472 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ Handler;
474 act.sa_handler = /*(SIG_PFV)*/ Handler;
477 //==== Always detected the signal "SIGFPE" =================================
478 stat = sigaction(SIGFPE,&act,&oact); // ...... floating point exception
481 cerr << "sigaction does not work !!! KO " << endl;
483 perror("sigaction ");
486 //==== Detected the only the "free" signals ================================
487 sigaction(SIGHUP,&act,&oact); // ...... hangup
491 sigaction(SIGHUP,&oact,&oact);
494 sigaction(SIGINT,&act,&oact); // ...... interrupt
498 sigaction(SIGINT,&oact,&oact);
501 sigaction(SIGQUIT,&act,&oact); // ...... quit
505 sigaction(SIGQUIT,&oact,&oact);
508 sigaction(SIGILL,&act,&oact); // ...... illegal instruction
512 sigaction(SIGILL,&oact,&oact);
515 sigaction(SIGBUS,&act,&oact); // ...... bus error
519 sigaction(SIGBUS,&oact,&oact);
522 #if (!defined (linux)) && (!defined(__linux__))
523 sigaction(SIGSYS,&act,&oact); // ...... bad argument to system call
527 sigaction(SIGSYS,&oact,&oact);
531 #if defined (__sgi) || defined(IRIX)
532 sigaction(SIGTRAP,&act,&oact); // Integer Divide By Zero (IRIX)
536 sigaction(SIGTRAP,&oact,&oact);
541 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ SegvHandler;
543 act.sa_handler = /*(SIG_PFV)*/ SegvHandler;
546 if ( sigaction( SIGSEGV , &act , &oact ) ) // ...... segmentation violation
547 perror("OSD::SetSignal sigaction( SIGSEGV , &act , &oact ) ") ;
551 sigaction(SIGSEGV,&oact,&oact);
553 #if defined(__osf__) || defined(DECOSF1)
554 struct sigaction action, prev_action;
555 action.sa_handler = SIG_IGN;
559 if (sigaction (SIGFPE, &action, &prev_action) == -1) {
560 perror ("sigaction");
567 //============================================================================
569 //============================================================================
571 void OSD :: ControlBreak ()
574 fCtrlBrk = Standard_False;
575 OSD_Exception_CTRL_BREAK::Raise ("*** INTERRUPT ***");