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