src/share/vm/utilities/vmError.cpp

changeset 435
a61af66fc99e
child 491
31d829b33f26
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/vmError.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,875 @@
     1.4 +/*
     1.5 + * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +# include "incls/_precompiled.incl"
    1.29 +# include "incls/_vmError.cpp.incl"
    1.30 +
    1.31 +// List of environment variables that should be reported in error log file.
    1.32 +const char *env_list[] = {
    1.33 +  // All platforms
    1.34 +  "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
    1.35 +  "JAVA_COMPILER", "PATH", "USERNAME",
    1.36 +
    1.37 +  // Env variables that are defined on Solaris/Linux
    1.38 +  "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
    1.39 +  "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
    1.40 +
    1.41 +  // defined on Linux
    1.42 +  "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
    1.43 +
    1.44 +  // defined on Windows
    1.45 +  "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
    1.46 +
    1.47 +  (const char *)0
    1.48 +};
    1.49 +
    1.50 +// Fatal error handler for internal errors and crashes.
    1.51 +//
    1.52 +// The default behavior of fatal error handler is to print a brief message
    1.53 +// to standard out (defaultStream::output_fd()), then save detailed information
    1.54 +// into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
    1.55 +// threads are having troubles at the same time, only one error is reported.
    1.56 +// The thread that is reporting error will abort VM when it is done, all other
    1.57 +// threads are blocked forever inside report_and_die().
    1.58 +
    1.59 +// Constructor for crashes
    1.60 +VMError::VMError(Thread* thread, int sig, address pc, void* siginfo, void* context) {
    1.61 +    _thread = thread;
    1.62 +    _id = sig;
    1.63 +    _pc   = pc;
    1.64 +    _siginfo = siginfo;
    1.65 +    _context = context;
    1.66 +
    1.67 +    _verbose = false;
    1.68 +    _current_step = 0;
    1.69 +    _current_step_info = NULL;
    1.70 +
    1.71 +    _message = "";
    1.72 +    _filename = NULL;
    1.73 +    _lineno = 0;
    1.74 +
    1.75 +    _size = 0;
    1.76 +}
    1.77 +
    1.78 +// Constructor for internal errors
    1.79 +VMError::VMError(Thread* thread, const char* message, const char* filename, int lineno) {
    1.80 +    _thread = thread;
    1.81 +    _id = internal_error;     // set it to a value that's not an OS exception/signal
    1.82 +    _filename = filename;
    1.83 +    _lineno = lineno;
    1.84 +    _message = message;
    1.85 +
    1.86 +    _verbose = false;
    1.87 +    _current_step = 0;
    1.88 +    _current_step_info = NULL;
    1.89 +
    1.90 +    _pc = NULL;
    1.91 +    _siginfo = NULL;
    1.92 +    _context = NULL;
    1.93 +
    1.94 +    _size = 0;
    1.95 +}
    1.96 +
    1.97 +// Constructor for OOM errors
    1.98 +VMError::VMError(Thread* thread, size_t size, const char* message, const char* filename, int lineno) {
    1.99 +    _thread = thread;
   1.100 +    _id = oom_error;     // set it to a value that's not an OS exception/signal
   1.101 +    _filename = filename;
   1.102 +    _lineno = lineno;
   1.103 +    _message = message;
   1.104 +
   1.105 +    _verbose = false;
   1.106 +    _current_step = 0;
   1.107 +    _current_step_info = NULL;
   1.108 +
   1.109 +    _pc = NULL;
   1.110 +    _siginfo = NULL;
   1.111 +    _context = NULL;
   1.112 +
   1.113 +    _size = size;
   1.114 +}
   1.115 +
   1.116 +
   1.117 +// Constructor for non-fatal errors
   1.118 +VMError::VMError(const char* message) {
   1.119 +    _thread = NULL;
   1.120 +    _id = internal_error;     // set it to a value that's not an OS exception/signal
   1.121 +    _filename = NULL;
   1.122 +    _lineno = 0;
   1.123 +    _message = message;
   1.124 +
   1.125 +    _verbose = false;
   1.126 +    _current_step = 0;
   1.127 +    _current_step_info = NULL;
   1.128 +
   1.129 +    _pc = NULL;
   1.130 +    _siginfo = NULL;
   1.131 +    _context = NULL;
   1.132 +
   1.133 +    _size = 0;
   1.134 +}
   1.135 +
   1.136 +// -XX:OnError=<string>, where <string> can be a list of commands, separated
   1.137 +// by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
   1.138 +// a single "%". Some examples:
   1.139 +//
   1.140 +// -XX:OnError="pmap %p"                // show memory map
   1.141 +// -XX:OnError="gcore %p; dbx - %p"     // dump core and launch debugger
   1.142 +// -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
   1.143 +// -XX:OnError="kill -9 %p"             // ?#!@#
   1.144 +
   1.145 +// A simple parser for -XX:OnError, usage:
   1.146 +//  ptr = OnError;
   1.147 +//  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
   1.148 +//     ... ...
   1.149 +static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
   1.150 +  if (ptr == NULL || *ptr == NULL) return NULL;
   1.151 +
   1.152 +  const char* cmd = *ptr;
   1.153 +
   1.154 +  // skip leading blanks or ';'
   1.155 +  while (*cmd == ' ' || *cmd == ';') cmd++;
   1.156 +
   1.157 +  if (*cmd == '\0') return NULL;
   1.158 +
   1.159 +  const char * cmdend = cmd;
   1.160 +  while (*cmdend != '\0' && *cmdend != ';') cmdend++;
   1.161 +
   1.162 +  Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
   1.163 +
   1.164 +  *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
   1.165 +  return buf;
   1.166 +}
   1.167 +
   1.168 +
   1.169 +static void print_bug_submit_message(outputStream *out, Thread *thread) {
   1.170 +  if (out == NULL) return;
   1.171 +  out->print_raw_cr("# If you would like to submit a bug report, please visit:");
   1.172 +  out->print_raw   ("#   ");
   1.173 +  out->print_raw_cr(Arguments::java_vendor_url_bug());
   1.174 +  // If the crash is in native code, encourage user to submit a bug to the
   1.175 +  // provider of that code.
   1.176 +  if (thread && thread->is_Java_thread()) {
   1.177 +    JavaThread* jt = (JavaThread*)thread;
   1.178 +    if (jt->thread_state() == _thread_in_native) {
   1.179 +      out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
   1.180 +    }
   1.181 +  }
   1.182 +  out->print_raw_cr("#");
   1.183 +}
   1.184 +
   1.185 +
   1.186 +// Return a string to describe the error
   1.187 +char* VMError::error_string(char* buf, int buflen) {
   1.188 +  char signame_buf[64];
   1.189 +  const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
   1.190 +
   1.191 +  if (signame) {
   1.192 +    jio_snprintf(buf, buflen,
   1.193 +                 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
   1.194 +                 signame, _id, _pc,
   1.195 +                 os::current_process_id(), os::current_thread_id());
   1.196 +  } else {
   1.197 +    if (_filename != NULL && _lineno > 0) {
   1.198 +      // skip directory names
   1.199 +      char separator = os::file_separator()[0];
   1.200 +      const char *p = strrchr(_filename, separator);
   1.201 +
   1.202 +      jio_snprintf(buf, buflen,
   1.203 +        "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT " \nError: %s",
   1.204 +        p ? p + 1 : _filename, _lineno,
   1.205 +        os::current_process_id(), os::current_thread_id(),
   1.206 +        _message ? _message : "");
   1.207 +    } else {
   1.208 +      jio_snprintf(buf, buflen,
   1.209 +        "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
   1.210 +        _id, os::current_process_id(), os::current_thread_id());
   1.211 +    }
   1.212 +  }
   1.213 +
   1.214 +  return buf;
   1.215 +}
   1.216 +
   1.217 +
   1.218 +// This is the main function to report a fatal error. Only one thread can
   1.219 +// call this function, so we don't need to worry about MT-safety. But it's
   1.220 +// possible that the error handler itself may crash or die on an internal
   1.221 +// error, for example, when the stack/heap is badly damaged. We must be
   1.222 +// able to handle recursive errors that happen inside error handler.
   1.223 +//
   1.224 +// Error reporting is done in several steps. If a crash or internal error
   1.225 +// occurred when reporting an error, the nested signal/exception handler
   1.226 +// can skip steps that are already (or partially) done. Error reporting will
   1.227 +// continue from the next step. This allows us to retrieve and print
   1.228 +// information that may be unsafe to get after a fatal error. If it happens,
   1.229 +// you may find nested report_and_die() frames when you look at the stack
   1.230 +// in a debugger.
   1.231 +//
   1.232 +// In general, a hang in error handler is much worse than a crash or internal
   1.233 +// error, as it's harder to recover from a hang. Deadlock can happen if we
   1.234 +// try to grab a lock that is already owned by current thread, or if the
   1.235 +// owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
   1.236 +// error handler and all the functions it called should avoid grabbing any
   1.237 +// lock. An important thing to notice is that memory allocation needs a lock.
   1.238 +//
   1.239 +// We should avoid using large stack allocated buffers. Many errors happen
   1.240 +// when stack space is already low. Making things even worse is that there
   1.241 +// could be nested report_and_die() calls on stack (see above). Only one
   1.242 +// thread can report error, so large buffers are statically allocated in data
   1.243 +// segment.
   1.244 +
   1.245 +void VMError::report(outputStream* st) {
   1.246 +# define BEGIN if (_current_step == 0) { _current_step = 1;
   1.247 +# define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
   1.248 +# define END }
   1.249 +
   1.250 +  // don't allocate large buffer on stack
   1.251 +  static char buf[O_BUFLEN];
   1.252 +
   1.253 +  BEGIN
   1.254 +
   1.255 +  STEP(10, "(printing unexpected error message)")
   1.256 +
   1.257 +     st->print_cr("#");
   1.258 +     st->print_cr("# An unexpected error has been detected by Java Runtime Environment:");
   1.259 +
   1.260 +  STEP(15, "(printing type of error)")
   1.261 +
   1.262 +     switch(_id) {
   1.263 +       case oom_error:
   1.264 +         st->print_cr("#");
   1.265 +         st->print("# java.lang.OutOfMemoryError: ");
   1.266 +         if (_size) {
   1.267 +           st->print("requested ");
   1.268 +           sprintf(buf,"%d",_size);
   1.269 +           st->print(buf);
   1.270 +           st->print(" bytes");
   1.271 +           if (_message != NULL) {
   1.272 +             st->print(" for ");
   1.273 +             st->print(_message);
   1.274 +           }
   1.275 +           st->print_cr(". Out of swap space?");
   1.276 +         } else {
   1.277 +           if (_message != NULL)
   1.278 +             st->print_cr(_message);
   1.279 +         }
   1.280 +         break;
   1.281 +       case internal_error:
   1.282 +       default:
   1.283 +         break;
   1.284 +     }
   1.285 +
   1.286 +  STEP(20, "(printing exception/signal name)")
   1.287 +
   1.288 +     st->print_cr("#");
   1.289 +     st->print("#  ");
   1.290 +     // Is it an OS exception/signal?
   1.291 +     if (os::exception_name(_id, buf, sizeof(buf))) {
   1.292 +       st->print("%s", buf);
   1.293 +       st->print(" (0x%x)", _id);                // signal number
   1.294 +       st->print(" at pc=" PTR_FORMAT, _pc);
   1.295 +     } else {
   1.296 +       st->print("Internal Error");
   1.297 +       if (_filename != NULL && _lineno > 0) {
   1.298 +#ifdef PRODUCT
   1.299 +         // In product mode chop off pathname?
   1.300 +         char separator = os::file_separator()[0];
   1.301 +         const char *p = strrchr(_filename, separator);
   1.302 +         const char *file = p ? p+1 : _filename;
   1.303 +#else
   1.304 +         const char *file = _filename;
   1.305 +#endif
   1.306 +         size_t len = strlen(file);
   1.307 +         size_t buflen = sizeof(buf);
   1.308 +
   1.309 +         strncpy(buf, file, buflen);
   1.310 +         if (len + 10 < buflen) {
   1.311 +           sprintf(buf + len, ":" SIZE_FORMAT, _lineno);
   1.312 +         }
   1.313 +         st->print(" (%s)", buf);
   1.314 +       } else {
   1.315 +         st->print(" (0x%x)", _id);
   1.316 +       }
   1.317 +     }
   1.318 +
   1.319 +  STEP(30, "(printing current thread and pid)")
   1.320 +
   1.321 +     // process id, thread id
   1.322 +     st->print(", pid=%d", os::current_process_id());
   1.323 +     st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
   1.324 +     st->cr();
   1.325 +
   1.326 +  STEP(40, "(printing error message)")
   1.327 +
   1.328 +     // error message
   1.329 +     if (_message && _message[0] != '\0') {
   1.330 +       st->print_cr("#  Error: %s", _message);
   1.331 +     }
   1.332 +
   1.333 +  STEP(50, "(printing Java version string)")
   1.334 +
   1.335 +     // VM version
   1.336 +     st->print_cr("#");
   1.337 +     st->print_cr("# Java VM: %s (%s %s %s)",
   1.338 +                   Abstract_VM_Version::vm_name(),
   1.339 +                   Abstract_VM_Version::vm_release(),
   1.340 +                   Abstract_VM_Version::vm_info_string(),
   1.341 +                   Abstract_VM_Version::vm_platform_string()
   1.342 +                 );
   1.343 +
   1.344 +  STEP(60, "(printing problematic frame)")
   1.345 +
   1.346 +     // Print current frame if we have a context (i.e. it's a crash)
   1.347 +     if (_context) {
   1.348 +       st->print_cr("# Problematic frame:");
   1.349 +       st->print("# ");
   1.350 +       frame fr = os::fetch_frame_from_context(_context);
   1.351 +       fr.print_on_error(st, buf, sizeof(buf));
   1.352 +       st->cr();
   1.353 +       st->print_cr("#");
   1.354 +     }
   1.355 +
   1.356 +  STEP(65, "(printing bug submit message)")
   1.357 +
   1.358 +     if (_verbose) print_bug_submit_message(st, _thread);
   1.359 +
   1.360 +  STEP(70, "(printing thread)" )
   1.361 +
   1.362 +     if (_verbose) {
   1.363 +       st->cr();
   1.364 +       st->print_cr("---------------  T H R E A D  ---------------");
   1.365 +       st->cr();
   1.366 +     }
   1.367 +
   1.368 +  STEP(80, "(printing current thread)" )
   1.369 +
   1.370 +     // current thread
   1.371 +     if (_verbose) {
   1.372 +       if (_thread) {
   1.373 +         st->print("Current thread (" PTR_FORMAT "):  ", _thread);
   1.374 +         _thread->print_on_error(st, buf, sizeof(buf));
   1.375 +         st->cr();
   1.376 +       } else {
   1.377 +         st->print_cr("Current thread is native thread");
   1.378 +       }
   1.379 +       st->cr();
   1.380 +     }
   1.381 +
   1.382 +  STEP(90, "(printing siginfo)" )
   1.383 +
   1.384 +     // signal no, signal code, address that caused the fault
   1.385 +     if (_verbose && _siginfo) {
   1.386 +       os::print_siginfo(st, _siginfo);
   1.387 +       st->cr();
   1.388 +     }
   1.389 +
   1.390 +  STEP(100, "(printing registers, top of stack, instructions near pc)")
   1.391 +
   1.392 +     // registers, top of stack, instructions near pc
   1.393 +     if (_verbose && _context) {
   1.394 +       os::print_context(st, _context);
   1.395 +       st->cr();
   1.396 +     }
   1.397 +
   1.398 +  STEP(110, "(printing stack bounds)" )
   1.399 +
   1.400 +     if (_verbose) {
   1.401 +       st->print("Stack: ");
   1.402 +
   1.403 +       address stack_top;
   1.404 +       size_t stack_size;
   1.405 +
   1.406 +       if (_thread) {
   1.407 +          stack_top = _thread->stack_base();
   1.408 +          stack_size = _thread->stack_size();
   1.409 +       } else {
   1.410 +          stack_top = os::current_stack_base();
   1.411 +          stack_size = os::current_stack_size();
   1.412 +       }
   1.413 +
   1.414 +       address stack_bottom = stack_top - stack_size;
   1.415 +       st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
   1.416 +
   1.417 +       frame fr = _context ? os::fetch_frame_from_context(_context)
   1.418 +                           : os::current_frame();
   1.419 +
   1.420 +       if (fr.sp()) {
   1.421 +         st->print(",  sp=" PTR_FORMAT, fr.sp());
   1.422 +         st->print(",  free space=%dk",
   1.423 +                     ((intptr_t)fr.sp() - (intptr_t)stack_bottom) >> 10);
   1.424 +       }
   1.425 +
   1.426 +       st->cr();
   1.427 +     }
   1.428 +
   1.429 +  STEP(120, "(printing native stack)" )
   1.430 +
   1.431 +     if (_verbose) {
   1.432 +       frame fr = _context ? os::fetch_frame_from_context(_context)
   1.433 +                           : os::current_frame();
   1.434 +
   1.435 +       // see if it's a valid frame
   1.436 +       if (fr.pc()) {
   1.437 +          st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
   1.438 +
   1.439 +          int count = 0;
   1.440 +
   1.441 +          while (count++ < StackPrintLimit) {
   1.442 +             fr.print_on_error(st, buf, sizeof(buf));
   1.443 +             st->cr();
   1.444 +             if (os::is_first_C_frame(&fr)) break;
   1.445 +             fr = os::get_sender_for_C_frame(&fr);
   1.446 +          }
   1.447 +
   1.448 +          if (count > StackPrintLimit) {
   1.449 +             st->print_cr("...<more frames>...");
   1.450 +          }
   1.451 +
   1.452 +          st->cr();
   1.453 +       }
   1.454 +     }
   1.455 +
   1.456 +  STEP(130, "(printing Java stack)" )
   1.457 +
   1.458 +     if (_verbose && _thread && _thread->is_Java_thread()) {
   1.459 +       JavaThread* jt = (JavaThread*)_thread;
   1.460 +       if (jt->has_last_Java_frame()) {
   1.461 +         st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
   1.462 +         for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
   1.463 +           sfs.current()->print_on_error(st, buf, sizeof(buf));
   1.464 +           st->cr();
   1.465 +         }
   1.466 +       }
   1.467 +     }
   1.468 +
   1.469 +  STEP(140, "(printing VM operation)" )
   1.470 +
   1.471 +     if (_verbose && _thread && _thread->is_VM_thread()) {
   1.472 +        VMThread* t = (VMThread*)_thread;
   1.473 +        VM_Operation* op = t->vm_operation();
   1.474 +        if (op) {
   1.475 +          op->print_on_error(st);
   1.476 +          st->cr();
   1.477 +          st->cr();
   1.478 +        }
   1.479 +     }
   1.480 +
   1.481 +  STEP(150, "(printing current compile task)" )
   1.482 +
   1.483 +     if (_verbose && _thread && _thread->is_Compiler_thread()) {
   1.484 +        CompilerThread* t = (CompilerThread*)_thread;
   1.485 +        if (t->task()) {
   1.486 +           st->cr();
   1.487 +           st->print_cr("Current CompileTask:");
   1.488 +           t->task()->print_line_on_error(st, buf, sizeof(buf));
   1.489 +           st->cr();
   1.490 +        }
   1.491 +     }
   1.492 +
   1.493 +  STEP(160, "(printing process)" )
   1.494 +
   1.495 +     if (_verbose) {
   1.496 +       st->cr();
   1.497 +       st->print_cr("---------------  P R O C E S S  ---------------");
   1.498 +       st->cr();
   1.499 +     }
   1.500 +
   1.501 +  STEP(170, "(printing all threads)" )
   1.502 +
   1.503 +     // all threads
   1.504 +     if (_verbose && _thread) {
   1.505 +       Threads::print_on_error(st, _thread, buf, sizeof(buf));
   1.506 +       st->cr();
   1.507 +     }
   1.508 +
   1.509 +  STEP(175, "(printing VM state)" )
   1.510 +
   1.511 +     if (_verbose) {
   1.512 +       // Safepoint state
   1.513 +       st->print("VM state:");
   1.514 +
   1.515 +       if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
   1.516 +       else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
   1.517 +       else st->print("not at safepoint");
   1.518 +
   1.519 +       // Also see if error occurred during initialization or shutdown
   1.520 +       if (!Universe::is_fully_initialized()) {
   1.521 +         st->print(" (not fully initialized)");
   1.522 +       } else if (VM_Exit::vm_exited()) {
   1.523 +         st->print(" (shutting down)");
   1.524 +       } else {
   1.525 +         st->print(" (normal execution)");
   1.526 +       }
   1.527 +       st->cr();
   1.528 +       st->cr();
   1.529 +     }
   1.530 +
   1.531 +  STEP(180, "(printing owned locks on error)" )
   1.532 +
   1.533 +     // mutexes/monitors that currently have an owner
   1.534 +     if (_verbose) {
   1.535 +       print_owned_locks_on_error(st);
   1.536 +       st->cr();
   1.537 +     }
   1.538 +
   1.539 +  STEP(190, "(printing heap information)" )
   1.540 +
   1.541 +     if (_verbose && Universe::is_fully_initialized()) {
   1.542 +       // print heap information before vm abort
   1.543 +       Universe::print_on(st);
   1.544 +       st->cr();
   1.545 +     }
   1.546 +
   1.547 +  STEP(200, "(printing dynamic libraries)" )
   1.548 +
   1.549 +     if (_verbose) {
   1.550 +       // dynamic libraries, or memory map
   1.551 +       os::print_dll_info(st);
   1.552 +       st->cr();
   1.553 +     }
   1.554 +
   1.555 +  STEP(210, "(printing VM options)" )
   1.556 +
   1.557 +     if (_verbose) {
   1.558 +       // VM options
   1.559 +       Arguments::print_on(st);
   1.560 +       st->cr();
   1.561 +     }
   1.562 +
   1.563 +  STEP(220, "(printing environment variables)" )
   1.564 +
   1.565 +     if (_verbose) {
   1.566 +       os::print_environment_variables(st, env_list, buf, sizeof(buf));
   1.567 +       st->cr();
   1.568 +     }
   1.569 +
   1.570 +  STEP(225, "(printing signal handlers)" )
   1.571 +
   1.572 +     if (_verbose) {
   1.573 +       os::print_signal_handlers(st, buf, sizeof(buf));
   1.574 +       st->cr();
   1.575 +     }
   1.576 +
   1.577 +  STEP(230, "" )
   1.578 +
   1.579 +     if (_verbose) {
   1.580 +       st->cr();
   1.581 +       st->print_cr("---------------  S Y S T E M  ---------------");
   1.582 +       st->cr();
   1.583 +     }
   1.584 +
   1.585 +  STEP(240, "(printing OS information)" )
   1.586 +
   1.587 +     if (_verbose) {
   1.588 +       os::print_os_info(st);
   1.589 +       st->cr();
   1.590 +     }
   1.591 +
   1.592 +  STEP(250, "(printing CPU info)" )
   1.593 +     if (_verbose) {
   1.594 +       os::print_cpu_info(st);
   1.595 +       st->cr();
   1.596 +     }
   1.597 +
   1.598 +  STEP(260, "(printing memory info)" )
   1.599 +
   1.600 +     if (_verbose) {
   1.601 +       os::print_memory_info(st);
   1.602 +       st->cr();
   1.603 +     }
   1.604 +
   1.605 +  STEP(270, "(printing internal vm info)" )
   1.606 +
   1.607 +     if (_verbose) {
   1.608 +       st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
   1.609 +       st->cr();
   1.610 +     }
   1.611 +
   1.612 +  STEP(280, "(printing date and time)" )
   1.613 +
   1.614 +     if (_verbose) {
   1.615 +       os::print_date_and_time(st);
   1.616 +       st->cr();
   1.617 +     }
   1.618 +
   1.619 +  END
   1.620 +
   1.621 +# undef BEGIN
   1.622 +# undef STEP
   1.623 +# undef END
   1.624 +}
   1.625 +
   1.626 +
   1.627 +void VMError::report_and_die() {
   1.628 +  // Don't allocate large buffer on stack
   1.629 +  static char buffer[O_BUFLEN];
   1.630 +
   1.631 +  // First error, and its thread id. We must be able to handle native thread,
   1.632 +  // so use thread id instead of Thread* to identify thread.
   1.633 +  static VMError* first_error;
   1.634 +  static jlong    first_error_tid;
   1.635 +
   1.636 +  // An error could happen before tty is initialized or after it has been
   1.637 +  // destroyed. Here we use a very simple unbuffered fdStream for printing.
   1.638 +  // Only out.print_raw() and out.print_raw_cr() should be used, as other
   1.639 +  // printing methods need to allocate large buffer on stack. To format a
   1.640 +  // string, use jio_snprintf() with a static buffer or use staticBufferStream.
   1.641 +  static fdStream out(defaultStream::output_fd());
   1.642 +
   1.643 +  // How many errors occurred in error handler when reporting first_error.
   1.644 +  static int recursive_error_count;
   1.645 +
   1.646 +  // We will first print a brief message to standard out (verbose = false),
   1.647 +  // then save detailed information in log file (verbose = true).
   1.648 +  static bool out_done = false;         // done printing to standard out
   1.649 +  static bool log_done = false;         // done saving error log
   1.650 +  static fdStream log;                  // error log
   1.651 +
   1.652 +  if (SuppressFatalErrorMessage) {
   1.653 +      os::abort();
   1.654 +  }
   1.655 +  jlong mytid = os::current_thread_id();
   1.656 +  if (first_error == NULL &&
   1.657 +      Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
   1.658 +
   1.659 +    // first time
   1.660 +    first_error_tid = mytid;
   1.661 +    set_error_reported();
   1.662 +
   1.663 +    if (ShowMessageBoxOnError) {
   1.664 +      show_message_box(buffer, sizeof(buffer));
   1.665 +
   1.666 +      // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
   1.667 +      // WatcherThread can kill JVM if the error handler hangs.
   1.668 +      ShowMessageBoxOnError = false;
   1.669 +    }
   1.670 +
   1.671 +    // reset signal handlers or exception filter; make sure recursive crashes
   1.672 +    // are handled properly.
   1.673 +    reset_signal_handlers();
   1.674 +
   1.675 +  } else {
   1.676 +    // This is not the first error, see if it happened in a different thread
   1.677 +    // or in the same thread during error reporting.
   1.678 +    if (first_error_tid != mytid) {
   1.679 +      jio_snprintf(buffer, sizeof(buffer),
   1.680 +                   "[thread " INT64_FORMAT " also had an error]",
   1.681 +                   mytid);
   1.682 +      out.print_raw_cr(buffer);
   1.683 +
   1.684 +      // error reporting is not MT-safe, block current thread
   1.685 +      os::infinite_sleep();
   1.686 +
   1.687 +    } else {
   1.688 +      if (recursive_error_count++ > 30) {
   1.689 +        out.print_raw_cr("[Too many errors, abort]");
   1.690 +        os::die();
   1.691 +      }
   1.692 +
   1.693 +      jio_snprintf(buffer, sizeof(buffer),
   1.694 +                   "[error occurred during error reporting %s, id 0x%x]",
   1.695 +                   first_error ? first_error->_current_step_info : "",
   1.696 +                   _id);
   1.697 +      if (log.is_open()) {
   1.698 +        log.cr();
   1.699 +        log.print_raw_cr(buffer);
   1.700 +        log.cr();
   1.701 +      } else {
   1.702 +        out.cr();
   1.703 +        out.print_raw_cr(buffer);
   1.704 +        out.cr();
   1.705 +      }
   1.706 +    }
   1.707 +  }
   1.708 +
   1.709 +  // print to screen
   1.710 +  if (!out_done) {
   1.711 +    first_error->_verbose = false;
   1.712 +
   1.713 +    staticBufferStream sbs(buffer, sizeof(buffer), &out);
   1.714 +    first_error->report(&sbs);
   1.715 +
   1.716 +    out_done = true;
   1.717 +
   1.718 +    first_error->_current_step = 0;         // reset current_step
   1.719 +    first_error->_current_step_info = "";   // reset current_step string
   1.720 +  }
   1.721 +
   1.722 +  // print to error log file
   1.723 +  if (!log_done) {
   1.724 +    first_error->_verbose = true;
   1.725 +
   1.726 +    // see if log file is already open
   1.727 +    if (!log.is_open()) {
   1.728 +      // open log file
   1.729 +      int fd = -1;
   1.730 +
   1.731 +      if (ErrorFile != NULL) {
   1.732 +        bool copy_ok =
   1.733 +          Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
   1.734 +        if (copy_ok) {
   1.735 +          fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
   1.736 +        }
   1.737 +      }
   1.738 +
   1.739 +      if (fd == -1) {
   1.740 +        const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
   1.741 +        size_t len = strlen(cwd);
   1.742 +        // either user didn't specify, or the user's location failed,
   1.743 +        // so use the default name in the current directory
   1.744 +        jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
   1.745 +                     os::file_separator(), os::current_process_id());
   1.746 +        fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
   1.747 +      }
   1.748 +
   1.749 +      if (fd == -1) {
   1.750 +        // try temp directory
   1.751 +        const char * tmpdir = os::get_temp_directory();
   1.752 +        jio_snprintf(buffer, sizeof(buffer), "%shs_err_pid%u.log",
   1.753 +                     (tmpdir ? tmpdir : ""), os::current_process_id());
   1.754 +        fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
   1.755 +      }
   1.756 +
   1.757 +      if (fd != -1) {
   1.758 +        out.print_raw("# An error report file with more information is saved as:\n# ");
   1.759 +        out.print_raw_cr(buffer);
   1.760 +        os::set_error_file(buffer);
   1.761 +
   1.762 +        log.set_fd(fd);
   1.763 +      } else {
   1.764 +        out.print_raw_cr("# Can not save log file, dump to screen..");
   1.765 +        log.set_fd(defaultStream::output_fd());
   1.766 +      }
   1.767 +    }
   1.768 +
   1.769 +    staticBufferStream sbs(buffer, O_BUFLEN, &log);
   1.770 +    first_error->report(&sbs);
   1.771 +    first_error->_current_step = 0;         // reset current_step
   1.772 +    first_error->_current_step_info = "";   // reset current_step string
   1.773 +
   1.774 +    if (log.fd() != defaultStream::output_fd()) {
   1.775 +      close(log.fd());
   1.776 +    }
   1.777 +
   1.778 +    log.set_fd(-1);
   1.779 +    log_done = true;
   1.780 +  }
   1.781 +
   1.782 +
   1.783 +  static bool skip_OnError = false;
   1.784 +  if (!skip_OnError && OnError && OnError[0]) {
   1.785 +    skip_OnError = true;
   1.786 +
   1.787 +    out.print_raw_cr("#");
   1.788 +    out.print_raw   ("# -XX:OnError=\"");
   1.789 +    out.print_raw   (OnError);
   1.790 +    out.print_raw_cr("\"");
   1.791 +
   1.792 +    char* cmd;
   1.793 +    const char* ptr = OnError;
   1.794 +    while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
   1.795 +      out.print_raw   ("#   Executing ");
   1.796 +#if defined(LINUX)
   1.797 +      out.print_raw   ("/bin/sh -c ");
   1.798 +#elif defined(SOLARIS)
   1.799 +      out.print_raw   ("/usr/bin/sh -c ");
   1.800 +#endif
   1.801 +      out.print_raw   ("\"");
   1.802 +      out.print_raw   (cmd);
   1.803 +      out.print_raw_cr("\" ...");
   1.804 +
   1.805 +      os::fork_and_exec(cmd);
   1.806 +    }
   1.807 +
   1.808 +    // done with OnError
   1.809 +    OnError = NULL;
   1.810 +  }
   1.811 +
   1.812 +  static bool skip_bug_url = false;
   1.813 +  if (!skip_bug_url) {
   1.814 +    skip_bug_url = true;
   1.815 +
   1.816 +    out.print_raw_cr("#");
   1.817 +    print_bug_submit_message(&out, _thread);
   1.818 +  }
   1.819 +
   1.820 +  if (!UseOSErrorReporting) {
   1.821 +    // os::abort() will call abort hooks, try it first.
   1.822 +    static bool skip_os_abort = false;
   1.823 +    if (!skip_os_abort) {
   1.824 +      skip_os_abort = true;
   1.825 +      os::abort();
   1.826 +    }
   1.827 +
   1.828 +    // if os::abort() doesn't abort, try os::die();
   1.829 +    os::die();
   1.830 +  }
   1.831 +}
   1.832 +
   1.833 +/*
   1.834 + * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
   1.835 + * ensures utilities such as jmap can observe the process is a consistent state.
   1.836 + */
   1.837 +class VM_ReportJavaOutOfMemory : public VM_Operation {
   1.838 + private:
   1.839 +  VMError *_err;
   1.840 + public:
   1.841 +  VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
   1.842 +  VMOp_Type type() const                 { return VMOp_ReportJavaOutOfMemory; }
   1.843 +  void doit();
   1.844 +};
   1.845 +
   1.846 +void VM_ReportJavaOutOfMemory::doit() {
   1.847 +  // Don't allocate large buffer on stack
   1.848 +  static char buffer[O_BUFLEN];
   1.849 +
   1.850 +  tty->print_cr("#");
   1.851 +  tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
   1.852 +  tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
   1.853 +
   1.854 +  // make heap parsability
   1.855 +  Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
   1.856 +
   1.857 +  char* cmd;
   1.858 +  const char* ptr = OnOutOfMemoryError;
   1.859 +  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
   1.860 +    tty->print("#   Executing ");
   1.861 +#if defined(LINUX)
   1.862 +    tty->print  ("/bin/sh -c ");
   1.863 +#elif defined(SOLARIS)
   1.864 +    tty->print  ("/usr/bin/sh -c ");
   1.865 +#endif
   1.866 +    tty->print_cr("\"%s\"...", cmd);
   1.867 +
   1.868 +    os::fork_and_exec(cmd);
   1.869 +  }
   1.870 +}
   1.871 +
   1.872 +void VMError::report_java_out_of_memory() {
   1.873 +  if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
   1.874 +    MutexLocker ml(Heap_lock);
   1.875 +    VM_ReportJavaOutOfMemory op(this);
   1.876 +    VMThread::execute(&op);
   1.877 +  }
   1.878 +}

mercurial