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.
18 //---------- All Systems except Windows NT : ----------------------------------
22 #include <OSD_WhoAmI.hxx>
23 #include <OSD_SIGHUP.hxx>
24 #include <OSD_SIGINT.hxx>
25 #include <OSD_SIGQUIT.hxx>
26 #include <OSD_SIGILL.hxx>
27 #include <OSD_SIGKILL.hxx>
28 #include <OSD_SIGBUS.hxx>
29 #include <OSD_SIGSEGV.hxx>
30 #include <OSD_SIGSYS.hxx>
31 #include <OSD_Exception_CTRL_BREAK.hxx>
32 #include <Standard_NumericError.hxx>
33 #include <Standard_DivideByZero.hxx>
34 #include <Standard_Overflow.hxx>
36 #include <Standard_ErrorHandler.hxx>
43 static Standard_Boolean fFltExceptions = Standard_False;
46 // variable signalling that Control-C has been pressed (SIGINT signal)
47 static Standard_Boolean fCtrlBrk;
49 //const OSD_WhoAmI Iam = OSD_WPackage;
51 typedef void (ACT_SIGIO_HANDLER)(void) ;
52 ACT_SIGIO_HANDLER *ADR_ACT_SIGIO_HANDLER = NULL ;
55 typedef void (* SIG_PFV) (int);
64 # include <sys/siginfo.h>
68 typedef void (* SIG_PFV) (int);
72 #if !defined(__ANDROID__)
73 #include <sys/signal.h>
76 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
77 //============================================================================
78 //==== GetOldSigAction
80 //============================================================================
82 static struct sigaction *GetOldSigAction()
84 static struct sigaction oldSignals[NSIG];
89 static sigfpe_handler_type *GetOldFPE()
91 static sigfpe_handler_type aIEEEHandler[5] = { NULL, NULL, NULL, NULL, NULL } ;
97 //============================================================================
99 //==== Catche the differents signals:
100 //==== 1- The Fatal signals, which cause the end of process:
101 //==== 2- The exceptions which are "signaled" by Raise.
102 //==== The Fatal Signals:
103 //==== SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGKILL, SIGBUS, SIGSYS
104 //==== The Exceptions:
106 //==== (SUN versions)
107 //==== FPE_INTOVF_TRAP // ..... integer overflow
108 //==== FPE_INTDIV_TRAP // ..... integer divide by zero
109 //==== FPE_FLTINEX_TRAP // ..... [floating inexact result]
110 //==== FPE_FLTDIV_TRAP // ..... [floating divide by zero]
111 //==== FPE_FLTUND_TRAP // ..... [floating underflow]
112 //==== FPE_FLTOPER_TRAP // ..... [floating inexact result]
113 //==== FPE_FLTOVF_TRAP // ..... [floating overflow]
114 //==== SIGSEGV is handled by "SegvHandler()"
115 //============================================================================
117 static void Handler (const int theSignal, siginfo_t *theSigInfo, const Standard_Address theContext)
119 static void Handler (const int theSignal)
122 struct sigaction oldact, act;
123 // re-install the signal
124 if ( ! sigaction (theSignal, NULL, &oldact) ) {
125 // cout << " signal is " << theSignal << " handler is " << oldact.sa_handler << endl;
126 if (sigaction (theSignal, &oldact, &act)) perror ("sigaction");
129 perror ("sigaction");
132 siginfo_t * aSigInfo = NULL;
134 aSigInfo = theSigInfo;
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 switch (aSigInfo->si_signo) {
151 sigfpe_handler_type *aIEEEHandlerTab = GetOldFPE();
152 sigfpe_handler_type aIEEEHandler = NULL;
153 switch (aSigInfo->si_code) {
154 case FPE_INTDIV_TRAP :
155 case FPE_FLTDIV_TRAP :
156 aIEEEHandler = aIEEEHandlerTab[1];
158 case FPE_INTOVF_TRAP :
159 case FPE_FLTOVF_TRAP :
160 aIEEEHandler = aIEEEHandlerTab[2];
162 case FPE_FLTUND_TRAP :
163 aIEEEHandler = aIEEEHandlerTab[4];
165 case FPE_FLTRES_TRAP :
166 aIEEEHandler = aIEEEHandlerTab[3];
168 case FPE_FLTINV_TRAP :
169 aIEEEHandler = aIEEEHandlerTab[0];
171 case FPE_FLTSUB_TRAP :
176 // cout << "OSD::Handler: calling previous IEEE signal handler with info" << endl ;
177 (*aIEEEHandler) (theSignal, aSigInfo, theContext);
187 if (aCurInfoHandle) {
188 // cout << "OSD::Handler: calling previous signal handler with info " << aCurInfoHandle << endl ;
189 (*aCurInfoHandle) (theSignal, aSigInfo, theContext);
191 cerr << " previous signal handler return" << endl ;
196 // cout << "OSD::Handler: no handler with info for the signal" << endl;
200 // no siginfo needed for the signal
201 void (*aCurHandler) (int) = asigacthandler.sa_handler;
203 // cout << "OSD::Handler: calling previous signal handler" << endl ;
204 (*aCurHandler) (theSignal);
206 cerr << " previous signal handler return" << endl ;
211 // cout << " Signal occured outside a try block, but no handler for it" <<endl;
216 // cout << "OSD::Handler: signal " << (int) theSignal << " occured inside a try block " << endl ;
217 if ( ADR_ACT_SIGIO_HANDLER != NULL )
218 (*ADR_ACT_SIGIO_HANDLER)() ;
221 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
222 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
228 OSD_SIGHUP::NewInstance("SIGHUP 'hangup' detected.")->Jump();
232 // For safe handling of Control-C as stop event, arm a variable but do not
233 // generate longjump (we are out of context anyway)
234 fCtrlBrk = Standard_True;
235 // OSD_SIGINT::NewInstance("SIGINT 'interrupt' detected.")->Jump();
239 OSD_SIGQUIT::NewInstance("SIGQUIT 'quit' detected.")->Jump();
243 OSD_SIGILL::NewInstance("SIGILL 'illegal instruction' detected.")->Jump();
247 OSD_SIGKILL::NewInstance("SIGKILL 'kill' detected.")->Jump();
251 sigaddset(&set, SIGBUS);
252 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
253 OSD_SIGBUS::NewInstance("SIGBUS 'bus error' detected.")->Jump();
257 OSD_SIGSEGV::NewInstance("SIGSEGV 'segmentation violation' detected.")->Jump();
262 OSD_SIGSYS::NewInstance("SIGSYS 'bad argument to system call' detected.")->Jump();
267 sigaddset(&set, SIGFPE);
268 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
270 // Pour DEC/OSF1 SIGFPE = Division par zero.
271 // should be clarified why in debug mode only?
273 Standard_DivideByZero::NewInstance('')->Jump;
277 #if (!defined (__sun)) && (!defined(SOLARIS))
278 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
283 switch(aSigInfo->si_code) {
284 case FPE_FLTDIV_TRAP :
285 Standard_DivideByZero::NewInstance("Floating Divide By Zero")->Jump();
287 case FPE_INTDIV_TRAP :
288 Standard_DivideByZero::NewInstance("Integer Divide By Zero")->Jump();
290 case FPE_FLTOVF_TRAP :
291 Standard_Overflow::NewInstance("Floating Overflow")->Jump();
293 case FPE_INTOVF_TRAP :
294 Standard_Overflow::NewInstance("Integer Overflow")->Jump();
296 case FPE_FLTUND_TRAP :
297 Standard_NumericError::NewInstance("Floating Underflow")->Jump();
299 case FPE_FLTRES_TRAP:
300 Standard_NumericError::NewInstance("Floating Point Inexact Result")->Jump();
302 case FPE_FLTINV_TRAP :
303 Standard_NumericError::NewInstance("Invalid Floating Point Operation")->Jump();
306 Standard_NumericError::NewInstance("Numeric Error")->Jump();
310 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
314 #if defined (__sgi) || defined(IRIX)
316 sigaddset(&set, SIGTRAP);
317 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
318 Standard_DivideByZero::NewInstance("SIGTRAP IntegerDivideByZero")->Jump(); break;
322 cout << "Unexpected signal " << theSignal << endl ;
328 //============================================================================
330 //============================================================================
333 static void SegvHandler(const int theSignal,
335 const Standard_Address theContext)
337 #ifdef NO_CXX_EXCEPTION
338 if (!Standard_ErrorHandler::IsInTryBlock()) {
339 Handler(theSignal, ip, theContext);
345 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
346 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
348 // cout << "OSD::SegvHandler activated(SA_SIGINFO)" << endl ;
352 sigaddset(&set, SIGSEGV);
353 sigprocmask (SIG_UNBLOCK, &set, NULL) ;
354 void *address = ip->si_addr ;
357 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",
359 OSD_SIGSEGV::NewInstance(Msg)->Jump();
364 cout << "Wrong undefined address." << endl ;
370 #elif defined (_hpux) || defined(HPUX)
371 // Not ACTIVE ? SA_SIGINFO is defined on SUN, OSF, SGI and HP (and Linux) !
372 // pour version 09.07
374 static void SegvHandler(const int theSignal,
376 const Standard_Address theContext)
378 unsigned long Space ;
379 unsigned long Offset ;
382 if ( theContext != NULL ) {
383 Space = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr20 ;
384 Offset = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr21 ;
385 // cout << "Wrong address = " << hex(Offset) << endl ;
387 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",Offset) ;
388 OSD_SIGSEGV::Jump(Msg);
389 // scp->sc_pcoq_head = scp->sc_pcoq_tail ; Permettrait de continuer a
390 // scp->sc_pcoq_tail = scp->sc_pcoq_tail + 0x4 ; l'intruction suivant le segv.
395 cout << "Wrong undefined address." << endl ;
403 //============================================================================
405 //==== Set the differents signals:
406 //============================================================================
408 void OSD::SetSignal(const Standard_Boolean aFloatingSignal)
410 static int first_time = 3 ;
411 struct sigaction act, oact;
414 if( aFloatingSignal ) {
415 //==== Enable the floating point exceptions ===============
416 #if defined (__sun) || defined (SOLARIS)
417 sigfpe_handler_type PHandler = (sigfpe_handler_type) Handler ;
418 stat = ieee_handler("set", "invalid", PHandler);
419 stat = ieee_handler("set", "division", PHandler) || stat;
420 stat = ieee_handler("set", "overflow", PHandler) || stat;
422 //stat = ieee_handler("set", "underflow", PHandler) || stat;
423 //stat = ieee_handler("set", "inexact", PHandler) || stat;
427 cerr << "ieee_handler does not work !!! KO " << endl;
430 #elif defined (linux)
431 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
432 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
433 fFltExceptions = Standard_True;
436 else if ( first_time & 1 ) {
437 // cout << "SetSignal( Standard_False ) is not implemented..." << endl ;
438 first_time = first_time & (~ 1) ;
441 #if defined (sgi) || defined (IRIX )
442 if ( first_time & 2 ) {
443 char *TRAP_FPE = getenv("TRAP_FPE") ;
444 if ( TRAP_FPE == NULL ) {
446 cout << "On SGI you must set TRAP_FPE environment variable : " << endl ;
447 cout << "set env(TRAP_FPE) \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\" or" << endl ;
448 cout << "setenv TRAP_FPE \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\"" << endl ;
451 first_time = first_time & (~ 2) ;
456 //==== Save the old Signal Handler, and set the new one ===================
458 sigemptyset(&act.sa_mask) ;
461 act.sa_flags = SA_RESTART ;
466 act.sa_flags = act.sa_flags | SA_SIGINFO ;
467 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ Handler;
469 act.sa_handler = /*(SIG_PFV)*/ Handler;
472 //==== Always detected the signal "SIGFPE" =================================
473 stat = sigaction(SIGFPE,&act,&oact); // ...... floating point exception
476 cerr << "sigaction does not work !!! KO " << endl;
478 perror("sigaction ");
481 //==== Detected the only the "free" signals ================================
482 sigaction(SIGHUP,&act,&oact); // ...... hangup
486 sigaction(SIGHUP,&oact,&oact);
489 sigaction(SIGINT,&act,&oact); // ...... interrupt
493 sigaction(SIGINT,&oact,&oact);
496 sigaction(SIGQUIT,&act,&oact); // ...... quit
500 sigaction(SIGQUIT,&oact,&oact);
503 sigaction(SIGILL,&act,&oact); // ...... illegal instruction
507 sigaction(SIGILL,&oact,&oact);
510 sigaction(SIGBUS,&act,&oact); // ...... bus error
514 sigaction(SIGBUS,&oact,&oact);
517 #if (!defined (linux)) && (!defined(LININTEL))
518 sigaction(SIGSYS,&act,&oact); // ...... bad argument to system call
522 sigaction(SIGSYS,&oact,&oact);
526 #if defined (__sgi) || defined(IRIX)
527 sigaction(SIGTRAP,&act,&oact); // Integer Divide By Zero (IRIX)
531 sigaction(SIGTRAP,&oact,&oact);
536 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ SegvHandler;
538 act.sa_handler = /*(SIG_PFV)*/ SegvHandler;
541 if ( sigaction( SIGSEGV , &act , &oact ) ) // ...... segmentation violation
542 perror("OSD::SetSignal sigaction( SIGSEGV , &act , &oact ) ") ;
546 sigaction(SIGSEGV,&oact,&oact);
548 #if defined(__osf__) || defined(DECOSF1)
549 struct sigaction action, prev_action;
550 action.sa_handler = SIG_IGN;
554 if (sigaction (SIGFPE, &action, &prev_action) == -1) {
555 perror ("sigaction");
562 //============================================================================
564 //============================================================================
566 void OSD :: ControlBreak ()
569 fCtrlBrk = Standard_False;
570 OSD_Exception_CTRL_BREAK::Raise ("*** INTERRUPT ***");