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