< prev index next >

src/os/bsd/vm/vmError_bsd.cpp

Print this page
rev 7703 : 8065895 Synchronous signals during error reporting may terminate or hang VM process
Contributed-by: stuefe


  46                "Do you want to debug the problem?\n\n"
  47                "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " INTX_FORMAT " (" INTPTR_FORMAT ")\n"
  48                "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n"
  49                "Otherwise, press RETURN to abort...",
  50                os::current_process_id(), os::current_process_id(),
  51                os::current_thread_id(), os::current_thread_id());
  52 
  53     yes = os::message_box("Unexpected Error", buf);
  54 
  55     if (yes) {
  56       // yes, user asked VM to launch debugger
  57       jio_snprintf(buf, buflen, "gdb /proc/%d/exe %d",
  58                    os::current_process_id(), os::current_process_id());
  59 
  60       os::fork_and_exec(buf);
  61       yes = false;
  62     }
  63   } while (yes);
  64 }
  65 












  66 // Space for our "saved" signal flags and handlers
  67 static int resettedSigflags[2];
  68 static address resettedSighandler[2];
  69 
  70 static void save_signal(int idx, int sig)
  71 {
  72   struct sigaction sa;
  73   sigaction(sig, NULL, &sa);
  74   resettedSigflags[idx]   = sa.sa_flags;
  75   resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
  76                               ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  77                               : CAST_FROM_FN_PTR(address, sa.sa_handler);
  78 }
  79 
  80 int VMError::get_resetted_sigflags(int sig) {
  81   if(SIGSEGV == sig) {
  82     return resettedSigflags[0];
  83   } else if(SIGBUS == sig) {
  84     return resettedSigflags[1];
  85   }
  86   return -1;
  87 }
  88 
  89 address VMError::get_resetted_sighandler(int sig) {
  90   if(SIGSEGV == sig) {
  91     return resettedSighandler[0];
  92   } else if(SIGBUS == sig) {
  93     return resettedSighandler[1];
  94   }
  95   return NULL;
  96 }
  97 
  98 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
  99   // unmask current signal
 100   sigset_t newset;
 101   sigemptyset(&newset);
 102   sigaddset(&newset, sig);
 103   sigprocmask(SIG_UNBLOCK, &newset, NULL);




 104 
 105   VMError err(NULL, sig, NULL, info, ucVoid);
 106   err.report_and_die();
 107 }
 108 
 109 void VMError::reset_signal_handlers() {
 110   // Save sigflags for resetted signals
 111   save_signal(0, SIGSEGV);
 112   save_signal(1, SIGBUS);
 113   os::signal(SIGSEGV, CAST_FROM_FN_PTR(void *, crash_handler));
 114   os::signal(SIGBUS, CAST_FROM_FN_PTR(void *, crash_handler));





 115 }


  46                "Do you want to debug the problem?\n\n"
  47                "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " INTX_FORMAT " (" INTPTR_FORMAT ")\n"
  48                "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n"
  49                "Otherwise, press RETURN to abort...",
  50                os::current_process_id(), os::current_process_id(),
  51                os::current_thread_id(), os::current_thread_id());
  52 
  53     yes = os::message_box("Unexpected Error", buf);
  54 
  55     if (yes) {
  56       // yes, user asked VM to launch debugger
  57       jio_snprintf(buf, buflen, "gdb /proc/%d/exe %d",
  58                    os::current_process_id(), os::current_process_id());
  59 
  60       os::fork_and_exec(buf);
  61       yes = false;
  62     }
  63   } while (yes);
  64 }
  65 
  66 // handle all synchronous program error signals which may happen during error 
  67 // reporting. They must be unblocked, caught, handled.
  68 // 
  69 // Note that in the hotspot signal handlers are installed with a fully filled 
  70 // signal mask, i.e. upon delivery all signals - also synchronous error signals 
  71 // - are blocked. For synchronous error signals this is deadly - program will 
  72 // hang or be ended immediately if secondary errors happend during error 
  73 // reporting.
  74 
  75 static const int SIGNALS[] = { SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP }; // add more if needed
  76 static const int NUM_SIGNALS = sizeof(SIGNALS) / sizeof(int);
  77 
  78 // Space for our "saved" signal flags and handlers
  79 static int resettedSigflags[NUM_SIGNALS];
  80 static address resettedSighandler[NUM_SIGNALS];
  81 
  82 static void save_signal(int idx, int sig)
  83 {
  84   struct sigaction sa;
  85   sigaction(sig, NULL, &sa);
  86   resettedSigflags[idx]   = sa.sa_flags;
  87   resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
  88                               ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  89                               : CAST_FROM_FN_PTR(address, sa.sa_handler);
  90 }
  91 
  92 int VMError::get_resetted_sigflags(int sig) {
  93   for (int i = 0; i < NUM_SIGNALS; i++) {
  94     if (SIGNALS[i] == sig) {
  95       return resettedSigflags[i];
  96     }
  97   }
  98   return -1;
  99 }
 100 
 101 address VMError::get_resetted_sighandler(int sig) {
 102   for (int i = 0; i < NUM_SIGNALS; i++) {
 103     if (SIGNALS[i] == sig) {
 104       return resettedSighandler[i];
 105     }
 106   }
 107   return NULL;
 108 }
 109 
 110 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
 111   // unmask current signal
 112   sigset_t newset;
 113   sigemptyset(&newset);
 114   sigaddset(&newset, sig);
 115   // also unmask other synchronous signals
 116   for (int i = 0; i < NUM_SIGNALS; i++) {
 117     sigaddset(&newset, SIGNALS[i]);
 118   }
 119   pthread_sigmask(SIG_UNBLOCK, &newset, NULL);
 120 
 121   VMError err(NULL, sig, NULL, info, ucVoid);
 122   err.report_and_die();
 123 }
 124 
 125 void VMError::reset_signal_handlers() {
 126   // install signal handlers for all synchronous program error signals
 127   sigset_t newset;
 128   sigemptyset(&newset);
 129 
 130   for (int i = 0; i < NUM_SIGNALS; i++) {
 131     save_signal(i, SIGNALS[i]);
 132     os::signal(SIGNALS[i], CAST_FROM_FN_PTR(void *, crash_handler));
 133     sigaddset(&newset, SIGNALS[i]);
 134   }
 135   pthread_sigmask(SIG_UNBLOCK, &newset, NULL);
 136 }
< prev index next >