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 <sys/syscall.h> 34 #include <unistd.h> 35 #include <signal.h> 36 37 void VMError::show_message_box(char *buf, int buflen) { 38 bool yes; 39 do { 40 error_string(buf, buflen); 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 '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 static const int SIGNALS[] = { SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP }; // add more if needed 70 static const int NUM_SIGNALS = sizeof(SIGNALS) / sizeof(int); 71 72 // Space for our "saved" signal flags and handlers 73 static int resettedSigflags[NUM_SIGNALS]; 74 static address resettedSighandler[NUM_SIGNALS]; 75 76 static void save_signal(int idx, int sig) 77 { 78 struct sigaction sa; 79 sigaction(sig, NULL, &sa); 80 resettedSigflags[idx] = sa.sa_flags; 81 resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO) 82 ? CAST_FROM_FN_PTR(address, sa.sa_sigaction) 83 : CAST_FROM_FN_PTR(address, sa.sa_handler); 84 } 85 86 int VMError::get_resetted_sigflags(int sig) { 87 for (int i = 0; i < NUM_SIGNALS; i++) { 88 if (SIGNALS[i] == sig) { 89 return resettedSigflags[i]; 90 } 91 } 92 return -1; 93 } 94 95 address VMError::get_resetted_sighandler(int sig) { 96 for (int i = 0; i < NUM_SIGNALS; i++) { 97 if (SIGNALS[i] == sig) { 98 return resettedSighandler[i]; 99 } 100 } 101 return NULL; 102 } 103 104 static void crash_handler(int sig, siginfo_t* info, void* ucVoid) { 105 // unmask current signal 106 sigset_t newset; 107 sigemptyset(&newset); 108 sigaddset(&newset, sig); 109 // also unmask other synchronous signals 110 for (int i = 0; i < NUM_SIGNALS; i++) { 111 sigaddset(&newset, SIGNALS[i]); 112 } 113 pthread_sigmask(SIG_UNBLOCK, &newset, NULL); 114 115 VMError err(NULL, sig, NULL, info, ucVoid); 116 err.report_and_die(); 117 } 118 119 void VMError::reset_signal_handlers() { 120 // install signal handlers for all synchronous program error signals 121 sigset_t newset; 122 sigemptyset(&newset); 123 124 for (int i = 0; i < NUM_SIGNALS; i++) { 125 save_signal(i, SIGNALS[i]); 126 os::signal(SIGNALS[i], CAST_FROM_FN_PTR(void *, crash_handler)); 127 sigaddset(&newset, SIGNALS[i]); 128 } 129 pthread_sigmask(SIG_UNBLOCK, &newset, NULL); 130 }