Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH, gdbserver] Scan for existing threads during attachment on Linux
@ 2011-05-20 18:41 Luis Machado
  2011-05-23 10:38 ` Pedro Alves
  0 siblings, 1 reply; 5+ messages in thread
From: Luis Machado @ 2011-05-20 18:41 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1346 bytes --]

Hi,

This patch deals with GDBServer's behavior on Linux only.

Currently, when told to attach to a PID, GDBServer does so without 
checking for other threads that may already exist in the thread group of 
which that PID is part of. As a result, it leaves threads running in the 
background until GDB connects to it and allows the use of thread_db to 
list/attach to the threads.

if GDB takes too long to connect to GDBServer, those existing threads 
that were not detected by GDBServer may end, and the user loses the 
opportunity to inspect them.

The patch changes the behavior of the linux_attach (...) function so it 
checks if the PID that we should attach to is the thread group leader. 
If so, we scan /proc/<pid>/task for existing threads and attach to them 
as well.

In that case, when GDB connects to GDBServer, the process will still 
hold all of its threads.

If the user asks GDBServer to attach to a PID that is not the thread 
group leader, that means we will attach to a single thread. The patch 
doesn't change this case, but is this really a useful use case? Or 
should GDBServer look for the PID's thread group leader and attach to 
all the threads that are part of that group?

If attaching to a single thread is useful, this patch should be OK. If 
not, i can change the patch to change that scenario.

Ok?

Regards,
Luis

[-- Attachment #2: stop_threads.diff --]
[-- Type: text/x-patch, Size: 3357 bytes --]

2011-05-20  Luis Machado  <lgustavo@codesourcery.com>

	* linux-low.c (set_resume_kind_stop): New function.
	(linux_attach): Scan for existing threads when attaching to a
	process.

--- .pc/stop_threads.diff/gdb/gdbserver/linux-low.c	2011-05-20 10:37:41.915272001 -0300
+++ gdb/gdbserver/linux-low.c	2011-05-20 14:25:59.019272001 -0300
@@ -561,6 +561,16 @@ linux_create_inferior (char *program, ch
   return pid;
 }
 
+/* Set a threads's last_resume_kind to RESUME_STOP.  */
+
+static void
+set_resume_kind_stop (struct inferior_list_entry *entry)
+{
+  struct thread_info *thread= (struct thread_info *) entry;
+
+  thread->last_resume_kind = resume_stop;
+}
+
 /* Attach to an inferior process.  */
 
 static void
@@ -646,22 +656,100 @@ linux_attach_lwp (unsigned long lwpid)
   linux_attach_lwp_1 (lwpid, 0);
 }
 
+/* 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);
+
+  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);
+    }
+  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;
 }
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH, gdbserver] Scan for existing threads during attachment on Linux
  2011-05-20 18:41 [PATCH, gdbserver] Scan for existing threads during attachment on Linux Luis Machado
@ 2011-05-23 10:38 ` Pedro Alves
  2011-05-23 10:51   ` Pedro Alves
  2011-05-23 15:45   ` Luis Machado
  0 siblings, 2 replies; 5+ messages in thread
