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 : ----------------------------------
26 #include <OSD_WhoAmI.hxx>
27 #include <OSD_SIGHUP.hxx>
28 #include <OSD_SIGINT.hxx>
29 #include <OSD_SIGQUIT.hxx>
30 #include <OSD_SIGILL.hxx>
31 #include <OSD_SIGKILL.hxx>
32 #include <OSD_SIGBUS.hxx>
33 #include <OSD_SIGSEGV.hxx>
34 #include <OSD_SIGSYS.hxx>
35 #include <OSD_Exception_CTRL_BREAK.hxx>
36 #include <Standard_NumericError.hxx>
37 #include <Standard_DivideByZero.hxx>
38 #include <Standard_Overflow.hxx>
40 #include <Standard_ErrorHandler.hxx>
48 static pthread_t getOCCThread () {
49 static pthread_t TheOCCThread = 0;
56 static Standard_Boolean fFltExceptions = Standard_False;
59 // variable signalling that Control-C has been pressed (SIGINT signal)
60 static Standard_Boolean fCtrlBrk;
62 //const OSD_WhoAmI Iam = OSD_WPackage;
64 typedef void (ACT_SIGIO_HANDLER)(void) ;
65 ACT_SIGIO_HANDLER *ADR_ACT_SIGIO_HANDLER = NULL ;
67 #if defined(HAVE_FLOATINGPOINT_H) && defined(HAVE_SYS_MACHSIG_H)
68 # include <floatingpoint.h>
69 # include <sys/machsig.h>
70 // JPT : Difference between SUN/SUNOS and SUN/SOLARIS
71 # define FPE_FLTDIV_TRAP FPE_FLTDIV
72 # define FPE_INTDIV_TRAP FPE_INTDIV
73 # define FPE_FLTOVF_TRAP FPE_FLTOVF
74 # define FPE_INTOVF_TRAP FPE_INTOVF
75 # define FPE_FLTUND_TRAP FPE_FLTUND
77 #define FPE_FLTRES_TRAP FPE_FLTRES /* floating point inexact result */
78 #define FPE_FLTINV_TRAP FPE_FLTINV /* invalid floating point operation */
79 #define FPE_FLTSUB_TRAP FPE_FLTSUB /* subscript out of range */
81 extern "C" {int ieee_handler(char *,char *, sigfpe_handler_type&);}
86 typedef void (* SIG_PFV) (int);
89 #if defined(HAVE_SIGFPE_H) && defined(HAVE_SYS_SIGINFO_H)
91 # include <sys/siginfo.h>
92 # define FPE_FLTDIV_TRAP FPE_FLTDIV
93 # define FPE_INTDIV_TRAP FPE_INTDIV
94 # define FPE_FLTOVF_TRAP FPE_FLTOVF
95 # define FPE_INTOVF_TRAP FPE_INTOVF
96 # define FPE_FLTUND_TRAP FPE_FLTUND
105 # include <sys/siginfo.h>
109 typedef void (* SIG_PFV) (int);
115 #ifdef HAVE_SYS_SIGNAL_H
116 # include <sys/signal.h>
119 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
120 //============================================================================
121 //==== GetOldSigAction
123 //============================================================================
125 static struct sigaction *GetOldSigAction()
127 static struct sigaction oldSignals[NSIG];
132 static sigfpe_handler_type *GetOldFPE()
134 static sigfpe_handler_type aIEEEHandler[5] = { NULL, NULL, NULL, NULL, NULL } ;
140 //============================================================================
142 //==== Catche the differents signals:
143 //==== 1- The Fatal signals, which cause the end of process:
144 //==== 2- The exceptions which are "signaled" by Raise.
145 //==== The Fatal Signals:
146 //==== SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGKILL, SIGBUS, SIGSYS
147 //==== The Exceptions:
149 //==== (SUN versions)
150 //==== FPE_INTOVF_TRAP // ..... integer overflow
151 //==== FPE_INTDIV_TRAP // ..... integer divide by zero
152 //==== FPE_FLTINEX_TRAP // ..... [floating inexact result]
153 //==== FPE_FLTDIV_TRAP // ..... [floating divide by zero]
154 //==== FPE_FLTUND_TRAP // ..... [floating underflow]
155 //==== FPE_FLTOPER_TRAP // ..... [floating inexact result]
156 //==== FPE_FLTOVF_TRAP // ..... [floating overflow]
157 //==== SIGSEGV is handled by "SegvHandler()"
158 //============================================================================
160 static void Handler (const int theSignal, siginfo_t *theSigInfo, const Standard_Address theContext)
162 static void Handler (const int theSignal)
165 struct sigaction oldact, act;
166 // re-install the signal
167 if ( ! sigaction (theSignal, NULL, &oldact) ) {
168 // cout << " signal is " << theSignal << " handler is " << oldact.sa_handler << endl;
169 if (sigaction (theSignal, &oldact, &act)) perror ("sigaction");
172 perror ("sigaction");
175 siginfo_t * aSigInfo = NULL;
177 aSigInfo = theSigInfo;
180 #if defined(HAVE_PTHREAD_H) && defined(NO_CXX_EXCEPTION)
181 if (pthread_self() != getOCCThread() || !Standard_ErrorHandler::IsInTryBlock()) {
182 // use the previous signal handler
183 // cout << "OSD::Handler: signal " << (int) theSignal << " occured outside a try block " << endl ;
184 struct sigaction *oldSignals = GetOldSigAction();
185 struct sigaction asigacthandler = oldSignals[theSignal >= 0 && theSignal < NSIG ? theSignal : 0];
187 if (asigacthandler.sa_flags & SA_SIGINFO) {
188 void (*aCurInfoHandle)(int, siginfo_t *, void *) = asigacthandler.sa_sigaction;
190 switch (aSigInfo->si_signo) {
194 sigfpe_handler_type *aIEEEHandlerTab = GetOldFPE();
195 sigfpe_handler_type aIEEEHandler = NULL;
196 switch (aSigInfo->si_code) {
197 case FPE_INTDIV_TRAP :
198 case FPE_FLTDIV_TRAP :
199 aIEEEHandler = aIEEEHandlerTab[1];
201 case FPE_INTOVF_TRAP :
202 case FPE_FLTOVF_TRAP :
203 aIEEEHandler = aIEEEHandlerTab[2];
205 case FPE_FLTUND_TRAP :
206 aIEEEHandler = aIEEEHandlerTab[4];
208 case FPE_FLTRES_TRAP :
209 aIEEEHandler = aIEEEHandlerTab[3];
211 case FPE_FLTINV_TRAP :
212 aIEEEHandler = aIEEEHandlerTab[0];
214 case FPE_FLTSUB_TRAP :
219 // cout << "OSD::Handler: calling previous IEEE signal handler with info" << endl ;
220 (*aIEEEHandler) (theSignal, aSigInfo, theContext);
230 if (aCurInfoHandle) {
231 // cout << "OSD::Handler: calling previous signal handler with info " << aCurInfoHandle << endl ;
232 (*aCurInfoHandle) (theSignal, aSigInfo, theContext);
233 cerr << " previous signal handler return" << endl ;
237 // cout << "OSD::Handler: no handler with info for the signal" << endl;
241 // no siginfo needed for the signal
242 void (*aCurHandler) (int) = asigacthandler.sa_handler;
244 // cout << "OSD::Handler: calling previous signal handler" << endl ;
245 (*aCurHandler) (theSignal);
246 cerr << " previous signal handler return" << endl ;
250 // cout << " Signal occured outside a try block, but no handler for it" <<endl;
255 // cout << "OSD::Handler: signal " << (int) theSignal << " occured inside a try block " << endl ;
256 if ( ADR_ACT_SIGIO_HANDLER != NULL )
257 (*ADR_ACT_SIGIO_HANDLER)() ;
260 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
261 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
267 OSD_SIGHUP::NewInstance("SIGHUP 'hangup' detected.")->Jump();
271 // For safe handling of Control-C as stop event, arm a variable but do not
272 // generate longjump (we are out of context anyway)
273 fCtrlBrk = Standard_True;
274 // OSD_SIGINT::NewInstance("SIGINT 'interrupt' detected.")->Jump();
278 OSD_SIGQUIT::NewInstance("SIGQUIT 'quit' detected.")->Jump();
282 OSD_SIGILL::NewInstance("SIGILL 'illegal instruction' detected.")->Jump();
286 OSD_SIGKILL::NewInstance("SIGKILL 'kill' detected.")->Jump();
290 sigaddset(&set, SIGBUS);
291 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
292 OSD_SIGBUS::NewInstance("SIGBUS 'bus error' detected.")->Jump();
296 OSD_SIGSEGV::NewInstance("SIGSEGV 'segmentation violation' detected.")->Jump();
301 OSD_SIGSYS::NewInstance("SIGSYS 'bad argument to system call' detected.")->Jump();
306 sigaddset(&set, SIGFPE);
307 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
309 // Pour DEC/OSF1 SIGFPE = Division par zero.
310 // should be clarified why in debug mode only?
312 Standard_DivideByZero::NewInstance('')->Jump;
316 #if (!defined (__sun)) && (!defined(SOLARIS))
317 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
322 switch(aSigInfo->si_code) {
323 case FPE_FLTDIV_TRAP :
324 Standard_DivideByZero::NewInstance("Floating Divide By Zero")->Jump();
326 case FPE_INTDIV_TRAP :
327 Standard_DivideByZero::NewInstance("Integer Divide By Zero")->Jump();
329 case FPE_FLTOVF_TRAP :
330 Standard_Overflow::NewInstance("Floating Overflow")->Jump();
332 case FPE_INTOVF_TRAP :
333 Standard_Overflow::NewInstance("Integer Overflow")->Jump();
335 case FPE_FLTUND_TRAP :
336 Standard_NumericError::NewInstance("Floating Underflow")->Jump();
338 case FPE_FLTRES_TRAP:
339 Standard_NumericError::NewInstance("Floating Point Inexact Result")->Jump();
341 case FPE_FLTINV_TRAP :
342 Standard_NumericError::NewInstance("Invalid Floating Point Operation")->Jump();
345 Standard_NumericError::NewInstance("Numeric Error")->Jump();
349 Standard_NumericError::NewInstance("SIGFPE Arithmetic exception detected")->Jump();
353 #if defined (__sgi) || defined(IRIX)
355 sigaddset(&set, SIGTRAP);
356 sigprocmask(SIG_UNBLOCK, &set, NULL) ;
357 Standard_DivideByZero::NewInstance("SIGTRAP IntegerDivideByZero")->Jump(); break;
360 cout << "Unexpected signal " << theSignal << endl ;
365 //============================================================================
367 //============================================================================
370 static void SegvHandler(const int theSignal,
372 const Standard_Address theContext)
374 #ifdef NO_CXX_EXCEPTION
375 if (!Standard_ErrorHandler::IsInTryBlock()) {
376 Handler(theSignal, ip, theContext);
382 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
383 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
385 // cout << "OSD::SegvHandler activated(SA_SIGINFO)" << endl ;
389 sigaddset(&set, SIGSEGV);
390 sigprocmask (SIG_UNBLOCK, &set, NULL) ;
391 void *address = ip->si_addr ;
394 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",
396 OSD_SIGSEGV::NewInstance(Msg)->Jump();
400 cout << "Wrong undefined address." << endl ;
405 #elif defined (_hpux) || defined(HPUX)
406 // Not ACTIVE ? SA_SIGINFO is defined on SUN, OSF, SGI and HP (and Linux) !
407 // pour version 09.07
409 static void SegvHandler(const int theSignal,
411 const Standard_Address theContext)
413 unsigned long Space ;
414 unsigned long Offset ;
417 if ( theContext != NULL ) {
418 Space = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr20 ;
419 Offset = ((struct sigcontext *)theContext)->sc_sl.sl_ss.ss_cr21 ;
420 // cout << "Wrong address = " << hex(Offset) << endl ;
422 sprintf(Msg,"SIGSEGV 'segmentation violation' detected. Address %lx",Offset) ;
423 OSD_SIGSEGV::Jump(Msg);
424 // scp->sc_pcoq_head = scp->sc_pcoq_tail ; Permettrait de continuer a
425 // scp->sc_pcoq_tail = scp->sc_pcoq_tail + 0x4 ; l'intruction suivant le segv.
429 cout << "Wrong undefined address." << endl ;
436 //============================================================================
438 //==== Set the differents signals:
439 //============================================================================
441 void OSD::SetSignal(const Standard_Boolean aFloatingSignal)
443 static int first_time = 3 ;
444 struct sigaction act, oact;
447 if( aFloatingSignal ) {
448 //==== Enable the floating point exceptions ===============
449 #if defined (__sun) || defined (SOLARIS)
450 sigfpe_handler_type PHandler = (sigfpe_handler_type) Handler ;
451 stat = ieee_handler("set", "invalid", PHandler);
452 stat = ieee_handler("set", "division", PHandler) || stat;
453 stat = ieee_handler("set", "overflow", PHandler) || stat;
455 //stat = ieee_handler("set", "underflow", PHandler) || stat;
456 //stat = ieee_handler("set", "inexact", PHandler) || stat;
459 cerr << "ieee_handler does not work !!! KO " << endl;
461 #elif defined (linux)
462 feenableexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
463 //feenableexcept (FE_INVALID | FE_DIVBYZERO);
464 fFltExceptions = Standard_True;
467 else if ( first_time & 1 ) {
468 // cout << "SetSignal( Standard_False ) is not implemented..." << endl ;
469 first_time = first_time & (~ 1) ;
472 #if defined (sgi) || defined (IRIX )
473 if ( first_time & 2 ) {
474 char *TRAP_FPE = getenv("TRAP_FPE") ;
475 if ( TRAP_FPE == NULL ) {
476 cout << "On SGI you must set TRAP_FPE environment variable : " << endl ;
477 cout << "set env(TRAP_FPE) \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\" or" << endl ;
478 cout << "setenv TRAP_FPE \"UNDERFL=FLUSH_ZERO;OVERFL=DEFAULT;DIVZERO=DEFAULT;INT_OVERFL=DEFAULT\"" << endl ;
480 first_time = first_time & (~ 2) ;
485 //==== Save the old Signal Handler, and set the new one ===================
487 sigemptyset(&act.sa_mask) ;
490 act.sa_flags = SA_RESTART ;
495 act.sa_flags = act.sa_flags | SA_SIGINFO ;
496 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ Handler;
498 act.sa_handler = /*(SIG_PFV)*/ Handler;
501 //==== Always detected the signal "SIGFPE" =================================
502 stat = sigaction(SIGFPE,&act,&oact); // ...... floating point exception
504 cerr << "sigaction does not work !!! KO " << endl;
505 perror("sigaction ");
508 //==== Detected the only the "free" signals ================================
509 sigaction(SIGHUP,&act,&oact); // ...... hangup
513 sigaction(SIGHUP,&oact,&oact);
516 sigaction(SIGINT,&act,&oact); // ...... interrupt
520 sigaction(SIGINT,&oact,&oact);
523 sigaction(SIGQUIT,&act,&oact); // ...... quit
527 sigaction(SIGQUIT,&oact,&oact);
530 sigaction(SIGILL,&act,&oact); // ...... illegal instruction
534 sigaction(SIGILL,&oact,&oact);
537 sigaction(SIGBUS,&act,&oact); // ...... bus error
541 sigaction(SIGBUS,&oact,&oact);
544 #if (!defined (linux)) && (!defined(LININTEL))
545 sigaction(SIGSYS,&act,&oact); // ...... bad argument to system call
549 sigaction(SIGSYS,&oact,&oact);
553 #if defined (__sgi) || defined(IRIX)
554 sigaction(SIGTRAP,&act,&oact); // Integer Divide By Zero (IRIX)
558 sigaction(SIGTRAP,&oact,&oact);
563 act.sa_sigaction = /*(void(*)(int, siginfo_t *, void*))*/ SegvHandler;
565 act.sa_handler = /*(SIG_PFV)*/ SegvHandler;
568 if ( sigaction( SIGSEGV , &act , &oact ) ) // ...... segmentation violation
569 perror("OSD::SetSignal sigaction( SIGSEGV , &act , &oact ) ") ;
573 sigaction(SIGSEGV,&oact,&oact);
575 #if defined(__osf__) || defined(DECOSF1)
576 struct sigaction action, prev_action;
577 action.sa_handler = SIG_IGN;
581 if (sigaction (SIGFPE, &action, &prev_action) == -1) {
582 perror ("sigaction");
589 //============================================================================
591 //============================================================================
593 void OSD :: ControlBreak ()
596 fCtrlBrk = Standard_False;
597 OSD_Exception_CTRL_BREAK::Raise ("*** INTERRUPT ***");