Teach GDBserver's Linux backend about no unwaited-for children (TARGET_WAITKIND_NO_RESUMED).

GDBserver currently hangs forever in waitpid if the leader thread
exits before other threads, or if all resumed threads exit - e.g.,
next over a thread exit with sched-locking on.  This is exposed by
leader-exit.exp.  leader-exit.exp is part of a series of tests for a
set of related problems.  See
<http://www.sourceware.org/ml/gdb-patches/2011-10/msg00704.html>:

 "
 To recap, on the Linux kernel, ptrace/waitpid don't allow reaping the
 leader thread until all other threads in the group are reaped.  When
 the leader exits, it goes zombie, but waitpid will not return an exit
 status until the other threads are gone.  This is presently exercised
 by the gdb.threads/leader-exit.exp test.  The fix for that test, in
 linux-nat.c:wait_lwp, handles the case where we see the leader gone
 when we're stopping all threads to report an event to some other
 thread to the core.

 (...)

 The latter bit about not blocking if there no resumed threads in the
 process also applies to some other thread exiting, not just the main
 thread.  E.g., this test starts a thread, and runs to a breakpoint in
 that thread:

 ...
 (gdb) c
 Continuing.
 [New Thread 0x7ffff75a4700 (LWP 23397)]
 [Switching to Thread 0x7ffff75a4700 (LWP 23397)]

 Breakpoint 2, thread_a (arg=0x0) at ../../../src/gdb/testsuite/gdb.threads/no-unwaited-for-left.c:28
 28        return 0; /* break-here */
 (gdb) info threads
 * 2 Thread 0x7ffff75a4700 (LWP 23397)  thread_a (arg=0x0) at ../../../src/gdb/testsuite/gdb.threads/no-unwaited-for-left.c:28
   1 Thread 0x7ffff7fcb720 (LWP 23391)  0x00007ffff7bc606d in pthread_join (threadid=140737343276800, thread_return=0x0) at pthread_join.c:89

 The thread will exit as soon as we resume it.  But if we only resume
 that thread, leaving the rest of the threads stopped:

 (gdb) set scheduler-locking on
 (gdb) c
 Continuing.
 ^C^C^C^C^C^C^C^C
 "

This patch fixes the issues by implementing TARGET_WAITKIND_NO_RESUMED
on GDBserver, similarly to what the patch above did for native
Linux GDB.

gdb.threads/leader-exit.exp now passes.

gdb.threads/no-unwaited-for-left.exp now at least errors out instead
of hanging:

 continue
 Continuing.
 warning: Remote failure reply: E.No unwaited-for children left.

 [Thread 15454] #1 stopped.
 0x00000034cf408e60 in pthread_join (threadid=140737353922368, thread_return=0x0) at pthread_join.c:93
 93          lll_wait_tid (pd->tid);
 (gdb) FAIL: gdb.threads/no-unwaited-for-left.exp: continue stops when the main thread exits

The gdb.threads/non-ldr-exc-*.exp tests are skipped because GDBserver
unfortunately doesn't support fork/exec yet, but I'm confident this
fixes the related issues.

I'm leaving modeling TARGET_WAITKIND_NO_RESUMED in the RSP for a
separate pass.

(BTW, in case of error in response to a vCont, it would be better for
GDB to query the target for the current thread, or re-select one,
instead of assuming current inferior_ptid is still the selected
thread.)

This implementation is a little different from GDB's, because I'm
avoiding bringing in more of this broken use of waitpid(PID) into
GDBserver.  Specifically, this avoids waitpid(PID) when stopping all
threads.  There's really no need for wait_for_sigstop to wait for each
LWP in turn.  Instead, with some refactoring, we make it reuse
linux_wait_for_event.

gdb/gdbserver/
2014-02-27  Pedro Alves  <palves@redhat.com>

	PR 12702
	* inferiors.h (A_I_NEXT, ALL_INFERIORS_TYPE, ALL_PROCESSES): New
	macros.
	* linux-low.c (delete_lwp, handle_extended_wait): Add debug
	output.
	(last_thread_of_process_p): Take a PID argument instead of a
	thread pointer.
	(linux_wait_for_lwp): Delete.
	(num_lwps, check_zombie_leaders, not_stopped_callback): New
	functions.
	(linux_low_filter_event): New function, party factored out from
	linux_wait_for_event.
	(linux_wait_for_event): Rename to ...
	(linux_wait_for_event_filtered): ... this.  Add new filter ptid
	argument.  Partly rewrite.  Always use waitpid(-1, WNOHANG) and
	sigsuspend.  Check for zombie leaders.
	(linux_wait_for_event): Reimplement as wrapper around
	linux_wait_for_event_filtered.
	(linux_wait_1): Handle TARGET_WAITKIND_NO_RESUMED.  Assume that if
	a normal or signal exit is seen, it's the whole process exiting.
	(wait_for_sigstop): No longer a for_each_inferior callback.
	Rewrite on top of linux_wait_for_event_filtered.
	(stop_all_lwps): Call wait_for_sigstop directly.
	* server.c (resume, handle_target_event): Handle
	TARGET_WAITKIND_NO_RESUMED.
