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