src/share/vm/utilities/vmError.cpp
author coleenp
Wed Jan 12 13:59:18 2011 -0800 (2 years ago)
changeset 1999 2c8e1acf0433
parent 19952ecd0d1194d2
child 2412a541ca8fa0e3
permissions -rw-r--r--
7009828: Fix for 6938627 breaks visualvm monitoring when -Djava.io.tmpdir is defined
Summary: Change get_temp_directory() back to /tmp and %TEMP% like it always was and where the tools expect it to be.
Reviewed-by: phh, dcubed, kamg, alanb
duke@0
/*
trims@1772
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
duke@0
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0
 *
duke@0
 * This code is free software; you can redistribute it and/or modify it
duke@0
 * under the terms of the GNU General Public License version 2 only, as
duke@0
 * published by the Free Software Foundation.
duke@0
 *
duke@0
 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
duke@0
 * version 2 for more details (a copy is included in the LICENSE file that
duke@0
 * accompanied this code).
duke@0
 *
duke@0
 * You should have received a copy of the GNU General Public License version
duke@0
 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0
 *
trims@1772
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1772
 * or visit www.oracle.com if you need additional information or have any
trims@1772
 * questions.
duke@0
 *
duke@0
 */
duke@0
duke@0
# include "incls/_precompiled.incl"
duke@0
# include "incls/_vmError.cpp.incl"
duke@0
duke@0
// List of environment variables that should be reported in error log file.
duke@0
const char *env_list[] = {
duke@0
  // All platforms
duke@0
  "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
duke@0
  "JAVA_COMPILER", "PATH", "USERNAME",
duke@0
duke@0
  // Env variables that are defined on Solaris/Linux
duke@0
  "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
duke@0
  "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
duke@0
duke@0
  // defined on Linux
duke@0
  "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
duke@0
duke@0
  // defined on Windows
duke@0
  "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
duke@0
duke@0
  (const char *)0
duke@0
};
duke@0
duke@0
// Fatal error handler for internal errors and crashes.
duke@0
//
duke@0
// The default behavior of fatal error handler is to print a brief message
duke@0
// to standard out (defaultStream::output_fd()), then save detailed information
duke@0
// into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
duke@0
// threads are having troubles at the same time, only one error is reported.
duke@0
// The thread that is reporting error will abort VM when it is done, all other
duke@0
// threads are blocked forever inside report_and_die().
duke@0
duke@0
// Constructor for crashes
duke@0
VMError::VMError(Thread* thread, int sig, address pc, void* siginfo, void* context) {
duke@0
    _thread = thread;
duke@0
    _id = sig;
duke@0
    _pc   = pc;
duke@0
    _siginfo = siginfo;
duke@0
    _context = context;
duke@0
duke@0
    _verbose = false;
duke@0
    _current_step = 0;
duke@0
    _current_step_info = NULL;
duke@0
jcoomes@1700
    _message = NULL;
jcoomes@1700
    _detail_msg = NULL;
duke@0
    _filename = NULL;
duke@0
    _lineno = 0;
duke@0
duke@0
    _size = 0;
duke@0
}
duke@0
duke@0
// Constructor for internal errors
jcoomes@1700
VMError::VMError(Thread* thread, const char* filename, int lineno,
jcoomes@1700
                 const char* message, const char * detail_msg)
