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