Blame SOURCES/0027-Implement-queueing-of-threads-before-dispatching-the.patch

8e169c
From 496f922dad9b58b891f417b937ac5fb0b0a8649a Mon Sep 17 00:00:00 2001
8e169c
From: Eugene Syromyatnikov <evgsyr@gmail.com>
8e169c
Date: Wed, 8 Aug 2018 21:41:39 +0200
8e169c
Subject: [PATCH 27/27] Implement queueing of threads before dispatching them
8e169c
8e169c
It is possible that some tracees call a lot of cheap syscalls too fast,
8e169c
and that can stress the tracer to the point some tracees are not served
8e169c
for indefinite amount of time.  In order to avoid that unfairness, try
8e169c
to collect all the pending tracees first along with the relevant
8e169c
information and only then dispatch the events.
8e169c
8e169c
* defs.h: Include "list.h".
8e169c
(struct tcb): Add wait_data_idx, delayed_wait_data, and wait_list
8e169c
fields.
8e169c
* strace.c (struct tcb_wait_data): Add "msg" field.
8e169c
(tcb_wait_tab, tcb_wait_tab_size): New static variables.
8e169c
(alloctcb): Initialize wait_list.
8e169c
(droptcb): Remove tcp from wait_list.
8e169c
(maybe_switch_tcbs): Get old pid from
8e169c
tcb_wait_tab[tcp->wait_data_idx].msg instead of calling
8e169c
ptrace(PTRACE_GETEVENTMSG).
8e169c
(trace_wait_data_size, init_trace_wait_data, copy_trace_wait_data,
8e169c
free_trace_wait_data, tcb_wait_tab_check_size): New functions, in order
8e169c
to allow the code outside next_event to operate with wait_data as with
8e169c
an opaque object (needed for dispatch_event and restart_delayed_tcb).
8e169c
(next_event): Add pending_tcps, extra_tcp, wait_nohang, elem, and
8e169c
wait_tab_pos variables; check for elements in pending_tcps and skip
8e169c
waiting if the list is not empty; check for extra_tcp and skip waiting
8e169c
along with swapping wait_data_idx with wait_extra_data_idx;
8e169c
after the initial wait4(), call wait4() in loop with WNOHANG flag set;
8e169c
fetch siginfo on signal and eventmsg on PTRACE_EVENT_EXEC;
8e169c
return the first tcp in pending_tcps list.
8e169c
(dispatch_event): Store a pointer to a copy of tcb_wait_data in
8e169c
tcp->delayed_wait_data if tcp's restart has to be delayed.
8e169c
(restart_delayed_tcb): Use tcp->delayed_wait_data, create a stub
8e169c
tcb_wait_data if it is NULL, free temporary trace_wait_data.
8e169c
* tests/Makefile.am (XFAIL_TEST): Remove looping_threads.test.
8e169c
8e169c
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=478419
8e169c
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=526740
8e169c
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=851457
8e169c
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1609318
8e169c
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1610774
8e169c
Co-Authored-by: Dmitry V. Levin <ldv@altlinux.org>
8e169c
Co-Authored-by: Denys Vlasenko <dvlasenk@redhat.com>
8e169c
Co-Authored-by: Andreas Schwab <aschwab@redhat.com>
8e169c
Co-Authored-by: Jeff Law <law@redhat.com>
8e169c
Co-Authored-by: DJ Delorie <dj@redhat.com>
8e169c
8e169c
Conflicts:
8e169c
	defs.h
8e169c
	tests/Makefile.am
8e169c
---
8e169c
 defs.h            |  12 ++
8e169c
 strace.c          | 393 +++++++++++++++++++++++++++++++++++++-----------------
8e169c
 tests/Makefile.am |   3 +-
8e169c
 3 files changed, 283 insertions(+), 125 deletions(-)
8e169c
8e169c
diff --git a/defs.h b/defs.h
8e169c
index 811bb0d..2a614a7 100644
8e169c
--- a/defs.h
8e169c
+++ b/defs.h
8e169c
@@ -36,6 +36,7 @@
8e169c
 #include "arch_defs.h"
8e169c
 #include "error_prints.h"
8e169c
 #include "gcc_compat.h"
8e169c
+#include "list.h"
8e169c
 #include "kernel_types.h"