This commit is contained in:
Pedro Alves 2014-02-27 14:30:08 +00:00
parent d632a0971c
commit fa96cb382c
4 changed files with 586 additions and 370 deletions

View File

@ -1,3 +1,31 @@
2014-02-27 Pedro Alves <palves@redhat.com>
PR 12702
* inferiors.h (A_I_NEXT, ALL_INFERIORS_TYPE, ALL_PROCESSES): New
macros.
* linux-low.c (delete_lwp, handle_extended_wait): Add debug
output.
(last_thread_of_process_p): Take a PID argument instead of a
thread pointer.
(linux_wait_for_lwp): Delete.
(num_lwps, check_zombie_leaders, not_stopped_callback): New
functions.
(linux_low_filter_event): New function, party factored out from
linux_wait_for_event.
(linux_wait_for_event): Rename to ...
(linux_wait_for_event_filtered): ... this. Add new filter ptid
argument. Partly rewrite. Always use waitpid(-1, WNOHANG) and
sigsuspend. Check for zombie leaders.
(linux_wait_for_event): Reimplement as wrapper around
linux_wait_for_event_filtered.
(linux_wait_1): Handle TARGET_WAITKIND_NO_RESUMED. Assume that if
a normal or signal exit is seen, it's the whole process exiting.
(wait_for_sigstop): No longer a for_each_inferior callback.
Rewrite on top of linux_wait_for_event_filtered.
(stop_all_lwps): Call wait_for_sigstop directly.
* server.c (resume, handle_target_event): Handle
TARGET_WAITKIND_NO_RESUMED.
2014-02-26 Joel Brobecker <brobecker@adacore.com>
* win32-low.c (psapi_get_dll_name,

View File

@ -99,6 +99,28 @@ void clear_inferior_list (struct inferior_list *list);
int one_inferior_p (struct inferior_list *list);
/* Helper for ALL_INFERIORS_TYPE. Gets the next element starting at
CUR, if CUR is not NULL. */
#define A_I_NEXT(type, list, cur) \
((cur) != NULL \
? (type *) ((struct inferior_list_entry *) cur)->next \
: NULL)
/* Iterate over all inferiors of type TYPE in LIST, open loop
style. */
#define ALL_INFERIORS_TYPE(type, list, cur, tmp) \
for ((cur) = (type *) (list)->head, (tmp) = A_I_NEXT (type, list, cur); \
(cur) != NULL; \
(cur) = (tmp), (tmp) = A_I_NEXT (type, list, cur))
/* Iterate over all inferiors in LIST, open loop style. */
#define ALL_INFERIORS(list, cur, tmp) \
ALL_INFERIORS_TYPE (struct inferior_list_entry, list, cur, tmp)
/* Iterate over all processes, open loop style. */
#define ALL_PROCESSES(cur, tmp) \
ALL_INFERIORS_TYPE (struct process_info, &all_processes, cur, tmp)
extern struct thread_info *current_inferior;
void remove_inferior (struct inferior_list *list,
struct inferior_list_entry *entry);

File diff suppressed because it is too large Load Diff

View File

@ -2367,8 +2367,18 @@ resume (struct thread_resume *actions, size_t num_actions)
{
last_ptid = mywait (minus_one_ptid, &last_status, 0, 1);
if (last_status.kind == TARGET_WAITKIND_NO_RESUMED)
{
/* No proper RSP support for this yet. At least return
error. */
sprintf (own_buf, "E.No unwaited-for children left.");
disable_async_io ();
return;
}
if (last_status.kind != TARGET_WAITKIND_EXITED
&& last_status.kind != TARGET_WAITKIND_SIGNALLED)
&& last_status.kind != TARGET_WAITKIND_SIGNALLED
&& last_status.kind != TARGET_WAITKIND_NO_RESUMED)
current_inferior->last_status = last_status;
/* From the client's perspective, all-stop mode always stops all
@ -3897,7 +3907,11 @@ handle_target_event (int err, gdb_client_data client_data)
last_ptid = mywait (minus_one_ptid, &last_status,
TARGET_WNOHANG, 1);
if (last_status.kind != TARGET_WAITKIND_IGNORE)
if (last_status.kind == TARGET_WAITKIND_NO_RESUMED)
{
/* No RSP support for this yet. */
}
else if (last_status.kind != TARGET_WAITKIND_IGNORE)
{
int pid = ptid_get_pid (last_ptid);
struct process_info *process = find_process_pid (pid);