示例#1
0
void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
                             Thread* thread, address pc, void* siginfo, void* context, const char* filename,
                             int lineno, size_t size)
{
  // Don't allocate large buffer on stack
  static char buffer[O_BUFLEN];
  out.set_scratch_buffer(buffer, sizeof(buffer));
  log.set_scratch_buffer(buffer, sizeof(buffer));

  // How many errors occurred in error handler when reporting first_error.
  static int recursive_error_count;

  // We will first print a brief message to standard out (verbose = false),
  // then save detailed information in log file (verbose = true).
  static bool out_done = false;         // done printing to standard out
  static bool log_done = false;         // done saving error log
  static bool transmit_report_done = false; // done error reporting

  if (SuppressFatalErrorMessage) {
      os::abort(CreateCoredumpOnCrash);
  }
  intptr_t mytid = os::current_thread_id();
  if (first_error_tid == -1 &&
      Atomic::cmpxchg_ptr(mytid, &first_error_tid, -1) == -1) {

    // Initialize time stamps to use the same base.
    out.time_stamp().update_to(1);
    log.time_stamp().update_to(1);

    _id = id;
    _message = message;
    _thread = thread;
    _pc = pc;
    _siginfo = siginfo;
    _context = context;
    _filename = filename;
    _lineno = lineno;
    _size = size;
    jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args);

    // first time
    set_error_reported();

    if (ShowMessageBoxOnError || PauseAtExit) {
      show_message_box(buffer, sizeof(buffer));

      // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
      // WatcherThread can kill JVM if the error handler hangs.
      ShowMessageBoxOnError = false;
    }

    os::check_dump_limit(buffer, sizeof(buffer));

    // reset signal handlers or exception filter; make sure recursive crashes
    // are handled properly.
    reset_signal_handlers();

    TRACE_VM_ERROR();

  } else {
    // If UseOsErrorReporting we call this for each level of the call stack
    // while searching for the exception handler.  Only the first level needs
    // to be reported.
    if (UseOSErrorReporting && log_done) return;

    // This is not the first error, see if it happened in a different thread
    // or in the same thread during error reporting.
    if (first_error_tid != mytid) {
      char msgbuf[64];
      jio_snprintf(msgbuf, sizeof(msgbuf),
                   "[thread " INTX_FORMAT " also had an error]",
                   mytid);
      out.print_raw_cr(msgbuf);

      // error reporting is not MT-safe, block current thread
      os::infinite_sleep();

    } else {
      if (recursive_error_count++ > 30) {
        out.print_raw_cr("[Too many errors, abort]");
        os::die();
      }

      jio_snprintf(buffer, sizeof(buffer),
                   "[error occurred during error reporting (%s), id 0x%x]",
                   _current_step_info, _id);
      if (log.is_open()) {
        log.cr();
        log.print_raw_cr(buffer);
        log.cr();
      } else {
        out.cr();
        out.print_raw_cr(buffer);
        out.cr();
      }
    }
  }

  // print to screen
  if (!out_done) {
    report(&out, false);

    out_done = true;

    _current_step = 0;
    _current_step_info = "";
  }

  // print to error log file
  if (!log_done) {
    // see if log file is already open
    if (!log.is_open()) {
      // open log file
      int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
      if (fd != -1) {
        out.print_raw("# An error report file with more information is saved as:\n# ");
        out.print_raw_cr(buffer);

        log.set_fd(fd);
      } else {
        out.print_raw_cr("# Can not save log file, dump to screen..");
        log.set_fd(defaultStream::output_fd());
        /* Error reporting currently needs dumpfile.
         * Maybe implement direct streaming in the future.*/
        transmit_report_done = true;
      }
    }

    report(&log, true);
    _current_step = 0;
    _current_step_info = "";

    // Run error reporting to determine whether or not to report the crash.
    if (!transmit_report_done && should_report_bug(_id)) {
      transmit_report_done = true;
      const int fd2 = ::dup(log.fd());
      FILE* const hs_err = ::fdopen(fd2, "r");
      if (NULL != hs_err) {
        ErrorReporter er;
        er.call(hs_err, buffer, O_BUFLEN);
      }
      ::fclose(hs_err);
    }

    if (log.fd() != defaultStream::output_fd()) {
      close(log.fd());
    }

    log.set_fd(-1);
    log_done = true;
  }

  static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
  if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
    skip_replay = true;
    ciEnv* env = ciEnv::current();
    if (env != NULL) {
      int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
      if (fd != -1) {
        FILE* replay_data_file = os::open(fd, "w");
        if (replay_data_file != NULL) {
          fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
          env->dump_replay_data_unsafe(&replay_data_stream);
          out.print_raw("#\n# Compiler replay data is saved as:\n# ");
          out.print_raw_cr(buffer);
        } else {
          int e = errno;
          out.print_raw("#\n# Can't open file to dump replay data. Error: ");
          out.print_raw_cr(os::strerror(e));
        }
      }
    }
  }

  static bool skip_bug_url = !should_report_bug(_id);
  if (!skip_bug_url) {
    skip_bug_url = true;

    out.print_raw_cr("#");
    print_bug_submit_message(&out, _thread);
  }

  static bool skip_OnError = false;
  if (!skip_OnError && OnError && OnError[0]) {
    skip_OnError = true;

    // Flush output and finish logs before running OnError commands.
    ostream_abort();

    out.print_raw_cr("#");
    out.print_raw   ("# -XX:OnError=\"");
    out.print_raw   (OnError);
    out.print_raw_cr("\"");

    char* cmd;
    const char* ptr = OnError;
    while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
      out.print_raw   ("#   Executing ");
#if defined(LINUX) || defined(_ALLBSD_SOURCE)
      out.print_raw   ("/bin/sh -c ");
#elif defined(SOLARIS)
      out.print_raw   ("/usr/bin/sh -c ");
#endif
      out.print_raw   ("\"");
      out.print_raw   (cmd);
      out.print_raw_cr("\" ...");

      if (os::fork_and_exec(cmd) < 0) {
        out.print_cr("os::fork_and_exec failed: %s (%s=%d)",
                     os::strerror(errno), os::errno_name(errno), errno);
      }
    }

    // done with OnError
    OnError = NULL;
  }

  if (!UseOSErrorReporting) {
    // os::abort() will call abort hooks, try it first.
    static bool skip_os_abort = false;
    if (!skip_os_abort) {
      skip_os_abort = true;
      bool dump_core = should_report_bug(_id);
      os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context);
    }

    // if os::abort() doesn't abort, try os::die();
    os::die();
  }
}
示例#2
0
void VMError::report_and_die() {
  // Don't allocate large buffer on stack
  static char buffer[O_BUFLEN];

  // An error could happen before tty is initialized or after it has been
  // destroyed. Here we use a very simple unbuffered fdStream for printing.
  // Only out.print_raw() and out.print_raw_cr() should be used, as other
  // printing methods need to allocate large buffer on stack. To format a
  // string, use jio_snprintf() with a static buffer or use staticBufferStream.
  static fdStream out(defaultStream::output_fd());

  // How many errors occurred in error handler when reporting first_error.
  static int recursive_error_count;

  // We will first print a brief message to standard out (verbose = false),
  // then save detailed information in log file (verbose = true).
  static bool out_done = false;         // done printing to standard out
  static bool log_done = false;         // done saving error log
  static bool transmit_report_done = false; // done error reporting
  static fdStream log;                  // error log

  if (SuppressFatalErrorMessage) {
      os::abort();
  }
  jlong mytid = os::current_thread_id();
  if (first_error == NULL &&
      Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {

    // first time
    first_error_tid = mytid;
    set_error_reported();

    if (ShowMessageBoxOnError || PauseAtExit) {
      show_message_box(buffer, sizeof(buffer));

      // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
      // WatcherThread can kill JVM if the error handler hangs.
      ShowMessageBoxOnError = false;
    }

    // Write a minidump on Windows, check core dump limits on Linux/Solaris
    os::check_or_create_dump(_siginfo, _context, buffer, sizeof(buffer));

    // reset signal handlers or exception filter; make sure recursive crashes
    // are handled properly.
    reset_signal_handlers();

  } else {
    // If UseOsErrorReporting we call this for each level of the call stack
    // while searching for the exception handler.  Only the first level needs
    // to be reported.
    if (UseOSErrorReporting && log_done) return;

    // This is not the first error, see if it happened in a different thread
    // or in the same thread during error reporting.
    if (first_error_tid != mytid) {
      jio_snprintf(buffer, sizeof(buffer),
                   "[thread " INT64_FORMAT " also had an error]",
                   mytid);
      out.print_raw_cr(buffer);

      // error reporting is not MT-safe, block current thread
      os::infinite_sleep();

    } else {
      if (recursive_error_count++ > 30) {
        out.print_raw_cr("[Too many errors, abort]");
        os::die();
      }

      jio_snprintf(buffer, sizeof(buffer),
                   "[error occurred during error reporting %s, id 0x%x]",
                   first_error ? first_error->_current_step_info : "",
                   _id);
      if (log.is_open()) {
        log.cr();
        log.print_raw_cr(buffer);
        log.cr();
      } else {
        out.cr();
        out.print_raw_cr(buffer);
        out.cr();
      }
    }
  }

  // print to screen
  if (!out_done) {
    first_error->_verbose = false;

    staticBufferStream sbs(buffer, sizeof(buffer), &out);
    first_error->report(&sbs);

    out_done = true;

    first_error->_current_step = 0;         // reset current_step
    first_error->_current_step_info = "";   // reset current_step string
  }

  // print to error log file
  if (!log_done) {
    first_error->_verbose = true;

    // see if log file is already open
    if (!log.is_open()) {
      // open log file
      int fd = -1;

      if (ErrorFile != NULL) {
        bool copy_ok =
          Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
        if (copy_ok) {
          fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
        }
      }

      if (fd == -1) {
        const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
        size_t len = strlen(cwd);
        // either user didn't specify, or the user's location failed,
        // so use the default name in the current directory
        jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
                     os::file_separator(), os::current_process_id());
        fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
      }

      if (fd == -1) {
        const char * tmpdir = os::get_temp_directory();
        // try temp directory if it exists.
        if (tmpdir != NULL && tmpdir[0] != '\0') {
          jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
                       tmpdir, os::file_separator(), os::current_process_id());
          fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
        }
      }

      if (fd != -1) {
        out.print_raw("# An error report file with more information is saved as:\n# ");
        out.print_raw_cr(buffer);
        os::set_error_file(buffer);

        log.set_fd(fd);
      } else {
        out.print_raw_cr("# Can not save log file, dump to screen..");
        log.set_fd(defaultStream::output_fd());
        /* Error reporting currently needs dumpfile.
         * Maybe implement direct streaming in the future.*/
        transmit_report_done = true;
      }
    }

    staticBufferStream sbs(buffer, O_BUFLEN, &log);
    first_error->report(&sbs);
    first_error->_current_step = 0;         // reset current_step
    first_error->_current_step_info = "";   // reset current_step string

    // Run error reporting to determine whether or not to report the crash.
    if (!transmit_report_done && should_report_bug(first_error->_id)) {
      transmit_report_done = true;
      FILE* hs_err = ::fdopen(log.fd(), "r");
      if (NULL != hs_err) {
        ErrorReporter er;
        er.call(hs_err, buffer, O_BUFLEN);
      }
    }

    if (log.fd() != defaultStream::output_fd()) {
      close(log.fd());
    }

    log.set_fd(-1);
    log_done = true;
  }


  static bool skip_OnError = false;
  if (!skip_OnError && OnError && OnError[0]) {
    skip_OnError = true;

    out.print_raw_cr("#");
    out.print_raw   ("# -XX:OnError=\"");
    out.print_raw   (OnError);
    out.print_raw_cr("\"");

    char* cmd;
    const char* ptr = OnError;
    while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
      out.print_raw   ("#   Executing ");
#if defined(LINUX) || defined(_ALLBSD_SOURCE)
      out.print_raw   ("/bin/sh -c ");
#elif defined(SOLARIS)
      out.print_raw   ("/usr/bin/sh -c ");
#endif
      out.print_raw   ("\"");
      out.print_raw   (cmd);
      out.print_raw_cr("\" ...");

      os::fork_and_exec(cmd);
    }

    // done with OnError
    OnError = NULL;
  }

  static bool skip_bug_url = !should_report_bug(first_error->_id);
  if (!skip_bug_url) {
    skip_bug_url = true;

    out.print_raw_cr("#");
    print_bug_submit_message(&out, _thread);
  }

  if (!UseOSErrorReporting) {
    // os::abort() will call abort hooks, try it first.
    static bool skip_os_abort = false;
    if (!skip_os_abort) {
      skip_os_abort = true;
      bool dump_core = should_report_bug(first_error->_id);
      os::abort(dump_core);
    }

    // if os::abort() doesn't abort, try os::die();
    os::die();
  }
}
void VMError::report_and_die() {
  // Don't allocate large buffer on stack
  static char buffer[O_BUFLEN];

  // How many errors occurred in error handler when reporting first_error.
  static int recursive_error_count;

  // We will first print a brief message to standard out (verbose = false),
  // then save detailed information in log file (verbose = true).
  static bool out_done = false;         // done printing to standard out
  static bool log_done = false;         // done saving error log
  static bool transmit_report_done = false; // done error reporting

  // disble NMT to avoid further exception
  MemTracker::shutdown(MemTracker::NMT_error_reporting);

  if (SuppressFatalErrorMessage) {
      os::abort();
  }
  jlong mytid = os::current_thread_id();
  if (first_error == NULL &&
      Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {

    // first time
    first_error_tid = mytid;
    set_error_reported();

    if (ShowMessageBoxOnError || PauseAtExit) {
      show_message_box(buffer, sizeof(buffer));

      // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
      // WatcherThread can kill JVM if the error handler hangs.
      ShowMessageBoxOnError = false;
    }

    // Write a minidump on Windows, check core dump limits on Linux/Solaris
    os::check_or_create_dump(_siginfo, _context, buffer, sizeof(buffer));

    // reset signal handlers or exception filter; make sure recursive crashes
    // are handled properly.
    reset_signal_handlers();

  } else {
    // If UseOsErrorReporting we call this for each level of the call stack
    // while searching for the exception handler.  Only the first level needs
    // to be reported.
    if (UseOSErrorReporting && log_done) return;

    // This is not the first error, see if it happened in a different thread
    // or in the same thread during error reporting.
    if (first_error_tid != mytid) {
      char msgbuf[64];
      jio_snprintf(msgbuf, sizeof(msgbuf),
                   "[thread " INT64_FORMAT " also had an error]",
                   mytid);
      out.print_raw_cr(msgbuf);

      // error reporting is not MT-safe, block current thread
      os::infinite_sleep();

    } else {
      if (recursive_error_count++ > 30) {
        out.print_raw_cr("[Too many errors, abort]");
        os::die();
      }

      jio_snprintf(buffer, sizeof(buffer),
                   "[error occurred during error reporting %s, id 0x%x]",
                   first_error ? first_error->_current_step_info : "",
                   _id);
      if (log.is_open()) {
        log.cr();
        log.print_raw_cr(buffer);
        log.cr();
      } else {
        out.cr();
        out.print_raw_cr(buffer);
        out.cr();
      }
    }
  }

  // print to screen
  if (!out_done) {
    first_error->_verbose = false;

    staticBufferStream sbs(buffer, sizeof(buffer), &out);
    first_error->report(&sbs);

    out_done = true;

    first_error->_current_step = 0;         // reset current_step
    first_error->_current_step_info = "";   // reset current_step string
  }

  // print to error log file
  if (!log_done) {
    first_error->_verbose = true;

    // see if log file is already open
    if (!log.is_open()) {
      // open log file
      int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
      if (fd != -1) {
        out.print_raw("# An error report file with more information is saved as:\n# ");
        out.print_raw_cr(buffer);
        os::set_error_file(buffer);

        log.set_fd(fd);
      } else {
        out.print_raw_cr("# Can not save log file, dump to screen..");
        log.set_fd(defaultStream::output_fd());
        /* Error reporting currently needs dumpfile.
         * Maybe implement direct streaming in the future.*/
        transmit_report_done = true;
      }
    }

    staticBufferStream sbs(buffer, O_BUFLEN, &log);
    first_error->report(&sbs);
    first_error->_current_step = 0;         // reset current_step
    first_error->_current_step_info = "";   // reset current_step string

    // Run error reporting to determine whether or not to report the crash.
    if (!transmit_report_done && should_report_bug(first_error->_id)) {
      transmit_report_done = true;
      FILE* hs_err = os::open(log.fd(), "r");
      if (NULL != hs_err) {
        ErrorReporter er;
        er.call(hs_err, buffer, O_BUFLEN);
      }
    }

    if (log.fd() != defaultStream::output_fd()) {
      close(log.fd());
    }

    log.set_fd(-1);
    log_done = true;
  }


  static bool skip_OnError = false;
  if (!skip_OnError && OnError && OnError[0]) {
    skip_OnError = true;

    out.print_raw_cr("#");
    out.print_raw   ("# -XX:OnError=\"");
    out.print_raw   (OnError);
    out.print_raw_cr("\"");

    char* cmd;
    const char* ptr = OnError;
    while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
      out.print_raw   ("#   Executing ");
#if defined(LINUX) || defined(_ALLBSD_SOURCE)
      out.print_raw   ("/bin/sh -c ");
#elif defined(SOLARIS)
      out.print_raw   ("/usr/bin/sh -c ");
#endif
      out.print_raw   ("\"");
      out.print_raw   (cmd);
      out.print_raw_cr("\" ...");

      os::fork_and_exec(cmd);
    }

    // done with OnError
    OnError = NULL;
  }

  static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay
  if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) {
    skip_replay = true;
    ciEnv* env = ciEnv::current();
    if (env != NULL) {
      int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
      if (fd != -1) {
        FILE* replay_data_file = os::open(fd, "w");
        if (replay_data_file != NULL) {
          fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
          env->dump_replay_data_unsafe(&replay_data_stream);
          out.print_raw("#\n# Compiler replay data is saved as:\n# ");
          out.print_raw_cr(buffer);
        } else {
          out.print_raw("#\n# Can't open file to dump replay data. Error: ");
          out.print_raw_cr(strerror(os::get_last_error()));
        }
      }
    }
  }

  static bool skip_bug_url = !should_report_bug(first_error->_id);
  if (!skip_bug_url) {
    skip_bug_url = true;

    out.print_raw_cr("#");
    print_bug_submit_message(&out, _thread);
  }

  if (!UseOSErrorReporting) {
    // os::abort() will call abort hooks, try it first.
    static bool skip_os_abort = false;
    if (!skip_os_abort) {
      skip_os_abort = true;
      bool dump_core = should_report_bug(first_error->_id);
      os::abort(dump_core);
    }

    // if os::abort() doesn't abort, try os::die();
    os::die();
  }
}