1 /* $NetBSD: freebsd_ptrace.c,v 1.3 2000/12/01 12:28:31 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/errno.h> 48 #include <sys/ptrace.h> 49 #include <sys/uio.h> 50 #include <sys/user.h> 51 #include <sys/mount.h> 52 #include <sys/syscallargs.h> 53 54 #include <machine/reg.h> 55 #include <machine/freebsd_machdep.h> 56 57 #include <compat/freebsd/freebsd_syscallargs.h> 58 #include <compat/common/compat_util.h> 59 #include <compat/freebsd/freebsd_ptrace.h> 60 61 /* 62 * Process debugging system call. 63 */ 64 int 65 freebsd_sys_ptrace(p, v, retval) 66 struct proc *p; 67 void *v; 68 register_t *retval; 69 { 70 struct freebsd_sys_ptrace_args /* { 71 syscallarg(int) req; 72 syscallarg(pid_t) pid; 73 syscallarg(caddr_t) addr; 74 syscallarg(int) data; 75 } */ *uap = v; 76 int error; 77 caddr_t sg; 78 struct { 79 struct reg regs; 80 struct fpreg fpregs; 81 } *nrp; 82 struct sys_ptrace_args npa; 83 struct freebsd_ptrace_reg fr; 84 85 switch (SCARG(uap, req)) { 86 #ifdef PT_STEP 87 case FREEBSD_PT_STEP: 88 SCARG(&npa, req) = PT_STEP; 89 SCARG(&npa, pid) = SCARG(uap, pid); 90 SCARG(&npa, addr) = SCARG(uap, addr); 91 SCARG(&npa, data) = SCARG(uap, data); 92 return sys_ptrace(p, &npa, retval); 93 #endif 94 case FREEBSD_PT_TRACE_ME: 95 case FREEBSD_PT_READ_I: 96 case FREEBSD_PT_READ_D: 97 case FREEBSD_PT_WRITE_I: 98 case FREEBSD_PT_WRITE_D: 99 case FREEBSD_PT_CONTINUE: 100 case FREEBSD_PT_KILL: 101 /* These requests are compatible with NetBSD */ 102 return sys_ptrace(p, uap, retval); 103 104 case FREEBSD_PT_READ_U: 105 case FREEBSD_PT_WRITE_U: 106 sg = stackgap_init(p->p_emul); 107 nrp = stackgap_alloc(&sg, sizeof(*nrp)); 108 #ifdef PT_GETREGS 109 SCARG(&npa, req) = PT_GETREGS; 110 SCARG(&npa, pid) = SCARG(uap, pid); 111 SCARG(&npa, addr) = (caddr_t)&nrp->regs; 112 if ((error = sys_ptrace(p, &npa, retval)) != 0) 113 return error; 114 #endif 115 #ifdef PT_GETFPREGS 116 SCARG(&npa, req) = PT_GETFPREGS; 117 SCARG(&npa, pid) = SCARG(uap, pid); 118 SCARG(&npa, addr) = (caddr_t)&nrp->fpregs; 119 if ((error = sys_ptrace(p, &npa, retval)) != 0) 120 return error; 121 #endif 122 netbsd_to_freebsd_ptrace_regs(&nrp->regs, &nrp->fpregs, &fr); 123 switch (SCARG(uap, req)) { 124 case FREEBSD_PT_READ_U: 125 return freebsd_ptrace_getregs(&fr, SCARG(uap, addr), 126 retval); 127 128 case FREEBSD_PT_WRITE_U: 129 error = freebsd_ptrace_setregs(&fr, 130 SCARG(uap, addr), SCARG(uap, data)); 131 if (error) 132 return error; 133 freebsd_to_netbsd_ptrace_regs(&fr, 134 &nrp->regs, &nrp->fpregs); 135 #ifdef PT_SETREGS 136 SCARG(&npa, req) = PT_SETREGS; 137 SCARG(&npa, pid) = SCARG(uap, pid); 138 SCARG(&npa, addr) = (caddr_t)&nrp->regs; 139 if ((error = sys_ptrace(p, &npa, retval)) != 0) 140 return error; 141 #endif 142 #ifdef PT_SETFPREGS 143 SCARG(&npa, req) = PT_SETFPREGS; 144 SCARG(&npa, pid) = SCARG(uap, pid); 145 SCARG(&npa, addr) = (caddr_t)&nrp->fpregs; 146 if ((error = sys_ptrace(p, &npa, retval)) != 0) 147 return error; 148 #endif 149 return 0; 150 } 151 152 default: /* It was not a legal request. */ 153 return (EINVAL); 154 } 155 156 #ifdef DIAGNOSTIC 157 panic("freebsd_ptrace: impossible"); 158 #endif 159 } 160