< prev index next >

src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2003, 2013, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 20,41 **** * or visit www.oracle.com if you need additional information or have any * questions. * */ - #ifdef INCLUDE_SA_ATTACH - #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <errno.h> #include <elf.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/ptrace.h> #include <sys/uio.h> #include "libproc_impl.h" #if defined(x86_64) && !defined(amd64) #define amd64 1 #endif --- 20,42 ---- * or visit www.oracle.com if you need additional information or have any * questions. * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <errno.h> #include <elf.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/ptrace.h> #include <sys/uio.h> + #ifndef INCLUDE_SA_ATTACH + #include <dirent.h> + #endif #include "libproc_impl.h" #if defined(x86_64) && !defined(amd64) #define amd64 1 #endif
*** 218,230 **** // attach to a process/thread specified by "pid" 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); return false; } else { return ptrace_waitpid(pid); } } --- 219,233 ---- // attach to a process/thread specified by "pid" 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]; ! 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); } }
*** 399,421 **** // read library info and symbol tables, must do this before attaching threads, // as the symbols in the pthread library will be used to figure out // the list of threads within the same process. read_lib_info(ph); // read thread info read_thread_info(ph, add_new_thread); // attach to the threads 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) { // even if one attach fails, we get return NULL Prelease(ph); return NULL; } thr = thr->next; } return ph; } - - #endif // INCLUDE_SA_ATTACH --- 402,458 ---- // read library info and symbol tables, must do this before attaching threads, // as the symbols in the pthread library will be used to figure out // 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); // attach to the threads thr = ph->threads; while (thr) { // don't attach to the main thread again ! 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/<pid>/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; }
< prev index next >