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