xref: /netbsd-src/sys/kern/sys_process.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: sys_process.c,v 1.147 2009/06/28 11:42:07 yamt 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.147 2009/06/28 11:42:07 yamt 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 /*
145  * Process debugging system call.
146  */
147 int
148 sys_ptrace(struct lwp *l, const struct sys_ptrace_args *uap, register_t *retval)
149 {
150 	/* {
151 		syscallarg(int) req;
152 		syscallarg(pid_t) pid;
153 		syscallarg(void *) addr;
154 		syscallarg(int) data;
155 	} */
156 	struct proc *p = l->l_proc;
157 	struct lwp *lt;
158 	struct proc *t;				/* target process */
159 	struct uio uio;
160 	struct iovec iov;
161 	struct ptrace_io_desc piod;
162 	struct ptrace_lwpinfo pl;
163 	struct vmspace *vm;
164 	int error, write, tmp, req, pheld;
165 	int signo;
166 	ksiginfo_t ksi;
167 	char *path;
168 	int len;
169 
170 	error = 0;
171 	req = SCARG(uap, req);
172 
173 	/*
174 	 * If attaching or detaching, we need to get a write hold on the
175 	 * proclist lock so that we can re-parent the target process.
176 	 */
177 	mutex_enter(proc_lock);
178 
179 	/* "A foolish consistency..." XXX */
180 	if (req == PT_TRACE_ME) {
181 		t = p;
182 		mutex_enter(t->p_lock);
183 	} else {
184 		/* Find the process we're supposed to be operating on. */
185 		if ((t = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
186 			mutex_exit(proc_lock);
187 			return (ESRCH);
188 		}
189 
190 		/* XXX-elad */
191 		mutex_enter(t->p_lock);
192 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
193 		    t, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
194 		if (error) {
195 			mutex_exit(proc_lock);
196 			mutex_exit(t->p_lock);
197 			return (ESRCH);
198 		}
199 	}
200 
201 	/*
202 	 * Grab a reference on the process to prevent it from execing or
203 	 * exiting.
204 	 */
205 	if (!rw_tryenter(&t->p_reflock, RW_READER)) {
206 		mutex_exit(proc_lock);
207 		mutex_exit(t->p_lock);
208 		return EBUSY;
209 	}
210 
211 	/* Make sure we can operate on it. */
212 	switch (req) {
213 	case  PT_TRACE_ME:
214 		/* Saying that you're being traced is always legal. */
215 		break;
216 
217 	case  PT_ATTACH:
218 		/*
219 		 * You can't attach to a process if:
220 		 *	(1) it's the process that's doing the attaching,
221 		 */
222 		if (t->p_pid == p->p_pid) {
223 			error = EINVAL;
224 			break;
225 		}
226 
227 		/*
228 		 *  (2) it's a system process
229 		 */
230 		if (t->p_flag & PK_SYSTEM) {
231 			error = EPERM;
232 			break;
233 		}
234 
235 		/*
236 		 *	(3) it's already being traced, or
237 		 */
238 		if (ISSET(t->p_slflag, PSL_TRACED)) {
239 			error = EBUSY;
240 			break;
241 		}
242 
243 		/*
244 		 * 	(4) the tracer is chrooted, and its root directory is
245 		 * 	    not at or above the root directory of the tracee
246 		 */
247 		mutex_exit(t->p_lock);	/* XXXSMP */
248 		tmp = proc_isunder(t, l);
249 		mutex_enter(t->p_lock);	/* XXXSMP */
250 		if (!tmp) {
251 			error = EPERM;
252 			break;
253 		}
254 		break;
255 
256 	case  PT_READ_I:
257 	case  PT_READ_D:
258 	case  PT_WRITE_I:
259 	case  PT_WRITE_D:
260 	case  PT_IO:
261 #ifdef PT_GETREGS
262 	case  PT_GETREGS:
263 #endif
264 #ifdef PT_SETREGS
265 	case  PT_SETREGS:
266 #endif
267 #ifdef PT_GETFPREGS
268 	case  PT_GETFPREGS:
269 #endif
270 #ifdef PT_SETFPREGS
271 	case  PT_SETFPREGS:
272 #endif
273 #ifdef __HAVE_PTRACE_MACHDEP
274 	PTRACE_MACHDEP_REQUEST_CASES
275 #endif
276 		/*
277 		 * You can't read/write the memory or registers of a process
278 		 * if the tracer is chrooted, and its root directory is not at
279 		 * or above the root directory of the tracee.
280 		 */
281 		mutex_exit(t->p_lock);	/* XXXSMP */
282 		tmp = proc_isunder(t, l);
283 		mutex_enter(t->p_lock);	/* XXXSMP */
284 		if (!tmp) {
285 			error = EPERM;
286 			break;
287 		}
288 		/*FALLTHROUGH*/
289 
290 	case  PT_CONTINUE:
291 	case  PT_KILL:
292 	case  PT_DETACH:
293 	case  PT_LWPINFO:
294 	case  PT_SYSCALL:
295 	case  PT_DUMPCORE:
296 #ifdef PT_STEP
297 	case  PT_STEP:
298 #endif
299 		/*
300 		 * You can't do what you want to the process if:
301 		 *	(1) It's not being traced at all,
302 		 */
303 		if (!ISSET(t->p_slflag, PSL_TRACED)) {
304 			error = EPERM;
305 			break;
306 		}
307 
308 		/*
309 		 *	(2) it's being traced by procfs (which has
310 		 *	    different signal delivery semantics),
311 		 */
312 		if (ISSET(t->p_slflag, PSL_FSTRACE)) {
313 			uprintf("file system traced\n");
314 			error = EBUSY;
315 			break;
316 		}
317 
318 		/*
319 		 *	(3) it's not being traced by _you_, or
320 		 */
321 		if (t->p_pptr != p) {
322 			uprintf("parent %d != %d\n", t->p_pptr->p_pid,
323 			    p->p_pid);
324 			error = EBUSY;
325 			break;
326 		}
327 
328 		/*
329 		 *	(4) it's not currently stopped.
330 		 */
331 		if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */) {
332 			uprintf("stat %d flag %d\n", t->p_stat,
333 			    !t->p_waited);
334 			error = EBUSY;
335 			break;
336 		}
337 		break;
338 
339 	default:			/* It was not a legal request. */
340 		error = EINVAL;
341 		break;
342 	}
343 
344 	if (error == 0) {
345 		error = kauth_authorize_process(l->l_cred,
346 		    KAUTH_PROCESS_PTRACE, t, KAUTH_ARG(req),
347 		    NULL, NULL);
348 	}
349 	if (error == 0) {
350 		lt = lwp_find_first(t);
351 		if (lt == NULL)
352 			error = ESRCH;
353 	}
354 
355 	if (error != 0) {
356 		mutex_exit(proc_lock);
357 		mutex_exit(t->p_lock);
358 		rw_exit(&t->p_reflock);
359 		return error;
360 	}
361 
362 	/* Do single-step fixup if needed. */
363 	FIX_SSTEP(t);
364 	KASSERT(lt != NULL);
365 	lwp_addref(lt);
366 
367 	/*
368 	 * Which locks do we need held? XXX Ugly.
369 	 */
370 	switch (req) {
371 #ifdef PT_STEP
372 	case PT_STEP:
373 #endif
374 	case PT_CONTINUE:
375 	case PT_DETACH:
376 	case PT_KILL:
377 	case PT_SYSCALL:
378 	case PT_ATTACH:
379 	case PT_TRACE_ME:
380 		pheld = 1;
381 		break;
382 	default:
383 		mutex_exit(proc_lock);
384 		mutex_exit(t->p_lock);
385 		pheld = 0;
386 		break;
387 	}
388 
389 	/* Now do the operation. */
390 	write = 0;
391 	*retval = 0;
392 	tmp = 0;
393 
394 	switch (req) {
395 	case  PT_TRACE_ME:
396 		/* Just set the trace flag. */
397 		SET(t->p_slflag, PSL_TRACED);
398 		t->p_opptr = t->p_pptr;
399 		break;
400 
401 	case  PT_WRITE_I:		/* XXX no separate I and D spaces */
402 	case  PT_WRITE_D:
403 #if defined(__HAVE_RAS)
404 		/*
405 		 * Can't write to a RAS
406 		 */
407 		if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
408 			error = EACCES;
409 			break;
410 		}
411 #endif
412 		write = 1;
413 		tmp = SCARG(uap, data);
414 		/* FALLTHROUGH */
415 
416 	case  PT_READ_I:		/* XXX no separate I and D spaces */
417 	case  PT_READ_D:
418 		/* write = 0 done above. */
419 		iov.iov_base = (void *)&tmp;
420 		iov.iov_len = sizeof(tmp);
421 		uio.uio_iov = &iov;
422 		uio.uio_iovcnt = 1;
423 		uio.uio_offset = (off_t)(unsigned long)SCARG(uap, addr);
424 		uio.uio_resid = sizeof(tmp);
425 		uio.uio_rw = write ? UIO_WRITE : UIO_READ;
426 		UIO_SETUP_SYSSPACE(&uio);
427 
428 		error = process_domem(l, lt, &uio);
429 		if (!write)
430 			*retval = tmp;
431 		break;
432 
433 	case  PT_IO:
434 		error = copyin(SCARG(uap, addr), &piod, sizeof(piod));
435 		if (error)
436 			break;
437 		switch (piod.piod_op) {
438 		case PIOD_READ_D:
439 		case PIOD_READ_I:
440 			uio.uio_rw = UIO_READ;
441 			break;
442 		case PIOD_WRITE_D:
443 		case PIOD_WRITE_I:
444 			/*
445 			 * Can't write to a RAS
446 			 */
447 			if (ras_lookup(t, SCARG(uap, addr)) != (void *)-1) {
448 				return (EACCES);
449 			}
450 			uio.uio_rw = UIO_WRITE;
451 			break;
452 		default:
453 			error = EINVAL;
454 			break;
455 		}
456 		if (error)
457 			break;
458 		error = proc_vmspace_getref(l->l_proc, &vm);
459 		if (error)
460 			break;
461 		iov.iov_base = piod.piod_addr;
462 		iov.iov_len = piod.piod_len;
463 		uio.uio_iov = &iov;
464 		uio.uio_iovcnt = 1;
465 		uio.uio_offset = (off_t)(unsigned long)piod.piod_offs;
466 		uio.uio_resid = piod.piod_len;
467 		uio.uio_vmspace = vm;
468 
469 		error = process_domem(l, lt, &uio);
470 		piod.piod_len -= uio.uio_resid;
471 		(void) copyout(&piod, SCARG(uap, addr), sizeof(piod));
472 		uvmspace_free(vm);
473 		break;
474 
475 	case  PT_DUMPCORE:
476 		if ((path = SCARG(uap, addr)) != NULL) {
477 			char *dst;
478 			len = SCARG(uap, data);
479 
480 			if (len < 0 || len >= MAXPATHLEN) {
481 				error = EINVAL;
482 				break;
483 			}
484 			dst = kmem_alloc(len + 1, KM_SLEEP);
485 			if ((error = copyin(path, dst, len)) != 0) {
486 				kmem_free(dst, len + 1);
487 				break;
488 			}
489 			path = dst;
490 			path[len] = '\0';
491 		}
492 		error = (*coredump_vec)(lt, path);
493 		if (path)
494 			kmem_free(path, len + 1);
495 		break;
496 
497 #ifdef PT_STEP
498 	case  PT_STEP:
499 		/*
500 		 * From the 4.4BSD PRM:
501 		 * "Execution continues as in request PT_CONTINUE; however
502 		 * as soon as possible after execution of at least one
503 		 * instruction, execution stops again. [ ... ]"
504 		 */
505 #endif
506 	case  PT_CONTINUE:
507 	case  PT_SYSCALL:
508 	case  PT_DETACH:
509 		if (req == PT_SYSCALL) {
510 			if (!ISSET(t->p_slflag, PSL_SYSCALL)) {
511 				SET(t->p_slflag, PSL_SYSCALL);
512 #ifdef __HAVE_SYSCALL_INTERN
513 				(*t->p_emul->e_syscall_intern)(t);
514 #endif
515 			}
516 		} else {
517 			if (ISSET(t->p_slflag, PSL_SYSCALL)) {
518 				CLR(t->p_slflag, PSL_SYSCALL);
519 #ifdef __HAVE_SYSCALL_INTERN
520 				(*t->p_emul->e_syscall_intern)(t);
521 #endif
522 			}
523 		}
524 		p->p_trace_enabled = trace_is_enabled(p);
525 
526 		/*
527 		 * From the 4.4BSD PRM:
528 		 * "The data argument is taken as a signal number and the
529 		 * child's execution continues at location addr as if it
530 		 * incurred that signal.  Normally the signal number will
531 		 * be either 0 to indicate that the signal that caused the
532 		 * stop should be ignored, or that value fetched out of
533 		 * the process's image indicating which signal caused
534 		 * the stop.  If addr is (int *)1 then execution continues
535 		 * from where it stopped."
536 		 */
537 
538 		/* Check that the data is a valid signal number or zero. */
539 		if (SCARG(uap, data) < 0 || SCARG(uap, data) >= NSIG) {
540 			error = EINVAL;
541 			break;
542 		}
543 
544 		uvm_lwp_hold(lt);
545 
546 		/* If the address parameter is not (int *)1, set the pc. */
547 		if ((int *)SCARG(uap, addr) != (int *)1)
548 			if ((error = process_set_pc(lt, SCARG(uap, addr))) != 0) {
549 				uvm_lwp_rele(lt);
550 				break;
551 			}
552 
553 #ifdef PT_STEP
554 		/*
555 		 * Arrange for a single-step, if that's requested and possible.
556 		 */
557 		error = process_sstep(lt, req == PT_STEP);
558 		if (error) {
559 			uvm_lwp_rele(lt);
560 			break;
561 		}
562 #endif
563 
564 		uvm_lwp_rele(lt);
565 
566 		if (req == PT_DETACH) {
567 			CLR(t->p_slflag, PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
568 
569 			/* give process back to original parent or init */
570 			if (t->p_opptr != t->p_pptr) {
571 				struct proc *pp = t->p_opptr;
572 				proc_reparent(t, pp ? pp : initproc);
573 			}
574 
575 			/* not being traced any more */
576 			t->p_opptr = NULL;
577 		}
578 
579 		signo = SCARG(uap, data);
580 	sendsig:
581 		/* Finally, deliver the requested signal (or none). */
582 		if (t->p_stat == SSTOP) {
583 			/*
584 			 * Unstop the process.  If it needs to take a
585 			 * signal, make all efforts to ensure that at
586 			 * an LWP runs to see it.
587 			 */
588 			t->p_xstat = signo;
589 			proc_unstop(t);
590 		} else if (signo != 0) {
591 			KSI_INIT_EMPTY(&ksi);
592 			ksi.ksi_signo = signo;
593 			kpsignal2(t, &ksi);
594 		}
595 		break;
596 
597 	case  PT_KILL:
598 		/* just send the process a KILL signal. */
599 		signo = SIGKILL;
600 		goto sendsig;	/* in PT_CONTINUE, above. */
601 
602 	case  PT_ATTACH:
603 		/*
604 		 * Go ahead and set the trace flag.
605 		 * Save the old parent (it's reset in
606 		 *   _DETACH, and also in kern_exit.c:wait4()
607 		 * Reparent the process so that the tracing
608 		 *   proc gets to see all the action.
609 		 * Stop the target.
610 		 */
611 		t->p_opptr = t->p_pptr;
612 		if (t->p_pptr != p) {
613 			struct proc *parent = t->p_pptr;
614 
615 			if (parent->p_lock < t->p_lock) {
616 				if (!mutex_tryenter(parent->p_lock)) {
617 					mutex_exit(t->p_lock);
618 					mutex_enter(parent->p_lock);
619 				}
620 			} else if (parent->p_lock > t->p_lock) {
621 				mutex_enter(parent->p_lock);
622 			}
623 			parent->p_slflag |= PSL_CHTRACED;
624 			proc_reparent(t, p);
625 			if (parent->p_lock != t->p_lock)
626 				mutex_exit(parent->p_lock);
627 		}
628 		SET(t->p_slflag, PSL_TRACED);
629 		signo = SIGSTOP;
630 		goto sendsig;
631 
632 	case PT_LWPINFO:
633 		if (SCARG(uap, data) != sizeof(pl)) {
634 			error = EINVAL;
635 			break;
636 		}
637 		error = copyin(SCARG(uap, addr), &pl, sizeof(pl));
638 		if (error)
639 			break;
640 		tmp = pl.pl_lwpid;
641 		lwp_delref(lt);
642 		mutex_enter(t->p_lock);
643 		if (tmp == 0)
644 			lt = lwp_find_first(t);
645 		else {
646 			lt = lwp_find(t, tmp);
647 			if (lt == NULL) {
648 				mutex_exit(t->p_lock);
649 				error = ESRCH;
650 				break;
651 			}
652 			lt = LIST_NEXT(lt, l_sibling);
653 		}
654 		while (lt != NULL && !lwp_alive(lt))
655 			lt = LIST_NEXT(lt, l_sibling);
656 		pl.pl_lwpid = 0;
657 		pl.pl_event = 0;
658 		if (lt) {
659 			lwp_addref(lt);
660 			pl.pl_lwpid = lt->l_lid;
661 			if (lt->l_lid == t->p_sigctx.ps_lwp)
662 				pl.pl_event = PL_EVENT_SIGNAL;
663 		}
664 		mutex_exit(t->p_lock);
665 
666 		error = copyout(&pl, SCARG(uap, addr), sizeof(pl));
667 		break;
668 
669 #ifdef PT_SETREGS
670 	case  PT_SETREGS:
671 		write = 1;
672 #endif
673 #ifdef PT_GETREGS
674 	case  PT_GETREGS:
675 		/* write = 0 done above. */
676 #endif
677 #if defined(PT_SETREGS) || defined(PT_GETREGS)
678 		tmp = SCARG(uap, data);
679 		if (tmp != 0 && t->p_nlwps > 1) {
680 			lwp_delref(lt);
681 			mutex_enter(t->p_lock);
682 			lt = lwp_find(t, tmp);
683 			if (lt == NULL) {
684 				mutex_exit(t->p_lock);
685 				error = ESRCH;
686 				break;
687 			}
688 			lwp_addref(lt);
689 			mutex_exit(t->p_lock);
690 		}
691 		if (!process_validregs(lt))
692 			error = EINVAL;
693 		else {
694 			error = proc_vmspace_getref(l->l_proc, &vm);
695 			if (error)
696 				break;
697 			iov.iov_base = SCARG(uap, addr);
698 			iov.iov_len = sizeof(struct reg);
699 			uio.uio_iov = &iov;
700 			uio.uio_iovcnt = 1;
701 			uio.uio_offset = 0;
702 			uio.uio_resid = sizeof(struct reg);
703 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
704 			uio.uio_vmspace = vm;
705 
706 			error = process_doregs(l, lt, &uio);
707 			uvmspace_free(vm);
708 		}
709 		break;
710 #endif
711 
712 #ifdef PT_SETFPREGS
713 	case  PT_SETFPREGS:
714 		write = 1;
715 #endif
716 #ifdef PT_GETFPREGS
717 	case  PT_GETFPREGS:
718 		/* write = 0 done above. */
719 #endif
720 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
721 		tmp = SCARG(uap, data);
722 		if (tmp != 0 && t->p_nlwps > 1) {
723 			lwp_delref(lt);
724 			mutex_enter(t->p_lock);
725 			lt = lwp_find(t, tmp);
726 			if (lt == NULL) {
727 				mutex_exit(t->p_lock);
728 				error = ESRCH;
729 				break;
730 			}
731 			lwp_addref(lt);
732 			mutex_exit(t->p_lock);
733 		}
734 		if (!process_validfpregs(lt))
735 			error = EINVAL;
736 		else {
737 			error = proc_vmspace_getref(l->l_proc, &vm);
738 			if (error)
739 				break;
740 			iov.iov_base = SCARG(uap, addr);
741 			iov.iov_len = sizeof(struct fpreg);
742 			uio.uio_iov = &iov;
743 			uio.uio_iovcnt = 1;
744 			uio.uio_offset = 0;
745 			uio.uio_resid = sizeof(struct fpreg);
746 			uio.uio_rw = write ? UIO_WRITE : UIO_READ;
747 			uio.uio_vmspace = vm;
748 
749 			error = process_dofpregs(l, lt, &uio);
750 			uvmspace_free(vm);
751 		}
752 		break;
753 #endif
754 
755 #ifdef __HAVE_PTRACE_MACHDEP
756 	PTRACE_MACHDEP_REQUEST_CASES
757 		error = ptrace_machdep_dorequest(l, lt,
758 		    req, SCARG(uap, addr), SCARG(uap, data));
759 		break;
760 #endif
761 	}
762 
763 	if (pheld) {
764 		mutex_exit(t->p_lock);
765 		mutex_exit(proc_lock);
766 	}
767 	if (lt != NULL)
768 		lwp_delref(lt);
769 	rw_exit(&t->p_reflock);
770 
771 	return error;
772 }
773 
774 int
775 process_doregs(struct lwp *curl /*tracer*/,
776     struct lwp *l /*traced*/,
777     struct uio *uio)
778 {
779 #if defined(PT_GETREGS) || defined(PT_SETREGS)
780 	int error;
781 	struct reg r;
782 	char *kv;
783 	int kl;
784 
785 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
786 		return EINVAL;
787 
788 	kl = sizeof(r);
789 	kv = (char *)&r;
790 
791 	kv += uio->uio_offset;
792 	kl -= uio->uio_offset;
793 	if ((size_t)kl > uio->uio_resid)
794 		kl = uio->uio_resid;
795 
796 	uvm_lwp_hold(l);
797 
798 	error = process_read_regs(l, &r);
799 	if (error == 0)
800 		error = uiomove(kv, kl, uio);
801 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
802 		if (l->l_stat != LSSTOP)
803 			error = EBUSY;
804 		else
805 			error = process_write_regs(l, &r);
806 	}
807 
808 	uvm_lwp_rele(l);
809 
810 	uio->uio_offset = 0;
811 	return (error);
812 #else
813 	return (EINVAL);
814 #endif
815 }
816 
817 int
818 process_validregs(struct lwp *l)
819 {
820 
821 #if defined(PT_SETREGS) || defined(PT_GETREGS)
822 	return ((l->l_flag & LW_SYSTEM) == 0);
823 #else
824 	return (0);
825 #endif
826 }
827 
828 int
829 process_dofpregs(struct lwp *curl /*tracer*/,
830     struct lwp *l /*traced*/,
831     struct uio *uio)
832 {
833 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
834 	int error;
835 	struct fpreg r;
836 	char *kv;
837 	int kl;
838 
839 	if (uio->uio_offset < 0 || uio->uio_offset > (off_t)sizeof(r))
840 		return EINVAL;
841 
842 	kl = sizeof(r);
843 	kv = (char *)&r;
844 
845 	kv += uio->uio_offset;
846 	kl -= uio->uio_offset;
847 	if ((size_t)kl > uio->uio_resid)
848 		kl = uio->uio_resid;
849 
850 	uvm_lwp_hold(l);
851 
852 	error = process_read_fpregs(l, &r);
853 	if (error == 0)
854 		error = uiomove(kv, kl, uio);
855 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
856 		if (l->l_stat != LSSTOP)
857 			error = EBUSY;
858 		else
859 			error = process_write_fpregs(l, &r);
860 	}
861 
862 	uvm_lwp_rele(l);
863 
864 	uio->uio_offset = 0;
865 	return (error);
866 #else
867 	return (EINVAL);
868 #endif
869 }
870 
871 int
872 process_validfpregs(struct lwp *l)
873 {
874 
875 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
876 	return ((l->l_flag & LW_SYSTEM) == 0);
877 #else
878 	return (0);
879 #endif
880 }
881 #endif /* PTRACE */
882 
883 #if defined(KTRACE) || defined(PTRACE)
884 int
885 process_domem(struct lwp *curl /*tracer*/,
886     struct lwp *l /*traced*/,
887     struct uio *uio)
888 {
889 	struct proc *p = l->l_proc;	/* traced */
890 	struct vmspace *vm;
891 	int error;
892 
893 	size_t len;
894 #ifdef PMAP_NEED_PROCWR
895 	vaddr_t	addr;
896 #endif
897 
898 	error = 0;
899 	len = uio->uio_resid;
900 
901 	if (len == 0)
902 		return (0);
903 
904 #ifdef PMAP_NEED_PROCWR
905 	addr = uio->uio_offset;
906 #endif
907 
908 	vm = p->p_vmspace;
909 
910 	mutex_enter(&vm->vm_map.misc_lock);
911 	if ((l->l_flag & LW_WEXIT) || vm->vm_refcnt < 1)
912 		error = EFAULT;
913 	if (error == 0)
914 		p->p_vmspace->vm_refcnt++;  /* XXX */
915 	mutex_exit(&vm->vm_map.misc_lock);
916 	if (error != 0)
917 		return (error);
918 	error = uvm_io(&vm->vm_map, uio);
919 	uvmspace_free(vm);
920 
921 #ifdef PMAP_NEED_PROCWR
922 	if (error == 0 && uio->uio_rw == UIO_WRITE)
923 		pmap_procwr(p, addr, len);
924 #endif
925 	return (error);
926 }
927 #endif /* KTRACE || PTRACE */
928 
929 #if defined(KTRACE) || defined(PTRACE)
930 void
931 process_stoptrace(void)
932 {
933 	struct lwp *l = curlwp;
934 	struct proc *p = l->l_proc, *pp;
935 
936 	mutex_enter(proc_lock);
937 	mutex_enter(p->p_lock);
938 	pp = p->p_pptr;
939 	if (pp->p_pid == 1) {
940 		CLR(p->p_slflag, PSL_SYSCALL);	/* XXXSMP */
941 		mutex_exit(p->p_lock);
942 		mutex_exit(proc_lock);
943 		return;
944 	}
945 
946 	p->p_xstat = SIGTRAP;
947 	proc_stop(p, 1, SIGSTOP);
948 	mutex_exit(proc_lock);
949 
950 	/*
951 	 * Call issignal() once only, to have it take care of the
952 	 * pending stop.  Signal processing will take place as usual
953 	 * from userret().
954 	 */
955 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
956 	(void)issignal(l);
957 	mutex_exit(p->p_lock);
958 	KERNEL_LOCK(l->l_biglocks, l);
959 }
960 #endif	/* KTRACE || PTRACE */
961