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