Blame SOURCES/strace-rh851457.patch

384337
diff -Nrup a/defs.h b/defs.h
384337
--- a/defs.h	2016-05-29 20:29:14.000000000 -0400
384337
+++ b/defs.h	2016-07-22 16:52:17.891092163 -0400
384337
@@ -294,6 +294,9 @@ struct tcb {
384337
 	int pid;		/* If 0, this tcb is free */
384337
 	int qual_flg;		/* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
384337
 	int u_error;		/* Error code */
384337
+	int wait_status;        /* Status from last wait() */
384337
+	struct tcb *next_need_service;
384337
+				/* Linked list of tracees found by wait()s */
384337
 	long scno;		/* System call number */
384337
 	long u_arg[MAX_ARGS];	/* System call arguments */
384337
 #if defined(LINUX_MIPSN32) || defined(X32)
384337
diff -Nrup a/strace.c b/strace.c
384337
--- a/strace.c	2016-05-26 11:34:28.000000000 -0400
384337
+++ b/strace.c	2016-07-22 16:52:17.895092175 -0400
384337
@@ -2095,17 +2095,40 @@ startup_tcb(struct tcb *tcp)
384337
 	}
384337
 }
384337
 
384337
+static int remembered_pid;
384337
+static int remembered_status;
384337
+
384337
 /* Returns true iff the main trace loop has to continue. */
384337
 static bool
384337
 trace(void)
