1 /*
   2  * Copyright (c) 2003, 2016, 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 <fcntl.h>
  26 #include "precompiled.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "logging/logConfiguration.hpp"
  32 #include "prims/whitebox.hpp"
  33 #include "runtime/arguments.hpp"
  34 #include "runtime/atomic.hpp"
  35 #include "runtime/frame.inline.hpp"
  36 #include "runtime/init.hpp"
  37 #include "runtime/os.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 #include "runtime/vmThread.hpp"
  40 #include "runtime/vm_operations.hpp"
  41 #include "runtime/vm_version.hpp"
  42 #include "services/memTracker.hpp"
  43 #include "trace/traceMacros.hpp"
  44 #include "utilities/debug.hpp"
  45 #include "utilities/decoder.hpp"
  46 #include "utilities/defaultStream.hpp"
  47 #include "utilities/errorReporter.hpp"
  48 #include "utilities/events.hpp"
  49 #include "utilities/vmError.hpp"
  50 
  51 // List of environment variables that should be reported in error log file.
  52 const char *env_list[] = {
  53   // All platforms
  54   "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
  55   "JAVA_COMPILER", "PATH", "USERNAME",
  56 
  57   // Env variables that are defined on Solaris/Linux/BSD
  58   "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
  59   "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
  60 
  61   // defined on Linux
  62   "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
  63 
  64   // defined on Darwin
  65   "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
  66   "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
  67   "DYLD_INSERT_LIBRARIES",
  68 
  69   // defined on Windows
  70   "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
  71 
  72   (const char *)0
  73 };
  74 
  75 // A simple parser for -XX:OnError, usage:
  76 //  ptr = OnError;
  77 //  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
  78 //     ... ...
  79 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
  80   if (ptr == NULL || *ptr == NULL) return NULL;
  81 
  82   const char* cmd = *ptr;
  83 
  84   // skip leading blanks or ';'
  85   while (*cmd == ' ' || *cmd == ';') cmd++;
  86 
  87   if (*cmd == '\0') return NULL;
  88 
  89   const char * cmdend = cmd;
  90   while (*cmdend != '\0' && *cmdend != ';') cmdend++;
  91 
  92   Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
  93 
  94   *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
  95   return buf;
  96 }
  97 
  98 static void print_bug_submit_message(outputStream *out, Thread *thread) {
  99   if (out == NULL) return;
 100   out->print_raw_cr("# If you would like to submit a bug report, please visit:");
 101   out->print_raw   ("#   ");
 102   out->print_raw_cr(Arguments::java_vendor_url_bug());
 103   // If the crash is in native code, encourage user to submit a bug to the
 104   // provider of that code.
 105   if (thread && thread->is_Java_thread() &&
 106       !thread->is_hidden_from_external_view()) {
 107     JavaThread* jt = (JavaThread*)thread;
 108     if (jt->thread_state() == _thread_in_native) {
 109       out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
 110     }
 111   }
 112   out->print_raw_cr("#");
 113 }
 114 
 115 bool VMError::coredump_status;
 116 char VMError::coredump_message[O_BUFLEN];
 117 
 118 void VMError::record_coredump_status(const char* message, bool status) {
 119   coredump_status = status;
 120   strncpy(coredump_message, message, sizeof(coredump_message));
 121   coredump_message[sizeof(coredump_message)-1] = 0;
 122 }
 123 
 124 // Return a string to describe the error
 125 char* VMError::error_string(char* buf, int buflen) {
 126   char signame_buf[64];
 127   const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
 128 
 129   if (signame) {
 130     jio_snprintf(buf, buflen,
 131                  "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
 132                  signame, _id, _pc,
 133                  os::current_process_id(), os::current_thread_id());
 134   } else if (_filename != NULL && _lineno > 0) {
 135     // skip directory names
 136     char separator = os::file_separator()[0];
 137     const char *p = strrchr(_filename, separator);
 138     int n = jio_snprintf(buf, buflen,
 139                          "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
 140                          p ? p + 1 : _filename, _lineno,
 141                          os::current_process_id(), os::current_thread_id());
 142     if (n >= 0 && n < buflen && _message) {
 143       if (strlen(_detail_msg) > 0) {
 144         jio_snprintf(buf + n, buflen - n, "%s%s: %s",
 145         os::line_separator(), _message, _detail_msg);
 146       } else {
 147         jio_snprintf(buf + n, buflen - n, "%sError: %s",
 148                      os::line_separator(), _message);
 149       }
 150     }
 151   } else {
 152     jio_snprintf(buf, buflen,
 153                  "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
 154                  _id, os::current_process_id(), os::current_thread_id());
 155   }
 156 
 157   return buf;
 158 }
 159 
 160 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
 161                                 char* buf, int buflen, bool verbose) {
 162 #ifdef ZERO
 163   if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
 164     // StackFrameStream uses the frame anchor, which may not have
 165     // been set up.  This can be done at any time in Zero, however,
 166     // so if it hasn't been set up then we just set it up now and
 167     // clear it again when we're done.
 168     bool has_last_Java_frame = jt->has_last_Java_frame();
 169     if (!has_last_Java_frame)
 170       jt->set_last_Java_frame();
 171     st->print("Java frames:");
 172 
 173     // If the top frame is a Shark frame and the frame anchor isn't
 174     // set up then it's possible that the information in the frame
 175     // is garbage: it could be from a previous decache, or it could
 176     // simply have never been written.  So we print a warning...
 177     StackFrameStream sfs(jt);
 178     if (!has_last_Java_frame && !sfs.is_done()) {
 179       if (sfs.current()->zeroframe()->is_shark_frame()) {
 180         st->print(" (TOP FRAME MAY BE JUNK)");
 181       }
 182     }
 183     st->cr();
 184 
 185     // Print the frames
 186     for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
 187       sfs.current()->zero_print_on_error(i, st, buf, buflen);
 188       st->cr();
 189     }
 190 
 191     // Reset the frame anchor if necessary
 192     if (!has_last_Java_frame)
 193       jt->reset_last_Java_frame();
 194   }
 195 #else
 196   if (jt->has_last_Java_frame()) {
 197     st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
 198     for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
 199       sfs.current()->print_on_error(st, buf, buflen, verbose);
 200       st->cr();
 201     }
 202   }
 203 #endif // ZERO
 204 }
 205 
 206 static void print_oom_reasons(outputStream* st) {
 207   st->print_cr("# Possible reasons:");
 208   st->print_cr("#   The system is out of physical RAM or swap space");
 209   if (UseCompressedOops) {
 210     st->print_cr("#   The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap");
 211   }
 212   if (LogBytesPerWord == 2) {
 213     st->print_cr("#   In 32 bit mode, the process size limit was hit");
 214   }
 215   st->print_cr("# Possible solutions:");
 216   st->print_cr("#   Reduce memory load on the system");
 217   st->print_cr("#   Increase physical memory or swap space");
 218   st->print_cr("#   Check if swap backing store is full");
 219   if (LogBytesPerWord == 2) {
 220     st->print_cr("#   Use 64 bit Java on a 64 bit OS");
 221   }
 222   st->print_cr("#   Decrease Java heap size (-Xmx/-Xms)");
 223   st->print_cr("#   Decrease number of Java threads");
 224   st->print_cr("#   Decrease Java thread stack sizes (-Xss)");
 225   st->print_cr("#   Set larger code cache with -XX:ReservedCodeCacheSize=");
 226   if (UseCompressedOops) {
 227     switch (Universe::narrow_oop_mode()) {
 228       case Universe::UnscaledNarrowOop:
 229         st->print_cr("#   JVM is running with Unscaled Compressed Oops mode in which the Java heap is");
 230         st->print_cr("#     placed in the first 4GB address space. The Java Heap base address is the");
 231         st->print_cr("#     maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
 232         st->print_cr("#     to set the Java Heap base and to place the Java Heap above 4GB virtual address.");
 233         break;
 234       case Universe::ZeroBasedNarrowOop:
 235         st->print_cr("#   JVM is running with Zero Based Compressed Oops mode in which the Java heap is");
 236         st->print_cr("#     placed in the first 32GB address space. The Java Heap base address is the");
 237         st->print_cr("#     maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
 238         st->print_cr("#     to set the Java Heap base and to place the Java Heap above 32GB virtual address.");
 239         break;
 240     }
 241   }
 242   st->print_cr("# This output file may be truncated or incomplete.");
 243 }
 244 
 245 static const char* gc_mode() {
 246   if (UseG1GC)            return "g1 gc";
 247   if (UseParallelGC)      return "parallel gc";
 248   if (UseConcMarkSweepGC) return "concurrent mark sweep gc";
 249   if (UseSerialGC)        return "serial gc";
 250   return "ERROR in GC mode";
 251 }
 252 
 253 static void report_vm_version(outputStream* st, char* buf, int buflen) {
 254    // VM version
 255    st->print_cr("#");
 256    JDK_Version::current().to_string(buf, buflen);
 257    const char* runtime_name = JDK_Version::runtime_name() != NULL ?
 258                                 JDK_Version::runtime_name() : "";
 259    const char* runtime_version = JDK_Version::runtime_version() != NULL ?
 260                                    JDK_Version::runtime_version() : "";
 261    const char* jdk_debug_level = Abstract_VM_Version::printable_jdk_debug_level() != NULL ?
 262                                    Abstract_VM_Version::printable_jdk_debug_level() : "";
 263 
 264    st->print_cr("# JRE version: %s (%s) (%sbuild %s)", runtime_name, buf,
 265                  jdk_debug_level, runtime_version);
 266 
 267    // This is the long version with some default settings added
 268    st->print_cr("# Java VM: %s (%s%s, %s%s%s%s%s, %s, %s)",
 269                  Abstract_VM_Version::vm_name(),
 270                  jdk_debug_level,
 271                  Abstract_VM_Version::vm_release(),
 272                  Abstract_VM_Version::vm_info_string(),
 273                  TieredCompilation ? ", tiered" : "",
 274 #if INCLUDE_JVMCI
 275                  EnableJVMCI ? ", jvmci" : "",
 276                  UseJVMCICompiler ? ", jvmci compiler" : "",
 277 #else
 278                  "", "",
 279 #endif
 280                  UseCompressedOops ? ", compressed oops" : "",
 281                  gc_mode(),
 282                  Abstract_VM_Version::vm_platform_string()
 283                );
 284 }
 285 
 286 // This is the main function to report a fatal error. Only one thread can
 287 // call this function, so we don't need to worry about MT-safety. But it's
 288 // possible that the error handler itself may crash or die on an internal
 289 // error, for example, when the stack/heap is badly damaged. We must be
 290 // able to handle recursive errors that happen inside error handler.
 291 //
 292 // Error reporting is done in several steps. If a crash or internal error
 293 // occurred when reporting an error, the nested signal/exception handler
 294 // can skip steps that are already (or partially) done. Error reporting will
 295 // continue from the next step. This allows us to retrieve and print
 296 // information that may be unsafe to get after a fatal error. If it happens,
 297 // you may find nested report_and_die() frames when you look at the stack
 298 // in a debugger.
 299 //
 300 // In general, a hang in error handler is much worse than a crash or internal
 301 // error, as it's harder to recover from a hang. Deadlock can happen if we
 302 // try to grab a lock that is already owned by current thread, or if the
 303 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
 304 // error handler and all the functions it called should avoid grabbing any
 305 // lock. An important thing to notice is that memory allocation needs a lock.
 306 //
 307 // We should avoid using large stack allocated buffers. Many errors happen
 308 // when stack space is already low. Making things even worse is that there
 309 // could be nested report_and_die() calls on stack (see above). Only one
 310 // thread can report error, so large buffers are statically allocated in data
 311 // segment.
 312 
 313 int          VMError::_current_step;
 314 const char*  VMError::_current_step_info;
 315 
 316 // Timeout handling.
 317 static volatile jlong reporting_start_time = -1;
 318 static volatile bool reporting_timeout = false;
 319 static volatile jlong step_start_time = -1;
 320 static volatile bool step_timeout = false;
 321 
 322 // Helper function, sets a timestamp to the current time.
 323 static void set_to_now(volatile jlong* p) {
 324   const jlong now = os::javaTimeMillis();
 325   if (VM_Version::supports_cx8()) {
 326     Atomic::store(now, p);
 327   } else {
 328     *p = now;
 329   }
 330 }
 331 
 332 // helper function, returns value of time stamp.
 333 static jlong get_timestamp(volatile jlong* p) {
 334   if (VM_Version::supports_cx8()) {
 335     return Atomic::load(p);
 336   } else {
 337     return *p;
 338   }
 339 }
 340 
 341 void VMError::report(outputStream* st, bool _verbose) {
 342 
 343 # define BEGIN if (_current_step == 0) { _current_step = __LINE__;
 344 # define STEP(s) } if (_current_step < __LINE__) { _current_step = __LINE__; _current_step_info = s; \
 345   set_to_now(&step_start_time); step_timeout = false;
 346 # define END }
 347 
 348   // don't allocate large buffer on stack
 349   static char buf[O_BUFLEN];
 350 
 351   BEGIN
 352 
 353   STEP("printing fatal error message")
 354 
 355     st->print_cr("#");
 356     if (should_report_bug(_id)) {
 357       st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
 358     } else {
 359       st->print_cr("# There is insufficient memory for the Java "
 360                    "Runtime Environment to continue.");
 361     }
 362 
 363 #ifndef PRODUCT
 364   // Error handler self tests
 365 
 366   // test secondary error handling. Test it twice, to test that resetting
 367   // error handler after a secondary crash works.
 368   STEP("test secondary crash 1")
 369     if (_verbose && TestCrashInErrorHandler != 0) {
 370       st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...",
 371         TestCrashInErrorHandler);
 372       controlled_crash(TestCrashInErrorHandler);
 373     }
 374 
 375   STEP("test secondary crash 2")
 376     if (_verbose && TestCrashInErrorHandler != 0) {
 377       st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...",
 378         TestCrashInErrorHandler);
 379       controlled_crash(TestCrashInErrorHandler);
 380     }
 381 
 382   // TestUnresponsiveErrorHandler: three times to trigger first a step timeout, then
 383   // the global error reporting timeout.
 384   STEP("test unresponsive error reporting step 1")
 385     if (_verbose && TestUnresponsiveErrorHandler) { os::infinite_sleep(); }
 386 
 387   STEP("test unresponsive error reporting step 2")
 388     if (_verbose && TestUnresponsiveErrorHandler) { os::infinite_sleep(); }
 389 
 390   STEP("test unresponsive error reporting step 3")
 391     if (_verbose && TestUnresponsiveErrorHandler) { os::infinite_sleep(); }
 392 
 393   STEP("test safefetch in error handler")
 394     // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice
 395     // to test that resetting the signal handler works correctly.
 396     if (_verbose && TestSafeFetchInErrorHandler) {
 397       st->print_cr("Will test SafeFetch...");
 398       if (CanUseSafeFetch32()) {
 399         int* const invalid_pointer = (int*) get_segfault_address();
 400         const int x = 0x76543210;
 401         int i1 = SafeFetch32(invalid_pointer, x);
 402         int i2 = SafeFetch32(invalid_pointer, x);
 403         if (i1 == x && i2 == x) {
 404           st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern
 405         } else {
 406           st->print_cr("??");
 407         }
 408       } else {
 409         st->print_cr("not possible; skipped.");
 410       }
 411     }
 412 #endif // PRODUCT
 413 
 414   STEP("printing type of error")
 415 
 416      switch(_id) {
 417        case OOM_MALLOC_ERROR:
 418        case OOM_MMAP_ERROR:
 419          if (_size) {
 420            st->print("# Native memory allocation ");
 421            st->print((_id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " :
 422                                                  "(mmap) failed to map ");
 423            jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size);
 424            st->print("%s", buf);
 425            st->print(" bytes");
 426            if (strlen(_detail_msg) > 0) {
 427              st->print(" for ");
 428              st->print("%s", _detail_msg);
 429            }
 430            st->cr();
 431          } else {
 432            if (strlen(_detail_msg) > 0) {
 433              st->print("# ");
 434              st->print_cr("%s", _detail_msg);
 435            }
 436          }
 437          // In error file give some solutions
 438          if (_verbose) {
 439            print_oom_reasons(st);
 440          } else {
 441            return;  // that's enough for the screen
 442          }
 443          break;
 444        case INTERNAL_ERROR:
 445        default:
 446          break;
 447      }
 448 
 449   STEP("printing exception/signal name")
 450 
 451      st->print_cr("#");
 452      st->print("#  ");
 453      // Is it an OS exception/signal?
 454      if (os::exception_name(_id, buf, sizeof(buf))) {
 455        st->print("%s", buf);
 456        st->print(" (0x%x)", _id);                // signal number
 457        st->print(" at pc=" PTR_FORMAT, p2i(_pc));
 458      } else {
 459        if (should_report_bug(_id)) {
 460          st->print("Internal Error");
 461        } else {
 462          st->print("Out of Memory Error");
 463        }
 464        if (_filename != NULL && _lineno > 0) {
 465 #ifdef PRODUCT
 466          // In product mode chop off pathname?
 467          char separator = os::file_separator()[0];
 468          const char *p = strrchr(_filename, separator);
 469          const char *file = p ? p+1 : _filename;
 470 #else
 471          const char *file = _filename;
 472 #endif
 473          st->print(" (%s:%d)", file, _lineno);
 474        } else {
 475          st->print(" (0x%x)", _id);
 476        }
 477      }
 478 
 479   STEP("printing current thread and pid")
 480 
 481      // process id, thread id
 482      st->print(", pid=%d", os::current_process_id());
 483      st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
 484      st->cr();
 485 
 486   STEP("printing error message")
 487 
 488      if (should_report_bug(_id)) {  // already printed the message.
 489        // error message
 490        if (strlen(_detail_msg) > 0) {
 491          st->print_cr("#  %s: %s", _message ? _message : "Error", _detail_msg);
 492        } else if (_message) {
 493          st->print_cr("#  Error: %s", _message);
 494        }
 495      }
 496 
 497   STEP("printing Java version string")
 498 
 499      report_vm_version(st, buf, sizeof(buf));
 500 
 501   STEP("printing problematic frame")
 502 
 503      // Print current frame if we have a context (i.e. it's a crash)
 504      if (_context) {
 505        st->print_cr("# Problematic frame:");
 506        st->print("# ");
 507        frame fr = os::fetch_frame_from_context(_context);
 508        fr.print_on_error(st, buf, sizeof(buf));
 509        st->cr();
 510        st->print_cr("#");
 511      }
 512 
 513   STEP("printing core file information")
 514     st->print("# ");
 515     if (CreateCoredumpOnCrash) {
 516       if (coredump_status) {
 517         st->print("Core dump will be written. Default location: %s", coredump_message);
 518       } else {
 519         st->print("No core dump will be written. %s", coredump_message);
 520       }
 521     } else {
 522       st->print("CreateCoredumpOnCrash turned off, no core file dumped");
 523     }
 524     st->cr();
 525     st->print_cr("#");
 526 
 527   STEP("printing bug submit message")
 528 
 529      if (should_report_bug(_id) && _verbose) {
 530        print_bug_submit_message(st, _thread);
 531      }
 532 
 533   STEP("printing summary")
 534 
 535      if (_verbose) {
 536        st->cr();
 537        st->print_cr("---------------  S U M M A R Y ------------");
 538        st->cr();
 539      }
 540 
 541   STEP("printing VM option summary")
 542 
 543      if (_verbose) {
 544        // VM options
 545        Arguments::print_summary_on(st);
 546        st->cr();
 547      }
 548 
 549   STEP("printing summary machine and OS info")
 550 
 551      if (_verbose) {
 552        os::print_summary_info(st, buf, sizeof(buf));
 553      }
 554 
 555 
 556   STEP("printing date and time")
 557 
 558      if (_verbose) {
 559        os::print_date_and_time(st, buf, sizeof(buf));
 560      }
 561 
 562   STEP("printing thread")
 563 
 564      if (_verbose) {
 565        st->cr();
 566        st->print_cr("---------------  T H R E A D  ---------------");
 567        st->cr();
 568      }
 569 
 570   STEP("printing current thread")
 571 
 572      // current thread
 573      if (_verbose) {
 574        if (_thread) {
 575          st->print("Current thread (" PTR_FORMAT "):  ", p2i(_thread));
 576          _thread->print_on_error(st, buf, sizeof(buf));
 577          st->cr();
 578        } else {
 579          st->print_cr("Current thread is native thread");
 580        }
 581        st->cr();
 582      }
 583 
 584   STEP("printing current compile task")
 585 
 586      if (_verbose && _thread && _thread->is_Compiler_thread()) {
 587         CompilerThread* t = (CompilerThread*)_thread;
 588         if (t->task()) {
 589            st->cr();
 590            st->print_cr("Current CompileTask:");
 591            t->task()->print_line_on_error(st, buf, sizeof(buf));
 592            st->cr();
 593         }
 594      }
 595 
 596 
 597   STEP("printing stack bounds")
 598 
 599      if (_verbose) {
 600        st->print("Stack: ");
 601 
 602        address stack_top;
 603        size_t stack_size;
 604 
 605        if (_thread) {
 606           stack_top = _thread->stack_base();
 607           stack_size = _thread->stack_size();
 608        } else {
 609           stack_top = os::current_stack_base();
 610           stack_size = os::current_stack_size();
 611        }
 612 
 613        address stack_bottom = stack_top - stack_size;
 614        st->print("[" PTR_FORMAT "," PTR_FORMAT "]", p2i(stack_bottom), p2i(stack_top));
 615 
 616        frame fr = _context ? os::fetch_frame_from_context(_context)
 617                            : os::current_frame();
 618 
 619        if (fr.sp()) {
 620          st->print(",  sp=" PTR_FORMAT, p2i(fr.sp()));
 621          size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
 622          st->print(",  free space=" SIZE_FORMAT "k", free_stack_size);
 623        }
 624 
 625        st->cr();
 626      }
 627 
 628   STEP("printing native stack")
 629 
 630    if (_verbose) {
 631      if (os::platform_print_native_stack(st, _context, buf, sizeof(buf))) {
 632        // We have printed the native stack in platform-specific code
 633        // Windows/x64 needs special handling.
 634      } else {
 635        frame fr = _context ? os::fetch_frame_from_context(_context)
 636                            : os::current_frame();
 637 
 638        print_native_stack(st, fr, _thread, buf, sizeof(buf));
 639      }
 640    }
 641 
 642   STEP("printing Java stack")
 643 
 644      if (_verbose && _thread && _thread->is_Java_thread()) {
 645        print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
 646      }
 647 
 648   STEP("printing target Java thread stack")
 649 
 650      // printing Java thread stack trace if it is involved in GC crash
 651      if (_verbose && _thread && (_thread->is_Named_thread())) {
 652        JavaThread*  jt = ((NamedThread *)_thread)->processed_thread();
 653        if (jt != NULL) {
 654          st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id());
 655          print_stack_trace(st, jt, buf, sizeof(buf), true);
 656        }
 657      }
 658 
 659   STEP("printing siginfo")
 660 
 661      // signal no, signal code, address that caused the fault
 662      if (_verbose && _siginfo) {
 663        st->cr();
 664        os::print_siginfo(st, _siginfo);
 665        st->cr();
 666      }
 667 
 668   STEP("CDS archive access warning")
 669 
 670      // Print an explicit hint if we crashed on access to the CDS archive.
 671      if (_verbose && _siginfo) {
 672        check_failing_cds_access(st, _siginfo);
 673        st->cr();
 674      }
 675 
 676   STEP("printing register info")
 677 
 678      // decode register contents if possible
 679      if (_verbose && _context && Universe::is_fully_initialized()) {
 680        os::print_register_info(st, _context);
 681        st->cr();
 682      }
 683 
 684   STEP("printing registers, top of stack, instructions near pc")
 685 
 686      // registers, top of stack, instructions near pc
 687      if (_verbose && _context) {
 688        os::print_context(st, _context);
 689        st->cr();
 690      }
 691 
 692   STEP("printing code blob if possible")
 693 
 694      if (_verbose && _context) {
 695        CodeBlob* cb = CodeCache::find_blob(_pc);
 696        if (cb != NULL) {
 697          if (Interpreter::contains(_pc)) {
 698            // The interpreter CodeBlob is very large so try to print the codelet instead.
 699            InterpreterCodelet* codelet = Interpreter::codelet_containing(_pc);
 700            if (codelet != NULL) {
 701              codelet->print_on(st);
 702              Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
 703            }
 704          } else {
 705            StubCodeDesc* desc = StubCodeDesc::desc_for(_pc);
 706            if (desc != NULL) {
 707              desc->print_on(st);
 708              Disassembler::decode(desc->begin(), desc->end(), st);
 709            } else {
 710              Disassembler::decode(cb, st);
 711              st->cr();
 712            }
 713          }
 714        }
 715      }
 716 
 717   STEP("printing VM operation")
 718 
 719      if (_verbose && _thread && _thread->is_VM_thread()) {
 720         VMThread* t = (VMThread*)_thread;
 721         VM_Operation* op = t->vm_operation();
 722         if (op) {
 723           op->print_on_error(st);
 724           st->cr();
 725           st->cr();
 726         }
 727      }
 728 
 729   STEP("printing process")
 730 
 731      if (_verbose) {
 732        st->cr();
 733        st->print_cr("---------------  P R O C E S S  ---------------");
 734        st->cr();
 735      }
 736 
 737   STEP("printing all threads")
 738 
 739      // all threads
 740      if (_verbose && _thread) {
 741        Threads::print_on_error(st, _thread, buf, sizeof(buf));
 742        st->cr();
 743      }
 744 
 745   STEP("printing VM state")
 746 
 747      if (_verbose) {
 748        // Safepoint state
 749        st->print("VM state:");
 750 
 751        if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
 752        else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
 753        else st->print("not at safepoint");
 754 
 755        // Also see if error occurred during initialization or shutdown
 756        if (!Universe::is_fully_initialized()) {
 757          st->print(" (not fully initialized)");
 758        } else if (VM_Exit::vm_exited()) {
 759          st->print(" (shutting down)");
 760        } else {
 761          st->print(" (normal execution)");
 762        }
 763        st->cr();
 764        st->cr();
 765      }
 766 
 767   STEP("printing owned locks on error")
 768 
 769      // mutexes/monitors that currently have an owner
 770      if (_verbose) {
 771        print_owned_locks_on_error(st);
 772        st->cr();
 773      }
 774 
 775   STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
 776 
 777      if (_verbose && Exceptions::has_exception_counts()) {
 778        st->print_cr("OutOfMemory and StackOverflow Exception counts:");
 779        Exceptions::print_exception_counts_on_error(st);
 780        st->cr();
 781      }
 782 
 783   STEP("printing compressed oops mode")
 784 
 785      if (_verbose && UseCompressedOops) {
 786        Universe::print_compressed_oops_mode(st);
 787        if (UseCompressedClassPointers) {
 788          Metaspace::print_compressed_class_space(st);
 789        }
 790        st->cr();
 791      }
 792 
 793   STEP("printing heap information")
 794 
 795      if (_verbose && Universe::is_fully_initialized()) {
 796        Universe::heap()->print_on_error(st);
 797        st->cr();
 798        st->print_cr("Polling page: " INTPTR_FORMAT, p2i(os::get_polling_page()));
 799        st->cr();
 800      }
 801 
 802   STEP("printing code cache information")
 803 
 804      if (_verbose && Universe::is_fully_initialized()) {
 805        // print code cache information before vm abort
 806        CodeCache::print_summary(st);
 807        st->cr();
 808      }
 809 
 810   STEP("printing ring buffers")
 811 
 812      if (_verbose) {
 813        Events::print_all(st);
 814        st->cr();
 815      }
 816 
 817   STEP("printing dynamic libraries")
 818 
 819      if (_verbose) {
 820        // dynamic libraries, or memory map
 821        os::print_dll_info(st);
 822        st->cr();
 823      }
 824 
 825   STEP("printing VM options")
 826 
 827      if (_verbose) {
 828        // VM options
 829        Arguments::print_on(st);
 830        st->cr();
 831      }
 832 
 833   STEP("printing warning if internal testing API used")
 834 
 835      if (WhiteBox::used()) {
 836        st->print_cr("Unsupported internal testing APIs have been used.");
 837        st->cr();
 838      }
 839 
 840   STEP("printing log configuration")
 841     if (_verbose){
 842       st->print_cr("Logging:");
 843       LogConfiguration::describe_current_configuration(st);
 844       st->cr();
 845     }
 846 
 847   STEP("printing all environment variables")
 848 
 849      if (_verbose) {
 850        os::print_environment_variables(st, env_list);
 851        st->cr();
 852      }
 853 
 854   STEP("printing signal handlers")
 855 
 856      if (_verbose) {
 857        os::print_signal_handlers(st, buf, sizeof(buf));
 858        st->cr();
 859      }
 860 
 861   STEP("Native Memory Tracking")
 862      if (_verbose) {
 863        MemTracker::error_report(st);
 864      }
 865 
 866   STEP("printing system")
 867 
 868      if (_verbose) {
 869        st->cr();
 870        st->print_cr("---------------  S Y S T E M  ---------------");
 871        st->cr();
 872      }
 873 
 874   STEP("printing OS information")
 875 
 876      if (_verbose) {
 877        os::print_os_info(st);
 878        st->cr();
 879      }
 880 
 881   STEP("printing CPU info")
 882      if (_verbose) {
 883        os::print_cpu_info(st, buf, sizeof(buf));
 884        st->cr();
 885      }
 886 
 887   STEP("printing memory info")
 888 
 889      if (_verbose) {
 890        os::print_memory_info(st);
 891        st->cr();
 892      }
 893 
 894   STEP("printing internal vm info")
 895 
 896      if (_verbose) {
 897        st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
 898        st->cr();
 899      }
 900 
 901   // print a defined marker to show that error handling finished correctly.
 902   STEP("printing end marker")
 903 
 904      if (_verbose) {
 905        st->print_cr("END.");
 906      }
 907 
 908   END
 909 
 910 # undef BEGIN
 911 # undef STEP
 912 # undef END
 913 }
 914 
 915 // Report for the vm_info_cmd. This prints out the information above omitting
 916 // crash and thread specific information.  If output is added above, it should be added
 917 // here also, if it is safe to call during a running process.
 918 void VMError::print_vm_info(outputStream* st) {
 919 
 920   char buf[O_BUFLEN];
 921   report_vm_version(st, buf, sizeof(buf));
 922 
 923   // STEP("printing summary")
 924 
 925   st->cr();
 926   st->print_cr("---------------  S U M M A R Y ------------");
 927   st->cr();
 928 
 929   // STEP("printing VM option summary")
 930 
 931   // VM options
 932   Arguments::print_summary_on(st);
 933   st->cr();
 934 
 935   // STEP("printing summary machine and OS info")
 936 
 937   os::print_summary_info(st, buf, sizeof(buf));
 938 
 939   // STEP("printing date and time")
 940 
 941   os::print_date_and_time(st, buf, sizeof(buf));
 942 
 943   // Skip: STEP("printing thread")
 944 
 945   // STEP("printing process")
 946 
 947   st->cr();
 948   st->print_cr("---------------  P R O C E S S  ---------------");
 949   st->cr();
 950 
 951   // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
 952 
 953   if (Exceptions::has_exception_counts()) {
 954     st->print_cr("OutOfMemory and StackOverflow Exception counts:");
 955     Exceptions::print_exception_counts_on_error(st);
 956     st->cr();
 957   }
 958 
 959   // STEP("printing compressed oops mode")
 960 
 961   if (UseCompressedOops) {
 962     Universe::print_compressed_oops_mode(st);
 963     if (UseCompressedClassPointers) {
 964       Metaspace::print_compressed_class_space(st);
 965     }
 966     st->cr();
 967   }
 968 
 969   // STEP("printing heap information")
 970 
 971   if (Universe::is_fully_initialized()) {
 972     MutexLocker hl(Heap_lock);
 973     Universe::heap()->print_on_error(st);
 974     st->cr();
 975     st->print_cr("Polling page: " INTPTR_FORMAT, p2i(os::get_polling_page()));
 976     st->cr();
 977   }
 978 
 979   // STEP("printing code cache information")
 980 
 981   if (Universe::is_fully_initialized()) {
 982     // print code cache information before vm abort
 983     CodeCache::print_summary(st);
 984     st->cr();
 985   }
 986 
 987   // STEP("printing ring buffers")
 988 
 989   Events::print_all(st);
 990   st->cr();
 991 
 992   // STEP("printing dynamic libraries")
 993 
 994   // dynamic libraries, or memory map
 995   os::print_dll_info(st);
 996   st->cr();
 997 
 998   // STEP("printing VM options")
 999 
1000   // VM options
1001   Arguments::print_on(st);
1002   st->cr();
1003 
1004   // STEP("printing warning if internal testing API used")
1005 
1006   if (WhiteBox::used()) {
1007     st->print_cr("Unsupported internal testing APIs have been used.");
1008     st->cr();
1009   }
1010 
1011   // STEP("printing log configuration")
1012   st->print_cr("Logging:");
1013   LogConfiguration::describe(st);
1014   st->cr();
1015 
1016   // STEP("printing all environment variables")
1017 
1018   os::print_environment_variables(st, env_list);
1019   st->cr();
1020 
1021   // STEP("printing signal handlers")
1022 
1023   os::print_signal_handlers(st, buf, sizeof(buf));
1024   st->cr();
1025 
1026   // STEP("Native Memory Tracking")
1027 
1028   MemTracker::error_report(st);
1029 
1030   // STEP("printing system")
1031 
1032   st->cr();
1033   st->print_cr("---------------  S Y S T E M  ---------------");
1034   st->cr();
1035 
1036   // STEP("printing OS information")
1037 
1038   os::print_os_info(st);
1039   st->cr();
1040 
1041   // STEP("printing CPU info")
1042 
1043   os::print_cpu_info(st, buf, sizeof(buf));
1044   st->cr();
1045 
1046   // STEP("printing memory info")
1047 
1048   os::print_memory_info(st);
1049   st->cr();
1050 
1051   // STEP("printing internal vm info")
1052 
1053   st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
1054   st->cr();
1055 
1056   // print a defined marker to show that error handling finished correctly.
1057   // STEP("printing end marker")
1058 
1059   st->print_cr("END.");
1060 }
1061 
1062 volatile intptr_t VMError::first_error_tid = -1;
1063 
1064 // An error could happen before tty is initialized or after it has been
1065 // destroyed.
1066 // Please note: to prevent large stack allocations, the log- and
1067 // output-stream use a global scratch buffer for format printing.
1068 // (see VmError::report_and_die(). Access to those streams is synchronized
1069 // in  VmError::report_and_die() - there is only one reporting thread at
1070 // any given time.
1071 fdStream VMError::out(defaultStream::output_fd());
1072 fdStream VMError::log; // error log used by VMError::report_and_die()
1073 
1074 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
1075 static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) {
1076   int fd = -1;
1077   if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1078     // the O_EXCL flag will cause the open to fail if the file exists
1079     fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0666);
1080   }
1081   return fd;
1082 }
1083 
1084 /**
1085  * Construct file name for a log file and return it's file descriptor.
1086  * Name and location depends on pattern, default_pattern params and access
1087  * permissions.
1088  */
1089 static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) {
1090   int fd = -1;
1091 
1092   // If possible, use specified pattern to construct log file name
1093   if (pattern != NULL) {
1094     fd = expand_and_open(pattern, buf, buflen, 0);
1095   }
1096 
1097   // Either user didn't specify, or the user's location failed,
1098   // so use the default name in the current directory
1099   if (fd == -1) {
1100     const char* cwd = os::get_current_directory(buf, buflen);
1101     if (cwd != NULL) {
1102       size_t pos = strlen(cwd);
1103       int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1104       pos += fsep_len;
1105       if (fsep_len > 0) {
1106         fd = expand_and_open(default_pattern, buf, buflen, pos);
1107       }
1108     }
1109   }
1110 
1111    // try temp directory if it exists.
1112    if (fd == -1) {
1113      const char* tmpdir = os::get_temp_directory();
1114      if (tmpdir != NULL && strlen(tmpdir) > 0) {
1115        int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1116        if (pos > 0) {
1117          fd = expand_and_open(default_pattern, buf, buflen, pos);
1118        }
1119      }
1120    }
1121 
1122   return fd;
1123 }
1124 
1125 int         VMError::_id;
1126 const char* VMError::_message;
1127 char        VMError::_detail_msg[1024];
1128 Thread*     VMError::_thread;
1129 address     VMError::_pc;
1130 void*       VMError::_siginfo;
1131 void*       VMError::_context;
1132 const char* VMError::_filename;
1133 int         VMError::_lineno;
1134 size_t      VMError::_size;
1135 
1136 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo,
1137                              void* context, const char* detail_fmt, ...)
1138 {
1139   va_list detail_args;
1140   va_start(detail_args, detail_fmt);
1141   report_and_die(sig, NULL, detail_fmt, detail_args, thread, pc, siginfo, context, NULL, 0, 0);
1142   va_end(detail_args);
1143 }
1144 
1145 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context)
1146 {
1147   report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1148 }
1149 
1150 void VMError::report_and_die(const char* message, const char* detail_fmt, ...)
1151 {
1152   va_list detail_args;
1153   va_start(detail_args, detail_fmt);
1154   report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, NULL, NULL, NULL, NULL, NULL, 0, 0);
1155   va_end(detail_args);
1156 }
1157 
1158 void VMError::report_and_die(const char* message)
1159 {
1160   report_and_die(message, "%s", "");
1161 }
1162 
1163 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, const char* message,
1164                              const char* detail_fmt, va_list detail_args)
1165 {
1166   report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, 0);
1167 }
1168 
1169 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1170                              VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1171   report_and_die(vm_err_type, NULL, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, size);
1172 }
1173 
1174 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1175                              Thread* thread, address pc, void* siginfo, void* context, const char* filename,
1176                              int lineno, size_t size)
1177 {
1178   // Don't allocate large buffer on stack
1179   static char buffer[O_BUFLEN];
1180   out.set_scratch_buffer(buffer, sizeof(buffer));
1181   log.set_scratch_buffer(buffer, sizeof(buffer));
1182 
1183   // How many errors occurred in error handler when reporting first_error.
1184   static int recursive_error_count;
1185 
1186   // We will first print a brief message to standard out (verbose = false),
1187   // then save detailed information in log file (verbose = true).
1188   static bool out_done = false;         // done printing to standard out
1189   static bool log_done = false;         // done saving error log
1190   static bool transmit_report_done = false; // done error reporting
1191 
1192   if (SuppressFatalErrorMessage) {
1193       os::abort(CreateCoredumpOnCrash);
1194   }
1195   intptr_t mytid = os::current_thread_id();
1196   if (first_error_tid == -1 &&
1197       Atomic::cmpxchg_ptr(mytid, &first_error_tid, -1) == -1) {
1198 
1199     // Initialize time stamps to use the same base.
1200     out.time_stamp().update_to(1);
1201     log.time_stamp().update_to(1);
1202 
1203     _id = id;
1204     _message = message;
1205     _thread = thread;
1206     _pc = pc;
1207     _siginfo = siginfo;
1208     _context = context;
1209     _filename = filename;
1210     _lineno = lineno;
1211     _size = size;
1212     jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1213 
1214     // first time
1215     set_error_reported();
1216 
1217     reporting_started();
1218     set_to_now(&reporting_start_time);
1219 
1220     if (ShowMessageBoxOnError || PauseAtExit) {
1221       show_message_box(buffer, sizeof(buffer));
1222 
1223       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1224       // WatcherThread can kill JVM if the error handler hangs.
1225       ShowMessageBoxOnError = false;
1226     }
1227 
1228     os::check_dump_limit(buffer, sizeof(buffer));
1229 
1230     // reset signal handlers or exception filter; make sure recursive crashes
1231     // are handled properly.
1232     reset_signal_handlers();
1233 
1234     TRACE_VM_ERROR();
1235 
1236   } else {
1237     // If UseOsErrorReporting we call this for each level of the call stack
1238     // while searching for the exception handler.  Only the first level needs
1239     // to be reported.
1240     if (UseOSErrorReporting && log_done) return;
1241 
1242     // This is not the first error, see if it happened in a different thread
1243     // or in the same thread during error reporting.
1244     if (first_error_tid != mytid) {
1245       char msgbuf[64];
1246       jio_snprintf(msgbuf, sizeof(msgbuf),
1247                    "[thread " INTX_FORMAT " also had an error]",
1248                    mytid);
1249       out.print_raw_cr(msgbuf);
1250 
1251       // error reporting is not MT-safe, block current thread
1252       os::infinite_sleep();
1253 
1254     } else {
1255       if (recursive_error_count++ > 30) {
1256         out.print_raw_cr("[Too many errors, abort]");
1257         os::die();
1258       }
1259 
1260       outputStream* const st = log.is_open() ? &log : &out;
1261       st->cr();
1262 
1263       // Timeout handling.
1264       if (step_timeout) {
1265         // The current step had a timeout. Lets continue reporting with the next step.
1266         st->print_raw("[timeout occurred during error reporting in step \"");
1267         st->print_raw(_current_step_info);
1268         st->print_cr("\"] after " INT64_FORMAT " ms.", os::javaTimeMillis() - step_start_time);
1269       } else if (reporting_timeout) {
1270         // We hit ErrorLogTimeout. Reporting will stop altogether. Lets wrap things
1271         // up, the process is about to be stopped by the WatcherThread.
1272         st->print_cr("------ Timout during error reporting after " INT64_FORMAT "ms. ------",
1273           os::javaTimeMillis() - reporting_start_time);
1274         st->flush();
1275         // Watcherthread is about to call os::die. Lets just wait.
1276         os::infinite_sleep();
1277       } else {
1278         // Crash or assert during error reporting. Lets continue reporting with the next step.
1279         jio_snprintf(buffer, sizeof(buffer),
1280            "[error occurred during error reporting (%s), id 0x%x]",
1281                    _current_step_info, _id);
1282         st->print_raw_cr(buffer);
1283         st->cr();
1284       }
1285     }
1286   }
1287 
1288   // print to screen
1289   if (!out_done) {
1290     report(&out, false);
1291 
1292     out_done = true;
1293 
1294     _current_step = 0;
1295     _current_step_info = "";
1296   }
1297 
1298   // print to error log file
1299   if (!log_done) {
1300     // see if log file is already open
1301     if (!log.is_open()) {
1302       // open log file
1303       int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
1304       if (fd != -1) {
1305         out.print_raw("# An error report file with more information is saved as:\n# ");
1306         out.print_raw_cr(buffer);
1307 
1308         log.set_fd(fd);
1309       } else {
1310         out.print_raw_cr("# Can not save log file, dump to screen..");
1311         log.set_fd(defaultStream::output_fd());
1312         /* Error reporting currently needs dumpfile.
1313          * Maybe implement direct streaming in the future.*/
1314         transmit_report_done = true;
1315       }
1316     }
1317 
1318     report(&log, true);
1319     _current_step = 0;
1320     _current_step_info = "";
1321 
1322     // Run error reporting to determine whether or not to report the crash.
1323     if (!transmit_report_done && should_report_bug(_id)) {
1324       transmit_report_done = true;
1325       const int fd2 = ::dup(log.fd());
1326       FILE* const hs_err = ::fdopen(fd2, "r");
1327       if (NULL != hs_err) {
1328         ErrorReporter er;
1329         er.call(hs_err, buffer, O_BUFLEN);
1330       }
1331       ::fclose(hs_err);
1332     }
1333 
1334     if (log.fd() != defaultStream::output_fd()) {
1335       close(log.fd());
1336     }
1337 
1338     log.set_fd(-1);
1339     log_done = true;
1340   }
1341 
1342   static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
1343   if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1344     skip_replay = true;
1345     ciEnv* env = ciEnv::current();
1346     if (env != NULL) {
1347       int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
1348       if (fd != -1) {
1349         FILE* replay_data_file = os::open(fd, "w");
1350         if (replay_data_file != NULL) {
1351           fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1352           env->dump_replay_data_unsafe(&replay_data_stream);
1353           out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1354           out.print_raw_cr(buffer);
1355         } else {
1356           int e = errno;
1357           out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1358           out.print_raw_cr(os::strerror(e));
1359         }
1360       }
1361     }
1362   }
1363 
1364   static bool skip_bug_url = !should_report_bug(_id);
1365   if (!skip_bug_url) {
1366     skip_bug_url = true;
1367 
1368     out.print_raw_cr("#");
1369     print_bug_submit_message(&out, _thread);
1370   }
1371 
1372   static bool skip_OnError = false;
1373   if (!skip_OnError && OnError && OnError[0]) {
1374     skip_OnError = true;
1375 
1376     // Flush output and finish logs before running OnError commands.
1377     ostream_abort();
1378 
1379     out.print_raw_cr("#");
1380     out.print_raw   ("# -XX:OnError=\"");
1381     out.print_raw   (OnError);
1382     out.print_raw_cr("\"");
1383 
1384     char* cmd;
1385     const char* ptr = OnError;
1386     while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1387       out.print_raw   ("#   Executing ");
1388 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1389       out.print_raw   ("/bin/sh -c ");
1390 #elif defined(SOLARIS)
1391       out.print_raw   ("/usr/bin/sh -c ");
1392 #endif
1393       out.print_raw   ("\"");
1394       out.print_raw   (cmd);
1395       out.print_raw_cr("\" ...");
1396 
1397       if (os::fork_and_exec(cmd) < 0) {
1398         out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1399                      os::strerror(errno), os::errno_name(errno), errno);
1400       }
1401     }
1402 
1403     // done with OnError
1404     OnError = NULL;
1405   }
1406 
1407   if (!UseOSErrorReporting) {
1408     // os::abort() will call abort hooks, try it first.
1409     static bool skip_os_abort = false;
1410     if (!skip_os_abort) {
1411       skip_os_abort = true;
1412       bool dump_core = should_report_bug(_id);
1413       os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1414     }
1415 
1416     // if os::abort() doesn't abort, try os::die();
1417     os::die();
1418   }
1419 }
1420 
1421 /*
1422  * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
1423  * ensures utilities such as jmap can observe the process is a consistent state.
1424  */
1425 class VM_ReportJavaOutOfMemory : public VM_Operation {
1426  private:
1427   const char* _message;
1428  public:
1429   VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
1430   VMOp_Type type() const                        { return VMOp_ReportJavaOutOfMemory; }
1431   void doit();
1432 };
1433 
1434 void VM_ReportJavaOutOfMemory::doit() {
1435   // Don't allocate large buffer on stack
1436   static char buffer[O_BUFLEN];
1437 
1438   tty->print_cr("#");
1439   tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
1440   tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
1441 
1442   // make heap parsability
1443   Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
1444 
1445   char* cmd;
1446   const char* ptr = OnOutOfMemoryError;
1447   while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1448     tty->print("#   Executing ");
1449 #if defined(LINUX)
1450     tty->print  ("/bin/sh -c ");
1451 #elif defined(SOLARIS)
1452     tty->print  ("/usr/bin/sh -c ");
1453 #endif
1454     tty->print_cr("\"%s\"...", cmd);
1455 
1456     if (os::fork_and_exec(cmd) < 0) {
1457       tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
1458                      os::strerror(errno), os::errno_name(errno), errno);
1459     }
1460   }
1461 }
1462 
1463 void VMError::report_java_out_of_memory(const char* message) {
1464   if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
1465     MutexLocker ml(Heap_lock);
1466     VM_ReportJavaOutOfMemory op(message);
1467     VMThread::execute(&op);
1468   }
1469 }
1470 
1471 void VMError::show_message_box(char *buf, int buflen) {
1472   bool yes;
1473   do {
1474     error_string(buf, buflen);
1475     yes = os::start_debugging(buf,buflen);
1476   } while (yes);
1477 }
1478 
1479 // Timeout handling: check if a timeout happened (either a single step did
1480 // timeout or the whole of error reporting hit ErrorLogTimeout). Interrupt
1481 // the reporting thread if that is the case.
1482 bool VMError::check_timeout() {
1483 
1484   if (ErrorLogTimeout == 0) {
1485     return false;
1486   }
1487 
1488   // Do not check for timeouts if we still have a message box to show to the
1489   // user or if there are OnError handlers to be run.
1490   if (ShowMessageBoxOnError
1491       || (OnError != NULL && OnError[0] != '\0')
1492       || Arguments::abort_hook() != NULL) {
1493     return false;
1494   }
1495 
1496   const jlong reporting_start_time_l = get_timestamp(&reporting_start_time);
1497   const jlong now = os::javaTimeMillis();
1498   if (reporting_start_time_l > 0) {
1499     const jlong end = reporting_start_time_l + ErrorLogTimeout * 1000;
1500     if (end <= now) {
1501       reporting_timeout = true;
1502       VMError::interrupt_reporting_thread();
1503       return true; // global timeout
1504     }
1505   }
1506 
1507   const jlong step_start_time_l = get_timestamp(&step_start_time);
1508   if (step_start_time_l > 0) {
1509     // A step times out after half of the total timeout. Steps are mostly fast unless they
1510     // hang for some reason, so this simple rule allows for one hanging step and still
1511     // leaves time enough for the rest of the steps to finish.
1512     const jlong end = step_start_time_l + ErrorLogTimeout * 1000 / 2;
1513     if (end <= now) {
1514       step_timeout = true;
1515       VMError::interrupt_reporting_thread();
1516       return false; // (Not a global timeout)
1517     }
1518   }
1519 
1520   return false;
1521 
1522 }
1523