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