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