Module Name:    src
Committed By:   kamil
Date:           Thu Feb  7 23:03:33 UTC 2019

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

Log Message:
Refactor GPR and FPR tests in t_ptrace_wait* tests

This change deduplicates the code and puts all the test into shared
function body.

No functional change except ranaming tests regs* to access_regs* and
fpregs* to access_fpregs*.


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/tests/lib/libc/sys/t_ptrace_wait.c

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.71 src/tests/lib/libc/sys/t_ptrace_wait.c:1.72
--- src/tests/lib/libc/sys/t_ptrace_wait.c:1.71	Thu Feb  7 00:24:59 2019
+++ src/tests/lib/libc/sys/t_ptrace_wait.c	Thu Feb  7 23:03:33 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_ptrace_wait.c,v 1.71 2019/02/07 00:24:59 kamil Exp $	*/
+/*	$NetBSD: t_ptrace_wait.c,v 1.72 2019/02/07 23:03:33 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.71 2019/02/07 00:24:59 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.72 2019/02/07 23:03:33 kamil Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -2613,15 +2613,9 @@ BYTES_TRANSFER(bytes_transfer_piod_read_
 
 /// ----------------------------------------------------------------------------
 
-#if defined(HAVE_GPREGS)
-ATF_TC(regs1);
-ATF_TC_HEAD(regs1, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify plain PT_GETREGS call without further steps");
-}
-
-ATF_TC_BODY(regs1, tc)
+#if defined(HAVE_GPREGS) || defined(HAVE_FPGPREGS)
+static void
+access_regs(const char *regset, const char *aux)
 {
 	const int exitval = 5;
 	const int sigval = SIGSTOP;
@@ -2629,173 +2623,23 @@ ATF_TC_BODY(regs1, tc)
 #if defined(TWAIT_HAVE_STATUS)
 	int status;
 #endif
-	struct reg r;
-
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
-
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
-	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
-#endif
-
 #if defined(HAVE_GPREGS)
-ATF_TC(regs2);
-ATF_TC_HEAD(regs2, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify plain PT_GETREGS call and retrieve PC");
-}
-
-ATF_TC_BODY(regs2, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
-#endif
-	struct reg r;
-
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
-
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
-	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
-
-	DPRINTF("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r));
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
+	struct reg gpr;
 #endif
-
-#if defined(HAVE_GPREGS)
-ATF_TC(regs3);
-ATF_TC_HEAD(regs3, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify plain PT_GETREGS call and retrieve SP");
-}
-
-ATF_TC_BODY(regs3, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
+#if defined(HAVE_FPREGS)
+	struct fpreg fpr;
 #endif
-	struct reg r;
-
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+	register_t rgstr;
 
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
-	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
-
-	DPRINTF("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r));
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
+#if !defined(HAVE_GPREGS)
+	if (strcmp(regset, "regs") == 0)
+		atf_tc_fail("Impossible test scenario!");
 #endif
 
-#if defined(HAVE_GPREGS)
-ATF_TC(regs4);
-ATF_TC_HEAD(regs4, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify plain PT_GETREGS call and retrieve INTRV");
-}
-
-ATF_TC_BODY(regs4, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
+#if !defined(HAVE_FPREGS)
+	if (strcmp(regset, "fpregs") == 0)
+		atf_tc_fail("Impossible test scenario!");
 #endif
-	struct reg r;
 
 	DPRINTF("Before forking process PID=%d\n", getpid());
 	SYSCALL_REQUIRE((child = fork()) != -1);
@@ -2816,67 +2660,47 @@ ATF_TC_BODY(regs4, tc)
 
 	validate_status_stopped(status, sigval);
 
-	DPRINTF("Call GETREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
-
-	DPRINTF("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r));
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
-#endif
-
 #if defined(HAVE_GPREGS)
-ATF_TC(regs5);
-ATF_TC_HEAD(regs5, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify PT_GETREGS and PT_SETREGS calls without changing regs");
-}
-
-ATF_TC_BODY(regs5, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
+	if (strcmp(regset, "regs") == 0) {
+		DPRINTF("Call GETREGS for the child process\n");
+		SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
+
+		if (strcmp(aux, "none") == 0) {
+			DPRINTF("Retrieved registers\n");
+		} else if (strcmp(aux, "pc") == 0) {
+			rgstr = PTRACE_REG_PC(&gpr);
+			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
+		} else if (strcmp(aux, "set_pc") == 0) {
+			rgstr = PTRACE_REG_PC(&gpr);
+			PTRACE_REG_SET_PC(&gpr, rgstr);
+		} else if (strcmp(aux, "sp") == 0) {
+			rgstr = PTRACE_REG_SP(&gpr);
+			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
+		} else if (strcmp(aux, "intrv") == 0) {
+			rgstr = PTRACE_REG_INTRV(&gpr);
+			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
+		} else if (strcmp(aux, "setregs") == 0) {
+			DPRINTF("Call SETREGS for the child process\n");
+			SYSCALL_REQUIRE(
+			    ptrace(PT_GETREGS, child, &gpr, 0) != -1);
+		}
+	}
 #endif
-	struct reg r;
-
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
 
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
+#if defined(HAVE_FPREGS)
+	if (strcmp(regset, "fpregs") == 0) {
+		DPRINTF("Call GETFPREGS for the child process\n");
+		SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &fpr, 0) != -1);
+
+		if (strcmp(aux, "getfpregs") == 0) {
+			DPRINTF("Retrieved FP registers\n");
+		} else if (strcmp(aux, "setfpregs") == 0) {
+			DPRINTF("Call SETFPREGS for the child\n");
+			SYSCALL_REQUIRE(
+			    ptrace(PT_SETFPREGS, child, &fpr, 0) != -1);
+		}
 	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
-
-	DPRINTF("Call SETREGS for the child process (without changed regs)\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+#endif
 
 	DPRINTF("Before resuming the child process where it left off and "
 	    "without signal to be sent\n");
@@ -2890,119 +2714,36 @@ ATF_TC_BODY(regs5, tc)
 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
 }
-#endif
 
-#if defined(HAVE_FPREGS)
-ATF_TC(fpregs1);
-ATF_TC_HEAD(fpregs1, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify plain PT_GETFPREGS call without further steps");
+#define ACCESS_REGS(test, regset, aux)					\
+ATF_TC(test);								\
+ATF_TC_HEAD(test, tc)							\
+{									\
+        atf_tc_set_md_var(tc, "descr",					\
+            "Verify " regset " with auxiliary operation: " aux);	\
+}									\
+									\
+ATF_TC_BODY(test, tc)							\
+{									\
+									\
+        access_regs(regset, aux);					\
 }
-
-ATF_TC_BODY(fpregs1, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
 #endif
-	struct fpreg r;
 
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
-
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
-	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETFPREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
+#if defined(HAVE_GPREGS)
+ACCESS_REGS(access_regs1, "regs", "none")
+ACCESS_REGS(access_regs2, "regs", "pc")
+ACCESS_REGS(access_regs3, "regs", "set_pc")
+ACCESS_REGS(access_regs4, "regs", "sp")
+ACCESS_REGS(access_regs5, "regs", "intrv")
+ACCESS_REGS(access_regs6, "regs", "setregs")
 #endif
-
 #if defined(HAVE_FPREGS)
-ATF_TC(fpregs2);
-ATF_TC_HEAD(fpregs2, tc)
-{
-	atf_tc_set_md_var(tc, "descr",
-	    "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
-	    "regs");
-}
-
-ATF_TC_BODY(fpregs2, tc)
-{
-	const int exitval = 5;
-	const int sigval = SIGSTOP;
-	pid_t child, wpid;
-#if defined(TWAIT_HAVE_STATUS)
-	int status;
+ACCESS_REGS(access_fpregs1, "fpregs", "getfpregs")
+ACCESS_REGS(access_fpregs2, "fpregs", "setfpregs")
 #endif
-	struct fpreg r;
-
-	DPRINTF("Before forking process PID=%d\n", getpid());
-	SYSCALL_REQUIRE((child = fork()) != -1);
-	if (child == 0) {
-		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
-		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
-
-		DPRINTF("Before raising %s from child\n", strsignal(sigval));
-		FORKEE_ASSERT(raise(sigval) == 0);
-
-		DPRINTF("Before exiting of the child process\n");
-		_exit(exitval);
-	}
-	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
 
-	validate_status_stopped(status, sigval);
-
-	DPRINTF("Call GETFPREGS for the child process\n");
-	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
-
-	DPRINTF("Call SETFPREGS for the child (without changed regs)\n");
-	SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
-
-	DPRINTF("Before resuming the child process where it left off and "
-	    "without signal to be sent\n");
-	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
-
-	validate_status_exited(status, exitval);
-
-	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
-	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
-}
-#endif
+/// ----------------------------------------------------------------------------
 
 #if defined(PT_STEP)
 static void
@@ -5844,14 +5585,15 @@ ATF_TP_ADD_TCS(tp)
 
 	ATF_TP_ADD_TC(tp, bytes_transfer_piod_read_auxv);
 
-	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
-	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
-	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
-	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
-	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs4);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs5);
+	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs6);
 
-	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
-	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
+	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs1);
+	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs2);
 
 	ATF_TP_ADD_TC_PT_STEP(tp, step1);
 	ATF_TP_ADD_TC_PT_STEP(tp, step2);

Reply via email to