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