From: Pedro Alves @ 2011-05-23 10:38 UTC (permalink / raw)
  To: gdb-patches, lgustavo

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH, gdbserver] Scan for existing threads during attachment on Linux
  2011-05-23 10:38 ` Pedro Alves
@ 2011-05-23 10:51   ` Pedro Alves
  2011-05-23 15:45   ` Luis Machado
  1 sibling, 0 replies; 5+ messages in thread
From: Pedro Alves @ 2011-05-23 10:51 UTC (permalink / raw)
  To: gdb-patches; +Cc: lgustavo

Sorry, I missed this before.

On Monday 23 May 2011 11:37:53, Pedro Alves wrote:
> > +  /* Mark the threads as stopped.  */
> > +  if (!non_stop)
> > +    for_each_inferior (&all_threads, set_resume_kind_stop);

You don't want to do this to all_threads.  You want to do
this to the threads of PID.  Might as well do it in the
loop you've added, right after attaching to each lwp?

There's a comment in linux_attach_lwp_1 that is now stale:

>	??? If the process already has several threads we leave the other
>	threads running.


Pedro Alves


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH, gdbserver] Scan for existing threads during attachment on Linux
  2011-05-23 10:38 ` Pedro Alves
  2011-05-23 10:51   ` Pedro Alves
@ 2011-05-23 15:45   ` Luis Machado
  2011-05-23 18:25     ` Pedro Alves
  1 sibling, 1 reply; 5+ messages in thread
From: Luis Machado @ 2011-05-23 15:45 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 3747 bytes --]

On 05/23/2011 07:37 AM, Pedro Alves wrote:
> 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?
>

Done.

>> +
>> +  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?
>

Doesn't make sense really. Fixed.

>> +  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.
>

I've also addressed the two comments from the other reply. We're 
stopping only the threads from the PID we're attaching to.

The stale comments were updated to reflect the current situation. We can 
still have a lwp that isn't the tgid, but we handle the case of pid == 
tgid correctly now. The new comments reflect that.

Luis

[-- Attachment #2: stop_threads.diff --]
[-- Type: text/x-patch, Size: 4489 bytes --]

2011-05-23  Luis Machado  <lgustavo@codesourcery.com>

	* linux-low.c (tgid_of_pid): New function.
	(linux_attach): Scan for existing threads when attaching to a
	process that is the tgid.

--- .pc/stop_threads.diff/gdb/gdbserver/linux-low.c	2011-05-23 12:05:42.447095000 -0300
+++ gdb/gdbserver/linux-low.c	2011-05-23 12:40:47.735095000 -0300
@@ -495,6 +495,38 @@ get_stop_pc (struct lwp_info *lwp)
   return stop_pc;
 }
 
+/* Extract the tgid (thread group leader id) of PID.  Return
+   the tgid if successful and 0 otherwise.  */
+
+static unsigned long
+tgid_of_pid (unsigned long pid)
+{
+  char pathname[128];
+  FILE *fd;
+  unsigned long tgid = 0;
+
+  sprintf (pathname, "/proc/%ld/stat", pid);
+
+  fd = fopen (pathname, "r");
+
+  if (fd != NULL)
+    {
+      int proc_id, ppid, pgrp;
+      char comm[NAME_MAX + 1], state;
+
+      /* Extract the pgrp field.  */
+      fscanf (fd, "%d %s %c %d %d", &proc_id, comm, &state, &ppid, &pgrp);
+      fclose (fd);
+      tgid = pgrp;
+    }
+  else
+    {
+      fprintf (stderr, "Could not open /proc/%ld/stat.\n", pid);
+      fflush (stderr);
+    }
+    return tgid;
+}
+
 static void *
 add_lwp (ptid_t ptid)
 {
@@ -586,7 +618,9 @@ linux_attach_lwp_1 (unsigned long lwpid,
     }
 
   if (initial)
-    /* NOTE/FIXME: This lwp might have not been the tgid.  */
+    /* If lwp is the tgid, we handle adding existing threads later.
+       Otherwise we just add lwp without bothering about any other
+       threads.  */
     ptid = ptid_build (lwpid, lwpid, 0);
   else
     {
@@ -621,8 +655,10 @@ linux_attach_lwp_1 (unsigned long lwpid,
 	In this case we want the process thread to stop.
 	This is handled by having linux_attach set last_resume_kind ==
 	resume_stop after we return.
-	??? If the process already has several threads we leave the other
-	threads running.
+
+	If the pid we are attaching to is also the tgid, we attach to and
+	stop all the existing threads.  Otherwise, we attach to pid and
+	ignore any other threads in the same group as this pid.
 
      3) GDB is connecting to gdbserver and is requesting an enumeration of all
 	existing threads.
@@ -646,22 +682,93 @@ linux_attach_lwp (unsigned long lwpid)
   linux_attach_lwp_1 (lwpid, 0);
 }
 
+/* Attach to PID.  If PID is the tgdi, attach to it and stop
+   all of its threads.  */
+
 int
 linux_attach (unsigned long pid)
 {
+  struct thread_info *thread;
+
+  /* Attach to PID.  We will check for other threads
+     soon.  */
   linux_attach_lwp_1 (pid, 1);
   linux_add_process (pid, 1);
 
   if (!non_stop)
     {
-      struct thread_info *thread;
-
-     /* 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;
     }
 
+  if (tgid_of_pid (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++;
+
+		      /* Mark the threads as stopped.  */
+		      if (!non_stop)
+			{
+			  thread = find_thread_ptid (ptid_build (pid, lwp, 0));
+
+			  /* Check if we actually attached to this thread.  It
+			     may have failed.  */
+			  if (thread)
+			    thread->last_resume_kind = resume_stop;
+			}
+
+		      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);
+	}
+    }
+
   return 0;
 }
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH, gdbserver] Scan for existing threads during attachment on Linux
  2011-05-23 15:45   ` Luis Machado
