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