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