--- old/src/os/aix/vm/os_aix.cpp 2014-11-30 00:17:07.329831554 +0900 +++ new/src/os/aix/vm/os_aix.cpp 2014-11-30 00:17:07.128832956 +0900 @@ -5072,6 +5072,9 @@ return 0; } + jio_snprintf(buffer, bufferSize, "%s/core or core.%d", + p, current_process_id()); + return strlen(buffer); } --- old/src/os/bsd/vm/os_bsd.cpp 2014-11-30 00:17:08.061826447 +0900 +++ new/src/os/bsd/vm/os_bsd.cpp 2014-11-30 00:17:07.889827647 +0900 @@ -4681,7 +4681,7 @@ // Get the default path to the core file // Returns the length of the string int os::get_core_path(char* buffer, size_t bufferSize) { - int n = jio_snprintf(buffer, bufferSize, "/cores"); + int n = jio_snprintf(buffer, bufferSize, "/cores/core.%d", current_process_id()); // Truncate if theoretical string was longer than bufferSize n = MIN2(n, (int)bufferSize); --- old/src/os/linux/vm/os_linux.cpp 2014-11-30 00:17:08.728821795 +0900 +++ new/src/os/linux/vm/os_linux.cpp 2014-11-30 00:17:08.567822918 +0900 @@ -5991,13 +5991,79 @@ // Get the default path to the core file // Returns the length of the string int os::get_core_path(char* buffer, size_t bufferSize) { - const char* p = get_current_directory(buffer, bufferSize); + /* + * Max length of /proc/sys/kernel/core_pattern is 128 characters. + * See https://www.kernel.org/doc/Documentation/sysctl/kernel.txt + */ + const int core_pattern_len = 129; + char core_pattern[core_pattern_len] = {0}; - if (p == NULL) { - assert(p != NULL, "failed to get current directory"); + FILE *core_pattern_file = fopen("/proc/sys/kernel/core_pattern", "r"); + if (core_pattern_file != NULL) { + char *ret_ptr = fgets(core_pattern, core_pattern_len, core_pattern_file); + fclose(core_pattern_file); + + if (ret_ptr != NULL) { + char *last_char = core_pattern + strlen(core_pattern) - 1; + + if (*last_char == '\n') { + *last_char = '\0'; + } + + } + + } + + if (strlen(core_pattern) == 0) { return 0; } + char *pid_pos = strstr(core_pattern, "%p"); + size_t written; + + if (core_pattern[0] == '/') { + written = jio_snprintf(buffer, bufferSize, core_pattern); + } else { + char *cwd = (char *)os::malloc(PATH_MAX, mtInternal); + if (cwd == NULL) { + return 0; + } + + const char* p = get_current_directory(cwd, PATH_MAX); + if (p == NULL) { + os::free(cwd); + assert(p != NULL, "failed to get current directory"); + return 0; + } + + if (core_pattern[0] == '|') { + written = jio_snprintf(buffer, bufferSize, + "\"%s\" (or dumping to %s/core.%d)", + &core_pattern[1], p, current_process_id()); + } else { + written = jio_snprintf(buffer, bufferSize, "%s/%s", p, core_pattern); + } + + os::free(cwd); + } + + if ((written >= 0) && (written < bufferSize) + && (pid_pos == NULL) && (core_pattern[0] != '|')) { + FILE *core_uses_pid_file = fopen("/proc/sys/kernel/core_uses_pid", "r"); + + if (core_uses_pid_file != NULL) { + int core_uses_pid = fgetc(core_uses_pid_file); + fclose(core_uses_pid_file); + + if (core_uses_pid == '1'){ + jio_snprintf(buffer + written, bufferSize - written, + ".%d", current_process_id()); + } + + } + + } + return strlen(buffer); } --- old/src/os/posix/vm/os_posix.cpp 2014-11-30 00:17:09.454816730 +0900 +++ new/src/os/posix/vm/os_posix.cpp 2014-11-30 00:17:09.301817797 +0900 @@ -51,15 +51,29 @@ struct rlimit rlim; bool success; - n = get_core_path(buffer, bufferSize); + char *core_path = (char *)os::malloc(PATH_MAX, mtInternal); + if (core_path == NULL) { + VMError::report_coredump_status("", false); + return; + } + + n = get_core_path(core_path, PATH_MAX); - if (getrlimit(RLIMIT_CORE, &rlim) != 0) { - jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (may not exist)", current_process_id()); + if (n <= 0) { + jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id()); + success = true; +#ifdef LINUX + } else if (core_path[0] == '"') { // redirect to user process + jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path); + success = true; +#endif + } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) { + jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path); success = true; } else { switch(rlim.rlim_cur) { case RLIM_INFINITY: - jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d", current_process_id()); + jio_snprintf(buffer, bufferSize, "%s", core_path); success = true; break; case 0: @@ -67,11 +81,13 @@ success = false; break; default: - jio_snprintf(buffer + n, bufferSize - n, "/core or core.%d (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", current_process_id(), (unsigned long)(rlim.rlim_cur >> 10)); + jio_snprintf(buffer, bufferSize, "%s (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, (unsigned long)(rlim.rlim_cur >> 10)); success = true; break; } } + + os::free(core_path); VMError::report_coredump_status(buffer, success); } --- old/src/os/solaris/vm/os_solaris.cpp 2014-11-30 00:17:10.046812600 +0900 +++ new/src/os/solaris/vm/os_solaris.cpp 2014-11-30 00:17:09.892813675 +0900 @@ -5977,6 +5977,9 @@ return 0; } + jio_snprintf(buffer, bufferSize, "%s/core or core.%d", + p, current_process_id()); + return strlen(buffer); }