duke@435: /* never@3499: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "compiler/compileBroker.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" mgerdin@3619: #include "prims/whitebox.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/frame.inline.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "runtime/thread.hpp" stefank@2314: #include "runtime/vmThread.hpp" stefank@2314: #include "runtime/vm_operations.hpp" zgu@3900: #include "services/memTracker.hpp" stefank@2314: #include "utilities/debug.hpp" zgu@2364: #include "utilities/decoder.hpp" stefank@2314: #include "utilities/defaultStream.hpp" kamg@2515: #include "utilities/errorReporter.hpp" never@3499: #include "utilities/events.hpp" stefank@2314: #include "utilities/top.hpp" stefank@2314: #include "utilities/vmError.hpp" duke@435: duke@435: // List of environment variables that should be reported in error log file. duke@435: const char *env_list[] = { duke@435: // All platforms duke@435: "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH", duke@435: "JAVA_COMPILER", "PATH", "USERNAME", duke@435: never@3156: // Env variables that are defined on Solaris/Linux/BSD duke@435: "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY", duke@435: "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE", duke@435: duke@435: // defined on Linux duke@435: "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM", duke@435: never@3156: // defined on Darwin never@3156: "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH", never@3156: "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH", never@3156: "DYLD_INSERT_LIBRARIES", never@3156: duke@435: // defined on Windows duke@435: "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR", duke@435: duke@435: (const char *)0 duke@435: }; duke@435: duke@435: // Fatal error handler for internal errors and crashes. duke@435: // duke@435: // The default behavior of fatal error handler is to print a brief message duke@435: // to standard out (defaultStream::output_fd()), then save detailed information duke@435: // into an error report file (hs_err_pid.log) and abort VM. If multiple duke@435: // threads are having troubles at the same time, only one error is reported. duke@435: // The thread that is reporting error will abort VM when it is done, all other duke@435: // threads are blocked forever inside report_and_die(). duke@435: duke@435: // Constructor for crashes coleenp@2418: VMError::VMError(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) { duke@435: _thread = thread; duke@435: _id = sig; duke@435: _pc = pc; duke@435: _siginfo = siginfo; duke@435: _context = context; duke@435: duke@435: _verbose = false; duke@435: _current_step = 0; duke@435: _current_step_info = NULL; duke@435: jcoomes@1845: _message = NULL; jcoomes@1845: _detail_msg = NULL; duke@435: _filename = NULL; duke@435: _lineno = 0; duke@435: duke@435: _size = 0; duke@435: } duke@435: duke@435: // Constructor for internal errors jcoomes@1845: VMError::VMError(Thread* thread, const char* filename, int lineno, jcoomes@1845: const char* message, const char * detail_msg) jcoomes@1845: { jcoomes@1845: _thread = thread; jcoomes@1845: _id = internal_error; // Value that's not an OS exception/signal jcoomes@1845: _filename = filename; jcoomes@1845: _lineno = lineno; jcoomes@1845: _message = message; jcoomes@1845: _detail_msg = detail_msg; jcoomes@1845: jcoomes@1845: _verbose = false; jcoomes@1845: _current_step = 0; jcoomes@1845: _current_step_info = NULL; jcoomes@1845: jcoomes@1845: _pc = NULL; jcoomes@1845: _siginfo = NULL; jcoomes@1845: _context = NULL; jcoomes@1845: jcoomes@1845: _size = 0; jcoomes@1845: } jcoomes@1845: jcoomes@1845: // Constructor for OOM errors jcoomes@1845: VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size, jcoomes@1845: const char* message) { duke@435: _thread = thread; jcoomes@1845: _id = oom_error; // Value that's not an OS exception/signal duke@435: _filename = filename; duke@435: _lineno = lineno; duke@435: _message = message; jcoomes@1845: _detail_msg = NULL; duke@435: duke@435: _verbose = false; duke@435: _current_step = 0; duke@435: _current_step_info = NULL; duke@435: duke@435: _pc = NULL; duke@435: _siginfo = NULL; duke@435: _context = NULL; duke@435: duke@435: _size = size; duke@435: } duke@435: duke@435: duke@435: // Constructor for non-fatal errors duke@435: VMError::VMError(const char* message) { duke@435: _thread = NULL; jcoomes@1845: _id = internal_error; // Value that's not an OS exception/signal duke@435: _filename = NULL; duke@435: _lineno = 0; duke@435: _message = message; jcoomes@1845: _detail_msg = NULL; duke@435: duke@435: _verbose = false; duke@435: _current_step = 0; duke@435: _current_step_info = NULL; duke@435: duke@435: _pc = NULL; duke@435: _siginfo = NULL; duke@435: _context = NULL; duke@435: duke@435: _size = 0; duke@435: } duke@435: duke@435: // -XX:OnError=, where can be a list of commands, separated duke@435: // by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by duke@435: // a single "%". Some examples: duke@435: // duke@435: // -XX:OnError="pmap %p" // show memory map duke@435: // -XX:OnError="gcore %p; dbx - %p" // dump core and launch debugger duke@435: // -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com" duke@435: // -XX:OnError="kill -9 %p" // ?#!@# duke@435: duke@435: // A simple parser for -XX:OnError, usage: duke@435: // ptr = OnError; duke@435: // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL) duke@435: // ... ... duke@435: static char* next_OnError_command(char* buf, int buflen, const char** ptr) { duke@435: if (ptr == NULL || *ptr == NULL) return NULL; duke@435: duke@435: const char* cmd = *ptr; duke@435: duke@435: // skip leading blanks or ';' duke@435: while (*cmd == ' ' || *cmd == ';') cmd++; duke@435: duke@435: if (*cmd == '\0') return NULL; duke@435: duke@435: const char * cmdend = cmd; duke@435: while (*cmdend != '\0' && *cmdend != ';') cmdend++; duke@435: duke@435: Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen); duke@435: duke@435: *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1); duke@435: return buf; duke@435: } duke@435: duke@435: duke@435: static void print_bug_submit_message(outputStream *out, Thread *thread) { duke@435: if (out == NULL) return; duke@435: out->print_raw_cr("# If you would like to submit a bug report, please visit:"); duke@435: out->print_raw ("# "); duke@435: out->print_raw_cr(Arguments::java_vendor_url_bug()); duke@435: // If the crash is in native code, encourage user to submit a bug to the duke@435: // provider of that code. coleenp@491: if (thread && thread->is_Java_thread() && coleenp@491: !thread->is_hidden_from_external_view()) { duke@435: JavaThread* jt = (JavaThread*)thread; duke@435: if (jt->thread_state() == _thread_in_native) { duke@435: out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug."); duke@435: } duke@435: } duke@435: out->print_raw_cr("#"); duke@435: } duke@435: ctornqvi@2520: bool VMError::coredump_status; ctornqvi@2520: char VMError::coredump_message[O_BUFLEN]; ctornqvi@2520: ctornqvi@2520: void VMError::report_coredump_status(const char* message, bool status) { ctornqvi@2520: coredump_status = status; ctornqvi@2520: strncpy(coredump_message, message, sizeof(coredump_message)); ctornqvi@2520: coredump_message[sizeof(coredump_message)-1] = 0; ctornqvi@2520: } ctornqvi@2520: duke@435: duke@435: // Return a string to describe the error duke@435: char* VMError::error_string(char* buf, int buflen) { duke@435: char signame_buf[64]; duke@435: const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf)); duke@435: duke@435: if (signame) { duke@435: jio_snprintf(buf, buflen, duke@435: "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT, duke@435: signame, _id, _pc, duke@435: os::current_process_id(), os::current_thread_id()); jcoomes@1845: } else if (_filename != NULL && _lineno > 0) { jcoomes@1845: // skip directory names jcoomes@1845: char separator = os::file_separator()[0]; jcoomes@1845: const char *p = strrchr(_filename, separator); jcoomes@1845: int n = jio_snprintf(buf, buflen, jcoomes@1845: "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT, jcoomes@1845: p ? p + 1 : _filename, _lineno, jcoomes@1845: os::current_process_id(), os::current_thread_id()); jcoomes@1845: if (n >= 0 && n < buflen && _message) { jcoomes@1845: if (_detail_msg) { jcoomes@1845: jio_snprintf(buf + n, buflen - n, "%s%s: %s", jcoomes@1845: os::line_separator(), _message, _detail_msg); jcoomes@1845: } else { jcoomes@1845: jio_snprintf(buf + n, buflen - n, "%sError: %s", jcoomes@1845: os::line_separator(), _message); jcoomes@1845: } jcoomes@1845: } duke@435: } else { jcoomes@1845: jio_snprintf(buf, buflen, jcoomes@1845: "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT, jcoomes@1845: _id, os::current_process_id(), os::current_thread_id()); duke@435: } duke@435: duke@435: return buf; duke@435: } duke@435: twisti@1819: void VMError::print_stack_trace(outputStream* st, JavaThread* jt, twisti@1819: char* buf, int buflen, bool verbose) { twisti@1819: #ifdef ZERO twisti@1819: if (jt->zero_stack()->sp() && jt->top_zero_frame()) { twisti@1819: // StackFrameStream uses the frame anchor, which may not have twisti@1819: // been set up. This can be done at any time in Zero, however, twisti@1819: // so if it hasn't been set up then we just set it up now and twisti@1819: // clear it again when we're done. twisti@1819: bool has_last_Java_frame = jt->has_last_Java_frame(); twisti@1819: if (!has_last_Java_frame) twisti@1819: jt->set_last_Java_frame(); twisti@1819: st->print("Java frames:"); twisti@1819: twisti@1819: // If the top frame is a Shark frame and the frame anchor isn't twisti@1819: // set up then it's possible that the information in the frame twisti@1819: // is garbage: it could be from a previous decache, or it could twisti@1819: // simply have never been written. So we print a warning... twisti@1819: StackFrameStream sfs(jt); twisti@1819: if (!has_last_Java_frame && !sfs.is_done()) { twisti@1819: if (sfs.current()->zeroframe()->is_shark_frame()) { twisti@1819: st->print(" (TOP FRAME MAY BE JUNK)"); twisti@1819: } twisti@1819: } twisti@1819: st->cr(); twisti@1819: twisti@1819: // Print the frames twisti@1819: for(int i = 0; !sfs.is_done(); sfs.next(), i++) { twisti@1819: sfs.current()->zero_print_on_error(i, st, buf, buflen); twisti@1819: st->cr(); twisti@1819: } twisti@1819: twisti@1819: // Reset the frame anchor if necessary twisti@1819: if (!has_last_Java_frame) twisti@1819: jt->reset_last_Java_frame(); twisti@1819: } twisti@1819: #else twisti@1819: if (jt->has_last_Java_frame()) { twisti@1819: st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)"); twisti@1819: for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) { twisti@1819: sfs.current()->print_on_error(st, buf, buflen, verbose); twisti@1819: st->cr(); twisti@1819: } twisti@1819: } twisti@1819: #endif // ZERO twisti@1819: } duke@435: duke@435: // This is the main function to report a fatal error. Only one thread can duke@435: // call this function, so we don't need to worry about MT-safety. But it's duke@435: // possible that the error handler itself may crash or die on an internal duke@435: // error, for example, when the stack/heap is badly damaged. We must be duke@435: // able to handle recursive errors that happen inside error handler. duke@435: // duke@435: // Error reporting is done in several steps. If a crash or internal error duke@435: // occurred when reporting an error, the nested signal/exception handler duke@435: // can skip steps that are already (or partially) done. Error reporting will duke@435: // continue from the next step. This allows us to retrieve and print duke@435: // information that may be unsafe to get after a fatal error. If it happens, duke@435: // you may find nested report_and_die() frames when you look at the stack duke@435: // in a debugger. duke@435: // duke@435: // In general, a hang in error handler is much worse than a crash or internal duke@435: // error, as it's harder to recover from a hang. Deadlock can happen if we duke@435: // try to grab a lock that is already owned by current thread, or if the duke@435: // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the duke@435: // error handler and all the functions it called should avoid grabbing any duke@435: // lock. An important thing to notice is that memory allocation needs a lock. duke@435: // duke@435: // We should avoid using large stack allocated buffers. Many errors happen duke@435: // when stack space is already low. Making things even worse is that there duke@435: // could be nested report_and_die() calls on stack (see above). Only one duke@435: // thread can report error, so large buffers are statically allocated in data duke@435: // segment. duke@435: duke@435: void VMError::report(outputStream* st) { duke@435: # define BEGIN if (_current_step == 0) { _current_step = 1; duke@435: # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s; duke@435: # define END } duke@435: duke@435: // don't allocate large buffer on stack duke@435: static char buf[O_BUFLEN]; duke@435: duke@435: BEGIN duke@435: coleenp@491: STEP(10, "(printing fatal error message)") duke@435: coleenp@2418: st->print_cr("#"); coleenp@2418: if (should_report_bug(_id)) { coleenp@2418: st->print_cr("# A fatal error has been detected by the Java Runtime Environment:"); coleenp@2418: } else { coleenp@2418: st->print_cr("# There is insufficient memory for the Java " coleenp@2418: "Runtime Environment to continue."); coleenp@2418: } duke@435: duke@435: STEP(15, "(printing type of error)") duke@435: duke@435: switch(_id) { duke@435: case oom_error: duke@435: if (_size) { coleenp@2418: st->print("# Native memory allocation (malloc) failed to allocate "); coleenp@2418: jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size); duke@435: st->print(buf); duke@435: st->print(" bytes"); duke@435: if (_message != NULL) { duke@435: st->print(" for "); duke@435: st->print(_message); duke@435: } coleenp@2418: st->cr(); duke@435: } else { duke@435: if (_message != NULL) coleenp@2418: st->print("# "); duke@435: st->print_cr(_message); duke@435: } coleenp@2418: // In error file give some solutions coleenp@2418: if (_verbose) { coleenp@2418: st->print_cr("# Possible reasons:"); coleenp@2418: st->print_cr("# The system is out of physical RAM or swap space"); coleenp@2418: st->print_cr("# In 32 bit mode, the process size limit was hit"); coleenp@2418: st->print_cr("# Possible solutions:"); coleenp@2418: st->print_cr("# Reduce memory load on the system"); coleenp@2418: st->print_cr("# Increase physical memory or swap space"); coleenp@2418: st->print_cr("# Check if swap backing store is full"); coleenp@2418: st->print_cr("# Use 64 bit Java on a 64 bit OS"); coleenp@2418: st->print_cr("# Decrease Java heap size (-Xmx/-Xms)"); coleenp@2418: st->print_cr("# Decrease number of Java threads"); coleenp@2418: st->print_cr("# Decrease Java thread stack sizes (-Xss)"); coleenp@2418: st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize="); coleenp@2418: st->print_cr("# This output file may be truncated or incomplete."); coleenp@2418: } else { coleenp@2418: return; // that's enough for the screen coleenp@2418: } duke@435: break; duke@435: case internal_error: duke@435: default: duke@435: break; duke@435: } duke@435: duke@435: STEP(20, "(printing exception/signal name)") duke@435: duke@435: st->print_cr("#"); duke@435: st->print("# "); duke@435: // Is it an OS exception/signal? duke@435: if (os::exception_name(_id, buf, sizeof(buf))) { duke@435: st->print("%s", buf); duke@435: st->print(" (0x%x)", _id); // signal number duke@435: st->print(" at pc=" PTR_FORMAT, _pc); duke@435: } else { coleenp@2418: if (should_report_bug(_id)) { coleenp@2418: st->print("Internal Error"); coleenp@2418: } else { coleenp@2418: st->print("Out of Memory Error"); coleenp@2418: } duke@435: if (_filename != NULL && _lineno > 0) { duke@435: #ifdef PRODUCT duke@435: // In product mode chop off pathname? duke@435: char separator = os::file_separator()[0]; duke@435: const char *p = strrchr(_filename, separator); duke@435: const char *file = p ? p+1 : _filename; duke@435: #else duke@435: const char *file = _filename; duke@435: #endif duke@435: size_t len = strlen(file); duke@435: size_t buflen = sizeof(buf); duke@435: duke@435: strncpy(buf, file, buflen); duke@435: if (len + 10 < buflen) { twisti@1038: sprintf(buf + len, ":%d", _lineno); duke@435: } duke@435: st->print(" (%s)", buf); duke@435: } else { duke@435: st->print(" (0x%x)", _id); duke@435: } duke@435: } duke@435: duke@435: STEP(30, "(printing current thread and pid)") duke@435: duke@435: // process id, thread id duke@435: st->print(", pid=%d", os::current_process_id()); duke@435: st->print(", tid=" UINTX_FORMAT, os::current_thread_id()); duke@435: st->cr(); duke@435: duke@435: STEP(40, "(printing error message)") duke@435: coleenp@2418: if (should_report_bug(_id)) { // already printed the message. coleenp@2418: // error message coleenp@2418: if (_detail_msg) { coleenp@2418: st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg); coleenp@2418: } else if (_message) { coleenp@2418: st->print_cr("# Error: %s", _message); coleenp@2418: } coleenp@2418: } duke@435: duke@435: STEP(50, "(printing Java version string)") duke@435: duke@435: // VM version duke@435: st->print_cr("#"); coleenp@908: JDK_Version::current().to_string(buf, sizeof(buf)); twisti@3884: const char* runtime_name = JDK_Version::runtime_name() != NULL ? twisti@3884: JDK_Version::runtime_name() : ""; sla@4232: const char* runtime_version = JDK_Version::runtime_version() != NULL ? sla@4232: JDK_Version::runtime_version() : ""; sla@4232: st->print_cr("# JRE version: %s (%s) (build %s)", runtime_name, buf, runtime_version); coleenp@548: st->print_cr("# Java VM: %s (%s %s %s %s)", duke@435: Abstract_VM_Version::vm_name(), duke@435: Abstract_VM_Version::vm_release(), duke@435: Abstract_VM_Version::vm_info_string(), coleenp@548: Abstract_VM_Version::vm_platform_string(), coleenp@548: UseCompressedOops ? "compressed oops" : "" duke@435: ); duke@435: duke@435: STEP(60, "(printing problematic frame)") duke@435: duke@435: // Print current frame if we have a context (i.e. it's a crash) duke@435: if (_context) { duke@435: st->print_cr("# Problematic frame:"); duke@435: st->print("# "); duke@435: frame fr = os::fetch_frame_from_context(_context); duke@435: fr.print_on_error(st, buf, sizeof(buf)); duke@435: st->cr(); duke@435: st->print_cr("#"); duke@435: } ctornqvi@2520: STEP(63, "(printing core file information)") ctornqvi@2520: st->print("# "); ctornqvi@2520: if (coredump_status) { ctornqvi@2520: st->print("Core dump written. Default location: %s", coredump_message); ctornqvi@2520: } else { ctornqvi@2520: st->print("Failed to write core dump. %s", coredump_message); ctornqvi@2520: } ctornqvi@2520: st->print_cr(""); ctornqvi@2520: st->print_cr("#"); duke@435: duke@435: STEP(65, "(printing bug submit message)") duke@435: coleenp@2418: if (should_report_bug(_id) && _verbose) { coleenp@2418: print_bug_submit_message(st, _thread); coleenp@2418: } duke@435: duke@435: STEP(70, "(printing thread)" ) duke@435: duke@435: if (_verbose) { duke@435: st->cr(); duke@435: st->print_cr("--------------- T H R E A D ---------------"); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(80, "(printing current thread)" ) duke@435: duke@435: // current thread duke@435: if (_verbose) { duke@435: if (_thread) { duke@435: st->print("Current thread (" PTR_FORMAT "): ", _thread); duke@435: _thread->print_on_error(st, buf, sizeof(buf)); duke@435: st->cr(); duke@435: } else { duke@435: st->print_cr("Current thread is native thread"); duke@435: } duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(90, "(printing siginfo)" ) duke@435: duke@435: // signal no, signal code, address that caused the fault duke@435: if (_verbose && _siginfo) { duke@435: os::print_siginfo(st, _siginfo); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(100, "(printing registers, top of stack, instructions near pc)") duke@435: duke@435: // registers, top of stack, instructions near pc duke@435: if (_verbose && _context) { duke@435: os::print_context(st, _context); duke@435: st->cr(); duke@435: } duke@435: never@2262: STEP(105, "(printing register info)") never@2262: never@2262: // decode register contents if possible never@2262: if (_verbose && _context && Universe::is_fully_initialized()) { never@2262: os::print_register_info(st, _context); never@2262: st->cr(); never@2262: } never@2262: duke@435: STEP(110, "(printing stack bounds)" ) duke@435: duke@435: if (_verbose) { duke@435: st->print("Stack: "); duke@435: duke@435: address stack_top; duke@435: size_t stack_size; duke@435: duke@435: if (_thread) { duke@435: stack_top = _thread->stack_base(); duke@435: stack_size = _thread->stack_size(); duke@435: } else { duke@435: stack_top = os::current_stack_base(); duke@435: stack_size = os::current_stack_size(); duke@435: } duke@435: duke@435: address stack_bottom = stack_top - stack_size; duke@435: st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top); duke@435: duke@435: frame fr = _context ? os::fetch_frame_from_context(_context) duke@435: : os::current_frame(); duke@435: duke@435: if (fr.sp()) { duke@435: st->print(", sp=" PTR_FORMAT, fr.sp()); kvn@2039: size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024); kvn@2039: st->print(", free space=" SIZE_FORMAT "k", free_stack_size); duke@435: } duke@435: duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(120, "(printing native stack)" ) duke@435: duke@435: if (_verbose) { duke@435: frame fr = _context ? os::fetch_frame_from_context(_context) duke@435: : os::current_frame(); duke@435: duke@435: // see if it's a valid frame duke@435: if (fr.pc()) { duke@435: st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)"); duke@435: zgu@2364: duke@435: int count = 0; duke@435: while (count++ < StackPrintLimit) { duke@435: fr.print_on_error(st, buf, sizeof(buf)); duke@435: st->cr(); duke@435: if (os::is_first_C_frame(&fr)) break; duke@435: fr = os::get_sender_for_C_frame(&fr); duke@435: } duke@435: duke@435: if (count > StackPrintLimit) { duke@435: st->print_cr("......"); duke@435: } duke@435: duke@435: st->cr(); duke@435: } duke@435: } duke@435: duke@435: STEP(130, "(printing Java stack)" ) duke@435: duke@435: if (_verbose && _thread && _thread->is_Java_thread()) { twisti@1819: print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf)); duke@435: } duke@435: minqi@1554: STEP(135, "(printing target Java thread stack)" ) minqi@1554: minqi@1554: // printing Java thread stack trace if it is involved in GC crash never@2262: if (_verbose && _thread && (_thread->is_Named_thread())) { minqi@1554: JavaThread* jt = ((NamedThread *)_thread)->processed_thread(); minqi@1554: if (jt != NULL) { minqi@1554: st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id()); twisti@1819: print_stack_trace(st, jt, buf, sizeof(buf), true); minqi@1554: } minqi@1554: } minqi@1554: duke@435: STEP(140, "(printing VM operation)" ) duke@435: duke@435: if (_verbose && _thread && _thread->is_VM_thread()) { duke@435: VMThread* t = (VMThread*)_thread; duke@435: VM_Operation* op = t->vm_operation(); duke@435: if (op) { duke@435: op->print_on_error(st); duke@435: st->cr(); duke@435: st->cr(); duke@435: } duke@435: } duke@435: duke@435: STEP(150, "(printing current compile task)" ) duke@435: duke@435: if (_verbose && _thread && _thread->is_Compiler_thread()) { duke@435: CompilerThread* t = (CompilerThread*)_thread; duke@435: if (t->task()) { duke@435: st->cr(); duke@435: st->print_cr("Current CompileTask:"); duke@435: t->task()->print_line_on_error(st, buf, sizeof(buf)); duke@435: st->cr(); duke@435: } duke@435: } duke@435: duke@435: STEP(160, "(printing process)" ) duke@435: duke@435: if (_verbose) { duke@435: st->cr(); duke@435: st->print_cr("--------------- P R O C E S S ---------------"); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(170, "(printing all threads)" ) duke@435: duke@435: // all threads duke@435: if (_verbose && _thread) { duke@435: Threads::print_on_error(st, _thread, buf, sizeof(buf)); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(175, "(printing VM state)" ) duke@435: duke@435: if (_verbose) { duke@435: // Safepoint state duke@435: st->print("VM state:"); duke@435: duke@435: if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing"); duke@435: else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint"); duke@435: else st->print("not at safepoint"); duke@435: duke@435: // Also see if error occurred during initialization or shutdown duke@435: if (!Universe::is_fully_initialized()) { duke@435: st->print(" (not fully initialized)"); duke@435: } else if (VM_Exit::vm_exited()) { duke@435: st->print(" (shutting down)"); duke@435: } else { duke@435: st->print(" (normal execution)"); duke@435: } duke@435: st->cr(); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(180, "(printing owned locks on error)" ) duke@435: duke@435: // mutexes/monitors that currently have an owner duke@435: if (_verbose) { duke@435: print_owned_locks_on_error(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(190, "(printing heap information)" ) duke@435: duke@435: if (_verbose && Universe::is_fully_initialized()) { stefank@4904: Universe::heap()->print_on_error(st); never@3687: st->cr(); never@3687: never@3687: st->print_cr("Polling page: " INTPTR_FORMAT, os::get_polling_page()); never@3687: st->cr(); duke@435: } duke@435: never@2262: STEP(195, "(printing code cache information)" ) never@2262: never@2262: if (_verbose && Universe::is_fully_initialized()) { never@2262: // print code cache information before vm abort vladidan@4438: CodeCache::print_summary(st); never@2262: st->cr(); never@2262: } never@2262: never@3499: STEP(200, "(printing ring buffers)" ) never@3499: never@3499: if (_verbose) { never@3499: Events::print_all(st); never@3499: st->cr(); never@3499: } never@3499: never@3499: STEP(205, "(printing dynamic libraries)" ) duke@435: duke@435: if (_verbose) { duke@435: // dynamic libraries, or memory map duke@435: os::print_dll_info(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(210, "(printing VM options)" ) duke@435: duke@435: if (_verbose) { duke@435: // VM options duke@435: Arguments::print_on(st); duke@435: st->cr(); duke@435: } duke@435: mgerdin@3619: STEP(215, "(printing warning if internal testing API used)" ) mgerdin@3619: mgerdin@3619: if (WhiteBox::used()) { mgerdin@3619: st->print_cr("Unsupported internal testing APIs have been used."); mgerdin@3619: st->cr(); mgerdin@3619: } mgerdin@3619: duke@435: STEP(220, "(printing environment variables)" ) duke@435: duke@435: if (_verbose) { duke@435: os::print_environment_variables(st, env_list, buf, sizeof(buf)); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(225, "(printing signal handlers)" ) duke@435: duke@435: if (_verbose) { duke@435: os::print_signal_handlers(st, buf, sizeof(buf)); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(230, "" ) duke@435: duke@435: if (_verbose) { duke@435: st->cr(); duke@435: st->print_cr("--------------- S Y S T E M ---------------"); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(240, "(printing OS information)" ) duke@435: duke@435: if (_verbose) { duke@435: os::print_os_info(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(250, "(printing CPU info)" ) duke@435: if (_verbose) { duke@435: os::print_cpu_info(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(260, "(printing memory info)" ) duke@435: duke@435: if (_verbose) { duke@435: os::print_memory_info(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(270, "(printing internal vm info)" ) duke@435: duke@435: if (_verbose) { duke@435: st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string()); duke@435: st->cr(); duke@435: } duke@435: duke@435: STEP(280, "(printing date and time)" ) duke@435: duke@435: if (_verbose) { duke@435: os::print_date_and_time(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: END duke@435: duke@435: # undef BEGIN duke@435: # undef STEP duke@435: # undef END duke@435: } duke@435: bobv@2036: VMError* volatile VMError::first_error = NULL; bobv@2036: volatile jlong VMError::first_error_tid = -1; duke@435: vlivanov@5027: /** Expand a pattern into a buffer starting at pos and open a file using constructed path */ vlivanov@5027: static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) { vlivanov@5027: int fd = -1; vlivanov@5027: if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) { vlivanov@5027: fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666); vlivanov@5027: } vlivanov@5027: return fd; vlivanov@5027: } vlivanov@5027: vlivanov@5027: /** vlivanov@5027: * Construct file name for a log file and return it's file descriptor. vlivanov@5027: * Name and location depends on pattern, default_pattern params and access vlivanov@5027: * permissions. vlivanov@5027: */ vlivanov@5027: static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) { vlivanov@5027: int fd = -1; vlivanov@5027: vlivanov@5027: // If possible, use specified pattern to construct log file name vlivanov@5027: if (pattern != NULL) { vlivanov@5027: fd = expand_and_open(pattern, buf, buflen, 0); vlivanov@5027: } vlivanov@5027: vlivanov@5027: // Either user didn't specify, or the user's location failed, vlivanov@5027: // so use the default name in the current directory vlivanov@5027: if (fd == -1) { vlivanov@5027: const char* cwd = os::get_current_directory(buf, buflen); vlivanov@5027: if (cwd != NULL) { vlivanov@5027: size_t pos = strlen(cwd); vlivanov@5027: int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator()); vlivanov@5027: pos += fsep_len; vlivanov@5027: if (fsep_len > 0) { vlivanov@5027: fd = expand_and_open(default_pattern, buf, buflen, pos); vlivanov@5027: } vlivanov@5027: } vlivanov@5027: } vlivanov@5027: vlivanov@5027: // try temp directory if it exists. vlivanov@5027: if (fd == -1) { vlivanov@5027: const char* tmpdir = os::get_temp_directory(); vlivanov@5027: if (tmpdir != NULL && strlen(tmpdir) > 0) { vlivanov@5027: int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator()); vlivanov@5027: if (pos > 0) { vlivanov@5027: fd = expand_and_open(default_pattern, buf, buflen, pos); vlivanov@5027: } vlivanov@5027: } vlivanov@5027: } vlivanov@5027: vlivanov@5027: return fd; vlivanov@5027: } vlivanov@5027: duke@435: void VMError::report_and_die() { duke@435: // Don't allocate large buffer on stack duke@435: static char buffer[O_BUFLEN]; duke@435: duke@435: // An error could happen before tty is initialized or after it has been duke@435: // destroyed. Here we use a very simple unbuffered fdStream for printing. duke@435: // Only out.print_raw() and out.print_raw_cr() should be used, as other duke@435: // printing methods need to allocate large buffer on stack. To format a duke@435: // string, use jio_snprintf() with a static buffer or use staticBufferStream. duke@435: static fdStream out(defaultStream::output_fd()); duke@435: duke@435: // How many errors occurred in error handler when reporting first_error. duke@435: static int recursive_error_count; duke@435: duke@435: // We will first print a brief message to standard out (verbose = false), duke@435: // then save detailed information in log file (verbose = true). duke@435: static bool out_done = false; // done printing to standard out duke@435: static bool log_done = false; // done saving error log kamg@2515: static bool transmit_report_done = false; // done error reporting duke@435: static fdStream log; // error log duke@435: zgu@3900: // disble NMT to avoid further exception zgu@3900: MemTracker::shutdown(MemTracker::NMT_error_reporting); zgu@3900: duke@435: if (SuppressFatalErrorMessage) { duke@435: os::abort(); duke@435: } duke@435: jlong mytid = os::current_thread_id(); duke@435: if (first_error == NULL && duke@435: Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) { duke@435: duke@435: // first time duke@435: first_error_tid = mytid; duke@435: set_error_reported(); duke@435: sla@2584: if (ShowMessageBoxOnError || PauseAtExit) { duke@435: show_message_box(buffer, sizeof(buffer)); duke@435: duke@435: // User has asked JVM to abort. Reset ShowMessageBoxOnError so the duke@435: // WatcherThread can kill JVM if the error handler hangs. duke@435: ShowMessageBoxOnError = false; duke@435: } duke@435: ctornqvi@2520: // Write a minidump on Windows, check core dump limits on Linux/Solaris ctornqvi@2520: os::check_or_create_dump(_siginfo, _context, buffer, sizeof(buffer)); ctornqvi@2520: duke@435: // reset signal handlers or exception filter; make sure recursive crashes duke@435: // are handled properly. duke@435: reset_signal_handlers(); duke@435: duke@435: } else { coleenp@946: // If UseOsErrorReporting we call this for each level of the call stack coleenp@946: // while searching for the exception handler. Only the first level needs coleenp@946: // to be reported. coleenp@946: if (UseOSErrorReporting && log_done) return; coleenp@946: duke@435: // This is not the first error, see if it happened in a different thread duke@435: // or in the same thread during error reporting. duke@435: if (first_error_tid != mytid) { duke@435: jio_snprintf(buffer, sizeof(buffer), duke@435: "[thread " INT64_FORMAT " also had an error]", duke@435: mytid); duke@435: out.print_raw_cr(buffer); duke@435: duke@435: // error reporting is not MT-safe, block current thread duke@435: os::infinite_sleep(); duke@435: duke@435: } else { duke@435: if (recursive_error_count++ > 30) { duke@435: out.print_raw_cr("[Too many errors, abort]"); duke@435: os::die(); duke@435: } duke@435: duke@435: jio_snprintf(buffer, sizeof(buffer), duke@435: "[error occurred during error reporting %s, id 0x%x]", duke@435: first_error ? first_error->_current_step_info : "", duke@435: _id); duke@435: if (log.is_open()) { duke@435: log.cr(); duke@435: log.print_raw_cr(buffer); duke@435: log.cr(); duke@435: } else { duke@435: out.cr(); duke@435: out.print_raw_cr(buffer); duke@435: out.cr(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // print to screen duke@435: if (!out_done) { duke@435: first_error->_verbose = false; duke@435: duke@435: staticBufferStream sbs(buffer, sizeof(buffer), &out); duke@435: first_error->report(&sbs); duke@435: duke@435: out_done = true; duke@435: duke@435: first_error->_current_step = 0; // reset current_step duke@435: first_error->_current_step_info = ""; // reset current_step string duke@435: } duke@435: duke@435: // print to error log file duke@435: if (!log_done) { duke@435: first_error->_verbose = true; duke@435: duke@435: // see if log file is already open duke@435: if (!log.is_open()) { duke@435: // open log file vlivanov@5027: int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer)); duke@435: if (fd != -1) { duke@435: out.print_raw("# An error report file with more information is saved as:\n# "); duke@435: out.print_raw_cr(buffer); duke@435: os::set_error_file(buffer); duke@435: duke@435: log.set_fd(fd); duke@435: } else { duke@435: out.print_raw_cr("# Can not save log file, dump to screen.."); duke@435: log.set_fd(defaultStream::output_fd()); kamg@2515: /* Error reporting currently needs dumpfile. kamg@2515: * Maybe implement direct streaming in the future.*/ kamg@2515: transmit_report_done = true; duke@435: } duke@435: } duke@435: duke@435: staticBufferStream sbs(buffer, O_BUFLEN, &log); duke@435: first_error->report(&sbs); duke@435: first_error->_current_step = 0; // reset current_step duke@435: first_error->_current_step_info = ""; // reset current_step string duke@435: kamg@2515: // Run error reporting to determine whether or not to report the crash. kamg@2515: if (!transmit_report_done && should_report_bug(first_error->_id)) { kamg@2515: transmit_report_done = true; vlivanov@5027: FILE* hs_err = os::open(log.fd(), "r"); kamg@2515: if (NULL != hs_err) { kamg@2515: ErrorReporter er; kamg@2515: er.call(hs_err, buffer, O_BUFLEN); kamg@2515: } kamg@2515: } kamg@2515: duke@435: if (log.fd() != defaultStream::output_fd()) { duke@435: close(log.fd()); duke@435: } duke@435: duke@435: log.set_fd(-1); duke@435: log_done = true; duke@435: } duke@435: duke@435: duke@435: static bool skip_OnError = false; duke@435: if (!skip_OnError && OnError && OnError[0]) { duke@435: skip_OnError = true; duke@435: duke@435: out.print_raw_cr("#"); duke@435: out.print_raw ("# -XX:OnError=\""); duke@435: out.print_raw (OnError); duke@435: out.print_raw_cr("\""); duke@435: duke@435: char* cmd; duke@435: const char* ptr = OnError; duke@435: while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ duke@435: out.print_raw ("# Executing "); never@3156: #if defined(LINUX) || defined(_ALLBSD_SOURCE) duke@435: out.print_raw ("/bin/sh -c "); duke@435: #elif defined(SOLARIS) duke@435: out.print_raw ("/usr/bin/sh -c "); duke@435: #endif duke@435: out.print_raw ("\""); duke@435: out.print_raw (cmd); duke@435: out.print_raw_cr("\" ..."); duke@435: duke@435: os::fork_and_exec(cmd); duke@435: } duke@435: duke@435: // done with OnError duke@435: OnError = NULL; duke@435: } duke@435: minqi@4267: static bool skip_replay = false; minqi@4267: if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) { minqi@4267: skip_replay = true; minqi@4267: ciEnv* env = ciEnv::current(); minqi@4267: if (env != NULL) { vlivanov@5027: int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer)); vlivanov@5027: if (fd != -1) { vlivanov@5027: FILE* replay_data_file = os::open(fd, "w"); vlivanov@5027: if (replay_data_file != NULL) { vlivanov@5027: fileStream replay_data_stream(replay_data_file, /*need_close=*/true); vlivanov@5027: env->dump_replay_data(&replay_data_stream); vlivanov@5027: out.print_raw("#\n# Compiler replay data is saved as:\n# "); vlivanov@5027: out.print_raw_cr(buffer); vlivanov@5027: } else { vlivanov@5027: out.print_raw("#\n# Can't open file to dump replay data. Error: "); vlivanov@5027: out.print_raw_cr(strerror(os::get_last_error())); vlivanov@5027: } vlivanov@5027: } minqi@4267: } minqi@4267: } minqi@4267: coleenp@2418: static bool skip_bug_url = !should_report_bug(first_error->_id); duke@435: if (!skip_bug_url) { duke@435: skip_bug_url = true; duke@435: duke@435: out.print_raw_cr("#"); duke@435: print_bug_submit_message(&out, _thread); duke@435: } duke@435: duke@435: if (!UseOSErrorReporting) { duke@435: // os::abort() will call abort hooks, try it first. duke@435: static bool skip_os_abort = false; duke@435: if (!skip_os_abort) { duke@435: skip_os_abort = true; coleenp@2418: bool dump_core = should_report_bug(first_error->_id); coleenp@2418: os::abort(dump_core); duke@435: } duke@435: duke@435: // if os::abort() doesn't abort, try os::die(); duke@435: os::die(); duke@435: } duke@435: } duke@435: duke@435: /* duke@435: * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this duke@435: * ensures utilities such as jmap can observe the process is a consistent state. duke@435: */ duke@435: class VM_ReportJavaOutOfMemory : public VM_Operation { duke@435: private: duke@435: VMError *_err; duke@435: public: duke@435: VM_ReportJavaOutOfMemory(VMError *err) { _err = err; } duke@435: VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; } duke@435: void doit(); duke@435: }; duke@435: duke@435: void VM_ReportJavaOutOfMemory::doit() { duke@435: // Don't allocate large buffer on stack duke@435: static char buffer[O_BUFLEN]; duke@435: duke@435: tty->print_cr("#"); duke@435: tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message()); duke@435: tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError); duke@435: duke@435: // make heap parsability duke@435: Universe::heap()->ensure_parsability(false); // no need to retire TLABs duke@435: duke@435: char* cmd; duke@435: const char* ptr = OnOutOfMemoryError; duke@435: while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ duke@435: tty->print("# Executing "); duke@435: #if defined(LINUX) duke@435: tty->print ("/bin/sh -c "); duke@435: #elif defined(SOLARIS) duke@435: tty->print ("/usr/bin/sh -c "); duke@435: #endif duke@435: tty->print_cr("\"%s\"...", cmd); duke@435: duke@435: os::fork_and_exec(cmd); duke@435: } duke@435: } duke@435: duke@435: void VMError::report_java_out_of_memory() { duke@435: if (OnOutOfMemoryError && OnOutOfMemoryError[0]) { duke@435: MutexLocker ml(Heap_lock); duke@435: VM_ReportJavaOutOfMemory op(this); duke@435: VMThread::execute(&op); duke@435: } duke@435: }