Home | History | Annotate | Line # | Download | only in freebsd
freebsd_ptrace.c revision 1.11
      1 /*	$NetBSD: freebsd_ptrace.c,v 1.11 2006/08/31 15:01:56 he 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.11 2006/08/31 15:01:56 he Exp $");
     75 
     76 #if defined(_KERNEL_OPT)
     77 #include "opt_ptrace.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/proc.h>
     83 #include <sys/errno.h>
     84 #include <sys/ptrace.h>
     85 #include <sys/uio.h>
     86 #include <sys/user.h>
     87 #include <sys/mount.h>
     88 #include <sys/sa.h>
     89 #include <sys/syscallargs.h>
     90 
     91 #include <compat/sys/signal.h>
     92 #include <compat/sys/signalvar.h>
     93 
     94 #include <machine/reg.h>
     95 #include <machine/freebsd_machdep.h>
     96 
     97 #include <compat/common/compat_util.h>
     98 #include <compat/freebsd/freebsd_syscallargs.h>
     99 #include <compat/freebsd/freebsd_ptrace.h>
    100 
    101 /*
    102  * Process debugging system call.
    103  */
    104 int
    105 freebsd_sys_ptrace(l, v, retval)
    106 	struct lwp *l;
    107 	void *v;
    108 	register_t *retval;
    109 {
    110 	struct freebsd_sys_ptrace_args /* {
    111 		syscallarg(int) req;
    112 		syscallarg(pid_t) pid;
    113 		syscallarg(caddr_t) addr;
    114 		syscallarg(int) data;
    115 	} */ *uap = v;
    116 	struct proc *p = l->l_proc;
    117 	int error;
    118 	caddr_t sg;
    119 	struct {
    120 		struct reg regs;
    121 		struct fpreg fpregs;
    122 	} *nrp;
    123 	struct sys_ptrace_args npa;
    124 	struct freebsd_ptrace_reg fr;
    125 
    126 	switch (SCARG(uap, req)) {
    127 #ifdef PT_STEP
    128 	case FREEBSD_PT_STEP:
    129 		SCARG(&npa, req) = PT_STEP;
    130 		SCARG(&npa, pid) = SCARG(uap, pid);
    131 		SCARG(&npa, addr) = SCARG(uap, addr);
    132 		SCARG(&npa, data) = SCARG(uap, data);
    133 		return sys_ptrace(l, &npa, retval);
    134 #endif
    135 	case FREEBSD_PT_TRACE_ME:
    136 	case FREEBSD_PT_READ_I:
    137 	case FREEBSD_PT_READ_D:
    138 	case FREEBSD_PT_WRITE_I:
    139 	case FREEBSD_PT_WRITE_D:
    140 	case FREEBSD_PT_CONTINUE:
    141 	case FREEBSD_PT_KILL:
    142 		/* These requests are compatible with NetBSD */
    143 		return sys_ptrace(l, uap, retval);
    144 
    145 	case FREEBSD_PT_READ_U:
    146 	case FREEBSD_PT_WRITE_U:
    147 		sg = stackgap_init(p, 0);
    148 		nrp = stackgap_alloc(p, &sg, sizeof(*nrp));
    149 #ifdef PT_GETREGS
    150 		SCARG(&npa, req) = PT_GETREGS;
    151 		SCARG(&npa, pid) = SCARG(uap, pid);
    152 		SCARG(&npa, addr) = (caddr_t)&nrp->regs;
    153 		if ((error = sys_ptrace(l, &npa, retval)) != 0)
    154 			return error;
    155 #endif
    156 #ifdef PT_GETFPREGS
    157 		SCARG(&npa, req) = PT_GETFPREGS;
    158 		SCARG(&npa, pid) = SCARG(uap, pid);
    159 		SCARG(&npa, addr) = (caddr_t)&nrp->fpregs;
    160 		if ((error = sys_ptrace(l, &npa, retval)) != 0)
    161 			return error;
    162 #endif
    163 		netbsd_to_freebsd_ptrace_regs(&nrp->regs, &nrp->fpregs, &fr);
    164 		switch (SCARG(uap, req)) {
    165 		case FREEBSD_PT_READ_U:
    166 			return freebsd_ptrace_getregs(&fr, SCARG(uap, addr),
    167 						      retval);
    168 
    169 		case FREEBSD_PT_WRITE_U:
    170 			error = freebsd_ptrace_setregs(&fr,
    171 			    SCARG(uap, addr), SCARG(uap, data));
    172 			if (error)
    173 			    return error;
    174 			freebsd_to_netbsd_ptrace_regs(&fr,
    175 						&nrp->regs, &nrp->fpregs);
    176 #ifdef PT_SETREGS
    177 			SCARG(&npa, req) = PT_SETREGS;
    178 			SCARG(&npa, pid) = SCARG(uap, pid);
    179 			SCARG(&npa, addr) = (caddr_t)&nrp->regs;
    180 			if ((error = sys_ptrace(l, &npa, retval)) != 0)
    181 				return error;
    182 #endif
    183 #ifdef PT_SETFPREGS
    184 			SCARG(&npa, req) = PT_SETFPREGS;
    185 			SCARG(&npa, pid) = SCARG(uap, pid);
    186 			SCARG(&npa, addr) = (caddr_t)&nrp->fpregs;
    187 			if ((error = sys_ptrace(l, &npa, retval)) != 0)
    188 				return error;
    189 #endif
    190 			return 0;
    191 		}
    192 
    193 	default:			/* It was not a legal request. */
    194 		return (EINVAL);
    195 	}
    196 
    197 #ifdef DIAGNOSTIC
    198 	panic("freebsd_ptrace: impossible");
    199 #endif
    200 }
    201