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