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