Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.130
      1 /*	$NetBSD: kern_ktrace.c,v 1.130 2007/12/08 19:29:48 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1989, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)kern_ktrace.c	8.5 (Berkeley) 5/14/95
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.130 2007/12/08 19:29:48 pooka Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/proc.h>
     76 #include <sys/file.h>
     77 #include <sys/namei.h>
     78 #include <sys/vnode.h>
     79 #include <sys/kernel.h>
     80 #include <sys/kthread.h>
     81 #include <sys/ktrace.h>
     82 #include <sys/kmem.h>
     83 #include <sys/syslog.h>
     84 #include <sys/filedesc.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/callout.h>
     87 #include <sys/kauth.h>
     88 
     89 #include <sys/mount.h>
     90 #include <sys/syscallargs.h>
     91 
     92 /*
     93  * TODO:
     94  *	- need better error reporting?
     95  *	- userland utility to sort ktrace.out by timestamp.
     96  *	- keep minimum information in ktrace_entry when rest of alloc failed.
     97  *	- per trace control of configurable parameters.
     98  */
     99 
    100 struct ktrace_entry {
    101 	TAILQ_ENTRY(ktrace_entry) kte_list;
    102 	struct	ktr_header kte_kth;
    103 	void	*kte_buf;
    104 	size_t	kte_bufsz;
    105 #define	KTE_SPACE		32
    106 	uint8_t kte_space[KTE_SPACE];
    107 };
    108 
    109 struct ktr_desc {
    110 	TAILQ_ENTRY(ktr_desc) ktd_list;
    111 	int ktd_flags;
    112 #define	KTDF_WAIT		0x0001
    113 #define	KTDF_DONE		0x0002
    114 #define	KTDF_BLOCKING		0x0004
    115 #define	KTDF_INTERACTIVE	0x0008
    116 	int ktd_error;
    117 #define	KTDE_ENOMEM		0x0001
    118 #define	KTDE_ENOSPC		0x0002
    119 	int ktd_errcnt;
    120 	int ktd_ref;			/* # of reference */
    121 	int ktd_qcount;			/* # of entry in the queue */
    122 
    123 	/*
    124 	 * Params to control behaviour.
    125 	 */
    126 	int ktd_delayqcnt;		/* # of entry allowed to delay */
    127 	int ktd_wakedelay;		/* delay of wakeup in *tick* */
    128 	int ktd_intrwakdl;		/* ditto, but when interactive */
    129 
    130 	struct file *ktd_fp;		/* trace output file */
    131 	lwp_t *ktd_lwp;			/* our kernel thread */
    132 	TAILQ_HEAD(, ktrace_entry) ktd_queue;
    133 	callout_t ktd_wakch;		/* delayed wakeup */
    134 	kcondvar_t ktd_sync_cv;
    135 	kcondvar_t ktd_cv;
    136 };
    137 
    138 static int	ktealloc(struct ktrace_entry **, void **, lwp_t *, int,
    139 			 size_t);
    140 static void	ktrwrite(struct ktr_desc *, struct ktrace_entry *);
    141 static int	ktrace_common(lwp_t *, int, int, int, struct file *);
    142 static int	ktrops(lwp_t *, struct proc *, int, int,
    143 		    struct ktr_desc *);
    144 static int	ktrsetchildren(lwp_t *, struct proc *, int, int,
    145 		    struct ktr_desc *);
    146 static int	ktrcanset(lwp_t *, struct proc *);
    147 static int	ktrsamefile(struct file *, struct file *);
    148 static void	ktr_kmem(lwp_t *, int, const void *, size_t);
    149 static void	ktr_io(lwp_t *, int, enum uio_rw, struct iovec *, size_t);
    150 
    151 static struct ktr_desc *
    152 		ktd_lookup(struct file *);
    153 static void	ktdrel(struct ktr_desc *);
    154 static void	ktdref(struct ktr_desc *);
    155 static void	ktraddentry(lwp_t *, struct ktrace_entry *, int);
    156 /* Flags for ktraddentry (3rd arg) */
    157 #define	KTA_NOWAIT		0x0000
    158 #define	KTA_WAITOK		0x0001
    159 #define	KTA_LARGE		0x0002
    160 static void	ktefree(struct ktrace_entry *);
    161 static void	ktd_logerrl(struct ktr_desc *, int);
    162 static void	ktrace_thread(void *);
    163 static int	ktrderefall(struct ktr_desc *, int);
    164 
    165 /*
    166  * Default vaules.
    167  */
    168 #define	KTD_MAXENTRY		1000	/* XXX: tune */
    169 #define	KTD_TIMEOUT		5	/* XXX: tune */
    170 #define	KTD_DELAYQCNT		100	/* XXX: tune */
    171 #define	KTD_WAKEDELAY		5000	/* XXX: tune */
    172 #define	KTD_INTRWAKDL		100	/* XXX: tune */
    173 
    174 /*
    175  * Patchable variables.
    176  */
    177 int ktd_maxentry = KTD_MAXENTRY;	/* max # of entry in the queue */
    178 int ktd_timeout = KTD_TIMEOUT;		/* timeout in seconds */
    179 int ktd_delayqcnt = KTD_DELAYQCNT;	/* # of entry allowed to delay */
    180 int ktd_wakedelay = KTD_WAKEDELAY;	/* delay of wakeup in *ms* */
    181 int ktd_intrwakdl = KTD_INTRWAKDL;	/* ditto, but when interactive */
    182 
    183 kmutex_t ktrace_lock;
    184 int ktrace_on;
    185 static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq);
    186 
    187 MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer");
    188 POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0,
    189     "ktepl", &pool_allocator_nointr, IPL_NONE);
    190 
    191 static void
    192 ktd_wakeup(struct ktr_desc *ktd)
    193 {
    194 
    195 	callout_stop(&ktd->ktd_wakch);
    196 	cv_signal(&ktd->ktd_cv);
    197 }
    198 
    199 static void
    200 ktd_callout(void *arg)
    201 {
    202 
    203 	mutex_enter(&ktrace_lock);
    204 	ktd_wakeup(arg);
    205 	mutex_exit(&ktrace_lock);
    206 }
    207 
    208 static void
    209 ktd_logerrl(struct ktr_desc *ktd, int error)
    210 {
    211 
    212 	ktd->ktd_error |= error;
    213 	ktd->ktd_errcnt++;
    214 }
    215 
    216 #if 0
    217 static void
    218 ktd_logerr(struct proc *p, int error)
    219 {
    220 	struct ktr_desc *ktd;
    221 
    222 	KASSERT(mutex_owned(&ktrace_lock));
    223 
    224 	ktd = p->p_tracep;
    225 	if (ktd == NULL)
    226 		return;
    227 
    228 	ktd_logerrl(ktd, error);
    229 }
    230 #endif
    231 
    232 static inline int
    233 ktrenter(lwp_t *l)
    234 {
    235 
    236 	if ((l->l_pflag & LP_KTRACTIVE) != 0)
    237 		return 1;
    238 	l->l_pflag |= LP_KTRACTIVE;
    239 	return 0;
    240 }
    241 
    242 static inline void
    243 ktrexit(lwp_t *l)
    244 {
    245 
    246 	l->l_pflag &= ~LP_KTRACTIVE;
    247 }
    248 
    249 /*
    250  * Initialise the ktrace system.
    251  */
    252 void
    253 ktrinit(void)
    254 {
    255 
    256 	mutex_init(&ktrace_lock, MUTEX_DEFAULT, IPL_NONE);
    257 }
    258 
    259 /*
    260  * Release a reference.  Called with ktrace_lock held.
    261  */
    262 void
    263 ktdrel(struct ktr_desc *ktd)
    264 {
    265 
    266 	KASSERT(mutex_owned(&ktrace_lock));
    267 
    268 	KDASSERT(ktd->ktd_ref != 0);
    269 	KASSERT(ktd->ktd_ref > 0);
    270 	KASSERT(ktrace_on > 0);
    271 	ktrace_on--;
    272 	if (--ktd->ktd_ref <= 0) {
    273 		ktd->ktd_flags |= KTDF_DONE;
    274 		cv_signal(&ktd->ktd_cv);
    275 	}
    276 }
    277 
    278 void
    279 ktdref(struct ktr_desc *ktd)
    280 {
    281 
    282 	KASSERT(mutex_owned(&ktrace_lock));
    283 
    284 	ktd->ktd_ref++;
    285 	ktrace_on++;
    286 }
    287 
    288 struct ktr_desc *
    289 ktd_lookup(struct file *fp)
    290 {
    291 	struct ktr_desc *ktd;
    292 
    293 	KASSERT(mutex_owned(&ktrace_lock));
    294 
    295 	for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL;
    296 	    ktd = TAILQ_NEXT(ktd, ktd_list)) {
    297 		if (ktrsamefile(ktd->ktd_fp, fp)) {
    298 			ktdref(ktd);
    299 			break;
    300 		}
    301 	}
    302 
    303 	return (ktd);
    304 }
    305 
    306 void
    307 ktraddentry(lwp_t *l, struct ktrace_entry *kte, int flags)
    308 {
    309 	struct proc *p = l->l_proc;
    310 	struct ktr_desc *ktd;
    311 #ifdef DEBUG
    312 	struct timeval t1, t2;
    313 #endif
    314 
    315 	mutex_enter(&ktrace_lock);
    316 
    317 	if (p->p_traceflag & KTRFAC_TRC_EMUL) {
    318 		/* Add emulation trace before first entry for this process */
    319 		p->p_traceflag &= ~KTRFAC_TRC_EMUL;
    320 		mutex_exit(&ktrace_lock);
    321 		ktrexit(l);
    322 		ktremul();
    323 		(void)ktrenter(l);
    324 		mutex_enter(&ktrace_lock);
    325 	}
    326 
    327 	/* Tracing may have been cancelled. */
    328 	ktd = p->p_tracep;
    329 	if (ktd == NULL)
    330 		goto freekte;
    331 
    332 	/*
    333 	 * Bump reference count so that the object will remain while
    334 	 * we are here.  Note that the trace is controlled by other
    335 	 * process.
    336 	 */
    337 	ktdref(ktd);
    338 
    339 	if (ktd->ktd_flags & KTDF_DONE)
    340 		goto relktd;
    341 
    342 	if (ktd->ktd_qcount > ktd_maxentry) {
    343 		ktd_logerrl(ktd, KTDE_ENOSPC);
    344 		goto relktd;
    345 	}
    346 	TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
    347 	ktd->ktd_qcount++;
    348 	if (ktd->ktd_flags & KTDF_BLOCKING)
    349 		goto skip_sync;
    350 
    351 	if (flags & KTA_WAITOK &&
    352 	    (/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT ||
    353 	    ktd->ktd_qcount > ktd_maxentry >> 1))
    354 		/*
    355 		 * Sync with writer thread since we're requesting rather
    356 		 * big one or many requests are pending.
    357 		 */
    358 		do {
    359 			ktd->ktd_flags |= KTDF_WAIT;
    360 			ktd_wakeup(ktd);
    361 #ifdef DEBUG
    362 			getmicrouptime(&t1);
    363 #endif
    364 			if (cv_timedwait(&ktd->ktd_sync_cv, &ktrace_lock,
    365 			    ktd_timeout * hz) != 0) {
    366 				ktd->ktd_flags |= KTDF_BLOCKING;
    367 				/*
    368 				 * Maybe the writer thread is blocking
    369 				 * completely for some reason, but
    370 				 * don't stop target process forever.
    371 				 */
    372 				log(LOG_NOTICE, "ktrace timeout\n");
    373 				break;
    374 			}
    375 #ifdef DEBUG
    376 			getmicrouptime(&t2);
    377 			timersub(&t2, &t1, &t2);
    378 			if (t2.tv_sec > 0)
    379 				log(LOG_NOTICE,
    380 				    "ktrace long wait: %ld.%06ld\n",
    381 				    t2.tv_sec, t2.tv_usec);
    382 #endif
    383 		} while (p->p_tracep == ktd &&
    384 		    (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
    385 	else {
    386 		/* Schedule delayed wakeup */
    387 		if (ktd->ktd_qcount > ktd->ktd_delayqcnt)
    388 			ktd_wakeup(ktd);	/* Wakeup now */
    389 		else if (!callout_pending(&ktd->ktd_wakch))
    390 			callout_reset(&ktd->ktd_wakch,
    391 			    ktd->ktd_flags & KTDF_INTERACTIVE ?
    392 			    ktd->ktd_intrwakdl : ktd->ktd_wakedelay,
    393 			    ktd_callout, ktd);
    394 	}
    395 
    396 skip_sync:
    397 	ktdrel(ktd);
    398 	mutex_exit(&ktrace_lock);
    399 	ktrexit(l);
    400 	return;
    401 
    402 relktd:
    403 	ktdrel(ktd);
    404 
    405 freekte:
    406 	mutex_exit(&ktrace_lock);
    407 	ktefree(kte);
    408 	ktrexit(l);
    409 }
    410 
    411 void
    412 ktefree(struct ktrace_entry *kte)
    413 {
    414 
    415 	KERNEL_LOCK(1, curlwp);			/* XXXSMP */
    416 	if (kte->kte_buf != kte->kte_space)
    417 		kmem_free(kte->kte_buf, kte->kte_bufsz);
    418 	pool_put(&kte_pool, kte);
    419 	KERNEL_UNLOCK_ONE(curlwp);		/* XXXSMP */
    420 }
    421 
    422 /*
    423  * "deep" compare of two files for the purposes of clearing a trace.
    424  * Returns true if they're the same open file, or if they point at the
    425  * same underlying vnode/socket.
    426  */
    427 
    428 int
    429 ktrsamefile(struct file *f1, struct file *f2)
    430 {
    431 
    432 	return ((f1 == f2) ||
    433 	    ((f1 != NULL) && (f2 != NULL) &&
    434 		(f1->f_type == f2->f_type) &&
    435 		(f1->f_data == f2->f_data)));
    436 }
    437 
    438 void
    439 ktrderef(struct proc *p)
    440 {
    441 	struct ktr_desc *ktd = p->p_tracep;
    442 
    443 	KASSERT(mutex_owned(&ktrace_lock));
    444 
    445 	p->p_traceflag = 0;
    446 	if (ktd == NULL)
    447 		return;
    448 	p->p_tracep = NULL;
    449 
    450 	cv_broadcast(&ktd->ktd_sync_cv);
    451 	ktdrel(ktd);
    452 }
    453 
    454 void
    455 ktradref(struct proc *p)
    456 {
    457 	struct ktr_desc *ktd = p->p_tracep;
    458 
    459 	KASSERT(mutex_owned(&ktrace_lock));
    460 
    461 	ktdref(ktd);
    462 }
    463 
    464 int
    465 ktrderefall(struct ktr_desc *ktd, int auth)
    466 {
    467 	lwp_t *curl = curlwp;
    468 	struct proc *p;
    469 	int error = 0;
    470 
    471 	mutex_enter(&proclist_lock);
    472 	PROCLIST_FOREACH(p, &allproc) {
    473 		if (p->p_tracep != ktd)
    474 			continue;
    475 		mutex_enter(&p->p_mutex);
    476 		mutex_enter(&ktrace_lock);
    477 		if (p->p_tracep == ktd) {
    478 			if (!auth || ktrcanset(curl, p))
    479 				ktrderef(p);
    480 			else
    481 				error = EPERM;
    482 		}
    483 		mutex_exit(&ktrace_lock);
    484 		mutex_exit(&p->p_mutex);
    485 	}
    486 	mutex_exit(&proclist_lock);
    487 
    488 	return error;
    489 }
    490 
    491 int
    492 ktealloc(struct ktrace_entry **ktep, void **bufp, lwp_t *l, int type,
    493 	 size_t sz)
    494 {
    495 	struct proc *p = l->l_proc;
    496 	struct ktrace_entry *kte;
    497 	struct ktr_header *kth;
    498 	void *buf;
    499 
    500 	if (ktrenter(l))
    501 		return EAGAIN;
    502 
    503 	KERNEL_LOCK(1, l);			/* XXXSMP */
    504 	kte = pool_get(&kte_pool, PR_WAITOK);
    505 	if (sz > sizeof(kte->kte_space)) {
    506 		if ((buf = kmem_alloc(sz, KM_SLEEP)) == NULL) {
    507 			pool_put(&kte_pool, kte);
    508 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    509 			ktrexit(l);
    510 			return ENOMEM;
    511 		}
    512 	} else
    513 		buf = kte->kte_space;
    514 	KERNEL_UNLOCK_ONE(l);			/* XXXSMP */
    515 
    516 	kte->kte_bufsz = sz;
    517 	kte->kte_buf = buf;
    518 
    519 	kth = &kte->kte_kth;
    520 	(void)memset(kth, 0, sizeof(*kth));
    521 	kth->ktr_len = sz;
    522 	kth->ktr_type = type;
    523 	kth->ktr_pid = p->p_pid;
    524 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    525 	kth->ktr_version = KTRFAC_VERSION(p->p_traceflag);
    526 
    527 	switch (KTRFAC_VERSION(p->p_traceflag)) {
    528 	case 0:
    529 		/* This is the original format */
    530 		microtime(&kth->ktr_tv);
    531 		break;
    532 	case 1:
    533 		kth->ktr_lid = l->l_lid;
    534 		nanotime(&kth->ktr_time);
    535 		break;
    536 	default:
    537 		break;
    538 	}
    539 
    540 	*ktep = kte;
    541 	*bufp = buf;
    542 
    543 	return 0;
    544 }
    545 
    546 void
    547 ktr_syscall(register_t code, register_t realcode,
    548 	    const struct sysent *callp, register_t args[])
    549 {
    550 	lwp_t *l = curlwp;
    551 	struct proc *p = l->l_proc;
    552 	struct ktrace_entry *kte;
    553 	struct ktr_syscall *ktp;
    554 	register_t *argp;
    555 	int argsize;
    556 	size_t len;
    557 	u_int i;
    558 
    559 	if (!KTRPOINT(p, KTR_SYSCALL))
    560 		return;
    561 
    562 	if (callp == NULL)
    563 		callp = p->p_emul->e_sysent;
    564 
    565 	argsize = callp[code].sy_argsize;
    566 #ifdef _LP64
    567 	if (p->p_flag & PK_32)
    568 		argsize = argsize << 1;
    569 #endif
    570 	len = sizeof(struct ktr_syscall) + argsize;
    571 
    572 	if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSCALL, len))
    573 		return;
    574 
    575 	ktp->ktr_code = realcode;
    576 	ktp->ktr_argsize = argsize;
    577 	argp = (register_t *)(ktp + 1);
    578 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    579 		*argp++ = args[i];
    580 
    581 	ktraddentry(l, kte, KTA_WAITOK);
    582 }
    583 
    584 void
    585 ktr_sysret(register_t code, int error, register_t *retval)
    586 {
    587 	lwp_t *l = curlwp;
    588 	struct ktrace_entry *kte;
    589 	struct ktr_sysret *ktp;
    590 
    591 	if (!KTRPOINT(l->l_proc, KTR_SYSRET))
    592 		return;
    593 
    594 	if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSRET,
    595 	    sizeof(struct ktr_sysret)))
    596 		return;
    597 
    598 	ktp->ktr_code = code;
    599 	ktp->ktr_eosys = 0;			/* XXX unused */
    600 	ktp->ktr_error = error;
    601 	ktp->ktr_retval = retval ? retval[0] : 0;
    602 	ktp->ktr_retval_1 = retval ? retval[1] : 0;
    603 
    604 	ktraddentry(l, kte, KTA_WAITOK);
    605 }
    606 
    607 void
    608 ktr_namei(const char *path, size_t pathlen)
    609 {
    610 	lwp_t *l = curlwp;
    611 
    612 	if (!KTRPOINT(l->l_proc, KTR_NAMEI))
    613 		return;
    614 
    615 	ktr_kmem(l, KTR_NAMEI, path, pathlen);
    616 }
    617 
    618 void
    619 ktr_namei2(const char *eroot, size_t erootlen,
    620 	  const char *path, size_t pathlen)
    621 {
    622 	lwp_t *l = curlwp;
    623 	struct ktrace_entry *kte;
    624 	void *buf;
    625 
    626 	if (!KTRPOINT(l->l_proc, KTR_NAMEI))
    627 		return;
    628 
    629 	if (ktealloc(&kte, &buf, l, KTR_NAMEI, erootlen + pathlen))
    630 		return;
    631 	memcpy(buf, eroot, erootlen);
    632 	buf = (char *)buf + erootlen;
    633 	memcpy(buf, path, pathlen);
    634 	ktraddentry(l, kte, KTA_WAITOK);
    635 }
    636 
    637 void
    638 ktr_emul(void)
    639 {
    640 	lwp_t *l = curlwp;
    641 	const char *emul = l->l_proc->p_emul->e_name;
    642 
    643 	if (!KTRPOINT(l->l_proc, KTR_EMUL))
    644 		return;
    645 
    646 	ktr_kmem(l, KTR_EMUL, emul, strlen(emul));
    647 }
    648 
    649 void
    650 ktr_execarg(const void *bf, size_t len)
    651 {
    652 	lwp_t *l = curlwp;
    653 
    654 	if (!KTRPOINT(l->l_proc, KTR_EXEC_ARG))
    655 		return;
    656 
    657 	ktr_kmem(l, KTR_EXEC_ARG, bf, len);
    658 }
    659 
    660 void
    661 ktr_execenv(const void *bf, size_t len)
    662 {
    663 	lwp_t *l = curlwp;
    664 
    665 	if (!KTRPOINT(l->l_proc, KTR_EXEC_ENV))
    666 		return;
    667 
    668 	ktr_kmem(l, KTR_EXEC_ENV, bf, len);
    669 }
    670 
    671 static void
    672 ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
    673 {
    674 	struct ktrace_entry *kte;
    675 	void *buf;
    676 
    677 	if (ktealloc(&kte, &buf, l, type, len))
    678 		return;
    679 	memcpy(buf, bf, len);
    680 	ktraddentry(l, kte, KTA_WAITOK);
    681 }
    682 
    683 static void
    684 ktr_io(lwp_t *l, int fd, enum uio_rw rw, struct iovec *iov, size_t len)
    685 {
    686 	struct ktrace_entry *kte;
    687 	struct ktr_genio *ktp;
    688 	size_t resid = len, cnt, buflen;
    689 	void *cp;
    690 
    691  next:
    692 	buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
    693 
    694 	if (ktealloc(&kte, (void *)&ktp, l, KTR_GENIO, buflen))
    695 		return;
    696 
    697 	ktp->ktr_fd = fd;
    698 	ktp->ktr_rw = rw;
    699 
    700 	cp = (void *)(ktp + 1);
    701 	buflen -= sizeof(struct ktr_genio);
    702 	kte->kte_kth.ktr_len = sizeof(struct ktr_genio);
    703 
    704 	while (buflen > 0) {
    705 		cnt = min(iov->iov_len, buflen);
    706 		if (copyin(iov->iov_base, cp, cnt) != 0)
    707 			goto out;
    708 		kte->kte_kth.ktr_len += cnt;
    709 		buflen -= cnt;
    710 		resid -= cnt;
    711 		iov->iov_len -= cnt;
    712 		if (iov->iov_len == 0)
    713 			iov++;
    714 		else
    715 			iov->iov_base = (char *)iov->iov_base + cnt;
    716 	}
    717 
    718 	/*
    719 	 * Don't push so many entry at once.  It will cause kmem map
    720 	 * shortage.
    721 	 */
    722 	ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
    723 	if (resid > 0) {
    724 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
    725 			(void)ktrenter(l);
    726 			preempt();
    727 			ktrexit(l);
    728 		}
    729 
    730 		goto next;
    731 	}
    732 
    733 	return;
    734 
    735 out:
    736 	ktefree(kte);
    737 	ktrexit(l);
    738 }
    739 
    740 void
    741 ktr_genio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
    742 {
    743 	lwp_t *l = curlwp;
    744 	struct iovec iov;
    745 
    746 	if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
    747 		return;
    748 	iov.iov_base = __UNCONST(addr);
    749 	iov.iov_len = len;
    750 	ktr_io(l, fd, rw, &iov, len);
    751 }
    752 
    753 void
    754 ktr_geniov(int fd, enum uio_rw rw, struct iovec *iov, size_t len, int error)
    755 {
    756 	lwp_t *l = curlwp;
    757 
    758 	if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
    759 		return;
    760 	ktr_io(l, fd, rw, iov, len);
    761 }
    762 
    763 void
    764 ktr_mibio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
    765 {
    766 	lwp_t *l = curlwp;
    767 	struct iovec iov;
    768 
    769 	if (!KTRPOINT(l->l_proc, KTR_MIB) || error != 0)
    770 		return;
    771 	iov.iov_base = __UNCONST(addr);
    772 	iov.iov_len = len;
    773 	ktr_io(l, fd, rw, &iov, len);
    774 }
    775 
    776 void
    777 ktr_psig(int sig, sig_t action, const sigset_t *mask,
    778 	 const ksiginfo_t *ksi)
    779 {
    780 	struct ktrace_entry *kte;
    781 	lwp_t *l = curlwp;
    782 	struct {
    783 		struct ktr_psig	kp;
    784 		siginfo_t	si;
    785 	} *kbuf;
    786 
    787 	if (!KTRPOINT(l->l_proc, KTR_PSIG))
    788 		return;
    789 
    790 	if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf)))
    791 		return;
    792 
    793 	kbuf->kp.signo = (char)sig;
    794 	kbuf->kp.action = action;
    795 	kbuf->kp.mask = *mask;
    796 
    797 	if (ksi) {
    798 		kbuf->kp.code = KSI_TRAPCODE(ksi);
    799 		(void)memset(&kbuf->si, 0, sizeof(kbuf->si));
    800 		kbuf->si._info = ksi->ksi_info;
    801 		kte->kte_kth.ktr_len = sizeof(*kbuf);
    802 	} else {
    803 		kbuf->kp.code = 0;
    804 		kte->kte_kth.ktr_len = sizeof(struct ktr_psig);
    805 	}
    806 
    807 	ktraddentry(l, kte, KTA_WAITOK);
    808 }
    809 
    810 void
    811 ktr_csw(int out, int user)
    812 {
    813 	lwp_t *l = curlwp;
    814 	struct proc *p = l->l_proc;
    815 	struct ktrace_entry *kte;
    816 	struct ktr_csw *kc;
    817 
    818 	if (!KTRPOINT(p, KTR_CSW))
    819 		return;
    820 
    821 	/*
    822 	 * Don't record context switches resulting from blocking on
    823 	 * locks; it's too easy to get duff results.
    824 	 */
    825 	if (l->l_syncobj == &mutex_syncobj || l->l_syncobj == &rw_syncobj)
    826 		return;
    827 
    828 	/*
    829 	 * We can't sleep if we're already going to sleep (if original
    830 	 * condition is met during sleep, we hang up).
    831 	 *
    832 	 * XXX This is not ideal: it would be better to maintain a pool
    833 	 * of ktes and actually push this to the kthread when context
    834 	 * switch happens, however given the points where we are called
    835 	 * from that is difficult to do.
    836 	 */
    837 	if (out) {
    838 		if (ktrenter(l))
    839 			return;
    840 
    841 		switch (KTRFAC_VERSION(p->p_traceflag)) {
    842 		case 0:
    843 			/* This is the original format */
    844 			microtime(&l->l_ktrcsw.tv);
    845 			l->l_pflag |= LP_KTRCSW;
    846 			break;
    847 		case 1:
    848 			nanotime(&l->l_ktrcsw.ts);
    849 			l->l_pflag |= LP_KTRCSW;
    850 			break;
    851 		default:
    852 			break;
    853 		}
    854 
    855 		if (user)
    856 			l->l_pflag |= LP_KTRCSWUSER;
    857 		else
    858 			l->l_pflag &= ~LP_KTRCSWUSER;
    859 
    860 		ktrexit(l);
    861 		return;
    862 	}
    863 
    864 	/*
    865 	 * On the way back in, we need to record twice: once for entry, and
    866 	 * once for exit.
    867 	 */
    868 	if ((l->l_pflag & LP_KTRCSW) != 0) {
    869 		l->l_pflag &= ~LP_KTRCSW;
    870 
    871 		if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
    872 			return;
    873 
    874 		kc->out = 1;
    875 		kc->user = ((l->l_pflag & LP_KTRCSWUSER) != 0);
    876 
    877 		switch (KTRFAC_VERSION(p->p_traceflag)) {
    878 		case 0:
    879 			/* This is the original format */
    880 			memcpy(&kte->kte_kth.ktr_tv, &l->l_ktrcsw.tv,
    881 			    sizeof(kte->kte_kth.ktr_tv));
    882 			break;
    883 		case 1:
    884 			memcpy(&kte->kte_kth.ktr_time, &l->l_ktrcsw.ts,
    885 			    sizeof(kte->kte_kth.ktr_time));
    886 			break;
    887 		default:
    888 			break;
    889 		}
    890 
    891 		ktraddentry(l, kte, KTA_WAITOK);
    892 	}
    893 
    894 	if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
    895 		return;
    896 
    897 	kc->out = 0;
    898 	kc->user = user;
    899 
    900 	ktraddentry(l, kte, KTA_WAITOK);
    901 }
    902 
    903 bool
    904 ktr_point(int fac_bit)
    905 {
    906 	return curlwp->l_proc->p_traceflag & fac_bit;
    907 }
    908 
    909 int
    910 ktruser(const char *id, void *addr, size_t len, int ustr)
    911 {
    912 	struct ktrace_entry *kte;
    913 	struct ktr_user *ktp;
    914 	lwp_t *l = curlwp;
    915 	void *user_dta;
    916 	int error;
    917 
    918 	if (!KTRPOINT(l->l_proc, KTR_USER))
    919 		return 0;
    920 
    921 	if (len > KTR_USER_MAXLEN)
    922 		return ENOSPC;
    923 
    924 	error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
    925 	if (error != 0)
    926 		return error;
    927 
    928 	if (ustr) {
    929 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    930 			ktp->ktr_id[0] = '\0';
    931 	} else
    932 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    933 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    934 
    935 	user_dta = (void *)(ktp + 1);
    936 	if ((error = copyin(addr, (void *)user_dta, len)) != 0)
    937 		len = 0;
    938 
    939 	ktraddentry(l, kte, KTA_WAITOK);
    940 	return error;
    941 }
    942 
    943 void
    944 ktr_kuser(const char *id, void *addr, size_t len)
    945 {
    946 	struct ktrace_entry *kte;
    947 	struct ktr_user *ktp;
    948 	lwp_t *l = curlwp;
    949 	int error;
    950 
    951 	if (!KTRPOINT(l->l_proc, KTR_USER))
    952 		return;
    953 
    954 	if (len > KTR_USER_MAXLEN)
    955 		return;
    956 
    957 	error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
    958 	if (error != 0)
    959 		return;
    960 
    961 	strlcpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    962 
    963 	memcpy(ktp + 1, addr, len);
    964 
    965 	ktraddentry(l, kte, KTA_WAITOK);
    966 }
    967 
    968 void
    969 ktr_mmsg(const void *msgh, size_t size)
    970 {
    971 	lwp_t *l = curlwp;
    972 
    973 	if (!KTRPOINT(l->l_proc, KTR_MMSG))
    974 		return;
    975 
    976 	ktr_kmem(l, KTR_MMSG, msgh, size);
    977 }
    978 
    979 void
    980 ktr_mool(const void *kaddr, size_t size, const void *uaddr)
    981 {
    982 	struct ktrace_entry *kte;
    983 	struct ktr_mool *kp;
    984 	struct ktr_mool *bf;
    985 	lwp_t *l = curlwp;
    986 
    987 	if (!KTRPOINT(l->l_proc, KTR_MOOL))
    988 		return;
    989 
    990 	if (ktealloc(&kte, (void *)&kp, l, KTR_MOOL, size + sizeof(*kp)))
    991 		return;
    992 
    993 	kp->uaddr = uaddr;
    994 	kp->size = size;
    995 	bf = kp + 1; /* Skip uaddr and size */
    996 	(void)memcpy(bf, kaddr, size);
    997 
    998 	ktraddentry(l, kte, KTA_WAITOK);
    999 }
   1000 
   1001 void
   1002 ktr_mib(const int *name, u_int namelen)
   1003 {
   1004 	struct ktrace_entry *kte;
   1005 	int *namep;
   1006 	size_t size;
   1007 	lwp_t *l = curlwp;
   1008 
   1009 	if (!KTRPOINT(l->l_proc, KTR_MIB))
   1010 		return;
   1011 
   1012 	size = namelen * sizeof(*name);
   1013 
   1014 	if (ktealloc(&kte, (void *)&namep, l, KTR_MIB, size))
   1015 		return;
   1016 
   1017 	(void)memcpy(namep, name, namelen * sizeof(*name));
   1018 
   1019 	ktraddentry(l, kte, KTA_WAITOK);
   1020 }
   1021 
   1022 /* Interface and common routines */
   1023 
   1024 int
   1025 ktrace_common(lwp_t *curl, int ops, int facs, int pid, struct file *fp)
   1026 {
   1027 	struct proc *curp;
   1028 	struct proc *p;
   1029 	struct pgrp *pg;
   1030 	struct ktr_desc *ktd = NULL;
   1031 	int ret = 0;
   1032 	int error = 0;
   1033 	int descend;
   1034 
   1035 	curp = curl->l_proc;
   1036 	descend = ops & KTRFLAG_DESCEND;
   1037 	facs = facs & ~((unsigned) KTRFAC_ROOT);
   1038 
   1039 	(void)ktrenter(curl);
   1040 
   1041 	switch (KTROP(ops)) {
   1042 
   1043 	case KTROP_CLEARFILE:
   1044 		/*
   1045 		 * Clear all uses of the tracefile
   1046 		 */
   1047 		mutex_enter(&ktrace_lock);
   1048 		ktd = ktd_lookup(fp);
   1049 		mutex_exit(&ktrace_lock);
   1050 		if (ktd == NULL)
   1051 			goto done;
   1052 		error = ktrderefall(ktd, 1);
   1053 		goto done;
   1054 
   1055 	case KTROP_SET:
   1056 		mutex_enter(&ktrace_lock);
   1057 		ktd = ktd_lookup(fp);
   1058 		mutex_exit(&ktrace_lock);
   1059 		if (ktd == NULL) {
   1060 			ktd = kmem_alloc(sizeof(*ktd), KM_SLEEP);
   1061 			TAILQ_INIT(&ktd->ktd_queue);
   1062 			callout_init(&ktd->ktd_wakch, 0);
   1063 			cv_init(&ktd->ktd_cv, "ktrwait");
   1064 			cv_init(&ktd->ktd_sync_cv, "ktrsync");
   1065 			ktd->ktd_flags = 0;
   1066 			ktd->ktd_qcount = 0;
   1067 			ktd->ktd_error = 0;
   1068 			ktd->ktd_errcnt = 0;
   1069 			ktd->ktd_delayqcnt = ktd_delayqcnt;
   1070 			ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
   1071 			ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
   1072 			ktd->ktd_ref = 0;
   1073 			mutex_enter(&ktrace_lock);
   1074 			ktdref(ktd);
   1075 			mutex_exit(&ktrace_lock);
   1076 
   1077 			/*
   1078 			 * XXX: not correct.  needs an way to detect
   1079 			 * whether ktruss or ktrace.
   1080 			 */
   1081 			if (fp->f_type == DTYPE_PIPE)
   1082 				ktd->ktd_flags |= KTDF_INTERACTIVE;
   1083 
   1084 			error = kthread_create(PRI_NONE, 0, NULL,
   1085 			    ktrace_thread, ktd, &ktd->ktd_lwp, "ktrace");
   1086 			if (error != 0) {
   1087 				kmem_free(ktd, sizeof(*ktd));
   1088 				goto done;
   1089 			}
   1090 
   1091 			mutex_enter(&fp->f_lock);
   1092 			fp->f_count++;
   1093 			mutex_exit(&fp->f_lock);
   1094 			ktd->ktd_fp = fp;
   1095 
   1096 			mutex_enter(&ktrace_lock);
   1097 			if (ktd_lookup(fp) != NULL) {
   1098 				ktdrel(ktd);
   1099 				ktd = NULL;
   1100 			} else
   1101 				TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
   1102 			if (ktd == NULL)
   1103 				cv_wait(&lbolt, &ktrace_lock);
   1104 			mutex_exit(&ktrace_lock);
   1105 			if (ktd == NULL)
   1106 				goto done;
   1107 		}
   1108 		break;
   1109 
   1110 	case KTROP_CLEAR:
   1111 		break;
   1112 	}
   1113 
   1114 	/*
   1115 	 * need something to (un)trace (XXX - why is this here?)
   1116 	 */
   1117 	if (!facs) {
   1118 		error = EINVAL;
   1119 		goto done;
   1120 	}
   1121 
   1122 	/*
   1123 	 * do it
   1124 	 */
   1125 	mutex_enter(&proclist_lock);
   1126 	if (pid < 0) {
   1127 		/*
   1128 		 * by process group
   1129 		 */
   1130 		pg = pg_find(-pid, PFIND_LOCKED);
   1131 		if (pg == NULL)
   1132 			error = ESRCH;
   1133 		else {
   1134 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
   1135 				if (descend)
   1136 					ret |= ktrsetchildren(curl, p, ops,
   1137 					    facs, ktd);
   1138 				else
   1139 					ret |= ktrops(curl, p, ops, facs,
   1140 					    ktd);
   1141 			}
   1142 		}
   1143 
   1144 	} else {
   1145 		/*
   1146 		 * by pid
   1147 		 */
   1148 		p = p_find(pid, PFIND_LOCKED);
   1149 		if (p == NULL)
   1150 			error = ESRCH;
   1151 		else if (descend)
   1152 			ret |= ktrsetchildren(curl, p, ops, facs, ktd);
   1153 		else
   1154 			ret |= ktrops(curl, p, ops, facs, ktd);
   1155 	}
   1156 	mutex_exit(&proclist_lock);
   1157 	if (error == 0 && !ret)
   1158 		error = EPERM;
   1159 done:
   1160 	if (ktd != NULL) {
   1161 		mutex_enter(&ktrace_lock);
   1162 		if (error != 0) {
   1163 			/*
   1164 			 * Wakeup the thread so that it can be die if we
   1165 			 * can't trace any process.
   1166 			 */
   1167 			ktd_wakeup(ktd);
   1168 		}
   1169 		if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE)
   1170 			ktdrel(ktd);
   1171 		mutex_exit(&ktrace_lock);
   1172 	}
   1173 	ktrexit(curl);
   1174 	return (error);
   1175 }
   1176 
   1177 /*
   1178  * fktrace system call
   1179  */
   1180 /* ARGSUSED */
   1181 int
   1182 sys_fktrace(lwp_t *l, void *v, register_t *retval)
   1183 {
   1184 	struct sys_fktrace_args /* {
   1185 		syscallarg(int) fd;
   1186 		syscallarg(int) ops;
   1187 		syscallarg(int) facs;
   1188 		syscallarg(int) pid;
   1189 	} */ *uap = v;
   1190 	struct file *fp = NULL;
   1191 	struct filedesc *fdp = l->l_proc->p_fd;
   1192 	int error;
   1193 
   1194 	fdp = l->l_proc->p_fd;
   1195 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
   1196 		return (EBADF);
   1197 
   1198 	FILE_USE(fp);
   1199 
   1200 	if ((fp->f_flag & FWRITE) == 0)
   1201 		error = EBADF;
   1202 	else
   1203 		error = ktrace_common(l, SCARG(uap, ops),
   1204 		    SCARG(uap, facs), SCARG(uap, pid), fp);
   1205 
   1206 	FILE_UNUSE(fp, l);
   1207 
   1208 	return error;
   1209 }
   1210 
   1211 /*
   1212  * ktrace system call
   1213  */
   1214 /* ARGSUSED */
   1215 int
   1216 sys_ktrace(lwp_t *l, void *v, register_t *retval)
   1217 {
   1218 	struct sys_ktrace_args /* {
   1219 		syscallarg(const char *) fname;
   1220 		syscallarg(int) ops;
   1221 		syscallarg(int) facs;
   1222 		syscallarg(int) pid;
   1223 	} */ *uap = v;
   1224 	struct vnode *vp = NULL;
   1225 	struct file *fp = NULL;
   1226 	struct nameidata nd;
   1227 	int error = 0;
   1228 	int fd;
   1229 
   1230 	if (ktrenter(l))
   1231 		return EAGAIN;
   1232 
   1233 	if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) {
   1234 		/*
   1235 		 * an operation which requires a file argument.
   1236 		 */
   1237 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname));
   1238 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
   1239 			ktrexit(l);
   1240 			return (error);
   1241 		}
   1242 		vp = nd.ni_vp;
   1243 		VOP_UNLOCK(vp, 0);
   1244 		if (vp->v_type != VREG) {
   1245 			(void) vn_close(vp, FREAD|FWRITE, l->l_cred, l);
   1246 			ktrexit(l);
   1247 			return (EACCES);
   1248 		}
   1249 		/*
   1250 		 * XXX This uses up a file descriptor slot in the
   1251 		 * tracing process for the duration of this syscall.
   1252 		 * This is not expected to be a problem.  If
   1253 		 * falloc(NULL, ...) DTRT we could skip that part, but
   1254 		 * that would require changing its interface to allow
   1255 		 * the caller to pass in a ucred..
   1256 		 *
   1257 		 * This will FILE_USE the fp it returns, if any.
   1258 		 * Keep it in use until we return.
   1259 		 */
   1260 		if ((error = falloc(l, &fp, &fd)) != 0)
   1261 			goto done;
   1262 
   1263 		fp->f_flag = FWRITE;
   1264 		fp->f_type = DTYPE_VNODE;
   1265 		fp->f_ops = &vnops;
   1266 		fp->f_data = (void *)vp;
   1267 		FILE_SET_MATURE(fp);
   1268 		vp = NULL;
   1269 	}
   1270 	error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
   1271 	    SCARG(uap, pid), fp);
   1272 done:
   1273 	if (vp != NULL)
   1274 		(void) vn_close(vp, FWRITE, l->l_cred, l);
   1275 	if (fp != NULL) {
   1276 		FILE_UNUSE(fp, l);	/* release file */
   1277 		fdrelease(l, fd); 	/* release fd table slot */
   1278 	}
   1279 	return (error);
   1280 }
   1281 
   1282 int
   1283 ktrops(lwp_t *curl, struct proc *p, int ops, int facs,
   1284     struct ktr_desc *ktd)
   1285 {
   1286 	int vers = ops & KTRFAC_VER_MASK;
   1287 	int error = 0;
   1288 
   1289 	mutex_enter(&p->p_mutex);
   1290 	mutex_enter(&ktrace_lock);
   1291 
   1292 	if (!ktrcanset(curl, p))
   1293 		goto out;
   1294 
   1295 	switch (vers) {
   1296 	case KTRFACv0:
   1297 	case KTRFACv1:
   1298 		break;
   1299 	default:
   1300 		error = EINVAL;
   1301 		goto out;
   1302 	}
   1303 
   1304 	if (KTROP(ops) == KTROP_SET) {
   1305 		if (p->p_tracep != ktd) {
   1306 			/*
   1307 			 * if trace file already in use, relinquish
   1308 			 */
   1309 			ktrderef(p);
   1310 			p->p_tracep = ktd;
   1311 			ktradref(p);
   1312 		}
   1313 		p->p_traceflag |= facs;
   1314 		if (kauth_authorize_generic(curl->l_cred,
   1315 		    KAUTH_GENERIC_ISSUSER, NULL) == 0)
   1316 			p->p_traceflag |= KTRFAC_ROOT;
   1317 	} else {
   1318 		/* KTROP_CLEAR */
   1319 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
   1320 			/* no more tracing */
   1321 			ktrderef(p);
   1322 		}
   1323 	}
   1324 
   1325 	if (p->p_traceflag)
   1326 		p->p_traceflag |= vers;
   1327 	/*
   1328 	 * Emit an emulation record, every time there is a ktrace
   1329 	 * change/attach request.
   1330 	 */
   1331 	if (KTRPOINT(p, KTR_EMUL))
   1332 		p->p_traceflag |= KTRFAC_TRC_EMUL;
   1333 #ifdef __HAVE_SYSCALL_INTERN
   1334 	(*p->p_emul->e_syscall_intern)(p);
   1335 #endif
   1336 
   1337  out:
   1338  	mutex_exit(&ktrace_lock);
   1339  	mutex_exit(&p->p_mutex);
   1340 
   1341 	return (1);
   1342 }
   1343 
   1344 int
   1345 ktrsetchildren(lwp_t *curl, struct proc *top, int ops, int facs,
   1346     struct ktr_desc *ktd)
   1347 {
   1348 	struct proc *p;
   1349 	int ret = 0;
   1350 
   1351 	KASSERT(mutex_owned(&proclist_lock));
   1352 
   1353 	p = top;
   1354 	for (;;) {
   1355 		ret |= ktrops(curl, p, ops, facs, ktd);
   1356 		/*
   1357 		 * If this process has children, descend to them next,
   1358 		 * otherwise do any siblings, and if done with this level,
   1359 		 * follow back up the tree (but not past top).
   1360 		 */
   1361 		if (LIST_FIRST(&p->p_children) != NULL) {
   1362 			p = LIST_FIRST(&p->p_children);
   1363 			continue;
   1364 		}
   1365 		for (;;) {
   1366 			if (p == top)
   1367 				return (ret);
   1368 			if (LIST_NEXT(p, p_sibling) != NULL) {
   1369 				p = LIST_NEXT(p, p_sibling);
   1370 				break;
   1371 			}
   1372 			p = p->p_pptr;
   1373 		}
   1374 	}
   1375 	/*NOTREACHED*/
   1376 }
   1377 
   1378 void
   1379 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
   1380 {
   1381 	struct uio auio;
   1382 	struct iovec aiov[64], *iov;
   1383 	struct ktrace_entry *top = kte;
   1384 	struct ktr_header *kth;
   1385 	struct file *fp = ktd->ktd_fp;
   1386 	int error;
   1387 next:
   1388 	auio.uio_iov = iov = &aiov[0];
   1389 	auio.uio_offset = 0;
   1390 	auio.uio_rw = UIO_WRITE;
   1391 	auio.uio_resid = 0;
   1392 	auio.uio_iovcnt = 0;
   1393 	UIO_SETUP_SYSSPACE(&auio);
   1394 	do {
   1395 		kth = &kte->kte_kth;
   1396 
   1397 		if (kth->ktr_version == 0) {
   1398 			/*
   1399 			 * Convert back to the old format fields
   1400 			 */
   1401 			TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time);
   1402 			kth->ktr_unused = NULL;
   1403 		}
   1404 		iov->iov_base = (void *)kth;
   1405 		iov++->iov_len = sizeof(struct ktr_header);
   1406 		auio.uio_resid += sizeof(struct ktr_header);
   1407 		auio.uio_iovcnt++;
   1408 		if (kth->ktr_len > 0) {
   1409 			iov->iov_base = kte->kte_buf;
   1410 			iov++->iov_len = kth->ktr_len;
   1411 			auio.uio_resid += kth->ktr_len;
   1412 			auio.uio_iovcnt++;
   1413 		}
   1414 	} while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
   1415 	    auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
   1416 
   1417 again:
   1418 	mutex_enter(&fp->f_lock);
   1419 	FILE_USE(fp);
   1420 	error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
   1421 	    fp->f_cred, FOF_UPDATE_OFFSET);
   1422 	FILE_UNUSE(fp, NULL);
   1423 	switch (error) {
   1424 
   1425 	case 0:
   1426 		if (auio.uio_resid > 0)
   1427 			goto again;
   1428 		if (kte != NULL)
   1429 			goto next;
   1430 		break;
   1431 
   1432 	case EWOULDBLOCK:
   1433 		kpause("ktrzzz", false, 1, NULL);
   1434 		goto again;
   1435 
   1436 	default:
   1437 		/*
   1438 		 * If error encountered, give up tracing on this
   1439 		 * vnode.  Don't report EPIPE as this can easily
   1440 		 * happen with fktrace()/ktruss.
   1441 		 */
   1442 #ifndef DEBUG
   1443 		if (error != EPIPE)
   1444 #endif
   1445 			log(LOG_NOTICE,
   1446 			    "ktrace write failed, errno %d, tracing stopped\n",
   1447 			    error);
   1448 		(void)ktrderefall(ktd, 0);
   1449 	}
   1450 
   1451 	while ((kte = top) != NULL) {
   1452 		top = TAILQ_NEXT(top, kte_list);
   1453 		ktefree(kte);
   1454 	}
   1455 }
   1456 
   1457 void
   1458 ktrace_thread(void *arg)
   1459 {
   1460 	struct ktr_desc *ktd = arg;
   1461 	struct file *fp = ktd->ktd_fp;
   1462 	struct ktrace_entry *kte;
   1463 	int ktrerr, errcnt;
   1464 
   1465 	mutex_enter(&ktrace_lock);
   1466 	for (;;) {
   1467 		kte = TAILQ_FIRST(&ktd->ktd_queue);
   1468 		if (kte == NULL) {
   1469 			if (ktd->ktd_flags & KTDF_WAIT) {
   1470 				ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING);
   1471 				cv_broadcast(&ktd->ktd_sync_cv);
   1472 			}
   1473 			if (ktd->ktd_ref == 0)
   1474 				break;
   1475 			cv_wait(&ktd->ktd_cv, &ktrace_lock);
   1476 			continue;
   1477 		}
   1478 		TAILQ_INIT(&ktd->ktd_queue);
   1479 		ktd->ktd_qcount = 0;
   1480 		ktrerr = ktd->ktd_error;
   1481 		errcnt = ktd->ktd_errcnt;
   1482 		ktd->ktd_error = ktd->ktd_errcnt = 0;
   1483 		mutex_exit(&ktrace_lock);
   1484 
   1485 		if (ktrerr) {
   1486 			log(LOG_NOTICE,
   1487 			    "ktrace failed, fp %p, error 0x%x, total %d\n",
   1488 			    fp, ktrerr, errcnt);
   1489 		}
   1490 		ktrwrite(ktd, kte);
   1491 		mutex_enter(&ktrace_lock);
   1492 	}
   1493 
   1494 	TAILQ_REMOVE(&ktdq, ktd, ktd_list);
   1495 	mutex_exit(&ktrace_lock);
   1496 
   1497 	mutex_enter(&fp->f_lock);
   1498 	FILE_USE(fp);
   1499 
   1500 	/*
   1501 	 * ktrace file descriptor can't be watched (are not visible to
   1502 	 * userspace), so no kqueue stuff here
   1503 	 * XXX: The above comment is wrong, because the fktrace file
   1504 	 * descriptor is available in userland.
   1505 	 */
   1506 	closef(fp, NULL);
   1507 
   1508 	callout_stop(&ktd->ktd_wakch);
   1509 	callout_destroy(&ktd->ktd_wakch);
   1510 	kmem_free(ktd, sizeof(*ktd));
   1511 
   1512 	kthread_exit(0);
   1513 }
   1514 
   1515 /*
   1516  * Return true if caller has permission to set the ktracing state
   1517  * of target.  Essentially, the target can't possess any
   1518  * more permissions than the caller.  KTRFAC_ROOT signifies that
   1519  * root previously set the tracing status on the target process, and
   1520  * so, only root may further change it.
   1521  *
   1522  * TODO: check groups.  use caller effective gid.
   1523  */
   1524 int
   1525 ktrcanset(lwp_t *calll, struct proc *targetp)
   1526 {
   1527 	KASSERT(mutex_owned(&targetp->p_mutex));
   1528 	KASSERT(mutex_owned(&ktrace_lock));
   1529 
   1530 	if (kauth_authorize_process(calll->l_cred, KAUTH_PROCESS_CANKTRACE,
   1531 	    targetp, NULL, NULL, NULL) == 0)
   1532 		return (1);
   1533 
   1534 	return (0);
   1535 }
   1536 
   1537 /*
   1538  * Put user defined entry to ktrace records.
   1539  */
   1540 int
   1541 sys_utrace(lwp_t *l, void *v, register_t *retval)
   1542 {
   1543 	struct sys_utrace_args /* {
   1544 		syscallarg(const char *) label;
   1545 		syscallarg(void *) addr;
   1546 		syscallarg(size_t) len;
   1547 	} */ *uap = v;
   1548 
   1549 	return ktruser(SCARG(uap, label), SCARG(uap, addr),
   1550 	    SCARG(uap, len), 1);
   1551 }
   1552