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