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