xref: /netbsd-src/sys/kern/sys_process.c (revision abb0f93cd77b67f080613360c65701f85e5f5cfe)
1 /*	$NetBSD: sys_process.c,v 1.151 2009/10/21 21:12:06 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1989, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * This code is derived from software contributed to Berkeley by
42  * Jan-Simon Pendry.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
69  */
70 
71 /*-
72  * Copyright (c) 1993 Jan-Simon Pendry.
73  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
74  *
75  * This code is derived from software contributed to Berkeley by
76  * Jan-Simon Pendry.
77  *
78  * Redistribution and use in source and binary forms, with or without
79  * modification, are permitted provided that the following conditions
80  * are met:
81  * 1. Redistributions of source code must retain the above copyright
82  *    notice, this list of conditions and the following disclaimer.
83  * 2. Redistributions in binary form must reproduce the above copyright
84  *    notice, this list of conditions and the following disclaimer in the
85  *    documentation and/or other materials provided with the distribution.
86  * 3. All advertising materials mentioning features or use of this software
87  *    must display the following acknowledgement:
88  *	This product includes software developed by the University of
89  *	California, Berkeley and its contributors.
90  * 4. Neither the name of the University nor the names of its contributors
91  *    may be used to endorse or promote products derived from this software
92  *    without specific prior written permission.
93  *
94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104  * SUCH DAMAGE.
105  *
106  *	from: @(#)sys_process.c	8.1 (Berkeley) 6/10/93
107  */
108 
109 /*
110  * References:
111  *	(1) Bach's "The Design of the UNIX Operating System",
112  *	(2) sys/miscfs/procfs from UCB's 4.4BSD-Lite distribution,
113  *	(3) the "4.4BSD Programmer's Reference Manual" published
114  *		by USENIX and O'Reilly & Associates.
115  * The 4.4BSD PRM does a reasonably good job of documenting what the various
116  * ptrace() requests should actually do, and its text is quoted several times
117  * in this file.
118  */
119 
120 #include <sys/cdefs.h>
121 __KERNEL_RCSID(0, "$NetBSD: sys_process.c,v 1.151 2009/10/21 21:12:06 rmind Exp $");
122 
123 #include "opt_ptrace.h"
124 #include "opt_ktrace.h"
125 
126 #include <sys/param.h>
127 #include <sys/systm.h>
128 #include <sys/proc.h>
129 #include <sys/errno.h>
130 #include <sys/ptrace.h>
131 #include <sys/uio.h>
132 #include <sys/user.h>
133 #include <sys/ras.h>
134 #include <sys/kmem.h>
135 #include <sys/kauth.h>
136 #include <sys/mount.h>
137 #include <sys/syscallargs.h>
138 
139 #include <uvm/uvm_extern.h>
140 
141 #include <machine/reg.h>
142 
143 #ifdef PTRACE
144 static kauth_listener_t ptrace_listener;
145 
146 static int
147 ptrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
148     void *arg0, void *arg1, void *arg2, void *arg3)
149 {
150 	struct proc *p;
151 	int result;
152 
153 	result = KAUTH_RESULT_DEFER;
154 	p = arg0;
155 
156 	if (action != KAUTH_PROCESS_PTRACE)
157 		return result;
158 
159 	switch ((u_long)arg1) {
160 	case PT_TRACE_ME:
161 	case PT_ATTACH:
162 	case PT_WRITE_I:
163 	case PT_WRITE_D:
164 	case PT_READ_I:
165 	case PT_READ_D:
166 	case PT_IO:
167 #ifdef PT_GETREGS
168 	case PT_GETREGS:
169 #endif
170 #ifdef PT_SETREGS
171 	case PT_SETREGS:
172 #endif
173 #ifdef PT_GETFPREGS
174 	case PT_GETFPREGS:
175 #endif
176 #ifdef PT_SETFPREGS
177 	case PT_SETFPREGS:
178 #endif
179 #ifdef __HAVE_PTRACE_MACHDEP
180 	PTRACE_MACHDEP_REQUEST_CASES
181 #endif
182 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
183 		    ISSET(p->p_flag, PK_SUGID)) {
184 			break;
185 		}
186 
187 		result = KAUTH_RESULT_ALLOW;
188 
189 	break;
190 
191 #ifdef PT_STEP
192 	case PT_STEP:
193 #endif
194 	case PT_CONTINUE:
195 	case PT_KILL:
196 	case PT_DETACH:
197 	case PT_LWPINFO:
198 	case PT_SYSCALL:
199 	case PT_DUMPCORE:
200 		result = KAUTH_RESULT_ALLOW;
201 		break;
202 
203 	default:
204 		break;
205 	}
206 
207 	return result;
208 }
209 
210 void
211 ptrace_init(void)
212 {
213 
214 	ptrace_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
215 	    ptrace_listener_cb, NULL);
216 }
217 
218 /*
219  * Process debugging system call.
220  */
221 int
222 sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
223 {
224 	/* {
225 		syscallarg(int) req;
226 		syscallarg(pid_t) pid;
227 		syscallarg(void *) addr;
228 		syscallarg(int) data;
229 	} */
230 	struct proc *p = l->l_proc;
231 	struct lwp *lt;
232 	struct proc *t;				/* target process */
233 	struct uio uio;
234 	struct iovec iov;
235 	struct ptrace_io_desc piod;
236 	struct ptrace_lwpinfo pl;
237 	struct vmspace *vm;
238 	int error, write, tmp, req, pheld;
239 	int signo;
240 	ksiginfo_t ksi;
241 	char *path;
242 	int len;
243 
244 	error = 0;
245 	req = SCARG(uap, req);
246 
247 	/*
248 	 * If attaching or detaching, we need to get a write hold on the
249 	 * proclist lock so that we can re-parent the target process.
250 	 */
251 	mutex_enter(proc_lock);
252 
253 	/* "A foolish consistency..." XXX */
254 	if (req == PT_TRACE_ME) {
255 		t = p;
256 		mutex_enter(t->p_lock);
257 	} else {
258 		/* Find the process we're supposed to be operating on. */
259 		if ((t = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
260 			mutex_exit(proc_lock);
261 			return (ESRCH);
262 		}
263 
264 		/* XXX-elad */
265 		mutex_enter(t->p_lock);
266 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
267 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
268 		if (error) {
269 			mutex_exit(proc_lock);
270 			mutex_exit(t->p_lock);
271 			return (ESRCH);
272 		}
273 	}
274 
275 	/*
276 	 * Grab a reference on the process to prevent it from execing or
277 	 * exiting.
278 	 */
279 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
280 		mutex_exit(proc_lock);
281 		mutex_exit(t->p_lock);
282 		return EBUSY;
283 	}
284 
285 	/* Make sure we can operate on it. */
286 	switch (req) {
287 	case  PT_TRACE_ME:
288 		/* Saying that you're being traced is always legal. */
289 		break;
290 
291 	case  PT_ATTACH:
292 		/*
293 		 * You can't attach to a process if:
294 		 *	(1) it's the process that's doing the attaching,
295 		 */
296 		if (t->p_pid == p->p_pid) {
297 			error = EINVAL;
298 			break;
299 		}
300 
301 		/*
302 		 *  (2) it's a system process
303 		 */
304 		if (t->p_flag & PK_SYSTEM) {
305 			error = EPERM;
306 			break;
307 		}
308 
309 		/*
310 		 *	(3) it's already being traced, or
311 		 */
312 		if (ISSET(t->p_slflag, PSL_TRACED)) {
313 			error = EBUSY;
314 			break;
315 		}
316 
317 		/*
318 		 * 	(4) the tracer is chrooted, and its root directory is
319 		 * 	    not at or above the root directory of the tracee
320 		 */
321 		mutex_exit(t->p_lock);	/* XXXSMP */
322 		tmp = proc_isunder(t, l);
323 		mutex_enter(t->p_lock);	/* XXXSMP */
324 		if (!tmp) {
325 			error = EPERM;
326 			break;
327 		}
328 		break;
329 
330 	case  PT_READ_I:
331 	case  PT_READ_D:
332 	case  PT_WRITE_I:
333 	case  PT_WRITE_D:
334 	case  PT_IO:
335 #ifdef PT_GETREGS
336 	case  PT_GETREGS:
337 #endif
338 #ifdef PT_SETREGS
339 	case  PT_SETREGS:
340 #endif
341 #ifdef PT_GETFPREGS
342 	case  PT_GETFPREGS:
343 #endif
344 #ifdef PT_SETFPREGS
345 	case  PT_SETFPREGS:
346 #endif
347 #ifdef __HAVE_PTRACE_MACHDEP
348 	PTRACE_MACHDEP_REQUEST_CASES
349 #endif
350 		/*
351 		 * You can't read/write the memory or registers of a process
352 		 * if the tracer is chrooted, and its root directory is not at
353 		 * or above the root directory of the tracee.
354 		 */
355 		mutex_exit(t->p_lock);	/* XXXSMP */
356 		tmp = proc_isunder(t, l);
357 		mutex_enter(t->p_lock);	/* XXXSMP */
358 		if (!tmp) {
359 			error = EPERM;
360 			break;
361 		}
362 		/*FALLTHROUGH*/
363 
364 	case  PT_CONTINUE:
365 	case  PT_KILL:
366 	case  PT_DETACH:
367 	case  PT_LWPINFO:
368 	case  PT_SYSCALL:
369 	case  PT_DUMPCORE:
370 #ifdef PT_STEP
371 	case  PT_STEP:
372 #endif
373 		/*
374 		 * You can't do what you want to the process if:
375 		 *	(1) It's not being traced at all,
376 		 */
377 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
378 			error = EPERM;
379 			break;
380 		}
381 
382 		/*
383 		 *	(2) it's being traced by procfs (which has
384 		 *	    different signal delivery semantics),
385 		 */
386 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
387 			uprintf("file system traced\n");
388 			error = EBUSY;
389 			break;
390 		}
391 
392 		/*
393 		 *	(3) it's not being traced by _you_, or
394 		 */
395 		if (t->p_pptr != p) {
396 			uprintf("parent %d != %d\n", t->p_pptr->p_pid,
397 			    p->p_pid);
398 			error = EBUSY;
399 			break;
400 		}
401 
402 		/*
403 		 *	(4) it's not currently stopped.
404 		 */
405 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
406 			uprintf("stat %d flag %d\n", t->p_stat,
407 			    !t->p_waited);
408 			error = EBUSY;
409 			break;
410 		}
411 		break;
412 
413 	default:			/* It was not a legal request. */
414 		error = EINVAL;
415 		break;
416 	}
417 
418 	if (error == 0) {
419 		error = kauth_authorize_process(l->l_cred,
420 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
421 		    NULL, NULL);
422 	}
423 	if (error == 0) {
424 		lt = lwp_find_first(t);
425 		if (lt == NULL)
426 			error = ESRCH;
427 	}
428 
429 	if (error != 0) {
430 		mutex_exit(proc_lock);
431 		mutex_exit(t->p_lock);
432 		rw_exit(&t->p_reflock);
433 		return error;
434 	}
435 
436 	/* Do single-step fixup if needed. */
437 	FIX_SSTEP(t);
438 	KASSERT(lt != NULL);
439 	lwp_addref(lt);
440 
441 	/*
442 	 * Which locks do we need held? XXX Ugly.
443 	 */
444 	switch (req) {
445 #ifdef PT_STEP
446 	case PT_STEP:
447 #endif
448 	case PT_CONTINUE:
449 	case PT_DETACH:
450 	case PT_KILL:
451 	case PT_SYSCALL:
452 	case PT_ATTACH:
453 	case PT_TRACE_ME:
454 		pheld = 1;
455 		break;
456 	default:
457 		mutex_exit(proc_lock);
458 		mutex_exit(t->p_lock);
459 		pheld = 0;
460 		break;
461 	}
462 
463 	/* Now do the operation. */
464 	write = 0;
465 	*retval = 0;
466 	tmp = 0;
467 
468 	switch (req) {
469 	case  PT_TRACE_ME:
470 		/* Just set the trace flag. */
471 		SET(t->p_slflag, PSL_TRACED);
472 		t->p_opptr = t->p_pptr;
473 		break;
474 
475 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
476 	case  PT_WRITE_D:
477 #if defined(__HAVE_RAS)
478 		/*
479 		 * Can't write to a RAS
480 		 */
481 		if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
482 			error = EACCES;
483 			break;
484 		}
485 #endif
486 		write = 1;
487 		tmp = SCARG(uap, data);
488 		/* FALLTHROUGH */
489 
490 	case  PT_READ_I:		/* XXX no separate I and D spaces */
491 	case  PT_READ_D:
492 		/* write = 0 done above. */
493 		iov.iov_base = (void *)&tmp;
494 		iov.iov_len = sizeof(tmp);
495 		uio.uio_iov = &iov;
496 		uio.uio_iovcnt = 1;
497 		uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
498 		uio.uio_resid = sizeof(tmp);
499 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
500 		UIO_SETUP_SYSSPACE(&uio);
501 
502 		error = process_domem(l, lt, &uio);
503 		if (!write)
504 			*retval = tmp;
505 		break;
506 
507 	case  PT_IO:
508 		error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
509 		if (error)
510 			break;
511 		switch (piod.piod_op) {
512 		case PIOD_READ_D:
513 		case PIOD_READ_I:
514 			uio.uio_rw = UIO_READ;
515 			break;
516 		case PIOD_WRITE_D:
517 		case PIOD_WRITE_I:
518 			/*
519 			 * Can't write to a RAS
520 			 */
521 			if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
522 				return (EACCES);
523 			}
524 			uio.uio_rw = UIO_WRITE;
525 			break;
526 		default:
527 			error = EINVAL;
528 			break;
529 		}
530 		if (error)
531 			break;
532 		error = proc_vmspace_getref(l->l_proc, &vm);
533 		if (error)
534 			break;
535 		iov.iov_base = piod.piod_addr;
536 		iov.iov_len = piod.piod_len;
537 		uio.uio_iov = &iov;
538 		uio.uio_iovcnt = 1;
539 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
540 		uio.uio_resid = piod.piod_len;
541 		uio.uio_vmspace = vm;
542 
543 		error = process_domem(l, lt, &uio);
544 		piod.piod_len -= uio.uio_resid;
545 		(void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
546 		uvmspace_free(vm);
547 		break;
548 
549 	case  PT_DUMPCORE:
550 		if ((path = SCARG(uap, addr)) != NULL) {
551 			char *dst;
552 			len = SCARG(uap, data);
553 
554 			if (len < 0 || len >= MAXPATHLEN) {
555 				error = EINVAL;
556 				break;
557 			}
558 			dst = kmem_alloc(len + 1, KM_SLEEP);
559 			if ((error = copyin(path, dst, len)) != 0) {
560 				kmem_free(dst, len + 1);
561 				break;
562 			}
563 			path = dst;
564 			path[len] = '\0';
565 		}
566 		error = (*coredump_vec)(lt, path);
567 		if (path)
568 			kmem_free(path, len + 1);
569 		break;
570 
571 #ifdef PT_STEP
572 	case  PT_STEP:
573 		/*
574 		 * From the 4.4BSD PRM:
575 		 * "Execution continues as in request PT_CONTINUE; however
576 		 * as soon as possible after execution of at least one
577 		 * instruction, execution stops again. [ ... ]"
578 		 */
579 #endif
580 	case  PT_CONTINUE:
581 	case  PT_SYSCALL:
582 	case  PT_DETACH:
583 		if (req == PT_SYSCALL) {
584 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
585 				SET(t->p_slflag, PSL_SYSCALL);
586 #ifdef __HAVE_SYSCALL_INTERN
587 				(*t->p_emul->e_syscall_intern)(t);
588 #endif
589 			}
590 		} else {
591 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
592 				CLR(t->p_slflag, PSL_SYSCALL);
593 #ifdef __HAVE_SYSCALL_INTERN
594 				(*t->p_emul->e_syscall_intern)(t);
595 #endif
596 			}
597 		}
598 		p->p_trace_enabled = trace_is_enabled(p);
599 
600 		/*
601 		 * From the 4.4BSD PRM:
602 		 * "The data argument is taken as a signal number and the
603 		 * child's execution continues at location addr as if it
604 		 * incurred that signal.  Normally the signal number will
605 		 * be either 0 to indicate that the signal that caused the
606 		 * stop should be ignored, or that value fetched out of
607 		 * the process's image indicating which signal caused
608 		 * the stop.  If addr is (int *)1 then execution continues
609 		 * from where it stopped."
610 		 */
611 
612 		/* Check that the data is a valid signal number or zero. */
613 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) {
614 			error = EINVAL;
615 			break;
616 		}
617 
618 		/* If the address parameter is not (int *)1, set the pc. */
619 		if ((int *)SCARG(uap, addr) != (int *)1) {
620 			error = process_set_pc(lt, SCARG(uap, addr));
621 			if (error != 0)
622 				break;
623 		}
624 #ifdef PT_STEP
625 		/*
626 		 * Arrange for a single-step, if that's requested and possible.
627 		 */
628 		error = process_sstep(lt, req == PT_STEP);
629 		if (error)
630 			break;
631 #endif
632 		if (req == PT_DETACH) {
633 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
634 
635 			/* give process back to original parent or init */
636 			if (t->p_opptr != t->p_pptr) {
637 				struct proc *pp = t->p_opptr;
638 				proc_reparent(t, pp ? pp : initproc);
639 			}
640 
641 			/* not being traced any more */
642 			t->p_opptr = NULL;
643 		}
644 
645 		signo = SCARG(uap, data);
646 	sendsig:
647 		/* Finally, deliver the requested signal (or none). */
648 		if (t->p_stat == SSTOP) {
649 			/*
650 			 * Unstop the process.  If it needs to take a
651 			 * signal, make all efforts to ensure that at
652 			 * an LWP runs to see it.
653 			 */
654 			t->p_xstat = signo;
655 			proc_unstop(t);
656 		} else if (signo != 0) {
657 			KSI_INIT_EMPTY(&ksi);
658 			ksi.ksi_signo = signo;
659 			kpsignal2(t, &ksi);
660 		}
661 		break;
662 
663 	case  PT_KILL:
664 		/* just send the process a KILL signal. */
665 		signo = SIGKILL;
666 		goto sendsig;	/* in PT_CONTINUE, above. */
667 
668 	case  PT_ATTACH:
669 		/*
670 		 * Go ahead and set the trace flag.
671 		 * Save the old parent (it's reset in
672 		 *   _DETACH, and also in kern_exit.c:wait4()
673 		 * Reparent the process so that the tracing
674 		 *   proc gets to see all the action.
675 		 * Stop the target.
676 		 */
677 		t->p_opptr = t->p_pptr;
678 		if (t->p_pptr != p) {
679 			struct proc *parent = t->p_pptr;
680 
681 			if (parent->p_lock < t->p_lock) {
682 				if (!mutex_tryenter(parent->p_lock)) {
683 					mutex_exit(t->p_lock);
684 					mutex_enter(parent->p_lock);
685 				}
686 			} else if (parent->p_lock > t->p_lock) {
687 				mutex_enter(parent->p_lock);
688 			}
689 			parent->p_slflag |= PSL_CHTRACED;
690 			proc_reparent(t, p);
691 			if (parent->p_lock != t->p_lock)
692 				mutex_exit(parent->p_lock);
693 		}
694 		SET(t->p_slflag, PSL_TRACED);
695 		signo = SIGSTOP;
696 		goto sendsig;
697 
698 	case PT_LWPINFO:
699 		if (SCARG(uap, data) != sizeof(pl)) {
700 			error = EINVAL;
701 			break;
702 		}
703 		error = copyin(SCARG(uap, addr), &pl, sizeof(pl));
704 		if (error)
705 			break;
706 		tmp = pl.pl_lwpid;
707 		lwp_delref(lt);
708 		mutex_enter(t->p_lock);
709 		if (tmp == 0)
710 			lt = lwp_find_first(t);
711 		else {
712 			lt = lwp_find(t, tmp);
713 			if (lt == NULL) {
714 				mutex_exit(t->p_lock);
715 				error = ESRCH;
716 				break;
717 			}
718 			lt = LIST_NEXT(lt, l_sibling);
719 		}
720 		while (lt != NULL && !lwp_alive(lt))
721 			lt = LIST_NEXT(lt, l_sibling);
722 		pl.pl_lwpid = 0;
723 		pl.pl_event = 0;
724 		if (lt) {
725 			lwp_addref(lt);
726 			pl.pl_lwpid = lt->l_lid;
727 			if (lt->l_lid == t->p_sigctx.ps_lwp)
728 				pl.pl_event = PL_EVENT_SIGNAL;
729 		}
730 		mutex_exit(t->p_lock);
731 
732 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
733 		break;
734 
735 #ifdef PT_SETREGS
736 	case  PT_SETREGS:
737 		write = 1;
738 #endif
739 #ifdef PT_GETREGS
740 	case  PT_GETREGS:
741 		/* write = 0 done above. */
742 #endif
743 #if defined(PT_SETREGS) || defined(PT_GETREGS)
744 		tmp = SCARG(uap, data);
745 		if (tmp != 0 && t->p_nlwps > 1) {
746 			lwp_delref(lt);
747 			mutex_enter(t->p_lock);
748 			lt = lwp_find(t, tmp);
749 			if (lt == NULL) {
750 				mutex_exit(t->p_lock);
751 				error = ESRCH;
752 				break;
753 			}
754 			lwp_addref(lt);
755 			mutex_exit(t->p_lock);
756 		}
757 		if (!process_validregs(lt))
758 			error = EINVAL;
759 		else {
760 			error = proc_vmspace_getref(l->l_proc, &vm);
761 			if (error)
762 				break;
763 			iov.iov_base = SCARG(uap, addr);
764 			iov.iov_len = sizeof(struct reg);
765 			uio.uio_iov = &iov;
766 			uio.uio_iovcnt = 1;
767 			uio.uio_offset = 0;
768 			uio.uio_resid = sizeof(struct reg);
769 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
770 			uio.uio_vmspace = vm;
771 
772 			error = process_doregs(l, lt, &uio);
773 			uvmspace_free(vm);
774 		}
775 		break;
776 #endif
777 
778 #ifdef PT_SETFPREGS
779 	case  PT_SETFPREGS:
780 		write = 1;
781 #endif
782 #ifdef PT_GETFPREGS
783 	case  PT_GETFPREGS:
784 		/* write = 0 done above. */
785 #endif
786 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
787 		tmp = SCARG(uap, data);
788 		if (tmp != 0 && t->p_nlwps > 1) {
789 			lwp_delref(lt);
790 			mutex_enter(t->p_lock);
791 			lt = lwp_find(t, tmp);
792 			if (lt == NULL) {
793 				mutex_exit(t->p_lock);
794 				error = ESRCH;
795 				break;
796 			}
797 			lwp_addref(lt);
798 			mutex_exit(t->p_lock);
799 		}
800 		if (!process_validfpregs(lt))
801 			error = EINVAL;
802 		else {
803 			error = proc_vmspace_getref(l->l_proc, &vm);
804 			if (error)
805 				break;
806 			iov.iov_base = SCARG(uap, addr);
807 			iov.iov_len = sizeof(struct fpreg);
808 			uio.uio_iov = &iov;
809 			uio.uio_iovcnt = 1;
810 			uio.uio_offset = 0;
811 			uio.uio_resid = sizeof(struct fpreg);
812 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
813 			uio.uio_vmspace = vm;
814 
815 			error = process_dofpregs(l, lt, &uio);
816 			uvmspace_free(vm);
817 		}
818 		break;
819 #endif
820 
821 #ifdef __HAVE_PTRACE_MACHDEP
822 	PTRACE_MACHDEP_REQUEST_CASES
823 		error = ptrace_machdep_dorequest(l, lt,
824 		    req, SCARG(uap, addr), SCARG(uap, data));
825 		break;
826 #endif
827 	}
828 
829 	if (pheld) {
830 		mutex_exit(t->p_lock);
831 		mutex_exit(proc_lock);
832 	}
833 	if (lt != NULL)
834 		lwp_delref(lt);
835 	rw_exit(&t->p_reflock);
836 
837 	return error;
838 }
839 
840 int
841 process_doregs(struct lwp *curl /*tracer*/,
842     struct lwp *l /*traced*/,
843     struct uio *uio)
844 {
845 #if defined(PT_GETREGS) || defined(PT_SETREGS)
846 	int error;
847 	struct reg r;
848 	char *kv;
849 	int kl;
850 
851 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
852 		return EINVAL;
853 
854 	kl = sizeof(r);
855 	kv = (char *)&r;
856 
857 	kv += uio->uio_offset;
858 	kl -= uio->uio_offset;
859 	if ((size_t)kl > uio->uio_resid)
860 		kl = uio->uio_resid;
861 
862 	error = process_read_regs(l, &r);
863 	if (error == 0)
864 		error = uiomove(kv, kl, uio);
865 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
866 		if (l->l_stat != LSSTOP)
867 			error = EBUSY;
868 		else
869 			error = process_write_regs(l, &r);
870 	}
871 
872 	uio->uio_offset = 0;
873 	return (error);
874 #else
875 	return (EINVAL);
876 #endif
877 }
878 
879 int
880 process_validregs(struct lwp *l)
881 {
882 
883 #if defined(PT_SETREGS) || defined(PT_GETREGS)
884 	return ((l->l_flag & LW_SYSTEM) == 0);
885 #else
886 	return (0);
887 #endif
888 }
889 
890 int
891 process_dofpregs(struct lwp *curl /*tracer*/,
892     struct lwp *l /*traced*/,
893     struct uio *uio)
894 {
895 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
896 	int error;
897 	struct fpreg r;
898 	char *kv;
899 	int kl;
900 
901 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
902 		return EINVAL;
903 
904 	kl = sizeof(r);
905 	kv = (char *)&r;
906 
907 	kv += uio->uio_offset;
908 	kl -= uio->uio_offset;
909 	if ((size_t)kl > uio->uio_resid)
910 		kl = uio->uio_resid;
911 
912 	error = process_read_fpregs(l, &r);
913 	if (error == 0)
914 		error = uiomove(kv, kl, uio);
915 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
916 		if (l->l_stat != LSSTOP)
917 			error = EBUSY;
918 		else
919 			error = process_write_fpregs(l, &r);
920 	}
921 	uio->uio_offset = 0;
922 	return (error);
923 #else
924 	return (EINVAL);
925 #endif
926 }
927 
928 int
929 process_validfpregs(struct lwp *l)
930 {
931 
932 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
933 	return ((l->l_flag & LW_SYSTEM) == 0);
934 #else
935 	return (0);
936 #endif
937 }
938 #endif /* PTRACE */
939 
940 #if defined(KTRACE) || defined(PTRACE)
941 int
942 process_domem(struct lwp *curl /*tracer*/,
943     struct lwp *l /*traced*/,
944     struct uio *uio)
945 {
946 	struct proc *p = l->l_proc;	/* traced */
947 	struct vmspace *vm;
948 	int error;
949 
950 	size_t len;
951 #ifdef PMAP_NEED_PROCWR
952 	vaddr_t	addr;
953 #endif
954 
955 	error = 0;
956 	len = uio->uio_resid;
957 
958 	if (len == 0)
959 		return (0);
960 
961 #ifdef PMAP_NEED_PROCWR
962 	addr = uio->uio_offset;
963 #endif
964 
965 	vm = p->p_vmspace;
966 
967 	mutex_enter(&vm->vm_map.misc_lock);
968 	if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
969 		error = EFAULT;
970 	if (error == 0)
971 		p->p_vmspace->vm_refcnt++;  /* XXX */
972 	mutex_exit(&vm->vm_map.misc_lock);
973 	if (error != 0)
974 		return (error);
975 	error = uvm_io(&vm->vm_map, uio);
976 	uvmspace_free(vm);
977 
978 #ifdef PMAP_NEED_PROCWR
979 	if (error == 0 && uio->uio_rw == UIO_WRITE)
980 		pmap_procwr(p, addr, len);
981 #endif
982 	return (error);
983 }
984 #endif /* KTRACE || PTRACE */
985 
986 #if defined(KTRACE) || defined(PTRACE)
987 void
988 process_stoptrace(void)
989 {
990 	struct lwp *l = curlwp;
991 	struct proc *p = l->l_proc, *pp;
992 
993 	mutex_enter(proc_lock);
994 	mutex_enter(p->p_lock);
995 	pp = p->p_pptr;
996 	if (pp->p_pid == 1) {
997 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
998 		mutex_exit(p->p_lock);
999 		mutex_exit(proc_lock);
1000 		return;
1001 	}
1002 
1003 	p->p_xstat = SIGTRAP;
1004 	proc_stop(p, 1, SIGSTOP);
1005 	mutex_exit(proc_lock);
1006 
1007 	/*
1008 	 * Call issignal() once only, to have it take care of the
1009 	 * pending stop.  Signal processing will take place as usual
1010 	 * from userret().
1011 	 */
1012 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
1013 	(void)issignal(l);
1014 	mutex_exit(p->p_lock);
1015 	KERNEL_LOCK(l->l_biglocks, l);
1016 }
1017 #endif	/* KTRACE || PTRACE */
1018