1 /* $NetBSD: freebsd_ptrace.c,v 1.18 2008/11/12 12:36:10 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 37 */ 38 39 /*- 40 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: freebsd_ptrace.c,v 1.18 2008/11/12 12:36:10 ad Exp $"); 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/proc.h> 79 #include <sys/errno.h> 80 #include <sys/ptrace.h> 81 #include <sys/uio.h> 82 #include <sys/user.h> 83 #include <sys/mount.h> 84 #include <sys/syscall.h> 85 #include <sys/syscallargs.h> 86 87 #include <compat/sys/signal.h> 88 #include <compat/sys/signalvar.h> 89 90 #include <machine/reg.h> 91 #include <machine/freebsd_machdep.h> 92 93 #include <compat/common/compat_util.h> 94 #include <compat/freebsd/freebsd_syscallargs.h> 95 #include <compat/freebsd/freebsd_ptrace.h> 96 97 /* 98 * Process debugging system call. 99 */ 100 int 101 freebsd_sys_ptrace(struct lwp *l, const struct freebsd_sys_ptrace_args *uap, register_t *retval) 102 { 103 /* { 104 syscallarg(int) req; 105 syscallarg(pid_t) pid; 106 syscallarg(void *) addr; 107 syscallarg(int) data; 108 } */ 109 struct sys_ptrace_args npa; 110 sy_call_t *fn = sysent[SYS_ptrace].sy_call; 111 112 switch (SCARG(uap, req)) { 113 #ifdef PT_STEP 114 case FREEBSD_PT_STEP: 115 SCARG(&npa, req) = PT_STEP; 116 SCARG(&npa, pid) = SCARG(uap, pid); 117 SCARG(&npa, addr) = SCARG(uap, addr); 118 SCARG(&npa, data) = SCARG(uap, data); 119 return (*fn)(l, &npa, retval); 120 #endif 121 case FREEBSD_PT_TRACE_ME: 122 case FREEBSD_PT_READ_I: 123 case FREEBSD_PT_READ_D: 124 case FREEBSD_PT_WRITE_I: 125 case FREEBSD_PT_WRITE_D: 126 case FREEBSD_PT_CONTINUE: 127 case FREEBSD_PT_KILL: 128 /* These requests are compatible with NetBSD */ 129 return (*fn)(l, (const void *)uap, retval); 130 131 #if 0 132 /* 133 * XXX: I've commented out this code, it is broken on too many fronts to fix. 134 * 1) It is doing an unlocked read-modify-write cycle on process that 135 * I assume might be running! 136 * and in code that might sleep (due to a pagefault), never mind 137 * what happens on an SMP system 138 * 2) It accesses data in userspace without using copyin/out. 139 * 3) It all looks like a nasty hack that isn't likely to work. 140 * 4) It uses the stackgap. 141 * dsl June 2007 142 */ 143 case FREEBSD_PT_READ_U: 144 case FREEBSD_PT_WRITE_U: 145 { 146 int error; 147 struct { 148 struct reg regs; 149 struct fpreg fpregs; 150 } *nrp; 151 struct freebsd_ptrace_reg fr; 152 sg = stackgap_init(p, 0); 153 nrp = stackgap_alloc(p, &sg, sizeof(*nrp)); 154 #ifdef PT_GETREGS 155 SCARG(&npa, req) = PT_GETREGS; 156 SCARG(&npa, pid) = SCARG(uap, pid); 157 SCARG(&npa, addr) = (void *)&nrp->regs; 158 if ((error = (*fn)(l, &npa, retval)) != 0) 159 return error; 160 #endif 161 #ifdef PT_GETFPREGS 162 SCARG(&npa, req) = PT_GETFPREGS; 163 SCARG(&npa, pid) = SCARG(uap, pid); 164 SCARG(&npa, addr) = (void *)&nrp->fpregs; 165 if ((error = (*fn)(l, &npa, retval)) != 0) 166 return error; 167 #endif 168 netbsd_to_freebsd_ptrace_regs(&nrp->regs, &nrp->fpregs, &fr); 169 switch (SCARG(uap, req)) { 170 case FREEBSD_PT_READ_U: 171 return freebsd_ptrace_getregs(&fr, SCARG(uap, addr), 172 retval); 173 174 case FREEBSD_PT_WRITE_U: 175 error = freebsd_ptrace_setregs(&fr, 176 SCARG(uap, addr), SCARG(uap, data)); 177 if (error) 178 return error; 179 freebsd_to_netbsd_ptrace_regs(&fr, 180 &nrp->regs, &nrp->fpregs); 181 #ifdef PT_SETREGS 182 SCARG(&npa, req) = PT_SETREGS; 183 SCARG(&npa, pid) = SCARG(uap, pid); 184 SCARG(&npa, addr) = (void *)&nrp->regs; 185 if ((error = (*fn)(l, &npa, retval)) != 0) 186 return error; 187 #endif 188 #ifdef PT_SETFPREGS 189 SCARG(&npa, req) = PT_SETFPREGS; 190 SCARG(&npa, pid) = SCARG(uap, pid); 191 SCARG(&npa, addr) = (void *)&nrp->fpregs; 192 if ((error = (*fn)(l, &npa, retval)) != 0) 193 return error; 194 #endif 195 return 0; 196 } 197 } 198 #endif 199 200 default: /* It was not a legal request. */ 201 return (EINVAL); 202 } 203 204 #ifdef DIAGNOSTIC 205 panic("freebsd_ptrace: impossible"); 206 #endif 207 } 208