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