1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "runtime/arguments.hpp"
  27 #include "runtime/os.hpp"
  28 #include "runtime/thread.hpp"
  29 #include "utilities/vmError.hpp"
  30 
  31 #include <sys/types.h>
  32 #include <sys/wait.h>
  33 #include <thread.h>
  34 #include <signal.h>
  35 
  36 void VMError::show_message_box(char *buf, int buflen, int id, const char* message, const char* detail_fmt,
  37                                va_list detail_args, address pc, const char* filename, int lineno) {
  38   bool yes;
  39   do {
  40     error_string(buf, buflen, id, message, detail_fmt, detail_args, pc, filename, lineno);
  41     int len = (int)strlen(buf);
  42     char *p = &buf[len];
  43 
  44     jio_snprintf(p, buflen - len,
  45                "\n\n"
  46                "Do you want to debug the problem?\n\n"
  47                "To debug, run 'dbx - %d'; then switch to thread " INTX_FORMAT "\n"
  48                "Enter 'yes' to launch dbx automatically (PATH must include dbx)\n"
  49                "Otherwise, press RETURN to abort...",
  50                os::current_process_id(), os::current_thread_id());
  51 
  52     yes = os::message_box("Unexpected Error", buf);
  53 
  54     if (yes) {
  55       // yes, user asked VM to launch debugger
  56       jio_snprintf(buf, buflen, "dbx - %d", os::current_process_id());
  57 
  58       os::fork_and_exec(buf);
  59       yes = false;
  60     }
  61   } while (yes);
  62 }
  63 
  64 // handle all synchronous program error signals which may happen during error
  65 // reporting. They must be unblocked, caught, handled.
  66 
  67 static const int SIGNALS[] = { SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP }; // add more if needed
  68 static const int NUM_SIGNALS = sizeof(SIGNALS) / sizeof(int);
  69 
  70 // Space for our "saved" signal flags and handlers
  71 static int resettedSigflags[NUM_SIGNALS];
  72 static address resettedSighandler[NUM_SIGNALS];
  73 
  74 static void save_signal(int idx, int sig)
  75 {
  76   struct sigaction sa;
  77   sigaction(sig, NULL, &sa);
  78   resettedSigflags[idx]   = sa.sa_flags;
  79   resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
  80                               ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  81                               : CAST_FROM_FN_PTR(address, sa.sa_handler);
  82 }
  83 
  84 int VMError::get_resetted_sigflags(int sig) {
  85   for (int i = 0; i < NUM_SIGNALS; i++) {
  86     if (SIGNALS[i] == sig) {
  87       return resettedSigflags[i];
  88     }
  89   }
  90   return -1;
  91 }
  92 
  93 address VMError::get_resetted_sighandler(int sig) {
  94   for (int i = 0; i < NUM_SIGNALS; i++) {
  95     if (SIGNALS[i] == sig) {
  96       return resettedSighandler[i];
  97     }
  98   }
  99   return NULL;
 100 }
 101 
 102 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
 103   // unmask current signal
 104   sigset_t newset;
 105   sigemptyset(&newset);
 106   sigaddset(&newset, sig);
 107   // also unmask other synchronous signals
 108   for (int i = 0; i < NUM_SIGNALS; i++) {
 109     sigaddset(&newset, SIGNALS[i]);
 110   }
 111   thr_sigsetmask(SIG_UNBLOCK, &newset, NULL);
 112 
 113   // support safefetch faults in error handling
 114   ucontext_t* const uc = (ucontext_t*) ucVoid;
 115   address const pc = uc ? os::Solaris::ucontext_get_pc(uc) : NULL;
 116   if (uc && pc && StubRoutines::is_safefetch_fault(pc)) {
 117     os::Solaris::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc));
 118     return;
 119   }
 120 
 121   VMError::report_and_die(NULL, sig, pc, info, ucVoid);
 122 }
 123 
 124 void VMError::reset_signal_handlers() {
 125   // install signal handlers for all synchronous program error signals
 126   sigset_t newset;
 127   sigemptyset(&newset);
 128 
 129   for (int i = 0; i < NUM_SIGNALS; i++) {
 130     save_signal(i, SIGNALS[i]);
 131     os::signal(SIGNALS[i], CAST_FROM_FN_PTR(void *, crash_handler));
 132     sigaddset(&newset, SIGNALS[i]);
 133   }
 134   thr_sigsetmask(SIG_UNBLOCK, &newset, NULL);
 135 }