sys_ptrace_common.c revision 1.5.2.3 1 1.5.2.3 skrll /* $NetBSD: sys_ptrace_common.c,v 1.5.2.3 2017/02/05 13:40:56 skrll Exp $ */
2 1.5.2.2 skrll
3 1.5.2.2 skrll /*-
4 1.5.2.2 skrll * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.5.2.2 skrll * All rights reserved.
6 1.5.2.2 skrll *
7 1.5.2.2 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.5.2.2 skrll * by Andrew Doran.
9 1.5.2.2 skrll *
10 1.5.2.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.5.2.2 skrll * modification, are permitted provided that the following conditions
12 1.5.2.2 skrll * are met:
13 1.5.2.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.5.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.5.2.2 skrll * documentation and/or other materials provided with the distribution.
18 1.5.2.2 skrll *
19 1.5.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.5.2.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.5.2.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.5.2.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.5.2.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.5.2.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.5.2.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5.2.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.5.2.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.5.2.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.5.2.2 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.5.2.2 skrll */
31 1.5.2.2 skrll
32 1.5.2.2 skrll /*-
33 1.5.2.2 skrll * Copyright (c) 1982, 1986, 1989, 1993
34 1.5.2.2 skrll * The Regents of the University of California. All rights reserved.
35 1.5.2.2 skrll * (c) UNIX System Laboratories, Inc.
36 1.5.2.2 skrll * All or some portions of this file are derived from material licensed
37 1.5.2.2 skrll * to the University of California by American Telephone and Telegraph
38 1.5.2.2 skrll * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.5.2.2 skrll * the permission of UNIX System Laboratories, Inc.
40 1.5.2.2 skrll *
41 1.5.2.2 skrll * This code is derived from software contributed to Berkeley by
42 1.5.2.2 skrll * Jan-Simon Pendry.
43 1.5.2.2 skrll *
44 1.5.2.2 skrll * Redistribution and use in source and binary forms, with or without
45 1.5.2.2 skrll * modification, are permitted provided that the following conditions
46 1.5.2.2 skrll * are met:
47 1.5.2.2 skrll * 1. Redistributions of source code must retain the above copyright
48 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer.
49 1.5.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
50 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer in the
51 1.5.2.2 skrll * documentation and/or other materials provided with the distribution.
52 1.5.2.2 skrll * 3. Neither the name of the University nor the names of its contributors
53 1.5.2.2 skrll * may be used to endorse or promote products derived from this software
54 1.5.2.2 skrll * without specific prior written permission.
55 1.5.2.2 skrll *
56 1.5.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.5.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.5.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.5.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.5.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.5.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.5.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.5.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.5.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.5.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.5.2.2 skrll * SUCH DAMAGE.
67 1.5.2.2 skrll *
68 1.5.2.2 skrll * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
69 1.5.2.2 skrll */
70 1.5.2.2 skrll
71 1.5.2.2 skrll /*-
72 1.5.2.2 skrll * Copyright (c) 1993 Jan-Simon Pendry.
73 1.5.2.2 skrll * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
74 1.5.2.2 skrll *
75 1.5.2.2 skrll * This code is derived from software contributed to Berkeley by
76 1.5.2.2 skrll * Jan-Simon Pendry.
77 1.5.2.2 skrll *
78 1.5.2.2 skrll * Redistribution and use in source and binary forms, with or without
79 1.5.2.2 skrll * modification, are permitted provided that the following conditions
80 1.5.2.2 skrll * are met:
81 1.5.2.2 skrll * 1. Redistributions of source code must retain the above copyright
82 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer.
83 1.5.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
84 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer in the
85 1.5.2.2 skrll * documentation and/or other materials provided with the distribution.
86 1.5.2.2 skrll * 3. All advertising materials mentioning features or use of this software
87 1.5.2.2 skrll * must display the following acknowledgement:
88 1.5.2.2 skrll * This product includes software developed by the University of
89 1.5.2.2 skrll * California, Berkeley and its contributors.
90 1.5.2.2 skrll * 4. Neither the name of the University nor the names of its contributors
91 1.5.2.2 skrll * may be used to endorse or promote products derived from this software
92 1.5.2.2 skrll * without specific prior written permission.
93 1.5.2.2 skrll *
94 1.5.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 1.5.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 1.5.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 1.5.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 1.5.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 1.5.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 1.5.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 1.5.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 1.5.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 1.5.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 1.5.2.2 skrll * SUCH DAMAGE.
105 1.5.2.2 skrll *
106 1.5.2.2 skrll * from: @(#)sys_process.c 8.1 (Berkeley) 6/10/93
107 1.5.2.2 skrll */
108 1.5.2.2 skrll
109 1.5.2.2 skrll /*
110 1.5.2.2 skrll * References:
111 1.5.2.2 skrll * (1) Bach's "The Design of the UNIX Operating System",
112 1.5.2.2 skrll * (2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113 1.5.2.2 skrll * (3) the "4.4BSD Programmer's Reference Manual" published
114 1.5.2.2 skrll * by USENIX and O'Reilly & Associates.
115 1.5.2.2 skrll * The 4.4BSD PRM does a reasonably good job of documenting what the various
116 1.5.2.2 skrll * ptrace() requests should actually do, and its text is quoted several times
117 1.5.2.2 skrll * in this file.
118 1.5.2.2 skrll */
119 1.5.2.2 skrll
120 1.5.2.2 skrll #include <sys/cdefs.h>
121 1.5.2.3 skrll __KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.5.2.3 2017/02/05 13:40:56 skrll Exp $");
122 1.5.2.2 skrll
123 1.5.2.2 skrll #ifdef _KERNEL_OPT
124 1.5.2.2 skrll #include "opt_ptrace.h"
125 1.5.2.2 skrll #include "opt_ktrace.h"
126 1.5.2.2 skrll #include "opt_pax.h"
127 1.5.2.2 skrll #endif
128 1.5.2.2 skrll
129 1.5.2.2 skrll #include <sys/param.h>
130 1.5.2.2 skrll #include <sys/systm.h>
131 1.5.2.2 skrll #include <sys/proc.h>
132 1.5.2.2 skrll #include <sys/errno.h>
133 1.5.2.2 skrll #include <sys/exec.h>
134 1.5.2.2 skrll #include <sys/pax.h>
135 1.5.2.2 skrll #include <sys/ptrace.h>
136 1.5.2.2 skrll #include <sys/uio.h>
137 1.5.2.2 skrll #include <sys/ras.h>
138 1.5.2.2 skrll #include <sys/kmem.h>
139 1.5.2.2 skrll #include <sys/kauth.h>
140 1.5.2.2 skrll #include <sys/mount.h>
141 1.5.2.2 skrll #include <sys/syscallargs.h>
142 1.5.2.2 skrll #include <sys/module.h>
143 1.5.2.2 skrll #include <sys/condvar.h>
144 1.5.2.2 skrll #include <sys/mutex.h>
145 1.5.2.2 skrll
146 1.5.2.2 skrll #include <uvm/uvm_extern.h>
147 1.5.2.2 skrll
148 1.5.2.2 skrll #include <machine/reg.h>
149 1.5.2.2 skrll
150 1.5.2.2 skrll #ifdef PTRACE
151 1.5.2.2 skrll
152 1.5.2.2 skrll # ifdef DEBUG
153 1.5.2.2 skrll # define DPRINTF(a) uprintf a
154 1.5.2.2 skrll # else
155 1.5.2.2 skrll # define DPRINTF(a)
156 1.5.2.2 skrll # endif
157 1.5.2.2 skrll
158 1.5.2.2 skrll static kauth_listener_t ptrace_listener;
159 1.5.2.2 skrll static int process_auxv_offset(struct proc *, struct uio *);
160 1.5.2.2 skrll
161 1.5.2.2 skrll #if 0
162 1.5.2.2 skrll static int ptrace_cbref;
163 1.5.2.2 skrll static kmutex_t ptrace_mtx;
164 1.5.2.2 skrll static kcondvar_t ptrace_cv;
165 1.5.2.2 skrll #endif
166 1.5.2.2 skrll
167 1.5.2.2 skrll static int
168 1.5.2.2 skrll ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
169 1.5.2.2 skrll void *arg0, void *arg1, void *arg2, void *arg3)
170 1.5.2.2 skrll {
171 1.5.2.2 skrll struct proc *p;
172 1.5.2.2 skrll int result;
173 1.5.2.2 skrll
174 1.5.2.2 skrll result = KAUTH_RESULT_DEFER;
175 1.5.2.2 skrll p = arg0;
176 1.5.2.2 skrll
177 1.5.2.2 skrll #if 0
178 1.5.2.2 skrll mutex_enter(&ptrace_mtx);
179 1.5.2.2 skrll ptrace_cbref++;
180 1.5.2.2 skrll mutex_exit(&ptrace_mtx);
181 1.5.2.2 skrll #endif
182 1.5.2.2 skrll if (action != KAUTH_PROCESS_PTRACE)
183 1.5.2.2 skrll goto out;
184 1.5.2.2 skrll
185 1.5.2.2 skrll switch ((u_long)arg1) {
186 1.5.2.2 skrll case PT_TRACE_ME:
187 1.5.2.2 skrll case PT_ATTACH:
188 1.5.2.2 skrll case PT_WRITE_I:
189 1.5.2.2 skrll case PT_WRITE_D:
190 1.5.2.2 skrll case PT_READ_I:
191 1.5.2.2 skrll case PT_READ_D:
192 1.5.2.2 skrll case PT_IO:
193 1.5.2.2 skrll #ifdef PT_GETREGS
194 1.5.2.2 skrll case PT_GETREGS:
195 1.5.2.2 skrll #endif
196 1.5.2.2 skrll #ifdef PT_SETREGS
197 1.5.2.2 skrll case PT_SETREGS:
198 1.5.2.2 skrll #endif
199 1.5.2.2 skrll #ifdef PT_GETFPREGS
200 1.5.2.2 skrll case PT_GETFPREGS:
201 1.5.2.2 skrll #endif
202 1.5.2.2 skrll #ifdef PT_SETFPREGS
203 1.5.2.2 skrll case PT_SETFPREGS:
204 1.5.2.2 skrll #endif
205 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
206 1.5.2.3 skrll case PT_READ_WATCHPOINT:
207 1.5.2.3 skrll case PT_WRITE_WATCHPOINT:
208 1.5.2.3 skrll case PT_COUNT_WATCHPOINTS:
209 1.5.2.3 skrll #endif
210 1.5.2.2 skrll case PT_SET_EVENT_MASK:
211 1.5.2.2 skrll case PT_GET_EVENT_MASK:
212 1.5.2.2 skrll case PT_GET_PROCESS_STATE:
213 1.5.2.3 skrll case PT_SET_SIGINFO:
214 1.5.2.3 skrll case PT_GET_SIGINFO:
215 1.5.2.2 skrll #ifdef __HAVE_PTRACE_MACHDEP
216 1.5.2.2 skrll PTRACE_MACHDEP_REQUEST_CASES
217 1.5.2.2 skrll #endif
218 1.5.2.2 skrll if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
219 1.5.2.2 skrll ISSET(p->p_flag, PK_SUGID)) {
220 1.5.2.2 skrll break;
221 1.5.2.2 skrll }
222 1.5.2.2 skrll
223 1.5.2.2 skrll result = KAUTH_RESULT_ALLOW;
224 1.5.2.2 skrll
225 1.5.2.2 skrll break;
226 1.5.2.2 skrll
227 1.5.2.2 skrll #ifdef PT_STEP
228 1.5.2.2 skrll case PT_STEP:
229 1.5.2.2 skrll #endif
230 1.5.2.2 skrll case PT_CONTINUE:
231 1.5.2.2 skrll case PT_KILL:
232 1.5.2.2 skrll case PT_DETACH:
233 1.5.2.2 skrll case PT_LWPINFO:
234 1.5.2.2 skrll case PT_SYSCALL:
235 1.5.2.2 skrll case PT_SYSCALLEMU:
236 1.5.2.2 skrll case PT_DUMPCORE:
237 1.5.2.2 skrll result = KAUTH_RESULT_ALLOW;
238 1.5.2.2 skrll break;
239 1.5.2.2 skrll
240 1.5.2.2 skrll default:
241 1.5.2.2 skrll break;
242 1.5.2.2 skrll }
243 1.5.2.2 skrll
244 1.5.2.2 skrll out:
245 1.5.2.2 skrll #if 0
246 1.5.2.2 skrll mutex_enter(&ptrace_mtx);
247 1.5.2.2 skrll if (--ptrace_cbref == 0)
248 1.5.2.2 skrll cv_broadcast(&ptrace_cv);
249 1.5.2.2 skrll mutex_exit(&ptrace_mtx);
250 1.5.2.2 skrll #endif
251 1.5.2.2 skrll
252 1.5.2.2 skrll return result;
253 1.5.2.2 skrll }
254 1.5.2.2 skrll
255 1.5.2.2 skrll int
256 1.5.2.2 skrll ptrace_init(void)
257 1.5.2.2 skrll {
258 1.5.2.2 skrll
259 1.5.2.2 skrll #if 0
260 1.5.2.2 skrll mutex_init(&ptrace_mtx, MUTEX_DEFAULT, IPL_NONE);
261 1.5.2.2 skrll cv_init(&ptrace_cv, "ptracecb");
262 1.5.2.2 skrll ptrace_cbref = 0;
263 1.5.2.2 skrll #endif
264 1.5.2.2 skrll ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
265 1.5.2.2 skrll ptrace_listener_cb, NULL);
266 1.5.2.2 skrll return 0;
267 1.5.2.2 skrll }
268 1.5.2.2 skrll
269 1.5.2.2 skrll int
270 1.5.2.2 skrll ptrace_fini(void)
271 1.5.2.2 skrll {
272 1.5.2.2 skrll
273 1.5.2.2 skrll kauth_unlisten_scope(ptrace_listener);
274 1.5.2.2 skrll
275 1.5.2.2 skrll #if 0
276 1.5.2.2 skrll /* Make sure no-one is executing our kauth listener */
277 1.5.2.2 skrll
278 1.5.2.2 skrll mutex_enter(&ptrace_mtx);
279 1.5.2.2 skrll while (ptrace_cbref != 0)
280 1.5.2.2 skrll cv_wait(&ptrace_cv, &ptrace_mtx);
281 1.5.2.2 skrll mutex_exit(&ptrace_mtx);
282 1.5.2.2 skrll mutex_destroy(&ptrace_mtx);
283 1.5.2.2 skrll cv_destroy(&ptrace_cv);
284 1.5.2.2 skrll #endif
285 1.5.2.2 skrll
286 1.5.2.2 skrll return 0;
287 1.5.2.2 skrll }
288 1.5.2.2 skrll
289 1.5.2.2 skrll int
290 1.5.2.2 skrll do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid,
291 1.5.2.2 skrll void *addr, int data, register_t *retval)
292 1.5.2.2 skrll {
293 1.5.2.2 skrll struct proc *p = l->l_proc;
294 1.5.2.2 skrll struct lwp *lt;
295 1.5.2.2 skrll #ifdef PT_STEP
296 1.5.2.2 skrll struct lwp *lt2;
297 1.5.2.2 skrll #endif
298 1.5.2.2 skrll struct proc *t; /* target process */
299 1.5.2.2 skrll struct uio uio;
300 1.5.2.2 skrll struct iovec iov;
301 1.5.2.2 skrll struct ptrace_io_desc piod;
302 1.5.2.2 skrll struct ptrace_event pe;
303 1.5.2.2 skrll struct ptrace_state ps;
304 1.5.2.2 skrll struct ptrace_lwpinfo pl;
305 1.5.2.3 skrll struct ptrace_siginfo psi;
306 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
307 1.5.2.3 skrll struct ptrace_watchpoint pw;
308 1.5.2.3 skrll #endif
309 1.5.2.2 skrll struct vmspace *vm;
310 1.5.2.2 skrll int error, write, tmp, pheld;
311 1.5.2.2 skrll int signo = 0;
312 1.5.2.2 skrll int resume_all;
313 1.5.2.2 skrll ksiginfo_t ksi;
314 1.5.2.2 skrll char *path;
315 1.5.2.2 skrll int len = 0;
316 1.5.2.2 skrll error = 0;
317 1.5.2.2 skrll
318 1.5.2.2 skrll /*
319 1.5.2.2 skrll * If attaching or detaching, we need to get a write hold on the
320 1.5.2.2 skrll * proclist lock so that we can re-parent the target process.
321 1.5.2.2 skrll */
322 1.5.2.2 skrll mutex_enter(proc_lock);
323 1.5.2.2 skrll
324 1.5.2.2 skrll /* "A foolish consistency..." XXX */
325 1.5.2.2 skrll if (req == PT_TRACE_ME) {
326 1.5.2.2 skrll t = p;
327 1.5.2.2 skrll mutex_enter(t->p_lock);
328 1.5.2.2 skrll } else {
329 1.5.2.2 skrll /* Find the process we're supposed to be operating on. */
330 1.5.2.2 skrll t = proc_find(pid);
331 1.5.2.2 skrll if (t == NULL) {
332 1.5.2.2 skrll mutex_exit(proc_lock);
333 1.5.2.2 skrll return ESRCH;
334 1.5.2.2 skrll }
335 1.5.2.2 skrll
336 1.5.2.2 skrll /* XXX-elad */
337 1.5.2.2 skrll mutex_enter(t->p_lock);
338 1.5.2.2 skrll error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
339 1.5.2.2 skrll t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
340 1.5.2.2 skrll if (error) {
341 1.5.2.2 skrll mutex_exit(proc_lock);
342 1.5.2.2 skrll mutex_exit(t->p_lock);
343 1.5.2.2 skrll return ESRCH;
344 1.5.2.2 skrll }
345 1.5.2.2 skrll }
346 1.5.2.2 skrll
347 1.5.2.2 skrll /*
348 1.5.2.2 skrll * Grab a reference on the process to prevent it from execing or
349 1.5.2.2 skrll * exiting.
350 1.5.2.2 skrll */
351 1.5.2.2 skrll if (!rw_tryenter(&t->p_reflock, RW_READER)) {
352 1.5.2.2 skrll mutex_exit(proc_lock);
353 1.5.2.2 skrll mutex_exit(t->p_lock);
354 1.5.2.2 skrll return EBUSY;
355 1.5.2.2 skrll }
356 1.5.2.2 skrll
357 1.5.2.2 skrll /* Make sure we can operate on it. */
358 1.5.2.2 skrll switch (req) {
359 1.5.2.2 skrll case PT_TRACE_ME:
360 1.5.2.2 skrll /* Saying that you're being traced is always legal. */
361 1.5.2.2 skrll break;
362 1.5.2.2 skrll
363 1.5.2.2 skrll case PT_ATTACH:
364 1.5.2.2 skrll /*
365 1.5.2.2 skrll * You can't attach to a process if:
366 1.5.2.2 skrll * (1) it's the process that's doing the attaching,
367 1.5.2.2 skrll */
368 1.5.2.2 skrll if (t->p_pid == p->p_pid) {
369 1.5.2.2 skrll error = EINVAL;
370 1.5.2.2 skrll break;
371 1.5.2.2 skrll }
372 1.5.2.2 skrll
373 1.5.2.2 skrll /*
374 1.5.2.2 skrll * (2) it's a system process
375 1.5.2.2 skrll */
376 1.5.2.2 skrll if (t->p_flag & PK_SYSTEM) {
377 1.5.2.2 skrll error = EPERM;
378 1.5.2.2 skrll break;
379 1.5.2.2 skrll }
380 1.5.2.2 skrll
381 1.5.2.2 skrll /*
382 1.5.2.2 skrll * (3) it's already being traced, or
383 1.5.2.2 skrll */
384 1.5.2.2 skrll if (ISSET(t->p_slflag, PSL_TRACED)) {
385 1.5.2.2 skrll error = EBUSY;
386 1.5.2.2 skrll break;
387 1.5.2.2 skrll }
388 1.5.2.2 skrll
389 1.5.2.2 skrll /*
390 1.5.2.2 skrll * (4) the tracer is chrooted, and its root directory is
391 1.5.2.2 skrll * not at or above the root directory of the tracee
392 1.5.2.2 skrll */
393 1.5.2.2 skrll mutex_exit(t->p_lock); /* XXXSMP */
394 1.5.2.2 skrll tmp = proc_isunder(t, l);
395 1.5.2.2 skrll mutex_enter(t->p_lock); /* XXXSMP */
396 1.5.2.2 skrll if (!tmp) {
397 1.5.2.2 skrll error = EPERM;
398 1.5.2.2 skrll break;
399 1.5.2.2 skrll }
400 1.5.2.2 skrll break;
401 1.5.2.2 skrll
402 1.5.2.2 skrll case PT_READ_I:
403 1.5.2.2 skrll case PT_READ_D:
404 1.5.2.2 skrll case PT_WRITE_I:
405 1.5.2.2 skrll case PT_WRITE_D:
406 1.5.2.2 skrll case PT_IO:
407 1.5.2.2 skrll #ifdef PT_GETREGS
408 1.5.2.2 skrll case PT_GETREGS:
409 1.5.2.2 skrll #endif
410 1.5.2.2 skrll #ifdef PT_SETREGS
411 1.5.2.2 skrll case PT_SETREGS:
412 1.5.2.2 skrll #endif
413 1.5.2.2 skrll #ifdef PT_GETFPREGS
414 1.5.2.2 skrll case PT_GETFPREGS:
415 1.5.2.2 skrll #endif
416 1.5.2.2 skrll #ifdef PT_SETFPREGS
417 1.5.2.2 skrll case PT_SETFPREGS:
418 1.5.2.2 skrll #endif
419 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
420 1.5.2.3 skrll case PT_READ_WATCHPOINT:
421 1.5.2.3 skrll case PT_WRITE_WATCHPOINT:
422 1.5.2.3 skrll case PT_COUNT_WATCHPOINTS:
423 1.5.2.3 skrll #endif
424 1.5.2.2 skrll #ifdef __HAVE_PTRACE_MACHDEP
425 1.5.2.2 skrll PTRACE_MACHDEP_REQUEST_CASES
426 1.5.2.2 skrll #endif
427 1.5.2.2 skrll /*
428 1.5.2.2 skrll * You can't read/write the memory or registers of a process
429 1.5.2.2 skrll * if the tracer is chrooted, and its root directory is not at
430 1.5.2.2 skrll * or above the root directory of the tracee.
431 1.5.2.2 skrll */
432 1.5.2.2 skrll mutex_exit(t->p_lock); /* XXXSMP */
433 1.5.2.2 skrll tmp = proc_isunder(t, l);
434 1.5.2.2 skrll mutex_enter(t->p_lock); /* XXXSMP */
435 1.5.2.2 skrll if (!tmp) {
436 1.5.2.2 skrll error = EPERM;
437 1.5.2.2 skrll break;
438 1.5.2.2 skrll }
439 1.5.2.2 skrll /*FALLTHROUGH*/
440 1.5.2.2 skrll
441 1.5.2.2 skrll case PT_CONTINUE:
442 1.5.2.2 skrll case PT_KILL:
443 1.5.2.2 skrll case PT_DETACH:
444 1.5.2.2 skrll case PT_LWPINFO:
445 1.5.2.2 skrll case PT_SYSCALL:
446 1.5.2.2 skrll case PT_SYSCALLEMU:
447 1.5.2.2 skrll case PT_DUMPCORE:
448 1.5.2.2 skrll #ifdef PT_STEP
449 1.5.2.2 skrll case PT_STEP:
450 1.5.2.2 skrll #endif
451 1.5.2.2 skrll case PT_SET_EVENT_MASK:
452 1.5.2.2 skrll case PT_GET_EVENT_MASK:
453 1.5.2.2 skrll case PT_GET_PROCESS_STATE:
454 1.5.2.3 skrll case PT_SET_SIGINFO:
455 1.5.2.3 skrll case PT_GET_SIGINFO:
456 1.5.2.2 skrll /*
457 1.5.2.2 skrll * You can't do what you want to the process if:
458 1.5.2.2 skrll * (1) It's not being traced at all,
459 1.5.2.2 skrll */
460 1.5.2.2 skrll if (!ISSET(t->p_slflag, PSL_TRACED)) {
461 1.5.2.2 skrll error = EPERM;
462 1.5.2.2 skrll break;
463 1.5.2.2 skrll }
464 1.5.2.2 skrll
465 1.5.2.2 skrll /*
466 1.5.2.2 skrll * (2) it's being traced by procfs (which has
467 1.5.2.2 skrll * different signal delivery semantics),
468 1.5.2.2 skrll */
469 1.5.2.2 skrll if (ISSET(t->p_slflag, PSL_FSTRACE)) {
470 1.5.2.2 skrll DPRINTF(("file system traced\n"));
471 1.5.2.2 skrll error = EBUSY;
472 1.5.2.2 skrll break;
473 1.5.2.2 skrll }
474 1.5.2.2 skrll
475 1.5.2.2 skrll /*
476 1.5.2.2 skrll * (3) it's not being traced by _you_, or
477 1.5.2.2 skrll */
478 1.5.2.2 skrll if (t->p_pptr != p) {
479 1.5.2.2 skrll DPRINTF(("parent %d != %d\n", t->p_pptr->p_pid,
480 1.5.2.2 skrll p->p_pid));
481 1.5.2.2 skrll error = EBUSY;
482 1.5.2.2 skrll break;
483 1.5.2.2 skrll }
484 1.5.2.2 skrll
485 1.5.2.2 skrll /*
486 1.5.2.2 skrll * (4) it's not currently stopped.
487 1.5.2.2 skrll */
488 1.5.2.2 skrll if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
489 1.5.2.2 skrll DPRINTF(("stat %d flag %d\n", t->p_stat,
490 1.5.2.2 skrll !t->p_waited));
491 1.5.2.2 skrll error = EBUSY;
492 1.5.2.2 skrll break;
493 1.5.2.2 skrll }
494 1.5.2.2 skrll break;
495 1.5.2.2 skrll
496 1.5.2.2 skrll default: /* It was not a legal request. */
497 1.5.2.2 skrll error = EINVAL;
498 1.5.2.2 skrll break;
499 1.5.2.2 skrll }
500 1.5.2.2 skrll
501 1.5.2.2 skrll if (error == 0) {
502 1.5.2.2 skrll error = kauth_authorize_process(l->l_cred,
503 1.5.2.2 skrll KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
504 1.5.2.2 skrll NULL, NULL);
505 1.5.2.2 skrll }
506 1.5.2.2 skrll if (error == 0) {
507 1.5.2.2 skrll lt = lwp_find_first(t);
508 1.5.2.2 skrll if (lt == NULL)
509 1.5.2.2 skrll error = ESRCH;
510 1.5.2.2 skrll }
511 1.5.2.2 skrll
512 1.5.2.2 skrll if (error != 0) {
513 1.5.2.2 skrll mutex_exit(proc_lock);
514 1.5.2.2 skrll mutex_exit(t->p_lock);
515 1.5.2.2 skrll rw_exit(&t->p_reflock);
516 1.5.2.2 skrll return error;
517 1.5.2.2 skrll }
518 1.5.2.2 skrll
519 1.5.2.2 skrll /* Do single-step fixup if needed. */
520 1.5.2.2 skrll FIX_SSTEP(t);
521 1.5.2.2 skrll KASSERT(lt != NULL);
522 1.5.2.2 skrll lwp_addref(lt);
523 1.5.2.2 skrll
524 1.5.2.2 skrll /*
525 1.5.2.2 skrll * Which locks do we need held? XXX Ugly.
526 1.5.2.2 skrll */
527 1.5.2.2 skrll switch (req) {
528 1.5.2.2 skrll #ifdef PT_STEP
529 1.5.2.2 skrll case PT_STEP:
530 1.5.2.2 skrll #endif
531 1.5.2.2 skrll case PT_CONTINUE:
532 1.5.2.2 skrll case PT_DETACH:
533 1.5.2.2 skrll case PT_KILL:
534 1.5.2.2 skrll case PT_SYSCALL:
535 1.5.2.2 skrll case PT_SYSCALLEMU:
536 1.5.2.2 skrll case PT_ATTACH:
537 1.5.2.2 skrll case PT_TRACE_ME:
538 1.5.2.2 skrll pheld = 1;
539 1.5.2.2 skrll break;
540 1.5.2.2 skrll default:
541 1.5.2.2 skrll mutex_exit(proc_lock);
542 1.5.2.2 skrll mutex_exit(t->p_lock);
543 1.5.2.2 skrll pheld = 0;
544 1.5.2.2 skrll break;
545 1.5.2.2 skrll }
546 1.5.2.2 skrll
547 1.5.2.2 skrll /* Now do the operation. */
548 1.5.2.2 skrll write = 0;
549 1.5.2.2 skrll *retval = 0;
550 1.5.2.2 skrll tmp = 0;
551 1.5.2.2 skrll resume_all = 1;
552 1.5.2.2 skrll
553 1.5.2.2 skrll switch (req) {
554 1.5.2.2 skrll case PT_TRACE_ME:
555 1.5.2.2 skrll /* Just set the trace flag. */
556 1.5.2.2 skrll SET(t->p_slflag, PSL_TRACED);
557 1.5.2.2 skrll t->p_opptr = t->p_pptr;
558 1.5.2.2 skrll break;
559 1.5.2.2 skrll
560 1.5.2.2 skrll case PT_WRITE_I: /* XXX no separate I and D spaces */
561 1.5.2.2 skrll case PT_WRITE_D:
562 1.5.2.2 skrll #if defined(__HAVE_RAS)
563 1.5.2.2 skrll /*
564 1.5.2.2 skrll * Can't write to a RAS
565 1.5.2.2 skrll */
566 1.5.2.2 skrll if (ras_lookup(t, addr) != (void *)-1) {
567 1.5.2.2 skrll error = EACCES;
568 1.5.2.2 skrll break;
569 1.5.2.2 skrll }
570 1.5.2.2 skrll #endif
571 1.5.2.2 skrll write = 1;
572 1.5.2.2 skrll tmp = data;
573 1.5.2.2 skrll /* FALLTHROUGH */
574 1.5.2.2 skrll
575 1.5.2.2 skrll case PT_READ_I: /* XXX no separate I and D spaces */
576 1.5.2.2 skrll case PT_READ_D:
577 1.5.2.2 skrll /* write = 0 done above. */
578 1.5.2.2 skrll iov.iov_base = (void *)&tmp;
579 1.5.2.2 skrll iov.iov_len = sizeof(tmp);
580 1.5.2.2 skrll uio.uio_iov = &iov;
581 1.5.2.2 skrll uio.uio_iovcnt = 1;
582 1.5.2.2 skrll uio.uio_offset = (off_t)(unsigned long)addr;
583 1.5.2.2 skrll uio.uio_resid = sizeof(tmp);
584 1.5.2.2 skrll uio.uio_rw = write ? UIO_WRITE : UIO_READ;
585 1.5.2.2 skrll UIO_SETUP_SYSSPACE(&uio);
586 1.5.2.2 skrll
587 1.5.2.2 skrll error = process_domem(l, lt, &uio);
588 1.5.2.2 skrll if (!write)
589 1.5.2.2 skrll *retval = tmp;
590 1.5.2.2 skrll break;
591 1.5.2.2 skrll
592 1.5.2.2 skrll case PT_IO:
593 1.5.2.2 skrll error = ptm->ptm_copyinpiod(&piod, addr);
594 1.5.2.2 skrll if (error)
595 1.5.2.2 skrll break;
596 1.5.2.2 skrll
597 1.5.2.2 skrll iov.iov_base = piod.piod_addr;
598 1.5.2.2 skrll iov.iov_len = piod.piod_len;
599 1.5.2.2 skrll uio.uio_iov = &iov;
600 1.5.2.2 skrll uio.uio_iovcnt = 1;
601 1.5.2.2 skrll uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
602 1.5.2.2 skrll uio.uio_resid = piod.piod_len;
603 1.5.2.2 skrll
604 1.5.2.2 skrll switch (piod.piod_op) {
605 1.5.2.2 skrll case PIOD_READ_D:
606 1.5.2.2 skrll case PIOD_READ_I:
607 1.5.2.2 skrll uio.uio_rw = UIO_READ;
608 1.5.2.2 skrll break;
609 1.5.2.2 skrll case PIOD_WRITE_D:
610 1.5.2.2 skrll case PIOD_WRITE_I:
611 1.5.2.2 skrll /*
612 1.5.2.2 skrll * Can't write to a RAS
613 1.5.2.2 skrll */
614 1.5.2.2 skrll if (ras_lookup(t, addr) != (void *)-1) {
615 1.5.2.2 skrll return EACCES;
616 1.5.2.2 skrll }
617 1.5.2.2 skrll uio.uio_rw = UIO_WRITE;
618 1.5.2.2 skrll break;
619 1.5.2.2 skrll case PIOD_READ_AUXV:
620 1.5.2.2 skrll req = PT_READ_D;
621 1.5.2.2 skrll uio.uio_rw = UIO_READ;
622 1.5.2.3 skrll tmp = t->p_execsw->es_arglen;
623 1.5.2.2 skrll if (uio.uio_offset > tmp)
624 1.5.2.2 skrll return EIO;
625 1.5.2.2 skrll if (uio.uio_resid > tmp - uio.uio_offset)
626 1.5.2.2 skrll uio.uio_resid = tmp - uio.uio_offset;
627 1.5.2.2 skrll piod.piod_len = iov.iov_len = uio.uio_resid;
628 1.5.2.2 skrll error = process_auxv_offset(t, &uio);
629 1.5.2.2 skrll break;
630 1.5.2.2 skrll default:
631 1.5.2.2 skrll error = EINVAL;
632 1.5.2.2 skrll break;
633 1.5.2.2 skrll }
634 1.5.2.2 skrll if (error)
635 1.5.2.2 skrll break;
636 1.5.2.2 skrll error = proc_vmspace_getref(l->l_proc, &vm);
637 1.5.2.2 skrll if (error)
638 1.5.2.2 skrll break;
639 1.5.2.2 skrll uio.uio_vmspace = vm;
640 1.5.2.2 skrll
641 1.5.2.2 skrll error = process_domem(l, lt, &uio);
642 1.5.2.2 skrll piod.piod_len -= uio.uio_resid;
643 1.5.2.2 skrll (void) ptm->ptm_copyoutpiod(&piod, addr);
644 1.5.2.2 skrll
645 1.5.2.2 skrll uvmspace_free(vm);
646 1.5.2.2 skrll break;
647 1.5.2.2 skrll
648 1.5.2.2 skrll case PT_DUMPCORE:
649 1.5.2.2 skrll if ((path = addr) != NULL) {
650 1.5.2.2 skrll char *dst;
651 1.5.2.2 skrll len = data;
652 1.5.2.2 skrll
653 1.5.2.2 skrll if (len < 0 || len >= MAXPATHLEN) {
654 1.5.2.2 skrll error = EINVAL;
655 1.5.2.2 skrll break;
656 1.5.2.2 skrll }
657 1.5.2.2 skrll dst = kmem_alloc(len + 1, KM_SLEEP);
658 1.5.2.2 skrll if ((error = copyin(path, dst, len)) != 0) {
659 1.5.2.2 skrll kmem_free(dst, len + 1);
660 1.5.2.2 skrll break;
661 1.5.2.2 skrll }
662 1.5.2.2 skrll path = dst;
663 1.5.2.2 skrll path[len] = '\0';
664 1.5.2.2 skrll }
665 1.5.2.2 skrll error = (*coredump_vec)(lt, path);
666 1.5.2.2 skrll if (path)
667 1.5.2.2 skrll kmem_free(path, len + 1);
668 1.5.2.2 skrll break;
669 1.5.2.2 skrll
670 1.5.2.2 skrll #ifdef PT_STEP
671 1.5.2.2 skrll case PT_STEP:
672 1.5.2.2 skrll /*
673 1.5.2.2 skrll * From the 4.4BSD PRM:
674 1.5.2.2 skrll * "Execution continues as in request PT_CONTINUE; however
675 1.5.2.2 skrll * as soon as possible after execution of at least one
676 1.5.2.2 skrll * instruction, execution stops again. [ ... ]"
677 1.5.2.2 skrll */
678 1.5.2.2 skrll #endif
679 1.5.2.2 skrll case PT_CONTINUE:
680 1.5.2.2 skrll case PT_SYSCALL:
681 1.5.2.2 skrll case PT_DETACH:
682 1.5.2.2 skrll if (req == PT_SYSCALL) {
683 1.5.2.2 skrll if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
684 1.5.2.2 skrll SET(t->p_slflag, PSL_SYSCALL);
685 1.5.2.2 skrll #ifdef __HAVE_SYSCALL_INTERN
686 1.5.2.2 skrll (*t->p_emul->e_syscall_intern)(t);
687 1.5.2.2 skrll #endif
688 1.5.2.2 skrll }
689 1.5.2.2 skrll } else {
690 1.5.2.2 skrll if (ISSET(t->p_slflag, PSL_SYSCALL)) {
691 1.5.2.2 skrll CLR(t->p_slflag, PSL_SYSCALL);
692 1.5.2.2 skrll #ifdef __HAVE_SYSCALL_INTERN
693 1.5.2.2 skrll (*t->p_emul->e_syscall_intern)(t);
694 1.5.2.2 skrll #endif
695 1.5.2.2 skrll }
696 1.5.2.2 skrll }
697 1.5.2.2 skrll t->p_trace_enabled = trace_is_enabled(t);
698 1.5.2.2 skrll
699 1.5.2.2 skrll /*
700 1.5.2.2 skrll * Pick up the LWPID, if supplied. There are two cases:
701 1.5.2.2 skrll * data < 0 : step or continue single thread, lwp = -data
702 1.5.2.2 skrll * data > 0 in PT_STEP : step this thread, continue others
703 1.5.2.2 skrll * For operations other than PT_STEP, data > 0 means
704 1.5.2.2 skrll * data is the signo to deliver to the process.
705 1.5.2.2 skrll */
706 1.5.2.2 skrll tmp = data;
707 1.5.2.2 skrll if (tmp >= 0) {
708 1.5.2.2 skrll #ifdef PT_STEP
709 1.5.2.2 skrll if (req == PT_STEP)
710 1.5.2.2 skrll signo = 0;
711 1.5.2.2 skrll else
712 1.5.2.2 skrll #endif
713 1.5.2.2 skrll {
714 1.5.2.2 skrll signo = tmp;
715 1.5.2.2 skrll tmp = 0; /* don't search for LWP */
716 1.5.2.2 skrll }
717 1.5.2.2 skrll } else
718 1.5.2.2 skrll tmp = -tmp;
719 1.5.2.2 skrll
720 1.5.2.2 skrll if (tmp > 0) {
721 1.5.2.2 skrll if (req == PT_DETACH) {
722 1.5.2.2 skrll error = EINVAL;
723 1.5.2.2 skrll break;
724 1.5.2.2 skrll }
725 1.5.2.2 skrll lwp_delref2 (lt);
726 1.5.2.2 skrll lt = lwp_find(t, tmp);
727 1.5.2.2 skrll if (lt == NULL) {
728 1.5.2.2 skrll error = ESRCH;
729 1.5.2.2 skrll break;
730 1.5.2.2 skrll }
731 1.5.2.2 skrll lwp_addref(lt);
732 1.5.2.2 skrll resume_all = 0;
733 1.5.2.2 skrll signo = 0;
734 1.5.2.2 skrll }
735 1.5.2.2 skrll
736 1.5.2.2 skrll /*
737 1.5.2.2 skrll * From the 4.4BSD PRM:
738 1.5.2.2 skrll * "The data argument is taken as a signal number and the
739 1.5.2.2 skrll * child's execution continues at location addr as if it
740 1.5.2.2 skrll * incurred that signal. Normally the signal number will
741 1.5.2.2 skrll * be either 0 to indicate that the signal that caused the
742 1.5.2.2 skrll * stop should be ignored, or that value fetched out of
743 1.5.2.2 skrll * the process's image indicating which signal caused
744 1.5.2.2 skrll * the stop. If addr is (int *)1 then execution continues
745 1.5.2.2 skrll * from where it stopped."
746 1.5.2.2 skrll */
747 1.5.2.2 skrll
748 1.5.2.2 skrll /* Check that the data is a valid signal number or zero. */
749 1.5.2.2 skrll if (signo < 0 || signo >= NSIG) {
750 1.5.2.2 skrll error = EINVAL;
751 1.5.2.2 skrll break;
752 1.5.2.2 skrll }
753 1.5.2.2 skrll
754 1.5.2.2 skrll /* If the address parameter is not (int *)1, set the pc. */
755 1.5.2.2 skrll if ((int *)addr != (int *)1) {
756 1.5.2.2 skrll error = process_set_pc(lt, addr);
757 1.5.2.2 skrll if (error != 0)
758 1.5.2.2 skrll break;
759 1.5.2.2 skrll }
760 1.5.2.2 skrll #ifdef PT_STEP
761 1.5.2.2 skrll /*
762 1.5.2.2 skrll * Arrange for a single-step, if that's requested and possible.
763 1.5.2.2 skrll * More precisely, set the single step status as requested for
764 1.5.2.2 skrll * the requested thread, and clear it for other threads.
765 1.5.2.2 skrll */
766 1.5.2.2 skrll LIST_FOREACH(lt2, &t->p_lwps, l_sibling) {
767 1.5.2.2 skrll if (lt != lt2) {
768 1.5.2.2 skrll lwp_lock(lt2);
769 1.5.2.2 skrll process_sstep(lt2, 0);
770 1.5.2.2 skrll lwp_unlock(lt2);
771 1.5.2.2 skrll }
772 1.5.2.2 skrll }
773 1.5.2.2 skrll error = process_sstep(lt, req == PT_STEP);
774 1.5.2.2 skrll if (error)
775 1.5.2.2 skrll break;
776 1.5.2.2 skrll #endif
777 1.5.2.2 skrll if (req == PT_DETACH) {
778 1.5.2.2 skrll CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
779 1.5.2.2 skrll
780 1.5.2.2 skrll /* give process back to original parent or init */
781 1.5.2.2 skrll if (t->p_opptr != t->p_pptr) {
782 1.5.2.2 skrll struct proc *pp = t->p_opptr;
783 1.5.2.2 skrll proc_reparent(t, pp ? pp : initproc);
784 1.5.2.2 skrll }
785 1.5.2.2 skrll
786 1.5.2.2 skrll /* not being traced any more */
787 1.5.2.2 skrll t->p_opptr = NULL;
788 1.5.2.2 skrll }
789 1.5.2.2 skrll sendsig:
790 1.5.2.2 skrll t->p_fpid = 0;
791 1.5.2.3 skrll t->p_vfpid = 0;
792 1.5.2.3 skrll t->p_vfpid_done = 0;
793 1.5.2.3 skrll t->p_lwp_created = 0;
794 1.5.2.3 skrll t->p_lwp_exited = 0;
795 1.5.2.2 skrll /* Finally, deliver the requested signal (or none). */
796 1.5.2.2 skrll if (t->p_stat == SSTOP) {
797 1.5.2.2 skrll /*
798 1.5.2.2 skrll * Unstop the process. If it needs to take a
799 1.5.2.2 skrll * signal, make all efforts to ensure that at
800 1.5.2.2 skrll * an LWP runs to see it.
801 1.5.2.2 skrll */
802 1.5.2.2 skrll t->p_xsig = signo;
803 1.5.2.2 skrll if (resume_all)
804 1.5.2.2 skrll proc_unstop(t);
805 1.5.2.2 skrll else
806 1.5.2.2 skrll lwp_unstop(lt);
807 1.5.2.3 skrll } else if (t->p_sigctx.ps_faked) {
808 1.5.2.3 skrll if (signo != t->p_sigctx.ps_info._signo) {
809 1.5.2.3 skrll error = EINVAL;
810 1.5.2.3 skrll break;
811 1.5.2.3 skrll }
812 1.5.2.3 skrll t->p_sigctx.ps_faked = false;
813 1.5.2.3 skrll KSI_INIT_EMPTY(&ksi);
814 1.5.2.3 skrll ksi.ksi_info = t->p_sigctx.ps_info;
815 1.5.2.3 skrll ksi.ksi_lid = t->p_sigctx.ps_lwp;
816 1.5.2.3 skrll kpsignal2(t, &ksi);
817 1.5.2.2 skrll } else if (signo != 0) {
818 1.5.2.2 skrll KSI_INIT_EMPTY(&ksi);
819 1.5.2.2 skrll ksi.ksi_signo = signo;
820 1.5.2.2 skrll kpsignal2(t, &ksi);
821 1.5.2.2 skrll }
822 1.5.2.2 skrll break;
823 1.5.2.2 skrll
824 1.5.2.2 skrll case PT_SYSCALLEMU:
825 1.5.2.2 skrll if (!ISSET(t->p_slflag, PSL_SYSCALL) || t->p_stat != SSTOP) {
826 1.5.2.2 skrll error = EINVAL;
827 1.5.2.2 skrll break;
828 1.5.2.2 skrll }
829 1.5.2.2 skrll SET(t->p_slflag, PSL_SYSCALLEMU);
830 1.5.2.2 skrll break;
831 1.5.2.2 skrll
832 1.5.2.2 skrll case PT_KILL:
833 1.5.2.2 skrll /* just send the process a KILL signal. */
834 1.5.2.2 skrll signo = SIGKILL;
835 1.5.2.2 skrll goto sendsig; /* in PT_CONTINUE, above. */
836 1.5.2.2 skrll
837 1.5.2.2 skrll case PT_ATTACH:
838 1.5.2.2 skrll /*
839 1.5.2.2 skrll * Go ahead and set the trace flag.
840 1.5.2.2 skrll * Save the old parent (it's reset in
841 1.5.2.2 skrll * _DETACH, and also in kern_exit.c:wait4()
842 1.5.2.2 skrll * Reparent the process so that the tracing
843 1.5.2.2 skrll * proc gets to see all the action.
844 1.5.2.2 skrll * Stop the target.
845 1.5.2.2 skrll */
846 1.5.2.2 skrll proc_changeparent(t, p);
847 1.5.2.2 skrll signo = SIGSTOP;
848 1.5.2.2 skrll goto sendsig;
849 1.5.2.2 skrll
850 1.5.2.2 skrll case PT_GET_EVENT_MASK:
851 1.5.2.2 skrll if (data != sizeof(pe)) {
852 1.5.2.2 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req,
853 1.5.2.2 skrll data, sizeof(pe)));
854 1.5.2.2 skrll error = EINVAL;
855 1.5.2.2 skrll break;
856 1.5.2.2 skrll }
857 1.5.2.2 skrll memset(&pe, 0, sizeof(pe));
858 1.5.2.2 skrll pe.pe_set_event = ISSET(t->p_slflag, PSL_TRACEFORK) ?
859 1.5.2.2 skrll PTRACE_FORK : 0;
860 1.5.2.3 skrll pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK) ?
861 1.5.2.3 skrll PTRACE_VFORK : 0;
862 1.5.2.3 skrll pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACEVFORK_DONE) ?
863 1.5.2.3 skrll PTRACE_VFORK_DONE : 0;
864 1.5.2.3 skrll pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_CREATE) ?
865 1.5.2.3 skrll PTRACE_LWP_CREATE : 0;
866 1.5.2.3 skrll pe.pe_set_event |= ISSET(t->p_slflag, PSL_TRACELWP_EXIT) ?
867 1.5.2.3 skrll PTRACE_LWP_EXIT : 0;
868 1.5.2.2 skrll error = copyout(&pe, addr, sizeof(pe));
869 1.5.2.2 skrll break;
870 1.5.2.2 skrll
871 1.5.2.2 skrll case PT_SET_EVENT_MASK:
872 1.5.2.2 skrll if (data != sizeof(pe)) {
873 1.5.2.2 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
874 1.5.2.2 skrll sizeof(pe)));
875 1.5.2.2 skrll error = EINVAL;
876 1.5.2.2 skrll break;
877 1.5.2.2 skrll }
878 1.5.2.2 skrll if ((error = copyin(addr, &pe, sizeof(pe))) != 0)
879 1.5.2.2 skrll return error;
880 1.5.2.2 skrll if (pe.pe_set_event & PTRACE_FORK)
881 1.5.2.2 skrll SET(t->p_slflag, PSL_TRACEFORK);
882 1.5.2.2 skrll else
883 1.5.2.2 skrll CLR(t->p_slflag, PSL_TRACEFORK);
884 1.5.2.3 skrll #if notyet
885 1.5.2.3 skrll if (pe.pe_set_event & PTRACE_VFORK)
886 1.5.2.3 skrll SET(t->p_slflag, PSL_TRACEVFORK);
887 1.5.2.3 skrll else
888 1.5.2.3 skrll CLR(t->p_slflag, PSL_TRACEVFORK);
889 1.5.2.3 skrll #else
890 1.5.2.3 skrll if (pe.pe_set_event & PTRACE_VFORK) {
891 1.5.2.3 skrll error = ENOTSUP;
892 1.5.2.3 skrll break;
893 1.5.2.3 skrll }
894 1.5.2.3 skrll #endif
895 1.5.2.3 skrll if (pe.pe_set_event & PTRACE_VFORK_DONE)
896 1.5.2.3 skrll SET(t->p_slflag, PSL_TRACEVFORK_DONE);
897 1.5.2.3 skrll else
898 1.5.2.3 skrll CLR(t->p_slflag, PSL_TRACEVFORK_DONE);
899 1.5.2.3 skrll if (pe.pe_set_event & PTRACE_LWP_CREATE)
900 1.5.2.3 skrll SET(t->p_slflag, PSL_TRACELWP_CREATE);
901 1.5.2.3 skrll else
902 1.5.2.3 skrll CLR(t->p_slflag, PSL_TRACELWP_CREATE);
903 1.5.2.3 skrll if (pe.pe_set_event & PTRACE_LWP_EXIT)
904 1.5.2.3 skrll SET(t->p_slflag, PSL_TRACELWP_EXIT);
905 1.5.2.3 skrll else
906 1.5.2.3 skrll CLR(t->p_slflag, PSL_TRACELWP_EXIT);
907 1.5.2.2 skrll break;
908 1.5.2.2 skrll
909 1.5.2.2 skrll case PT_GET_PROCESS_STATE:
910 1.5.2.2 skrll if (data != sizeof(ps)) {
911 1.5.2.2 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
912 1.5.2.2 skrll sizeof(ps)));
913 1.5.2.2 skrll error = EINVAL;
914 1.5.2.2 skrll break;
915 1.5.2.2 skrll }
916 1.5.2.2 skrll memset(&ps, 0, sizeof(ps));
917 1.5.2.2 skrll if (t->p_fpid) {
918 1.5.2.2 skrll ps.pe_report_event = PTRACE_FORK;
919 1.5.2.2 skrll ps.pe_other_pid = t->p_fpid;
920 1.5.2.3 skrll } else if (t->p_vfpid) {
921 1.5.2.3 skrll ps.pe_report_event = PTRACE_VFORK;
922 1.5.2.3 skrll ps.pe_other_pid = t->p_vfpid;
923 1.5.2.3 skrll } else if (t->p_vfpid_done) {
924 1.5.2.3 skrll ps.pe_report_event = PTRACE_VFORK_DONE;
925 1.5.2.3 skrll ps.pe_other_pid = t->p_vfpid_done;
926 1.5.2.3 skrll } else if (t->p_lwp_created) {
927 1.5.2.3 skrll ps.pe_report_event = PTRACE_LWP_CREATE;
928 1.5.2.3 skrll ps.pe_lwp = t->p_lwp_created;
929 1.5.2.3 skrll } else if (t->p_lwp_exited) {
930 1.5.2.3 skrll ps.pe_report_event = PTRACE_LWP_EXIT;
931 1.5.2.3 skrll ps.pe_lwp = t->p_lwp_exited;
932 1.5.2.2 skrll }
933 1.5.2.2 skrll error = copyout(&ps, addr, sizeof(ps));
934 1.5.2.2 skrll break;
935 1.5.2.2 skrll
936 1.5.2.2 skrll case PT_LWPINFO:
937 1.5.2.2 skrll if (data != sizeof(pl)) {
938 1.5.2.2 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
939 1.5.2.2 skrll sizeof(pl)));
940 1.5.2.2 skrll error = EINVAL;
941 1.5.2.2 skrll break;
942 1.5.2.2 skrll }
943 1.5.2.2 skrll error = copyin(addr, &pl, sizeof(pl));
944 1.5.2.2 skrll if (error)
945 1.5.2.2 skrll break;
946 1.5.2.2 skrll tmp = pl.pl_lwpid;
947 1.5.2.2 skrll lwp_delref(lt);
948 1.5.2.2 skrll mutex_enter(t->p_lock);
949 1.5.2.2 skrll if (tmp == 0)
950 1.5.2.2 skrll lt = lwp_find_first(t);
951 1.5.2.2 skrll else {
952 1.5.2.2 skrll lt = lwp_find(t, tmp);
953 1.5.2.2 skrll if (lt == NULL) {
954 1.5.2.2 skrll mutex_exit(t->p_lock);
955 1.5.2.2 skrll error = ESRCH;
956 1.5.2.2 skrll break;
957 1.5.2.2 skrll }
958 1.5.2.2 skrll lt = LIST_NEXT(lt, l_sibling);
959 1.5.2.2 skrll }
960 1.5.2.2 skrll while (lt != NULL && !lwp_alive(lt))
961 1.5.2.2 skrll lt = LIST_NEXT(lt, l_sibling);
962 1.5.2.2 skrll pl.pl_lwpid = 0;
963 1.5.2.2 skrll pl.pl_event = 0;
964 1.5.2.2 skrll if (lt) {
965 1.5.2.2 skrll lwp_addref(lt);
966 1.5.2.2 skrll pl.pl_lwpid = lt->l_lid;
967 1.5.2.3 skrll /*
968 1.5.2.3 skrll * If we match the lwp, or it was sent to every lwp,
969 1.5.2.3 skrll * we set PL_EVENT_SIGNAL.
970 1.5.2.3 skrll * XXX: ps_lwp == 0 means everyone and noone, so
971 1.5.2.3 skrll * check ps_signo too.
972 1.5.2.3 skrll */
973 1.5.2.3 skrll if (lt->l_lid == t->p_sigctx.ps_lwp
974 1.5.2.3 skrll || (t->p_sigctx.ps_lwp == 0 &&
975 1.5.2.3 skrll t->p_sigctx.ps_info._signo))
976 1.5.2.2 skrll pl.pl_event = PL_EVENT_SIGNAL;
977 1.5.2.2 skrll }
978 1.5.2.2 skrll mutex_exit(t->p_lock);
979 1.5.2.2 skrll
980 1.5.2.2 skrll error = copyout(&pl, addr, sizeof(pl));
981 1.5.2.2 skrll break;
982 1.5.2.2 skrll
983 1.5.2.3 skrll case PT_SET_SIGINFO:
984 1.5.2.3 skrll if (data != sizeof(psi)) {
985 1.5.2.3 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
986 1.5.2.3 skrll sizeof(psi)));
987 1.5.2.3 skrll error = EINVAL;
988 1.5.2.3 skrll break;
989 1.5.2.3 skrll }
990 1.5.2.3 skrll
991 1.5.2.3 skrll error = copyin(addr, &psi, sizeof(psi));
992 1.5.2.3 skrll if (error)
993 1.5.2.3 skrll break;
994 1.5.2.3 skrll
995 1.5.2.3 skrll /* Check that the data is a valid signal number or zero. */
996 1.5.2.3 skrll if (psi.psi_siginfo.si_signo < 0 ||
997 1.5.2.3 skrll psi.psi_siginfo.si_signo >= NSIG) {
998 1.5.2.3 skrll error = EINVAL;
999 1.5.2.3 skrll break;
1000 1.5.2.3 skrll }
1001 1.5.2.3 skrll
1002 1.5.2.3 skrll tmp = psi.psi_lwpid;
1003 1.5.2.3 skrll if (tmp != 0)
1004 1.5.2.3 skrll lwp_delref(lt);
1005 1.5.2.3 skrll
1006 1.5.2.3 skrll mutex_enter(t->p_lock);
1007 1.5.2.3 skrll
1008 1.5.2.3 skrll if (tmp != 0) {
1009 1.5.2.3 skrll lt = lwp_find(t, tmp);
1010 1.5.2.3 skrll if (lt == NULL) {
1011 1.5.2.3 skrll mutex_exit(t->p_lock);
1012 1.5.2.3 skrll error = ESRCH;
1013 1.5.2.3 skrll break;
1014 1.5.2.3 skrll }
1015 1.5.2.3 skrll lwp_addref(lt);
1016 1.5.2.3 skrll }
1017 1.5.2.3 skrll
1018 1.5.2.3 skrll t->p_sigctx.ps_faked = true;
1019 1.5.2.3 skrll t->p_sigctx.ps_info = psi.psi_siginfo._info;
1020 1.5.2.3 skrll t->p_sigctx.ps_lwp = psi.psi_lwpid;
1021 1.5.2.3 skrll mutex_exit(t->p_lock);
1022 1.5.2.3 skrll break;
1023 1.5.2.3 skrll
1024 1.5.2.3 skrll case PT_GET_SIGINFO:
1025 1.5.2.3 skrll if (data != sizeof(psi)) {
1026 1.5.2.3 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req, data,
1027 1.5.2.3 skrll sizeof(psi)));
1028 1.5.2.3 skrll error = EINVAL;
1029 1.5.2.3 skrll break;
1030 1.5.2.3 skrll }
1031 1.5.2.3 skrll mutex_enter(t->p_lock);
1032 1.5.2.3 skrll psi.psi_siginfo._info = t->p_sigctx.ps_info;
1033 1.5.2.3 skrll psi.psi_lwpid = t->p_sigctx.ps_lwp;
1034 1.5.2.3 skrll mutex_exit(t->p_lock);
1035 1.5.2.3 skrll
1036 1.5.2.3 skrll error = copyout(&psi, addr, sizeof(psi));
1037 1.5.2.3 skrll if (error)
1038 1.5.2.3 skrll break;
1039 1.5.2.3 skrll
1040 1.5.2.3 skrll break;
1041 1.5.2.3 skrll
1042 1.5.2.2 skrll #ifdef PT_SETREGS
1043 1.5.2.2 skrll case PT_SETREGS:
1044 1.5.2.2 skrll write = 1;
1045 1.5.2.2 skrll #endif
1046 1.5.2.2 skrll #ifdef PT_GETREGS
1047 1.5.2.2 skrll case PT_GETREGS:
1048 1.5.2.2 skrll /* write = 0 done above. */
1049 1.5.2.2 skrll #endif
1050 1.5.2.2 skrll #if defined(PT_SETREGS) || defined(PT_GETREGS)
1051 1.5.2.2 skrll tmp = data;
1052 1.5.2.2 skrll if (tmp != 0 && t->p_nlwps > 1) {
1053 1.5.2.2 skrll lwp_delref(lt);
1054 1.5.2.2 skrll mutex_enter(t->p_lock);
1055 1.5.2.2 skrll lt = lwp_find(t, tmp);
1056 1.5.2.2 skrll if (lt == NULL) {
1057 1.5.2.2 skrll mutex_exit(t->p_lock);
1058 1.5.2.2 skrll error = ESRCH;
1059 1.5.2.2 skrll break;
1060 1.5.2.2 skrll }
1061 1.5.2.2 skrll lwp_addref(lt);
1062 1.5.2.2 skrll mutex_exit(t->p_lock);
1063 1.5.2.2 skrll }
1064 1.5.2.2 skrll if (!process_validregs(lt))
1065 1.5.2.2 skrll error = EINVAL;
1066 1.5.2.2 skrll else {
1067 1.5.2.2 skrll error = proc_vmspace_getref(p, &vm);
1068 1.5.2.2 skrll if (error)
1069 1.5.2.2 skrll break;
1070 1.5.2.2 skrll iov.iov_base = addr;
1071 1.5.2.2 skrll iov.iov_len = PROC_REGSZ(p);
1072 1.5.2.2 skrll uio.uio_iov = &iov;
1073 1.5.2.2 skrll uio.uio_iovcnt = 1;
1074 1.5.2.2 skrll uio.uio_offset = 0;
1075 1.5.2.2 skrll uio.uio_resid = iov.iov_len;
1076 1.5.2.2 skrll uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1077 1.5.2.2 skrll uio.uio_vmspace = vm;
1078 1.5.2.2 skrll
1079 1.5.2.2 skrll error = ptm->ptm_doregs(l, lt, &uio);
1080 1.5.2.2 skrll uvmspace_free(vm);
1081 1.5.2.2 skrll }
1082 1.5.2.2 skrll break;
1083 1.5.2.2 skrll #endif
1084 1.5.2.2 skrll
1085 1.5.2.2 skrll #ifdef PT_SETFPREGS
1086 1.5.2.2 skrll case PT_SETFPREGS:
1087 1.5.2.2 skrll write = 1;
1088 1.5.2.2 skrll /*FALLTHROUGH*/
1089 1.5.2.2 skrll #endif
1090 1.5.2.2 skrll #ifdef PT_GETFPREGS
1091 1.5.2.2 skrll case PT_GETFPREGS:
1092 1.5.2.2 skrll /* write = 0 done above. */
1093 1.5.2.2 skrll #endif
1094 1.5.2.2 skrll #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1095 1.5.2.2 skrll tmp = data;
1096 1.5.2.2 skrll if (tmp != 0 && t->p_nlwps > 1) {
1097 1.5.2.2 skrll lwp_delref(lt);
1098 1.5.2.2 skrll mutex_enter(t->p_lock);
1099 1.5.2.2 skrll lt = lwp_find(t, tmp);
1100 1.5.2.2 skrll if (lt == NULL) {
1101 1.5.2.2 skrll mutex_exit(t->p_lock);
1102 1.5.2.2 skrll error = ESRCH;
1103 1.5.2.2 skrll break;
1104 1.5.2.2 skrll }
1105 1.5.2.2 skrll lwp_addref(lt);
1106 1.5.2.2 skrll mutex_exit(t->p_lock);
1107 1.5.2.2 skrll }
1108 1.5.2.2 skrll if (!process_validfpregs(lt))
1109 1.5.2.2 skrll error = EINVAL;
1110 1.5.2.2 skrll else {
1111 1.5.2.2 skrll error = proc_vmspace_getref(p, &vm);
1112 1.5.2.2 skrll if (error)
1113 1.5.2.2 skrll break;
1114 1.5.2.2 skrll iov.iov_base = addr;
1115 1.5.2.2 skrll iov.iov_len = PROC_FPREGSZ(p);
1116 1.5.2.2 skrll uio.uio_iov = &iov;
1117 1.5.2.2 skrll uio.uio_iovcnt = 1;
1118 1.5.2.2 skrll uio.uio_offset = 0;
1119 1.5.2.2 skrll uio.uio_resid = iov.iov_len;
1120 1.5.2.2 skrll uio.uio_rw = write ? UIO_WRITE : UIO_READ;
1121 1.5.2.2 skrll uio.uio_vmspace = vm;
1122 1.5.2.2 skrll
1123 1.5.2.2 skrll error = ptm->ptm_dofpregs(l, lt, &uio);
1124 1.5.2.2 skrll uvmspace_free(vm);
1125 1.5.2.2 skrll }
1126 1.5.2.2 skrll break;
1127 1.5.2.2 skrll #endif
1128 1.5.2.2 skrll
1129 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
1130 1.5.2.3 skrll /*
1131 1.5.2.3 skrll * The "write" variable is used as type of operation.
1132 1.5.2.3 skrll * Possible values:
1133 1.5.2.3 skrll * 0 - return the number of supported hardware watchpoints
1134 1.5.2.3 skrll * 1 - set new watchpoint value
1135 1.5.2.3 skrll * 2 - get existing watchpoint image
1136 1.5.2.3 skrll */
1137 1.5.2.3 skrll case PT_WRITE_WATCHPOINT:
1138 1.5.2.3 skrll write = 1;
1139 1.5.2.3 skrll case PT_READ_WATCHPOINT:
1140 1.5.2.3 skrll /* write = 0 done above */
1141 1.5.2.3 skrll
1142 1.5.2.3 skrll if (data != sizeof(pw)) {
1143 1.5.2.3 skrll DPRINTF(("ptrace(%d): %d != %zu\n", req,
1144 1.5.2.3 skrll data, sizeof(pe)));
1145 1.5.2.3 skrll error = EINVAL;
1146 1.5.2.3 skrll break;
1147 1.5.2.3 skrll }
1148 1.5.2.3 skrll error = copyin(addr, &pw, sizeof(pw));
1149 1.5.2.3 skrll if (error)
1150 1.5.2.3 skrll break;
1151 1.5.2.3 skrll tmp = pw.pw_lwpid;
1152 1.5.2.3 skrll if (tmp != 0 && t->p_nlwps > 1) {
1153 1.5.2.3 skrll lwp_delref(lt);
1154 1.5.2.3 skrll mutex_enter(t->p_lock);
1155 1.5.2.3 skrll lt = lwp_find(t, tmp);
1156 1.5.2.3 skrll if (lt == NULL) {
1157 1.5.2.3 skrll mutex_exit(t->p_lock);
1158 1.5.2.3 skrll error = ESRCH;
1159 1.5.2.3 skrll break;
1160 1.5.2.3 skrll }
1161 1.5.2.3 skrll lwp_addref(lt);
1162 1.5.2.3 skrll mutex_exit(t->p_lock);
1163 1.5.2.3 skrll }
1164 1.5.2.3 skrll ++write;
1165 1.5.2.3 skrll case PT_COUNT_WATCHPOINTS:
1166 1.5.2.3 skrll if (!process_validwatchpoint(lt))
1167 1.5.2.3 skrll error = EINVAL;
1168 1.5.2.3 skrll else {
1169 1.5.2.3 skrll lwp_lock(lt);
1170 1.5.2.3 skrll error = ptm->ptm_dowatchpoint(l, lt, write, &pw, addr,
1171 1.5.2.3 skrll retval);
1172 1.5.2.3 skrll lwp_unlock(lt);
1173 1.5.2.3 skrll }
1174 1.5.2.3 skrll break;
1175 1.5.2.3 skrll #endif
1176 1.5.2.3 skrll
1177 1.5.2.2 skrll #ifdef __HAVE_PTRACE_MACHDEP
1178 1.5.2.2 skrll PTRACE_MACHDEP_REQUEST_CASES
1179 1.5.2.2 skrll error = ptrace_machdep_dorequest(l, lt, req, addr, data);
1180 1.5.2.2 skrll break;
1181 1.5.2.2 skrll #endif
1182 1.5.2.2 skrll }
1183 1.5.2.2 skrll
1184 1.5.2.2 skrll if (pheld) {
1185 1.5.2.2 skrll mutex_exit(t->p_lock);
1186 1.5.2.2 skrll mutex_exit(proc_lock);
1187 1.5.2.2 skrll }
1188 1.5.2.2 skrll if (lt != NULL)
1189 1.5.2.2 skrll lwp_delref(lt);
1190 1.5.2.2 skrll rw_exit(&t->p_reflock);
1191 1.5.2.2 skrll
1192 1.5.2.2 skrll return error;
1193 1.5.2.2 skrll }
1194 1.5.2.2 skrll
1195 1.5.2.2 skrll int
1196 1.5.2.2 skrll process_doregs(struct lwp *curl /*tracer*/,
1197 1.5.2.2 skrll struct lwp *l /*traced*/,
1198 1.5.2.2 skrll struct uio *uio)
1199 1.5.2.2 skrll {
1200 1.5.2.2 skrll #if defined(PT_GETREGS) || defined(PT_SETREGS)
1201 1.5.2.2 skrll int error;
1202 1.5.2.2 skrll struct reg r;
1203 1.5.2.2 skrll char *kv;
1204 1.5.2.2 skrll int kl;
1205 1.5.2.2 skrll
1206 1.5.2.2 skrll if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1207 1.5.2.2 skrll return EINVAL;
1208 1.5.2.2 skrll
1209 1.5.2.2 skrll kl = sizeof(r);
1210 1.5.2.2 skrll kv = (char *)&r;
1211 1.5.2.2 skrll
1212 1.5.2.2 skrll kv += uio->uio_offset;
1213 1.5.2.2 skrll kl -= uio->uio_offset;
1214 1.5.2.2 skrll if ((size_t)kl > uio->uio_resid)
1215 1.5.2.2 skrll kl = uio->uio_resid;
1216 1.5.2.2 skrll
1217 1.5.2.2 skrll error = process_read_regs(l, &r);
1218 1.5.2.2 skrll if (error == 0)
1219 1.5.2.2 skrll error = uiomove(kv, kl, uio);
1220 1.5.2.2 skrll if (error == 0 && uio->uio_rw == UIO_WRITE) {
1221 1.5.2.2 skrll if (l->l_stat != LSSTOP)
1222 1.5.2.2 skrll error = EBUSY;
1223 1.5.2.2 skrll else
1224 1.5.2.2 skrll error = process_write_regs(l, &r);
1225 1.5.2.2 skrll }
1226 1.5.2.2 skrll
1227 1.5.2.2 skrll uio->uio_offset = 0;
1228 1.5.2.2 skrll return error;
1229 1.5.2.2 skrll #else
1230 1.5.2.2 skrll return EINVAL;
1231 1.5.2.2 skrll #endif
1232 1.5.2.2 skrll }
1233 1.5.2.2 skrll
1234 1.5.2.2 skrll int
1235 1.5.2.2 skrll process_validregs(struct lwp *l)
1236 1.5.2.2 skrll {
1237 1.5.2.2 skrll
1238 1.5.2.2 skrll #if defined(PT_SETREGS) || defined(PT_GETREGS)
1239 1.5.2.2 skrll return (l->l_flag & LW_SYSTEM) == 0;
1240 1.5.2.2 skrll #else
1241 1.5.2.2 skrll return 0;
1242 1.5.2.2 skrll #endif
1243 1.5.2.2 skrll }
1244 1.5.2.2 skrll
1245 1.5.2.2 skrll int
1246 1.5.2.2 skrll process_dofpregs(struct lwp *curl /*tracer*/,
1247 1.5.2.2 skrll struct lwp *l /*traced*/,
1248 1.5.2.2 skrll struct uio *uio)
1249 1.5.2.2 skrll {
1250 1.5.2.2 skrll #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
1251 1.5.2.2 skrll int error;
1252 1.5.2.2 skrll struct fpreg r;
1253 1.5.2.2 skrll char *kv;
1254 1.5.2.2 skrll size_t kl;
1255 1.5.2.2 skrll
1256 1.5.2.2 skrll if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
1257 1.5.2.2 skrll return EINVAL;
1258 1.5.2.2 skrll
1259 1.5.2.2 skrll kl = sizeof(r);
1260 1.5.2.2 skrll kv = (char *)&r;
1261 1.5.2.2 skrll
1262 1.5.2.2 skrll kv += uio->uio_offset;
1263 1.5.2.2 skrll kl -= uio->uio_offset;
1264 1.5.2.2 skrll if (kl > uio->uio_resid)
1265 1.5.2.2 skrll kl = uio->uio_resid;
1266 1.5.2.2 skrll
1267 1.5.2.2 skrll error = process_read_fpregs(l, &r, &kl);
1268 1.5.2.2 skrll if (error == 0)
1269 1.5.2.2 skrll error = uiomove(kv, kl, uio);
1270 1.5.2.2 skrll if (error == 0 && uio->uio_rw == UIO_WRITE) {
1271 1.5.2.2 skrll if (l->l_stat != LSSTOP)
1272 1.5.2.2 skrll error = EBUSY;
1273 1.5.2.2 skrll else
1274 1.5.2.2 skrll error = process_write_fpregs(l, &r, kl);
1275 1.5.2.2 skrll }
1276 1.5.2.2 skrll uio->uio_offset = 0;
1277 1.5.2.2 skrll return error;
1278 1.5.2.2 skrll #else
1279 1.5.2.2 skrll return EINVAL;
1280 1.5.2.2 skrll #endif
1281 1.5.2.2 skrll }
1282 1.5.2.2 skrll
1283 1.5.2.2 skrll int
1284 1.5.2.2 skrll process_validfpregs(struct lwp *l)
1285 1.5.2.2 skrll {
1286 1.5.2.2 skrll
1287 1.5.2.2 skrll #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
1288 1.5.2.2 skrll return (l->l_flag & LW_SYSTEM) == 0;
1289 1.5.2.2 skrll #else
1290 1.5.2.2 skrll return 0;
1291 1.5.2.2 skrll #endif
1292 1.5.2.2 skrll }
1293 1.5.2.2 skrll
1294 1.5.2.2 skrll static int
1295 1.5.2.2 skrll process_auxv_offset(struct proc *p, struct uio *uio)
1296 1.5.2.2 skrll {
1297 1.5.2.2 skrll struct ps_strings pss;
1298 1.5.2.2 skrll int error;
1299 1.5.2.2 skrll off_t off = (off_t)p->p_psstrp;
1300 1.5.2.2 skrll
1301 1.5.2.2 skrll if ((error = copyin_psstrings(p, &pss)) != 0)
1302 1.5.2.2 skrll return error;
1303 1.5.2.2 skrll
1304 1.5.2.2 skrll if (pss.ps_envstr == NULL)
1305 1.5.2.2 skrll return EIO;
1306 1.5.2.2 skrll
1307 1.5.2.2 skrll uio->uio_offset += (off_t)(vaddr_t)(pss.ps_envstr + pss.ps_nenvstr + 1);
1308 1.5.2.2 skrll #ifdef __MACHINE_STACK_GROWS_UP
1309 1.5.2.2 skrll if (uio->uio_offset < off)
1310 1.5.2.2 skrll return EIO;
1311 1.5.2.2 skrll #else
1312 1.5.2.2 skrll if (uio->uio_offset > off)
1313 1.5.2.2 skrll return EIO;
1314 1.5.2.2 skrll if ((uio->uio_offset + uio->uio_resid) > off)
1315 1.5.2.2 skrll uio->uio_resid = off - uio->uio_offset;
1316 1.5.2.2 skrll #endif
1317 1.5.2.2 skrll return 0;
1318 1.5.2.2 skrll }
1319 1.5.2.3 skrll
1320 1.5.2.3 skrll int
1321 1.5.2.3 skrll process_dowatchpoint(struct lwp *curl /*tracer*/, struct lwp *l /*traced*/,
1322 1.5.2.3 skrll int operation, struct ptrace_watchpoint *pw, void *addr,
1323 1.5.2.3 skrll register_t *retval)
1324 1.5.2.3 skrll {
1325 1.5.2.3 skrll
1326 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
1327 1.5.2.3 skrll int error;
1328 1.5.2.3 skrll
1329 1.5.2.3 skrll KASSERT(operation >= 0);
1330 1.5.2.3 skrll KASSERT(operation <= 2);
1331 1.5.2.3 skrll
1332 1.5.2.3 skrll switch (operation) {
1333 1.5.2.3 skrll case 0:
1334 1.5.2.3 skrll return process_count_watchpoints(l, retval);
1335 1.5.2.3 skrll case 1:
1336 1.5.2.3 skrll error = process_read_watchpoint(l, pw);
1337 1.5.2.3 skrll if (error)
1338 1.5.2.3 skrll return error;
1339 1.5.2.3 skrll return copyout(pw, addr, sizeof(*pw));
1340 1.5.2.3 skrll default:
1341 1.5.2.3 skrll return process_write_watchpoint(l, pw);
1342 1.5.2.3 skrll }
1343 1.5.2.3 skrll #else
1344 1.5.2.3 skrll return EINVAL;
1345 1.5.2.3 skrll #endif
1346 1.5.2.3 skrll }
1347 1.5.2.3 skrll
1348 1.5.2.3 skrll int
1349 1.5.2.3 skrll process_validwatchpoint(struct lwp *l)
1350 1.5.2.3 skrll {
1351 1.5.2.3 skrll
1352 1.5.2.3 skrll #ifdef __HAVE_PTRACE_WATCHPOINTS
1353 1.5.2.3 skrll return (l->l_flag & LW_SYSTEM) == 0;
1354 1.5.2.3 skrll #else
1355 1.5.2.3 skrll return 0;
1356 1.5.2.3 skrll #endif
1357 1.5.2.3 skrll }
1358 1.5.2.2 skrll #endif /* PTRACE */
1359 1.5.2.2 skrll
1360 1.5.2.2 skrll MODULE(MODULE_CLASS_EXEC, ptrace_common, "");
1361 1.5.2.3 skrll
1362 1.5.2.2 skrll static int
1363 1.5.2.2 skrll ptrace_common_modcmd(modcmd_t cmd, void *arg)
1364 1.5.2.2 skrll {
1365 1.5.2.2 skrll int error;
1366 1.5.2.3 skrll
1367 1.5.2.2 skrll switch (cmd) {
1368 1.5.2.2 skrll case MODULE_CMD_INIT:
1369 1.5.2.2 skrll error = ptrace_init();
1370 1.5.2.2 skrll break;
1371 1.5.2.2 skrll case MODULE_CMD_FINI:
1372 1.5.2.2 skrll error = ptrace_fini();
1373 1.5.2.2 skrll break;
1374 1.5.2.2 skrll default:
1375 1.5.2.2 skrll ptrace_hooks();
1376 1.5.2.2 skrll error = ENOTTY;
1377 1.5.2.2 skrll break;
1378 1.5.2.2 skrll }
1379 1.5.2.2 skrll return error;
1380 1.5.2.2 skrll }
1381