Blame SOURCES/0004-strace.c-introduce-struct-tcb_wait_data.patch

904f19
From acdd2e8d3d1551b41170a24951addb80b0b0d423 Mon Sep 17 00:00:00 2001
904f19
From: "Dmitry V. Levin" <ldv@altlinux.org>
904f19
Date: Tue, 14 Aug 2018 13:43:34 +0000
904f19
Subject: [PATCH 04/27] strace.c: introduce struct tcb_wait_data
904f19
904f19
Introduce a new structure to pass information between next_event(),
904f19
restart_delayed_tcb(), and dispatch_event().
904f19
904f19
This is going to be used by a subsequent change of next_event().
904f19
904f19
* strace.c (struct tcb_wait_data): New type.
904f19
(next_event): Remove parameters, return a pointer
904f19
to const struct tcb_wait_data.  Return NULL instead of TE_BREAK.
904f19
(dispatch_event): Replace all parameters with a pointer
904f19
to const struct tcb_wait_data, obtain the trace event, siginfo,
904f19
and status from its fields.
904f19
(restart_delayed_tcb): Add local struct tcb_wait_data variable
904f19
with te field set to TE_RESTART, pass it to dispatch_event().
904f19
(main): Remove status and si variables, update next_event()
904f19
and dispatch_event() invocations.
904f19
904f19
Co-Authored-by: Eugene Syromyatnikov <evgsyr@gmail.com>
904f19
---
904f19
 strace.c | 107 ++++++++++++++++++++++++++++++++++++++++-----------------------
904f19
 1 file changed, 69 insertions(+), 38 deletions(-)
904f19
904f19
diff --git a/strace.c b/strace.c
904f19
index cd04b98..6d70d20 100644
904f19
--- a/strace.c
904f19
+++ b/strace.c
904f19
@@ -158,6 +158,12 @@ static bool open_append;
904f19
 struct tcb *printing_tcp;
904f19
 static struct tcb *current_tcp;
904f19
 
904f19
+struct tcb_wait_data {
904f19
+	enum trace_event te; /**< Event passed to dispatch_event() */
904f19
+	int status;          /**< status, returned by wait4() */
904f19
+	siginfo_t si;        /**< siginfo, returned by PTRACE_GETSIGINFO */
904f19
+};
904f19
+
904f19
 static struct tcb **tcbtab;
904f19
 static unsigned int nprocs;
904f19
 static size_t tcbtabsize;
904f19
@@ -2226,16 +2232,19 @@ print_event_exit(struct tcb *tcp)
904f19
 	line_ended();
904f19
 }
904f19
 
904f19
-static enum trace_event
904f19
-next_event(int *pstatus, siginfo_t *si)
904f19
+static const struct tcb_wait_data *
904f19
+next_event(void)
904f19
 {
904f19
+	static struct tcb_wait_data wait_data;
904f19
+
904f19
 	int pid;
904f19
 	int status;
904f19
 	struct tcb *tcp;
904f19
+	struct tcb_wait_data *wd = &wait_data;
904f19
 	struct rusage ru;
904f19
 
904f19
 	if (interrupted)
904f19
-		return TE_BREAK;
904f19
+		return NULL;
904f19
 
904f19
 	/*
904f19
 	 * Used to exit simply when nprocs hits zero, but in this testcase:
904f19
@@ -2255,7 +2264,7 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		 * on exit. Oh well...
904f19
 		 */
904f19
 		if (nprocs == 0)
904f19
-			return TE_BREAK;
904f19
+			return NULL;
904f19
 	}
904f19
 
904f19
 	const bool unblock_delay_timer = is_delay_timer_armed();
904f19
@@ -2278,7 +2287,7 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 	 * then the system call will be interrupted and
904f19
 	 * the expiration will be handled by the signal handler.
904f19
 	 */
904f19
-	pid = wait4(-1, pstatus, __WALL, (cflag ? &ru : NULL));
904f19
+	pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
904f19
 	const int wait_errno = errno;
904f19
 
