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