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