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