@ 2011-05-23 18:25     ` Pedro Alves
  0 siblings, 0 replies; 5+ messages in thread
From: Pedro Alves @ 2011-05-23 18:25 UTC (permalink / raw)
  To: gdb-patches, lgustavo

On Monday 23 May 2011 16:45:08, Luis Machado wrote:
> 2011-05-23  Luis Machado  <lgustavo@codesourcery.com>
> 
>         * linux-low.c (tgid_of_pid): New function.
>         (linux_attach): Scan for existing threads when attaching to a
>         process that is the tgid.
> 
> --- .pc/stop_threads.diff/gdb/gdbserver/linux-low.c     2011-05-23 12:05:42.447095000 -0300
> +++ gdb/gdbserver/linux-low.c   2011-05-23 12:40:47.735095000 -0300
> @@ -495,6 +495,38 @@ get_stop_pc (struct lwp_info *lwp)
>    return stop_pc;
>  }
>  
> +/* Extract the tgid (thread group leader id) of PID.  Return
> +   the tgid if successful and 0 otherwise.  */
> +
> +static unsigned long
> +tgid_of_pid (unsigned long pid)
> +{
> +  char pathname[128];
> +  FILE *fd;
> +  unsigned long tgid = 0;
> +
> +  sprintf (pathname, "/proc/%ld/stat", pid);
> +
> +  fd = fopen (pathname, "r");
> +
> +  if (fd != NULL)
> +    {
> +      int proc_id, ppid, pgrp;
> +      char comm[NAME_MAX + 1], state;
> +
> +      /* Extract the pgrp field.  */
> +      fscanf (fd, "%d %s %c %d %d", &proc_id, comm, &state, &ppid, &pgrp);

Wait, is that field really the process group?
That is not the same as the thread group.

> +      fclose (fd);
> +      tgid = pgrp;
> +    }
> +  else
> +    {
> +      fprintf (stderr, "Could not open /proc/%ld/stat.\n", pid);
> +      fflush (stderr);
> +    }
> +    return tgid;
> +}
> +
>  static void *
>  add_lwp (ptid_t ptid)
>  {
> @@ -586,7 +618,9 @@ linux_attach_lwp_1 (unsigned long lwpid,
>      }
>  
>    if (initial)
> -    /* NOTE/FIXME: This lwp might have not been the tgid.  */
> +    /* If lwp is the tgid, we handle adding existing threads later.
> +       Otherwise we just add lwp without bothering about any other
> +       threads.  */
>      ptid = ptid_build (lwpid, lwpid, 0);
>    else
>      {
> @@ -621,8 +655,10 @@ linux_attach_lwp_1 (unsigned long lwpid,
>         In this case we want the process thread to stop.
>         This is handled by having linux_attach set last_resume_kind ==
>         resume_stop after we return.
> -       ??? If the process already has several threads we leave the other
> -       threads running.
> +
> +       If the pid we are attaching to is also the tgid, we attach to and
> +       stop all the existing threads.  Otherwise, we attach to pid and
> +       ignore any other threads in the same group as this pid.
>  
>       3) GDB is connecting to gdbserver and is requesting an enumeration of all
>         existing threads.
> @@ -646,22 +682,93 @@ linux_attach_lwp (unsigned long lwpid)
>    linux_attach_lwp_1 (lwpid, 0);
>  }
>  
> +/* Attach to PID.  If PID is the tgdi, attach to it and stop
> +   all of its threads.  */
> +
>  int
>  linux_attach (unsigned long pid)
>  {
> +  struct thread_info *thread;
> +
> +  /* Attach to PID.  We will check for other threads
> +     soon.  */
>    linux_attach_lwp_1 (pid, 1);
>    linux_add_process (pid, 1);
>  
>    if (!non_stop)
>      {
> -      struct thread_info *thread;
> -
> -     /* 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;
>      }
>  
> +  if (tgid_of_pid (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)

Line too long.

> +                   {
> +                     linux_attach_lwp_1 (lwp, 0);
> +                     new_threads_found++;
> +
> +                     /* Mark the threads as stopped.  */
> +                     if (!non_stop)
> +                       {
> +                         thread = find_thread_ptid (ptid_build (pid, lwp, 0));
> +
> +                         /* Check if we actually attached to this thread.  It
> +                            may have failed.  */
> +                         if (thread)
> +                           thread->last_resume_kind = resume_stop;
> +                       }

One more thing... I'm curious to know whether setting resume_stop in
the threads actually made a difference?  server.c does a mywait just
after attaching, to collect the stop of the initial thread, and, given
we're in all-stop mode, that freezes all threads with resume_stop
anyway.

> +
> +                     if (debug_threads)
> +                       fprintf (stderr, "Found and attached to new lwp %ld\n", lwp);

This line too long.

> +                   }
> +               }
> +
> +             if (!new_threads_found)
> +               iterations++;
> +             else
> +               iterations = 0;
> +
> +             rewinddir (dir);
> +           }
> +         closedir (dir);
> +       }
> +    }
> +
>    return 0;
>  }

-- 
Pedro Alves


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-05-23 18:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-20 18:41 [PATCH, gdbserver] Scan for existing threads during attachment on Linux Luis Machado
2011-05-23 10:38 ` Pedro Alves
2011-05-23 10:51   ` Pedro Alves
2011-05-23 15:45   ` Luis Machado
2011-05-23 18:25     ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox