1312123Sngie/* $NetBSD: t_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */ 2312123Sngie 3312123Sngie/*- 4312123Sngie * Copyright (c) 2016 The NetBSD Foundation, Inc. 5312123Sngie * All rights reserved. 6312123Sngie * 7312123Sngie * Redistribution and use in source and binary forms, with or without 8312123Sngie * modification, are permitted provided that the following conditions 9312123Sngie * are met: 10312123Sngie * 1. Redistributions of source code must retain the above copyright 11312123Sngie * notice, this list of conditions and the following disclaimer. 12312123Sngie * 2. Redistributions in binary form must reproduce the above copyright 13312123Sngie * notice, this list of conditions and the following disclaimer in the 14312123Sngie * documentation and/or other materials provided with the distribution. 15312123Sngie * 16312123Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17312123Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18312123Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19312123Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20312123Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21312123Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22312123Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23312123Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24312123Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25312123Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26312123Sngie * POSSIBILITY OF SUCH DAMAGE. 27312123Sngie */ 28312123Sngie 29312123Sngie#include <sys/cdefs.h> 30312123Sngie__RCSID("$NetBSD: t_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $"); 31312123Sngie 32312123Sngie#include <sys/param.h> 33312123Sngie#include <sys/types.h> 34312123Sngie#include <sys/ptrace.h> 35312123Sngie#include <sys/resource.h> 36312123Sngie#include <sys/stat.h> 37312123Sngie#include <sys/sysctl.h> 38312123Sngie#include <sys/wait.h> 39312123Sngie#include <machine/reg.h> 40312123Sngie#include <err.h> 41312123Sngie#include <errno.h> 42312123Sngie#include <sched.h> 43312123Sngie#include <signal.h> 44312123Sngie#include <stdint.h> 45312123Sngie#include <stdio.h> 46312123Sngie#include <stdlib.h> 47312123Sngie#include <strings.h> 48312123Sngie#include <unistd.h> 49312123Sngie 50312123Sngie#include <atf-c.h> 51312123Sngie 52312123Sngie#include "h_macros.h" 53312123Sngie 54312123Sngie#include "../../t_ptrace_wait.h" 55312123Sngie 56312123Sngie 57312123Sngie#if defined(HAVE_GPREGS) 58312123SngieATF_TC(regs1); 59312123SngieATF_TC_HEAD(regs1, tc) 60312123Sngie{ 61312123Sngie atf_tc_set_md_var(tc, "descr", 62312123Sngie "Call PT_GETREGS and iterate over General Purpose registers"); 63312123Sngie} 64312123Sngie 65312123SngieATF_TC_BODY(regs1, tc) 66312123Sngie{ 67312123Sngie const int exitval = 5; 68312123Sngie const int sigval = SIGSTOP; 69312123Sngie pid_t child, wpid; 70312123Sngie#if defined(TWAIT_HAVE_STATUS) 71312123Sngie int status; 72312123Sngie#endif 73312123Sngie struct reg r; 74312123Sngie 75312123Sngie printf("Before forking process PID=%d\n", getpid()); 76312123Sngie ATF_REQUIRE((child = fork()) != -1); 77312123Sngie if (child == 0) { 78312123Sngie printf("Before calling PT_TRACE_ME from child %d\n", getpid()); 79312123Sngie FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 80312123Sngie 81312123Sngie printf("Before raising %s from child\n", strsignal(sigval)); 82312123Sngie FORKEE_ASSERT(raise(sigval) == 0); 83312123Sngie 84312123Sngie printf("Before exiting of the child process\n"); 85312123Sngie _exit(exitval); 86312123Sngie } 87312123Sngie printf("Parent process PID=%d, child's PID=%d\n", getpid(), child); 88312123Sngie 89312123Sngie printf("Before calling %s() for the child\n", TWAIT_FNAME); 90312123Sngie TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 91312123Sngie 92312123Sngie validate_status_stopped(status, sigval); 93312123Sngie 94312123Sngie printf("Call GETREGS for the child process\n"); 95312123Sngie ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1); 96312123Sngie 97312123Sngie printf("EAX=%#" PRIxREGISTER "\n", r.r_eax); 98312123Sngie printf("EBX=%#" PRIxREGISTER "\n", r.r_ebx); 99312123Sngie printf("ECX=%#" PRIxREGISTER "\n", r.r_ecx); 100312123Sngie printf("EDX=%#" PRIxREGISTER "\n", r.r_edx); 101312123Sngie 102312123Sngie printf("ESP=%#" PRIxREGISTER "\n", r.r_esp); 103312123Sngie printf("EBP=%#" PRIxREGISTER "\n", r.r_ebp); 104312123Sngie 105312123Sngie printf("ESI=%#" PRIxREGISTER "\n", r.r_esi); 106312123Sngie printf("EDI=%#" PRIxREGISTER "\n", r.r_edi); 107312123Sngie 108312123Sngie printf("EIP=%#" PRIxREGISTER "\n", r.r_eip); 109312123Sngie 110312123Sngie printf("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags); 111312123Sngie 112312123Sngie printf("CS=%#" PRIxREGISTER "\n", r.r_cs); 113312123Sngie printf("SS=%#" PRIxREGISTER "\n", r.r_ss); 114312123Sngie printf("DS=%#" PRIxREGISTER "\n", r.r_ds); 115312123Sngie printf("ES=%#" PRIxREGISTER "\n", r.r_es); 116312123Sngie printf("FS=%#" PRIxREGISTER "\n", r.r_fs); 117312123Sngie printf("GS=%#" PRIxREGISTER "\n", r.r_gs); 118312123Sngie 119312123Sngie printf("Before resuming the child process where it left off and " 120312123Sngie "without signal to be sent\n"); 121312123Sngie ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1); 122312123Sngie 123312123Sngie printf("Before calling %s() for the child\n", TWAIT_FNAME); 124312123Sngie TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child); 125312123Sngie 126312123Sngie validate_status_exited(status, exitval); 127312123Sngie 128312123Sngie printf("Before calling %s() for the child\n", TWAIT_FNAME); 129312123Sngie TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0)); 130312123Sngie} 131312123Sngie#endif 132312123Sngie 133312123SngieATF_TP_ADD_TCS(tp) 134312123Sngie{ 135312123Sngie setvbuf(stdout, NULL, _IONBF, 0); 136312123Sngie setvbuf(stderr, NULL, _IONBF, 0); 137312123Sngie 138312123Sngie ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1); 139312123Sngie 140312123Sngie return atf_no_error(); 141312123Sngie} 142