+ if (linux_proc_get_tgid (pid) == pid)
+ {
+ DIR *dir;
+ char pathname[128];
+
+ sprintf (pathname, "/proc/%ld/task", pid);
+
+ dir = opendir (pathname);
+
+ if (!dir)
+ {
+ fprintf (stderr, "Could not open /proc/%ld/task.\n", pid);
+ fflush (stderr);
+ }
+ else
+ {
+ /* At this point we attached to the tgid. Scan the task for
+ existing threads. */
+ unsigned long lwp;
+ int new_threads_found;
+ int iterations = 0;
+ struct dirent *dp;
+
+ while (iterations < 2)
+ {
+ new_threads_found = 0;
+ /* Add all the other threads. While we go through the
+ threads, new threads may be spawned. Cycle through
+ the list of threads until we have done two iterations without
+ finding new threads. */
+ while ((dp = readdir (dir)) != NULL)
+ {
+ /* Fetch one lwp. */
+ lwp = strtoul (dp->d_name, NULL, 10);
+
+ /* Is this a new thread? */
+ if (lwp
+ && find_thread_ptid (ptid_build (pid, lwp, 0)) == NULL)
+ {
+ linux_attach_lwp_1 (lwp, 0);
+ new_threads_found++;
+
+ if (debug_threads)
+ fprintf (stderr, "\
+Found and attached to new lwp %ld\n", lwp);
+ }
+ }
+
+ if (!new_threads_found)
+ iterations++;
+ else
+ iterations = 0;
+
+ rewinddir (dir);
+ }
+ closedir (dir);
+ }
+ }
+