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