904f19
 	/*
904f19
@@ -2292,14 +2301,16 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		sigprocmask(SIG_BLOCK, &timer_set, NULL);
904f19
 
904f19
 		if (restart_failed)
904f19
-			return TE_BREAK;
904f19
+			return NULL;
904f19
 	}
904f19
 
904f19
 	if (pid < 0) {
904f19
-		if (wait_errno == EINTR)
904f19
-			return TE_NEXT;
904f19
+		if (wait_errno == EINTR) {
904f19
+			wd->te = TE_NEXT;
904f19
+			return wd;
904f19
+		}
904f19
 		if (nprocs == 0 && wait_errno == ECHILD)
904f19
-			return TE_BREAK;
904f19
+			return NULL;
904f19
 		/*
904f19
 		 * If nprocs > 0, ECHILD is not expected,
904f19
 		 * treat it as any other error here:
904f19
@@ -2308,12 +2319,13 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		perror_msg_and_die("wait4(__WALL)");
904f19
 	}
904f19
 
904f19
-	status = *pstatus;
904f19
+	wd->status = status;
904f19
 
904f19
 	if (pid == popen_pid) {
904f19
 		if (!WIFSTOPPED(status))
904f19
 			popen_pid = 0;
904f19
-		return TE_NEXT;
904f19
+		wd->te = TE_NEXT;
904f19
+		return wd;
904f19
 	}
904f19
 
904f19
 	if (debug_flag)
904f19
@@ -2324,8 +2336,10 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 
904f19
 	if (!tcp) {
904f19
 		tcp = maybe_allocate_tcb(pid, status);
904f19
-		if (!tcp)
904f19
-			return TE_NEXT;
904f19
+		if (!tcp) {
904f19
+			wd->te = TE_NEXT;
904f19
+			return wd;
904f19
+		}
904f19
 	}
904f19
 
904f19
 	clear_regs(tcp);
904f19
@@ -2342,11 +2356,15 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		tcp->stime = stime;
904f19
 	}
904f19
 
904f19
-	if (WIFSIGNALED(status))
904f19
-		return TE_SIGNALLED;
904f19
+	if (WIFSIGNALED(status)) {
904f19
+		wd->te = TE_SIGNALLED;
904f19
+		return wd;
904f19
+	}
904f19
 
904f19
-	if (WIFEXITED(status))
904f19
-		return TE_EXITED;
904f19
+	if (WIFEXITED(status)) {
904f19
+		wd->te = TE_EXITED;
904f19
+		return wd;
904f19
+	}
904f19
 
904f19
 	/*
904f19
 	 * As WCONTINUED flag has not been specified to wait4,
904f19
@@ -2373,19 +2391,19 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
904f19
 			debug_func_msg("ignored SIGSTOP on pid %d", tcp->pid);
904f19
 			tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
904f19
-			return TE_RESTART;
904f19
+			wd->te = TE_RESTART;
904f19
 		} else if (sig == syscall_trap_sig) {
904f19
-			return TE_SYSCALL_STOP;
904f19
+			wd->te = TE_SYSCALL_STOP;
904f19
 		} else {
904f19
-			*si = (siginfo_t) {};
904f19
+			memset(&wd->si, 0, sizeof(wd->si));
904f19
 			/*
904f19
 			 * True if tracee is stopped by signal
904f19
 			 * (as opposed to "tracee received signal").
904f19
 			 * TODO: shouldn't we check for errno == EINVAL too?
904f19
 			 * We can get ESRCH instead, you know...
904f19
 			 */
904f19
-			bool stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, si) < 0;
904f19
-			return stopped ? TE_GROUP_STOP : TE_SIGNAL_DELIVERY_STOP;
904f19
+			bool stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, &wd->si) < 0;
904f19
+			wd->te = stopped ? TE_GROUP_STOP : TE_SIGNAL_DELIVERY_STOP;
904f19
 		}
904f19
 		break;
904f19
 	case PTRACE_EVENT_STOP:
904f19
@@ -2398,16 +2416,23 @@ next_event(int *pstatus, siginfo_t *si)
904f19
 		case SIGTSTP:
904f19
 		case SIGTTIN:
904f19
 		case SIGTTOU:
904f19
-			return TE_GROUP_STOP;
904f19
+			wd->te = TE_GROUP_STOP;
904f19
+			break;
904f19
+		default:
904f19
+			wd->te = TE_RESTART;
904f19
 		}
904f19
-		return TE_RESTART;
904f19
+		break;
904f19
 	case PTRACE_EVENT_EXEC:
904f19
-		return TE_STOP_BEFORE_EXECVE;
904f19
+		wd->te = TE_STOP_BEFORE_EXECVE;
904f19
+		break;
904f19
 	case PTRACE_EVENT_EXIT:
