xref: /freebsd/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*	$NetBSD: t_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
35 #include <sys/resource.h>
36 #include <sys/stat.h>
37 #include <sys/sysctl.h>
38 #include <sys/wait.h>
39 #include <machine/reg.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <sched.h>
43 #include <signal.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include <unistd.h>
49 
50 #include <atf-c.h>
51 
52 #include "h_macros.h"
53 
54 #include "../../t_ptrace_wait.h"
55 
56 
57 #if defined(HAVE_GPREGS)
58 ATF_TC(regs1);
59 ATF_TC_HEAD(regs1, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr",
62 	    "Call PT_GETREGS and iterate over General Purpose registers");
63 }
64 
65 ATF_TC_BODY(regs1, tc)
66 {
67 	const int exitval = 5;
68 	const int sigval = SIGSTOP;
69 	pid_t child, wpid;
70 #if defined(TWAIT_HAVE_STATUS)
71 	int status;
72 #endif
73 	struct reg r;
74 
75 	printf("Before forking process PID=%d\n", getpid());
76 	ATF_REQUIRE((child = fork()) != -1);
77 	if (child == 0) {
78 		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
79 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
80 
81 		printf("Before raising %s from child\n", strsignal(sigval));
82 		FORKEE_ASSERT(raise(sigval) == 0);
83 
84 		printf("Before exiting of the child process\n");
85 		_exit(exitval);
86 	}
87 	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
88 
89 	printf("Before calling %s() for the child\n", TWAIT_FNAME);
90 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
91 
92 	validate_status_stopped(status, sigval);
93 
94 	printf("Call GETREGS for the child process\n");
95 	ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
96 
97 	printf("EAX=%#" PRIxREGISTER "\n", r.r_eax);
98 	printf("EBX=%#" PRIxREGISTER "\n", r.r_ebx);
99 	printf("ECX=%#" PRIxREGISTER "\n", r.r_ecx);
100 	printf("EDX=%#" PRIxREGISTER "\n", r.r_edx);
101 
102 	printf("ESP=%#" PRIxREGISTER "\n", r.r_esp);
103 	printf("EBP=%#" PRIxREGISTER "\n", r.r_ebp);
104 
105 	printf("ESI=%#" PRIxREGISTER "\n", r.r_esi);
106 	printf("EDI=%#" PRIxREGISTER "\n", r.r_edi);
107 
108 	printf("EIP=%#" PRIxREGISTER "\n", r.r_eip);
109 
110 	printf("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags);
111 
112 	printf("CS=%#" PRIxREGISTER "\n", r.r_cs);
113 	printf("SS=%#" PRIxREGISTER "\n", r.r_ss);
114 	printf("DS=%#" PRIxREGISTER "\n", r.r_ds);
115 	printf("ES=%#" PRIxREGISTER "\n", r.r_es);
116 	printf("FS=%#" PRIxREGISTER "\n", r.r_fs);
117 	printf("GS=%#" PRIxREGISTER "\n", r.r_gs);
118 
119 	printf("Before resuming the child process where it left off and "
120 	    "without signal to be sent\n");
121 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
122 
123 	printf("Before calling %s() for the child\n", TWAIT_FNAME);
124 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
125 
126 	validate_status_exited(status, exitval);
127 
128 	printf("Before calling %s() for the child\n", TWAIT_FNAME);
129 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
130 }
131 #endif
132 
133 ATF_TP_ADD_TCS(tp)
134 {
135 	setvbuf(stdout, NULL, _IONBF, 0);
136 	setvbuf(stderr, NULL, _IONBF, 0);
137 
138 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
139 
140 	return atf_no_error();
141 }
142