From: Roland McGrath Date: Fri, 9 Apr 2004 00:25:21 +0000 (+0000) Subject: 2004-04-08 Roland McGrath X-Git-Tag: v4.5.18~607 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=70b08530b80a6ee6591c38cf397fe0eeba1b4d7a;p=strace 2004-04-08 Roland McGrath * strace.c (main) [LINUX]: When attaching for -p, look in /proc/PID/task for all threads and attach them as presumed CLONE_THREAD children. --- diff --git a/strace.c b/strace.c index 23d72c8d..a8a1e400 100644 --- a/strace.c +++ b/strace.c @@ -44,6 +44,7 @@ #include #include #include +#include #if defined(IA64) && defined(LINUX) # include @@ -399,6 +400,64 @@ char *argv[]; continue; } #else /* !USE_PROCFS */ +# ifdef LINUX + if (tcp->flags & TCB_CLONE_THREAD) + continue; + { + char procdir[MAXPATHLEN]; + DIR *dir; + sprintf(procdir, "/proc/%d/task", tcp->pid); + dir = opendir(procdir); + if (dir != NULL) { + unsigned int ntid = 0, nerr = 0; + struct dirent *de; + int tid; + while ((de = readdir(dir)) != NULL) { + if (de->d_fileno == 0 || + de->d_name[0] == '.') + continue; + tid = atoi(de->d_name); + if (tid <= 0) + continue; + ++ntid; + if (ptrace(PTRACE_ATTACH, tid, + (char *) 1, 0) < 0) + ++nerr; + else if (tid != tcbtab[c]->pid) { + tcp = alloctcb(tid); + if (tcp == NULL) { + fprintf(stderr, "%s: out of memory\n", + progname); + exit(1); + } + tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_CLONE_DETACHED; + tcbtab[c]->nchildren++; + tcbtab[c]->nclone_threads++; + tcbtab[c]->nclone_detached++; + tcp->parent = tcbtab[c]; + } + } + closedir(dir); + if (nerr == ntid) { + perror("attach: ptrace(PTRACE_ATTACH, ...)"); + droptcb(tcp); + continue; + } + if (!qflag) { + ntid -= nerr; + if (ntid > 1) + fprintf(stderr, "\ +Process %u attached with %u threads - interrupt to quit\n", + tcp->pid, ntid); + else + fprintf(stderr, "\ +Process %u attached - interrupt to quit\n", + tcp->pid); + } + continue; + } + } +# endif if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) { perror("attach: ptrace(PTRACE_ATTACH, ...)"); droptcb(tcp);