xref: /netbsd-src/sys/compat/freebsd/freebsd_ptrace.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: freebsd_ptrace.c,v 1.6 2003/01/18 07:33:16 thorpej 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/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: freebsd_ptrace.c,v 1.6 2003/01/18 07:33:16 thorpej Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/errno.h>
51 #include <sys/ptrace.h>
52 #include <sys/uio.h>
53 #include <sys/user.h>
54 #include <sys/mount.h>
55 #include <sys/sa.h>
56 #include <sys/syscallargs.h>
57 
58 #include <machine/reg.h>
59 #include <machine/freebsd_machdep.h>
60 
61 #include <compat/freebsd/freebsd_syscallargs.h>
62 #include <compat/common/compat_util.h>
63 #include <compat/freebsd/freebsd_ptrace.h>
64 
65 /*
66  * Process debugging system call.
67  */
68 int
69 freebsd_sys_ptrace(l, v, retval)
70 	struct lwp *l;
71 	void *v;
72 	register_t *retval;
73 {
74 	struct freebsd_sys_ptrace_args /* {
75 		syscallarg(int) req;
76 		syscallarg(pid_t) pid;
77 		syscallarg(caddr_t) addr;
78 		syscallarg(int) data;
79 	} */ *uap = v;
80 	struct proc *p = l->l_proc;
81 	int error;
82 	caddr_t sg;
83 	struct {
84 		struct reg regs;
85 		struct fpreg fpregs;
86 	} *nrp;
87 	struct sys_ptrace_args npa;
88 	struct freebsd_ptrace_reg fr;
89 
90 	switch (SCARG(uap, req)) {
91 #ifdef PT_STEP
92 	case FREEBSD_PT_STEP:
93 		SCARG(&npa, req) = PT_STEP;
94 		SCARG(&npa, pid) = SCARG(uap, pid);
95 		SCARG(&npa, addr) = SCARG(uap, addr);
96 		SCARG(&npa, data) = SCARG(uap, data);
97 		return sys_ptrace(l, &npa, retval);
98 #endif
99 	case FREEBSD_PT_TRACE_ME:
100 	case FREEBSD_PT_READ_I:
101 	case FREEBSD_PT_READ_D:
102 	case FREEBSD_PT_WRITE_I:
103 	case FREEBSD_PT_WRITE_D:
104 	case FREEBSD_PT_CONTINUE:
105 	case FREEBSD_PT_KILL:
106 		/* These requests are compatible with NetBSD */
107 		return sys_ptrace(l, uap, retval);
108 
109 	case FREEBSD_PT_READ_U:
110 	case FREEBSD_PT_WRITE_U:
111 		sg = stackgap_init(p, 0);
112 		nrp = stackgap_alloc(p, &sg, sizeof(*nrp));
113 #ifdef PT_GETREGS
114 		SCARG(&npa, req) = PT_GETREGS;
115 		SCARG(&npa, pid) = SCARG(uap, pid);
116 		SCARG(&npa, addr) = (caddr_t)&nrp->regs;
117 		if ((error = sys_ptrace(l, &npa, retval)) != 0)
118 			return error;
119 #endif
120 #ifdef PT_GETFPREGS
121 		SCARG(&npa, req) = PT_GETFPREGS;
122 		SCARG(&npa, pid) = SCARG(uap, pid);
123 		SCARG(&npa, addr) = (caddr_t)&nrp->fpregs;
124 		if ((error = sys_ptrace(l, &npa, retval)) != 0)
125 			return error;
126 #endif
127 		netbsd_to_freebsd_ptrace_regs(&nrp->regs, &nrp->fpregs, &fr);
128 		switch (SCARG(uap, req)) {
129 		case FREEBSD_PT_READ_U:
130 			return freebsd_ptrace_getregs(&fr, SCARG(uap, addr),
131 						      retval);
132 
133 		case FREEBSD_PT_WRITE_U:
134 			error = freebsd_ptrace_setregs(&fr,
135 			    SCARG(uap, addr), SCARG(uap, data));
136 			if (error)
137 			    return error;
138 			freebsd_to_netbsd_ptrace_regs(&fr,
139 						&nrp->regs, &nrp->fpregs);
140 #ifdef PT_SETREGS
141 			SCARG(&npa, req) = PT_SETREGS;
142 			SCARG(&npa, pid) = SCARG(uap, pid);
143 			SCARG(&npa, addr) = (caddr_t)&nrp->regs;
144 			if ((error = sys_ptrace(l, &npa, retval)) != 0)
145 				return error;
146 #endif
147 #ifdef PT_SETFPREGS
148 			SCARG(&npa, req) = PT_SETFPREGS;
149 			SCARG(&npa, pid) = SCARG(uap, pid);
150 			SCARG(&npa, addr) = (caddr_t)&nrp->fpregs;
151 			if ((error = sys_ptrace(l, &npa, retval)) != 0)
152 				return error;
153 #endif
154 			return 0;
155 		}
156 
157 	default:			/* It was not a legal request. */
158 		return (EINVAL);
159 	}
160 
161 #ifdef DIAGNOSTIC
162 	panic("freebsd_ptrace: impossible");
163 #endif
164 }
165