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