384337
 {
384337
 	int pid;
384337
+	struct tcb *tcp;
384337
+	struct tcb *found_tcps;
384337
+	struct tcb **nextp;
384337
+	struct tcb *next;
384337
+	int wnohang = 0;
384337
+
384337
+	if (remembered_pid) {
384337
+		pid = remembered_pid;
384337
+		remembered_pid = 0;
384337
+		if (debug_flag)
384337
+			fprintf(stderr, " [remembered wait(%#x) = %u]\n",
384337
+						remembered_status, pid);
384337
+		tcp = pid2tcb(pid); /* can't be NULL */
384337
+		tcp->wait_status = remembered_status;
384337
+		tcp->next_need_service = NULL;
384337
+		found_tcps = tcp;
384337
+		goto process_saved_tcbs;
384337
+	}
384337
+
384337
+	nextp = &found_tcps;
384337
+	found_tcps = NULL;
384337
+
384337
+	while (1) { /* RH 851457 - collect tcbs */
384337
 	int wait_errno;
384337
 	int status;
384337
-	bool stopped;
384337
-	unsigned int sig;
384337
 	unsigned int event;
384337
-	struct tcb *tcp;
384337
 	struct rusage ru;
384337
 
384337
 	if (interrupted)
384337
@@ -2134,14 +2157,24 @@ trace(void)
384337
 
384337
 	if (interactive)
384337
 		sigprocmask(SIG_SETMASK, &empty_set, NULL);
384337
-	pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
384337
+	pid = wait4(-1, &status, __WALL | wnohang, (cflag ? &ru : NULL));
384337
 	wait_errno = errno;
384337
 	if (interactive)
384337
 		sigprocmask(SIG_BLOCK, &blocked_set, NULL);
384337
 
384337
+	if (pid <= 0 && wnohang) {
384337
+		/* We had at least one successful
384337
+		 * wait() before. We waited
384337
+		 * with WNOHANG second time.
384337
+		 * Stop collecting more tracees,
384337
+		 * process what we already have.
384337
+		 */
384337
+		break; /* out of collect tcbs */
384337
+	}
384337
+
384337
 	if (pid < 0) {
384337
 		if (wait_errno == EINTR)
384337
-			return true;
384337
+			break; /* out of collect tcbs */
384337
 		if (nprocs == 0 && wait_errno == ECHILD)
384337
 			return false;
384337
 		/*
384337
@@ -2155,7 +2188,7 @@ trace(void)
384337
 	if (pid == popen_pid) {
384337
 		if (!WIFSTOPPED(status))
384337
 			popen_pid = 0;
384337
-		return true;
384337
+		break; /* out of collect tcbs */
384337
 	}
384337
 
384337
 	if (debug_flag)
384337
@@ -2167,14 +2200,9 @@ trace(void)
384337
 	if (!tcp) {
384337
 		tcp = maybe_allocate_tcb(pid, status);
384337
 		if (!tcp)
384337
-			return true;
384337
+			break; /* out of collect tcbs */
384337
 	}
384337
 
384337
-	if (WIFSTOPPED(status))
384337
-		get_regs(pid);
384337
-	else
384337
-		clear_regs();
384337
-
384337
 	event = (unsigned int) status >> 16;
384337
 
384337
 	if (event == PTRACE_EVENT_EXEC) {
384337
@@ -2198,29 +2226,86 @@ trace(void)
384337
 
384337
 		if (detach_on_execve && !skip_one_b_execve) {
384337
 			detach(tcp); /* do "-b execve" thingy */
384337
-			return true;
384337
+			break; /* out of collect tcbs */
384337
 		}
384337
 		skip_one_b_execve = 0;
384337
 	}
384337
 
384337
-	/* Set current output file */
384337
-	current_tcp = tcp;
384337
-
384337
 	if (cflag) {
384337
 		tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
384337
 		tcp->stime = ru.ru_stime;
384337
 	}
384337
 
384337
+	/* If we waited and got a stopped task notification,
384337
+	 * subsequent wait may return the same pid again, for example,
384337
+	 * with SIGKILL notification. SIGKILL kills even stopped tasks.
384337
+	 * We must not add it to the list
384337
+	 * (one task can't be inserted twice in the list).
384337
+	 */
384337
+	{
384337
+		struct tcb *f = found_tcps;
384337
+		while (f) {
384337
+			if (f == tcp) {
384337
+				remembered_pid = pid;
384337
+				remembered_status = status;
384337
+				goto process_saved_tcbs;
384337
+			}
384337
+			f = f->next_need_service;
384337
+		}
384337
+	}
384337
+	/* It is important to not invert the order of tasks
384337
+	 * to process. For one, alloc_tcb() above picks newly forked
384337
+	 * threads in some order, processing of them and their parent
384337
+	 * should be in the same order, otherwise bad things happen
384337
+	 * (misinterpreted SIGSTOPs and such).
384337
+	 */
384337
+	tcp->wait_status = status;
384337
+	*nextp = tcp;
384337
+	nextp = &tcp->next_need_service;
384337
+	*nextp = NULL;
384337
+	wnohang = WNOHANG;
384337
+
384337
+	} /* RH 851457 - collect tcbs */
384337
+
384337
+process_saved_tcbs:
384337
+
384337
+	for (tcp = found_tcps;
384337
+	     tcp;
384337
+	     tcp = next) { /* RH 851457 - process tcbs */
384337
+	int status;
384337
+	bool stopped;
384337
+	unsigned int sig;
384337
+	unsigned int event;
384337
+
384337
+	/* If the child exits, the TCP will get dropped and
384337
+	   thus we can't use it to find the next TCP needing
384337
+	   service.  So we save the next TCP needing service
384337
+	   and used the saved value when the loop iterates.  */
384337
+	next = tcp->next_need_service;
384337
+
384337
+	status = tcp->wait_status;
384337
+	pid = tcp->pid;
384337
+
384337
+	event = ((unsigned)status >> 16);
384337
+
384337
+	if (WIFSTOPPED(status))
384337
+		get_regs(pid);
384337
+	else
384337
+		clear_regs();
384337
+
384337
+	/* Set current output file */
384337
+	current_tcp = tcp;
384337
+
384337
 	if (WIFSIGNALED(status)) {
384337
 		print_signalled(tcp, pid, status);
384337
 		droptcb(tcp);
384337
-		return true;
384337
+		continue; /* processing tcbs */
384337
 	}
384337
 
384337
 	if (WIFEXITED(status)) {
384337
 		print_exited(tcp, pid, status);
384337
 		droptcb(tcp);
384337
-		return true;
384337
+		continue; /* processing tcbs */
384337
 	}
384337
 
384337
 	if (!WIFSTOPPED(status)) {
384337
@@ -2230,7 +2315,7 @@ trace(void)
384337
 		 */
384337
 		error_msg("pid %u not stopped!", pid);
384337
 		droptcb(tcp);
384337
-		return true;
384337
+		continue; /* processing tcbs */
384337
 	}
384337
 
384337
 	/* Is this the very first time we see this tracee stopped? */
384337
@@ -2308,7 +2393,7 @@ show_stopsig:
384337
 				exit_code = 1;
384337
 				return false;
384337
 			}
384337
-			return true;
384337
+			continue; /* processing tcbs */
384337
 		}
384337
 		/* We don't have PTRACE_LISTEN support... */
384337
 		goto restart_tracee;
384337
@@ -2334,7 +2419,7 @@ show_stopsig:
384337
 		 * we can let this process to report its death to us
384337
 		 * normally, via WIFEXITED or WIFSIGNALED wait status.
384337
 		 */
384337
-		return true;
384337
+		continue; /* processing tcbs */
384337
 	}
384337
 
384337
 restart_tracee_with_sig_0:
384337
@@ -2347,6 +2432,8 @@ restart_tracee:
384337
 		return false;
384337
 	}
384337
 
384337
+	} /* RH 851457 - process tcbs */
384337
+
384337
 	return true;
384337
 }
384337