8e169c
 #include "macros.h"
8e169c
 #include "mpers_type.h"
8e169c
@@ -218,6 +219,17 @@ struct tcb {
8e169c
 
8e169c
 	struct mmap_cache_t *mmap_cache;
8e169c
 
8e169c
+	/*
8e169c
+	 * Data that is stored during process wait traversal.
8e169c
+	 * We use indices as the actual data is stored in an array
8e169c
+	 * that is realloc'ed at runtime.
8e169c
+	 */
8e169c
+	size_t wait_data_idx;
8e169c
+	/** Wait data storage for a delayed process. */
8e169c
+	struct tcb_wait_data *delayed_wait_data;
8e169c
+	struct list_item wait_list;
8e169c
+
8e169c
+
8e169c
 #ifdef HAVE_LINUX_KVM_H
8e169c
 	struct vcpu_info *vcpu_info_list;
8e169c
 #endif
8e169c
diff --git a/strace.c b/strace.c
8e169c
index 0745838..0914dc7 100644
8e169c
--- a/strace.c
8e169c
+++ b/strace.c
8e169c
@@ -141,6 +141,7 @@ static struct tcb *current_tcp;
8e169c
 struct tcb_wait_data {
8e169c
 	enum trace_event te; /**< Event passed to dispatch_event() */
8e169c
 	int status;          /**< status, returned by wait4() */
8e169c
+	unsigned long msg;   /**< Value returned by PTRACE_GETEVENTMSG */
8e169c
 	siginfo_t si;        /**< siginfo, returned by PTRACE_GETSIGINFO */
8e169c
 };
8e169c
 
8e169c
@@ -148,6 +149,10 @@ static struct tcb **tcbtab;
8e169c
 static unsigned int nprocs;
8e169c
 static size_t tcbtabsize;
8e169c
 
8e169c
+static struct tcb_wait_data *tcb_wait_tab;
8e169c
+static size_t tcb_wait_tab_size;
8e169c
+
8e169c
+
8e169c
 #ifndef HAVE_PROGRAM_INVOCATION_NAME
8e169c
 char *program_invocation_name;
8e169c
 #endif
8e169c
@@ -745,6 +750,7 @@ alloctcb(int pid)
8e169c
 		tcp = tcbtab[i];
8e169c
 		if (!tcp->pid) {
8e169c
 			memset(tcp, 0, sizeof(*tcp));
8e169c
+			list_init(&tcp->wait_list);
8e169c
 			tcp->pid = pid;
8e169c
 #if SUPPORTED_PERSONALITIES > 1
8e169c
 			tcp->currpers = current_personality;
8e169c
@@ -833,6 +839,8 @@ droptcb(struct tcb *tcp)
8e169c
 	if (printing_tcp == tcp)
8e169c
 		printing_tcp = NULL;
8e169c
 
8e169c
+	list_remove(&tcp->wait_list);
8e169c
+
8e169c
 	memset(tcp, 0, sizeof(*tcp));
8e169c
 }
8e169c
 
8e169c
@@ -2051,10 +2059,8 @@ maybe_switch_tcbs(struct tcb *tcp, const int pid)
8e169c
 {
8e169c
 	FILE *fp;
8e169c
 	struct tcb *execve_thread;
8e169c
-	long old_pid = 0;
8e169c
+	long old_pid = tcb_wait_tab[tcp->wait_data_idx].msg;
8e169c
 
8e169c
-	if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &old_pid) < 0)
8e169c
-		return tcp;
8e169c
 	/* Avoid truncation in pid2tcb() param passing */
8e169c
 	if (old_pid <= 0 || old_pid == pid)
8e169c
 		return tcp;
8e169c
@@ -2207,20 +2213,74 @@ print_event_exit(struct tcb *tcp)
8e169c
 	line_ended();
8e169c
 }
8e169c
 
8e169c
-static const struct tcb_wait_data *
8e169c
-next_event(void)
8e169c
+static size_t
8e169c
+trace_wait_data_size(struct tcb *tcp)
8e169c
 {
8e169c
-	static struct tcb_wait_data wait_data;
8e169c
+	return sizeof(struct tcb_wait_data);
8e169c
+}
8e169c
 
