Skip to content

Commit fec6f8a

Browse files
committed
8234741: enhance os::get_core_path on macOS
Reviewed-by: clanger, gziemski
1 parent 6ba58f7 commit fec6f8a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,11 +3763,30 @@ int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
37633763
}
37643764
}
37653765

3766-
// Get the default path to the core file
3766+
// Get the kern.corefile setting, or otherwise the default path to the core file
37673767
// Returns the length of the string
37683768
int os::get_core_path(char* buffer, size_t bufferSize) {
3769-
int n = jio_snprintf(buffer, bufferSize, "/cores/core.%d", current_process_id());
3770-
3769+
int n = 0;
3770+
#ifdef __APPLE__
3771+
char coreinfo[MAX_PATH];
3772+
size_t sz = sizeof(coreinfo);
3773+
int ret = sysctlbyname("kern.corefile", coreinfo, &sz, NULL, 0);
3774+
if (ret == 0) {
3775+
char *pid_pos = strstr(coreinfo, "%P");
3776+
// skip over the "%P" to preserve any optional custom user pattern
3777+
const char* tail = (pid_pos != NULL) ? (pid_pos + 2) : "";
3778+
3779+
if (pid_pos != NULL) {
3780+
*pid_pos = '\0';
3781+
n = jio_snprintf(buffer, bufferSize, "%s%d%s", coreinfo, os::current_process_id(), tail);
3782+
} else {
3783+
n = jio_snprintf(buffer, bufferSize, "%s", coreinfo);
3784+
}
3785+
} else
3786+
#endif
3787+
{
3788+
n = jio_snprintf(buffer, bufferSize, "/cores/core.%d", os::current_process_id());
3789+
}
37713790
// Truncate if theoretical string was longer than bufferSize
37723791
n = MIN2(n, (int)bufferSize);
37733792

0 commit comments

Comments
 (0)