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