jcoomes@1700
{
jcoomes@1700
  _thread = thread;
jcoomes@1700
  _id = internal_error;     // Value that's not an OS exception/signal
jcoomes@1700
  _filename = filename;
jcoomes@1700
  _lineno = lineno;
jcoomes@1700
  _message = message;
jcoomes@1700
  _detail_msg = detail_msg;
jcoomes@1700
jcoomes@1700
  _verbose = false;
jcoomes@1700
  _current_step = 0;
jcoomes@1700
  _current_step_info = NULL;
jcoomes@1700
jcoomes@1700
  _pc = NULL;
jcoomes@1700
  _siginfo = NULL;
jcoomes@1700
  _context = NULL;
jcoomes@1700
jcoomes@1700
  _size = 0;
jcoomes@1700
}
jcoomes@1700
jcoomes@1700
// Constructor for OOM errors
jcoomes@1700
VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size,
jcoomes@1700
                 const char* message) {
duke@0
    _thread = thread;
jcoomes@1700
    _id = oom_error;     // Value that's not an OS exception/signal
duke@0
    _filename = filename;
duke@0
    _lineno = lineno;
duke@0
    _message = message;
jcoomes@1700
    _detail_msg = NULL;
duke@0
duke@0
    _verbose = false;
duke@0
    _current_step = 0;
duke@0
    _current_step_info = NULL;
duke@0
duke@0
    _pc = NULL;
duke@0
    _siginfo = NULL;
duke@0
    _context = NULL;
duke@0
duke@0
    _size = size;
duke@0
}
duke@0
duke@0
duke@0
// Constructor for non-fatal errors
duke@0
VMError::VMError(const char* message) {
duke@0
    _thread = NULL;
jcoomes@1700
    _id = internal_error;     // Value that's not an OS exception/signal
duke@0
    _filename = NULL;
duke@0
    _lineno = 0;
duke@0
    _message = message;
jcoomes@1700
    _detail_msg = NULL;
duke@0
duke@0
    _verbose = false;
duke@0
    _current_step = 0;
duke@0
    _current_step_info = NULL;
duke@0
duke@0
    _pc = NULL;
duke@0
    _siginfo = NULL;
duke@0
    _context = NULL;
duke@0
duke@0
    _size = 0;
duke@0
}
duke@0
duke@0
// -XX:OnError=<string>, where <string> can be a list of commands, separated
duke@0
// by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
duke@0
// a single "%". Some examples:
duke@0
//
duke@0
// -XX:OnError="pmap %p"                // show memory map
duke@0
// -XX:OnError="gcore %p; dbx - %p"     // dump core and launch debugger
duke@0
// -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
duke@0
// -XX:OnError="kill -9 %p"             // ?#!@#
duke@0
duke@0
// A simple parser for -XX:OnError, usage:
duke@0
//  ptr = OnError;
duke@0
//  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
duke@0
//     ... ...
duke@0
static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
duke@0
  if (ptr == NULL || *ptr == NULL) return NULL;
duke@0
duke@0
  const char* cmd = *ptr;
duke@0
duke@0
  // skip leading blanks or ';'
duke@0
  while (*cmd == ' ' || *cmd == ';') cmd++;
duke@0
duke@0
  if (*cmd == '\0') return NULL;
duke@0
duke@0
  const char * cmdend = cmd;
duke@0
  while (*cmdend != '\0' && *cmdend != ';') cmdend++;
duke@0
duke@0
  Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
duke@0
duke@0
  *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
duke@0
  return buf;
