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