1 /*
   2  * Copyright (c) 2003, 2026, 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/flags/jvmFlag.hpp"
  47 #include "runtime/frame.inline.hpp"
  48 #include "runtime/init.hpp"
  49 #include "runtime/javaThread.inline.hpp"
  50 #include "runtime/os.inline.hpp"
  51 #include "runtime/osThread.hpp"
  52 #include "runtime/safefetch.hpp"
  53 #include "runtime/safepointMechanism.hpp"
  54 #include "runtime/stackFrameStream.inline.hpp"
  55 #include "runtime/stackOverflow.hpp"
  56 #include "runtime/threads.hpp"
  57 #include "runtime/threadSMR.hpp"
  58 #include "runtime/trimNativeHeap.hpp"
  59 #include "runtime/vm_version.hpp"
  60 #include "runtime/vmOperations.hpp"
  61 #include "runtime/vmThread.hpp"
  62 #include "sanitizers/address.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 Atomic<jlong>     VMError::_reporting_start_time{-1};
  89 Atomic<bool>      VMError::_reporting_did_timeout{false};
  90 Atomic<jlong>     VMError::_step_start_time{-1};
  91 Atomic<bool>      VMError::_step_did_timeout{false};
  92 Atomic<bool>      VMError::_step_did_succeed{false};
  93 Atomic<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 Atomic<Thread*>   VMError::_handshake_timed_out_thread{};
 108 Atomic<Thread*>   VMError::_safepoint_timed_out_thread{};
 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.load_relaxed()) {
 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.load_relaxed() != -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.load_relaxed() == 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   _reporting_start_time.store_relaxed(now);
 563 }
 564 
 565 jlong VMError::get_reporting_start_time() {
 566   return _reporting_start_time.load_relaxed();
 567 }
 568 
 569 void VMError::record_step_start_time() {
 570   const jlong now = get_current_timestamp();
 571   _step_start_time.store_relaxed(now);
 572 }
 573 
 574 jlong VMError::get_step_start_time() {
 575   return _step_start_time.load_relaxed();
 576 }
 577 
 578 void VMError::clear_step_start_time() {
 579   return _step_start_time.store_relaxed(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.store_relaxed(false);                \
 615     _current_step = __LINE__;                              \
 616     {
 617       // [Begin logic]
 618 
 619 # define STEP_IF(s, cond)                                  \
 620     }                                                      \
 621     _step_did_succeed.store_relaxed(true);                 \
 622   }                                                        \
 623   if (_current_step < __LINE__) {                          \
 624     _step_did_succeed.store_relaxed(false);                \
 625     _current_step = __LINE__;                              \
 626     _current_step_info = s;                                \
 627     if ((cond)) {                                          \
 628       record_step_start_time();                            \
 629       _step_did_timeout.store_relaxed(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.store_relaxed(true);                 \
 637   }                                                        \
 638   if (_current_step < __LINE__ && !_step_did_succeed.load_relaxed()) { \
 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.store_relaxed(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       _thread != 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     address sp = (address)fr.sp();
 969     if (sp != nullptr) {
 970       st->print(",  sp=" PTR_FORMAT, p2i(sp));
 971       if (sp >= stack_bottom && sp < stack_top) {
 972         size_t free_stack_size = pointer_delta(sp, stack_bottom, 1024);
 973         st->print(",  free space=%zuk", free_stack_size);
 974       } else {
 975         st->print(" **OUTSIDE STACK**.");
 976       }
 977     }
 978 
 979     st->cr();
 980 
 981   STEP_IF("printing native stack (with source info)", _verbose)
 982 
 983     NativeStackPrinter nsp(_thread, _context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
 984     if (nsp.print_stack(st, buf, sizeof(buf), lastpc,
 985                         true /*print_source_info */, -1 /* max stack */)) {
 986       // We have printed the native stack in platform-specific code
 987       // Windows/x64 needs special handling.
 988       // Stack walking may get stuck. Try to find the calling code.
 989       if (lastpc != nullptr) {
 990         const char* name = find_code_name(lastpc);
 991         if (name != nullptr) {
 992           st->print_cr("The last pc belongs to %s (printed below).", name);
 993         }
 994       }
 995     } else {
 996       _print_stack_from_frame_used = true; // frame-based native stack walk done
 997     }
 998 
 999   REATTEMPT_STEP_IF("retry printing native stack (no source info)", _verbose)
