src/share/vm/utilities/vmError.cpp

Wed, 01 Feb 2012 07:59:01 -0800

author
never
date
Wed, 01 Feb 2012 07:59:01 -0800
changeset 3499
aa3d708d67c4
parent 3430
d7e3846464d0
child 3619
2d503de963b3
permissions
-rw-r--r--

7141200: log some interesting information in ring buffers for crashes
Reviewed-by: kvn, jrose, kevinw, brutisso, twisti, jmasa

     1 /*
     2  * Copyright (c) 2003, 2012, 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  */
    25 #include "precompiled.hpp"
    26 #include "compiler/compileBroker.hpp"
    27 #include "gc_interface/collectedHeap.hpp"
    28 #include "runtime/arguments.hpp"
    29 #include "runtime/frame.inline.hpp"
    30 #include "runtime/init.hpp"
    31 #include "runtime/os.hpp"
    32 #include "runtime/thread.hpp"
    33 #include "runtime/vmThread.hpp"
    34 #include "runtime/vm_operations.hpp"
    35 #include "utilities/debug.hpp"
    36 #include "utilities/decoder.hpp"
    37 #include "utilities/defaultStream.hpp"
    38 #include "utilities/errorReporter.hpp"
    39 #include "utilities/events.hpp"
    40 #include "utilities/top.hpp"
    41 #include "utilities/vmError.hpp"
    43 // List of environment variables that should be reported in error log file.
    44 const char *env_list[] = {
    45   // All platforms
    46   "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
    47   "JAVA_COMPILER", "PATH", "USERNAME",
    49   // Env variables that are defined on Solaris/Linux/BSD
    50   "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
    51   "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
    53   // defined on Linux
    54   "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
    56   // defined on Darwin
    57   "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
    58   "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
    59   "DYLD_INSERT_LIBRARIES",
    61   // defined on Windows
    62   "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
    64   (const char *)0
    65 };
    67 // Fatal error handler for internal errors and crashes.
    68 //
    69 // The default behavior of fatal error handler is to print a brief message
    70 // to standard out (defaultStream::output_fd()), then save detailed information
    71 // into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
    72 // threads are having troubles at the same time, only one error is reported.
    73 // The thread that is reporting error will abort VM when it is done, all other
    74 // threads are blocked forever inside report_and_die().
    76 // Constructor for crashes
    77 VMError::VMError(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) {
    78     _thread = thread;
    79     _id = sig;
    80     _pc   = pc;
    81     _siginfo = siginfo;
    82     _context = context;
    84     _verbose = false;
    85     _current_step = 0;
    86     _current_step_info = NULL;
    88     _message = NULL;
    89     _detail_msg = NULL;
    90     _filename = NULL;
    91     _lineno = 0;
    93     _size = 0;
    94 }
    96 // Constructor for internal errors
    97 VMError::VMError(Thread* thread, const char* filename, int lineno,
    98                  const char* message, const char * detail_msg)
    99 {
   100   _thread = thread;
   101   _id = internal_error;     // Value that's not an OS exception/signal
   102   _filename = filename;
   103   _lineno = lineno;
   104   _message = message;
   105   _detail_msg = detail_msg;
   107   _verbose = false;
   108   _current_step = 0;
   109   _current_step_info = NULL;
   111   _pc = NULL;
   112   _siginfo = NULL;
   113   _context = NULL;
   115   _size = 0;
   116 }
   118 // Constructor for OOM errors
   119 VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size,
   120                  const char* message) {
   121     _thread = thread;
   122     _id = oom_error;     // Value that's not an OS exception/signal
   123     _filename = filename;
   124     _lineno = lineno;
   125     _message = message;
   126     _detail_msg = NULL;
   128     _verbose = false;
   129     _current_step = 0;
   130     _current_step_info = NULL;
   132     _pc = NULL;
   133     _siginfo = NULL;
   134     _context = NULL;
   136     _size = size;
   137 }
   140 // Constructor for non-fatal errors
   141 VMError::VMError(const char* message) {
   142     _thread = NULL;
   143     _id = internal_error;     // Value that's not an OS exception/signal
   144     _filename = NULL;
   145     _lineno = 0;
   146     _message = message;
   147     _detail_msg = NULL;
   149     _verbose = false;
   150     _current_step = 0;
   151     _current_step_info = NULL;
   153     _pc = NULL;
   154     _siginfo = NULL;
   155     _context = NULL;
   157     _size = 0;
   158 }
   160 // -XX:OnError=<string>, where <string> can be a list of commands, separated
   161 // by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
   162 // a single "%". Some examples:
   163 //
   164 // -XX:OnError="pmap %p"                // show memory map
   165 // -XX:OnError="gcore %p; dbx - %p"     // dump core and launch debugger
   166 // -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
   167 // -XX:OnError="kill -9 %p"             // ?#!@#
   169 // A simple parser for -XX:OnError, usage:
   170 //  ptr = OnError;
   171 //  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
   172 //     ... ...
   173 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
   174   if (ptr == NULL || *ptr == NULL) return NULL;
   176   const char* cmd = *ptr;
   178   // skip leading blanks or ';'
   179   while (*cmd == ' ' || *cmd == ';') cmd++;
   181   if (*cmd == '\0') return NULL;
   183   const char * cmdend = cmd;
   184   while (*cmdend != '\0' && *cmdend != ';') cmdend++;
   186   Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
   188   *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
   189   return buf;
   190 }
   193 static void print_bug_submit_message(outputStream *out, Thread *thread) {
   194   if (out == NULL) return;
   195   out->print_raw_cr("# If you would like to submit a bug report, please visit:");
   196   out->print_raw   ("#   ");
   197   out->print_raw_cr(Arguments::java_vendor_url_bug());
   198   // If the crash is in native code, encourage user to submit a bug to the
   199   // provider of that code.
   200   if (thread && thread->is_Java_thread() &&
   201       !thread->is_hidden_from_external_view()) {
   202     JavaThread* jt = (JavaThread*)thread;
   203     if (jt->thread_state() == _thread_in_native) {
   204       out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
   205     }
   206   }
   207   out->print_raw_cr("#");
   208 }
   210 bool VMError::coredump_status;
   211 char VMError::coredump_message[O_BUFLEN];
   213 void VMError::report_coredump_status(const char* message, bool status) {
   214   coredump_status = status;
   215   strncpy(coredump_message, message, sizeof(coredump_message));
   216   coredump_message[sizeof(coredump_message)-1] = 0;
   217 }
   220 // Return a string to describe the error
   221 char* VMError::error_string(char* buf, int buflen) {
   222   char signame_buf[64];
   223   const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
   225   if (signame) {
   226     jio_snprintf(buf, buflen,
   227                  "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
   228                  signame, _id, _pc,
   229                  os::current_process_id(), os::current_thread_id());
   230   } else if (_filename != NULL && _lineno > 0) {
   231     // skip directory names
   232     char separator = os::file_separator()[0];
   233     const char *p = strrchr(_filename, separator);
   234     int n = jio_snprintf(buf, buflen,
   235                          "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
   236                          p ? p + 1 : _filename, _lineno,
   237                          os::current_process_id(), os::current_thread_id());
   238     if (n >= 0 && n < buflen && _message) {
   239       if (_detail_msg) {
   240         jio_snprintf(buf + n, buflen - n, "%s%s: %s",
   241                      os::line_separator(), _message, _detail_msg);
   242       } else {
   243         jio_snprintf(buf + n, buflen - n, "%sError: %s",
   244                      os::line_separator(), _message);
   245       }
   246     }
   247   } else {
   248     jio_snprintf(buf, buflen,
   249                  "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
   250                  _id, os::current_process_id(), os::current_thread_id());
   251   }
   253   return buf;
   254 }
   256 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
   257                                 char* buf, int buflen, bool verbose) {
   258 #ifdef ZERO
   259   if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
   260     // StackFrameStream uses the frame anchor, which may not have
   261     // been set up.  This can be done at any time in Zero, however,
   262     // so if it hasn't been set up then we just set it up now and
   263     // clear it again when we're done.
   264     bool has_last_Java_frame = jt->has_last_Java_frame();
   265     if (!has_last_Java_frame)
   266       jt->set_last_Java_frame();
   267     st->print("Java frames:");
   269     // If the top frame is a Shark frame and the frame anchor isn't
   270     // set up then it's possible that the information in the frame
   271     // is garbage: it could be from a previous decache, or it could
   272     // simply have never been written.  So we print a warning...
   273     StackFrameStream sfs(jt);
   274     if (!has_last_Java_frame && !sfs.is_done()) {
   275       if (sfs.current()->zeroframe()->is_shark_frame()) {
   276         st->print(" (TOP FRAME MAY BE JUNK)");
   277       }
   278     }
   279     st->cr();
   281     // Print the frames
   282     for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
   283       sfs.current()->zero_print_on_error(i, st, buf, buflen);
   284       st->cr();
   285     }
   287     // Reset the frame anchor if necessary
   288     if (!has_last_Java_frame)
   289       jt->reset_last_Java_frame();
   290   }
   291 #else
   292   if (jt->has_last_Java_frame()) {
   293     st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
   294     for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
   295       sfs.current()->print_on_error(st, buf, buflen, verbose);
   296       st->cr();
   297     }
   298   }
   299 #endif // ZERO
   300 }
   302 // This is the main function to report a fatal error. Only one thread can
   303 // call this function, so we don't need to worry about MT-safety. But it's
   304 // possible that the error handler itself may crash or die on an internal
   305 // error, for example, when the stack/heap is badly damaged. We must be
   306 // able to handle recursive errors that happen inside error handler.
   307 //
   308 // Error reporting is done in several steps. If a crash or internal error
   309 // occurred when reporting an error, the nested signal/exception handler
   310 // can skip steps that are already (or partially) done. Error reporting will
   311 // continue from the next step. This allows us to retrieve and print
   312 // information that may be unsafe to get after a fatal error. If it happens,
   313 // you may find nested report_and_die() frames when you look at the stack
   314 // in a debugger.
   315 //
   316 // In general, a hang in error handler is much worse than a crash or internal
   317 // error, as it's harder to recover from a hang. Deadlock can happen if we
   318 // try to grab a lock that is already owned by current thread, or if the
   319 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
   320 // error handler and all the functions it called should avoid grabbing any
   321 // lock. An important thing to notice is that memory allocation needs a lock.
   322 //
   323 // We should avoid using large stack allocated buffers. Many errors happen
   324 // when stack space is already low. Making things even worse is that there
   325 // could be nested report_and_die() calls on stack (see above). Only one
   326 // thread can report error, so large buffers are statically allocated in data
   327 // segment.
   329 void VMError::report(outputStream* st) {
   330 # define BEGIN if (_current_step == 0) { _current_step = 1;
   331 # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
   332 # define END }
   334   // don't allocate large buffer on stack
   335   static char buf[O_BUFLEN];
   337   BEGIN
   339   STEP(10, "(printing fatal error message)")
   341     st->print_cr("#");
   342     if (should_report_bug(_id)) {
   343       st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
   344     } else {
   345       st->print_cr("# There is insufficient memory for the Java "
   346                    "Runtime Environment to continue.");
   347     }
   349   STEP(15, "(printing type of error)")
   351      switch(_id) {
   352        case oom_error:
   353          if (_size) {
   354            st->print("# Native memory allocation (malloc) failed to allocate ");
   355            jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size);
   356            st->print(buf);
   357            st->print(" bytes");
   358            if (_message != NULL) {
   359              st->print(" for ");
   360              st->print(_message);
   361            }
   362            st->cr();
   363          } else {
   364            if (_message != NULL)
   365              st->print("# ");
   366              st->print_cr(_message);
   367          }
   368          // In error file give some solutions
   369          if (_verbose) {
   370            st->print_cr("# Possible reasons:");
   371            st->print_cr("#   The system is out of physical RAM or swap space");
   372            st->print_cr("#   In 32 bit mode, the process size limit was hit");
   373            st->print_cr("# Possible solutions:");
   374            st->print_cr("#   Reduce memory load on the system");
   375            st->print_cr("#   Increase physical memory or swap space");
   376            st->print_cr("#   Check if swap backing store is full");
   377            st->print_cr("#   Use 64 bit Java on a 64 bit OS");
   378            st->print_cr("#   Decrease Java heap size (-Xmx/-Xms)");
   379            st->print_cr("#   Decrease number of Java threads");
   380            st->print_cr("#   Decrease Java thread stack sizes (-Xss)");
   381            st->print_cr("#   Set larger code cache with -XX:ReservedCodeCacheSize=");
   382            st->print_cr("# This output file may be truncated or incomplete.");
   383          } else {
   384            return;  // that's enough for the screen
   385          }
   386          break;
   387        case internal_error:
   388        default:
   389          break;
   390      }
   392   STEP(20, "(printing exception/signal name)")
   394      st->print_cr("#");
   395      st->print("#  ");
   396      // Is it an OS exception/signal?
   397      if (os::exception_name(_id, buf, sizeof(buf))) {
   398        st->print("%s", buf);
   399        st->print(" (0x%x)", _id);                // signal number
   400        st->print(" at pc=" PTR_FORMAT, _pc);
   401      } else {
   402        if (should_report_bug(_id)) {
   403          st->print("Internal Error");
   404        } else {
   405          st->print("Out of Memory Error");
   406        }
   407        if (_filename != NULL && _lineno > 0) {
   408 #ifdef PRODUCT
   409          // In product mode chop off pathname?
   410          char separator = os::file_separator()[0];
   411          const char *p = strrchr(_filename, separator);
   412          const char *file = p ? p+1 : _filename;
   413 #else
   414          const char *file = _filename;
   415 #endif
   416          size_t len = strlen(file);
   417          size_t buflen = sizeof(buf);
   419          strncpy(buf, file, buflen);
   420          if (len + 10 < buflen) {
   421            sprintf(buf + len, ":%d", _lineno);
   422          }
   423          st->print(" (%s)", buf);
   424        } else {
   425          st->print(" (0x%x)", _id);
   426        }
   427      }
   429   STEP(30, "(printing current thread and pid)")
   431      // process id, thread id
   432      st->print(", pid=%d", os::current_process_id());
   433      st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
   434      st->cr();
   436   STEP(40, "(printing error message)")
   438      if (should_report_bug(_id)) {  // already printed the message.
   439        // error message
   440        if (_detail_msg) {
   441          st->print_cr("#  %s: %s", _message ? _message : "Error", _detail_msg);
   442        } else if (_message) {
   443          st->print_cr("#  Error: %s", _message);
   444        }
   445     }
   447   STEP(50, "(printing Java version string)")
   449      // VM version
   450      st->print_cr("#");
   451      JDK_Version::current().to_string(buf, sizeof(buf));
   452      st->print_cr("# JRE version: %s", buf);
   453      st->print_cr("# Java VM: %s (%s %s %s %s)",
   454                    Abstract_VM_Version::vm_name(),
   455                    Abstract_VM_Version::vm_release(),
   456                    Abstract_VM_Version::vm_info_string(),
   457                    Abstract_VM_Version::vm_platform_string(),
   458                    UseCompressedOops ? "compressed oops" : ""
   459                  );
   461   STEP(60, "(printing problematic frame)")
   463      // Print current frame if we have a context (i.e. it's a crash)
   464      if (_context) {
   465        st->print_cr("# Problematic frame:");
   466        st->print("# ");
   467        frame fr = os::fetch_frame_from_context(_context);
   468        fr.print_on_error(st, buf, sizeof(buf));
   469        st->cr();
   470        st->print_cr("#");
   471      }
   472   STEP(63, "(printing core file information)")
   473     st->print("# ");
   474     if (coredump_status) {
   475       st->print("Core dump written. Default location: %s", coredump_message);
   476     } else {
   477       st->print("Failed to write core dump. %s", coredump_message);
   478     }
   479     st->print_cr("");
   480     st->print_cr("#");
   482   STEP(65, "(printing bug submit message)")
   484      if (should_report_bug(_id) && _verbose) {
   485        print_bug_submit_message(st, _thread);
   486      }
   488   STEP(70, "(printing thread)" )
   490      if (_verbose) {
   491        st->cr();
   492        st->print_cr("---------------  T H R E A D  ---------------");
   493        st->cr();
   494      }
   496   STEP(80, "(printing current thread)" )
   498      // current thread
   499      if (_verbose) {
   500        if (_thread) {
   501          st->print("Current thread (" PTR_FORMAT "):  ", _thread);
   502          _thread->print_on_error(st, buf, sizeof(buf));
   503          st->cr();
   504        } else {
   505          st->print_cr("Current thread is native thread");
   506        }
   507        st->cr();
   508      }
   510   STEP(90, "(printing siginfo)" )
   512      // signal no, signal code, address that caused the fault
   513      if (_verbose && _siginfo) {
   514        os::print_siginfo(st, _siginfo);
   515        st->cr();
   516      }
   518   STEP(100, "(printing registers, top of stack, instructions near pc)")
   520      // registers, top of stack, instructions near pc
   521      if (_verbose && _context) {
   522        os::print_context(st, _context);
   523        st->cr();
   524      }
   526   STEP(105, "(printing register info)")
   528      // decode register contents if possible
   529      if (_verbose && _context && Universe::is_fully_initialized()) {
   530        os::print_register_info(st, _context);
   531        st->cr();
   532      }
   534   STEP(110, "(printing stack bounds)" )
   536      if (_verbose) {
   537        st->print("Stack: ");
   539        address stack_top;
   540        size_t stack_size;
   542        if (_thread) {
   543           stack_top = _thread->stack_base();
   544           stack_size = _thread->stack_size();
   545        } else {
   546           stack_top = os::current_stack_base();
   547           stack_size = os::current_stack_size();
   548        }
   550        address stack_bottom = stack_top - stack_size;
   551        st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
   553        frame fr = _context ? os::fetch_frame_from_context(_context)
   554                            : os::current_frame();
   556        if (fr.sp()) {
   557          st->print(",  sp=" PTR_FORMAT, fr.sp());
   558          size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
   559          st->print(",  free space=" SIZE_FORMAT "k", free_stack_size);
   560        }
   562        st->cr();
   563      }
   565   STEP(120, "(printing native stack)" )
   567      if (_verbose) {
   568        frame fr = _context ? os::fetch_frame_from_context(_context)
   569                            : os::current_frame();
   571        // see if it's a valid frame
   572        if (fr.pc()) {
   573           st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
   576           int count = 0;
   577           while (count++ < StackPrintLimit) {
   578              fr.print_on_error(st, buf, sizeof(buf));
   579              st->cr();
   580              if (os::is_first_C_frame(&fr)) break;
   581              fr = os::get_sender_for_C_frame(&fr);
   582           }
   584           if (count > StackPrintLimit) {
   585              st->print_cr("...<more frames>...");
   586           }
   588           st->cr();
   589        }
   590      }
   592   STEP(130, "(printing Java stack)" )
   594      if (_verbose && _thread && _thread->is_Java_thread()) {
   595        print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
   596      }
   598   STEP(135, "(printing target Java thread stack)" )
   600      // printing Java thread stack trace if it is involved in GC crash
   601      if (_verbose && _thread && (_thread->is_Named_thread())) {
   602        JavaThread*  jt = ((NamedThread *)_thread)->processed_thread();
   603        if (jt != NULL) {
   604          st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
   605          print_stack_trace(st, jt, buf, sizeof(buf), true);
   606        }
   607      }
   609   STEP(140, "(printing VM operation)" )
   611      if (_verbose && _thread && _thread->is_VM_thread()) {
   612         VMThread* t = (VMThread*)_thread;
   613         VM_Operation* op = t->vm_operation();
   614         if (op) {
   615           op->print_on_error(st);
   616           st->cr();
   617           st->cr();
   618         }
   619      }
   621   STEP(150, "(printing current compile task)" )
   623      if (_verbose && _thread && _thread->is_Compiler_thread()) {
   624         CompilerThread* t = (CompilerThread*)_thread;
   625         if (t->task()) {
   626            st->cr();
   627            st->print_cr("Current CompileTask:");
   628            t->task()->print_line_on_error(st, buf, sizeof(buf));
   629            st->cr();
   630         }
   631      }
   633   STEP(160, "(printing process)" )
   635      if (_verbose) {
   636        st->cr();
   637        st->print_cr("---------------  P R O C E S S  ---------------");
   638        st->cr();
   639      }
   641   STEP(170, "(printing all threads)" )
   643      // all threads
   644      if (_verbose && _thread) {
   645        Threads::print_on_error(st, _thread, buf, sizeof(buf));
   646        st->cr();
   647      }
   649   STEP(175, "(printing VM state)" )
   651      if (_verbose) {
   652        // Safepoint state
   653        st->print("VM state:");
   655        if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
   656        else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
   657        else st->print("not at safepoint");
   659        // Also see if error occurred during initialization or shutdown
   660        if (!Universe::is_fully_initialized()) {
   661          st->print(" (not fully initialized)");
   662        } else if (VM_Exit::vm_exited()) {
   663          st->print(" (shutting down)");
   664        } else {
   665          st->print(" (normal execution)");
   666        }
   667        st->cr();
   668        st->cr();
   669      }
   671   STEP(180, "(printing owned locks on error)" )
   673      // mutexes/monitors that currently have an owner
   674      if (_verbose) {
   675        print_owned_locks_on_error(st);
   676        st->cr();
   677      }
   679   STEP(190, "(printing heap information)" )
   681      if (_verbose && Universe::is_fully_initialized()) {
   682        // Print heap information before vm abort. As we'd like as much
   683        // information as possible in the report we ask for the
   684        // extended (i.e., more detailed) version.
   685        Universe::print_on(st, true /* extended */);
   686        st->cr();
   687      }
   689   STEP(195, "(printing code cache information)" )
   691      if (_verbose && Universe::is_fully_initialized()) {
   692        // print code cache information before vm abort
   693        CodeCache::print_bounds(st);
   694        st->cr();
   695      }
   697   STEP(200, "(printing ring buffers)" )
   699      if (_verbose) {
   700        Events::print_all(st);
   701        st->cr();
   702      }
   704   STEP(205, "(printing dynamic libraries)" )
   706      if (_verbose) {
   707        // dynamic libraries, or memory map
   708        os::print_dll_info(st);
   709        st->cr();
   710      }
   712   STEP(210, "(printing VM options)" )
   714      if (_verbose) {
   715        // VM options
   716        Arguments::print_on(st);
   717        st->cr();
   718      }
   720   STEP(220, "(printing environment variables)" )
   722      if (_verbose) {
   723        os::print_environment_variables(st, env_list, buf, sizeof(buf));
   724        st->cr();
   725      }
   727   STEP(225, "(printing signal handlers)" )
   729      if (_verbose) {
   730        os::print_signal_handlers(st, buf, sizeof(buf));
   731        st->cr();
   732      }
   734   STEP(230, "" )
   736      if (_verbose) {
   737        st->cr();
   738        st->print_cr("---------------  S Y S T E M  ---------------");
   739        st->cr();
   740      }
   742   STEP(240, "(printing OS information)" )
   744      if (_verbose) {
   745        os::print_os_info(st);
   746        st->cr();
   747      }
   749   STEP(250, "(printing CPU info)" )
   750      if (_verbose) {
   751        os::print_cpu_info(st);
   752        st->cr();
   753      }
   755   STEP(260, "(printing memory info)" )
   757      if (_verbose) {
   758        os::print_memory_info(st);
   759        st->cr();
   760      }
   762   STEP(270, "(printing internal vm info)" )
   764      if (_verbose) {
   765        st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
   766        st->cr();
   767      }
   769   STEP(280, "(printing date and time)" )
   771      if (_verbose) {
   772        os::print_date_and_time(st);
   773        st->cr();
   774      }
   776   END
   778 # undef BEGIN
   779 # undef STEP
   780 # undef END
   781 }
   783 VMError* volatile VMError::first_error = NULL;
   784 volatile jlong VMError::first_error_tid = -1;
   786 void VMError::report_and_die() {
   787   // Don't allocate large buffer on stack
   788   static char buffer[O_BUFLEN];
   790   // An error could happen before tty is initialized or after it has been
   791   // destroyed. Here we use a very simple unbuffered fdStream for printing.
   792   // Only out.print_raw() and out.print_raw_cr() should be used, as other
   793   // printing methods need to allocate large buffer on stack. To format a
   794   // string, use jio_snprintf() with a static buffer or use staticBufferStream.
   795   static fdStream out(defaultStream::output_fd());
   797   // How many errors occurred in error handler when reporting first_error.
   798   static int recursive_error_count;
   800   // We will first print a brief message to standard out (verbose = false),
   801   // then save detailed information in log file (verbose = true).
   802   static bool out_done = false;         // done printing to standard out
   803   static bool log_done = false;         // done saving error log
   804   static bool transmit_report_done = false; // done error reporting
   805   static fdStream log;                  // error log
   807   if (SuppressFatalErrorMessage) {
   808       os::abort();
   809   }
   810   jlong mytid = os::current_thread_id();
   811   if (first_error == NULL &&
   812       Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
   814     // first time
   815     first_error_tid = mytid;
   816     set_error_reported();
   818     if (ShowMessageBoxOnError || PauseAtExit) {
   819       show_message_box(buffer, sizeof(buffer));
   821       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
   822       // WatcherThread can kill JVM if the error handler hangs.
   823       ShowMessageBoxOnError = false;
   824     }
   826     // Write a minidump on Windows, check core dump limits on Linux/Solaris
   827     os::check_or_create_dump(_siginfo, _context, buffer, sizeof(buffer));
   829     // reset signal handlers or exception filter; make sure recursive crashes
   830     // are handled properly.
   831     reset_signal_handlers();
   833   } else {
   834     // If UseOsErrorReporting we call this for each level of the call stack
   835     // while searching for the exception handler.  Only the first level needs
   836     // to be reported.
   837     if (UseOSErrorReporting && log_done) return;
   839     // This is not the first error, see if it happened in a different thread
   840     // or in the same thread during error reporting.
   841     if (first_error_tid != mytid) {
   842       jio_snprintf(buffer, sizeof(buffer),
   843                    "[thread " INT64_FORMAT " also had an error]",
   844                    mytid);
   845       out.print_raw_cr(buffer);
   847       // error reporting is not MT-safe, block current thread
   848       os::infinite_sleep();
   850     } else {
   851       if (recursive_error_count++ > 30) {
   852         out.print_raw_cr("[Too many errors, abort]");
   853         os::die();
   854       }
   856       jio_snprintf(buffer, sizeof(buffer),
   857                    "[error occurred during error reporting %s, id 0x%x]",
   858                    first_error ? first_error->_current_step_info : "",
   859                    _id);
   860       if (log.is_open()) {
   861         log.cr();
   862         log.print_raw_cr(buffer);
   863         log.cr();
   864       } else {
   865         out.cr();
   866         out.print_raw_cr(buffer);
   867         out.cr();
   868       }
   869     }
   870   }
   872   // print to screen
   873   if (!out_done) {
   874     first_error->_verbose = false;
   876     staticBufferStream sbs(buffer, sizeof(buffer), &out);
   877     first_error->report(&sbs);
   879     out_done = true;
   881     first_error->_current_step = 0;         // reset current_step
   882     first_error->_current_step_info = "";   // reset current_step string
   883   }
   885   // print to error log file
   886   if (!log_done) {
   887     first_error->_verbose = true;
   889     // see if log file is already open
   890     if (!log.is_open()) {
   891       // open log file
   892       int fd = -1;
   894       if (ErrorFile != NULL) {
   895         bool copy_ok =
   896           Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
   897         if (copy_ok) {
   898           fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
   899         }
   900       }
   902       if (fd == -1) {
   903         const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
   904         size_t len = strlen(cwd);
   905         // either user didn't specify, or the user's location failed,
   906         // so use the default name in the current directory
   907         jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
   908                      os::file_separator(), os::current_process_id());
   909         fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
   910       }
   912       if (fd == -1) {
   913         const char * tmpdir = os::get_temp_directory();
   914         // try temp directory if it exists.
   915         if (tmpdir != NULL && tmpdir[0] != '\0') {
   916           jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
   917                        tmpdir, os::file_separator(), os::current_process_id());
   918           fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
   919         }
   920       }
   922       if (fd != -1) {
   923         out.print_raw("# An error report file with more information is saved as:\n# ");
   924         out.print_raw_cr(buffer);
   925         os::set_error_file(buffer);
   927         log.set_fd(fd);
   928       } else {
   929         out.print_raw_cr("# Can not save log file, dump to screen..");
   930         log.set_fd(defaultStream::output_fd());
   931         /* Error reporting currently needs dumpfile.
   932          * Maybe implement direct streaming in the future.*/
   933         transmit_report_done = true;
   934       }
   935     }
   937     staticBufferStream sbs(buffer, O_BUFLEN, &log);
   938     first_error->report(&sbs);
   939     first_error->_current_step = 0;         // reset current_step
   940     first_error->_current_step_info = "";   // reset current_step string
   942     // Run error reporting to determine whether or not to report the crash.
   943     if (!transmit_report_done && should_report_bug(first_error->_id)) {
   944       transmit_report_done = true;
   945       FILE* hs_err = ::fdopen(log.fd(), "r");
   946       if (NULL != hs_err) {
   947         ErrorReporter er;
   948         er.call(hs_err, buffer, O_BUFLEN);
   949       }
   950     }
   952     if (log.fd() != defaultStream::output_fd()) {
   953       close(log.fd());
   954     }
   956     log.set_fd(-1);
   957     log_done = true;
   958   }
   961   static bool skip_OnError = false;
   962   if (!skip_OnError && OnError && OnError[0]) {
   963     skip_OnError = true;
   965     out.print_raw_cr("#");
   966     out.print_raw   ("# -XX:OnError=\"");
   967     out.print_raw   (OnError);
   968     out.print_raw_cr("\"");
   970     char* cmd;
   971     const char* ptr = OnError;
   972     while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
   973       out.print_raw   ("#   Executing ");
   974 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
   975       out.print_raw   ("/bin/sh -c ");
   976 #elif defined(SOLARIS)
   977       out.print_raw   ("/usr/bin/sh -c ");
   978 #endif
   979       out.print_raw   ("\"");
   980       out.print_raw   (cmd);
   981       out.print_raw_cr("\" ...");
   983       os::fork_and_exec(cmd);
   984     }
   986     // done with OnError
   987     OnError = NULL;
   988   }
   990   static bool skip_bug_url = !should_report_bug(first_error->_id);
   991   if (!skip_bug_url) {
   992     skip_bug_url = true;
   994     out.print_raw_cr("#");
   995     print_bug_submit_message(&out, _thread);
   996   }
   998   if (!UseOSErrorReporting) {
   999     // os::abort() will call abort hooks, try it first.
  1000     static bool skip_os_abort = false;
  1001     if (!skip_os_abort) {
  1002       skip_os_abort = true;
  1003       bool dump_core = should_report_bug(first_error->_id);
  1004       os::abort(dump_core);
  1007     // if os::abort() doesn't abort, try os::die();
  1008     os::die();
  1012 /*
  1013  * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
  1014  * ensures utilities such as jmap can observe the process is a consistent state.
  1015  */
  1016 class VM_ReportJavaOutOfMemory : public VM_Operation {
  1017  private:
  1018   VMError *_err;
  1019  public:
  1020   VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
  1021   VMOp_Type type() const                 { return VMOp_ReportJavaOutOfMemory; }
  1022   void doit();
  1023 };
  1025 void VM_ReportJavaOutOfMemory::doit() {
  1026   // Don't allocate large buffer on stack
  1027   static char buffer[O_BUFLEN];
  1029   tty->print_cr("#");
  1030   tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
  1031   tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
  1033   // make heap parsability
  1034   Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
  1036   char* cmd;
  1037   const char* ptr = OnOutOfMemoryError;
  1038   while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
  1039     tty->print("#   Executing ");
  1040 #if defined(LINUX)
  1041     tty->print  ("/bin/sh -c ");
  1042 #elif defined(SOLARIS)
  1043     tty->print  ("/usr/bin/sh -c ");
  1044 #endif
  1045     tty->print_cr("\"%s\"...", cmd);
  1047     os::fork_and_exec(cmd);
  1051 void VMError::report_java_out_of_memory() {
  1052   if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
  1053     MutexLocker ml(Heap_lock);
  1054     VM_ReportJavaOutOfMemory op(this);
  1055     VMThread::execute(&op);

mercurial