rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed

   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 "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "code/codeCache.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "logging/logConfiguration.hpp"
  32 #include "prims/whitebox.hpp"
  33 #include "runtime/arguments.hpp"
  34 #include "runtime/atomic.hpp"
  35 #include "runtime/frame.inline.hpp"
  36 #include "runtime/init.hpp"
  37 #include "runtime/os.hpp"
  38 #include "runtime/thread.inline.hpp"

  39 #include "runtime/vmThread.hpp"
  40 #include "runtime/vm_operations.hpp"
  41 #include "runtime/vm_version.hpp"
  42 #include "services/memTracker.hpp"
  43 #include "trace/traceMacros.hpp"
  44 #include "utilities/debug.hpp"
  45 #include "utilities/decoder.hpp"
  46 #include "utilities/defaultStream.hpp"
  47 #include "utilities/errorReporter.hpp"
  48 #include "utilities/events.hpp"
  49 #include "utilities/vmError.hpp"
  50 
  51 #ifndef PRODUCT
  52 #include <signal.h>
  53 #endif // PRODUCT
  54 
  55 bool VMError::_error_reported = false;
  56 
  57 // call this when the VM is dying--it might loosen some asserts
  58 bool VMError::is_error_reported() { return _error_reported; }
  59 
  60 // returns an address which is guaranteed to generate a SIGSEGV on read,
  61 // for test purposes, which is not NULL and contains bits in every word
  62 void* VMError::get_segfault_address() {
  63   return (void*)
  64 #ifdef _LP64
  65     0xABC0000000000ABCULL;
  66 #else
  67     0x00000ABC;
  68 #endif
  69 }
  70 
  71 // List of environment variables that should be reported in error log file.
  72 const char *env_list[] = {
  73   // All platforms
  74   "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
  75   "JAVA_COMPILER", "PATH", "USERNAME",
  76 
  77   // Env variables that are defined on Solaris/Linux/BSD
  78   "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
  79   "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
  80 
  81   // defined on Linux
  82   "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
  83 
  84   // defined on Darwin
  85   "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
  86   "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
  87   "DYLD_INSERT_LIBRARIES",
  88 
  89   // defined on Windows
  90   "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
  91 
  92   (const char *)0
  93 };
  94 
  95 // A simple parser for -XX:OnError, usage:
  96 //  ptr = OnError;
  97 //  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
  98 //     ... ...
  99 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
 100   if (ptr == NULL || *ptr == NULL) return NULL;
 101 
 102   const char* cmd = *ptr;
 103 
 104   // skip leading blanks or ';'
 105   while (*cmd == ' ' || *cmd == ';') cmd++;
 106 
 107   if (*cmd == '\0') return NULL;
 108 
 109   const char * cmdend = cmd;
 110   while (*cmdend != '\0' && *cmdend != ';') cmdend++;
 111 
 112   Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
 113 
 114   *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
 115   return buf;
 116 }
 117 
 118 static void print_bug_submit_message(outputStream *out, Thread *thread) {
 119   if (out == NULL) return;
 120   out->print_raw_cr("# If you would like to submit a bug report, please visit:");
 121   out->print_raw   ("#   ");
 122   out->print_raw_cr(Arguments::java_vendor_url_bug());
 123   // If the crash is in native code, encourage user to submit a bug to the
 124   // provider of that code.
 125   if (thread && thread->is_Java_thread() &&
 126       !thread->is_hidden_from_external_view()) {
 127     JavaThread* jt = (JavaThread*)thread;
 128     if (jt->thread_state() == _thread_in_native) {
 129       out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
 130     }
 131   }
 132   out->print_raw_cr("#");
 133 }
 134 
 135 bool VMError::coredump_status;
 136 char VMError::coredump_message[O_BUFLEN];
 137 
 138 void VMError::record_coredump_status(const char* message, bool status) {
 139   coredump_status = status;
 140   strncpy(coredump_message, message, sizeof(coredump_message));
 141   coredump_message[sizeof(coredump_message)-1] = 0;
 142 }
 143 
 144 // Return a string to describe the error
 145 char* VMError::error_string(char* buf, int buflen) {
 146   char signame_buf[64];
 147   const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
 148 
 149   if (signame) {
 150     jio_snprintf(buf, buflen,
 151                  "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
 152                  signame, _id, _pc,
 153                  os::current_process_id(), os::current_thread_id());
 154   } else if (_filename != NULL && _lineno > 0) {
 155     // skip directory names
 156     char separator = os::file_separator()[0];
 157     const char *p = strrchr(_filename, separator);
 158     int n = jio_snprintf(buf, buflen,
 159                          "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
 160                          p ? p + 1 : _filename, _lineno,
 161                          os::current_process_id(), os::current_thread_id());
 162     if (n >= 0 && n < buflen && _message) {
 163       if (strlen(_detail_msg) > 0) {
 164         jio_snprintf(buf + n, buflen - n, "%s%s: %s",
 165         os::line_separator(), _message, _detail_msg);
 166       } else {
 167         jio_snprintf(buf + n, buflen - n, "%sError: %s",
 168                      os::line_separator(), _message);
 169       }
 170     }
 171   } else {
 172     jio_snprintf(buf, buflen,
 173                  "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
 174                  _id, os::current_process_id(), os::current_thread_id());
 175   }
 176 
 177   return buf;
 178 }
 179 
 180 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
 181                                 char* buf, int buflen, bool verbose) {
 182 #ifdef ZERO
 183   if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
 184     // StackFrameStream uses the frame anchor, which may not have
 185     // been set up.  This can be done at any time in Zero, however,
 186     // so if it hasn't been set up then we just set it up now and
 187     // clear it again when we're done.
 188     bool has_last_Java_frame = jt->has_last_Java_frame();
 189     if (!has_last_Java_frame)
 190       jt->set_last_Java_frame();
 191     st->print("Java frames:");
 192     st->cr();
 193 
 194     // Print the frames
 195     StackFrameStream sfs(jt);
 196     for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
 197       sfs.current()->zero_print_on_error(i, st, buf, buflen);
 198       st->cr();
 199     }
 200 
 201     // Reset the frame anchor if necessary
 202     if (!has_last_Java_frame)
 203       jt->reset_last_Java_frame();
 204   }
 205 #else
 206   if (jt->has_last_Java_frame()) {
 207     st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
 208     for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
 209       sfs.current()->print_on_error(st, buf, buflen, verbose);
 210       st->cr();
 211     }
 212   }
 213 #endif // ZERO
 214 }
 215 
 216 void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) {
 217 
 218   // see if it's a valid frame
 219   if (fr.pc()) {
 220     st->print_cr("Native frames: (J=compiled Java code, A=aot compiled Java code, j=interpreted, Vv=VM code, C=native code)");
 221 
 222     int count = 0;
 223     while (count++ < StackPrintLimit) {
 224       fr.print_on_error(st, buf, buf_size);
 225       if (fr.pc()) { // print source file and line, if available
 226         char buf[128];
 227         int line_no;
 228         if (Decoder::get_source_info(fr.pc(), buf, sizeof(buf), &line_no)) {
 229           st->print("  (%s:%d)", buf, line_no);
 230         }
 231       }
 232       st->cr();
 233       // Compiled code may use EBP register on x86 so it looks like
 234       // non-walkable C frame. Use frame.sender() for java frames.
 235       if (t && t->is_Java_thread()) {
 236         // Catch very first native frame by using stack address.
 237         // For JavaThread stack_base and stack_size should be set.
 238         if (!t->on_local_stack((address)(fr.real_fp() + 1))) {
 239           break;
 240         }
 241         if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
 242           RegisterMap map((JavaThread*)t, false); // No update
 243           fr = fr.sender(&map);
 244         } else {
 245           fr = os::get_sender_for_C_frame(&fr);
 246         }
 247       } else {
 248         // is_first_C_frame() does only simple checks for frame pointer,
 249         // it will pass if java compiled code has a pointer in EBP.
 250         if (os::is_first_C_frame(&fr)) break;
 251         fr = os::get_sender_for_C_frame(&fr);
 252       }
 253     }
 254 
 255     if (count > StackPrintLimit) {
 256       st->print_cr("...<more frames>...");
 257     }
 258 
 259     st->cr();
 260   }
 261 }
 262 
 263 static void print_oom_reasons(outputStream* st) {
 264   st->print_cr("# Possible reasons:");
 265   st->print_cr("#   The system is out of physical RAM or swap space");
 266   if (UseCompressedOops) {
 267     st->print_cr("#   The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap");
 268   }
 269   if (LogBytesPerWord == 2) {
 270     st->print_cr("#   In 32 bit mode, the process size limit was hit");
 271   }
 272   st->print_cr("# Possible solutions:");
 273   st->print_cr("#   Reduce memory load on the system");
 274   st->print_cr("#   Increase physical memory or swap space");
 275   st->print_cr("#   Check if swap backing store is full");
 276   if (LogBytesPerWord == 2) {
 277     st->print_cr("#   Use 64 bit Java on a 64 bit OS");
 278   }
 279   st->print_cr("#   Decrease Java heap size (-Xmx/-Xms)");
 280   st->print_cr("#   Decrease number of Java threads");
 281   st->print_cr("#   Decrease Java thread stack sizes (-Xss)");
 282   st->print_cr("#   Set larger code cache with -XX:ReservedCodeCacheSize=");
 283   if (UseCompressedOops) {
 284     switch (Universe::narrow_oop_mode()) {
 285       case Universe::UnscaledNarrowOop:
 286         st->print_cr("#   JVM is running with Unscaled Compressed Oops mode in which the Java heap is");
 287         st->print_cr("#     placed in the first 4GB address space. The Java Heap base address is the");
 288         st->print_cr("#     maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
 289         st->print_cr("#     to set the Java Heap base and to place the Java Heap above 4GB virtual address.");
 290         break;
 291       case Universe::ZeroBasedNarrowOop:
 292         st->print_cr("#   JVM is running with Zero Based Compressed Oops mode in which the Java heap is");
 293         st->print_cr("#     placed in the first 32GB address space. The Java Heap base address is the");
 294         st->print_cr("#     maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress");
 295         st->print_cr("#     to set the Java Heap base and to place the Java Heap above 32GB virtual address.");
 296         break;
 297       default:
 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 native decoder state")
 891 
 892      if (_verbose) {
 893        Decoder::print_state_on(st);
 894        st->cr();
 895      }
 896 
 897   STEP("printing VM options")
 898 
 899      if (_verbose) {
 900        // VM options
 901        Arguments::print_on(st);
 902        st->cr();
 903      }
 904 
 905   STEP("printing warning if internal testing API used")
 906 
 907      if (WhiteBox::used()) {
 908        st->print_cr("Unsupported internal testing APIs have been used.");
 909        st->cr();
 910      }
 911 
 912   STEP("printing log configuration")
 913     if (_verbose){
 914       st->print_cr("Logging:");
 915       LogConfiguration::describe_current_configuration(st);
 916       st->cr();
 917     }
 918 
 919   STEP("printing all environment variables")
 920 
 921      if (_verbose) {
 922        os::print_environment_variables(st, env_list);
 923        st->cr();
 924      }
 925 
 926   STEP("printing signal handlers")
 927 
 928      if (_verbose) {
 929        os::print_signal_handlers(st, buf, sizeof(buf));
 930        st->cr();
 931      }
 932 
 933   STEP("Native Memory Tracking")
 934      if (_verbose) {
 935        MemTracker::error_report(st);
 936      }
 937 
 938   STEP("printing system")
 939 
 940      if (_verbose) {
 941        st->cr();
 942        st->print_cr("---------------  S Y S T E M  ---------------");
 943        st->cr();
 944      }
 945 
 946   STEP("printing OS information")
 947 
 948      if (_verbose) {
 949        os::print_os_info(st);
 950        st->cr();
 951      }
 952 
 953   STEP("printing CPU info")
 954      if (_verbose) {
 955        os::print_cpu_info(st, buf, sizeof(buf));
 956        st->cr();
 957      }
 958 
 959   STEP("printing memory info")
 960 
 961      if (_verbose) {
 962        os::print_memory_info(st);
 963        st->cr();
 964      }
 965 
 966   STEP("printing internal vm info")
 967 
 968      if (_verbose) {
 969        st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
 970        st->cr();
 971      }
 972 
 973   // print a defined marker to show that error handling finished correctly.
 974   STEP("printing end marker")
 975 
 976      if (_verbose) {
 977        st->print_cr("END.");
 978      }
 979 
 980   END
 981 
 982 # undef BEGIN
 983 # undef STEP
 984 # undef END
 985 }
 986 
 987 // Report for the vm_info_cmd. This prints out the information above omitting
 988 // crash and thread specific information.  If output is added above, it should be added
 989 // here also, if it is safe to call during a running process.
 990 void VMError::print_vm_info(outputStream* st) {
 991 
 992   char buf[O_BUFLEN];
 993   report_vm_version(st, buf, sizeof(buf));
 994 
 995   // STEP("printing summary")
 996 
 997   st->cr();
 998   st->print_cr("---------------  S U M M A R Y ------------");
 999   st->cr();
1000 
1001   // STEP("printing VM option summary")
1002 
1003   // VM options
1004   Arguments::print_summary_on(st);
1005   st->cr();
1006 
1007   // STEP("printing summary machine and OS info")
1008 
1009   os::print_summary_info(st, buf, sizeof(buf));
1010 
1011   // STEP("printing date and time")
1012 
1013   os::print_date_and_time(st, buf, sizeof(buf));
1014 
1015   // Skip: STEP("printing thread")
1016 
1017   // STEP("printing process")
1018 
1019   st->cr();
1020   st->print_cr("---------------  P R O C E S S  ---------------");
1021   st->cr();
1022 
1023   // STEP("printing number of OutOfMemoryError and StackOverflow exceptions")
1024 
1025   if (Exceptions::has_exception_counts()) {
1026     st->print_cr("OutOfMemory and StackOverflow Exception counts:");
1027     Exceptions::print_exception_counts_on_error(st);
1028     st->cr();
1029   }
1030 
1031   // STEP("printing compressed oops mode")
1032 
1033   if (UseCompressedOops) {
1034     Universe::print_compressed_oops_mode(st);
1035     if (UseCompressedClassPointers) {
1036       Metaspace::print_compressed_class_space(st);
1037     }
1038     st->cr();
1039   }
1040 
1041   // STEP("printing heap information")
1042 
1043   if (Universe::is_fully_initialized()) {
1044     MutexLocker hl(Heap_lock);
1045     Universe::heap()->print_on_error(st);
1046     st->cr();
1047     st->print_cr("Polling page: " INTPTR_FORMAT, p2i(os::get_polling_page()));
1048     st->cr();
1049   }
1050 
1051   // STEP("printing code cache information")
1052 
1053   if (Universe::is_fully_initialized()) {
1054     // print code cache information before vm abort
1055     CodeCache::print_summary(st);
1056     st->cr();
1057   }
1058 
1059   // STEP("printing ring buffers")
1060 
1061   Events::print_all(st);
1062   st->cr();
1063 
1064   // STEP("printing dynamic libraries")
1065 
1066   // dynamic libraries, or memory map
1067   os::print_dll_info(st);
1068   st->cr();
1069 
1070   // STEP("printing VM options")
1071 
1072   // VM options
1073   Arguments::print_on(st);
1074   st->cr();
1075 
1076   // STEP("printing warning if internal testing API used")
1077 
1078   if (WhiteBox::used()) {
1079     st->print_cr("Unsupported internal testing APIs have been used.");
1080     st->cr();
1081   }
1082 
1083   // STEP("printing log configuration")
1084   st->print_cr("Logging:");
1085   LogConfiguration::describe(st);
1086   st->cr();
1087 
1088   // STEP("printing all environment variables")
1089 
1090   os::print_environment_variables(st, env_list);
1091   st->cr();
1092 
1093   // STEP("printing signal handlers")
1094 
1095   os::print_signal_handlers(st, buf, sizeof(buf));
1096   st->cr();
1097 
1098   // STEP("Native Memory Tracking")
1099 
1100   MemTracker::error_report(st);
1101 
1102   // STEP("printing system")
1103 
1104   st->cr();
1105   st->print_cr("---------------  S Y S T E M  ---------------");
1106   st->cr();
1107 
1108   // STEP("printing OS information")
1109 
1110   os::print_os_info(st);
1111   st->cr();
1112 
1113   // STEP("printing CPU info")
1114 
1115   os::print_cpu_info(st, buf, sizeof(buf));
1116   st->cr();
1117 
1118   // STEP("printing memory info")
1119 
1120   os::print_memory_info(st);
1121   st->cr();
1122 
1123   // STEP("printing internal vm info")
1124 
1125   st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
1126   st->cr();
1127 
1128   // print a defined marker to show that error handling finished correctly.
1129   // STEP("printing end marker")
1130 
1131   st->print_cr("END.");
1132 }
1133 
1134 volatile intptr_t VMError::first_error_tid = -1;
1135 
1136 // An error could happen before tty is initialized or after it has been
1137 // destroyed.
1138 // Please note: to prevent large stack allocations, the log- and
1139 // output-stream use a global scratch buffer for format printing.
1140 // (see VmError::report_and_die(). Access to those streams is synchronized
1141 // in  VmError::report_and_die() - there is only one reporting thread at
1142 // any given time.
1143 fdStream VMError::out(defaultStream::output_fd());
1144 fdStream VMError::log; // error log used by VMError::report_and_die()
1145 
1146 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
1147 static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) {
1148   int fd = -1;
1149   if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
1150     // the O_EXCL flag will cause the open to fail if the file exists
1151     fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0666);
1152   }
1153   return fd;
1154 }
1155 
1156 /**
1157  * Construct file name for a log file and return it's file descriptor.
1158  * Name and location depends on pattern, default_pattern params and access
1159  * permissions.
1160  */
1161 static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) {
1162   int fd = -1;
1163 
1164   // If possible, use specified pattern to construct log file name
1165   if (pattern != NULL) {
1166     fd = expand_and_open(pattern, buf, buflen, 0);
1167   }
1168 
1169   // Either user didn't specify, or the user's location failed,
1170   // so use the default name in the current directory
1171   if (fd == -1) {
1172     const char* cwd = os::get_current_directory(buf, buflen);
1173     if (cwd != NULL) {
1174       size_t pos = strlen(cwd);
1175       int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
1176       pos += fsep_len;
1177       if (fsep_len > 0) {
1178         fd = expand_and_open(default_pattern, buf, buflen, pos);
1179       }
1180     }
1181   }
1182 
1183    // try temp directory if it exists.
1184    if (fd == -1) {
1185      const char* tmpdir = os::get_temp_directory();
1186      if (tmpdir != NULL && strlen(tmpdir) > 0) {
1187        int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
1188        if (pos > 0) {
1189          fd = expand_and_open(default_pattern, buf, buflen, pos);
1190        }
1191      }
1192    }
1193 
1194   return fd;
1195 }
1196 
1197 int         VMError::_id;
1198 const char* VMError::_message;
1199 char        VMError::_detail_msg[1024];
1200 Thread*     VMError::_thread;
1201 address     VMError::_pc;
1202 void*       VMError::_siginfo;
1203 void*       VMError::_context;
1204 const char* VMError::_filename;
1205 int         VMError::_lineno;
1206 size_t      VMError::_size;
1207 
1208 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo,
1209                              void* context, const char* detail_fmt, ...)
1210 {
1211   va_list detail_args;
1212   va_start(detail_args, detail_fmt);
1213   report_and_die(sig, NULL, detail_fmt, detail_args, thread, pc, siginfo, context, NULL, 0, 0);
1214   va_end(detail_args);
1215 }
1216 
1217 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context)
1218 {
1219   report_and_die(thread, sig, pc, siginfo, context, "%s", "");
1220 }
1221 
1222 void VMError::report_and_die(const char* message, const char* detail_fmt, ...)
1223 {
1224   va_list detail_args;
1225   va_start(detail_args, detail_fmt);
1226   report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, NULL, NULL, NULL, NULL, NULL, 0, 0);
1227   va_end(detail_args);
1228 }
1229 
1230 void VMError::report_and_die(const char* message)
1231 {
1232   report_and_die(message, "%s", "");
1233 }
1234 
1235 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, const char* message,
1236                              const char* detail_fmt, va_list detail_args)
1237 {
1238   report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, 0);
1239 }
1240 
1241 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
1242                              VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) {
1243   report_and_die(vm_err_type, NULL, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, size);
1244 }
1245 
1246 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
1247                              Thread* thread, address pc, void* siginfo, void* context, const char* filename,
1248                              int lineno, size_t size)
1249 {
1250   // Don't allocate large buffer on stack
1251   static char buffer[O_BUFLEN];
1252   out.set_scratch_buffer(buffer, sizeof(buffer));
1253   log.set_scratch_buffer(buffer, sizeof(buffer));
1254 
1255   // How many errors occurred in error handler when reporting first_error.
1256   static int recursive_error_count;
1257 
1258   // We will first print a brief message to standard out (verbose = false),
1259   // then save detailed information in log file (verbose = true).
1260   static bool out_done = false;         // done printing to standard out
1261   static bool log_done = false;         // done saving error log
1262   static bool transmit_report_done = false; // done error reporting
1263 
1264   if (SuppressFatalErrorMessage) {
1265       os::abort(CreateCoredumpOnCrash);
1266   }
1267   intptr_t mytid = os::current_thread_id();
1268   if (first_error_tid == -1 &&
1269       Atomic::cmpxchg(mytid, &first_error_tid, (intptr_t)-1) == -1) {
1270 
1271     // Initialize time stamps to use the same base.
1272     out.time_stamp().update_to(1);
1273     log.time_stamp().update_to(1);
1274 
1275     _id = id;
1276     _message = message;
1277     _thread = thread;
1278     _pc = pc;
1279     _siginfo = siginfo;
1280     _context = context;
1281     _filename = filename;
1282     _lineno = lineno;
1283     _size = size;
1284     jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);
1285 
1286     // first time
1287     _error_reported = true;
1288 
1289     reporting_started();
1290     record_reporting_start_time();
1291 
1292     if (ShowMessageBoxOnError || PauseAtExit) {
1293       show_message_box(buffer, sizeof(buffer));
1294 
1295       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1296       // WatcherThread can kill JVM if the error handler hangs.
1297       ShowMessageBoxOnError = false;
1298     }
1299 
1300     os::check_dump_limit(buffer, sizeof(buffer));
1301 
1302     // reset signal handlers or exception filter; make sure recursive crashes
1303     // are handled properly.
1304     reset_signal_handlers();
1305 
1306     TRACE_VM_ERROR();
1307 
1308   } else {
1309     // If UseOsErrorReporting we call this for each level of the call stack
1310     // while searching for the exception handler.  Only the first level needs
1311     // to be reported.
1312     if (UseOSErrorReporting && log_done) return;
1313 
1314     // This is not the first error, see if it happened in a different thread
1315     // or in the same thread during error reporting.
1316     if (first_error_tid != mytid) {
1317       char msgbuf[64];
1318       jio_snprintf(msgbuf, sizeof(msgbuf),
1319                    "[thread " INTX_FORMAT " also had an error]",
1320                    mytid);
1321       out.print_raw_cr(msgbuf);
1322 
1323       // error reporting is not MT-safe, block current thread
1324       os::infinite_sleep();
1325 
1326     } else {
1327       if (recursive_error_count++ > 30) {
1328         out.print_raw_cr("[Too many errors, abort]");
1329         os::die();
1330       }
1331 
1332       outputStream* const st = log.is_open() ? &log : &out;
1333       st->cr();
1334 
1335       // Timeout handling.
1336       if (_step_did_timeout) {
1337         // The current step had a timeout. Lets continue reporting with the next step.
1338         st->print_raw("[timeout occurred during error reporting in step \"");
1339         st->print_raw(_current_step_info);
1340         st->print_cr("\"] after " INT64_FORMAT " s.",
1341                      (int64_t)
1342                      ((get_current_timestamp() - _step_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1343       } else if (_reporting_did_timeout) {
1344         // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things
1345         // up, the process is about to be stopped by the WatcherThread.
1346         st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------",
1347                      (int64_t)
1348                      ((get_current_timestamp() - _reporting_start_time) / TIMESTAMP_TO_SECONDS_FACTOR));
1349         st->flush();
1350         // Watcherthread is about to call os::die. Lets just wait.
1351         os::infinite_sleep();
1352       } else {
1353         // Crash or assert during error reporting. Lets continue reporting with the next step.
1354         jio_snprintf(buffer, sizeof(buffer),
1355            "[error occurred during error reporting (%s), id 0x%x]",
1356                    _current_step_info, _id);
1357         st->print_raw_cr(buffer);
1358         st->cr();
1359       }
1360     }
1361   }
1362 
1363   // print to screen
1364   if (!out_done) {
1365     report(&out, false);
1366 
1367     out_done = true;
1368 
1369     _current_step = 0;
1370     _current_step_info = "";
1371   }
1372 
1373   // print to error log file
1374   if (!log_done) {
1375     // see if log file is already open
1376     if (!log.is_open()) {
1377       // open log file
1378       int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
1379       if (fd != -1) {
1380         out.print_raw("# An error report file with more information is saved as:\n# ");
1381         out.print_raw_cr(buffer);
1382 
1383         log.set_fd(fd);
1384       } else {
1385         out.print_raw_cr("# Can not save log file, dump to screen..");
1386         log.set_fd(defaultStream::output_fd());
1387         /* Error reporting currently needs dumpfile.
1388          * Maybe implement direct streaming in the future.*/
1389         transmit_report_done = true;
1390       }
1391     }
1392 
1393     report(&log, true);
1394     log_done = true;
1395     _current_step = 0;
1396     _current_step_info = "";
1397 
1398     // Run error reporting to determine whether or not to report the crash.
1399     if (!transmit_report_done && should_report_bug(_id)) {
1400       transmit_report_done = true;
1401       const int fd2 = ::dup(log.fd());
1402       if (fd2 != -1) {
1403         FILE* const hs_err = ::fdopen(fd2, "r");
1404         if (NULL != hs_err) {
1405           ErrorReporter er;
1406           er.call(hs_err, buffer, O_BUFLEN);
1407           ::fclose(hs_err);
1408         }
1409       }
1410     }
1411 
1412     if (log.fd() != defaultStream::output_fd()) {
1413       close(log.fd());
1414     }
1415 
1416     log.set_fd(-1);
1417   }
1418 
1419   static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
1420   if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1421     skip_replay = true;
1422     ciEnv* env = ciEnv::current();
1423     if (env != NULL) {
1424       int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
1425       if (fd != -1) {
1426         FILE* replay_data_file = os::open(fd, "w");
1427         if (replay_data_file != NULL) {
1428           fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1429           env->dump_replay_data_unsafe(&replay_data_stream);
1430           out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1431           out.print_raw_cr(buffer);
1432         } else {
1433           int e = errno;
1434           out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1435           out.print_raw_cr(os::strerror(e));
1436         }
1437       }
1438     }
1439   }
1440 
1441   static bool skip_bug_url = !should_report_bug(_id);
1442   if (!skip_bug_url) {
1443     skip_bug_url = true;
1444 
1445     out.print_raw_cr("#");
1446     print_bug_submit_message(&out, _thread);
1447   }
1448 
1449   static bool skip_OnError = false;
1450   if (!skip_OnError && OnError && OnError[0]) {
1451     skip_OnError = true;
1452 
1453     // Flush output and finish logs before running OnError commands.
1454     ostream_abort();
1455 
1456     out.print_raw_cr("#");
1457     out.print_raw   ("# -XX:OnError=\"");
1458     out.print_raw   (OnError);
1459     out.print_raw_cr("\"");
1460 
1461     char* cmd;
1462     const char* ptr = OnError;
1463     while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1464       out.print_raw   ("#   Executing ");
1465 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1466       out.print_raw   ("/bin/sh -c ");
1467 #elif defined(SOLARIS)
1468       out.print_raw   ("/usr/bin/sh -c ");
1469 #elif defined(WINDOWS)
1470       out.print_raw   ("cmd /C ");
1471 #endif
1472       out.print_raw   ("\"");
1473       out.print_raw   (cmd);
1474       out.print_raw_cr("\" ...");
1475 
1476       if (os::fork_and_exec(cmd) < 0) {
1477         out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
1478                      os::strerror(errno), os::errno_name(errno), errno);
1479       }
1480     }
1481 
1482     // done with OnError
1483     OnError = NULL;
1484   }
1485 
1486   if (!UseOSErrorReporting) {
1487     // os::abort() will call abort hooks, try it first.
1488     static bool skip_os_abort = false;
1489     if (!skip_os_abort) {
1490       skip_os_abort = true;
1491       bool dump_core = should_report_bug(_id);
1492       os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1493     }
1494 
1495     // if os::abort() doesn't abort, try os::die();
1496     os::die();
1497   }
1498 }
1499 
1500 /*
1501  * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
1502  * ensures utilities such as jmap can observe the process is a consistent state.
1503  */
1504 class VM_ReportJavaOutOfMemory : public VM_Operation {
1505  private:
1506   const char* _message;
1507  public:
1508   VM_ReportJavaOutOfMemory(const char* message) { _message = message; }
1509   VMOp_Type type() const                        { return VMOp_ReportJavaOutOfMemory; }
1510   void doit();
1511 };
1512 
1513 void VM_ReportJavaOutOfMemory::doit() {
1514   // Don't allocate large buffer on stack
1515   static char buffer[O_BUFLEN];
1516 
1517   tty->print_cr("#");
1518   tty->print_cr("# java.lang.OutOfMemoryError: %s", _message);
1519   tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
1520 
1521   // make heap parsability
1522   Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
1523 
1524   char* cmd;
1525   const char* ptr = OnOutOfMemoryError;
1526   while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1527     tty->print("#   Executing ");
1528 #if defined(LINUX)
1529     tty->print  ("/bin/sh -c ");
1530 #elif defined(SOLARIS)
1531     tty->print  ("/usr/bin/sh -c ");
1532 #endif
1533     tty->print_cr("\"%s\"...", cmd);
1534 
1535     if (os::fork_and_exec(cmd) < 0) {
1536       tty->print_cr("os::fork_and_exec failed: %s (%s=%d)",
1537                      os::strerror(errno), os::errno_name(errno), errno);
1538     }
1539   }
1540 }
1541 
1542 void VMError::report_java_out_of_memory(const char* message) {
1543   if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
1544     MutexLocker ml(Heap_lock);
1545     VM_ReportJavaOutOfMemory op(message);
1546     VMThread::execute(&op);
1547   }
1548 }
1549 
1550 void VMError::show_message_box(char *buf, int buflen) {
1551   bool yes;
1552   do {
1553     error_string(buf, buflen);
1554     yes = os::start_debugging(buf,buflen);
1555   } while (yes);
1556 }
1557 
1558 // Timeout handling: check if a timeout happened (either a single step did
1559 // timeout or the whole of error reporting hit ErrorLogTimeout). Interrupt
1560 // the reporting thread if that is the case.
1561 bool VMError::check_timeout() {
1562 
1563   if (ErrorLogTimeout == 0) {
1564     return false;
1565   }
1566 
1567   // Do not check for timeouts if we still have a message box to show to the
1568   // user or if there are OnError handlers to be run.
1569   if (ShowMessageBoxOnError
1570       || (OnError != NULL && OnError[0] != '\0')
1571       || Arguments::abort_hook() != NULL) {
1572     return false;
1573   }
1574 
1575   const jlong reporting_start_time_l = get_reporting_start_time();
1576   const jlong now = get_current_timestamp();
1577   // Timestamp is stored in nanos.
1578   if (reporting_start_time_l > 0) {
1579     const jlong end = reporting_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR;
1580     if (end <= now) {
1581       _reporting_did_timeout = true;
1582       interrupt_reporting_thread();
1583       return true; // global timeout
1584     }
1585   }
1586 
1587   const jlong step_start_time_l = get_step_start_time();
1588   if (step_start_time_l > 0) {
1589     // A step times out after a quarter of the total timeout. Steps are mostly fast unless they
1590     // hang for some reason, so this simple rule allows for three hanging step and still
1591     // hopefully leaves time enough for the rest of the steps to finish.
1592     const jlong end = step_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4;
1593     if (end <= now) {
1594       _step_did_timeout = true;
1595       interrupt_reporting_thread();
1596       return false; // (Not a global timeout)
1597     }
1598   }
1599 
1600   return false;
1601 
1602 }
1603 
1604 #ifndef PRODUCT
1605 typedef void (*voidfun_t)();
1606 // Crash with an authentic sigfpe
1607 static void crash_with_sigfpe() {
1608   // generate a native synchronous SIGFPE where possible;
1609   // if that did not cause a signal (e.g. on ppc), just
1610   // raise the signal.
1611   volatile int x = 0;
1612   volatile int y = 1/x;
1613 #ifndef _WIN32
1614   // OSX implements raise(sig) incorrectly so we need to
1615   // explicitly target the current thread
1616   pthread_kill(pthread_self(), SIGFPE);
1617 #endif
1618 } // end: crash_with_sigfpe
1619 
1620 // crash with sigsegv at non-null address.
1621 static void crash_with_segfault() {
1622 
1623   char* const crash_addr = (char*) VMError::get_segfault_address();
1624   *crash_addr = 'X';
1625 
1626 } // end: crash_with_segfault
1627 
1628 void VMError::test_error_handler() {
1629   controlled_crash(ErrorHandlerTest);
1630 }
1631 
1632 // crash in a controlled way:
1633 // how can be one of:
1634 // 1,2 - asserts
1635 // 3,4 - guarantee
1636 // 5-7 - fatal
1637 // 8 - vm_exit_out_of_memory
1638 // 9 - ShouldNotCallThis
1639 // 10 - ShouldNotReachHere
1640 // 11 - Unimplemented
1641 // 12,13 - (not guaranteed) crashes
1642 // 14 - SIGSEGV
1643 // 15 - SIGFPE
1644 void VMError::controlled_crash(int how) {
1645   if (how == 0) return;
1646 
1647   // If asserts are disabled, use the corresponding guarantee instead.
1648   NOT_DEBUG(if (how <= 2) how += 2);
1649 
1650   const char* const str = "hello";
1651   const size_t      num = (size_t)os::vm_page_size();
1652 
1653   const char* const eol = os::line_separator();
1654   const char* const msg = "this message should be truncated during formatting";
1655   char * const dataPtr = NULL;  // bad data pointer
1656   const void (*funcPtr)(void) = (const void(*)()) 0xF;  // bad function pointer
1657 
1658   // Keep this in sync with test/runtime/ErrorHandling/ErrorHandler.java





1659   switch (how) {
1660     case  1: vmassert(str == NULL, "expected null");
1661     case  2: vmassert(num == 1023 && *str == 'X',
1662                       "num=" SIZE_FORMAT " str=\"%s\"", num, str);
1663     case  3: guarantee(str == NULL, "expected null");
1664     case  4: guarantee(num == 1023 && *str == 'X',
1665                        "num=" SIZE_FORMAT " str=\"%s\"", num, str);
1666     case  5: fatal("expected null");
1667     case  6: fatal("num=" SIZE_FORMAT " str=\"%s\"", num, str);
1668     case  7: fatal("%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
1669                    "%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
1670                    "%s%s#    %s%s#    %s%s#    %s%s#    %s",
1671                    msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1672                    msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
1673                    msg, eol, msg, eol, msg, eol, msg, eol, msg);
1674     case  8: vm_exit_out_of_memory(num, OOM_MALLOC_ERROR, "ChunkPool::allocate");
1675     case  9: ShouldNotCallThis();
1676     case 10: ShouldNotReachHere();
1677     case 11: Unimplemented();
1678     // There's no guarantee the bad data pointer will crash us
1679     // so "break" out to the ShouldNotReachHere().
1680     case 12: *dataPtr = '\0'; break;
1681     // There's no guarantee the bad function pointer will crash us
1682     // so "break" out to the ShouldNotReachHere().
1683     case 13: (*funcPtr)(); break;
1684     case 14: crash_with_segfault(); break;
1685     case 15: crash_with_sigfpe(); break;











1686 
1687     default: tty->print_cr("ERROR: %d: unexpected test_num value.", how);
1688   }
1689   ShouldNotReachHere();
1690 }
1691 #endif // !PRODUCT
1692 
--- EOF ---