8e169c
-	int pid;
8e169c
-	int status;
8e169c
-	struct tcb *tcp;
8e169c
-	struct tcb_wait_data *wd = &wait_data;
8e169c
-	struct rusage ru;
8e169c
+static struct tcb_wait_data *
8e169c
+init_trace_wait_data(void *p)
8e169c
+{
8e169c
+	struct tcb_wait_data *wd = p;
8e169c
+
8e169c
+	memset(wd, 0, sizeof(*wd));
8e169c
+
8e169c
+	return wd;
8e169c
+}
8e169c
 
8e169c
+static struct tcb_wait_data *
8e169c
+copy_trace_wait_data(const struct tcb_wait_data *wd)
8e169c
+{
8e169c
+	struct tcb_wait_data *new_wd = xmalloc(sizeof(*new_wd));
8e169c
+
8e169c
+	memcpy(new_wd, wd, sizeof(*wd));
8e169c
+
8e169c
+	return new_wd;
8e169c
+}
8e169c
+
8e169c
+static void
8e169c
+free_trace_wait_data(struct tcb_wait_data *wd)
8e169c
+{
8e169c
+	free(wd);
8e169c
+}
8e169c
+
8e169c
+static void
8e169c
+tcb_wait_tab_check_size(const size_t size)
8e169c
+{
8e169c
+	while (size >= tcb_wait_tab_size) {
8e169c
+		tcb_wait_tab = xgrowarray(tcb_wait_tab,
8e169c
+					  &tcb_wait_tab_size,
8e169c
+					  sizeof(tcb_wait_tab[0]));
8e169c
+	}
8e169c
+}
8e169c
+
8e169c
+static const struct tcb_wait_data *
8e169c
+next_event(void)
8e169c
+{
8e169c
 	if (interrupted)
8e169c
 		return NULL;
8e169c
 
8e169c
+	struct tcb *tcp = NULL;
8e169c
+	struct list_item *elem;
8e169c
+
8e169c
+	static EMPTY_LIST(pending_tcps);
8e169c
+	/* Handle the queued tcbs before waiting for new events.  */
8e169c
+	if (!list_is_empty(&pending_tcps))
8e169c
+		goto next_event_get_tcp;
8e169c
+
8e169c
+	static struct tcb *extra_tcp;
8e169c
+	static size_t wait_extra_data_idx;
8e169c
+	/* Handle the extra tcb event.  */
8e169c
+	if (extra_tcp) {
8e169c
+		tcp = extra_tcp;
8e169c
+		extra_tcp = NULL;
8e169c
+		tcp->wait_data_idx = wait_extra_data_idx;
8e169c
+
8e169c
+		debug_msg("dequeued extra event for pid %u", tcp->pid);
8e169c
+		goto next_event_exit;
8e169c
+	}
8e169c
+
8e169c
 	/*
8e169c
 	 * Used to exit simply when nprocs hits zero, but in this testcase:
8e169c
 	 *  int main(void) { _exit(!!fork()); }
8e169c
@@ -2262,8 +2322,10 @@ next_event(void)
8e169c
 	 * then the system call will be interrupted and
8e169c
 	 * the expiration will be handled by the signal handler.
8e169c
 	 */
8e169c
-	pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
8e169c
-	const int wait_errno = errno;
8e169c
+	int status;
8e169c
+	struct rusage ru;
8e169c
+	int pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
8e169c
+	int wait_errno = errno;
8e169c
 
8e169c
 	/*
8e169c
 	 * The window of opportunity to handle expirations
8e169c
@@ -2279,135 +2341,202 @@ next_event(void)
8e169c
 			return NULL;
8e169c
 	}
8e169c
 
8e169c
-	if (pid < 0) {
8e169c
-		if (wait_errno == EINTR) {
8e169c
-			wd->te = TE_NEXT;
8e169c
-			return wd;
8e169c
+	size_t wait_tab_pos = 0;
8e169c
+	bool wait_nohang = false;
8e169c
+
8e169c
+	/*
8e169c
+	 * Wait for new events until wait4() returns 0 (meaning that there's
8e169c
+	 * nothing more to wait for for now), or a second event for some tcb
8e169c
+	 * appears (which may happen if a tracee was SIGKILL'ed, for example).
8e169c
+	 */
8e169c
+	for (;;) {
8e169c
+		struct tcb_wait_data *wd;
8e169c
+
8e169c
+		if (pid < 0) {
8e169c
+			if (wait_errno == EINTR)
8e169c
+				break;
8e169c
+			if (wait_nohang)
8e169c
+				break;
8e169c
+			if (nprocs == 0 && wait_errno == ECHILD)
8e169c
+				return NULL;
8e169c
+			/*
8e169c
+			 * If nprocs > 0, ECHILD is not expected,
8e169c
+			 * treat it as any other error here:
8e169c
+			 */
8e169c
+			errno = wait_errno;
8e169c
+			perror_msg_and_die("wait4(__WALL)");
8e169c
 		}
8e169c
-		if (nprocs == 0 && wait_errno == ECHILD)
8e169c
-			return NULL;
8e169c
-		/*
8e169c
-		 * If nprocs > 0, ECHILD is not expected,
8e169c
-		 * treat it as any other error here:
8e169c
-		 */
8e169c
-		errno = wait_errno;
8e169c
-		perror_msg_and_die("wait4(__WALL)");
8e169c
-	}
8e169c
 
8e169c
-	wd->status = status;
8e169c
+		if (!pid)
8e169c
+			break;
8e169c
 
8e169c
-	if (pid == popen_pid) {
8e169c
-		if (!WIFSTOPPED(status))
8e169c
-			popen_pid = 0;
8e169c
-		wd->te = TE_NEXT;
8e169c
-		return wd;
8e169c
-	}
8e169c
+		if (pid == popen_pid) {
8e169c
+			if (!WIFSTOPPED(status))
8e169c
+				popen_pid = 0;
8e169c
+			break;
8e169c
+		}
8e169c
 
8e169c
-	if (debug_flag)
8e169c
-		print_debug_info(pid, status);
8e169c
+		if (debug_flag)
8e169c
+			print_debug_info(pid, status);
8e169c
 
8e169c
-	/* Look up 'pid' in our table. */
8e169c
-	tcp = pid2tcb(pid);
8e169c
+		/* Look up 'pid' in our table. */
8e169c
+		tcp = pid2tcb(pid);
8e169c
 
8e169c
-	if (!tcp) {
8e169c
-		tcp = maybe_allocate_tcb(pid, status);
8e169c
 		if (!tcp) {
8e169c
-			wd->te = TE_NEXT;
8e169c
-			return wd;
8e169c
+			tcp = maybe_allocate_tcb(pid, status);
8e169c
+			if (!tcp)
8e169c
+				goto next_event_wait_next;
8e169c
 		}
8e169c
-	}
8e169c
 
8e169c
-	clear_regs(tcp);
8e169c
+		if (cflag) {
8e169c
+			struct timespec stime = {
8e169c
+				.tv_sec = ru.ru_stime.tv_sec,
8e169c
+				.tv_nsec = ru.ru_stime.tv_usec * 1000
8e169c
+			};
8e169c
+			ts_sub(&tcp->dtime, &stime, &tcp->stime);
8e169c
+			tcp->stime = stime;
8e169c
+		}
8e169c
 
8e169c
-	/* Set current output file */
8e169c
-	set_current_tcp(tcp);
8e169c
+		tcb_wait_tab_check_size(wait_tab_pos);
8e169c
 
8e169c
-	if (cflag) {
8e169c
-		struct timespec stime = {
8e169c
-			.tv_sec = ru.ru_stime.tv_sec,
8e169c
-			.tv_nsec = ru.ru_stime.tv_usec * 1000
8e169c
-		};
8e169c
-		ts_sub(&tcp->dtime, &stime, &tcp->stime);
8e169c
-		tcp->stime = stime;
8e169c
-	}
8e169c
+		/* Initialise a new wait data structure.  */
8e169c
+		wd = tcb_wait_tab + wait_tab_pos;
8e169c
+		init_trace_wait_data(wd);
8e169c
+		wd->status = status;
8e169c
 
8e169c
-	if (WIFSIGNALED(status)) {
8e169c
-		wd->te = TE_SIGNALLED;
8e169c
-		return wd;
8e169c
-	}
8e169c
+		if (WIFSIGNALED(status)) {
8e169c
+			wd->te = TE_SIGNALLED;
8e169c
+		} else if (WIFEXITED(status)) {
8e169c
+			wd->te = TE_EXITED;
8e169c
+		} else {
8e169c
+			/*
8e169c
+			 * As WCONTINUED flag has not been specified to wait4,
8e169c
+			 * it cannot be WIFCONTINUED(status), so the only case
8e169c
+			 * that remains is WIFSTOPPED(status).
8e169c
+			 */
8e169c
 
8e169c
-	if (WIFEXITED(status)) {
8e169c
-		wd->te = TE_EXITED;
8e169c
-		return wd;
8e169c
+			const unsigned int sig = WSTOPSIG(status);
8e169c
+			const unsigned int event = (unsigned int) status >> 16;
8e169c
+
8e169c
+			switch (event) {
8e169c
+			case 0:
8e169c
+				/*
8e169c
+				 * Is this post-attach SIGSTOP?
8e169c
+				 * Interestingly, the process may stop
8e169c
+				 * with STOPSIG equal to some other signal
8e169c
+				 * than SIGSTOP if we happened to attach
8e169c
+				 * just before the process takes a signal.
8e169c
+				 */
8e169c
+				if (sig == SIGSTOP &&
8e169c
+				    (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
8e169c
+					debug_func_msg("ignored SIGSTOP on "
8e169c
+						       "pid %d", tcp->pid);
8e169c
+					tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
8e169c
+					wd->te = TE_RESTART;
8e169c
+				} else if (sig == syscall_trap_sig) {
8e169c
+					wd->te = TE_SYSCALL_STOP;
8e169c
+				} else {
8e169c
+					/*
8e169c
+					 * True if tracee is stopped by signal
8e169c
+					 * (as opposed to "tracee received
8e169c
+					 * signal").
8e169c
+					 * TODO: shouldn't we check for
8e169c
+					 * errno == EINVAL too?
8e169c
+					 * We can get ESRCH instead, you know...
8e169c
+					 */
8e169c
+					bool stopped = ptrace(PTRACE_GETSIGINFO,
8e169c
+						pid, 0, &wd->si) < 0;
8e169c
+
8e169c
+					wd->te = stopped ? TE_GROUP_STOP
8e169c
+							 : TE_SIGNAL_DELIVERY_STOP;
8e169c
+				}
8e169c
+				break;
8e169c
+			case PTRACE_EVENT_STOP:
8e169c
+				/*
8e169c
+				 * PTRACE_INTERRUPT-stop or group-stop.
8e169c
+				 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
8e169c
+				 */
8e169c
+				switch (sig) {
8e169c
+				case SIGSTOP:
8e169c
+				case SIGTSTP:
8e169c
+				case SIGTTIN:
8e169c
+				case SIGTTOU:
8e169c
+					wd->te = TE_GROUP_STOP;
8e169c
+					break;
8e169c
+				default:
8e169c
+					wd->te = TE_RESTART;
8e169c
+				}
8e169c
+				break;
8e169c
+			case PTRACE_EVENT_EXEC:
8e169c
+					/*
8e169c
+					 * TODO: shouldn't we check for
8e169c
+					 * errno == EINVAL here, too?
8e169c
+					 * We can get ESRCH instead, you know...
8e169c
+					 */
8e169c
+				if (ptrace(PTRACE_GETEVENTMSG, pid, NULL,
8e169c
+				    &wd->msg) < 0)
8e169c
+					wd->msg = 0;
8e169c
+
8e169c
+				wd->te = TE_STOP_BEFORE_EXECVE;
8e169c
+				break;
8e169c
+			case PTRACE_EVENT_EXIT:
8e169c
+				wd->te = TE_STOP_BEFORE_EXIT;
8e169c
+				break;
8e169c
+			default:
8e169c
+				wd->te = TE_RESTART;
8e169c
+			}
8e169c
+		}
8e169c
+
8e169c
+		if (!wd->te)
8e169c
+			error_func_msg("Tracing event hasn't been determined "
8e169c
+				       "for pid %d, status %0#x", pid, status);
8e169c
+
8e169c
+		if (!list_is_empty(&tcp->wait_list)) {
8e169c
+			wait_extra_data_idx = wait_tab_pos;
8e169c
+			extra_tcp = tcp;
8e169c
+			debug_func_msg("queued extra pid %d", tcp->pid);
8e169c
+		} else {
8e169c
+			tcp->wait_data_idx = wait_tab_pos;
8e169c
+			list_append(&pending_tcps, &tcp->wait_list);
8e169c
+			debug_func_msg("queued pid %d", tcp->pid);
8e169c
+		}
8e169c
+
8e169c
+		wait_tab_pos++;
8e169c
+
8e169c
+		if (extra_tcp)
8e169c
+			break;
8e169c
+
8e169c
+next_event_wait_next:
8e169c
+		pid = wait4(-1, &status, __WALL | WNOHANG, (cflag ? &ru : NULL));
8e169c
+		wait_errno = errno;
8e169c
+		wait_nohang = true;
8e169c
 	}
8e169c
 
8e169c
-	/*
8e169c
-	 * As WCONTINUED flag has not been specified to wait4,
8e169c
-	 * it cannot be WIFCONTINUED(status), so the only case
8e169c
-	 * that remains is WIFSTOPPED(status).
8e169c
-	 */
8e169c
+next_event_get_tcp:
8e169c
+	elem = list_remove_head(&pending_tcps);
8e169c
+
8e169c
+	if (!elem) {
8e169c
+		tcb_wait_tab_check_size(0);
8e169c
+		memset(tcb_wait_tab, 0, sizeof(*tcb_wait_tab));
8e169c
+		tcb_wait_tab->te = TE_NEXT;
8e169c
 
8e169c
+		return tcb_wait_tab;
8e169c
+	} else {
8e169c
+		tcp = list_elem(elem, struct tcb, wait_list);
8e169c
+		debug_func_msg("dequeued pid %d", tcp->pid);
8e169c
+	}
8e169c
+
8e169c
+next_event_exit:
8e169c
 	/* Is this the very first time we see this tracee stopped? */
8e169c
 	if (tcp->flags & TCB_STARTUP)
8e169c
 		startup_tcb(tcp);
8e169c
 
8e169c
-	const unsigned int sig = WSTOPSIG(status);
8e169c
-	const unsigned int event = (unsigned int) status >> 16;
8e169c
+	clear_regs(tcp);
8e169c
 
8e169c
-	switch (event) {
8e169c
-	case 0:
8e169c
-		/*
8e169c
-		 * Is this post-attach SIGSTOP?
8e169c
-		 * Interestingly, the process may stop
8e169c
-		 * with STOPSIG equal to some other signal
8e169c
-		 * than SIGSTOP if we happened to attach
8e169c
-		 * just before the process takes a signal.
8e169c
-		 */
8e169c
-		if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
8e169c
-			debug_func_msg("ignored SIGSTOP on pid %d", tcp->pid);
8e169c
-			tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
8e169c
-			wd->te = TE_RESTART;
8e169c
-		} else if (sig == syscall_trap_sig) {
8e169c
-			wd->te = TE_SYSCALL_STOP;
8e169c
-		} else {
8e169c
-			memset(&wd->si, 0, sizeof(wd->si));
8e169c
-			/*
8e169c
-			 * True if tracee is stopped by signal
8e169c
-			 * (as opposed to "tracee received signal").
8e169c
-			 * TODO: shouldn't we check for errno == EINVAL too?
8e169c
-			 * We can get ESRCH instead, you know...
8e169c
-			 */
8e169c
-			bool stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, &wd->si) < 0;
8e169c
-			wd->te = stopped ? TE_GROUP_STOP : TE_SIGNAL_DELIVERY_STOP;
8e169c
-		}
8e169c
-		break;
8e169c
-	case PTRACE_EVENT_STOP:
8e169c
-		/*
8e169c
-		 * PTRACE_INTERRUPT-stop or group-stop.
8e169c
-		 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
8e169c
-		 */
8e169c
-		switch (sig) {
8e169c
-		case SIGSTOP:
8e169c
-		case SIGTSTP:
8e169c
-		case SIGTTIN:
8e169c
-		case SIGTTOU:
8e169c
-			wd->te = TE_GROUP_STOP;
8e169c
-			break;
8e169c
-		default:
8e169c
-			wd->te = TE_RESTART;
8e169c
-		}
8e169c
-		break;
8e169c
-	case PTRACE_EVENT_EXEC:
8e169c
-		wd->te = TE_STOP_BEFORE_EXECVE;
8e169c
-		break;
8e169c
-	case PTRACE_EVENT_EXIT:
8e169c
-		wd->te = TE_STOP_BEFORE_EXIT;
8e169c
-		break;
8e169c
-	default:
8e169c
-		wd->te = TE_RESTART;
8e169c
-	}
8e169c
+	/* Set current output file */
8e169c
+	set_current_tcp(tcp);
8e169c
 