duke@0
}
duke@0
duke@0
duke@0
static void print_bug_submit_message(outputStream *out, Thread *thread) {
duke@0
  if (out == NULL) return;
duke@0
  out->print_raw_cr("# If you would like to submit a bug report, please visit:");
duke@0
  out->print_raw   ("#   ");
duke@0
  out->print_raw_cr(Arguments::java_vendor_url_bug());
duke@0
  // If the crash is in native code, encourage user to submit a bug to the
duke@0
  // provider of that code.
coleenp@114
  if (thread && thread->is_Java_thread() &&
coleenp@114
      !thread->is_hidden_from_external_view()) {
duke@0
    JavaThread* jt = (JavaThread*)thread;
duke@0
    if (jt->thread_state() == _thread_in_native) {
duke@0
      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@0
    }
duke@0
  }
duke@0
  out->print_raw_cr("#");
duke@0
}
duke@0
duke@0
duke@0
// Return a string to describe the error
duke@0
char* VMError::error_string(char* buf, int buflen) {
duke@0
  char signame_buf[64];
duke@0
  const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
duke@0
duke@0
  if (signame) {
duke@0
    jio_snprintf(buf, buflen,
duke@0
                 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
duke@0
                 signame, _id, _pc,
duke@0
                 os::current_process_id(), os::current_thread_id());
jcoomes@1700
  } else if (_filename != NULL && _lineno > 0) {
jcoomes@1700
    // skip directory names
jcoomes@1700
    char separator = os::file_separator()[0];
jcoomes@1700
    const char *p = strrchr(_filename, separator);
jcoomes@1700
    int n = jio_snprintf(buf, buflen,
jcoomes@1700
                         "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
jcoomes@1700
                         p ? p + 1 : _filename, _lineno,
jcoomes@1700
                         os::current_process_id(), os::current_thread_id());
jcoomes@1700
    if (n >= 0 && n < buflen && _message) {
jcoomes@1700
      if (_detail_msg) {
jcoomes@1700
        jio_snprintf(buf + n, buflen - n, "%s%s: %s",
jcoomes@1700
                     os::line_separator(), _message, _detail_msg);
jcoomes@1700
      } else {
jcoomes@1700
        jio_snprintf(buf + n, buflen - n, "%sError: %s",
jcoomes@1700
                     os::line_separator(), _message);
jcoomes@1700
      }
jcoomes@1700
    }
duke@0
  } else {
jcoomes@1700
    jio_snprintf(buf, buflen,
jcoomes@1700
                 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
jcoomes@1700
                 _id, os::current_process_id(), os::current_thread_id());
duke@0
  }
duke@0
duke@0
  return buf;
duke@0
}
duke@0
twisti@1682
void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
twisti@1682
                                char* buf, int buflen, bool verbose) {
twisti@1682
#ifdef ZERO
twisti@1682
  if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
twisti@1682
    // StackFrameStream uses the frame anchor, which may not have
twisti@1682
    // been set up.  This can be done at any time in Zero, however,
twisti@1682
    // so if it hasn't been set up then we just set it up now and
twisti@1682
    // clear it again when we're done.
twisti@1682
    bool has_last_Java_frame = jt->has_last_Java_frame();
twisti@1682
    if (!has_last_Java_frame)
twisti@1682
      jt->set_last_Java_frame();
twisti@1682
    st->print("Java frames:");
twisti@1682
twisti@1682
    // If the top frame is a Shark frame and the frame anchor isn't
twisti@1682
    // set up then it's possible that the information in the frame
twisti@1682
    // is garbage: it could be from a previous decache, or it could
twisti@1682
    // simply have never been written.  So we print a warning...
twisti@1682
    StackFrameStream sfs(jt);
twisti@1682
    if (!has_last_Java_frame && !sfs.is_done()) {
twisti@1682
      if (sfs.current()->zeroframe()->is_shark_frame()) {
twisti@1682
        st->print(" (TOP FRAME MAY BE JUNK)");
twisti@1682
      }
twisti@1682
    }
twisti@1682
    st->cr();
twisti@1682
twisti@1682
    // Print the frames
twisti@1682
    for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
twisti@1682
      sfs.current()->zero_print_on_error(i, st, buf, buflen);
twisti@1682
      st->cr();
twisti@1682
    }
twisti@1682
twisti@1682
    // Reset the frame anchor if necessary
twisti@1682
    if (!has_last_Java_frame)
twisti@1682
      jt->reset_last_Java_frame();
twisti@1682
  }
twisti@1682
#else
twisti@1682
  if (jt->has_last_Java_frame()) {
twisti@1682
    st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
twisti@1682
    for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
twisti@1682
      sfs.current()->print_on_error(st, buf, buflen, verbose);
twisti@1682
      st->cr();
twisti@1682
    }
twisti@1682
  }
