Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.74.2.7
      1 /*	$NetBSD: kern_ktrace.c,v 1.74.2.7 2004/09/21 13:35:04 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)kern_ktrace.c	8.5 (Berkeley) 5/14/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.74.2.7 2004/09/21 13:35:04 skrll Exp $");
     36 
     37 #include "opt_ktrace.h"
     38 #include "opt_compat_mach.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/file.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/ktrace.h>
     47 #include <sys/malloc.h>
     48 #include <sys/syslog.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/ioctl.h>
     51 
     52 #include <sys/mount.h>
     53 #include <sys/sa.h>
     54 #include <sys/syscallargs.h>
     55 
     56 #ifdef KTRACE
     57 
     58 void	ktrinitheader(struct ktr_header *, struct lwp *, int);
     59 int	ktrwrite(struct lwp *, struct ktr_header *);
     60 int	ktrace_common(struct lwp *, int, int, int, struct file *);
     61 int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     62 int	ktrsetchildren(struct proc *, struct proc *, int, int,
     63 	    struct file *);
     64 int	ktrcanset(struct proc *, struct proc *);
     65 int	ktrsamefile(struct file *, struct file *);
     66 
     67 /*
     68  * "deep" compare of two files for the purposes of clearing a trace.
     69  * Returns true if they're the same open file, or if they point at the
     70  * same underlying vnode/socket.
     71  */
     72 
     73 int
     74 ktrsamefile(struct file *f1, struct file *f2)
     75 {
     76 
     77 	return ((f1 == f2) ||
     78 	    ((f1 != NULL) && (f2 != NULL) &&
     79 		(f1->f_type == f2->f_type) &&
     80 		(f1->f_data == f2->f_data)));
     81 }
     82 
     83 void
     84 ktrderef(struct proc *p)
     85 {
     86 	struct file *fp = p->p_tracep;
     87 	p->p_traceflag = 0;
     88 	if (fp == NULL)
     89 		return;
     90 	p->p_tracep = NULL;
     91 
     92 	simple_lock(&fp->f_slock);
     93 	FILE_USE(fp);
     94 
     95 	/*
     96 	 * ktrace file descriptor can't be watched (are not visible to
     97 	 * userspace), so no kqueue stuff here.
     98 	 * XXX: The above comment is wrong, because the fktrace file
     99 	 * descriptor is available in userland.
    100 	 */
    101 	closef(fp, NULL);
    102 }
    103 
    104 void
    105 ktradref(struct proc *p)
    106 {
    107 	struct file *fp = p->p_tracep;
    108 
    109 	fp->f_count++;
    110 }
    111 
    112 void
    113 ktrinitheader(struct ktr_header *kth, struct lwp *l, int type)
    114 {
    115 	struct proc *p = l->l_proc;
    116 
    117 	(void)memset(kth, 0, sizeof(*kth));
    118 	kth->ktr_type = type;
    119 	microtime(&kth->ktr_time);
    120 	kth->ktr_pid = p->p_pid;
    121 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    122 }
    123 
    124 int
    125 ktrsyscall(struct lwp *l, register_t code, register_t realcode,
    126     const struct sysent *callp, register_t args[])
    127 {
    128 	struct proc *p = l->l_proc;
    129 	struct ktr_syscall *ktp;
    130 	struct ktr_header kth;
    131 	register_t *argp;
    132 	int argsize, error;
    133 	size_t len;
    134 	u_int i;
    135 
    136 	if (callp == NULL)
    137 		callp = p->p_emul->e_sysent;
    138 
    139 	argsize = callp[code].sy_argsize;
    140 #ifdef _LP64
    141 	if (p->p_flag & P_32)
    142 		argsize = argsize << 1;
    143 #endif
    144 	len = sizeof(struct ktr_syscall) + argsize;
    145 
    146 	p->p_traceflag |= KTRFAC_ACTIVE;
    147 	ktrinitheader(&kth, l, KTR_SYSCALL);
    148 	ktp = malloc(len, M_TEMP, M_WAITOK);
    149 	ktp->ktr_code = realcode;
    150 	ktp->ktr_argsize = argsize;
    151 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    152 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    153 		*argp++ = args[i];
    154 	kth.ktr_buf = (caddr_t)ktp;
    155 	kth.ktr_len = len;
    156 	error = ktrwrite(l, &kth);
    157 	free(ktp, M_TEMP);
    158 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    159 	return error;
    160 }
    161 
    162 int
    163 ktrsysret(struct lwp *l, register_t code, int error, register_t *retval)
    164 {
    165 	struct proc *p = l->l_proc;
    166 	struct ktr_header kth;
    167 	struct ktr_sysret ktp;
    168 
    169 	p->p_traceflag |= KTRFAC_ACTIVE;
    170 	ktrinitheader(&kth, l, KTR_SYSRET);
    171 	ktp.ktr_code = code;
    172 	ktp.ktr_eosys = 0;			/* XXX unused */
    173 	ktp.ktr_error = error;
    174 	ktp.ktr_retval = retval ? retval[0] : 0;
    175 	ktp.ktr_retval_1 = retval ? retval[1] : 0;
    176 
    177 	kth.ktr_buf = (caddr_t)&ktp;
    178 	kth.ktr_len = sizeof(struct ktr_sysret);
    179 
    180 	error = ktrwrite(l, &kth);
    181 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    182 	return error;
    183 }
    184 
    185 int
    186 ktrnamei(struct lwp *l, char *path)
    187 {
    188 	struct proc *p = l->l_proc;
    189 	struct ktr_header kth;
    190 	int error;
    191 
    192 	p->p_traceflag |= KTRFAC_ACTIVE;
    193 	ktrinitheader(&kth, l, KTR_NAMEI);
    194 	kth.ktr_len = strlen(path);
    195 	kth.ktr_buf = path;
    196 
    197 	error = ktrwrite(l, &kth);
    198 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    199 	return error;
    200 }
    201 
    202 int
    203 ktremul(struct lwp *l)
    204 {
    205 	struct ktr_header kth;
    206 	const char *emul;
    207 	struct proc *p;
    208 	int error;
    209 
    210 	p = l->l_proc;
    211 	emul = p->p_emul->e_name;
    212 	p->p_traceflag |= KTRFAC_ACTIVE;
    213 	ktrinitheader(&kth, l, KTR_EMUL);
    214 	kth.ktr_len = strlen(emul);
    215 	kth.ktr_buf = (caddr_t)emul;
    216 
    217 	error = ktrwrite(l, &kth);
    218 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    219 	return error;
    220 }
    221 
    222 int
    223 ktrkmem(struct lwp *l, int ktr, const void *buf, size_t len)
    224 {
    225 	struct proc *p = l->l_proc;
    226 	struct ktr_header kth;
    227 	int error;
    228 
    229 	p->p_traceflag |= KTRFAC_ACTIVE;
    230 	ktrinitheader(&kth, l, ktr);
    231 	kth.ktr_len = len;
    232 	kth.ktr_buf = buf;
    233 
    234 	error = ktrwrite(l, &kth);
    235 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    236 	return error;
    237 }
    238 
    239 int
    240 ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov,
    241     int len, int error)
    242 {
    243 	struct proc *p = l->l_proc;
    244 	struct ktr_header kth;
    245 	struct ktr_genio *ktp;
    246 	int resid = len, cnt;
    247 	caddr_t cp;
    248 	int buflen;
    249 
    250 	if (error)
    251 		return error;
    252 
    253 	p->p_traceflag |= KTRFAC_ACTIVE;
    254 
    255 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    256 
    257 	ktrinitheader(&kth, l, KTR_GENIO);
    258 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    259 	ktp->ktr_fd = fd;
    260 	ktp->ktr_rw = rw;
    261 
    262 	kth.ktr_buf = (caddr_t)ktp;
    263 
    264 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    265 	buflen -= sizeof(struct ktr_genio);
    266 
    267 	while (resid > 0) {
    268 #if 0 /* XXX NJWLWP */
    269 		KDASSERT(p->p_cpu != NULL);
    270 		KDASSERT(p->p_cpu == curcpu());
    271 #endif
    272 		/* XXX NJWLWP */
    273 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    274 			preempt(1);
    275 
    276 		cnt = min(iov->iov_len, buflen);
    277 		if (cnt > resid)
    278 			cnt = resid;
    279 		if ((error = copyin(iov->iov_base, cp, cnt)) != 0)
    280 			break;
    281 
    282 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    283 
    284 		error = ktrwrite(l, &kth);
    285 		if (__predict_false(error != 0))
    286 			break;
    287 
    288 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    289 		iov->iov_len -= cnt;
    290 
    291 		if (iov->iov_len == 0)
    292 			iov++;
    293 
    294 		resid -= cnt;
    295 	}
    296 
    297 	free(ktp, M_TEMP);
    298 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    299 	return error;
    300 }
    301 
    302 int
    303 ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask,
    304     const ksiginfo_t *ksi)
    305 {
    306 	struct proc *p = l->l_proc;
    307 	int error;
    308 
    309 	struct ktr_header kth;
    310 	struct {
    311 		struct ktr_psig	kp;
    312 		siginfo_t	si;
    313 	} kbuf;
    314 
    315 	p->p_traceflag |= KTRFAC_ACTIVE;
    316 	ktrinitheader(&kth, l, KTR_PSIG);
    317 	kbuf.kp.signo = (char)sig;
    318 	kbuf.kp.action = action;
    319 	kbuf.kp.mask = *mask;
    320 	kth.ktr_buf = (caddr_t)&kbuf;
    321 	if (ksi) {
    322 		kbuf.kp.code = KSI_TRAPCODE(ksi);
    323 		(void)memset(&kbuf.si, 0, sizeof(kbuf.si));
    324 		kbuf.si._info = ksi->ksi_info;
    325 		kth.ktr_len = sizeof(kbuf);
    326 	} else {
    327 		kbuf.kp.code = 0;
    328 		kth.ktr_len = sizeof(struct ktr_psig);
    329 	}
    330 	error = ktrwrite(l, &kth);
    331 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    332 	return error;
    333 }
    334 
    335 int
    336 ktrcsw(struct lwp *l, int out, int user)
    337 {
    338 	struct proc *p = l->l_proc;
    339 	struct ktr_header kth;
    340 	struct ktr_csw kc;
    341 	int error;
    342 
    343 	p->p_traceflag |= KTRFAC_ACTIVE;
    344 	ktrinitheader(&kth, l, KTR_CSW);
    345 	kc.out = out;
    346 	kc.user = user;
    347 	kth.ktr_buf = (caddr_t)&kc;
    348 	kth.ktr_len = sizeof(struct ktr_csw);
    349 
    350 	error = ktrwrite(l, &kth);
    351 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    352 	return error;
    353 }
    354 
    355 int
    356 ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr)
    357 {
    358 	struct proc *p = l->l_proc;
    359 	struct ktr_header kth;
    360 	struct ktr_user *ktp;
    361 	caddr_t user_dta;
    362 	int error;
    363 
    364 	p->p_traceflag |= KTRFAC_ACTIVE;
    365 	ktrinitheader(&kth, l, KTR_USER);
    366 	ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
    367 	if (ustr) {
    368 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    369 			ktp->ktr_id[0] = '\0';
    370 	} else
    371 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    372 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    373 
    374 	user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
    375 	if (copyin(addr, (void *) user_dta, len) != 0)
    376 		len = 0;
    377 
    378 	kth.ktr_buf = (void *)ktp;
    379 	kth.ktr_len = sizeof(struct ktr_user) + len;
    380 	error = ktrwrite(l, &kth);
    381 
    382 	free(ktp, M_TEMP);
    383 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    384 	return error;
    385 
    386 }
    387 
    388 int
    389 ktrmmsg(struct lwp *l, const void *msgh, size_t size)
    390 {
    391 	struct proc *p = l->l_proc;
    392 	struct ktr_header kth;
    393 	struct ktr_mmsg	*kp;
    394 	int error;
    395 
    396 	p->p_traceflag |= KTRFAC_ACTIVE;
    397 	ktrinitheader(&kth, l, KTR_MMSG);
    398 
    399 	kp = (struct ktr_mmsg *)msgh;
    400 	kth.ktr_buf = (caddr_t)kp;
    401 	kth.ktr_len = size;
    402 	error = ktrwrite(l, &kth);
    403 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    404 	return error;
    405 }
    406 
    407 int
    408 ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr)
    409 {
    410 	struct proc *p = l->l_proc;
    411 	struct ktr_header kth;
    412 	struct ktr_mool *kp;
    413 	struct ktr_mool *buf;
    414 	int error;
    415 
    416 	p->p_traceflag |= KTRFAC_ACTIVE;
    417 	ktrinitheader(&kth, l, KTR_MOOL);
    418 
    419 	kp = malloc(size + sizeof(*kp), M_TEMP, M_WAITOK);
    420 	kp->uaddr = uaddr;
    421 	kp->size = size;
    422 	buf = kp + 1; /* Skip uaddr and size */
    423 	(void)memcpy(buf, kaddr, size);
    424 
    425 	kth.ktr_buf = (caddr_t)kp;
    426 	kth.ktr_len = size + sizeof(*kp);
    427 	error = ktrwrite(l, &kth);
    428 	free(kp, M_TEMP);
    429 
    430 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    431 	return error;
    432 }
    433 
    434 int
    435 ktrsaupcall(struct lwp *l, int type, int nevent, int nint, void *sas,
    436     void *ap)
    437 {
    438 	struct proc *p = l->l_proc;
    439 	struct ktr_header kth;
    440 	struct ktr_saupcall *ktp;
    441 	size_t len;
    442 	struct sa_t **sapp;
    443 	int error;
    444 	int i;
    445 
    446 	p->p_traceflag |= KTRFAC_ACTIVE;
    447 	ktrinitheader(&kth, l, KTR_SAUPCALL);
    448 
    449 	len = sizeof(struct ktr_saupcall);
    450 	ktp = malloc(len + sizeof(struct sa_t) * (nevent + nint + 1), M_TEMP,
    451 	    M_WAITOK);
    452 
    453 	ktp->ktr_type = type;
    454 	ktp->ktr_nevent = nevent;
    455 	ktp->ktr_nint = nint;
    456 	ktp->ktr_sas = sas;
    457 	ktp->ktr_ap = ap;
    458 	/*
    459 	 *  Copy the sa_t's
    460 	 */
    461 	sapp = (struct sa_t **) sas;
    462 
    463 	for (i = nevent + nint; i >= 0; i--) {
    464 		if (copyin(*sapp, (char *)ktp + len, sizeof(struct sa_t)) == 0)
    465 			len += sizeof(struct sa_t);
    466 		sapp++;
    467 	}
    468 
    469 	kth.ktr_buf = (void *)ktp;
    470 	kth.ktr_len = len;
    471 	error = ktrwrite(l, &kth);
    472 
    473 	free(ktp, M_TEMP);
    474 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    475 	return error;
    476 }
    477 
    478 /* Interface and common routines */
    479 
    480 int
    481 ktrace_common(struct lwp *l, int ops, int facs, int pid, struct file *fp)
    482 {
    483 	struct proc *curp = l->l_proc;
    484 	struct pgrp *pg;
    485 	struct proc *p;
    486 	int error = 0;
    487 	int ret = 0;
    488 	int one = 1;
    489 	int descend;
    490 
    491 	curp->p_traceflag |= KTRFAC_ACTIVE;
    492 	descend = ops & KTRFLAG_DESCEND;
    493 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    494 
    495 	/*
    496 	 * Clear all uses of the tracefile
    497 	 */
    498 	if (KTROP(ops) == KTROP_CLEARFILE) {
    499 		proclist_lock_read();
    500 		LIST_FOREACH(p, &allproc, p_list) {
    501 			if (ktrsamefile(p->p_tracep, fp)) {
    502 				if (ktrcanset(curp, p))
    503 					ktrderef(p);
    504 				else
    505 					error = EPERM;
    506 			}
    507 		}
    508 		proclist_unlock_read();
    509 		goto done;
    510 	}
    511 
    512 	/*
    513 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    514 	 */
    515 
    516 	if (fp != NULL) {
    517 		fp->f_flag |= FNONBLOCK;
    518 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, l);
    519 	}
    520 
    521 	/*
    522 	 * need something to (un)trace (XXX - why is this here?)
    523 	 */
    524 	if (!facs) {
    525 		error = EINVAL;
    526 		goto done;
    527 	}
    528 	/*
    529 	 * do it
    530 	 */
    531 	if (pid < 0) {
    532 		/*
    533 		 * by process group
    534 		 */
    535 		pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
    536 		if (pg == NULL) {
    537 			error = ESRCH;
    538 			goto done;
    539 		}
    540 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    541 			if (descend)
    542 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    543 			else
    544 				ret |= ktrops(curp, p, ops, facs, fp);
    545 		}
    546 
    547 	} else {
    548 		/*
    549 		 * by pid
    550 		 */
    551 		p = p_find(pid, PFIND_UNLOCK_FAIL);
    552 		if (p == NULL) {
    553 			error = ESRCH;
    554 			goto done;
    555 		}
    556 		if (descend)
    557 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    558 		else
    559 			ret |= ktrops(curp, p, ops, facs, fp);
    560 	}
    561 	proclist_unlock_read();	/* taken by p{g}_find */
    562 	if (!ret)
    563 		error = EPERM;
    564 done:
    565 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    566 	return (error);
    567 }
    568 
    569 /*
    570  * ktrace system call
    571  */
    572 /* ARGSUSED */
    573 int
    574 sys_fktrace(struct lwp *l, void *v, register_t *retval)
    575 {
    576 	struct sys_fktrace_args /* {
    577 		syscallarg(int) fd;
    578 		syscallarg(int) ops;
    579 		syscallarg(int) facs;
    580 		syscallarg(int) pid;
    581 	} */ *uap = v;
    582 	struct proc *curp;
    583 	struct file *fp = NULL;
    584 	struct filedesc *fdp = l->l_proc->p_fd;
    585 	int error;
    586 
    587 	curp = l->l_proc;
    588 	fdp = curp->p_fd;
    589 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    590 		return (EBADF);
    591 
    592 	FILE_USE(fp);
    593 
    594 	if ((fp->f_flag & FWRITE) == 0)
    595 		error = EBADF;
    596 	else
    597 		error = ktrace_common(l, SCARG(uap, ops),
    598 		    SCARG(uap, facs), SCARG(uap, pid), fp);
    599 
    600 	FILE_UNUSE(fp, l);
    601 
    602 	return error;
    603 }
    604 
    605 /*
    606  * ktrace system call
    607  */
    608 /* ARGSUSED */
    609 int
    610 sys_ktrace(struct lwp *l, void *v, register_t *retval)
    611 {
    612 	struct sys_ktrace_args /* {
    613 		syscallarg(const char *) fname;
    614 		syscallarg(int) ops;
    615 		syscallarg(int) facs;
    616 		syscallarg(int) pid;
    617 	} */ *uap = v;
    618 	struct proc *curp = l->l_proc;
    619 	struct vnode *vp = NULL;
    620 	struct file *fp = NULL;
    621 	int ops = SCARG(uap, ops);
    622 	struct nameidata nd;
    623 	int error = 0;
    624 	int fd;
    625 
    626 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    627 
    628 	curp->p_traceflag |= KTRFAC_ACTIVE;
    629 	if ((ops & KTROP_CLEAR) == 0) {
    630 		/*
    631 		 * an operation which requires a file argument.
    632 		 */
    633 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    634 		    l);
    635 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    636 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    637 			return (error);
    638 		}
    639 		vp = nd.ni_vp;
    640 		VOP_UNLOCK(vp, 0);
    641 		if (vp->v_type != VREG) {
    642 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, l);
    643 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    644 			return (EACCES);
    645 		}
    646 		/*
    647 		 * XXX This uses up a file descriptor slot in the
    648 		 * tracing process for the duration of this syscall.
    649 		 * This is not expected to be a problem.  If
    650 		 * falloc(NULL, ...) DTRT we could skip that part, but
    651 		 * that would require changing its interface to allow
    652 		 * the caller to pass in a ucred..
    653 		 *
    654 		 * This will FILE_USE the fp it returns, if any.
    655 		 * Keep it in use until we return.
    656 		 */
    657 		if ((error = falloc(curp, &fp, &fd)) != 0)
    658 			goto done;
    659 
    660 		fp->f_flag = FWRITE|FAPPEND;
    661 		fp->f_type = DTYPE_VNODE;
    662 		fp->f_ops = &vnops;
    663 		fp->f_data = (caddr_t)vp;
    664 		FILE_SET_MATURE(fp);
    665 		vp = NULL;
    666 	}
    667 	error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
    668 	    SCARG(uap, pid), fp);
    669 done:
    670 	if (vp != NULL)
    671 		(void) vn_close(vp, FWRITE, curp->p_ucred, l);
    672 	if (fp != NULL) {
    673 		FILE_UNUSE(fp, l);	/* release file */
    674 		fdrelease(l, fd); 	/* release fd table slot */
    675 	}
    676 	return (error);
    677 }
    678 
    679 int
    680 ktrops(struct proc *curp, struct proc *p, int ops, int facs,
    681     struct file *fp)
    682 {
    683 
    684 	if (!ktrcanset(curp, p))
    685 		return (0);
    686 	if (KTROP(ops) == KTROP_SET) {
    687 		if (p->p_tracep != fp) {
    688 			/*
    689 			 * if trace file already in use, relinquish
    690 			 */
    691 			ktrderef(p);
    692 			p->p_tracep = fp;
    693 			ktradref(p);
    694 		}
    695 		p->p_traceflag |= facs;
    696 		if (curp->p_ucred->cr_uid == 0)
    697 			p->p_traceflag |= KTRFAC_ROOT;
    698 	} else {
    699 		/* KTROP_CLEAR */
    700 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    701 			/* no more tracing */
    702 			ktrderef(p);
    703 		}
    704 	}
    705 
    706 	/*
    707 	 * Emit an emulation record, every time there is a ktrace
    708 	 * change/attach request.
    709 	 */
    710 	if (KTRPOINT(p, KTR_EMUL))
    711 		p->p_traceflag |= KTRFAC_TRC_EMUL;
    712 #ifdef __HAVE_SYSCALL_INTERN
    713 	(*p->p_emul->e_syscall_intern)(p);
    714 #endif
    715 
    716 	return (1);
    717 }
    718 
    719 int
    720 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
    721     struct file *fp)
    722 {
    723 	struct proc *p;
    724 	int ret = 0;
    725 
    726 	p = top;
    727 	for (;;) {
    728 		ret |= ktrops(curp, p, ops, facs, fp);
    729 		/*
    730 		 * If this process has children, descend to them next,
    731 		 * otherwise do any siblings, and if done with this level,
    732 		 * follow back up the tree (but not past top).
    733 		 */
    734 		if (LIST_FIRST(&p->p_children) != NULL) {
    735 			p = LIST_FIRST(&p->p_children);
    736 			continue;
    737 		}
    738 		for (;;) {
    739 			if (p == top)
    740 				return (ret);
    741 			if (LIST_NEXT(p, p_sibling) != NULL) {
    742 				p = LIST_NEXT(p, p_sibling);
    743 				break;
    744 			}
    745 			p = p->p_pptr;
    746 		}
    747 	}
    748 	/*NOTREACHED*/
    749 }
    750 
    751 int
    752 ktrwrite(struct lwp *l, struct ktr_header *kth)
    753 {
    754 	struct iovec aiov[2];
    755 	int error, tries;
    756 	struct uio auio;
    757 	struct file *fp;
    758 	struct proc *p;
    759 
    760 	p = l->l_proc;
    761 	fp = p->p_tracep;
    762 
    763 	if (fp == NULL)
    764 		return 0;
    765 
    766 	if (p->p_traceflag & KTRFAC_TRC_EMUL) {
    767 		/* Add emulation trace before first entry for this process */
    768 		p->p_traceflag &= ~KTRFAC_TRC_EMUL;
    769 		if ((error = ktremul(l)) != 0)
    770 			return error;
    771 	}
    772 
    773 	auio.uio_iov = &aiov[0];
    774 	auio.uio_offset = 0;
    775 	auio.uio_segflg = UIO_SYSSPACE;
    776 	auio.uio_rw = UIO_WRITE;
    777 	aiov[0].iov_base = (caddr_t)kth;
    778 	aiov[0].iov_len = sizeof(struct ktr_header);
    779 	auio.uio_resid = sizeof(struct ktr_header);
    780 	auio.uio_iovcnt = 1;
    781 	auio.uio_lwp = (struct lwp *)0;
    782 	if (kth->ktr_len > 0) {
    783 		auio.uio_iovcnt++;
    784 		aiov[1].iov_base = (void *)kth->ktr_buf;
    785 		aiov[1].iov_len = kth->ktr_len;
    786 		auio.uio_resid += kth->ktr_len;
    787 	}
    788 	kth->ktr_buf = (caddr_t)l->l_lid;
    789 
    790 	simple_lock(&fp->f_slock);
    791 	FILE_USE(fp);
    792 
    793 	tries = 0;
    794 	do {
    795 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    796 		    fp->f_cred, FOF_UPDATE_OFFSET);
    797 		tries++;
    798 		if (error == EWOULDBLOCK)
    799 			preempt(1);
    800 	} while ((error == EWOULDBLOCK) && (tries < 3));
    801 	FILE_UNUSE(fp, NULL);
    802 
    803 	if (__predict_true(error == 0))
    804 		return (0);
    805 	/*
    806 	 * If error encountered, give up tracing on this vnode.  Don't report
    807 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    808 	 */
    809 	if (error != EPIPE)
    810 		log(LOG_NOTICE,
    811 		    "ktrace write failed, errno %d, tracing stopped\n",
    812 		    error);
    813 	proclist_lock_read();
    814 	LIST_FOREACH(p, &allproc, p_list) {
    815 		if (ktrsamefile(p->p_tracep, fp))
    816 			ktrderef(p);
    817 	}
    818 	proclist_unlock_read();
    819 
    820 	return (error);
    821 }
    822 
    823 /*
    824  * Return true if caller has permission to set the ktracing state
    825  * of target.  Essentially, the target can't possess any
    826  * more permissions than the caller.  KTRFAC_ROOT signifies that
    827  * root previously set the tracing status on the target process, and
    828  * so, only root may further change it.
    829  *
    830  * TODO: check groups.  use caller effective gid.
    831  */
    832 int
    833 ktrcanset(struct proc *callp, struct proc *targetp)
    834 {
    835 	struct pcred *caller = callp->p_cred;
    836 	struct pcred *target = targetp->p_cred;
    837 
    838 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    839 	    target->p_ruid == target->p_svuid &&
    840 	    caller->p_rgid == target->p_rgid &&	/* XXX */
    841 	    target->p_rgid == target->p_svgid &&
    842 	    (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    843 	    (targetp->p_flag & P_SUGID) == 0) ||
    844 	    caller->pc_ucred->cr_uid == 0)
    845 		return (1);
    846 
    847 	return (0);
    848 }
    849 #endif /* KTRACE */
    850 
    851 /*
    852  * Put user defined entry to ktrace records.
    853  */
    854 int
    855 sys_utrace(struct lwp *l, void *v, register_t *retval)
    856 {
    857 #ifdef KTRACE
    858 	struct sys_utrace_args /* {
    859 		syscallarg(const char *) label;
    860 		syscallarg(void *) addr;
    861 		syscallarg(size_t) len;
    862 	} */ *uap = v;
    863 	struct proc *p = l->l_proc;
    864 
    865 	if (!KTRPOINT(p, KTR_USER))
    866 		return (0);
    867 
    868 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    869 		return (EINVAL);
    870 
    871 	ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    872 
    873 	return (0);
    874 #else /* !KTRACE */
    875 	return ENOSYS;
    876 #endif /* KTRACE */
    877 }
    878