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