twisti@1682
#endif // ZERO
twisti@1682
}
duke@0
duke@0
// This is the main function to report a fatal error. Only one thread can
duke@0
// call this function, so we don't need to worry about MT-safety. But it's
duke@0
// possible that the error handler itself may crash or die on an internal
duke@0
// error, for example, when the stack/heap is badly damaged. We must be
duke@0
// able to handle recursive errors that happen inside error handler.
duke@0
//
duke@0
// Error reporting is done in several steps. If a crash or internal error
duke@0
// occurred when reporting an error, the nested signal/exception handler
duke@0
// can skip steps that are already (or partially) done. Error reporting will
duke@0
// continue from the next step. This allows us to retrieve and print
duke@0
// information that may be unsafe to get after a fatal error. If it happens,
duke@0
// you may find nested report_and_die() frames when you look at the stack
duke@0
// in a debugger.
duke@0
//
duke@0
// In general, a hang in error handler is much worse than a crash or internal
duke@0
// error, as it's harder to recover from a hang. Deadlock can happen if we
duke@0
// try to grab a lock that is already owned by current thread, or if the
duke@0
// owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
duke@0
// error handler and all the functions it called should avoid grabbing any
duke@0
// lock. An important thing to notice is that memory allocation needs a lock.
duke@0
//
duke@0
// We should avoid using large stack allocated buffers. Many errors happen
duke@0
// when stack space is already low. Making things even worse is that there
duke@0
// could be nested report_and_die() calls on stack (see above). Only one
duke@0
// thread can report error, so large buffers are statically allocated in data
duke@0
// segment.
duke@0
duke@0
void VMError::report(outputStream* st) {
duke@0
# define BEGIN if (_current_step == 0) { _current_step = 1;
duke@0
# define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
duke@0
# define END }
duke@0
duke@0
  // don't allocate large buffer on stack
duke@0
  static char buf[O_BUFLEN];
duke@0
duke@0
  BEGIN
duke@0
coleenp@114
  STEP(10, "(printing fatal error message)")
duke@0
duke@0
     st->print_cr("#");
coleenp@114
     st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
duke@0
duke@0
  STEP(15, "(printing type of error)")
duke@0
duke@0
     switch(_id) {
duke@0
       case oom_error:
duke@0
         st->print_cr("#");
duke@0
         st->print("# java.lang.OutOfMemoryError: ");
duke@0
         if (_size) {
duke@0
           st->print("requested ");
kvn@478
           sprintf(buf,SIZE_FORMAT,_size);
duke@0
           st->print(buf);
duke@0
           st->print(" bytes");
duke@0
           if (_message != NULL) {
duke@0
             st->print(" for ");
duke@0
             st->print(_message);
duke@0
           }
duke@0
           st->print_cr(". Out of swap space?");
duke@0
         } else {
duke@0
           if (_message != NULL)
duke@0
             st->print_cr(_message);
duke@0
         }
duke@0
         break;
duke@0
       case internal_error:
duke@0
       default:
duke@0
         break;
duke@0
     }
duke@0
duke@0
  STEP(20, "(printing exception/signal name)")
duke@0
duke@0
     st->print_cr("#");
duke@0
     st->print("#  ");
duke@0
     // Is it an OS exception/signal?
duke@0
     if (os::exception_name(_id, buf, sizeof(buf))) {
duke@0
       st->print("%s", buf);
duke@0
       st->print(" (0x%x)", _id);                // signal number
duke@0
       st->print(" at pc=" PTR_FORMAT, _pc);
duke@0
     } else {
duke@0
       st->print("Internal Error");
duke@0
       if (_filename != NULL && _lineno > 0) {
duke@0
#ifdef PRODUCT
duke@0
         // In product mode chop off pathname?
duke@0
         char separator = os::file_separator()[0];
duke@0
         const char *p = strrchr(_filename, separator);
duke@0
         const char *file = p ? p+1 : _filename;
duke@0
#else
duke@0
         const char *file = _filename;
duke@0
#endif
duke@0
         size_t len = strlen(file);
duke@0
         size_t buflen = sizeof(buf);
duke@0
duke@0
         strncpy(buf, file, buflen);
duke@0
         if (len + 10 < buflen) {
twisti@734
           sprintf(buf + len, ":%d", _lineno);
duke@0
         }
duke@0
         st->print(" (%s)", buf);
duke@0
       } else {
duke@0
         st->print(" (0x%x)", _id);
duke@0
       }
duke@0
     }
duke@0
duke@0
  STEP(30, "(printing current thread and pid)")
duke@0
duke@0
     // process id, thread id
duke@0
     st->print(", pid=%d", os::current_process_id());
duke@0
     st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
duke@0
     st->cr();
duke@0
duke@0
  STEP(40, "(printing error message)")
duke@0
duke@0
     // error message
jcoomes@1700
     if (_detail_msg) {
jcoomes@1700
       st->print_cr("#  %s: %s", _message ? _message : "Error", _detail_msg);
jcoomes@1700
     } else if (_message) {
duke@0
       st->print_cr("#  Error: %s", _message);
duke@0
     }
duke@0
duke@0
  STEP(50, "(printing Java version string)")
duke@0
duke@0
     // VM version
duke@0
     st->print_cr("#");
coleenp@529
     JDK_Version::current().to_string(buf, sizeof(buf));
coleenp@529
     st->print_cr("# JRE version: %s", buf);
coleenp@170
     st->print_cr("# Java VM: %s (%s %s %s %s)",
duke@0
                   Abstract_VM_Version::vm_name(),
duke@0
                   Abstract_VM_Version::vm_release(),
duke@0
                   Abstract_VM_Version::vm_info_string(),
coleenp@170
                   Abstract_VM_Version::vm_platform_string(),
coleenp@170
                   UseCompressedOops ? "compressed oops" : ""
duke@0
                 );
duke@0
duke@0
  STEP(60, "(printing problematic frame)")
duke@0
duke@0
     // Print current frame if we have a context (i.e. it's a crash)
duke@0
     if (_context) {
duke@0
       st->print_cr("# Problematic frame:");
duke@0
       st->print("# ");
duke@0
       frame fr = os::fetch_frame_from_context(_context);
duke@0
       fr.print_on_error(st, buf, sizeof(buf));
duke@0
       st->cr();
duke@0
       st->print_cr("#");
duke@0
     }
duke@0
duke@0
  STEP(65, "(printing bug submit message)")
duke@0
duke@0
     if (_verbose) print_bug_submit_message(st, _thread);
duke@0
duke@0
  STEP(70, "(printing thread)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       st->cr();
duke@0
       st->print_cr("---------------  T H R E A D  ---------------");
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(80, "(printing current thread)" )
duke@0
duke@0
     // current thread
duke@0
     if (_verbose) {
duke@0
       if (_thread) {
duke@0
         st->print("Current thread (" PTR_FORMAT "):  ", _thread);
duke@0
         _thread->print_on_error(st, buf, sizeof(buf));
duke@0
         st->cr();
duke@0
       } else {
duke@0
         st->print_cr("Current thread is native thread");
duke@0
       }
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(90, "(printing siginfo)" )
duke@0
duke@0
     // signal no, signal code, address that caused the fault
duke@0
     if (_verbose && _siginfo) {
duke@0
       os::print_siginfo(st, _siginfo);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(100, "(printing registers, top of stack, instructions near pc)")
duke@0
duke@0
     // registers, top of stack, instructions near pc
duke@0
     if (_verbose && _context) {
duke@0
       os::print_context(st, _context);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(110, "(printing stack bounds)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       st->print("Stack: ");
duke@0
duke@0
       address stack_top;
duke@0
       size_t stack_size;
duke@0
duke@0
       if (_thread) {
duke@0
          stack_top = _thread->stack_base();
duke@0
          stack_size = _thread->stack_size();
duke@0
       } else {
duke@0
          stack_top = os::current_stack_base();
duke@0
          stack_size = os::current_stack_size();
duke@0
       }
duke@0
duke@0
       address stack_bottom = stack_top - stack_size;
duke@0
       st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
duke@0
duke@0
       frame fr = _context ? os::fetch_frame_from_context(_context)
duke@0
                           : os::current_frame();
duke@0
duke@0
       if (fr.sp()) {
duke@0
         st->print(",  sp=" PTR_FORMAT, fr.sp());
kvn@1895
         size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
kvn@1895
         st->print(",  free space=" SIZE_FORMAT "k", free_stack_size);
duke@0
       }
duke@0
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(120, "(printing native stack)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       frame fr = _context ? os::fetch_frame_from_context(_context)
duke@0
                           : os::current_frame();
duke@0
duke@0
       // see if it's a valid frame
duke@0
       if (fr.pc()) {
duke@0
          st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
duke@0
duke@0
          int count = 0;
duke@0
duke@0
          while (count++ < StackPrintLimit) {
duke@0
             fr.print_on_error(st, buf, sizeof(buf));
duke@0
             st->cr();
duke@0
             if (os::is_first_C_frame(&fr)) break;
duke@0
             fr = os::get_sender_for_C_frame(&fr);
duke@0
          }
duke@0
duke@0
          if (count > StackPrintLimit) {
duke@0
             st->print_cr("...<more frames>...");
duke@0
          }
duke@0
duke@0
          st->cr();
duke@0
       }
duke@0
     }
duke@0
duke@0
  STEP(130, "(printing Java stack)" )
duke@0
duke@0
     if (_verbose && _thread && _thread->is_Java_thread()) {
twisti@1682
       print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
duke@0
     }
duke@0
minqi@1350
  STEP(135, "(printing target Java thread stack)" )
minqi@1350
minqi@1350
     // printing Java thread stack trace if it is involved in GC crash
minqi@1990
     if (_verbose && _thread && (_thread->is_Named_thread())) {
minqi@1350
       JavaThread*  jt = ((NamedThread *)_thread)->processed_thread();
minqi@1350
       if (jt != NULL) {
minqi@1350
         st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
twisti@1682
         print_stack_trace(st, jt, buf, sizeof(buf), true);
minqi@1350
       }
minqi@1350
     }
minqi@1350
duke@0
  STEP(140, "(printing VM operation)" )
duke@0
duke@0
     if (_verbose && _thread && _thread->is_VM_thread()) {
duke@0
        VMThread* t = (VMThread*)_thread;
duke@0
        VM_Operation* op = t->vm_operation();
duke@0
        if (op) {
duke@0
          op->print_on_error(st);
duke@0
          st->cr();
duke@0
          st->cr();
duke@0
        }
duke@0
     }
duke@0
duke@0
  STEP(150, "(printing current compile task)" )
duke@0
duke@0
     if (_verbose && _thread && _thread->is_Compiler_thread()) {
duke@0
        CompilerThread* t = (CompilerThread*)_thread;
duke@0
        if (t->task()) {
duke@0
           st->cr();
duke@0
           st->print_cr("Current CompileTask:");
duke@0
           t->task()->print_line_on_error(st, buf, sizeof(buf));
duke@0
           st->cr();
duke@0
        }
duke@0
     }
duke@0
duke@0
  STEP(160, "(printing process)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       st->cr();
duke@0
       st->print_cr("---------------  P R O C E S S  ---------------");
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(170, "(printing all threads)" )
duke@0
duke@0
     // all threads
duke@0
     if (_verbose && _thread) {
duke@0
       Threads::print_on_error(st, _thread, buf, sizeof(buf));
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(175, "(printing VM state)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       // Safepoint state
duke@0
       st->print("VM state:");
duke@0
duke@0
       if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
duke@0
       else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
duke@0
       else st->print("not at safepoint");
duke@0
duke@0
       // Also see if error occurred during initialization or shutdown
duke@0
       if (!Universe::is_fully_initialized()) {
duke@0
         st->print(" (not fully initialized)");
duke@0
       } else if (VM_Exit::vm_exited()) {
duke@0
         st->print(" (shutting down)");
duke@0
       } else {
duke@0
         st->print(" (normal execution)");
duke@0
       }
duke@0
       st->cr();
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(180, "(printing owned locks on error)" )
duke@0
duke@0
     // mutexes/monitors that currently have an owner
duke@0
     if (_verbose) {
duke@0
       print_owned_locks_on_error(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(190, "(printing heap information)" )
duke@0
duke@0
     if (_verbose && Universe::is_fully_initialized()) {
duke@0
       // print heap information before vm abort
duke@0
       Universe::print_on(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(200, "(printing dynamic libraries)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       // dynamic libraries, or memory map
duke@0
       os::print_dll_info(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(210, "(printing VM options)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       // VM options
duke@0
       Arguments::print_on(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(220, "(printing environment variables)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       os::print_environment_variables(st, env_list, buf, sizeof(buf));
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(225, "(printing signal handlers)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       os::print_signal_handlers(st, buf, sizeof(buf));
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(230, "" )
duke@0
duke@0
     if (_verbose) {
duke@0
       st->cr();
duke@0
       st->print_cr("---------------  S Y S T E M  ---------------");
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(240, "(printing OS information)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       os::print_os_info(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(250, "(printing CPU info)" )
duke@0
     if (_verbose) {
duke@0
       os::print_cpu_info(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(260, "(printing memory info)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       os::print_memory_info(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(270, "(printing internal vm info)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  STEP(280, "(printing date and time)" )
duke@0
duke@0
     if (_verbose) {
duke@0
       os::print_date_and_time(st);
duke@0
       st->cr();
duke@0
     }
duke@0
duke@0
  END
duke@0
duke@0
# undef BEGIN
duke@0
# undef STEP
duke@0
# undef END
duke@0
}
duke@0
bobv@1892
VMError* volatile VMError::first_error = NULL;
bobv@1892
volatile jlong VMError::first_error_tid = -1;
duke@0
duke@0
void VMError::report_and_die() {
duke@0
  // Don't allocate large buffer on stack
duke@0
  static char buffer[O_BUFLEN];
duke@0
duke@0
  // An error could happen before tty is initialized or after it has been
duke@0
  // destroyed. Here we use a very simple unbuffered fdStream for printing.
duke@0
  // Only out.print_raw() and out.print_raw_cr() should be used, as other
duke@0
  // printing methods need to allocate large buffer on stack. To format a
duke@0
  // string, use jio_snprintf() with a static buffer or use staticBufferStream.
duke@0
  static fdStream out(defaultStream::output_fd());
duke@0
duke@0
  // How many errors occurred in error handler when reporting first_error.
duke@0
  static int recursive_error_count;
duke@0
duke@0
  // We will first print a brief message to standard out (verbose = false),
duke@0
  // then save detailed information in log file (verbose = true).
duke@0
  static bool out_done = false;         // done printing to standard out
duke@0
  static bool log_done = false;         // done saving error log
duke@0
  static fdStream log;                  // error log
duke@0
duke@0
  if (SuppressFatalErrorMessage) {
duke@0
      os::abort();
duke@0
  }
duke@0
  jlong mytid = os::current_thread_id();
duke@0
  if (first_error == NULL &&
duke@0
      Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
duke@0
duke@0
    // first time
duke@0
    first_error_tid = mytid;
duke@0
    set_error_reported();
duke@0
duke@0
    if (ShowMessageBoxOnError) {
duke@0
      show_message_box(buffer, sizeof(buffer));
duke@0
duke@0
      // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
duke@0
      // WatcherThread can kill JVM if the error handler hangs.
duke@0
      ShowMessageBoxOnError = false;
duke@0
    }
duke@0
duke@0
    // reset signal handlers or exception filter; make sure recursive crashes
duke@0
    // are handled properly.
duke@0
    reset_signal_handlers();
duke@0
duke@0
  } else {
coleenp@641
    // If UseOsErrorReporting we call this for each level of the call stack
coleenp@641
    // while searching for the exception handler.  Only the first level needs
coleenp@641
    // to be reported.
coleenp@641
    if (UseOSErrorReporting && log_done) return;
coleenp@641
duke@0
    // This is not the first error, see if it happened in a different thread
duke@0
    // or in the same thread during error reporting.
duke@0
    if (first_error_tid != mytid) {
duke@0
      jio_snprintf(buffer, sizeof(buffer),
duke@0
                   "[thread " INT64_FORMAT " also had an error]",
duke@0
                   mytid);
duke@0
      out.print_raw_cr(buffer);
duke@0
duke@0
      // error reporting is not MT-safe, block current thread
duke@0
      os::infinite_sleep();
duke@0
duke@0
    } else {
duke@0
      if (recursive_error_count++ > 30) {
duke@0
        out.print_raw_cr("[Too many errors, abort]");
duke@0
        os::die();
duke@0
      }
duke@0
duke@0
      jio_snprintf(buffer, sizeof(buffer),
duke@0
                   "[error occurred during error reporting %s, id 0x%x]",
duke@0
                   first_error ? first_error->_current_step_info : "",
duke@0
                   _id);
duke@0
      if (log.is_open()) {
duke@0
        log.cr();
duke@0
        log.print_raw_cr(buffer);
duke@0
        log.cr();
duke@0
      } else {
duke@0
        out.cr();
duke@0
        out.print_raw_cr(buffer);
duke@0
        out.cr();
duke@0
      }
duke@0
    }
duke@0
  }
duke@0
duke@0
  // print to screen
duke@0
  if (!out_done) {
duke@0
    first_error->_verbose = false;
duke@0
duke@0
    staticBufferStream sbs(buffer, sizeof(buffer), &out);
duke@0
    first_error->report(&sbs);
duke@0
duke@0
    out_done = true;
duke@0
duke@0
    first_error->_current_step = 0;         // reset current_step
duke@0
    first_error->_current_step_info = "";   // reset current_step string
duke@0
  }
duke@0
duke@0
  // print to error log file
duke@0
  if (!log_done) {
duke@0
    first_error->_verbose = true;
duke@0
duke@0
    // see if log file is already open
duke@0
    if (!log.is_open()) {
duke@0
      // open log file
duke@0
      int fd = -1;
duke@0
duke@0
      if (ErrorFile != NULL) {
duke@0
        bool copy_ok =
duke@0
          Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
duke@0
        if (copy_ok) {
duke@0
          fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
duke@0
        }
duke@0
      }
duke@0
duke@0
      if (fd == -1) {
duke@0
        const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
duke@0
        size_t len = strlen(cwd);
duke@0
        // either user didn't specify, or the user's location failed,
duke@0
        // so use the default name in the current directory
duke@0
        jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
duke@0
                     os::file_separator(), os::current_process_id());
duke@0
        fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
duke@0
      }
duke@0
duke@0
      if (fd == -1) {
duke@0
        const char * tmpdir = os::get_temp_directory();
coleenp@1999
        // try temp directory if it exists.
coleenp@1999
        if (tmpdir != NULL && tmpdir[0] != '\0') {
coleenp@1999
          jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
coleenp@1999
                       tmpdir, os::file_separator(), os::current_process_id());
coleenp@1999
          fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
coleenp@1999
        }
duke@0
      }
duke@0
duke@0
      if (fd != -1) {
duke@0
        out.print_raw("# An error report file with more information is saved as:\n# ");
duke@0
        out.print_raw_cr(buffer);
duke@0
        os::set_error_file(buffer);
duke@0
duke@0
        log.set_fd(fd);
duke@0
      } else {
duke@0
        out.print_raw_cr("# Can not save log file, dump to screen..");
duke@0
        log.set_fd(defaultStream::output_fd());
duke@0
      }
duke@0
    }
duke@0
duke@0
    staticBufferStream sbs(buffer, O_BUFLEN, &log);
duke@0
    first_error->report(&sbs);
duke@0
    first_error->_current_step = 0;         // reset current_step
duke@0
    first_error->_current_step_info = "";   // reset current_step string
duke@0
duke@0
    if (log.fd() != defaultStream::output_fd()) {
duke@0
      close(log.fd());
duke@0
    }
duke@0
duke@0
    log.set_fd(-1);
duke@0
    log_done = true;
duke@0
  }
duke@0
duke@0
duke@0
  static bool skip_OnError = false;
duke@0
  if (!skip_OnError && OnError && OnError[0]) {
duke@0
    skip_OnError = true;
duke@0
duke@0
    out.print_raw_cr("#");
duke@0
    out.print_raw   ("# -XX:OnError=\"");
duke@0
    out.print_raw   (OnError);
duke@0
    out.print_raw_cr("\"");
duke@0
duke@0
    char* cmd;
duke@0
    const char* ptr = OnError;
duke@0
    while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
duke@0
      out.print_raw   ("#   Executing ");
duke@0
#if defined(LINUX)
duke@0
      out.print_raw   ("/bin/sh -c ");
duke@0
#elif defined(SOLARIS)
duke@0
      out.print_raw   ("/usr/bin/sh -c ");
duke@0
#endif
duke@0
      out.print_raw   ("\"");
duke@0
      out.print_raw   (cmd);
duke@0
      out.print_raw_cr("\" ...");
duke@0
duke@0
      os::fork_and_exec(cmd);
duke@0
    }
duke@0
duke@0
    // done with OnError
duke@0
    OnError = NULL;
duke@0
  }
duke@0
duke@0
  static bool skip_bug_url = false;
duke@0
  if (!skip_bug_url) {
duke@0
    skip_bug_url = true;
duke@0
duke@0
    out.print_raw_cr("#");
duke@0
    print_bug_submit_message(&out, _thread);
duke@0
  }
duke@0
duke@0
  if (!UseOSErrorReporting) {
duke@0
    // os::abort() will call abort hooks, try it first.
duke@0
    static bool skip_os_abort = false;
duke@0
    if (!skip_os_abort) {
duke@0
      skip_os_abort = true;
duke@0
      os::abort();
duke@0
    }
duke@0
duke@0
    // if os::abort() doesn't abort, try os::die();
duke@0
    os::die();
duke@0
  }
duke@0
}
duke@0
duke@0
/*
duke@0
 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
duke@0
 * ensures utilities such as jmap can observe the process is a consistent state.
duke@0
 */
duke@0
class VM_ReportJavaOutOfMemory : public VM_Operation {
duke@0
 private:
duke@0
  VMError *_err;
duke@0
 public:
duke@0
  VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
duke@0
  VMOp_Type type() const                 { return VMOp_ReportJavaOutOfMemory; }
duke@0
  void doit();
duke@0
};
duke@0
duke@0
void VM_ReportJavaOutOfMemory::doit() {
duke@0
  // Don't allocate large buffer on stack
duke@0
  static char buffer[O_BUFLEN];
duke@0
duke@0
  tty->print_cr("#");
duke@0
  tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
duke@0
  tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
duke@0
duke@0
  // make heap parsability
duke@0
  Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
duke@0
duke@0
  char* cmd;
duke@0
  const char* ptr = OnOutOfMemoryError;
duke@0
  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
duke@0
    tty->print("#   Executing ");
duke@0
#if defined(LINUX)
duke@0
    tty->print  ("/bin/sh -c ");
duke@0
#elif defined(SOLARIS)
duke@0
    tty->print  ("/usr/bin/sh -c ");
duke@0
#endif
duke@0
    tty->print_cr("\"%s\"...", cmd);
duke@0
duke@0
    os::fork_and_exec(cmd);
duke@0
  }
duke@0
}
duke@0
duke@0
void VMError::report_java_out_of_memory() {
duke@0
  if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
duke@0
    MutexLocker ml(Heap_lock);
duke@0
    VM_ReportJavaOutOfMemory op(this);
duke@0
    VMThread::execute(&op);
duke@0
  }
duke@0
}