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