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