Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.74.2.8
      1 /*	$NetBSD: kern_ktrace.c,v 1.74.2.8 2004/09/24 10:53:42 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.8 2004/09/24 10:53:42 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/kernel.h>
     47 #include <sys/kthread.h>
     48 #include <sys/ktrace.h>
     49 #include <sys/malloc.h>
     50 #include <sys/syslog.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/callout.h>
     54 
     55 #include <sys/mount.h>
     56 #include <sys/sa.h>
     57 #include <sys/syscallargs.h>
     58 
     59 #ifdef KTRACE
     60 
     61 /*
     62  * XXX:
     63  *	- need better error reporting?
     64  *	- p->p_tracep access lock.  lock p_lock, lock ktd if !NULL, inc ref.
     65  *	- userland utility to sort ktrace.out by timestamp.
     66  *	- keep minimum information in ktrace_entry when rest of alloc failed.
     67  *	- enlarge ktrace_entry so that small entry won't require additional
     68  *	  alloc?
     69  *	- per trace control of configurable parameters.
     70  */
     71 
     72 struct ktrace_entry {
     73 	TAILQ_ENTRY(ktrace_entry) kte_list;
     74 	struct ktr_header kte_kth;
     75 	void *kte_buf;			/* ktr_buf */
     76 };
     77 
     78 struct ktr_desc {
     79 	TAILQ_ENTRY(ktr_desc) ktd_list;
     80 	int ktd_flags;
     81 #define	KTDF_WAIT		0x0001
     82 #define	KTDF_DONE		0x0002
     83 #define	KTDF_BLOCKING		0x0004
     84 #define	KTDF_INTERACTIVE	0x0008
     85 	int ktd_error;
     86 #define	KTDE_ENOMEM		0x0001
     87 #define	KTDE_ENOSPC		0x0002
     88 	int ktd_errcnt;
     89 	int ktd_ref;			/* # of reference */
     90 	int ktd_qcount;			/* # of entry in the queue */
     91 
     92 	/*
     93 	 * Params to control behaviour.
     94 	 */
     95 	int ktd_delayqcnt;		/* # of entry allowed to delay */
     96 	int ktd_wakedelay;		/* delay of wakeup in *tick* */
     97 	int ktd_intrwakdl;		/* ditto, but when interactive */
     98 
     99 	struct file *ktd_fp;		/* trace output file */
    100 	struct proc *ktd_proc;		/* our kernel thread */
    101 	TAILQ_HEAD(, ktrace_entry) ktd_queue;
    102 	struct callout ktd_wakch;	/* delayed wakeup */
    103 	struct simplelock ktd_slock;
    104 };
    105 
    106 static void	ktrinitheader(struct ktr_header *, struct lwp *, int);
    107 static void	ktrwrite(struct ktr_desc *, struct ktrace_entry *);
    108 static int	ktrace_common(struct proc *, int, int, int, struct file *);
    109 static int	ktrops(struct proc *, struct proc *, int, int,
    110 		    struct ktr_desc *);
    111 static int	ktrsetchildren(struct proc *, struct proc *, int, int,
    112 		    struct ktr_desc *);
    113 static int	ktrcanset(struct proc *, struct proc *);
    114 static int	ktrsamefile(struct file *, struct file *);
    115 
    116 static struct ktr_desc *
    117 		ktd_lookup(struct file *);
    118 static void	ktdrel(struct ktr_desc *);
    119 static void	ktdref(struct ktr_desc *);
    120 static void	ktraddentry(struct lwp *, struct ktrace_entry *, int);
    121 /* Flags for ktraddentry (3rd arg) */
    122 #define	KTA_NOWAIT		0x0000
    123 #define	KTA_WAITOK		0x0001
    124 #define	KTA_LARGE		0x0002
    125 static void	ktefree(struct ktrace_entry *);
    126 static void	ktd_logerrl(struct ktr_desc *, int);
    127 static void	ktd_logerr(struct proc *, int);
    128 static void	ktrace_thread(void *);
    129 
    130 /*
    131  * Default vaules.
    132  */
    133 #define	KTD_MAXENTRY		1000	/* XXX: tune */
    134 #define	KTD_TIMEOUT		5	/* XXX: tune */
    135 #define	KTD_DELAYQCNT		100	/* XXX: tune */
    136 #define	KTD_WAKEDELAY		5000	/* XXX: tune */
    137 #define	KTD_INTRWAKDL		100	/* XXX: tune */
    138 
    139 /*
    140  * Patchable variables.
    141  */
    142 int ktd_maxentry = KTD_MAXENTRY;	/* max # of entry in the queue */
    143 int ktd_timeout = KTD_TIMEOUT;		/* timeout in seconds */
    144 int ktd_delayqcnt = KTD_DELAYQCNT;	/* # of entry allowed to delay */
    145 int ktd_wakedelay = KTD_WAKEDELAY;	/* delay of wakeup in *ms* */
    146 int ktd_intrwakdl = KTD_INTRWAKDL;	/* ditto, but when interactive */
    147 
    148 static struct simplelock ktdq_slock = SIMPLELOCK_INITIALIZER;
    149 static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq);
    150 
    151 MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer");
    152 POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0,
    153     "ktepl", &pool_allocator_nointr);
    154 
    155 static __inline void
    156 ktd_wakeup(struct ktr_desc *ktd)
    157 {
    158 
    159 	callout_stop(&ktd->ktd_wakch);
    160 	wakeup(ktd);
    161 }
    162 
    163 static void
    164 ktd_logerrl(struct ktr_desc *ktd, int error)
    165 {
    166 
    167 	ktd->ktd_error |= error;
    168 	ktd->ktd_errcnt++;
    169 }
    170 
    171 static void
    172 ktd_logerr(struct proc *p, int error)
    173 {
    174 	struct ktr_desc *ktd = p->p_tracep;
    175 
    176 	if (ktd == NULL)
    177 		return;
    178 
    179 	simple_lock(&ktd->ktd_slock);
    180 	ktd_logerrl(ktd, error);
    181 	simple_unlock(&ktd->ktd_slock);
    182 }
    183 
    184 /*
    185  * Release a reference.  Called with ktd_slock held.
    186  */
    187 void
    188 ktdrel(struct ktr_desc *ktd)
    189 {
    190 
    191 	KDASSERT(ktd->ktd_ref != 0);
    192 	KASSERT(ktd->ktd_ref > 0);
    193 	if (--ktd->ktd_ref <= 0) {
    194 		ktd->ktd_flags |= KTDF_DONE;
    195 		wakeup(ktd);
    196 	}
    197 	simple_unlock(&ktd->ktd_slock);
    198 }
    199 
    200 void
    201 ktdref(struct ktr_desc *ktd)
    202 {
    203 
    204 	simple_lock(&ktd->ktd_slock);
    205 	ktd->ktd_ref++;
    206 	simple_unlock(&ktd->ktd_slock);
    207 }
    208 
    209 struct ktr_desc *
    210 ktd_lookup(struct file *fp)
    211 {
    212 	struct ktr_desc *ktd;
    213 
    214 	simple_lock(&ktdq_slock);
    215 	for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL;
    216 	    ktd = TAILQ_NEXT(ktd, ktd_list)) {
    217 		simple_lock(&ktd->ktd_slock);
    218 		if (ktrsamefile(ktd->ktd_fp, fp)) {
    219 			ktd->ktd_ref++;
    220 			simple_unlock(&ktd->ktd_slock);
    221 			break;
    222 		}
    223 		simple_unlock(&ktd->ktd_slock);
    224 	}
    225 	simple_unlock(&ktdq_slock);
    226 	return (ktd);
    227 }
    228 
    229 void
    230 ktraddentry(struct lwp *l, struct ktrace_entry *kte, int flags)
    231 {
    232 	struct proc *p = l->l_proc;
    233 	struct ktr_desc *ktd;
    234 #ifdef DEBUG
    235 	struct timeval t;
    236 	int s;
    237 #endif
    238 
    239 	if (p->p_traceflag & KTRFAC_TRC_EMUL) {
    240 		/* Add emulation trace before first entry for this process */
    241 		p->p_traceflag &= ~KTRFAC_TRC_EMUL;
    242 		ktremul(l);
    243 	}
    244 
    245 	/*
    246 	 * Tracing may be canceled while we were sleeping waiting for
    247 	 * memory.
    248 	 */
    249 	ktd = p->p_tracep;
    250 	if (ktd == NULL)
    251 		goto freekte;
    252 
    253 	/*
    254 	 * Bump reference count so that the object will remain while
    255 	 * we are here.  Note that the trace is controlled by other
    256 	 * process.
    257 	 */
    258 	ktdref(ktd);
    259 
    260 	simple_lock(&ktd->ktd_slock);
    261 	if (ktd->ktd_flags & KTDF_DONE)
    262 		goto relktd;
    263 
    264 	if (ktd->ktd_qcount > ktd_maxentry) {
    265 		ktd_logerrl(ktd, KTDE_ENOSPC);
    266 		goto relktd;
    267 	}
    268 	TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
    269 	ktd->ktd_qcount++;
    270 	if (ktd->ktd_flags & KTDF_BLOCKING)
    271 		goto skip_sync;
    272 
    273 	if (flags & KTA_WAITOK &&
    274 	    (/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT ||
    275 	    ktd->ktd_qcount > ktd_maxentry >> 1))
    276 		/*
    277 		 * Sync with writer thread since we're requesting rather
    278 		 * big one or many requests are pending.
    279 		 */
    280 		do {
    281 			ktd->ktd_flags |= KTDF_WAIT;
    282 			ktd_wakeup(ktd);
    283 #ifdef DEBUG
    284 			s = splclock();
    285 			t = mono_time;
    286 			splx(s);
    287 #endif
    288 			if (ltsleep(&ktd->ktd_flags, PWAIT, "ktrsync",
    289 			    ktd_timeout * hz, &ktd->ktd_slock) != 0) {
    290 				ktd->ktd_flags |= KTDF_BLOCKING;
    291 				/*
    292 				 * Maybe the writer thread is blocking
    293 				 * completely for some reason, but
    294 				 * don't stop target process forever.
    295 				 */
    296 				log(LOG_NOTICE, "ktrace timeout\n");
    297 				break;
    298 			}
    299 #ifdef DEBUG
    300 			s = splclock();
    301 			timersub(&mono_time, &t, &t);
    302 			splx(s);
    303 			if (t.tv_sec > 0)
    304 				log(LOG_NOTICE,
    305 				    "ktrace long wait: %ld.%06ld\n",
    306 				    t.tv_sec, t.tv_usec);
    307 #endif
    308 		} while (p->p_tracep == ktd &&
    309 		    (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
    310 	else {
    311 		/* Schedule delayed wakeup */
    312 		if (ktd->ktd_qcount > ktd->ktd_delayqcnt)
    313 			ktd_wakeup(ktd);	/* Wakeup now */
    314 		else if (!callout_pending(&ktd->ktd_wakch))
    315 			callout_reset(&ktd->ktd_wakch,
    316 			    ktd->ktd_flags & KTDF_INTERACTIVE ?
    317 			    ktd->ktd_intrwakdl : ktd->ktd_wakedelay,
    318 			    (void (*)(void *))wakeup, ktd);
    319 	}
    320 
    321 skip_sync:
    322 	ktdrel(ktd);
    323 	return;
    324 
    325 relktd:
    326 	ktdrel(ktd);
    327 
    328 freekte:
    329 	ktefree(kte);
    330 }
    331 
    332 void
    333 ktefree(struct ktrace_entry *kte)
    334 {
    335 	struct ktr_header *kth = &kte->kte_kth;
    336 
    337 	if (kth->ktr_len > 0)
    338 		free(kte->kte_buf, M_KTRACE);
    339 	pool_put(&kte_pool, kte);
    340 }
    341 
    342 /*
    343  * "deep" compare of two files for the purposes of clearing a trace.
    344  * Returns true if they're the same open file, or if they point at the
    345  * same underlying vnode/socket.
    346  */
    347 
    348 int
    349 ktrsamefile(struct file *f1, struct file *f2)
    350 {
    351 
    352 	return ((f1 == f2) ||
    353 	    ((f1 != NULL) && (f2 != NULL) &&
    354 		(f1->f_type == f2->f_type) &&
    355 		(f1->f_data == f2->f_data)));
    356 }
    357 
    358 void
    359 ktrderef(struct proc *p)
    360 {
    361 	struct ktr_desc *ktd = p->p_tracep;
    362 
    363 	p->p_traceflag = 0;
    364 	if (ktd == NULL)
    365 		return;
    366 	p->p_tracep = NULL;
    367 
    368 	simple_lock(&ktd->ktd_slock);
    369 	wakeup(&ktd->ktd_flags);
    370 	ktdrel(ktd);
    371 }
    372 
    373 void
    374 ktradref(struct proc *p)
    375 {
    376 	struct ktr_desc *ktd = p->p_tracep;
    377 
    378 	ktdref(ktd);
    379 }
    380 
    381 void
    382 ktrinitheader(struct ktr_header *kth, struct lwp *l, int type)
    383 {
    384 	struct proc *p = l->l_proc;
    385 
    386 	(void)memset(kth, 0, sizeof(*kth));
    387 	kth->ktr_type = type;
    388 	microtime(&kth->ktr_time);
    389 	kth->ktr_pid = p->p_pid;
    390 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    391 }
    392 
    393 void
    394 ktrsyscall(struct lwp *l, register_t code, register_t realcode,
    395     const struct sysent *callp, register_t args[])
    396 {
    397 	struct proc *p = l->l_proc;
    398 	struct ktrace_entry *kte;
    399 	struct ktr_header *kth;
    400 	struct ktr_syscall *ktp;
    401 	register_t *argp;
    402 	int argsize;
    403 	size_t len;
    404 	u_int i;
    405 
    406 	if (callp == NULL)
    407 		callp = p->p_emul->e_sysent;
    408 
    409 	argsize = callp[code].sy_argsize;
    410 #ifdef _LP64
    411 	if (p->p_flag & P_32)
    412 		argsize = argsize << 1;
    413 #endif
    414 	len = sizeof(struct ktr_syscall) + argsize;
    415 
    416 	p->p_traceflag |= KTRFAC_ACTIVE;
    417 	kte = pool_get(&kte_pool, PR_WAITOK);
    418 	kth = &kte->kte_kth;
    419 	ktrinitheader(kth, l, KTR_SYSCALL);
    420 
    421 	ktp = malloc(len, M_KTRACE, M_WAITOK);
    422 	ktp->ktr_code = realcode;
    423 	ktp->ktr_argsize = argsize;
    424 	argp = (register_t *)(ktp + 1);
    425 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    426 		*argp++ = args[i];
    427 	kth->ktr_len = len;
    428 	kte->kte_buf = ktp;
    429 
    430 	ktraddentry(l, kte, KTA_WAITOK);
    431 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    432 }
    433 
    434 void
    435 ktrsysret(struct lwp *l, register_t code, int error, register_t *retval)
    436 {
    437 	struct proc *p = l->l_proc;
    438 	struct ktrace_entry *kte;
    439 	struct ktr_header *kth;
    440 	struct ktr_sysret *ktp;
    441 
    442 	p->p_traceflag |= KTRFAC_ACTIVE;
    443 	kte = pool_get(&kte_pool, PR_WAITOK);
    444 	kth = &kte->kte_kth;
    445 	ktrinitheader(kth, l, KTR_SYSRET);
    446 
    447 	ktp = malloc(sizeof(struct ktr_sysret), M_KTRACE, M_WAITOK);
    448 	ktp->ktr_code = code;
    449 	ktp->ktr_eosys = 0;			/* XXX unused */
    450 	ktp->ktr_error = error;
    451 	ktp->ktr_retval = retval ? retval[0] : 0;
    452 	ktp->ktr_retval_1 = retval ? retval[1] : 0;
    453 
    454 	kth->ktr_len = sizeof(struct ktr_sysret);
    455 	kte->kte_buf = ktp;
    456 
    457 	ktraddentry(l, kte, KTA_WAITOK);
    458 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    459 }
    460 
    461 /*
    462  * XXX: ndp->ni_pathlen should be passed.
    463  */
    464 void
    465 ktrnamei(struct lwp *l, char *path)
    466 {
    467 
    468 	ktrkmem(l, KTR_NAMEI, path, strlen(path));
    469 }
    470 
    471 void
    472 ktremul(struct lwp *l)
    473 {
    474 	const char *emul = l->l_proc->p_emul->e_name;
    475 
    476 	ktrkmem(l, KTR_EMUL, emul, strlen(emul));
    477 }
    478 
    479 void
    480 ktrkmem(struct lwp *l, int type, const void *buf, size_t len)
    481 {
    482 	struct proc *p = l->l_proc;
    483 	struct ktrace_entry *kte;
    484 	struct ktr_header *kth;
    485 
    486 	p->p_traceflag |= KTRFAC_ACTIVE;
    487 	kte = pool_get(&kte_pool, PR_WAITOK);
    488 	kth = &kte->kte_kth;
    489 	ktrinitheader(kth, l, type);
    490 
    491 	kth->ktr_len = len;
    492 	kte->kte_buf = malloc(len, M_KTRACE, M_WAITOK);
    493 	memcpy(kte->kte_buf, buf, len);
    494 
    495 	ktraddentry(l, kte, KTA_WAITOK);
    496 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    497 }
    498 
    499 void
    500 ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov,
    501     int len, int error)
    502 {
    503 	struct proc *p = l->l_proc;
    504 	struct ktrace_entry *kte;
    505 	struct ktr_header *kth;
    506 	struct ktr_genio *ktp;
    507 	int resid = len, cnt;
    508 	caddr_t cp;
    509 	int buflen;
    510 
    511 	if (error)
    512 		return;
    513 
    514 	p->p_traceflag |= KTRFAC_ACTIVE;
    515 
    516 next:
    517 	buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
    518 
    519 	kte = pool_get(&kte_pool, PR_WAITOK);
    520 	kth = &kte->kte_kth;
    521 	ktrinitheader(kth, l, KTR_GENIO);
    522 
    523 	ktp = malloc(buflen, M_KTRACE, M_WAITOK);
    524 	ktp->ktr_fd = fd;
    525 	ktp->ktr_rw = rw;
    526 
    527 	kte->kte_buf = ktp;
    528 
    529 	cp = (caddr_t)(ktp + 1);
    530 	buflen -= sizeof(struct ktr_genio);
    531 	kth->ktr_len = sizeof(struct ktr_genio);
    532 
    533 	while (buflen > 0) {
    534 		cnt = min(iov->iov_len, buflen);
    535 		if (copyin(iov->iov_base, cp, cnt) != 0)
    536 			goto out;
    537 		kth->ktr_len += cnt;
    538 		buflen -= cnt;
    539 		resid -= cnt;
    540 		iov->iov_len -= cnt;
    541 		if (iov->iov_len == 0)
    542 			iov++;
    543 		else
    544 			iov->iov_base = (caddr_t)iov->iov_base + cnt;
    545 	}
    546 
    547 	/*
    548 	 * Don't push so many entry at once.  It will cause kmem map
    549 	 * shortage.
    550 	 */
    551 	ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
    552 	if (resid > 0) {
    553 #if 0 /* XXX NJWLWP */
    554 		KDASSERT(p->p_cpu != NULL);
    555 		KDASSERT(p->p_cpu == curcpu());
    556 #endif
    557 		/* XXX NJWLWP */
    558 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    559 			preempt(1);
    560 
    561 		goto next;
    562 	}
    563 
    564 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    565 	return;
    566 
    567 out:
    568 	ktefree(kte);
    569 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    570 }
    571 
    572 void
    573 ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask,
    574     const ksiginfo_t *ksi)
    575 {
    576 	struct proc *p = l->l_proc;
    577 	struct ktrace_entry *kte;
    578 	struct ktr_header *kth;
    579 	struct {
    580 		struct ktr_psig	kp;
    581 		siginfo_t	si;
    582 	} *kbuf;
    583 
    584 	p->p_traceflag |= KTRFAC_ACTIVE;
    585 	kte = pool_get(&kte_pool, PR_WAITOK);
    586 	kth = &kte->kte_kth;
    587 	ktrinitheader(kth, l, KTR_PSIG);
    588 
    589 	kbuf = malloc(sizeof(*kbuf), M_KTRACE, M_WAITOK);
    590 	kbuf->kp.signo = (char)sig;
    591 	kbuf->kp.action = action;
    592 	kbuf->kp.mask = *mask;
    593 	kte->kte_buf = kbuf;
    594 	if (ksi) {
    595 		kbuf->kp.code = KSI_TRAPCODE(ksi);
    596 		(void)memset(&kbuf->si, 0, sizeof(kbuf->si));
    597 		kbuf->si._info = ksi->ksi_info;
    598 		kth->ktr_len = sizeof(kbuf);
    599 	} else {
    600 		kbuf->kp.code = 0;
    601 		kth->ktr_len = sizeof(struct ktr_psig);
    602 	}
    603 
    604 	ktraddentry(l, kte, KTA_WAITOK);
    605 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    606 }
    607 
    608 void
    609 ktrcsw(struct lwp *l, int out, int user)
    610 {
    611 	struct proc *p = l->l_proc;
    612 	struct ktrace_entry *kte;
    613 	struct ktr_header *kth;
    614 	struct ktr_csw *kc;
    615 
    616 	p->p_traceflag |= KTRFAC_ACTIVE;
    617 
    618 	/*
    619 	 * We can't sleep if we're already going to sleep (if original
    620 	 * condition is met during sleep, we hang up).
    621 	 */
    622 	kte = pool_get(&kte_pool, out ? PR_NOWAIT : PR_WAITOK);
    623 	if (kte == NULL) {
    624 		ktd_logerr(p, KTDE_ENOMEM);
    625 		goto out;
    626 	}
    627 	kth = &kte->kte_kth;
    628 	ktrinitheader(kth, l, KTR_CSW);
    629 
    630 	kc = malloc(sizeof(struct ktr_csw), M_KTRACE,
    631 	    out ? M_NOWAIT : M_WAITOK);
    632 	if (kc == NULL) {
    633 		ktd_logerr(p, KTDE_ENOMEM);
    634 		goto free_kte;
    635 	}
    636 	kc->out = out;
    637 	kc->user = user;
    638 	kth->ktr_len = sizeof(struct ktr_csw);
    639 	kte->kte_buf = kc;
    640 
    641 	ktraddentry(l, kte, out ? KTA_NOWAIT : KTA_WAITOK);
    642 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    643 	return;
    644 
    645 free_kte:
    646 	pool_put(&kte_pool, kte);
    647 out:
    648 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    649 }
    650 
    651 void
    652 ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr)
    653 {
    654 	struct proc *p = l->l_proc;
    655 	struct ktrace_entry *kte;
    656 	struct ktr_header *kth;
    657 	struct ktr_user *ktp;
    658 	caddr_t user_dta;
    659 
    660 	p->p_traceflag |= KTRFAC_ACTIVE;
    661 	kte = pool_get(&kte_pool, PR_WAITOK);
    662 	kth = &kte->kte_kth;
    663 	ktrinitheader(kth, l, KTR_USER);
    664 
    665 	ktp = malloc(sizeof(struct ktr_user) + len, M_KTRACE, M_WAITOK);
    666 	if (ustr) {
    667 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    668 			ktp->ktr_id[0] = '\0';
    669 	} else
    670 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    671 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    672 
    673 	user_dta = (caddr_t)(ktp + 1);
    674 	if (copyin(addr, (void *)user_dta, len) != 0)
    675 		len = 0;
    676 
    677 	kth->ktr_len = sizeof(struct ktr_user) + len;
    678 	kte->kte_buf = ktp;
    679 
    680 	ktraddentry(l, kte, KTA_WAITOK);
    681 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    682 }
    683 
    684 void
    685 ktrmmsg(struct lwp *l, const void *msgh, size_t size)
    686 {
    687 	ktrkmem(l, KTR_MMSG, msgh, size);
    688 }
    689 
    690 void
    691 ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr)
    692 {
    693 	struct proc *p = l->l_proc;
    694 	struct ktrace_entry *kte;
    695 	struct ktr_header *kth;
    696 	struct ktr_mool *kp;
    697 	struct ktr_mool *buf;
    698 
    699 	p->p_traceflag |= KTRFAC_ACTIVE;
    700 	kte = pool_get(&kte_pool, PR_WAITOK);
    701 	kth = &kte->kte_kth;
    702 	ktrinitheader(kth, l, KTR_MOOL);
    703 
    704 	kp = malloc(size + sizeof(*kp), M_KTRACE, M_WAITOK);
    705 	kp->uaddr = uaddr;
    706 	kp->size = size;
    707 	buf = kp + 1; /* Skip uaddr and size */
    708 	(void)memcpy(buf, kaddr, size);
    709 
    710 	kth->ktr_len = size + sizeof(*kp);
    711 	kte->kte_buf = kp;
    712 
    713 	ktraddentry(l, kte, KTA_WAITOK);
    714 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    715 }
    716 
    717 void
    718 ktrsaupcall(struct lwp *l, int type, int nevent, int nint, void *sas,
    719     void *ap)
    720 {
    721 	struct proc *p = l->l_proc;
    722 	struct ktrace_entry *kte;
    723 	struct ktr_header *kth;
    724 	struct ktr_saupcall *ktp;
    725 	size_t len;
    726 	struct sa_t **sapp;
    727 	int i;
    728 
    729 	p->p_traceflag |= KTRFAC_ACTIVE;
    730 	kte = pool_get(&kte_pool, PR_WAITOK);
    731 	kth = &kte->kte_kth;
    732 	ktrinitheader(kth, l, KTR_SAUPCALL);
    733 
    734 	len = sizeof(struct ktr_saupcall);
    735 	ktp = malloc(len + sizeof(struct sa_t) * (nevent + nint + 1), M_KTRACE,
    736 	    M_WAITOK);
    737 
    738 	ktp->ktr_type = type;
    739 	ktp->ktr_nevent = nevent;
    740 	ktp->ktr_nint = nint;
    741 	ktp->ktr_sas = sas;
    742 	ktp->ktr_ap = ap;
    743 	/*
    744 	 *  Copy the sa_t's
    745 	 */
    746 	sapp = (struct sa_t **) sas;
    747 
    748 	for (i = nevent + nint; i >= 0; i--) {
    749 		if (copyin(*sapp, (char *)ktp + len, sizeof(struct sa_t)) == 0)
    750 			len += sizeof(struct sa_t);
    751 		sapp++;
    752 	}
    753 
    754 	kth->ktr_len = len;
    755 	kte->kte_buf = ktp;
    756 
    757 	ktraddentry(l, kte, KTA_WAITOK);
    758 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    759 }
    760 
    761 /* Interface and common routines */
    762 
    763 int
    764 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
    765 {
    766 	struct proc *p;
    767 	struct pgrp *pg;
    768 	struct ktr_desc *ktd = NULL;
    769 	int ret = 0;
    770 	int error = 0;
    771 	int descend;
    772 
    773 	curp->p_traceflag |= KTRFAC_ACTIVE;
    774 	descend = ops & KTRFLAG_DESCEND;
    775 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    776 
    777 	switch (KTROP(ops)) {
    778 
    779 	case KTROP_CLEARFILE:
    780 		/*
    781 		 * Clear all uses of the tracefile
    782 		 */
    783 
    784 		ktd = ktd_lookup(fp);
    785 		if (ktd == NULL)
    786 			goto done;
    787 
    788 		proclist_lock_read();
    789 		LIST_FOREACH(p, &allproc, p_list) {
    790 			if (p->p_tracep == ktd) {
    791 				if (ktrcanset(curp, p))
    792 					ktrderef(p);
    793 				else
    794 					error = EPERM;
    795 			}
    796 		}
    797 		proclist_unlock_read();
    798 		goto done;
    799 
    800 	case KTROP_SET:
    801 		ktd = ktd_lookup(fp);
    802 		if (ktd == NULL) {
    803 			ktd = malloc(sizeof(struct ktr_desc),
    804 			    M_KTRACE, M_WAITOK);
    805 			TAILQ_INIT(&ktd->ktd_queue);
    806 			simple_lock_init(&ktd->ktd_slock);
    807 			callout_init(&ktd->ktd_wakch);
    808 			ktd->ktd_flags = ktd->ktd_qcount =
    809 			    ktd->ktd_error = ktd->ktd_errcnt = 0;
    810 			ktd->ktd_ref = 1;
    811 			ktd->ktd_delayqcnt = ktd_delayqcnt;
    812 			ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
    813 			ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
    814 			/*
    815 			 * XXX: not correct.  needs an way to detect
    816 			 * whether ktruss or ktrace.
    817 			 */
    818 			if (fp->f_type == DTYPE_PIPE)
    819 				ktd->ktd_flags |= KTDF_INTERACTIVE;
    820 
    821 			error = kthread_create1(ktrace_thread, ktd,
    822 			    &ktd->ktd_proc, "ktr %p", ktd);
    823 			if (error != 0) {
    824 				free(ktd, M_KTRACE);
    825 				goto done;
    826 			}
    827 
    828 			simple_lock(&fp->f_slock);
    829 			fp->f_count++;
    830 			simple_unlock(&fp->f_slock);
    831 			ktd->ktd_fp = fp;
    832 
    833 			simple_lock(&ktdq_slock);
    834 			TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
    835 			simple_unlock(&ktdq_slock);
    836 		}
    837 		break;
    838 
    839 	case KTROP_CLEAR:
    840 		break;
    841 	}
    842 
    843 	/*
    844 	 * need something to (un)trace (XXX - why is this here?)
    845 	 */
    846 	if (!facs) {
    847 		error = EINVAL;
    848 		goto done;
    849 	}
    850 
    851 	/*
    852 	 * do it
    853 	 */
    854 	if (pid < 0) {
    855 		/*
    856 		 * by process group
    857 		 */
    858 		pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
    859 		if (pg == NULL) {
    860 			error = ESRCH;
    861 			goto done;
    862 		}
    863 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    864 			if (descend)
    865 				ret |= ktrsetchildren(curp, p, ops, facs, ktd);
    866 			else
    867 				ret |= ktrops(curp, p, ops, facs, ktd);
    868 		}
    869 
    870 	} else {
    871 		/*
    872 		 * by pid
    873 		 */
    874 		p = p_find(pid, PFIND_UNLOCK_FAIL);
    875 		if (p == NULL) {
    876 			error = ESRCH;
    877 			goto done;
    878 		}
    879 		if (descend)
    880 			ret |= ktrsetchildren(curp, p, ops, facs, ktd);
    881 		else
    882 			ret |= ktrops(curp, p, ops, facs, ktd);
    883 	}
    884 	proclist_unlock_read();	/* taken by p{g}_find */
    885 	if (!ret)
    886 		error = EPERM;
    887 done:
    888 	if (error != 0 && ktd != NULL)
    889 		/*
    890 		 * Wakup the thread so that it can be die if we
    891 		 * can't trace any process.
    892 		 */
    893 		ktd_wakeup(ktd);
    894 	if (KTROP(ops) == KTROP_SET ||
    895 	    KTROP(ops) == KTROP_CLEARFILE) {
    896 		simple_lock(&ktd->ktd_slock);
    897 		ktdrel(ktd);
    898 	}
    899 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    900 	return (error);
    901 }
    902 
    903 /*
    904  * fktrace system call
    905  */
    906 /* ARGSUSED */
    907 int
    908 sys_fktrace(struct lwp *l, void *v, register_t *retval)
    909 {
    910 	struct sys_fktrace_args /* {
    911 		syscallarg(int) fd;
    912 		syscallarg(int) ops;
    913 		syscallarg(int) facs;
    914 		syscallarg(int) pid;
    915 	} */ *uap = v;
    916 	struct proc *curp;
    917 	struct file *fp = NULL;
    918 	struct filedesc *fdp = l->l_proc->p_fd;
    919 	int error;
    920 
    921 	curp = l->l_proc;
    922 	fdp = curp->p_fd;
    923 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    924 		return (EBADF);
    925 
    926 	FILE_USE(fp);
    927 
    928 	if ((fp->f_flag & FWRITE) == 0)
    929 		error = EBADF;
    930 	else
    931 		error = ktrace_common(curp, SCARG(uap, ops),
    932 		    SCARG(uap, facs), SCARG(uap, pid), fp);
    933 
    934 	FILE_UNUSE(fp, l);
    935 
    936 	return error;
    937 }
    938 
    939 /*
    940  * ktrace system call
    941  */
    942 /* ARGSUSED */
    943 int
    944 sys_ktrace(struct lwp *l, void *v, register_t *retval)
    945 {
    946 	struct sys_ktrace_args /* {
    947 		syscallarg(const char *) fname;
    948 		syscallarg(int) ops;
    949 		syscallarg(int) facs;
    950 		syscallarg(int) pid;
    951 	} */ *uap = v;
    952 	struct proc *curp = l->l_proc;
    953 	struct vnode *vp = NULL;
    954 	struct file *fp = NULL;
    955 	int ops = SCARG(uap, ops);
    956 	struct nameidata nd;
    957 	int error = 0;
    958 	int fd;
    959 
    960 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    961 
    962 	curp->p_traceflag |= KTRFAC_ACTIVE;
    963 	if ((ops & KTROP_CLEAR) == 0) {
    964 		/*
    965 		 * an operation which requires a file argument.
    966 		 */
    967 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    968 		    l);
    969 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    970 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    971 			return (error);
    972 		}
    973 		vp = nd.ni_vp;
    974 		VOP_UNLOCK(vp, 0);
    975 		if (vp->v_type != VREG) {
    976 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, l);
    977 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    978 			return (EACCES);
    979 		}
    980 		/*
    981 		 * XXX This uses up a file descriptor slot in the
    982 		 * tracing process for the duration of this syscall.
    983 		 * This is not expected to be a problem.  If
    984 		 * falloc(NULL, ...) DTRT we could skip that part, but
    985 		 * that would require changing its interface to allow
    986 		 * the caller to pass in a ucred..
    987 		 *
    988 		 * This will FILE_USE the fp it returns, if any.
    989 		 * Keep it in use until we return.
    990 		 */
    991 		if ((error = falloc(curp, &fp, &fd)) != 0)
    992 			goto done;
    993 
    994 		fp->f_flag = FWRITE;
    995 		fp->f_type = DTYPE_VNODE;
    996 		fp->f_ops = &vnops;
    997 		fp->f_data = (caddr_t)vp;
    998 		FILE_SET_MATURE(fp);
    999 		vp = NULL;
   1000 	}
   1001 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
   1002 	    SCARG(uap, pid), fp);
   1003 done:
   1004 	if (vp != NULL)
   1005 		(void) vn_close(vp, FWRITE, curp->p_ucred, l);
   1006 	if (fp != NULL) {
   1007 		FILE_UNUSE(fp, l);	/* release file */
   1008 		fdrelease(l, fd); 	/* release fd table slot */
   1009 	}
   1010 	return (error);
   1011 }
   1012 
   1013 int
   1014 ktrops(struct proc *curp, struct proc *p, int ops, int facs,
   1015     struct ktr_desc *ktd)
   1016 {
   1017 
   1018 	if (!ktrcanset(curp, p))
   1019 		return (0);
   1020 	if (KTROP(ops) == KTROP_SET) {
   1021 		if (p->p_tracep != ktd) {
   1022 			/*
   1023 			 * if trace file already in use, relinquish
   1024 			 */
   1025 			ktrderef(p);
   1026 			p->p_tracep = ktd;
   1027 			ktradref(p);
   1028 		}
   1029 		p->p_traceflag |= facs;
   1030 		if (curp->p_ucred->cr_uid == 0)
   1031 			p->p_traceflag |= KTRFAC_ROOT;
   1032 	} else {
   1033 		/* KTROP_CLEAR */
   1034 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
   1035 			/* no more tracing */
   1036 			ktrderef(p);
   1037 		}
   1038 	}
   1039 
   1040 	/*
   1041 	 * Emit an emulation record, every time there is a ktrace
   1042 	 * change/attach request.
   1043 	 */
   1044 	if (KTRPOINT(p, KTR_EMUL))
   1045 		p->p_traceflag |= KTRFAC_TRC_EMUL;
   1046 #ifdef __HAVE_SYSCALL_INTERN
   1047 	(*p->p_emul->e_syscall_intern)(p);
   1048 #endif
   1049 
   1050 	return (1);
   1051 }
   1052 
   1053 int
   1054 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
   1055     struct ktr_desc *ktd)
   1056 {
   1057 	struct proc *p;
   1058 	int ret = 0;
   1059 
   1060 	p = top;
   1061 	for (;;) {
   1062 		ret |= ktrops(curp, p, ops, facs, ktd);
   1063 		/*
   1064 		 * If this process has children, descend to them next,
   1065 		 * otherwise do any siblings, and if done with this level,
   1066 		 * follow back up the tree (but not past top).
   1067 		 */
   1068 		if (LIST_FIRST(&p->p_children) != NULL) {
   1069 			p = LIST_FIRST(&p->p_children);
   1070 			continue;
   1071 		}
   1072 		for (;;) {
   1073 			if (p == top)
   1074 				return (ret);
   1075 			if (LIST_NEXT(p, p_sibling) != NULL) {
   1076 				p = LIST_NEXT(p, p_sibling);
   1077 				break;
   1078 			}
   1079 			p = p->p_pptr;
   1080 		}
   1081 	}
   1082 	/*NOTREACHED*/
   1083 }
   1084 
   1085 void
   1086 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
   1087 {
   1088 	struct uio auio;
   1089 	struct iovec aiov[64], *iov;
   1090 	struct ktrace_entry *top = kte;
   1091 	struct ktr_header *kth;
   1092 	struct file *fp = ktd->ktd_fp;
   1093 	struct proc *p;
   1094 	int error;
   1095 next:
   1096 	auio.uio_iov = iov = &aiov[0];
   1097 	auio.uio_offset = 0;
   1098 	auio.uio_segflg = UIO_SYSSPACE;
   1099 	auio.uio_rw = UIO_WRITE;
   1100 	auio.uio_resid = 0;
   1101 	auio.uio_iovcnt = 0;
   1102 	auio.uio_lwp = NULL;
   1103 	do {
   1104 		kth = &kte->kte_kth;
   1105 		iov->iov_base = (caddr_t)kth;
   1106 		iov++->iov_len = sizeof(struct ktr_header);
   1107 		auio.uio_resid += sizeof(struct ktr_header);
   1108 		auio.uio_iovcnt++;
   1109 		if (kth->ktr_len > 0) {
   1110 			iov->iov_base = kte->kte_buf;
   1111 			iov++->iov_len = kth->ktr_len;
   1112 			auio.uio_resid += kth->ktr_len;
   1113 			auio.uio_iovcnt++;
   1114 		}
   1115 	} while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
   1116 	    auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
   1117 
   1118 again:
   1119 	simple_lock(&fp->f_slock);
   1120 	FILE_USE(fp);
   1121 	error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
   1122 	    fp->f_cred, FOF_UPDATE_OFFSET);
   1123 	FILE_UNUSE(fp, NULL);
   1124 	switch (error) {
   1125 
   1126 	case 0:
   1127 		if (auio.uio_resid > 0)
   1128 			goto again;
   1129 		if (kte != NULL)
   1130 			goto next;
   1131 		break;
   1132 
   1133 	case EWOULDBLOCK:
   1134 		preempt(1);
   1135 		goto again;
   1136 
   1137 	default:
   1138 		/*
   1139 		 * If error encountered, give up tracing on this
   1140 		 * vnode.  Don't report EPIPE as this can easily
   1141 		 * happen with fktrace()/ktruss.
   1142 		 */
   1143 #ifndef DEBUG
   1144 		if (error != EPIPE)
   1145 #endif
   1146 			log(LOG_NOTICE,
   1147 			    "ktrace write failed, errno %d, tracing stopped\n",
   1148 			    error);
   1149 		proclist_lock_read();
   1150 		LIST_FOREACH(p, &allproc, p_list) {
   1151 			if (p->p_tracep == ktd)
   1152 				ktrderef(p);
   1153 		}
   1154 		proclist_unlock_read();
   1155 	}
   1156 
   1157 	while ((kte = top) != NULL) {
   1158 		top = TAILQ_NEXT(top, kte_list);
   1159 		ktefree(kte);
   1160 	}
   1161 }
   1162 
   1163 void
   1164 ktrace_thread(void *arg)
   1165 {
   1166 	struct ktr_desc *ktd = arg;
   1167 	struct file *fp = ktd->ktd_fp;
   1168 	struct ktrace_entry *kte;
   1169 	int ktrerr, errcnt;
   1170 
   1171 	for (;;) {
   1172 		simple_lock(&ktd->ktd_slock);
   1173 		kte = TAILQ_FIRST(&ktd->ktd_queue);
   1174 		if (kte == NULL) {
   1175 			if (ktd->ktd_flags & KTDF_WAIT) {
   1176 				ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING);
   1177 				wakeup(&ktd->ktd_flags);
   1178 			}
   1179 			if (ktd->ktd_ref == 0)
   1180 				break;
   1181 			ltsleep(ktd, PWAIT | PNORELOCK, "ktrwait", 0,
   1182 			    &ktd->ktd_slock);
   1183 			continue;
   1184 		}
   1185 		TAILQ_INIT(&ktd->ktd_queue);
   1186 		ktd->ktd_qcount = 0;
   1187 		ktrerr = ktd->ktd_error;
   1188 		errcnt = ktd->ktd_errcnt;
   1189 		ktd->ktd_error = ktd->ktd_errcnt = 0;
   1190 		simple_unlock(&ktd->ktd_slock);
   1191 
   1192 		if (ktrerr) {
   1193 			log(LOG_NOTICE,
   1194 			    "ktrace failed, fp %p, error 0x%x, total %d\n",
   1195 			    fp, ktrerr, errcnt);
   1196 		}
   1197 		ktrwrite(ktd, kte);
   1198 	}
   1199 	simple_unlock(&ktd->ktd_slock);
   1200 
   1201 	simple_lock(&ktdq_slock);
   1202 	TAILQ_REMOVE(&ktdq, ktd, ktd_list);
   1203 	simple_unlock(&ktdq_slock);
   1204 
   1205 	simple_lock(&fp->f_slock);
   1206 	FILE_USE(fp);
   1207 
   1208 	/*
   1209 	 * ktrace file descriptor can't be watched (are not visible to
   1210 	 * userspace), so no kqueue stuff here
   1211 	 * XXX: The above comment is wrong, because the fktrace file
   1212 	 * descriptor is available in userland.
   1213 	 */
   1214 	closef(fp, NULL);
   1215 
   1216 	callout_stop(&ktd->ktd_wakch);
   1217 	free(ktd, M_KTRACE);
   1218 
   1219 	kthread_exit(0);
   1220 }
   1221 
   1222 /*
   1223  * Return true if caller has permission to set the ktracing state
   1224  * of target.  Essentially, the target can't possess any
   1225  * more permissions than the caller.  KTRFAC_ROOT signifies that
   1226  * root previously set the tracing status on the target process, and
   1227  * so, only root may further change it.
   1228  *
   1229  * TODO: check groups.  use caller effective gid.
   1230  */
   1231 int
   1232 ktrcanset(struct proc *callp, struct proc *targetp)
   1233 {
   1234 	struct pcred *caller = callp->p_cred;
   1235 	struct pcred *target = targetp->p_cred;
   1236 
   1237 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
   1238 	    target->p_ruid == target->p_svuid &&
   1239 	    caller->p_rgid == target->p_rgid &&	/* XXX */
   1240 	    target->p_rgid == target->p_svgid &&
   1241 	    (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
   1242 	    (targetp->p_flag & P_SUGID) == 0) ||
   1243 	    caller->pc_ucred->cr_uid == 0)
   1244 		return (1);
   1245 
   1246 	return (0);
   1247 }
   1248 #endif /* KTRACE */
   1249 
   1250 /*
   1251  * Put user defined entry to ktrace records.
   1252  */
   1253 int
   1254 sys_utrace(struct lwp *l, void *v, register_t *retval)
   1255 {
   1256 #ifdef KTRACE
   1257 	struct sys_utrace_args /* {
   1258 		syscallarg(const char *) label;
   1259 		syscallarg(void *) addr;
   1260 		syscallarg(size_t) len;
   1261 	} */ *uap = v;
   1262 	struct proc *p = l->l_proc;
   1263 
   1264 	if (!KTRPOINT(p, KTR_USER))
   1265 		return (0);
   1266 
   1267 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
   1268 		return (EINVAL);
   1269 
   1270 	ktruser(l, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
   1271 
   1272 	return (0);
   1273 #else /* !KTRACE */
   1274 	return ENOSYS;
   1275 #endif /* KTRACE */
   1276 }
   1277