8e169c
-	return wd;
8e169c
+	return tcb_wait_tab + tcp->wait_data_idx;
8e169c
 }
8e169c
 
8e169c
 static int
8e169c
@@ -2569,8 +2698,15 @@ dispatch_event(const struct tcb_wait_data *wd)
8e169c
 		return false;
8e169c
 
8e169c
 	/* If the process is being delayed, do not ptrace_restart just yet */
8e169c
-	if (syscall_delayed(current_tcp))
8e169c
+	if (syscall_delayed(current_tcp)) {
8e169c
+		if (current_tcp->delayed_wait_data)
8e169c
+			error_func_msg("pid %d has delayed wait data set"
8e169c
+				       " already", current_tcp->pid);
8e169c
+
8e169c
+		current_tcp->delayed_wait_data = copy_trace_wait_data(wd);
8e169c
+
8e169c
 		return true;
8e169c
+	}
8e169c
 
8e169c
 	if (ptrace_restart(restart_op, current_tcp, restart_sig) < 0) {
8e169c
 		/* Note: ptrace_restart emitted error message */
8e169c
@@ -2583,7 +2719,15 @@ dispatch_event(const struct tcb_wait_data *wd)
8e169c
 static bool
8e169c
 restart_delayed_tcb(struct tcb *const tcp)
8e169c
 {
8e169c
-	const struct tcb_wait_data wd = { .te = TE_RESTART };
8e169c
+	struct tcb_wait_data *wd = tcp->delayed_wait_data;
8e169c
+
8e169c
+	if (!wd) {
8e169c
+		error_func_msg("No delayed wait data found for pid %d",
8e169c
+			       tcp->pid);
8e169c
+		wd = init_trace_wait_data(alloca(trace_wait_data_size(tcp)));
8e169c
+	}
8e169c
+
8e169c
+	wd->te = TE_RESTART;
8e169c
 
8e169c
 	debug_func_msg("pid %d", tcp->pid);
8e169c
 
8e169c
@@ -2591,9 +2735,12 @@ restart_delayed_tcb(struct tcb *const tcp)
8e169c
 
8e169c
 	struct tcb *const prev_tcp = current_tcp;
8e169c
 	current_tcp = tcp;
8e169c
-	bool ret = dispatch_event(&wd;;
8e169c
+	bool ret = dispatch_event(wd);
8e169c
 	current_tcp = prev_tcp;
8e169c
 
8e169c
+	free_trace_wait_data(tcp->delayed_wait_data);
8e169c
+	tcp->delayed_wait_data = NULL;
8e169c
+
8e169c
 	return ret;
8e169c
 }
8e169c
 
8e169c
diff --git a/tests/Makefile.am b/tests/Makefile.am
8e169c
index de7a3d2..51f00a9 100644
8e169c
--- a/tests/Makefile.am
8e169c
+++ b/tests/Makefile.am
8e169c
@@ -350,8 +350,7 @@ XFAIL_TESTS_m32 = $(STACKTRACE_TESTS)
8e169c
 XFAIL_TESTS_mx32 = $(STACKTRACE_TESTS)
8e169c
 XFAIL_TESTS_x86_64 = int_0x80.gen.test
8e169c
 XFAIL_TESTS_x32 = int_0x80.gen.test
8e169c
-XFAIL_TESTS = $(XFAIL_TESTS_$(MPERS_NAME)) $(XFAIL_TESTS_$(ARCH)) \
8e169c
-	      looping_threads.test
8e169c
+XFAIL_TESTS = $(XFAIL_TESTS_$(MPERS_NAME)) $(XFAIL_TESTS_$(ARCH))
8e169c
 
8e169c
 TEST_LOG_COMPILER = env
8e169c
 AM_TEST_LOG_FLAGS = STRACE_ARCH=$(ARCH) STRACE_NATIVE_ARCH=$(NATIVE_ARCH) \
8e169c
-- 
8e169c
2.1.4
8e169c