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