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