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