1 2007-07-08 Jan Kratochvil <jan.kratochvil@redhat.com>
3 * linux-nat.c (linux_lwp_is_zombie): New function.
4 (wait_lwp): Fix lockup on exit of the thread group leader.
5 (linux_xfer_partial): Renamed to ...
6 (linux_xfer_partial_lwp): ... here.
7 (linux_xfer_partial): New function wrapping LINUX_XFER_PARTIAL_LWP.
9 2008-02-24 Jan Kratochvil <jan.kratochvil@redhat.com>
13 Index: gdb-6.8.50.20081209/gdb/linux-nat.c
14 ===================================================================
15 --- gdb-6.8.50.20081209.orig/gdb/linux-nat.c 2008-12-10 01:27:34.000000000 +0100
16 +++ gdb-6.8.50.20081209/gdb/linux-nat.c 2008-12-10 01:28:14.000000000 +0100
17 @@ -1981,6 +1981,31 @@ linux_handle_extended_wait (struct lwp_i
18 _("unknown ptrace event %d"), event);
22 +linux_lwp_is_zombie (long lwp)
24 + char buffer[MAXPATHLEN];
28 + sprintf (buffer, "/proc/%ld/status", lwp);
29 + procfile = fopen (buffer, "r");
30 + if (procfile == NULL)
32 + warning (_("unable to open /proc file '%s'"), buffer);
35 + while (fgets (buffer, sizeof (buffer), procfile) != NULL)
36 + if (strcmp (buffer, "State:\tZ (zombie)\n") == 0)
46 /* Wait for LP to stop. Returns the wait status, or 0 if the LWP has
49 @@ -1988,16 +2013,31 @@ static int
50 wait_lwp (struct lwp_info *lp)
57 gdb_assert (!lp->stopped);
58 gdb_assert (lp->status == 0);
60 - pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
61 - if (pid == -1 && errno == ECHILD)
62 + /* Thread group leader may have exited but we would lock up by WAITPID as it
63 + waits on all its threads; __WCLONE is not applicable for the leader.
64 + The thread leader restrictions is only a performance optimization here.
65 + LINUX_NAT_THREAD_ALIVE cannot be used here as it requires a STOPPED
66 + process; it gets ESRCH both for the zombie and for running processes. */
67 + if (is_lwp (lp->ptid) && GET_PID (lp->ptid) == GET_LWP (lp->ptid)
68 + && linux_lwp_is_zombie (GET_LWP (lp->ptid)))
71 + if (debug_linux_nat)
72 + fprintf_unfiltered (gdb_stdlog, "WL: Threads leader %s vanished.\n",
73 + target_pid_to_str (lp->ptid));
78 - pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
79 + pid = my_waitpid (GET_LWP (lp->ptid), &status, 0);
80 + if (pid == -1 && errno == ECHILD)
81 + pid = my_waitpid (GET_LWP (lp->ptid), &status, __WCLONE);
82 if (pid == -1 && errno == ECHILD)
84 /* The thread has previously exited. We need to delete it
85 @@ -4153,8 +4193,10 @@ linux_nat_xfer_osdata (struct target_ops
89 +/* Transfer from the specific LWP currently set by PID of INFERIOR_PTID. */
92 -linux_xfer_partial (struct target_ops *ops, enum target_object object,
93 +linux_xfer_partial_lwp (struct target_ops *ops, enum target_object object,
94 const char *annex, gdb_byte *readbuf,
95 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
97 @@ -4201,6 +4243,45 @@ linux_xfer_partial (struct target_ops *o
101 +/* nptl_db expects being able to transfer memory just by specifying PID.
102 + After the thread group leader exists the Linux kernel turns the task
103 + into zombie no longer permitting accesses to its memory.
104 + Transfer the memory from an arbitrary LWP_LIST entry in such case. */
107 +linux_xfer_partial (struct target_ops *ops, enum target_object object,
108 + const char *annex, gdb_byte *readbuf,
109 + const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
112 + struct lwp_info *lp;
113 + /* Not using SAVE_INFERIOR_PTID already here for better performance. */
114 + struct cleanup *old_chain = NULL;
115 + ptid_t inferior_ptid_orig = inferior_ptid;
118 + xfer = linux_xfer_partial_lwp (ops, object, annex, readbuf, writebuf,
121 + for (lp = lwp_list; xfer == 0 && (errno == EACCES || errno == ESRCH)
122 + && lp != NULL; lp = lp->next)
124 + if (!is_lwp (lp->ptid) || ptid_equal (lp->ptid, inferior_ptid_orig))
127 + if (old_chain == NULL)
128 + old_chain = save_inferior_ptid ();
129 + inferior_ptid = BUILD_LWP (GET_LWP (lp->ptid), GET_LWP (lp->ptid));
131 + xfer = linux_xfer_partial_lwp (ops, object, annex, readbuf, writebuf,
135 + if (old_chain != NULL)
136 + do_cleanups (old_chain);
140 /* Create a prototype generic GNU/Linux target. The client can override
141 it with local methods. */