1 /*
   2  * Copyright (c) 2003, 2006, 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 "incls/_precompiled.incl"
  26 # include "incls/_vmError_linux.cpp.incl"
  27 
  28 #include <sys/types.h>
  29 #include <sys/wait.h>
  30 #include <sys/syscall.h>
  31 #include <unistd.h>
  32 #include <signal.h>
  33 
  34 void VMError::show_message_box(char *buf, int buflen) {
  35   bool yes;
  36   do {
  37     error_string(buf, buflen);
  38     int len = (int)strlen(buf);
  39     char *p = &buf[len];
  40 
  41     jio_snprintf(p, buflen - len,
  42                "\n\n"
  43                "Do you want to debug the problem?\n\n"
  44                "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " INTX_FORMAT "\n"
  45                "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n"
  46                "Otherwise, press RETURN to abort...",
  47                os::current_process_id(), os::current_process_id(),
  48                os::current_thread_id());
  49 
  50     yes = os::message_box("Unexpected Error", buf);
  51 
  52     if (yes) {
  53       // yes, user asked VM to launch debugger
  54       jio_snprintf(buf, buflen, "gdb /proc/%d/exe %d",
  55                    os::current_process_id(), os::current_process_id());
  56 
  57       os::fork_and_exec(buf);
  58       yes = false;
  59     }
  60   } while (yes);
  61 }
  62 
  63 // Space for our "saved" signal flags and handlers
  64 static int resettedSigflags[2];
  65 static address resettedSighandler[2];
  66 
  67 static void save_signal(int idx, int sig)
  68 {
  69   struct sigaction sa;
  70   sigaction(sig, NULL, &sa);
  71   resettedSigflags[idx]   = sa.sa_flags;
  72   resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
  73                               ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  74                               : CAST_FROM_FN_PTR(address, sa.sa_handler);
  75 }
  76 
  77 int VMError::get_resetted_sigflags(int sig) {
  78   if(SIGSEGV == sig) {
  79     return resettedSigflags[0];
  80   } else if(SIGBUS == sig) {
  81     return resettedSigflags[1];
  82   }
  83   return -1;
  84 }
  85 
  86 address VMError::get_resetted_sighandler(int sig) {
  87   if(SIGSEGV == sig) {
  88     return resettedSighandler[0];
  89   } else if(SIGBUS == sig) {
  90     return resettedSighandler[1];
  91   }
  92   return NULL;
  93 }
  94 
  95 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
  96   // unmask current signal
  97   sigset_t newset;
  98   sigemptyset(&newset);
  99   sigaddset(&newset, sig);
 100   sigprocmask(SIG_UNBLOCK, &newset, NULL);
 101 
 102   VMError err(NULL, sig, NULL, info, ucVoid);
 103   err.report_and_die();
 104 }
 105 
 106 void VMError::reset_signal_handlers() {
 107   // Save sigflags for resetted signals
 108   save_signal(0, SIGSEGV);
 109   save_signal(1, SIGBUS);
 110   os::signal(SIGSEGV, CAST_FROM_FN_PTR(void *, crash_handler));
 111   os::signal(SIGBUS, CAST_FROM_FN_PTR(void *, crash_handler));
 112 }