src/share/vm/utilities/vmError.cpp

changeset 5027
e12c9b3740db
parent 4904
7b835924c31c
child 5040
9ce110b1d14a
     1.1 --- a/src/share/vm/utilities/vmError.cpp	Wed Apr 24 18:20:04 2013 -0400
     1.2 +++ b/src/share/vm/utilities/vmError.cpp	Thu Apr 25 11:02:32 2013 -0700
     1.3 @@ -796,6 +796,56 @@
     1.4  VMError* volatile VMError::first_error = NULL;
     1.5  volatile jlong VMError::first_error_tid = -1;
     1.6  
     1.7 +/** Expand a pattern into a buffer starting at pos and open a file using constructed path */
     1.8 +static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) {
     1.9 +  int fd = -1;
    1.10 +  if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) {
    1.11 +    fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666);
    1.12 +  }
    1.13 +  return fd;
    1.14 +}
    1.15 +
    1.16 +/**
    1.17 + * Construct file name for a log file and return it's file descriptor.
    1.18 + * Name and location depends on pattern, default_pattern params and access
    1.19 + * permissions.
    1.20 + */
    1.21 +static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) {
    1.22 +  int fd = -1;
    1.23 +
    1.24 +  // If possible, use specified pattern to construct log file name
    1.25 +  if (pattern != NULL) {
    1.26 +    fd = expand_and_open(pattern, buf, buflen, 0);
    1.27 +  }
    1.28 +
    1.29 +  // Either user didn't specify, or the user's location failed,
    1.30 +  // so use the default name in the current directory
    1.31 +  if (fd == -1) {
    1.32 +    const char* cwd = os::get_current_directory(buf, buflen);
    1.33 +    if (cwd != NULL) {
    1.34 +      size_t pos = strlen(cwd);
    1.35 +      int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator());
    1.36 +      pos += fsep_len;
    1.37 +      if (fsep_len > 0) {
    1.38 +        fd = expand_and_open(default_pattern, buf, buflen, pos);
    1.39 +      }
    1.40 +    }
    1.41 +  }
    1.42 +
    1.43 +   // try temp directory if it exists.
    1.44 +   if (fd == -1) {
    1.45 +     const char* tmpdir = os::get_temp_directory();
    1.46 +     if (tmpdir != NULL && strlen(tmpdir) > 0) {
    1.47 +       int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator());
    1.48 +       if (pos > 0) {
    1.49 +         fd = expand_and_open(default_pattern, buf, buflen, pos);
    1.50 +       }
    1.51 +     }
    1.52 +   }
    1.53 +
    1.54 +  return fd;
    1.55 +}
    1.56 +
    1.57  void VMError::report_and_die() {
    1.58    // Don't allocate large buffer on stack
    1.59    static char buffer[O_BUFLEN];
    1.60 @@ -905,36 +955,7 @@
    1.61      // see if log file is already open
    1.62      if (!log.is_open()) {
    1.63        // open log file
    1.64 -      int fd = -1;
    1.65 -
    1.66 -      if (ErrorFile != NULL) {
    1.67 -        bool copy_ok =
    1.68 -          Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
    1.69 -        if (copy_ok) {
    1.70 -          fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
    1.71 -        }
    1.72 -      }
    1.73 -
    1.74 -      if (fd == -1) {
    1.75 -        const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
    1.76 -        size_t len = strlen(cwd);
    1.77 -        // either user didn't specify, or the user's location failed,
    1.78 -        // so use the default name in the current directory
    1.79 -        jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
    1.80 -                     os::file_separator(), os::current_process_id());
    1.81 -        fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
    1.82 -      }
    1.83 -
    1.84 -      if (fd == -1) {
    1.85 -        const char * tmpdir = os::get_temp_directory();
    1.86 -        // try temp directory if it exists.
    1.87 -        if (tmpdir != NULL && tmpdir[0] != '\0') {
    1.88 -          jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
    1.89 -                       tmpdir, os::file_separator(), os::current_process_id());
    1.90 -          fd = open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
    1.91 -        }
    1.92 -      }
    1.93 -
    1.94 +      int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer));
    1.95        if (fd != -1) {
    1.96          out.print_raw("# An error report file with more information is saved as:\n# ");
    1.97          out.print_raw_cr(buffer);
    1.98 @@ -958,7 +979,7 @@
    1.99      // Run error reporting to determine whether or not to report the crash.
   1.100      if (!transmit_report_done && should_report_bug(first_error->_id)) {
   1.101        transmit_report_done = true;
   1.102 -      FILE* hs_err = ::fdopen(log.fd(), "r");
   1.103 +      FILE* hs_err = os::open(log.fd(), "r");
   1.104        if (NULL != hs_err) {
   1.105          ErrorReporter er;
   1.106          er.call(hs_err, buffer, O_BUFLEN);
   1.107 @@ -1008,7 +1029,19 @@
   1.108      skip_replay = true;
   1.109      ciEnv* env = ciEnv::current();
   1.110      if (env != NULL) {
   1.111 -      env->dump_replay_data();
   1.112 +      int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer));
   1.113 +      if (fd != -1) {
   1.114 +        FILE* replay_data_file = os::open(fd, "w");
   1.115 +        if (replay_data_file != NULL) {
   1.116 +          fileStream replay_data_stream(replay_data_file, /*need_close=*/true);
   1.117 +          env->dump_replay_data(&replay_data_stream);
   1.118 +          out.print_raw("#\n# Compiler replay data is saved as:\n# ");
   1.119 +          out.print_raw_cr(buffer);
   1.120 +        } else {
   1.121 +          out.print_raw("#\n# Can't open file to dump replay data. Error: ");
   1.122 +          out.print_raw_cr(strerror(os::get_last_error()));
   1.123 +        }
   1.124 +      }
   1.125      }
   1.126    }
   1.127  

mercurial