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