Module Name:    src
Committed By:   kamil
Date:           Tue Nov 12 18:18:04 UTC 2019

Modified Files:
        src/tests/lib/libc/sys: t_ptrace_wait.c t_ptrace_wait.h

Log Message:
Rework thread_concurrent_signals and trace_thread_lwpcreate_and_exit

Change the code to remove the LWP id assumptions that broke after
src/sys/kern/kern_lwp.c r. 1.206.

Original code by <mgorny>, tested and tweaked by myself.


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 src/tests/lib/libc/sys/t_ptrace_wait.c
cvs rdiff -u -r1.17 -r1.18 src/tests/lib/libc/sys/t_ptrace_wait.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/sys/t_ptrace_wait.c
diff -u src/tests/lib/libc/sys/t_ptrace_wait.c:1.140 src/tests/lib/libc/sys/t_ptrace_wait.c:1.141
--- src/tests/lib/libc/sys/t_ptrace_wait.c:1.140	Mon Oct 21 18:36:08 2019
+++ src/tests/lib/libc/sys/t_ptrace_wait.c	Tue Nov 12 18:18:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_ptrace_wait.c,v 1.140 2019/10/21 18:36:08 kamil Exp $	*/
+/*	$NetBSD: t_ptrace_wait.c,v 1.141 2019/11/12 18:18:04 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.140 2019/10/21 18:36:08 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.141 2019/11/12 18:18:04 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -5478,7 +5478,7 @@ trace_threads(bool trace_create, bool tr
 	lwpid_t lid;
 
 	/* Track created and exited threads */
-	bool traced_lwps[__arraycount(t)];
+	struct lwp_event_count traced_lwps[__arraycount(t)] = {{0, 0}};
 
 	DPRINTF("Before forking process PID=%d\n", getpid());
 	SYSCALL_REQUIRE((child = fork()) != -1);
@@ -5541,8 +5541,6 @@ trace_threads(bool trace_create, bool tr
 	    "without signal to be sent\n");
 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 
-	memset(traced_lwps, 0, sizeof(traced_lwps));
-
 	for (n = 0; n < (trace_create ? __arraycount(t) : 0); n++) {
 		DPRINTF("Before calling %s() for the child - expected stopped "
 		    "SIGTRAP\n", TWAIT_FNAME);
@@ -5574,7 +5572,7 @@ trace_threads(bool trace_create, bool tr
 		lid = state.pe_lwp;
 		DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
 
-		traced_lwps[lid - 1] = true;
+		*FIND_EVENT_COUNT(traced_lwps, lid) += 1;
 
 		DPRINTF("Before resuming the child process where it left off "
 		    "and without signal to be sent\n");
@@ -5613,8 +5611,9 @@ trace_threads(bool trace_create, bool tr
 		DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
 
 		if (trace_create) {
-			ATF_REQUIRE(traced_lwps[lid - 1] == true);
-			traced_lwps[lid - 1] = false;
+			int *count = FIND_EVENT_COUNT(traced_lwps, lid);
+			ATF_REQUIRE_EQ(*count, 1);
+			*count = 0;
 		}
 
 		DPRINTF("Before resuming the child process where it left off "
@@ -7738,7 +7737,8 @@ ATF_TC_BODY(thread_concurrent_signals, t
 	const int sigval = SIGSTOP;
 	pid_t child, wpid;
 	int status;
-	int signal_counts[THREAD_CONCURRENT_SIGNALS_NUM] = {0};
+	struct lwp_event_count signal_counts[THREAD_CONCURRENT_SIGNALS_NUM]
+	    = {{0, 0}};
 	unsigned int i;
 
 	DPRINTF("Before forking process PID=%d\n", getpid());
@@ -7816,21 +7816,16 @@ ATF_TC_BODY(thread_concurrent_signals, t
 		    "lwp=%d, expected %d, got %d", info.psi_lwpid,
 		    expected_sig, WSTOPSIG(status));
 
-		/* We assume that LWPs will be given successive numbers starting
-		 * from 2.
-		 */
-		ATF_REQUIRE(info.psi_lwpid >= 2);
-		ATF_REQUIRE((unsigned int)info.psi_lwpid <
-		    __arraycount(signal_counts)+2);
-		signal_counts[info.psi_lwpid-2]++;
+		*FIND_EVENT_COUNT(signal_counts, info.psi_lwpid) += 1;
 
 		DPRINTF("Before resuming the child process\n");
 		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
 	}
 
 	for (i = 0; i < __arraycount(signal_counts); i++)
-		ATF_CHECK_EQ_MSG(signal_counts[i], 1, "signal_counts[%d]=%d",
-		    i, signal_counts[i]);
+		ATF_CHECK_EQ_MSG(signal_counts[i].lec_count, 1,
+		    "signal_counts[%d].lec_count=%d; lec_lwp=%d",
+		    i, signal_counts[i].lec_count, signal_counts[i].lec_lwp);
 
 	validate_status_exited(status, exitval);
 }

Index: src/tests/lib/libc/sys/t_ptrace_wait.h
diff -u src/tests/lib/libc/sys/t_ptrace_wait.h:1.17 src/tests/lib/libc/sys/t_ptrace_wait.h:1.18
--- src/tests/lib/libc/sys/t_ptrace_wait.h:1.17	Sat May 25 03:22:53 2019
+++ src/tests/lib/libc/sys/t_ptrace_wait.h	Tue Nov 12 18:18:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_ptrace_wait.h,v 1.17 2019/05/25 03:22:53 kamil Exp $	*/
+/*	$NetBSD: t_ptrace_wait.h,v 1.18 2019/11/12 18:18:04 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
@@ -674,6 +674,30 @@ trigger_bus(void)
 	*p = 'a';
 }
 
+struct lwp_event_count {
+	lwpid_t lec_lwp;
+	int lec_count;
+};
+
+static int *
+find_event_count(struct lwp_event_count list[], lwpid_t lwp, size_t max_lwps)
+{
+	size_t i;
+
+	for (i = 0; i < max_lwps; i++) {
+		if (list[i].lec_lwp == 0)
+			list[i].lec_lwp = lwp;
+		if (list[i].lec_lwp == lwp)
+			return &list[i].lec_count;
+	}
+
+	atf_tc_fail("More LWPs reported than expected");
+}
+
+#define FIND_EVENT_COUNT(list, lwp)			\
+	find_event_count(list, lwp, __arraycount(list))
+
+
 #if defined(TWAIT_HAVE_PID)
 #define ATF_TP_ADD_TC_HAVE_PID(a,b)	ATF_TP_ADD_TC(a,b)
 #else

Reply via email to