From: Pedro Alves <pedro@codesourcery.com>
To: gdb-patches@sourceware.org, lgustavo@codesourcery.com
Subject: Re: [PATCH, gdbserver] Scan for existing threads during attachment on Linux
Date: Mon, 23 May 2011 10:38:00 -0000 [thread overview]
Message-ID: <201105231137.53423.pedro@codesourcery.com> (raw)
In-Reply-To: <ir6ck7$9ij$1@dough.gmane.org>
On Friday 20 May 2011 19:42:02, Luis Machado wrote:
> +/* Attach to PID. If PID is the tgdi, attach to it and stop
> + all of its threads. */
> +
> int
> linux_attach (unsigned long pid)
> {
> + DIR *dir;
> + char pathname[128];
> + int is_tgid = 0;
> + FILE *fd;
> +
> + /* Attach to PID. We will check for other threads
> + soon. */
> linux_attach_lwp_1 (pid, 1);
> - linux_add_process (pid, 1);
>
> - if (!non_stop)
> + /* Find out what is the tgid of this lwp. */
> + sprintf (pathname, "/proc/%ld/stat", pid);
Can you factor this into a standalone function, something
like
static int
tgid_of_pid (int pid)
{
...
}
please?
> +
> + fd = fopen (pathname, "r");
> +
> + if (fd != NULL)
> {
> - struct thread_info *thread;
> + int proc_id, ppid, pgrp;
> + char comm[NAME_MAX + 1], state;
>
> - /* Don't ignore the initial SIGSTOP if we just attached to this
> - process. It will be collected by wait shortly. */
> - thread = find_thread_ptid (ptid_build (pid, pid, 0));
> - thread->last_resume_kind = resume_stop;
> + fscanf (fd, "%d %s %c %d %d", &proc_id, comm, &state, &ppid, &pgrp);
> + fclose (fd);
> +
> + if (pgrp == pid)
> + is_tgid = 1;
> + }
> + else
> + {
> + fprintf (stderr, "Could not open /proc/%ld/stat.\n", pid);
> + fflush (stderr);
> }
>
> + sprintf (pathname, "/proc/%ld/task", pid);
> +
> + dir = opendir (pathname);
> +
> + if (!dir)
> + {
> + fprintf (stderr, "Could not open /proc/%ld/task.\n", pid);
> + fflush (stderr);
> + }
Why even try to open the dir if is_tgid is false?
> + else if (is_tgid)
> + {
> + /* 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);
> + }
> +
> + linux_add_process (pid, 1);
> +
> + /* Mark the threads as stopped. */
> + if (!non_stop)
> + for_each_inferior (&all_threads, set_resume_kind_stop);
> +
> return 0;
> }
>
Otherwise okay.
--
Pedro Alves
next prev parent reply other threads:[~2011-05-23 10:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-20 18:41 Luis Machado
2011-05-23 10:38 ` Pedro Alves [this message]
2011-05-23 10:51 ` Pedro Alves
2011-05-23 15:45 ` Luis Machado
2011-05-23 18:25 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201105231137.53423.pedro@codesourcery.com \
--to=pedro@codesourcery.com \
--cc=gdb-patches@sourceware.org \
--cc=lgustavo@codesourcery.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox