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