1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <fcntl.h>
  26 #include "precompiled.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "prims/whitebox.hpp"
  32 #include "runtime/arguments.hpp"
  33 #include "runtime/atomic.inline.hpp"
  34 #include "runtime/frame.inline.hpp"
  35 #include "runtime/init.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/thread.inline.hpp"
  38 #include "runtime/vmThread.hpp"
  39 #include "runtime/vm_operations.hpp"
  40 #include "services/memTracker.hpp"
  41 #include "utilities/debug.hpp"
  42 #include "utilities/decoder.hpp"
  43 #include "utilities/defaultStream.hpp"
  44 #include "utilities/errorReporter.hpp"
  45 #include "utilities/events.hpp"
  46 #include "utilities/top.hpp"
  47 #include "utilities/vmError.hpp"
  48 
  49 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  50 
  51 // List of environment variables that should be reported in error log file.
  52 const char *env_list[] = {
  53   // All platforms
  54   "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
  55   "JAVA_COMPILER", "PATH", "USERNAME",
  56 
  57   // Env variables that are defined on Solaris/Linux/BSD
  58   "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
  59   "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
  60 
  61   // defined on Linux
  62   "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
  63 
  64   // defined on Darwin
  65   "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH",
  66   "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH",
  67   "DYLD_INSERT_LIBRARIES",
  68 
  69   // defined on Windows
  70   "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
  71 
  72   (const char *)0
  73 };
  74 
  75 // Fatal error handler for internal errors and crashes.
  76 //
  77 // The default behavior of fatal error handler is to print a brief message
  78 // to standard out (defaultStream::output_fd()), then save detailed information
  79 // into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
  80 // threads are having troubles at the same time, only one error is reported.
  81 // The thread that is reporting error will abort VM when it is done, all other
  82 // threads are blocked forever inside report_and_die().
  83 
  84 // Constructor for crashes
  85 VMError::VMError(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) {
  86     _thread = thread;
  87     _id = sig;
  88     _pc   = pc;
  89     _siginfo = siginfo;
  90     _context = context;
  91 
  92     _verbose = false;
  93     _current_step = 0;
  94     _current_step_info = NULL;
  95 
  96     _message = NULL;
  97     _detail_msg = NULL;
  98     _filename = NULL;
  99     _lineno = 0;
 100 
 101     _size = 0;
 102 }
 103 
 104 // Constructor for internal errors
 105 VMError::VMError(Thread* thread, const char* filename, int lineno,
 106                  const char* message, const char * detail_msg)
 107 {
 108   _thread = thread;
 109   _id = INTERNAL_ERROR;     // Value that's not an OS exception/signal
 110   _filename = filename;
 111   _lineno = lineno;
 112   _message = message;
 113   _detail_msg = detail_msg;
 114 
 115   _verbose = false;
 116   _current_step = 0;
 117   _current_step_info = NULL;
 118 
 119   _pc = NULL;
 120   _siginfo = NULL;
 121   _context = NULL;
 122 
 123   _size = 0;
 124 }
 125 
 126 // Constructor for OOM errors
 127 VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size,
 128                  VMErrorType vm_err_type, const char* message) {
 129     _thread = thread;
 130     _id = vm_err_type; // Value that's not an OS exception/signal
 131     _filename = filename;
 132     _lineno = lineno;
 133     _message = message;
 134     _detail_msg = NULL;
 135 
 136     _verbose = false;
 137     _current_step = 0;
 138     _current_step_info = NULL;
 139 
 140     _pc = NULL;
 141     _siginfo = NULL;
 142     _context = NULL;
 143 
 144     _size = size;
 145 }
 146 
 147 
 148 // Constructor for non-fatal errors
 149 VMError::VMError(const char* message) {
 150     _thread = NULL;
 151     _id = INTERNAL_ERROR;     // Value that's not an OS exception/signal
 152     _filename = NULL;
 153     _lineno = 0;
 154     _message = message;
 155     _detail_msg = NULL;
 156 
 157     _verbose = false;
 158     _current_step = 0;
 159     _current_step_info = NULL;
 160 
 161     _pc = NULL;
 162     _siginfo = NULL;
 163     _context = NULL;
 164 
 165     _size = 0;
 166 }
 167 
 168 // -XX:OnError=<string>, where <string> can be a list of commands, separated
 169 // by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
 170 // a single "%". Some examples:
 171 //
 172 // -XX:OnError="pmap %p"                // show memory map
 173 // -XX:OnError="gcore %p; dbx - %p"     // dump core and launch debugger
 174 // -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
 175 // -XX:OnError="kill -9 %p"             // ?#!@#
 176 
 177 // A simple parser for -XX:OnError, usage:
 178 //  ptr = OnError;
 179 //  while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
 180 //     ... ...
 181 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
 182   if (ptr == NULL || *ptr == NULL) return NULL;
 183 
 184   const char* cmd = *ptr;
 185 
 186   // skip leading blanks or ';'
 187   while (*cmd == ' ' || *cmd == ';') cmd++;
 188 
 189   if (*cmd == '\0') return NULL;
 190 
 191   const char * cmdend = cmd;
 192   while (*cmdend != '\0' && *cmdend != ';') cmdend++;
 193 
 194   Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
 195 
 196   *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
 197   return buf;
 198 }
 199 
 200 
 201 static void print_bug_submit_message(outputStream *out, Thread *thread) {
 202   if (out == NULL) return;
 203   out->print_raw_cr("# If you would like to submit a bug report, please visit:");
 204   out->print_raw   ("#   ");
 205   out->print_raw_cr(Arguments::java_vendor_url_bug());
 206   // If the crash is in native code, encourage user to submit a bug to the
 207   // provider of that code.
 208   if (thread && thread->is_Java_thread() &&
 209       !thread->is_hidden_from_external_view()) {
 210     JavaThread* jt = (JavaThread*)thread;
 211     if (jt->thread_state() == _thread_in_native) {
 212       out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
 213     }
 214   }
 215   out->print_raw_cr("#");
 216 }
 217 
 218 bool VMError::coredump_status;
 219 char VMError::coredump_message[O_BUFLEN];
 220 
 221 void VMError::record_coredump_status(const char* message, bool status) {
 222   coredump_status = status;
 223   strncpy(coredump_message, message, sizeof(coredump_message));
 224   coredump_message[sizeof(coredump_message)-1] = 0;
 225 }
 226 
 227 
 228 // Return a string to describe the error
 229 char* VMError::error_string(char* buf, int buflen) {
 230   char signame_buf[64];
 231   const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
 232 
 233   if (signame) {
 234     jio_snprintf(buf, buflen,
 235                  "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
 236                  signame, _id, _pc,
 237                  os::current_process_id(), os::current_thread_id());
 238   } else if (_filename != NULL && _lineno > 0) {
 239     // skip directory names
 240     char separator = os::file_separator()[0];
 241     const char *p = strrchr(_filename, separator);
 242     int n = jio_snprintf(buf, buflen,
 243                          "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
 244                          p ? p + 1 : _filename, _lineno,
 245                          os::current_process_id(), os::current_thread_id());
 246     if (n >= 0 && n < buflen && _message) {
 247       if (_detail_msg) {
 248         jio_snprintf(buf + n, buflen - n, "%s%s: %s",
 249                      os::line_separator(), _message, _detail_msg);
 250       } else {
 251         jio_snprintf(buf + n, buflen - n, "%sError: %s",
 252                      os::line_separator(), _message);
 253       }
 254     }
 255   } else {
 256     jio_snprintf(buf, buflen,
 257                  "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
 258                  _id, os::current_process_id(), os::current_thread_id());
 259   }
 260 
 261   return buf;
 262 }
 263 
 264 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
 265                                 char* buf, int buflen, bool verbose) {
 266 #ifdef ZERO
 267   if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
 268     // StackFrameStream uses the frame anchor, which may not have
 269     // been set up.  This can be done at any time in Zero, however,
 270     // so if it hasn't been set up then we just set it up now and
 271     // clear it again when we're done.
 272     bool has_last_Java_frame = jt->has_last_Java_frame();
 273     if (!has_last_Java_frame)
 274       jt->set_last_Java_frame();
 275     st->print("Java frames:");
 276 
 277     // If the top frame is a Shark frame and the frame anchor isn't
 278     // set up then it's possible that the information in the frame
 279     // is garbage: it could be from a previous decache, or it could
 280     // simply have never been written.  So we print a warning...
 281     StackFrameStream sfs(jt);
 282     if (!has_last_Java_frame && !sfs.is_done()) {
 283       if (sfs.current()->zeroframe()->is_shark_frame()) {
 284         st->print(" (TOP FRAME MAY BE JUNK)");
 285       }
 286     }
 287     st->cr();
 288 
 289     // Print the frames
 290     for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
 291       sfs.current()->zero_print_on_error(i, st, buf, buflen);
 292       st->cr();
 293     }
 294 
 295     // Reset the frame anchor if necessary
 296     if (!has_last_Java_frame)
 297       jt->reset_last_Java_frame();
 298   }
 299 #else
 300   if (jt->has_last_Java_frame()) {
 301     st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
 302     for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
 303       sfs.current()->print_on_error(st, buf, buflen, verbose);
 304       st->cr();
 305     }
 306   }
 307 #endif // ZERO
 308 }
 309 
 310 void VMError::print_oom_reasons(outputStream* st) {
 311   st->print_cr("# Possible reasons:");
 312   st->print_cr("#   The system is out of physical RAM or swap space");
 313   st->print_cr("#   In 32 bit mode, the process size limit was hit");
 314   st->print_cr("# Possible solutions:");
 315   st->print_cr("#   Reduce memory load on the system");
 316   st->print_cr("#   Increase physical memory or swap space");
 317   st->print_cr("#   Check if swap backing store is full");
 318   st->print_cr("#   Use 64 bit Java on a 64 bit OS");
 319   st->print_cr("#   Decrease Java heap size (-Xmx/-Xms)");
 320   st->print_cr("#   Decrease number of Java threads");
 321   st->print_cr("#   Decrease Java thread stack sizes (-Xss)");
 322   st->print_cr("#   Set larger code cache with -XX:ReservedCodeCacheSize=");
 323   st->print_cr("# This output file may be truncated or incomplete.");
 324 }
 325 
 326 const char* VMError::gc_mode() {
 327   if (UseG1GC)            return "g1 gc";
 328   if (UseParallelGC)      return "parallel gc";
 329   if (UseConcMarkSweepGC) return "concurrent mark sweep gc";
 330   if (UseSerialGC)        return "serial gc";
 331   return "ERROR in GC mode";
 332 }
 333 
 334 // This is the main function to report a fatal error. Only one thread can
 335 // call this function, so we don't need to worry about MT-safety. But it's
 336 // possible that the error handler itself may crash or die on an internal
 337 // error, for example, when the stack/heap is badly damaged. We must be
 338 // able to handle recursive errors that happen inside error handler.
 339 //
 340 // Error reporting is done in several steps. If a crash or internal error
 341 // occurred when reporting an error, the nested signal/exception handler
 342 // can skip steps that are already (or partially) done. Error reporting will
 343 // continue from the next step. This allows us to retrieve and print
 344 // information that may be unsafe to get after a fatal error. If it happens,
 345 // you may find nested report_and_die() frames when you look at the stack
 346 // in a debugger.
 347 //
 348 // In general, a hang in error handler is much worse than a crash or internal
 349 // error, as it's harder to recover from a hang. Deadlock can happen if we
 350 // try to grab a lock that is already owned by current thread, or if the
 351 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
 352 // error handler and all the functions it called should avoid grabbing any
 353 // lock. An important thing to notice is that memory allocation needs a lock.
 354 //
 355 // We should avoid using large stack allocated buffers. Many errors happen
 356 // when stack space is already low. Making things even worse is that there
 357 // could be nested report_and_die() calls on stack (see above). Only one
 358 // thread can report error, so large buffers are statically allocated in data
 359 // segment.
 360 
 361 void VMError::report(outputStream* st) {
 362 # define BEGIN if (_current_step == 0) { _current_step = 1;
 363 # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
 364 # define END }
 365 
 366   // don't allocate large buffer on stack
 367   static char buf[O_BUFLEN];
 368 
 369   BEGIN
 370 
 371   STEP(10, "(printing fatal error message)")
 372 
 373     st->print_cr("#");
 374     if (should_report_bug(_id)) {
 375       st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
 376     } else {
 377       st->print_cr("# There is insufficient memory for the Java "
 378                    "Runtime Environment to continue.");
 379     }
 380 
 381 #ifndef PRODUCT
 382   // Error handler self tests
 383 
 384   // test secondary error handling. Test it twice, to test that resetting
 385   // error handler after a secondary crash works.
 386   STEP(20, "(test secondary crash 1)")
 387     if (_verbose && TestCrashInErrorHandler != 0) {
 388       st->print_cr("Will crash now (TestCrashInErrorHandler=%d)...",
 389         TestCrashInErrorHandler);
 390       controlled_crash(TestCrashInErrorHandler);
 391     }
 392 
 393   STEP(30, "(test secondary crash 2)")
 394     if (_verbose && TestCrashInErrorHandler != 0) {
 395       st->print_cr("Will crash now (TestCrashInErrorHandler=%d)...",
 396         TestCrashInErrorHandler);
 397       controlled_crash(TestCrashInErrorHandler);
 398     }
 399 
 400   STEP(40, "(test safefetch in error handler)")
 401     // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice
 402     // to test that resetting the signal handler works correctly.
 403     if (_verbose && TestSafeFetchInErrorHandler) {
 404       st->print_cr("Will test SafeFetch...");
 405       if (CanUseSafeFetch32()) {
 406         int* const invalid_pointer = (int*) get_segfault_address();
 407         const int x = 0x76543210;
 408         int i1 = SafeFetch32(invalid_pointer, x);
 409         int i2 = SafeFetch32(invalid_pointer, x);
 410         if (i1 == x && i2 == x) {
 411           st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern
 412         } else {
 413           st->print_cr("??");
 414         }
 415       } else {
 416         st->print_cr("not possible; skipped.");
 417       }
 418     }
 419 #endif // PRODUCT
 420 
 421   STEP(50, "(printing type of error)")
 422 
 423      switch(_id) {
 424        case OOM_MALLOC_ERROR:
 425        case OOM_MMAP_ERROR:
 426          if (_size) {
 427            st->print("# Native memory allocation ");
 428            st->print((_id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " :
 429                                                  "(mmap) failed to map ");
 430            jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size);
 431            st->print("%s", buf);
 432            st->print(" bytes");
 433            if (_message != NULL) {
 434              st->print(" for ");
 435              st->print("%s", _message);
 436            }
 437            st->cr();
 438          } else {
 439            if (_message != NULL) {
 440              st->print("# ");
 441              st->print_cr("%s", _message);
 442            }
 443          }
 444          // In error file give some solutions
 445          if (_verbose) {
 446            print_oom_reasons(st);
 447          } else {
 448            return;  // that's enough for the screen
 449          }
 450          break;
 451        case INTERNAL_ERROR:
 452        default:
 453          break;
 454      }
 455 
 456   STEP(60, "(printing exception/signal name)")
 457 
 458      st->print_cr("#");
 459      st->print("#  ");
 460      // Is it an OS exception/signal?
 461      if (os::exception_name(_id, buf, sizeof(buf))) {
 462        st->print("%s", buf);
 463        st->print(" (0x%x)", _id);                // signal number
 464        st->print(" at pc=" PTR_FORMAT, _pc);
 465      } else {
 466        if (should_report_bug(_id)) {
 467          st->print("Internal Error");
 468        } else {
 469          st->print("Out of Memory Error");
 470        }
 471        if (_filename != NULL && _lineno > 0) {
 472 #ifdef PRODUCT
 473          // In product mode chop off pathname?
 474          char separator = os::file_separator()[0];
 475          const char *p = strrchr(_filename, separator);
 476          const char *file = p ? p+1 : _filename;
 477 #else
 478          const char *file = _filename;
 479 #endif
 480          st->print(" (%s:%d)", file, _lineno);
 481        } else {
 482          st->print(" (0x%x)", _id);
 483        }
 484      }
 485 
 486   STEP(70, "(printing current thread and pid)")
 487 
 488      // process id, thread id
 489      st->print(", pid=%d", os::current_process_id());
 490      st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
 491      st->cr();
 492 
 493   STEP(80, "(printing error message)")
 494 
 495      if (should_report_bug(_id)) {  // already printed the message.
 496        // error message
 497        if (_detail_msg) {
 498          st->print_cr("#  %s: %s", _message ? _message : "Error", _detail_msg);
 499        } else if (_message) {
 500          st->print_cr("#  Error: %s", _message);
 501        }
 502     }
 503 
 504   STEP(90, "(printing Java version string)")
 505 
 506      // VM version
 507      st->print_cr("#");
 508      JDK_Version::current().to_string(buf, sizeof(buf));
 509      const char* runtime_name = JDK_Version::runtime_name() != NULL ?
 510                                   JDK_Version::runtime_name() : "";
 511      const char* runtime_version = JDK_Version::runtime_version() != NULL ?
 512                                   JDK_Version::runtime_version() : "";
 513      st->print_cr("# JRE version: %s (%s) (build %s)", runtime_name, buf, runtime_version);
 514      // This is the long version with some default settings added
 515 #if INCLUDE_JVMCI
 516      st->print_cr("# Java VM: %s (%s, %s%s%s%s%s, %s, %s)",
 517 #else
 518      st->print_cr("# Java VM: %s (%s, %s%s%s, %s, %s)",
 519 #endif
 520                    Abstract_VM_Version::vm_name(),
 521                    Abstract_VM_Version::vm_release(),
 522                    Abstract_VM_Version::vm_info_string(),
 523                    TieredCompilation ? ", tiered" : "",
 524 #if INCLUDE_JVMCI
 525                    EnableJVMCI ? ", jvmci" : "",
 526                    UseJVMCICompiler ? ", jvmci compiler" : "",
 527 #endif
 528                    UseCompressedOops ? ", compressed oops" : "",
 529                    gc_mode(),
 530                    Abstract_VM_Version::vm_platform_string()
 531                  );
 532 
 533   STEP(100, "(printing problematic frame)")
 534 
 535      // Print current frame if we have a context (i.e. it's a crash)
 536      if (_context) {
 537        st->print_cr("# Problematic frame:");
 538        st->print("# ");
 539        frame fr = os::fetch_frame_from_context(_context);
 540        fr.print_on_error(st, buf, sizeof(buf));
 541        st->cr();
 542        st->print_cr("#");
 543      }
 544 
 545   STEP(110, "(printing core file information)")
 546     st->print("# ");
 547     if (CreateCoredumpOnCrash) {
 548       if (coredump_status) {
 549         st->print("Core dump will be written. Default location: %s", coredump_message);
 550       } else {
 551         st->print("No core dump will be written. %s", coredump_message);
 552       }
 553     } else {
 554       st->print("CreateCoredumpOnCrash turned off, no core file dumped");
 555     }
 556     st->cr();
 557     st->print_cr("#");
 558 
 559   STEP(120, "(printing bug submit message)")
 560 
 561      if (should_report_bug(_id) && _verbose) {
 562        print_bug_submit_message(st, _thread);
 563      }
 564 
 565   STEP(130, "(printing summary)" )
 566 
 567      if (_verbose) {
 568        st->cr();
 569        st->print_cr("---------------  S U M M A R Y ------------");
 570        st->cr();
 571      }
 572 
 573   STEP(140, "(printing VM option summary)" )
 574 
 575      if (_verbose) {
 576        // VM options
 577        Arguments::print_summary_on(st);
 578        st->cr();
 579      }
 580 
 581   STEP(150, "(printing summary machine and OS info)")
 582 
 583      if (_verbose) {
 584        os::print_summary_info(st, buf, sizeof(buf));
 585      }
 586 
 587 
 588   STEP(160, "(printing date and time)" )
 589 
 590      if (_verbose) {
 591        os::print_date_and_time(st, buf, sizeof(buf));
 592      }
 593 
 594   STEP(170, "(printing thread)" )
 595 
 596      if (_verbose) {
 597        st->cr();
 598        st->print_cr("---------------  T H R E A D  ---------------");
 599        st->cr();
 600      }
 601 
 602   STEP(180, "(printing current thread)" )
 603 
 604      // current thread
 605      if (_verbose) {
 606        if (_thread) {
 607          st->print("Current thread (" PTR_FORMAT "):  ", _thread);
 608          _thread->print_on_error(st, buf, sizeof(buf));
 609          st->cr();
 610        } else {
 611          st->print_cr("Current thread is native thread");
 612        }
 613        st->cr();
 614      }
 615 
 616   STEP(190, "(printing current compile task)" )
 617 
 618      if (_verbose && _thread && _thread->is_Compiler_thread()) {
 619         CompilerThread* t = (CompilerThread*)_thread;
 620         if (t->task()) {
 621            st->cr();
 622            st->print_cr("Current CompileTask:");
 623            t->task()->print_line_on_error(st, buf, sizeof(buf));
 624            st->cr();
 625         }
 626      }
 627 
 628 
 629   STEP(200, "(printing stack bounds)" )
 630 
 631      if (_verbose) {
 632        st->print("Stack: ");
 633 
 634        address stack_top;
 635        size_t stack_size;
 636 
 637        if (_thread) {
 638           stack_top = _thread->stack_base();
 639           stack_size = _thread->stack_size();
 640        } else {
 641           stack_top = os::current_stack_base();
 642           stack_size = os::current_stack_size();
 643        }
 644 
 645        address stack_bottom = stack_top - stack_size;
 646        st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
 647 
 648        frame fr = _context ? os::fetch_frame_from_context(_context)
 649                            : os::current_frame();
 650 
 651        if (fr.sp()) {
 652          st->print(",  sp=" PTR_FORMAT, fr.sp());
 653          size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
 654          st->print(",  free space=" SIZE_FORMAT "k", free_stack_size);
 655        }
 656 
 657        st->cr();
 658      }
 659 
 660   STEP(210, "(printing native stack)" )
 661 
 662    if (_verbose) {
 663      if (os::platform_print_native_stack(st, _context, buf, sizeof(buf))) {
 664        // We have printed the native stack in platform-specific code
 665        // Windows/x64 needs special handling.
 666      } else {
 667        frame fr = _context ? os::fetch_frame_from_context(_context)
 668                            : os::current_frame();
 669 
 670        print_native_stack(st, fr, _thread, buf, sizeof(buf));
 671      }
 672    }
 673 
 674   STEP(220, "(printing Java stack)" )
 675 
 676      if (_verbose && _thread && _thread->is_Java_thread()) {
 677        print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
 678      }
 679 
 680   STEP(230, "(printing target Java thread stack)" )
 681 
 682      // printing Java thread stack trace if it is involved in GC crash
 683      if (_verbose && _thread && (_thread->is_Named_thread())) {
 684        JavaThread*  jt = ((NamedThread *)_thread)->processed_thread();
 685        if (jt != NULL) {
 686          st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
 687          print_stack_trace(st, jt, buf, sizeof(buf), true);
 688        }
 689      }
 690 
 691   STEP(240, "(printing siginfo)" )
 692 
 693      // signal no, signal code, address that caused the fault
 694      if (_verbose && _siginfo) {
 695        st->cr();
 696        os::print_siginfo(st, _siginfo);
 697        st->cr();
 698      }
 699 
 700   STEP(250, "(printing register info)")
 701 
 702      // decode register contents if possible
 703      if (_verbose && _context && Universe::is_fully_initialized()) {
 704        os::print_register_info(st, _context);
 705        st->cr();
 706      }
 707 
 708   STEP(260, "(printing registers, top of stack, instructions near pc)")
 709 
 710      // registers, top of stack, instructions near pc
 711      if (_verbose && _context) {
 712        os::print_context(st, _context);
 713        st->cr();
 714      }
 715 
 716   STEP(265, "(printing code blob if possible)")
 717 
 718      if (_verbose && _context) {
 719        CodeBlob* cb = CodeCache::find_blob(_pc);
 720        if (cb != NULL) {
 721          if (Interpreter::contains(_pc)) {
 722            // The interpreter CodeBlob is very large so try to print the codelet instead.
 723            InterpreterCodelet* codelet = Interpreter::codelet_containing(_pc);
 724            if (codelet != NULL) {
 725              codelet->print_on(st);
 726              Disassembler::decode(codelet->code_begin(), codelet->code_end(), st);
 727            }
 728          } else {
 729            StubCodeDesc* desc = StubCodeDesc::desc_for(_pc);
 730            if (desc != NULL) {
 731              desc->print_on(st);
 732              Disassembler::decode(desc->begin(), desc->end(), st);
 733            } else {
 734              Disassembler::decode(cb, st);
 735              st->cr();
 736            }
 737          }
 738        }
 739      }
 740 
 741   STEP(270, "(printing VM operation)" )
 742 
 743      if (_verbose && _thread && _thread->is_VM_thread()) {
 744         VMThread* t = (VMThread*)_thread;
 745         VM_Operation* op = t->vm_operation();
 746         if (op) {
 747           op->print_on_error(st);
 748           st->cr();
 749           st->cr();
 750         }
 751      }
 752 
 753   STEP(280, "(printing process)" )
 754 
 755      if (_verbose) {
 756        st->cr();
 757        st->print_cr("---------------  P R O C E S S  ---------------");
 758        st->cr();
 759      }
 760 
 761   STEP(290, "(printing all threads)" )
 762 
 763      // all threads
 764      if (_verbose && _thread) {
 765        Threads::print_on_error(st, _thread, buf, sizeof(buf));
 766        st->cr();
 767      }
 768 
 769   STEP(300, "(printing VM state)" )
 770 
 771      if (_verbose) {
 772        // Safepoint state
 773        st->print("VM state:");
 774 
 775        if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
 776        else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
 777        else st->print("not at safepoint");
 778 
 779        // Also see if error occurred during initialization or shutdown
 780        if (!Universe::is_fully_initialized()) {
 781          st->print(" (not fully initialized)");
 782        } else if (VM_Exit::vm_exited()) {
 783          st->print(" (shutting down)");
 784        } else {
 785          st->print(" (normal execution)");
 786        }
 787        st->cr();
 788        st->cr();
 789      }
 790 
 791   STEP(310, "(printing owned locks on error)" )
 792 
 793      // mutexes/monitors that currently have an owner
 794      if (_verbose) {
 795        print_owned_locks_on_error(st);
 796        st->cr();
 797      }
 798 
 799   STEP(320, "(printing number of OutOfMemoryError and StackOverflow exceptions)")
 800 
 801      if (_verbose && Exceptions::has_exception_counts()) {
 802        st->print_cr("OutOfMemory and StackOverflow Exception counts:");
 803        Exceptions::print_exception_counts_on_error(st);
 804        st->cr();
 805      }
 806 
 807   STEP(330, "(printing compressed oops mode")
 808 
 809      if (_verbose && UseCompressedOops) {
 810        Universe::print_compressed_oops_mode(st);
 811        if (UseCompressedClassPointers) {
 812          Metaspace::print_compressed_class_space(st);
 813        }
 814        st->cr();
 815      }
 816 
 817   STEP(340, "(printing heap information)" )
 818 
 819      if (_verbose && Universe::is_fully_initialized()) {
 820        Universe::heap()->print_on_error(st);
 821        st->cr();
 822 
 823        st->print_cr("Polling page: " INTPTR_FORMAT, os::get_polling_page());
 824        st->cr();
 825      }
 826 
 827   STEP(350, "(printing code cache information)" )
 828 
 829      if (_verbose && Universe::is_fully_initialized()) {
 830        // print code cache information before vm abort
 831        CodeCache::print_summary(st);
 832        st->cr();
 833      }
 834 
 835   STEP(360, "(printing ring buffers)" )
 836 
 837      if (_verbose) {
 838        Events::print_all(st);
 839        st->cr();
 840      }
 841 
 842   STEP(370, "(printing dynamic libraries)" )
 843 
 844      if (_verbose) {
 845        // dynamic libraries, or memory map
 846        os::print_dll_info(st);
 847        st->cr();
 848      }
 849 
 850   STEP(380, "(printing VM options)" )
 851 
 852      if (_verbose) {
 853        // VM options
 854        Arguments::print_on(st);
 855        st->cr();
 856      }
 857 
 858   STEP(390, "(printing warning if internal testing API used)" )
 859 
 860      if (WhiteBox::used()) {
 861        st->print_cr("Unsupported internal testing APIs have been used.");
 862        st->cr();
 863      }
 864 
 865   STEP(400, "(printing all environment variables)" )
 866 
 867      if (_verbose) {
 868        os::print_environment_variables(st, env_list);
 869        st->cr();
 870      }
 871 
 872   STEP(410, "(printing signal handlers)" )
 873 
 874      if (_verbose) {
 875        os::print_signal_handlers(st, buf, sizeof(buf));
 876        st->cr();
 877      }
 878 
 879   STEP(420, "(Native Memory Tracking)" )
 880      if (_verbose) {
 881        MemTracker::error_report(st);
 882      }
 883 
 884   STEP(430, "(printing system)" )
 885 
 886      if (_verbose) {
 887        st->cr();
 888        st->print_cr("---------------  S Y S T E M  ---------------");
 889        st->cr();
 890      }
 891 
 892   STEP(440, "(printing OS information)" )
 893 
 894      if (_verbose) {
 895        os::print_os_info(st);
 896        st->cr();
 897      }
 898 
 899   STEP(450, "(printing CPU info)" )
 900      if (_verbose) {
 901        os::print_cpu_info(st, buf, sizeof(buf));
 902        st->cr();
 903      }
 904 
 905   STEP(460, "(printing memory info)" )
 906 
 907      if (_verbose) {
 908        os::print_memory_info(st);
 909        st->cr();
 910      }
 911 
 912   STEP(470, "(printing internal vm info)" )
 913 
 914      if (_verbose) {
 915        st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
 916        st->cr();
 917      }
 918 
 919   // print a defined marker to show that error handling finished correctly.
 920   STEP(480, "(printing end marker)" )
 921 
 922      if (_verbose) {
 923        st->print_cr("END.");
 924      }
 925 
 926   END
 927 
 928 # undef BEGIN
 929 # undef STEP
 930 # undef END
 931 }
 932 
 933 VMError* volatile VMError::first_error = NULL;
 934 volatile jlong VMError::first_error_tid = -1;
 935 
 936 // An error could happen before tty is initialized or after it has been
 937 // destroyed. Here we use a very simple unbuffered fdStream for printing.
 938 // Only out.print_raw() and out.print_raw_cr() should be used, as other
 939 // printing methods need to allocate large buffer on stack. To format a
 940 // string, use jio_snprintf() with a static buffer or use staticBufferStream.
 941 fdStream VMError::out(defaultStream::output_fd());
 942 fdStream VMError::log; // error log used by VMError::report_and_die()
 943 
 944 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */
 945 static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) {
 946   int fd = -1;
 947   if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
 948     // the O_EXCL flag will cause the open to fail if the file exists
 949     fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0666);
 950   }
 951   return fd;
 952 }
 953 
 954 /**
 955  * Construct file name for a log file and return it's file descriptor.
 956  * Name and location depends on pattern, default_pattern params and access
 957  * permissions.
 958  */
 959 static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) {
 960   int fd = -1;
 961 
 962   // If possible, use specified pattern to construct log file name
 963   if (pattern != NULL) {
 964     fd = expand_and_open(pattern, buf, buflen, 0);
 965   }
 966 
 967   // Either user didn't specify, or the user's location failed,
 968   // so use the default name in the current directory
 969   if (fd == -1) {
 970     const char* cwd = os::get_current_directory(buf, buflen);
 971     if (cwd != NULL) {
 972       size_t pos = strlen(cwd);
 973       int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
 974       pos += fsep_len;
 975       if (fsep_len > 0) {
 976         fd = expand_and_open(default_pattern, buf, buflen, pos);
 977       }
 978     }
 979   }
 980 
 981    // try temp directory if it exists.
 982    if (fd == -1) {
 983      const char* tmpdir = os::get_temp_directory();
 984      if (tmpdir != NULL && strlen(tmpdir) > 0) {
 985        int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
 986        if (pos > 0) {
 987          fd = expand_and_open(default_pattern, buf, buflen, pos);
 988        }
 989      }
 990    }
 991 
 992   return fd;
 993 }
 994 
 995 void VMError::report_and_die() {
 996   // Don't allocate large buffer on stack
 997   static char buffer[O_BUFLEN];
 998 
 999   // How many errors occurred in error handler when reporting first_error.
1000   static int recursive_error_count;
1001 
1002   // We will first print a brief message to standard out (verbose = false),
1003   // then save detailed information in log file (verbose = true).
1004   static bool out_done = false;         // done printing to standard out
1005   static bool log_done = false;         // done saving error log
1006   static bool transmit_report_done = false; // done error reporting
1007 
1008   if (SuppressFatalErrorMessage) {
1009       os::abort(CreateCoredumpOnCrash);
1010   }
1011   jlong mytid = os::current_thread_id();
1012   if (first_error == NULL &&
1013       Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
1014 
1015     // first time
1016     first_error_tid = mytid;
1017     set_error_reported();
1018 
1019     if (ShowMessageBoxOnError || PauseAtExit) {
1020       show_message_box(buffer, sizeof(buffer));
1021 
1022       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
1023       // WatcherThread can kill JVM if the error handler hangs.
1024       ShowMessageBoxOnError = false;
1025     }
1026 
1027     os::check_dump_limit(buffer, sizeof(buffer));
1028 
1029     // reset signal handlers or exception filter; make sure recursive crashes
1030     // are handled properly.
1031     reset_signal_handlers();
1032 
1033   } else {
1034     // If UseOsErrorReporting we call this for each level of the call stack
1035     // while searching for the exception handler.  Only the first level needs
1036     // to be reported.
1037     if (UseOSErrorReporting && log_done) return;
1038 
1039     // This is not the first error, see if it happened in a different thread
1040     // or in the same thread during error reporting.
1041     if (first_error_tid != mytid) {
1042       char msgbuf[64];
1043       jio_snprintf(msgbuf, sizeof(msgbuf),
1044                    "[thread " INT64_FORMAT " also had an error]",
1045                    mytid);
1046       out.print_raw_cr(msgbuf);
1047 
1048       // error reporting is not MT-safe, block current thread
1049       os::infinite_sleep();
1050 
1051     } else {
1052       if (recursive_error_count++ > 30) {
1053         out.print_raw_cr("[Too many errors, abort]");
1054         os::die();
1055       }
1056 
1057       jio_snprintf(buffer, sizeof(buffer),
1058                    "[error occurred during error reporting %s, id 0x%x]",
1059                    first_error ? first_error->_current_step_info : "",
1060                    _id);
1061       if (log.is_open()) {
1062         log.cr();
1063         log.print_raw_cr(buffer);
1064         log.cr();
1065       } else {
1066         out.cr();
1067         out.print_raw_cr(buffer);
1068         out.cr();
1069       }
1070     }
1071   }
1072 
1073   // print to screen
1074   if (!out_done) {
1075     first_error->_verbose = false;
1076 
1077     staticBufferStream sbs(buffer, sizeof(buffer), &out);
1078     first_error->report(&sbs);
1079 
1080     out_done = true;
1081 
1082     first_error->_current_step = 0;         // reset current_step
1083     first_error->_current_step_info = "";   // reset current_step string
1084   }
1085 
1086   // print to error log file
1087   if (!log_done) {
1088     first_error->_verbose = true;
1089 
1090     // see if log file is already open
1091     if (!log.is_open()) {
1092       // open log file
1093       int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
1094       if (fd != -1) {
1095         out.print_raw("# An error report file with more information is saved as:\n# ");
1096         out.print_raw_cr(buffer);
1097 
1098         log.set_fd(fd);
1099       } else {
1100         out.print_raw_cr("# Can not save log file, dump to screen..");
1101         log.set_fd(defaultStream::output_fd());
1102         /* Error reporting currently needs dumpfile.
1103          * Maybe implement direct streaming in the future.*/
1104         transmit_report_done = true;
1105       }
1106     }
1107 
1108     staticBufferStream sbs(buffer, O_BUFLEN, &log);
1109     first_error->report(&sbs);
1110     first_error->_current_step = 0;         // reset current_step
1111     first_error->_current_step_info = "";   // reset current_step string
1112 
1113     // Run error reporting to determine whether or not to report the crash.
1114     if (!transmit_report_done && should_report_bug(first_error->_id)) {
1115       transmit_report_done = true;
1116       const int fd2 = ::dup(log.fd());
1117       FILE* const hs_err = ::fdopen(fd2, "r");
1118       if (NULL != hs_err) {
1119         ErrorReporter er;
1120         er.call(hs_err, buffer, O_BUFLEN);
1121       }
1122       ::fclose(hs_err);
1123     }
1124 
1125     if (log.fd() != defaultStream::output_fd()) {
1126       close(log.fd());
1127     }
1128 
1129     log.set_fd(-1);
1130     log_done = true;
1131   }
1132 
1133 
1134   static bool skip_OnError = false;
1135   if (!skip_OnError && OnError && OnError[0]) {
1136     skip_OnError = true;
1137 
1138     out.print_raw_cr("#");
1139     out.print_raw   ("# -XX:OnError=\"");
1140     out.print_raw   (OnError);
1141     out.print_raw_cr("\"");
1142 
1143     char* cmd;
1144     const char* ptr = OnError;
1145     while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1146       out.print_raw   ("#   Executing ");
1147 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
1148       out.print_raw   ("/bin/sh -c ");
1149 #elif defined(SOLARIS)
1150       out.print_raw   ("/usr/bin/sh -c ");
1151 #endif
1152       out.print_raw   ("\"");
1153       out.print_raw   (cmd);
1154       out.print_raw_cr("\" ...");
1155 
1156       if (os::fork_and_exec(cmd) < 0) {
1157         out.print_cr("os::fork_and_exec failed: %s (%d)", strerror(errno), errno);
1158       }
1159     }
1160 
1161     // done with OnError
1162     OnError = NULL;
1163   }
1164 
1165   static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
1166   if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
1167     skip_replay = true;
1168     ciEnv* env = ciEnv::current();
1169     if (env != NULL) {
1170       int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
1171       if (fd != -1) {
1172         FILE* replay_data_file = os::open(fd, "w");
1173         if (replay_data_file != NULL) {
1174           fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
1175           env->dump_replay_data_unsafe(&replay_data_stream);
1176           out.print_raw("#\n# Compiler replay data is saved as:\n# ");
1177           out.print_raw_cr(buffer);
1178         } else {
1179           out.print_raw("#\n# Can't open file to dump replay data. Error: ");
1180           out.print_raw_cr(strerror(os::get_last_error()));
1181         }
1182       }
1183     }
1184   }
1185 
1186   static bool skip_bug_url = !should_report_bug(first_error->_id);
1187   if (!skip_bug_url) {
1188     skip_bug_url = true;
1189 
1190     out.print_raw_cr("#");
1191     print_bug_submit_message(&out, _thread);
1192   }
1193 
1194   if (!UseOSErrorReporting) {
1195     // os::abort() will call abort hooks, try it first.
1196     static bool skip_os_abort = false;
1197     if (!skip_os_abort) {
1198       skip_os_abort = true;
1199       bool dump_core = should_report_bug(first_error->_id);
1200       os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
1201     }
1202 
1203     // if os::abort() doesn't abort, try os::die();
1204     os::die();
1205   }
1206 }
1207 
1208 /*
1209  * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
1210  * ensures utilities such as jmap can observe the process is a consistent state.
1211  */
1212 class VM_ReportJavaOutOfMemory : public VM_Operation {
1213  private:
1214   VMError *_err;
1215  public:
1216   VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
1217   VMOp_Type type() const                 { return VMOp_ReportJavaOutOfMemory; }
1218   void doit();
1219 };
1220 
1221 void VM_ReportJavaOutOfMemory::doit() {
1222   // Don't allocate large buffer on stack
1223   static char buffer[O_BUFLEN];
1224 
1225   tty->print_cr("#");
1226   tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
1227   tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
1228 
1229   // make heap parsability
1230   Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
1231 
1232   char* cmd;
1233   const char* ptr = OnOutOfMemoryError;
1234   while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
1235     tty->print("#   Executing ");
1236 #if defined(LINUX)
1237     tty->print  ("/bin/sh -c ");
1238 #elif defined(SOLARIS)
1239     tty->print  ("/usr/bin/sh -c ");
1240 #endif
1241     tty->print_cr("\"%s\"...", cmd);
1242 
1243     if (os::fork_and_exec(cmd) < 0) {
1244       tty->print_cr("os::fork_and_exec failed: %s (%d)", strerror(errno), errno);
1245     }
1246   }
1247 }
1248 
1249 void VMError::report_java_out_of_memory() {
1250   if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
1251     MutexLocker ml(Heap_lock);
1252     VM_ReportJavaOutOfMemory op(this);
1253     VMThread::execute(&op);
1254   }
1255 }