1000     st->cr();
1001     st->print_cr("Retrying call stack printing without source information...");
1002     NativeStackPrinter nsp(_thread, _context, get_filename_only(), _lineno);
1003     nsp.print_stack_from_frame(st, buf, sizeof(buf),
1004                                false /*print_source_info */, -1 /* max stack */);
1005     _print_stack_from_frame_used = true;
1006 
1007   STEP_IF("printing Java stack", _verbose && _thread && _thread->is_Java_thread())
1008     if (_verbose && _thread && _thread->is_Java_thread()) {
1009       print_stack_trace(st, JavaThread::cast(_thread), buf, sizeof(buf));
1010     }
1011 
1012   STEP_IF("printing target Java thread stack",
1013       _verbose && _thread != nullptr && (_thread->is_Named_thread()))
1014     // printing Java thread stack trace if it is involved in GC crash
1015     Thread* thread = ((NamedThread *)_thread)->processed_thread();
1016     if (thread != nullptr && thread->is_Java_thread()) {
1017       JavaThread* jt = JavaThread::cast(thread);
1018       st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id());
1019       print_stack_trace(st, jt, buf, sizeof(buf), true);
1020     }
1021 
1022   STEP_IF("printing siginfo", _verbose && _siginfo != nullptr)
1023     // signal no, signal code, address that caused the fault
1024     st->cr();
1025     os::print_siginfo(st, _siginfo);
1026     st->cr();
1027 
1028   STEP_IF("CDS archive access warning", _verbose && _siginfo != nullptr)
1029     // Print an explicit hint if we crashed on access to the CDS archive.
1030     check_failing_cds_access(st, _siginfo);
1031     st->cr();
1032 
1033 #if defined(COMPILER1) || defined(COMPILER2)
1034   STEP_IF("printing pending compilation failure",
1035           _verbose && _thread != nullptr && _thread->is_Compiler_thread())
1036     CompilationFailureInfo::print_pending_compilation_failure(st);
1037   if (CompilationMemoryStatistic::enabled() && CompilationMemoryStatistic::in_oom_crash()) {
1038     st->cr();
1039     st->print_cr(">> Please see below for a detailed breakdown of compiler memory usage.");
1040     st->cr();
1041   }
1042 #endif
1043 
1044   STEP_IF("printing registers", _verbose && _context != nullptr)
1045     // printing registers
1046     os::print_context(st, _context);
1047     st->cr();
1048 
1049   STEP_IF("printing register info",
1050       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1051     continuation = 0;
1052     ResourceMark rm(_thread);
1053     st->print_cr("Register to memory mapping:");
1054     st->cr();
1055     os::print_register_info(st, _context, continuation);
1056     st->cr();
1057 
1058   REATTEMPT_STEP_IF("printing register info, attempt 2",
1059       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1060     ResourceMark rm(_thread);
1061     os::print_register_info(st, _context, continuation);
1062     st->cr();
1063 
1064   REATTEMPT_STEP_IF("printing register info, attempt 3",
1065       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1066     ResourceMark rm(_thread);
1067     os::print_register_info(st, _context, continuation);
1068     st->cr();
1069 
1070   STEP_IF("printing top of stack, instructions near pc", _verbose && _context != nullptr)
1071     // printing top of stack, instructions near pc
1072     os::print_tos_pc(st, _context);
1073     st->cr();
1074 
1075   STEP_IF("inspecting top of stack",
1076       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1077     continuation = 0;
1078     ResourceMark rm(_thread);
1079     st->print_cr("Stack slot to memory mapping:");
1080     st->cr();
1081     print_stack_location(st, _context, continuation);
1082     st->cr();
1083 
1084   REATTEMPT_STEP_IF("inspecting top of stack, attempt 2",
1085       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1086     ResourceMark rm(_thread);
1087     print_stack_location(st, _context, continuation);
1088     st->cr();
1089 
1090   REATTEMPT_STEP_IF("inspecting top of stack, attempt 3",
1091       _verbose && _context != nullptr && _thread != nullptr && Universe::is_fully_initialized())
1092     ResourceMark rm(_thread);
1093     print_stack_location(st, _context, continuation);
1094     st->cr();
1095 
1096   STEP_IF("printing lock stack", _verbose && _thread != nullptr && _thread->is_Java_thread());
1097     st->print_cr("Lock stack of current Java thread (top to bottom):");
1098     JavaThread::cast(_thread)->lock_stack().print_on(st);
1099     st->cr();
1100 
1101   STEP_IF("printing code blobs if possible", _verbose)
1102     const int printed_capacity = max_error_log_print_code;
1103     address printed[printed_capacity];
1104     printed[0] = nullptr;
1105     int printed_len = 0;
1106     // Even though ErrorLogPrintCodeLimit is ranged checked
1107     // during argument parsing, there's no way to prevent it
1108     // subsequently (i.e., after parsing) being set to a
1109     // value outside the range.
1110     int limit = MIN2(ErrorLogPrintCodeLimit, printed_capacity);
1111     if (limit > 0) {
1112       // Check if a pc was found by native stack trace above.
1113       if (lastpc != nullptr) {
1114         if (print_code(st, _thread, lastpc, true, printed, printed_capacity)) {
1115           printed_len++;
1116         }
1117       }
1118 
1119       // Scan the native stack
1120       if (!_print_stack_from_frame_used) {
1121         // Only try to print code of the crashing frame since
1122         // the native stack cannot be walked with next_frame.
1123         if (print_code(st, _thread, _pc, true, printed, printed_capacity)) {
1124           printed_len++;
1125         }
1126       } else {
1127         frame fr = _context ? os::fetch_frame_from_context(_context)
1128                             : os::current_frame();
1129         while (printed_len < limit && fr.pc() != nullptr) {
1130           if (print_code(st, _thread, fr.pc(), fr.pc() == _pc, printed, printed_capacity)) {
1131             printed_len++;
1132           }
1133           fr = frame::next_frame(fr, _thread);
1134         }
1135       }
1136 
1137       // Scan the Java stack
1138       if (_thread != nullptr && _thread->is_Java_thread()) {
1139         JavaThread* jt = JavaThread::cast(_thread);
1140         if (jt->has_last_Java_frame()) {
1141           for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); printed_len < limit && !sfs.is_done(); sfs.next()) {
1142             address pc = sfs.current()->pc();
1143             if (print_code(st, _thread, pc, pc == _pc, printed, printed_capacity)) {
1144               printed_len++;
1145             }
1146           }
1147         }
1148       }
1149     }
1150 
1151   STEP_IF("printing VM operation", _verbose && _thread != nullptr && _thread->is_VM_thread())
1152     VMThread* t = (VMThread*)_thread;
1153     VM_Operation* op = t->vm_operation();
1154     if (op) {
1155       op->print_on_error(st);
1156       st->cr();
1157       st->cr();
1158     }
1159 
1160   STEP_IF("printing registered callbacks", _verbose && _thread != nullptr);
1161     size_t count = 0;
1162     for (VMErrorCallback* callback = _thread->_vm_error_callbacks;
1163         callback != nullptr;
1164         callback = callback->_next) {
1165       st->print_cr("VMErrorCallback %zu:", ++count);
1166       callback->call(st);
1167       st->cr();
1168     }
1169 
1170   STEP_IF("printing process", _verbose)
1171     st->cr();
1172     st->print_cr("---------------  P R O C E S S  ---------------");
1173     st->cr();
1174 
1175   STEP_IF("printing user info", ExtensiveErrorReports && _verbose)
1176     os::print_user_info(st);
1177 
1178   STEP_IF("printing all threads", _verbose && _thread != nullptr)
1179     // all threads
1180     Threads::print_on_error(st, _thread, buf, sizeof(buf));
1181     st->cr();
1182 
1183   STEP_IF("printing VM state", _verbose)
1184     // Safepoint state
1185     st->print("VM state: ");
1186 
1187     if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
1188     else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
1189     else st->print("not at safepoint");
1190 
1191     // Also see if error occurred during initialization or shutdown
1192     if (!Universe::is_fully_initialized()) {
1193       st->print(" (not fully initialized)");
1194     } else if (VM_Exit::vm_exited()) {
1195       st->print(" (shutting down)");
1196     } else {
1197       st->print(" (normal execution)");
1198     }
1199     st->cr();
1200     st->cr();
1201 
1202   STEP_IF("printing owned locks on error", _verbose)
1203     // mutexes/monitors that currently have an owner
1204     Mutex::print_owned_locks_on_error(st);
1205     st->cr();
1206 
1207   STEP_IF("printing number of OutOfMemoryError and StackOverflow exceptions",
1208       _verbose && Exceptions::has_exception_counts())
1209     st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1210     Exceptions::print_exception_counts_on_error(st);
1211     st->cr();
1212 
1213 #ifdef _LP64
1214   STEP_IF("printing compressed oops mode", _verbose && UseCompressedOops)
1215     CompressedOops::print_mode(st);
1216     st->cr();
1217 
1218   STEP_IF("printing compressed klass pointers mode", _verbose && UseCompressedClassPointers)
1219     CDS_ONLY(AOTMetaspace::print_on(st);)
1220     Metaspace::print_compressed_class_space(st);
1221     CompressedKlassPointers::print_mode(st);
1222     st->cr();
1223 #endif
1224 
1225   STEP_IF("printing heap information", _verbose)
1226     GCLogPrecious::print_on_error(st);
1227 
1228     if (Universe::heap() != nullptr) {
1229       st->print_cr("Heap:");
1230       StreamIndentor si(st, 1);
1231       Universe::heap()->print_heap_on(st);
1232       st->cr();
1233     }
1234 
1235   STEP_IF("printing GC information", _verbose)
1236     if (Universe::heap() != nullptr) {
1237       Universe::heap()->print_gc_on(st);
1238       st->cr();
1239     }
1240 
1241     if (Universe::is_fully_initialized()) {
1242       st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1243       st->cr();
1244     }
1245 
1246   STEP_IF("printing metaspace information", _verbose && Universe::is_fully_initialized())
1247     st->print_cr("Metaspace:");
1248     MetaspaceUtils::print_on(st);
1249     MetaspaceUtils::print_basic_report(st, 0);
1250 
1251   STEP_IF("printing code cache information", _verbose && Universe::is_fully_initialized())
1252     // print code cache information before vm abort
1253     CodeCache::print_summary(st);
1254     st->cr();
1255 
1256   STEP_IF("printing ring buffers", _verbose)
1257     Events::print_all(st);
1258     st->cr();
1259 
1260   STEP_IF("printing dynamic libraries", _verbose)
1261     // dynamic libraries, or memory map
1262     os::print_dll_info(st);
1263     st->cr();
1264 
1265 #if INCLUDE_JVMTI
1266   STEP_IF("printing jvmti agent info", _verbose)
1267     os::print_jvmti_agent_info(st);
1268     st->cr();
1269 #endif
1270 
1271   STEP_IF("printing native decoder state", _verbose)
1272     Decoder::print_state_on(st);
1273     st->cr();
1274 
1275   STEP_IF("printing VM options", _verbose)
1276     // VM options
1277     Arguments::print_on(st);
1278     st->cr();
1279 
1280   STEP_IF("printing flags", _verbose)
1281     JVMFlag::printFlags(
1282       st,
1283       true, // with comments
1284       false, // no ranges
1285       true); // skip defaults
1286     st->cr();
1287 
1288   STEP_IF("printing warning if internal testing API used", WhiteBox::used())
1289     st->print_cr("Unsupported internal testing APIs have been used.");
1290     st->cr();
1291 
1292   STEP_IF("printing log configuration", _verbose)
1293     st->print_cr("Logging:");
1294     LogConfiguration::describe_current_configuration(st);
1295     st->cr();
1296 
1297   STEP_IF("printing release file content", _verbose)
1298     st->print_cr("Release file:");
1299     os::print_image_release_file(st);
1300 
1301   STEP_IF("printing all environment variables", _verbose)
1302     os::print_environment_variables(st, env_list);
1303     st->cr();
1304 
1305   STEP_IF("printing locale settings", _verbose)
1306     os::print_active_locale(st);
1307     st->cr();
1308 
1309   STEP_IF("printing signal handlers", _verbose)
1310     os::print_signal_handlers(st, buf, sizeof(buf));
1311     st->cr();
1312 
1313   STEP_IF("Native Memory Tracking", _verbose && _thread != nullptr)
1314     MemTracker::error_report(st);
1315     st->cr();
1316 
1317   STEP_IF("printing compiler memory info, if any", _verbose)
1318     CompilationMemoryStatistic::print_error_report(st);
1319     st->cr();
1320 
1321   STEP_IF("printing periodic trim state", _verbose)
1322     NativeHeapTrimmer::print_state(st);
1323     st->cr();
1324 
1325   STEP_IF("printing system", _verbose)
1326     st->print_cr("---------------  S Y S T E M  ---------------");
1327     st->cr();
1328 
1329   STEP_IF("printing OS information", _verbose)
1330     os::print_os_info(st);
1331     st->cr();
1332 #ifdef __APPLE__
1333     // Avoid large stack allocation on Mac for FD count during signal-handling.
1334     os::Bsd::print_open_file_descriptors(st, buf, sizeof(buf));
1335     st->cr();
1336 #else
1337     os::print_open_file_descriptors(st);
1338 #endif
1339 
1340   STEP_IF("printing CPU info", _verbose)
1341     os::print_cpu_info(st, buf, sizeof(buf));
1342     st->cr();
1343 
1344   STEP_IF("printing memory info", _verbose)
1345     os::print_memory_info(st);
1346     st->cr();
1347 
1348   STEP_IF("printing internal vm info", _verbose)
1349     st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1350     st->cr();
1351 
1352   // print a defined marker to show that error handling finished correctly.
1353   STEP_IF("printing end marker", _verbose)
1354     st->print_cr("END.");
1355 
1356   END
1357 
1358 # undef BEGIN
1359 # undef STEP_IF
1360 # undef STEP
1361 # undef REATTEMPT_STEP_IF
1362 # undef END
1363 }
1364 
1365 void VMError::set_handshake_timed_out_thread(Thread* thread) {
1366   // Only preserve the first thread to time-out this way. The atomic operation ensures
1367   // visibility to the target thread.
1368   _handshake_timed_out_thread.compare_exchange(nullptr, thread);
1369 }
1370 
1371 void VMError::set_safepoint_timed_out_thread(Thread* thread) {
1372   // Only preserve the first thread to time-out this way. The atomic operation ensures
1373   // visibility to the target thread.
1374   _safepoint_timed_out_thread.compare_exchange(nullptr, thread);
1375 }
1376 
1377 Thread* VMError::get_handshake_timed_out_thread() {
1378   return _handshake_timed_out_thread.load_relaxed();
1379 }
1380 
1381 Thread* VMError::get_safepoint_timed_out_thread() {
1382   return _safepoint_timed_out_thread.load_relaxed();
1383 }
1384 
1385 // Report for the vm_info_cmd. This prints out the information above omitting
1386 // crash and thread specific information.  If output is added above, it should be added
1387 // here also, if it is safe to call during a running process.
1388 void VMError::print_vm_info(outputStream* st) {
1389 
1390   char buf[O_BUFLEN];
1391   os::prepare_native_symbols();
1392 
1393   report_vm_version(st, buf, sizeof(buf));
1394 
1395   // STEP("printing summary")
1396 
1397   st->cr();
1398   st->print_cr("---------------  S U M M A R Y ------------");
1399   st->cr();
1400 
1401   // STEP("printing VM option summary")
1402 
1403   // VM options
1404   Arguments::print_summary_on(st);
1405   st->cr();
1406 
1407   // STEP("printing summary machine and OS info")
1408 
1409   os::print_summary_info(st, buf, sizeof(buf));
1410 
1411   // STEP("printing date and time")
1412 
1413   os::print_date_and_time(st, buf, sizeof(buf));
1414 
1415   // Skip: STEP("printing thread")
1416 
1417   // STEP("printing process")
1418 
1419   st->cr();
1420   st->print_cr("---------------  P R O C E S S  ---------------");
1421   st->cr();
1422 
1423   // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
1424 
1425   if (Exceptions::has_exception_counts()) {
1426     st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1427     Exceptions::print_exception_counts_on_error(st);
1428     st->cr();
1429   }
1430 
1431 #ifdef _LP64
1432   // STEP("printing compressed oops mode")
1433   if (UseCompressedOops) {
1434     CompressedOops::print_mode(st);
1435     st->cr();
1436   }
1437 #endif
1438 
1439   // STEP("printing compressed class ptrs mode")
1440   if (UseCompressedClassPointers) {
1441     CDS_ONLY(AOTMetaspace::print_on(st);)
1442     Metaspace::print_compressed_class_space(st);
1443     CompressedKlassPointers::print_mode(st);
1444     st->cr();
1445   }
1446 
1447   // Take heap lock over heap, GC and metaspace printing so that information
1448   // is consistent.
1449   if (Universe::is_fully_initialized()) {
1450     MutexLocker ml(Heap_lock);
1451 
1452     // STEP("printing heap information")
1453 
1454     GCLogPrecious::print_on_error(st);
1455 
1456     {
1457       st->print_cr("Heap:");
1458       StreamIndentor si(st, 1);
1459       Universe::heap()->print_heap_on(st);
1460       st->cr();
1461     }
1462 
1463     // STEP("printing GC information")
1464 
1465     Universe::heap()->print_gc_on(st);
1466     st->cr();
1467 
1468     st->print_cr("Polling page: " PTR_FORMAT, p2i(SafepointMechanism::get_polling_page()));
1469     st->cr();
1470 
1471     // STEP("printing metaspace information")
1472 
1473     st->print_cr("Metaspace:");
1474     MetaspaceUtils::print_on(st);
1475     MetaspaceUtils::print_basic_report(st, 0);
1476   }
1477 
1478   // STEP("printing code cache information")
1479 
1480   if (Universe::is_fully_initialized()) {
1481     // print code cache information before vm abort
1482     CodeCache::print_summary(st);
1483     st->cr();
1484   }
1485 
1486   // STEP("printing ring buffers")
1487 
1488   Events::print_all(st);
1489   st->cr();
1490 
1491   // STEP("printing dynamic libraries")
1492 
1493   // dynamic libraries, or memory map
1494   os::print_dll_info(st);
1495   st->cr();
1496 
1497 #if INCLUDE_JVMTI
1498   os::print_jvmti_agent_info(st);
1499   st->cr();
1500 #endif
1501 
1502   // STEP("printing VM options")
1503 
1504   // VM options
1505   Arguments::print_on(st);
1506   st->cr();
1507 
1508   // STEP("printing warning if internal testing API used")
1509 
1510   if (WhiteBox::used()) {
1511     st->print_cr("Unsupported internal testing APIs have been used.");
1512     st->cr();
1513   }
1514 
1515   // STEP("printing log configuration")
1516   st->print_cr("Logging:");
1517   LogConfiguration::describe(st);
1518   st->cr();
1519 
1520   // STEP("printing release file content")
1521   st->print_cr("Release file:");
1522   os::print_image_release_file(st);
1523 
1524   // STEP("printing all environment variables")
1525 
1526   os::print_environment_variables(st, env_list);
1527   st->cr();
1528 
1529   // STEP("printing locale settings")
1530 
1531   os::print_active_locale(st);
1532   st->cr();
1533 
1534 
1535   // STEP("printing signal handlers")
1536 
1537   os::print_signal_handlers(st, buf, sizeof(buf));
1538   st->cr();
1539 
1540   // STEP("Native Memory Tracking")
1541   MemTracker::error_report(st);
1542   st->cr();
1543 
1544   // STEP("Compiler Memory Statistic")
1545   CompilationMemoryStatistic::print_final_report(st);
1546 
1547   // STEP("printing periodic trim state")
1548   NativeHeapTrimmer::print_state(st);
1549   st->cr();
1550 
1551 
1552   // STEP("printing system")
1553   st->print_cr("---------------  S Y S T E M  ---------------");
1554   st->cr();
1555 
1556   // STEP("printing OS information")
1557 
1558   os::print_os_info(st);
1559   st->cr();
1560   os::print_open_file_descriptors(st);
1561   st->cr();
1562 
1563   // STEP("printing CPU info")
1564 
1565   os::print_cpu_info(st, buf, sizeof(buf));
1566   st->cr();
1567 
1568   // STEP("printing memory info")
1569 
1570   os::print_memory_info(st);
1571   st->cr();
1572 
1573   // STEP("printing internal vm info")
1574 
1575   st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string());
1576   st->cr();
1577 
1578   // print a defined marker to show that error handling finished correctly.
1579   // STEP("printing end marker")
1580 
1581   st->print_cr("END.");
1582 }
1583 
1584 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
1585 static int expand_and_open(const char* pattern, bool overwrite_existing, char* buf, size_t buflen, size_t pos) {
1586   int fd = -1;
1587   int mode = O_RDWR | O_CREAT;
1588   if (overwrite_existing) {
1589     mode |= O_TRUNC;
1590   } else {
1591     mode |= O_EXCL;
1592   }
1593   if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1594     fd = open(buf, mode, 0666);
1595   }
1596   return fd;
1597 }
1598 
1599 /**
1600  * Construct file name for a log file and return it's file descriptor.
1601  * Name and location depends on pattern, default_pattern params and access
1602  * permissions.
1603  */
1604 int VMError::prepare_log_file(const char* pattern, const char* default_pattern, bool overwrite_existing, char* buf, size_t buflen) {
1605   int fd = -1;
1606 
1607   // If possible, use specified pattern to construct log file name
1608   if (pattern != nullptr) {
1609     fd = expand_and_open(pattern, overwrite_existing, buf, buflen, 0);
1610   }
1611 
1612   // Either user didn't specify, or the user's location failed,
1613   // so use the default name in the current directory
1614   if (fd == -1) {
1615     const char* cwd = os::get_current_directory(buf, buflen);
1616     if (cwd != nullptr) {
1617       size_t pos = strlen(cwd);
1618       int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1619       pos += fsep_len;
1620       if (fsep_len > 0) {
1621         fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1622       }
1623     }
1624   }
1625 
1626    // try temp directory if it exists.
1627    if (fd == -1) {
1628      const char* tmpdir = os::get_temp_directory();
1629      if (tmpdir != nullptr && strlen(tmpdir) > 0) {
1630        int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1631        if (pos > 0) {
1632          fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos);
1633        }
1634      }
1635    }
1636 
1637   return fd;
1638 }
1639 
1640 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo,
1641                              const void* context, const char* detail_fmt, ...)
1642 {
1643   va_list detail_args;
1644   va_start(detail_args, detail_fmt);
1645   report_and_die(sig, nullptr, detail_fmt, detail_args, thread, pc, siginfo, context, nullptr, 0, 0);
1646   va_end(detail_args);
1647 }
1648 
1649 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1650                              const char* detail_fmt, ...) {
1651   va_list detail_args;
1652   va_start(detail_args, detail_fmt);
1653   report_and_die(thread, context, filename, lineno, message, detail_fmt, detail_args);
1654   va_end(detail_args);
1655 }
1656 
1657 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, const void* siginfo, const void* context)
1658 {
1659   if (ExecutingUnitTests) {
1660     // See TEST_VM_CRASH_SIGNAL gtest macro
1661     char tmp[64];
1662     fprintf(stderr, "signaled: %s", os::exception_name(sig, tmp, sizeof(tmp)));
1663   }
1664 
1665   report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1666 }
1667 
1668 void VMError::report_and_die(Thread* thread, const void* context, const char* filename, int lineno, const char* message,
1669                              const char* detail_fmt, va_list detail_args)
1670 {
1671   report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, nullptr, nullptr, context, filename, lineno, 0);
1672 }
1673 
1674 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1675                              VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1676   report_and_die(vm_err_type, nullptr, detail_fmt, detail_args, thread, nullptr, nullptr, nullptr, filename, lineno, size);
1677 }
1678 
1679 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1680                              Thread* thread, address pc, const void* siginfo, const void* context, const char* filename,
1681                              int lineno, size_t size)
1682 {
1683   // A single scratch buffer to be used from here on.
1684   // Do not rely on it being preserved across function calls.
1685   static char buffer[O_BUFLEN];
1686 
1687   // File descriptor to tty to print an error summary to.
1688   // Hard wired to stdout; see JDK-8215004 (compatibility concerns).
1689   static const int fd_out = 1; // stdout
1690 
1691   // File descriptor to the error log file.
1692   static int fd_log = -1;
1693 
1694 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT
1695   // Disarm assertion poison page, since from this point on we do not need this mechanism anymore and it may
1696   // cause problems in error handling during native OOM, see JDK-8227275.
1697   disarm_assert_poison();
1698 #endif
1699 
1700   // Use local fdStream objects only. Do not use global instances whose initialization
1701   // relies on dynamic initialization (see JDK-8214975). Do not rely on these instances
1702   // to carry over into recursions or invocations from other threads.
1703   fdStream out(fd_out);
1704   out.set_scratch_buffer(buffer, sizeof(buffer));
1705 
1706   // Depending on the re-entrance depth at this point, fd_log may be -1 or point to an open hs-err file.
1707   fdStream log(fd_log);
1708   log.set_scratch_buffer(buffer, sizeof(buffer));
1709 
1710   // How many errors occurred in error handler when reporting first_error.
1711   static int recursive_error_count;
1712 
1713   // We will first print a brief message to standard out (verbose = false),
1714   // then save detailed information in log file (verbose = true).
1715   static bool out_done = false;         // done printing to standard out
1716   static bool log_done = false;         // done saving error log
1717 
1718   intptr_t mytid = os::current_thread_id();
1719   if (_first_error_tid.compare_set(-1, mytid)) {
1720 
1721     if (SuppressFatalErrorMessage) {
1722       os::abort(CreateCoredumpOnCrash);
1723     }
1724 
1725     // Initialize time stamps to use the same base.
1726     out.time_stamp().update_to(1);
1727     log.time_stamp().update_to(1);
1728 
1729     _id = id;
1730     _message = message;
1731     _thread = thread;
1732     _pc = pc;
1733     _siginfo = siginfo;
1734     _context = context;
1735     _filename = filename;
1736     _lineno = lineno;
1737     _size = size;
1738     jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1739 
1740     reporting_started();
1741     if (!TestUnresponsiveErrorHandler) {
1742       // Record reporting_start_time unless we're running the
1743       // TestUnresponsiveErrorHandler test. For that test we record
1744       // reporting_start_time at the beginning of the test.
1745       record_reporting_start_time();
1746     } else {
1747       out.print_raw_cr("Delaying recording reporting_start_time for TestUnresponsiveErrorHandler.");
1748     }
1749 
1750     if (ShowMessageBoxOnError || PauseAtExit) {
1751       show_message_box(buffer, sizeof(buffer));
1752 
1753       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1754       // WatcherThread can kill JVM if the error handler hangs.
1755       ShowMessageBoxOnError = false;
1756     }
1757 
1758     os::check_core_dump_prerequisites(buffer, sizeof(buffer));
1759 
1760     // reset signal handlers or exception filter; make sure recursive crashes
1761     // are handled properly.
1762     install_secondary_signal_handler();
1763   } else {
1764     // This is not the first error, see if it happened in a different thread
1765     // or in the same thread during error reporting.
1766     if (_first_error_tid.load_relaxed() != mytid) {
1767       if (!SuppressFatalErrorMessage) {
1768         char msgbuf[64];
1769         jio_snprintf(msgbuf, sizeof(msgbuf),
1770                      "[thread %zd also had an error]",
1771                      mytid);
1772         out.print_raw_cr(msgbuf);
1773       }
1774 
1775       // Error reporting is not MT-safe, nor can we let the current thread
1776       // proceed, so we block it.
1777       os::infinite_sleep();
1778 
1779     } else {
1780       if (recursive_error_count++ > 30) {
1781         if (!SuppressFatalErrorMessage) {
1782           out.print_raw_cr("[Too many errors, abort]");
1783         }
1784         os::die();
1785       }
1786 
1787       if (SuppressFatalErrorMessage) {
1788         // If we already hit a secondary error during abort, then calling
1789         // it again is likely to hit another one. But eventually, if we
1790         // don't deadlock somewhere, we will call os::die() above.
1791         os::abort(CreateCoredumpOnCrash);
1792       }
1793 
1794       outputStream* const st = log.is_open() ? &log : &out;
1795       st->cr();
1796 
1797       // Timeout handling.
1798       if (_step_did_timeout.load_relaxed()) {
1799         // The current step had a timeout. Lets continue reporting with the next step.
1800         st->print_raw("[timeout occurred during error reporting in step \"");
1801         st->print_raw(_current_step_info);
1802         st->print_cr("\"] after " INT64_FORMAT " s.",
1803                      (int64_t)
1804                      ((get_current_timestamp() - get_step_start_time()) / TIMESTAMP_TO_SECONDS_FACTOR));
1805       } else if (_reporting_did_timeout.load_relaxed()) {
1806         // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things
1807         // up, the process is about to be stopped by the WatcherThread.
1808         st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------",
1809                      (int64_t)
1810                      ((get_current_timestamp() - get_reporting_start_time()) / TIMESTAMP_TO_SECONDS_FACTOR));
1811         st->flush();
1812         // Watcherthread is about to call os::die. Lets just wait.
1813         os::infinite_sleep();
1814       } else {
1815         // A secondary error happened. Print brief information, but take care, since crashing
1816         // here would just recurse endlessly.
1817         // Any information (signal, context, siginfo etc) printed here should use the function
1818         // arguments, not the information stored in *this, since those describe the primary crash.
1819         static char tmp[256]; // cannot use global scratch buffer
1820         // Note: this string does get parsed by a number of jtreg tests,
1821         // see hotspot/jtreg/runtime/ErrorHandling.
1822         st->print("[error occurred during error reporting (%s), id 0x%x",
1823                    _current_step_info, id);
1824         if (os::exception_name(id, tmp, sizeof(tmp))) {
1825           st->print(", %s (0x%x) at pc=" PTR_FORMAT, tmp, id, p2i(pc));
1826         } else {
1827           if (should_report_bug(id)) {
1828             st->print(", Internal Error (%s:%d)",
1829               filename == nullptr ? "??" : filename, lineno);
1830           } else {
1831             st->print(", Out of Memory Error (%s:%d)",
1832               filename == nullptr ? "??" : filename, lineno);
1833           }
1834         }
1835         st->print_cr("]");
1836         if (ErrorLogSecondaryErrorDetails) {
1837           static bool recursed = false;
1838           if (!recursed) {
1839             recursed = true;
1840             // Print even more information for secondary errors. This may generate a lot of output
1841             // and possibly disturb error reporting, therefore its optional and only available in debug builds.
1842             if (siginfo != nullptr) {
1843               st->print("[");
1844               os::print_siginfo(st, siginfo);
1845               st->print_cr("]");
1846             }
1847             st->print("[stack: ");
1848             NativeStackPrinter nsp(_thread, context, _filename != nullptr ? get_filename_only() : nullptr, _lineno);
1849             // Subsequent secondary errors build up stack; to avoid flooding the hs-err file with irrelevant
1850             // call stacks, limit the stack we print here (we are only interested in what happened before the
1851             // last assert/fault).
1852             const int max_stack_size = 15;
1853             nsp.print_stack_from_frame(st, tmp, sizeof(tmp), true /* print_source_info */, max_stack_size);
1854             st->print_cr("]");
1855           } // !recursed
1856           recursed = false; // Note: reset outside !recursed
1857         }
1858       }
1859     }
1860   }
1861 
1862   // Part 1: print an abbreviated version (the '#' section) to stdout.
1863   if (!out_done) {
1864     // Suppress this output if we plan to print Part 2 to stdout too.
1865     // No need to have the "#" section twice.
1866     if (!(ErrorFileToStdout && out.fd() == 1)) {
1867       report(&out, false);
1868     }
1869 
1870     out_done = true;
1871 
1872     _current_step = 0;
1873     _current_step_info = "";
1874   }
1875 
1876   // Part 2: print a full error log file (optionally to stdout or stderr).
1877   // print to error log file
1878   if (!log_done) {
1879     // see if log file is already open
1880     if (!log.is_open()) {
1881       // open log file
1882       if (ErrorFileToStdout) {
1883         fd_log = 1;
1884       } else if (ErrorFileToStderr) {
1885         fd_log = 2;
1886       } else {
1887         fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", true,
1888                  buffer, sizeof(buffer));
1889         if (fd_log != -1) {
1890           out.print_raw("# An error report file with more information is saved as:\n# ");
1891           out.print_raw_cr(buffer);
1892         } else {
1893           out.print_raw_cr("# Can not save log file, dump to screen..");
1894           fd_log = 1;
1895         }
1896       }
1897       log.set_fd(fd_log);
1898     }
1899 
1900     report(&log, true);
1901     log_done = true;
1902     _current_step = 0;
1903     _current_step_info = "";
1904 
1905     if (fd_log > 3) {
1906       ::close(fd_log);
1907       fd_log = -1;
1908     }
1909 
1910     log.set_fd(-1);
1911   }
1912 
1913   JFR_ONLY(Jfr::on_vm_shutdown(true, false, static_cast<VMErrorType>(_id) == OOM_JAVA_HEAP_FATAL);)
1914 
1915   if (PrintNMTStatistics) {
1916     fdStream fds(fd_out);
1917     MemTracker::final_report(&fds);
1918   }
1919 
1920   static bool skip_replay = ReplayCompiles && !ReplayReduce; // Do not overwrite file during replay
1921   if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1922     skip_replay = true;
1923     ciEnv* env = ciEnv::current();
1924     if (env != nullptr && env->task() != nullptr) {
1925       const bool overwrite = false; // We do not overwrite an existing replay file.
1926       int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", overwrite, buffer, sizeof(buffer));
1927       if (fd != -1) {
1928         FILE* replay_data_file = os::fdopen(fd, "w");
1929         if (replay_data_file != nullptr) {
1930           fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1931           env->dump_replay_data_unsafe(&replay_data_stream);
1932           out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1933           out.print_raw_cr(buffer);
1934         } else {
1935           int e = errno;
1936           out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1937           out.print_raw_cr(os::strerror(e));
1938           close(fd);
1939         }
1940       }
1941     }
1942   }
1943 
1944 #if INCLUDE_JVMCI
1945   if (JVMCI::fatal_log_filename() != nullptr) {
1946     out.print_raw("#\n# The JVMCI shared library error report file is saved as:\n# ");
1947     out.print_raw_cr(JVMCI::fatal_log_filename());
1948   }
1949 #endif
1950 
1951   static bool skip_bug_url = !should_submit_bug_report(_id);
1952   if (!skip_bug_url) {
1953     skip_bug_url = true;
1954 
1955     out.print_raw_cr("#");
1956     print_bug_submit_message(&out, _thread);
1957   }
1958 
1959   static bool skip_OnError = false;
1960   if (!skip_OnError && OnError && OnError[0]) {
1961     skip_OnError = true;
1962 
1963     // Flush output and finish logs before running OnError commands.
1964     ostream_abort();
1965 
1966     out.print_raw_cr("#");
1967     out.print_raw   ("# -XX:OnError=\"");
1968     out.print_raw   (OnError);
1969     out.print_raw_cr("\"");
1970 
1971     char* cmd;
1972     const char* ptr = OnError;
1973     while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
1974       out.print_raw   ("#   Executing ");
1975 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1976       out.print_raw   ("/bin/sh -c ");
1977 #elif defined(_WINDOWS)
1978       out.print_raw   ("cmd /C ");
1979 #endif
1980       out.print_raw   ("\"");
1981       out.print_raw   (cmd);
1982       out.print_raw_cr("\" ...");
1983 
1984       if (os::fork_and_exec(cmd) < 0) {
1985         out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1986                      os::strerror(errno), os::errno_name(errno), errno);
1987       }
1988     }
1989 
1990     // done with OnError
1991     OnError = nullptr;
1992   }
1993 
1994 #if defined _WINDOWS
1995   if (UseOSErrorReporting) {
1996     raise_fail_fast(_siginfo, _context);
1997   }
1998 #endif // _WINDOWS
1999 
2000   // os::abort() will call abort hooks, try it first.
2001   static bool skip_os_abort = false;
2002   if (!skip_os_abort) {
2003     skip_os_abort = true;
2004     bool dump_core = should_report_bug(_id);
2005     os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
2006     // if os::abort() doesn't abort, try os::die();
2007   }
2008   os::die();
2009 }
2010 
2011 /*
2012  * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
2013  * ensures utilities such as jmap can observe the process is a consistent state.
2014  */
2015 class VM_ReportJavaOutOfMemory : public VM_Operation {
2016  private:
2017   const char* _message;
2018  public:
2019   VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
2020   VMOp_Type type() const                        { return VMOp_ReportJavaOutOfMemory; }
2021   void doit();
2022 };
2023 
2024 void VM_ReportJavaOutOfMemory::doit() {
2025   // Don't allocate large buffer on stack
2026   static char buffer[O_BUFLEN];
2027 
2028   tty->print_cr("#");
2029   tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
2030   tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
2031 
2032   // make heap parsability
2033   Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
2034 
2035   char* cmd;
2036   const char* ptr = OnOutOfMemoryError;
2037   while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != nullptr){
2038     tty->print("#   Executing ");
2039 #if defined(LINUX)
2040     tty->print  ("/bin/sh -c ");
2041 #endif
2042     tty->print_cr("\"%s\"...", cmd);
2043 
2044     if (os::fork_and_exec(cmd) < 0) {
2045       tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
2046                      os::strerror(errno), os::errno_name(errno), errno);
2047     }
2048   }
2049 }
2050 
2051 void VMError::report_java_out_of_memory(const char* message) {
2052   if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
2053     MutexLocker ml(Heap_lock);
2054     VM_ReportJavaOutOfMemory op(message);
2055     VMThread::execute(&op);
2056   }
2057 }
2058 
2059 void VMError::show_message_box(char *buf, int buflen) {
2060   bool yes;
2061   do {
2062     error_string(buf, buflen);
2063     yes = os::start_debugging(buf,buflen);
2064   } while (yes);
2065 }
2066 
2067 // Fatal error handling is subject to several timeouts:
2068 // - a global timeout (controlled via ErrorLogTimeout)
2069 // - local error reporting step timeouts.
2070 //
2071 // The latter aims to "give the JVM a kick" if it gets stuck in one particular place during
2072 // error reporting. This prevents one error reporting step from hogging all the time allotted
2073 // to error reporting under ErrorLogTimeout.
2074 //
2075 // VMError::check_timeout() is called from the watcher thread and checks for either global
2076 // or step timeout. If a timeout happened, we interrupt the reporting thread and set either
2077 // _reporting_did_timeout or _step_did_timeout to signal which timeout fired. Function returns
2078 // true if the *global* timeout fired, which will cause WatcherThread to shut down the JVM
2079 // immediately.
2080 bool VMError::check_timeout() {
2081 
2082   // This function is supposed to be called from watcher thread during fatal error handling only.
2083   assert(VMError::is_error_reported(), "Only call during error handling");
2084   assert(Thread::current()->is_Watcher_thread(), "Only call from watcher thread");
2085 
2086   if (ErrorLogTimeout == 0) {
2087     return false;
2088   }
2089 
2090   // There are three situations where we suppress the *global* error timeout:
2091   // - if the JVM is embedded and the launcher has its abort hook installed.
2092   //   That must be allowed to run.
2093   // - if the user specified one or more OnError commands to run, and these
2094   //   did not yet run. These must have finished.
2095   // - if the user (typically developer) specified ShowMessageBoxOnError,
2096   //   and the error box has not yet been shown
2097   const bool ignore_global_timeout =
2098       (ShowMessageBoxOnError
2099             || (OnError != nullptr && OnError[0] != '\0')
2100             || Arguments::abort_hook() != nullptr);
2101 
2102   const jlong now = get_current_timestamp();
2103 
2104   // Global timeout hit?
2105   if (!ignore_global_timeout) {
2106     const jlong reporting_start_time = get_reporting_start_time();
2107     // Timestamp is stored in nanos.
2108     if (reporting_start_time > 0) {
2109       const jlong end = reporting_start_time + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR;
2110       if (end <= now && !_reporting_did_timeout.load_relaxed()) {
2111         // We hit ErrorLogTimeout and we haven't interrupted the reporting
2112         // thread yet.
2113         _reporting_did_timeout.store_relaxed(true);
2114         interrupt_reporting_thread();
2115         return true; // global timeout
2116       }
2117     }
2118   }
2119 
2120   // Reporting step timeout?
2121   const jlong step_start_time = get_step_start_time();
2122   if (step_start_time > 0) {
2123     // A step times out after a quarter of the total timeout. Steps are mostly fast unless they
2124     // hang for some reason, so this simple rule allows for three hanging step and still
2125     // hopefully leaves time enough for the rest of the steps to finish.
2126     const int max_step_timeout_secs = 5;
2127     const jlong timeout_duration = MAX2((jlong)max_step_timeout_secs, (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4);
2128     const jlong end = step_start_time + timeout_duration;
2129     if (end <= now && !_step_did_timeout.load_relaxed()) {
2130       // The step timed out and we haven't interrupted the reporting
2131       // thread yet.
2132       _step_did_timeout.store_relaxed(true);
2133       interrupt_reporting_thread();
2134       return false; // (Not a global timeout)
2135     }
2136   }
2137 
2138   return false;
2139 
2140 }
2141 
2142 #ifdef ASSERT
2143 typedef void (*voidfun_t)();
2144 
2145 // Crash with an authentic sigfpe; behavior is subtly different from a real signal
2146 // compared to one generated with raise (asynchronous vs synchronous). See JDK-8065895.
2147 volatile int sigfpe_int = 0;
2148 
2149 ATTRIBUTE_NO_UBSAN
2150 static void ALWAYSINLINE crash_with_sigfpe() {
2151 
2152   // generate a native synchronous SIGFPE where possible;
2153   sigfpe_int = sigfpe_int/sigfpe_int;
2154 
2155   // if that did not cause a signal (e.g. on ppc), just
2156   // raise the signal.
2157 #ifndef _WIN32
2158   // OSX implements raise(sig) incorrectly so we need to
2159   // explicitly target the current thread
2160   pthread_kill(pthread_self(), SIGFPE);
2161 #endif
2162 
2163 } // end: crash_with_sigfpe
2164 
2165 // crash with sigsegv at non-null address.
2166 static void ALWAYSINLINE crash_with_segfault() {
2167 
2168   int* crash_addr = reinterpret_cast<int*>(VMError::segfault_address);
2169   *crash_addr = 1;
2170 
2171 } // end: crash_with_segfault
2172 
2173 // crash in a controlled way:
2174 // 1  - assert
2175 // 2  - guarantee
2176 // 14 - SIGSEGV
2177 // 15 - SIGFPE
2178 void VMError::controlled_crash(int how) {
2179 
2180   // Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java.
2181   // Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java.
2182   // Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java.
2183   // Case 17 is tested by test/hotspot/jtreg/runtime/ErrorHandling/NestedThreadsListHandleInErrorHandlingTest.java.
2184 
2185   // We try to grab Threads_lock to keep ThreadsSMRSupport::print_info_on()
2186   // from racing with Threads::add() or Threads::remove() as we
2187   // generate the hs_err_pid file. This makes our ErrorHandling tests
2188   // more stable.
2189   if (!Threads_lock->owned_by_self()) {
2190     Threads_lock->try_lock();
2191     // The VM is going to die so no need to unlock Thread_lock.
2192   }
2193 
2194   switch (how) {
2195     case 1: assert(how == 0, "test assert"); break;
2196     case 2: guarantee(how == 0, "test guarantee"); break;
2197 
2198     // The other cases are unused.
2199     case 14: crash_with_segfault(); break;
2200     case 15: crash_with_sigfpe(); break;
2201     case 16: {
2202       ThreadsListHandle tlh;
2203       fatal("Force crash with an active ThreadsListHandle.");
2204     }
2205     case 17: {
2206       ThreadsListHandle tlh;
2207       {
2208         ThreadsListHandle tlh2;
2209         fatal("Force crash with a nested ThreadsListHandle.");
2210       }
2211     }
2212     case 18: {
2213       // Trigger an error that should cause ASAN to report a double free or use-after-free.
2214       // Please note that this is not 100% bullet-proof since it assumes that this block
2215       // is not immediately repurposed by some other thread after free.
2216       void* const p = os::malloc(4096, mtTest);
2217       os::free(p);
2218       os::free(p);
2219     }
2220     default:
2221       // If another number is given, give a generic crash.
2222       fatal("Crashing with number %d", how);
2223   }
2224   tty->print_cr("controlled_crash: survived intentional crash. Did you suppress the assert?");
2225   ShouldNotReachHere();
2226 }
2227 #endif // !ASSERT
2228 
2229 VMErrorCallbackMark::VMErrorCallbackMark(VMErrorCallback* callback)
2230   : _thread(Thread::current()) {
2231   callback->_next = _thread->_vm_error_callbacks;
2232   _thread->_vm_error_callbacks = callback;
2233 }
2234 
2235 VMErrorCallbackMark::~VMErrorCallbackMark() {
2236   assert(_thread->_vm_error_callbacks != nullptr, "Popped too far");
2237   _thread->_vm_error_callbacks = _thread->_vm_error_callbacks->_next;
2238 }