2796N/A/*
2796N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2796N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2796N/A *
2796N/A * This code is free software; you can redistribute it and/or modify it
2796N/A * under the terms of the GNU General Public License version 2 only, as
2796N/A * published by the Free Software Foundation.
2796N/A *
2796N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2796N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2796N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2796N/A * version 2 for more details (a copy is included in the LICENSE file that
2796N/A * accompanied this code).
2796N/A *
2796N/A * You should have received a copy of the GNU General Public License version
2796N/A * 2 along with this work; if not, write to the Free Software Foundation,
2796N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2796N/A *
2796N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2796N/A * or visit www.oracle.com if you need additional information or have any
2796N/A * questions.
2796N/A *
2796N/A */
2796N/A
2796N/A#include "precompiled.hpp"
2796N/A#include "runtime/arguments.hpp"
2796N/A#include "runtime/os.hpp"
2796N/A#include "runtime/thread.hpp"
2796N/A#include "utilities/vmError.hpp"
2796N/A
2796N/A#include <sys/types.h>
2796N/A#include <sys/wait.h>
2796N/A#include <sys/syscall.h>
2796N/A#include <unistd.h>
2796N/A#include <signal.h>
2796N/A
2796N/Avoid VMError::show_message_box(char *buf, int buflen) {
2796N/A bool yes;
2796N/A do {
2796N/A error_string(buf, buflen);
2796N/A int len = (int)strlen(buf);
2796N/A char *p = &buf[len];
2796N/A
2796N/A jio_snprintf(p, buflen - len,
2796N/A "\n\n"
2796N/A "Do you want to debug the problem?\n\n"
2796N/A "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " INTX_FORMAT " (" INTPTR_FORMAT ")\n"
2796N/A "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n"
2796N/A "Otherwise, press RETURN to abort...",
2796N/A os::current_process_id(), os::current_process_id(),
2796N/A os::current_thread_id(), os::current_thread_id());
2796N/A
2796N/A yes = os::message_box("Unexpected Error", buf);
2796N/A
2796N/A if (yes) {
2796N/A // yes, user asked VM to launch debugger
2796N/A jio_snprintf(buf, buflen, "gdb /proc/%d/exe %d",
2796N/A os::current_process_id(), os::current_process_id());
2796N/A
2796N/A os::fork_and_exec(buf);
2796N/A yes = false;
2796N/A }
2796N/A } while (yes);
2796N/A}
2796N/A
2796N/A// Space for our "saved" signal flags and handlers
2796N/Astatic int resettedSigflags[2];
2796N/Astatic address resettedSighandler[2];
2796N/A
2796N/Astatic void save_signal(int idx, int sig)
2796N/A{
2796N/A struct sigaction sa;
2796N/A sigaction(sig, NULL, &sa);
2796N/A resettedSigflags[idx] = sa.sa_flags;
2796N/A resettedSighandler[idx] = (sa.sa_flags & SA_SIGINFO)
2796N/A ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
2796N/A : CAST_FROM_FN_PTR(address, sa.sa_handler);
2796N/A}
2796N/A
2796N/Aint VMError::get_resetted_sigflags(int sig) {
2796N/A if(SIGSEGV == sig) {
2796N/A return resettedSigflags[0];
2796N/A } else if(SIGBUS == sig) {
2796N/A return resettedSigflags[1];
2796N/A }
2796N/A return -1;
2796N/A}
2796N/A
2796N/Aaddress VMError::get_resetted_sighandler(int sig) {
2796N/A if(SIGSEGV == sig) {
2796N/A return resettedSighandler[0];
2796N/A } else if(SIGBUS == sig) {
2796N/A return resettedSighandler[1];
2796N/A }
2796N/A return NULL;
2796N/A}
2796N/A
2796N/Astatic void crash_handler(int sig, siginfo_t* info, void* ucVoid) {
2796N/A // unmask current signal
2796N/A sigset_t newset;
2796N/A sigemptyset(&newset);
2796N/A sigaddset(&newset, sig);
2796N/A sigprocmask(SIG_UNBLOCK, &newset, NULL);
2796N/A
2796N/A VMError err(NULL, sig, NULL, info, ucVoid);
2796N/A err.report_and_die();
2796N/A}
2796N/A
2796N/Avoid VMError::reset_signal_handlers() {
2796N/A // Save sigflags for resetted signals
2796N/A save_signal(0, SIGSEGV);
2796N/A save_signal(1, SIGBUS);
2796N/A os::signal(SIGSEGV, CAST_FROM_FN_PTR(void *, crash_handler));
2796N/A os::signal(SIGBUS, CAST_FROM_FN_PTR(void *, crash_handler));
2796N/A}