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