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