Blame SOURCES/strace-rh851457.patch

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