5c49e68
2007-07-08  Jan Kratochvil  <jan.kratochvil@redhat.com>
5c49e68
5c49e68
	* linux-nat.c (linux_lwp_is_zombie): New function.
5c49e68
	(wait_lwp): Fix lockup on exit of the thread group leader.
5c49e68
	(linux_xfer_partial): Renamed to ...
5c49e68
	(linux_xfer_partial_lwp): ... here.
5c49e68
	(linux_xfer_partial): New function wrapping LINUX_XFER_PARTIAL_LWP.
5c49e68
5c49e68
--- ./gdb/linux-nat.c	3 Jul 2007 17:01:55 -0000	1.65
5c49e68
+++ ./gdb/linux-nat.c	7 Jul 2007 15:21:57 -0000
5c49e68
@@ -1343,6 +1343,31 @@ linux_handle_extended_wait (struct lwp_i
5c49e68
 		  _("unknown ptrace event %d"), event);
5c49e68
 }
5c49e68
 
5c49e68
+static int
5c49e68
+linux_lwp_is_zombie (long lwp)
5c49e68
+{
5c49e68
+  char buffer[MAXPATHLEN];
5c49e68
+  FILE *procfile;
5c49e68
+  int retval = 0;
5c49e68
+
5c49e68
+  sprintf (buffer, "/proc/%ld/status", lwp);
5c49e68
+  procfile = fopen (buffer, "r");
5c49e68
+  if (procfile == NULL)
5c49e68
+    {
5c49e68
+      warning (_("unable to open /proc file '%s'"), buffer);
5c49e68
+      return 0;
5c49e68
+    }
5c49e68
+  while (fgets (buffer, sizeof (buffer), procfile) != NULL)
5c49e68
+    if (strcmp (buffer, "State:\tZ (zombie)\n") == 0)
5c49e68
+      {
5c49e68
+	retval = 1;
5c49e68
+	break;
5c49e68
+      }
5c49e68
+  fclose (procfile);
5c49e68
+
5c49e68
+  return retval;
5c49e68
+}
5c49e68
+
5c49e68
 /* Wait for LP to stop.  Returns the wait status, or 0 if the LWP has
5c49e68
    exited.  */
5c49e68
 
5c49e68
@@ -1350,16 +1375,31 @@ static int
5c49e68
 wait_lwp (struct lwp_info *lp)
5c49e68
 {
5c49e68
   pid_t pid;
5c49e68
-  int status;
5c49e68
+  int status = 0;
5c49e68
   int thread_dead = 0;
5c49e68
 
5c49e68
   gdb_assert (!lp->stopped);
5c49e68
   gdb_assert (lp->status == 0);
5c49e68
 
5c49e68
-  pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
5c49e68
-  if (pid == -1 && errno == ECHILD)
5c49e68
+  /* Thread group leader may have exited but we would lock up by WAITPID as it
5c49e68
+     waits on all its threads; __WCLONE is not applicable for the leader.
5c49e68
+     The thread leader restrictions is only a performance optimization here.
5c49e68
+     LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
5c49e68
+     process; it gets ESRCH both for the zombie and for running processes.  */
5c49e68
+  if (is_lwp (lp->ptid) && GET_PID (lp->ptid) == GET_LWP (lp->ptid)
5c49e68
+      && linux_lwp_is_zombie (GET_LWP (lp->ptid)))
5c49e68
+    {
5c49e68
+      thread_dead = 1;
5c49e68
+      if (debug_linux_nat)
5c49e68
+	fprintf_unfiltered (gdb_stdlog, "WL: Threads leader %s vanished.\n",
5c49e68
+			    target_pid_to_str (lp->ptid));
5c49e68
+    }
5c49e68
+
5c49e68
+  if (!thread_dead)
5c49e68
     {
5c49e68
-      pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
5c49e68
+      pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
5c49e68
+      if (pid == -1 && errno == ECHILD)
5c49e68
+	pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
5c49e68
       if (pid == -1 && errno == ECHILD)
5c49e68
 	{
5c49e68
 	  /* The thread has previously exited.  We need to delete it
5c49e68
@@ -3144,10 +3159,12 @@ linux_proc_pending_signals (int pid, sig
5c49e68
   fclose (procfile);
5c49e68
 }
5c49e68
 
5c49e68
+/* Transfer from the specific LWP currently set by PID of INFERIOR_PTID.  */
5c49e68
+
5c49e68
 static LONGEST
5c49e68
-linux_xfer_partial (struct target_ops *ops, enum target_object object,
5c49e68
-                    const char *annex, gdb_byte *readbuf,
5c49e68
-		    const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
5c49e68
+linux_xfer_partial_lwp (struct target_ops *ops, enum target_object object,
5c49e68
+			const char *annex, gdb_byte *readbuf,
5c49e68
+			const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
5c49e68
 {
5c49e68
   LONGEST xfer;
5c49e68
 
5c49e68
@@ -3164,6 +3181,45 @@ linux_xfer_partial (struct target_ops *o
5c49e68
 			     offset, len);
5c49e68
 }
5c49e68
 
5c49e68
+/* nptl_db expects being able to transfer memory just by specifying PID.
5c49e68
+   After the thread group leader exists the Linux kernel turns the task
5c49e68
+   into zombie no longer permitting accesses to its memory.
5c49e68
+   Transfer the memory from an arbitrary LWP_LIST entry in such case.  */
5c49e68
+
5c49e68
+static LONGEST
5c49e68
+linux_xfer_partial (struct target_ops *ops, enum target_object object,
5c49e68
+                    const char *annex, gdb_byte *readbuf,
5c49e68
+		    const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
5c49e68
+{
5c49e68
+  LONGEST xfer;
5c49e68
+  struct lwp_info *lp;
5c49e68
+  /* Not using SAVE_INFERIOR_PTID already here for better performance.  */
5c49e68
+  struct cleanup *old_chain = NULL;
5c49e68
+  ptid_t inferior_ptid_orig = inferior_ptid;
5c49e68
+
5c49e68
+  errno = 0;
5c49e68
+  xfer = linux_xfer_partial_lwp (ops, object, annex, readbuf, writebuf,
5c49e68
+				 offset, len);
5c49e68
+
5c49e68
+  for (lp = lwp_list; xfer == 0 && (errno == EACCES || errno == ESRCH)
5c49e68
+		      && lp != NULL; lp = lp->next)
5c49e68
+    {
5c49e68
+      if (!is_lwp (lp->ptid) || ptid_equal (lp->ptid, inferior_ptid_orig))
5c49e68
+        continue;
5c49e68
+      
5c49e68
+      if (old_chain == NULL)
5c49e68
+	old_chain = save_inferior_ptid ();
5c49e68
+      inferior_ptid = BUILD_LWP (GET_LWP (lp->ptid), GET_LWP (lp->ptid));
5c49e68
+      errno = 0;
5c49e68
+      xfer = linux_xfer_partial_lwp (ops, object, annex, readbuf, writebuf,
5c49e68
+				     offset, len);
5c49e68
+    }
5c49e68
+
5c49e68
+  if (old_chain != NULL)
5c49e68
+    do_cleanups (old_chain);
5c49e68
+  return xfer;
5c49e68
+}
5c49e68
+
5c49e68
 /* Create a prototype generic Linux target.  The client can override
5c49e68
    it with local methods.  */
5c49e68