--- old/src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c 2017-05-25 14:37:18.472517922 +0530 +++ new/src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c 2017-05-25 14:37:18.224641913 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,8 +22,6 @@ * */ -#ifdef INCLUDE_SA_ATTACH - #include #include #include @@ -34,6 +32,9 @@ #include #include #include +#ifndef INCLUDE_SA_ATTACH +#include +#endif #include "libproc_impl.h" #if defined(x86_64) && !defined(amd64) @@ -220,9 +221,11 @@ static bool ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) { if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) { char buf[200]; - char* msg = strerror_r(errno, buf, sizeof(buf)); - snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg); - print_debug("%s\n", err_buf); + if (strerror_r(errno, buf, sizeof(buf) == 0)) { + snprintf(err_buf, err_buf_len, + "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, buf); + print_debug("%s\n", err_buf); + } return false; } else { return ptrace_waitpid(pid); @@ -401,6 +404,7 @@ // the list of threads within the same process. read_lib_info(ph); +#ifdef INCLUDE_SA_ATTACH // read thread info read_thread_info(ph, add_new_thread); @@ -408,14 +412,47 @@ thr = ph->threads; while (thr) { // don't attach to the main thread again - if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) { + if (pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) { // even if one attach fails, we get return NULL Prelease(ph); return NULL; } thr = thr->next; } +#else + DIR *dir = NULL; + struct dirent *ent = NULL; + char taskpath[80]; + + // Find the lwpids to attach to by traversing the /proc//task/ directory, + // and attach to those. + snprintf (taskpath, sizeof (taskpath), "/proc/%ld/task", (unsigned long) pid); + if ((dir = opendir(taskpath)) != NULL) { + while ((ent = readdir (dir)) != NULL) { + unsigned long lwp; + + if ((lwp = strtoul (ent->d_name, NULL, 10)) != 0) { + add_new_thread (ph, 0, lwp); + + // Don't attach to the main thread again + if (lwp == (unsigned long) pid) { + continue; + } + + if (ptrace_attach(lwp, err_buf, err_buf_len) != true) { + Prelease(ph); + closedir (dir); + return NULL; + } + } + } + } else { + print_debug("Could not open /proc/%ld/task.\n", (unsigned long) pid); + Prelease(ph); + return NULL; + } + + closedir (dir); +#endif return ph; } - -#endif // INCLUDE_SA_ATTACH