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