xref: /netbsd-src/tests/lib/libc/sys/t_ptrace_amd64_wait.h (revision 7d1a2cdc58775695ff03858c1c398c3814fd7c38)
1 /*	$NetBSD: t_ptrace_amd64_wait.h,v 1.12 2020/01/08 17:23:15 mgorny Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016, 2017, 2018, 2019 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 #if defined(__x86_64__)
30 
31 /// ----------------------------------------------------------------------------
32 
33 ATF_TC(x86_64_regs1);
ATF_TC_HEAD(x86_64_regs1,tc)34 ATF_TC_HEAD(x86_64_regs1, tc)
35 {
36 	atf_tc_set_md_var(tc, "descr",
37 	    "Call PT_GETREGS and iterate over General Purpose registers");
38 }
39 
ATF_TC_BODY(x86_64_regs1,tc)40 ATF_TC_BODY(x86_64_regs1, tc)
41 {
42 	const int exitval = 5;
43 	const int sigval = SIGSTOP;
44 	pid_t child, wpid;
45 #if defined(TWAIT_HAVE_STATUS)
46 	int status;
47 #endif
48 	struct reg r;
49 
50 	DPRINTF("Before forking process PID=%d\n", getpid());
51 	SYSCALL_REQUIRE((child = fork()) != -1);
52 	if (child == 0) {
53 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
54 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
55 
56 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
57 		FORKEE_ASSERT(raise(sigval) == 0);
58 
59 		DPRINTF("Before exiting of the child process\n");
60 		_exit(exitval);
61 	}
62 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
63 
64 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
65 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
66 
67 	validate_status_stopped(status, sigval);
68 
69 	DPRINTF("Call GETREGS for the child process\n");
70 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
71 
72 	DPRINTF("RAX=%#" PRIxREGISTER "\n", r.regs[_REG_RAX]);
73 	DPRINTF("RBX=%#" PRIxREGISTER "\n", r.regs[_REG_RBX]);
74 	DPRINTF("RCX=%#" PRIxREGISTER "\n", r.regs[_REG_RCX]);
75 	DPRINTF("RDX=%#" PRIxREGISTER "\n", r.regs[_REG_RDX]);
76 
77 	DPRINTF("RDI=%#" PRIxREGISTER "\n", r.regs[_REG_RDI]);
78 	DPRINTF("RSI=%#" PRIxREGISTER "\n", r.regs[_REG_RSI]);
79 
80 	DPRINTF("GS=%#" PRIxREGISTER "\n", r.regs[_REG_GS]);
81 	DPRINTF("FS=%#" PRIxREGISTER "\n", r.regs[_REG_FS]);
82 	DPRINTF("ES=%#" PRIxREGISTER "\n", r.regs[_REG_ES]);
83 	DPRINTF("DS=%#" PRIxREGISTER "\n", r.regs[_REG_DS]);
84 	DPRINTF("CS=%#" PRIxREGISTER "\n", r.regs[_REG_CS]);
85 	DPRINTF("SS=%#" PRIxREGISTER "\n", r.regs[_REG_SS]);
86 
87 	DPRINTF("RSP=%#" PRIxREGISTER "\n", r.regs[_REG_RSP]);
88 	DPRINTF("RIP=%#" PRIxREGISTER "\n", r.regs[_REG_RIP]);
89 
90 	DPRINTF("RFLAGS=%#" PRIxREGISTER "\n", r.regs[_REG_RFLAGS]);
91 
92 	DPRINTF("R8=%#" PRIxREGISTER "\n", r.regs[_REG_R8]);
93 	DPRINTF("R9=%#" PRIxREGISTER "\n", r.regs[_REG_R9]);
94 	DPRINTF("R10=%#" PRIxREGISTER "\n", r.regs[_REG_R10]);
95 	DPRINTF("R11=%#" PRIxREGISTER "\n", r.regs[_REG_R11]);
96 	DPRINTF("R12=%#" PRIxREGISTER "\n", r.regs[_REG_R12]);
97 	DPRINTF("R13=%#" PRIxREGISTER "\n", r.regs[_REG_R13]);
98 	DPRINTF("R14=%#" PRIxREGISTER "\n", r.regs[_REG_R14]);
99 	DPRINTF("R15=%#" PRIxREGISTER "\n", r.regs[_REG_R15]);
100 
101 	DPRINTF("Before resuming the child process where it left off and "
102 	    "without signal to be sent\n");
103 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
104 
105 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
106 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
107 
108 	validate_status_exited(status, exitval);
109 
110 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
111 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
112 }
113 
114 /// ----------------------------------------------------------------------------
115 
116 
117 #define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64() \
118 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, x86_64_regs1);
119 #else
120 #define ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64()
121 #endif
122