904f19
-		return TE_STOP_BEFORE_EXIT;
904f19
+		wd->te = TE_STOP_BEFORE_EXIT;
904f19
+		break;
904f19
 	default:
904f19
-		return TE_RESTART;
904f19
+		wd->te = TE_RESTART;
904f19
 	}
904f19
+
904f19
+	return wd;
904f19
 }
904f19
 
904f19
 static int
904f19
@@ -2436,12 +2461,18 @@ trace_syscall(struct tcb *tcp, unsigned int *sig)
904f19
 
904f19
 /* Returns true iff the main trace loop has to continue. */
904f19
 static bool
904f19
-dispatch_event(enum trace_event ret, int *pstatus, siginfo_t *si)
904f19
+dispatch_event(const struct tcb_wait_data *wd)
904f19
 {
904f19
 	unsigned int restart_op = PTRACE_SYSCALL;
904f19
 	unsigned int restart_sig = 0;
904f19
+	enum trace_event te = wd ? wd->te : TE_BREAK;
904f19
+	/*
904f19
+	 * Copy wd->status to a non-const variable to workaround glibc bugs
904f19
+	 * around union wait fixed by glibc commit glibc-2.24~391
904f19
+	 */
904f19
+	int status = wd ? wd->status : 0;
904f19
 
904f19
-	switch (ret) {
904f19
+	switch (te) {
904f19
 	case TE_BREAK:
904f19
 		return false;
904f19
 
904f19
@@ -2469,17 +2500,17 @@ dispatch_event(enum trace_event ret, int *pstatus, siginfo_t *si)
904f19
 		break;
904f19
 
904f19
 	case TE_SIGNAL_DELIVERY_STOP:
904f19
-		restart_sig = WSTOPSIG(*pstatus);
904f19
-		print_stopped(current_tcp, si, restart_sig);
904f19
+		restart_sig = WSTOPSIG(status);
904f19
+		print_stopped(current_tcp, &wd->si, restart_sig);
904f19
 		break;
904f19
 
904f19
 	case TE_SIGNALLED:
904f19
-		print_signalled(current_tcp, current_tcp->pid, *pstatus);
904f19
+		print_signalled(current_tcp, current_tcp->pid, status);
904f19
 		droptcb(current_tcp);
904f19
 		return true;
904f19
 
904f19
 	case TE_GROUP_STOP:
904f19
-		restart_sig = WSTOPSIG(*pstatus);
904f19
+		restart_sig = WSTOPSIG(status);
904f19
 		print_stopped(current_tcp, NULL, restart_sig);
904f19
 		if (use_seize) {
904f19
 			/*
904f19
@@ -2494,7 +2525,7 @@ dispatch_event(enum trace_event ret, int *pstatus, siginfo_t *si)
904f19
 		break;
904f19
 
904f19
 	case TE_EXITED:
904f19
-		print_exited(current_tcp, current_tcp->pid, *pstatus);
904f19
+		print_exited(current_tcp, current_tcp->pid, status);
904f19
 		droptcb(current_tcp);
904f19
 		return true;
904f19
 
904f19
@@ -2577,13 +2608,15 @@ dispatch_event(enum trace_event ret, int *pstatus, siginfo_t *si)
904f19
 static bool
904f19
 restart_delayed_tcb(struct tcb *const tcp)
904f19
 {
904f19
+	const struct tcb_wait_data wd = { .te = TE_RESTART };
904f19
+
904f19
 	debug_func_msg("pid %d", tcp->pid);
904f19
 
904f19
 	tcp->flags &= ~TCB_DELAYED;
904f19
 
904f19
 	struct tcb *const prev_tcp = current_tcp;
904f19
 	current_tcp = tcp;
904f19
-	bool ret = dispatch_event(TE_RESTART, NULL, NULL);
904f19
+	bool ret = dispatch_event(&wd;;
904f19
 	current_tcp = prev_tcp;
904f19
 
904f19
 	return ret;
904f19
@@ -2694,9 +2727,7 @@ main(int argc, char *argv[])
904f19
 
904f19
 	exit_code = !nprocs;
904f19
 
904f19
-	int status;
904f19
-	siginfo_t si;
904f19
-	while (dispatch_event(next_event(&status, &si), &status, &si))
904f19
+	while (dispatch_event(next_event()))
904f19
 		;
904f19
 	terminate();
904f19
 }
904f19
-- 
904f19
2.1.4
904f19