xref: /freebsd/sys/kern/kern_ktrace.c (revision cb05b60a8982e5497cd30449710deb2f4be653d4)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
32c255e9dSRobert Watson  *	The Regents of the University of California.
42c255e9dSRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
52c255e9dSRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
37db6a20e2SGarrett Wollman #include "opt_ktrace.h"
38467a273cSRobert Watson #include "opt_mac.h"
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41f23b4c91SGarrett Wollman #include <sys/systm.h>
42ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
43ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
44ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
48033eb86eSJeff Roberson #include <sys/mount.h>
49df8bae1dSRodney W. Grimes #include <sys/namei.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51ea3fc8e4SJohn Baldwin #include <sys/proc.h>
52ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
551005a129SJohn Baldwin #include <sys/sx.h>
56ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
57df8bae1dSRodney W. Grimes #include <sys/syslog.h>
58ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
59df8bae1dSRodney W. Grimes 
60aed55708SRobert Watson #include <security/mac/mac_framework.h>
61aed55708SRobert Watson 
622c255e9dSRobert Watson /*
632c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
642c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
652c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
662c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
672c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
682c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
692c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
702c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
712c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
722c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
732c255e9dSRobert Watson  *
742c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
752c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
762c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
772c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
782c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
792c255e9dSRobert Watson  */
802c255e9dSRobert Watson 
81a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8255166637SPoul-Henning Kamp 
83db6a20e2SGarrett Wollman #ifdef KTRACE
84ea3fc8e4SJohn Baldwin 
85ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
86ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
87ea3fc8e4SJohn Baldwin #endif
88ea3fc8e4SJohn Baldwin 
89ea3fc8e4SJohn Baldwin struct ktr_request {
90ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
91d977a583SRobert Watson 	void	*ktr_buffer;
92ea3fc8e4SJohn Baldwin 	union {
93ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
94ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
95ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
96ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
97ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
98ea3fc8e4SJohn Baldwin 	} ktr_data;
99ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
100ea3fc8e4SJohn Baldwin };
101ea3fc8e4SJohn Baldwin 
102ea3fc8e4SJohn Baldwin static int data_lengths[] = {
103ea3fc8e4SJohn Baldwin 	0,					/* none */
104ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
105ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
106ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
107ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
108ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
109ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
110ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
111ea3fc8e4SJohn Baldwin };
112ea3fc8e4SJohn Baldwin 
113ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
114ea3fc8e4SJohn Baldwin 
1155ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
11612301fc3SJohn Baldwin 
1178b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
11812301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
11912301fc3SJohn Baldwin 
1208b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
12112301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
12212301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
12312301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
124ea3fc8e4SJohn Baldwin 
125ea3fc8e4SJohn Baldwin static int print_message = 1;
126ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
1272c255e9dSRobert Watson static struct sx ktrace_sx;
128ea3fc8e4SJohn Baldwin 
129ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
130ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
1318b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize);
132ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1332c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
134ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
1352c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
136a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
137a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
138a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
13998d93822SBruce Evans 
1402c255e9dSRobert Watson /*
1412c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1422c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1432c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1442c255e9dSRobert Watson  * suppressed.
1452c255e9dSRobert Watson  */
1462c255e9dSRobert Watson static void
1472c255e9dSRobert Watson ktrace_enter(struct thread *td)
1482c255e9dSRobert Watson {
1492c255e9dSRobert Watson 
1502c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1512c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1522c255e9dSRobert Watson }
1532c255e9dSRobert Watson 
1542c255e9dSRobert Watson static void
1552c255e9dSRobert Watson ktrace_exit(struct thread *td)
1562c255e9dSRobert Watson {
1572c255e9dSRobert Watson 
1582c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1592c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1602c255e9dSRobert Watson }
1612c255e9dSRobert Watson 
1622c255e9dSRobert Watson static void
1632c255e9dSRobert Watson ktrace_assert(struct thread *td)
1642c255e9dSRobert Watson {
1652c255e9dSRobert Watson 
1662c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1672c255e9dSRobert Watson }
1682c255e9dSRobert Watson 
169ea3fc8e4SJohn Baldwin static void
170ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
171df8bae1dSRodney W. Grimes {
172ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
173ea3fc8e4SJohn Baldwin 	int i;
174df8bae1dSRodney W. Grimes 
175ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1762c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
177ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
178ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
179a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
180ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
181ea3fc8e4SJohn Baldwin 	}
182ea3fc8e4SJohn Baldwin }
183ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
184ea3fc8e4SJohn Baldwin 
185ea3fc8e4SJohn Baldwin static int
186ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
187ea3fc8e4SJohn Baldwin {
188ea3fc8e4SJohn Baldwin 	struct thread *td;
1898b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
190ea3fc8e4SJohn Baldwin 	int error;
191ea3fc8e4SJohn Baldwin 
192ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
193ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
194ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
195ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
196ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
1978b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
198ea3fc8e4SJohn Baldwin 	}
199ea3fc8e4SJohn Baldwin 
2008b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
201ea3fc8e4SJohn Baldwin 	if (error)
202ea3fc8e4SJohn Baldwin 		return (error);
203ea3fc8e4SJohn Baldwin 	td = curthread;
2042c255e9dSRobert Watson 	ktrace_enter(td);
205ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
206ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
207ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
208ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
2092c255e9dSRobert Watson 	ktrace_exit(td);
2108b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
211ea3fc8e4SJohn Baldwin 	if (error)
212ea3fc8e4SJohn Baldwin 		return (error);
213a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
214ea3fc8e4SJohn Baldwin 		return (ENOSPC);
215ea3fc8e4SJohn Baldwin 	return (0);
216ea3fc8e4SJohn Baldwin }
21712301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
218ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
219ea3fc8e4SJohn Baldwin 
2208b149b51SJohn Baldwin static u_int
2218b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize)
222ea3fc8e4SJohn Baldwin {
223ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
224a5896914SJoseph Koshy 	int bound;
225ea3fc8e4SJohn Baldwin 
226ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
227ea3fc8e4SJohn Baldwin 	print_message = 1;
228a5896914SJoseph Koshy 	bound = newsize - ktr_requestpool;
229a5896914SJoseph Koshy 	if (bound == 0)
230a5896914SJoseph Koshy 		return (ktr_requestpool);
231a5896914SJoseph Koshy 	if (bound < 0)
232ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
233a5896914SJoseph Koshy 		while (bound++ < 0) {
234ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
235ea3fc8e4SJohn Baldwin 			if (req == NULL)
236ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
237ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
238ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
239ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
240ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
241ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
242ea3fc8e4SJohn Baldwin 		}
243ea3fc8e4SJohn Baldwin 	else
244ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
245a5896914SJoseph Koshy 		while (bound-- > 0) {
246ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
247ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
248a163d034SWarner Losh 			    M_WAITOK);
249ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
250ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
251ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
252ea3fc8e4SJohn Baldwin 		}
253ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
254ea3fc8e4SJohn Baldwin }
255ea3fc8e4SJohn Baldwin 
256ea3fc8e4SJohn Baldwin static struct ktr_request *
257ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
258ea3fc8e4SJohn Baldwin {
259ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
260ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
261ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
262ea3fc8e4SJohn Baldwin 	int pm;
263ea3fc8e4SJohn Baldwin 
2642c255e9dSRobert Watson 	ktrace_enter(td);	/* XXX: In caller instead? */
265c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
266ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
267c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
2682c255e9dSRobert Watson 		ktrace_exit(td);
269ea3fc8e4SJohn Baldwin 		return (NULL);
270ea3fc8e4SJohn Baldwin 	}
271ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
272ea3fc8e4SJohn Baldwin 	if (req != NULL) {
273ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
274ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
27575768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
27675768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
27775768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
27875768576SJohn Baldwin 		}
279c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
280ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
281ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
2822bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
283e01eafefSJulian Elischer 		bcopy(td->td_name, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
284d977a583SRobert Watson 		req->ktr_buffer = NULL;
285ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
286ea3fc8e4SJohn Baldwin 	} else {
28775768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
288ea3fc8e4SJohn Baldwin 		pm = print_message;
289ea3fc8e4SJohn Baldwin 		print_message = 0;
290ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
291ea3fc8e4SJohn Baldwin 		if (pm)
292ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
2932c255e9dSRobert Watson 		ktrace_exit(td);
294ea3fc8e4SJohn Baldwin 	}
295ea3fc8e4SJohn Baldwin 	return (req);
296ea3fc8e4SJohn Baldwin }
297ea3fc8e4SJohn Baldwin 
2982c255e9dSRobert Watson /*
2992c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3002c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3012c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3022c255e9dSRobert Watson  * later.
3032c255e9dSRobert Watson  */
304ea3fc8e4SJohn Baldwin static void
3052c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
306ea3fc8e4SJohn Baldwin {
307ea3fc8e4SJohn Baldwin 
308ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3092c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
310ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3112c255e9dSRobert Watson 	ktrace_exit(td);
3122c255e9dSRobert Watson }
3132c255e9dSRobert Watson 
3142c255e9dSRobert Watson /*
3152c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3162c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3172c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3182c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
3192c255e9dSRobert Watson  * may me approximately "after" the current event.
3202c255e9dSRobert Watson  */
3212c255e9dSRobert Watson static void
3222c255e9dSRobert Watson ktr_drain(struct thread *td)
3232c255e9dSRobert Watson {
3242c255e9dSRobert Watson 	struct ktr_request *queued_req;
3252c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3262c255e9dSRobert Watson 
3272c255e9dSRobert Watson 	ktrace_assert(td);
3282c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3292c255e9dSRobert Watson 
3302c255e9dSRobert Watson 	STAILQ_INIT(&local_queue);	/* XXXRW: needed? */
3312c255e9dSRobert Watson 
3322c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3332c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3342c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3352c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3362c255e9dSRobert Watson 
3372c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3382c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3392c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3402c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3412c255e9dSRobert Watson 		}
3422c255e9dSRobert Watson 	}
3432c255e9dSRobert Watson }
3442c255e9dSRobert Watson 
3452c255e9dSRobert Watson /*
3462c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3472c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3482c255e9dSRobert Watson  * been cached in the thread.
3492c255e9dSRobert Watson  */
3502c255e9dSRobert Watson static void
3512c255e9dSRobert Watson ktr_submitrequest(struct thread *td, struct ktr_request *req)
3522c255e9dSRobert Watson {
3532c255e9dSRobert Watson 
3542c255e9dSRobert Watson 	ktrace_assert(td);
3552c255e9dSRobert Watson 
3562c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3572c255e9dSRobert Watson 	ktr_drain(td);
3582c255e9dSRobert Watson 	ktr_writerequest(td, req);
3592c255e9dSRobert Watson 	ktr_freerequest(req);
3602c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3612c255e9dSRobert Watson 
3622c255e9dSRobert Watson 	ktrace_exit(td);
363ea3fc8e4SJohn Baldwin }
364ea3fc8e4SJohn Baldwin 
365ea3fc8e4SJohn Baldwin static void
366ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
367ea3fc8e4SJohn Baldwin {
368ea3fc8e4SJohn Baldwin 
369d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
370d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
371ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
372ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
373ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
374ea3fc8e4SJohn Baldwin }
375ea3fc8e4SJohn Baldwin 
37626f9a767SRodney W. Grimes void
377ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
37871ddfdbbSDmitrij Tejblum 	int code, narg;
37971ddfdbbSDmitrij Tejblum 	register_t args[];
380df8bae1dSRodney W. Grimes {
381ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
382df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
383ea3fc8e4SJohn Baldwin 	size_t buflen;
3844b3aac3dSJohn Baldwin 	char *buf = NULL;
385df8bae1dSRodney W. Grimes 
3864b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3874b3aac3dSJohn Baldwin 	if (buflen > 0) {
388a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3894b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3904b3aac3dSJohn Baldwin 	}
391ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
39250c22331SPoul-Henning Kamp 	if (req == NULL) {
39350c22331SPoul-Henning Kamp 		if (buf != NULL)
39450c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
395ea3fc8e4SJohn Baldwin 		return;
39650c22331SPoul-Henning Kamp 	}
397ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
398df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
399df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
400ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
401ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
402d977a583SRobert Watson 		req->ktr_buffer = buf;
403ea3fc8e4SJohn Baldwin 	}
4042c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
40726f9a767SRodney W. Grimes void
408ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
40971ddfdbbSDmitrij Tejblum 	int code, error;
41071ddfdbbSDmitrij Tejblum 	register_t retval;
411df8bae1dSRodney W. Grimes {
412ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
413ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
414df8bae1dSRodney W. Grimes 
415ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
416ea3fc8e4SJohn Baldwin 	if (req == NULL)
417ea3fc8e4SJohn Baldwin 		return;
418ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
419ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
420ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
421ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
4222c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4232c255e9dSRobert Watson }
4242c255e9dSRobert Watson 
4252c255e9dSRobert Watson /*
4262c255e9dSRobert Watson  * When a process exits, drain per-process asynchronous trace records.
4272c255e9dSRobert Watson  */
4282c255e9dSRobert Watson void
4292c255e9dSRobert Watson ktrprocexit(struct thread *td)
4302c255e9dSRobert Watson {
4312c255e9dSRobert Watson 
4322c255e9dSRobert Watson 	ktrace_enter(td);
4332c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4342c255e9dSRobert Watson 	ktr_drain(td);
4352c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4362c255e9dSRobert Watson 	ktrace_exit(td);
4372c255e9dSRobert Watson }
4382c255e9dSRobert Watson 
4392c255e9dSRobert Watson /*
4402c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
4412c255e9dSRobert Watson  * system call.
4422c255e9dSRobert Watson  */
4432c255e9dSRobert Watson void
4442c255e9dSRobert Watson ktruserret(struct thread *td)
4452c255e9dSRobert Watson {
4462c255e9dSRobert Watson 
4472c255e9dSRobert Watson 	ktrace_enter(td);
4482c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4492c255e9dSRobert Watson 	ktr_drain(td);
4502c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4512c255e9dSRobert Watson 	ktrace_exit(td);
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
45426f9a767SRodney W. Grimes void
455ea3fc8e4SJohn Baldwin ktrnamei(path)
456df8bae1dSRodney W. Grimes 	char *path;
457df8bae1dSRodney W. Grimes {
458ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
459ea3fc8e4SJohn Baldwin 	int namelen;
4604b3aac3dSJohn Baldwin 	char *buf = NULL;
461df8bae1dSRodney W. Grimes 
4624b3aac3dSJohn Baldwin 	namelen = strlen(path);
4634b3aac3dSJohn Baldwin 	if (namelen > 0) {
464a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
4654b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
4664b3aac3dSJohn Baldwin 	}
467ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
46850c22331SPoul-Henning Kamp 	if (req == NULL) {
46950c22331SPoul-Henning Kamp 		if (buf != NULL)
47050c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
471ea3fc8e4SJohn Baldwin 		return;
47250c22331SPoul-Henning Kamp 	}
473ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
474ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
475d977a583SRobert Watson 		req->ktr_buffer = buf;
476ea3fc8e4SJohn Baldwin 	}
4772c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
48026f9a767SRodney W. Grimes void
481ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
482df8bae1dSRodney W. Grimes 	int fd;
483df8bae1dSRodney W. Grimes 	enum uio_rw rw;
48442ebfbf2SBrian Feldman 	struct uio *uio;
48542ebfbf2SBrian Feldman 	int error;
486df8bae1dSRodney W. Grimes {
487ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
488ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
489b92584a6SJohn Baldwin 	int datalen;
490b92584a6SJohn Baldwin 	char *buf;
491df8bae1dSRodney W. Grimes 
492552afd9cSPoul-Henning Kamp 	if (error) {
493552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
494df8bae1dSRodney W. Grimes 		return;
495552afd9cSPoul-Henning Kamp 	}
496b92584a6SJohn Baldwin 	uio->uio_offset = 0;
497b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
498b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
499a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
500552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
501552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
502552afd9cSPoul-Henning Kamp 	if (error) {
503b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
504ea3fc8e4SJohn Baldwin 		return;
505b92584a6SJohn Baldwin 	}
506b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
507b92584a6SJohn Baldwin 	if (req == NULL) {
508b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
509b92584a6SJohn Baldwin 		return;
510b92584a6SJohn Baldwin 	}
511ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
512ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
513ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
514b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
515d977a583SRobert Watson 	req->ktr_buffer = buf;
5162c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
517df8bae1dSRodney W. Grimes }
518df8bae1dSRodney W. Grimes 
51926f9a767SRodney W. Grimes void
520ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
521a93fdaacSMarcel Moolenaar 	int sig;
522df8bae1dSRodney W. Grimes 	sig_t action;
5232c42a146SMarcel Moolenaar 	sigset_t *mask;
524a93fdaacSMarcel Moolenaar 	int code;
525df8bae1dSRodney W. Grimes {
526ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
527ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
528df8bae1dSRodney W. Grimes 
529ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
530ea3fc8e4SJohn Baldwin 	if (req == NULL)
531ea3fc8e4SJohn Baldwin 		return;
532ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
533ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
534ea3fc8e4SJohn Baldwin 	kp->action = action;
535ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
536ea3fc8e4SJohn Baldwin 	kp->code = code;
5372c255e9dSRobert Watson 	ktr_enqueuerequest(curthread, req);
538df8bae1dSRodney W. Grimes }
539df8bae1dSRodney W. Grimes 
54026f9a767SRodney W. Grimes void
541ea3fc8e4SJohn Baldwin ktrcsw(out, user)
542df8bae1dSRodney W. Grimes 	int out, user;
543df8bae1dSRodney W. Grimes {
544ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
545ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
546df8bae1dSRodney W. Grimes 
547ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
548ea3fc8e4SJohn Baldwin 	if (req == NULL)
549ea3fc8e4SJohn Baldwin 		return;
550ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
551ea3fc8e4SJohn Baldwin 	kc->out = out;
552ea3fc8e4SJohn Baldwin 	kc->user = user;
5532c255e9dSRobert Watson 	ktr_enqueuerequest(curthread, req);
554df8bae1dSRodney W. Grimes }
55564cc6a13SJohn Baldwin #endif /* KTRACE */
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes /* Interface and common routines */
558df8bae1dSRodney W. Grimes 
559d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
560df8bae1dSRodney W. Grimes struct ktrace_args {
561df8bae1dSRodney W. Grimes 	char	*fname;
562df8bae1dSRodney W. Grimes 	int	ops;
563df8bae1dSRodney W. Grimes 	int	facs;
564df8bae1dSRodney W. Grimes 	int	pid;
565df8bae1dSRodney W. Grimes };
566d2d3e875SBruce Evans #endif
567df8bae1dSRodney W. Grimes /* ARGSUSED */
56826f9a767SRodney W. Grimes int
569b40ce416SJulian Elischer ktrace(td, uap)
570b40ce416SJulian Elischer 	struct thread *td;
571df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
572df8bae1dSRodney W. Grimes {
573db6a20e2SGarrett Wollman #ifdef KTRACE
574df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
575df8bae1dSRodney W. Grimes 	register struct proc *p;
576df8bae1dSRodney W. Grimes 	struct pgrp *pg;
577df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
578df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
579df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
580400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
58133f19beeSJohn Baldwin 	int flags, error = 0, vfslocked;
582df8bae1dSRodney W. Grimes 	struct nameidata nd;
583a5881ea5SJohn Baldwin 	struct ucred *cred;
584df8bae1dSRodney W. Grimes 
58564cc6a13SJohn Baldwin 	/*
58664cc6a13SJohn Baldwin 	 * Need something to (un)trace.
58764cc6a13SJohn Baldwin 	 */
58864cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
58964cc6a13SJohn Baldwin 		return (EINVAL);
59064cc6a13SJohn Baldwin 
5912c255e9dSRobert Watson 	ktrace_enter(td);
592df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
593df8bae1dSRodney W. Grimes 		/*
594df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
595df8bae1dSRodney W. Grimes 		 */
59633f19beeSJohn Baldwin 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
59733f19beeSJohn Baldwin 		    uap->fname, td);
598e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
5999e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
600797f2d22SPoul-Henning Kamp 		if (error) {
6012c255e9dSRobert Watson 			ktrace_exit(td);
602df8bae1dSRodney W. Grimes 			return (error);
603df8bae1dSRodney W. Grimes 		}
60433f19beeSJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
605762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
606df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
607b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
608df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
609a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
61033f19beeSJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
6112c255e9dSRobert Watson 			ktrace_exit(td);
612df8bae1dSRodney W. Grimes 			return (EACCES);
613df8bae1dSRodney W. Grimes 		}
61433f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
615df8bae1dSRodney W. Grimes 	}
616df8bae1dSRodney W. Grimes 	/*
61779deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
618df8bae1dSRodney W. Grimes 	 */
619df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
62051fd6380SMike Pritchard 		int vrele_count;
62151fd6380SMike Pritchard 
62251fd6380SMike Pritchard 		vrele_count = 0;
6231005a129SJohn Baldwin 		sx_slock(&allproc_lock);
6244f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
625a7ff7443SJohn Baldwin 			PROC_LOCK(p);
626a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
627ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
628ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
629a5881ea5SJohn Baldwin 					cred = p->p_tracecred;
630a5881ea5SJohn Baldwin 					p->p_tracecred = NULL;
631a5881ea5SJohn Baldwin 					p->p_tracevp = NULL;
632df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
633ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
63451fd6380SMike Pritchard 					vrele_count++;
635a5881ea5SJohn Baldwin 					crfree(cred);
63651fd6380SMike Pritchard 				} else
637df8bae1dSRodney W. Grimes 					error = EPERM;
638df8bae1dSRodney W. Grimes 			}
639a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
64079deba82SMatthew Dillon 		}
6411005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
64251fd6380SMike Pritchard 		if (vrele_count > 0) {
64351fd6380SMike Pritchard 			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
64451fd6380SMike Pritchard 			while (vrele_count-- > 0)
64551fd6380SMike Pritchard 				vrele(vp);
64651fd6380SMike Pritchard 			VFS_UNLOCK_GIANT(vfslocked);
64751fd6380SMike Pritchard 		}
648df8bae1dSRodney W. Grimes 		goto done;
649df8bae1dSRodney W. Grimes 	}
650df8bae1dSRodney W. Grimes 	/*
651df8bae1dSRodney W. Grimes 	 * do it
652df8bae1dSRodney W. Grimes 	 */
65364cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
654df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
655df8bae1dSRodney W. Grimes 		/*
656df8bae1dSRodney W. Grimes 		 * by process group
657df8bae1dSRodney W. Grimes 		 */
658df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
659df8bae1dSRodney W. Grimes 		if (pg == NULL) {
660ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
661df8bae1dSRodney W. Grimes 			error = ESRCH;
662df8bae1dSRodney W. Grimes 			goto done;
663df8bae1dSRodney W. Grimes 		}
664f591779bSSeigo Tanimura 		/*
665f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
666ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
667f591779bSSeigo Tanimura 		 */
668f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
669400a74bfSPawel Jakub Dawidek 		nfound = 0;
670400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
671400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
672400a74bfSPawel Jakub Dawidek 			if (p_cansee(td, p) != 0) {
673400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
674400a74bfSPawel Jakub Dawidek 				continue;
675400a74bfSPawel Jakub Dawidek 			}
676400a74bfSPawel Jakub Dawidek 			PROC_UNLOCK(p);
677400a74bfSPawel Jakub Dawidek 			nfound++;
678df8bae1dSRodney W. Grimes 			if (descend)
679a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
680df8bae1dSRodney W. Grimes 			else
681a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
682400a74bfSPawel Jakub Dawidek 		}
683400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
684400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
685400a74bfSPawel Jakub Dawidek 			error = ESRCH;
686400a74bfSPawel Jakub Dawidek 			goto done;
687400a74bfSPawel Jakub Dawidek 		}
688df8bae1dSRodney W. Grimes 	} else {
689df8bae1dSRodney W. Grimes 		/*
690df8bae1dSRodney W. Grimes 		 * by pid
691df8bae1dSRodney W. Grimes 		 */
692df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
693df8bae1dSRodney W. Grimes 		if (p == NULL) {
69464cc6a13SJohn Baldwin 			sx_sunlock(&proctree_lock);
695df8bae1dSRodney W. Grimes 			error = ESRCH;
696df8bae1dSRodney W. Grimes 			goto done;
697df8bae1dSRodney W. Grimes 		}
6984eb7c9f6SPawel Jakub Dawidek 		error = p_cansee(td, p);
69964cc6a13SJohn Baldwin 		/*
70064cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
70164cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
70264cc6a13SJohn Baldwin 		 */
70333a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
704b0d9aeddSPawel Jakub Dawidek 		if (error) {
705b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
7064eb7c9f6SPawel Jakub Dawidek 			goto done;
707b0d9aeddSPawel Jakub Dawidek 		}
708df8bae1dSRodney W. Grimes 		if (descend)
709a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
710df8bae1dSRodney W. Grimes 		else
711a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
712df8bae1dSRodney W. Grimes 	}
71364cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
714df8bae1dSRodney W. Grimes 	if (!ret)
715df8bae1dSRodney W. Grimes 		error = EPERM;
716df8bae1dSRodney W. Grimes done:
71764cc6a13SJohn Baldwin 	if (vp != NULL) {
71833f19beeSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
719a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
72033f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
72164cc6a13SJohn Baldwin 	}
7222c255e9dSRobert Watson 	ktrace_exit(td);
723df8bae1dSRodney W. Grimes 	return (error);
72464cc6a13SJohn Baldwin #else /* !KTRACE */
72564cc6a13SJohn Baldwin 	return (ENOSYS);
72664cc6a13SJohn Baldwin #endif /* KTRACE */
727df8bae1dSRodney W. Grimes }
728df8bae1dSRodney W. Grimes 
729e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
730e6c4b9baSPoul-Henning Kamp int
731b40ce416SJulian Elischer utrace(td, uap)
732b40ce416SJulian Elischer 	struct thread *td;
733e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
734e6c4b9baSPoul-Henning Kamp {
735b40ce416SJulian Elischer 
736e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
737ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
7387f05b035SAlfred Perlstein 	void *cp;
739c9e7d28eSJohn Baldwin 	int error;
740e6c4b9baSPoul-Henning Kamp 
741c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
742c9e7d28eSJohn Baldwin 		return (0);
743bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
7440bad156aSAlfred Perlstein 		return (EINVAL);
745a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
746c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
74750c22331SPoul-Henning Kamp 	if (error) {
74850c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
749c9e7d28eSJohn Baldwin 		return (error);
75050c22331SPoul-Henning Kamp 	}
751ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
75250c22331SPoul-Henning Kamp 	if (req == NULL) {
75350c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
754b10221ffSJoseph Koshy 		return (ENOMEM);
75550c22331SPoul-Henning Kamp 	}
756d977a583SRobert Watson 	req->ktr_buffer = cp;
757ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
7582c255e9dSRobert Watson 	ktr_submitrequest(td, req);
759e6c4b9baSPoul-Henning Kamp 	return (0);
76064cc6a13SJohn Baldwin #else /* !KTRACE */
761e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
76264cc6a13SJohn Baldwin #endif /* KTRACE */
763e6c4b9baSPoul-Henning Kamp }
764e6c4b9baSPoul-Henning Kamp 
765db6a20e2SGarrett Wollman #ifdef KTRACE
76687b6de2bSPoul-Henning Kamp static int
767a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
768a7ff7443SJohn Baldwin 	struct thread *td;
769a7ff7443SJohn Baldwin 	struct proc *p;
770df8bae1dSRodney W. Grimes 	int ops, facs;
771df8bae1dSRodney W. Grimes 	struct vnode *vp;
772df8bae1dSRodney W. Grimes {
773ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
774a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
775df8bae1dSRodney W. Grimes 
776a7ff7443SJohn Baldwin 	PROC_LOCK(p);
777a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
778a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
779df8bae1dSRodney W. Grimes 		return (0);
780a7ff7443SJohn Baldwin 	}
781ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
782df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
783a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
784df8bae1dSRodney W. Grimes 			/*
785a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
786df8bae1dSRodney W. Grimes 			 */
787a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
788ea3fc8e4SJohn Baldwin 			VREF(vp);
789a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
790a5881ea5SJohn Baldwin 		}
791a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
792a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
793a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
794df8bae1dSRodney W. Grimes 		}
795df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
79632f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
797df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
798df8bae1dSRodney W. Grimes 	} else {
799df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
800df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
801df8bae1dSRodney W. Grimes 			/* no more tracing */
802df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
803a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
804a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
805a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
806a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
807a7ff7443SJohn Baldwin 		}
808a7ff7443SJohn Baldwin 	}
809ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
810a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
81164cc6a13SJohn Baldwin 	if (tracevp != NULL) {
812033eb86eSJeff Roberson 		int vfslocked;
813033eb86eSJeff Roberson 
814033eb86eSJeff Roberson 		vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
815ea3fc8e4SJohn Baldwin 		vrele(tracevp);
816033eb86eSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
81764cc6a13SJohn Baldwin 	}
818a5881ea5SJohn Baldwin 	if (tracecred != NULL)
819a5881ea5SJohn Baldwin 		crfree(tracecred);
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	return (1);
822df8bae1dSRodney W. Grimes }
823df8bae1dSRodney W. Grimes 
82487b6de2bSPoul-Henning Kamp static int
825a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
826a7ff7443SJohn Baldwin 	struct thread *td;
827a7ff7443SJohn Baldwin 	struct proc *top;
828df8bae1dSRodney W. Grimes 	int ops, facs;
829df8bae1dSRodney W. Grimes 	struct vnode *vp;
830df8bae1dSRodney W. Grimes {
831df8bae1dSRodney W. Grimes 	register struct proc *p;
832df8bae1dSRodney W. Grimes 	register int ret = 0;
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	p = top;
83564cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
836df8bae1dSRodney W. Grimes 	for (;;) {
837a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
838df8bae1dSRodney W. Grimes 		/*
839df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
840df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
841df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
842df8bae1dSRodney W. Grimes 		 */
8432e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
8442e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
845df8bae1dSRodney W. Grimes 		else for (;;) {
84664cc6a13SJohn Baldwin 			if (p == top)
847df8bae1dSRodney W. Grimes 				return (ret);
8482e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
8492e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
850df8bae1dSRodney W. Grimes 				break;
851df8bae1dSRodney W. Grimes 			}
852b75356e1SJeffrey Hsu 			p = p->p_pptr;
853df8bae1dSRodney W. Grimes 		}
854df8bae1dSRodney W. Grimes 	}
855df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
856df8bae1dSRodney W. Grimes }
857df8bae1dSRodney W. Grimes 
85887b6de2bSPoul-Henning Kamp static void
8592c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
860df8bae1dSRodney W. Grimes {
861ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
862ea3fc8e4SJohn Baldwin 	struct vnode *vp;
863ea3fc8e4SJohn Baldwin 	struct proc *p;
864ea3fc8e4SJohn Baldwin 	struct ucred *cred;
865df8bae1dSRodney W. Grimes 	struct uio auio;
866ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
867f2a2857bSKirk McKusick 	struct mount *mp;
868ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
86933f19beeSJohn Baldwin 	int error, vfslocked;
870df8bae1dSRodney W. Grimes 
8712c255e9dSRobert Watson 	/*
8722c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
8732c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
8742c255e9dSRobert Watson 	 *
8752c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
8762c255e9dSRobert Watson 	 * the vnode has been closed.
8772c255e9dSRobert Watson 	 */
8782c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
8792c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
8802c255e9dSRobert Watson 	if (vp != NULL)
8812c255e9dSRobert Watson 		VREF(vp);
8822c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
8832c255e9dSRobert Watson 	if (cred != NULL)
8842c255e9dSRobert Watson 		crhold(cred);
8852c255e9dSRobert Watson 	mtx_unlock(&ktrace_mtx);
8862c255e9dSRobert Watson 
887ea3fc8e4SJohn Baldwin 	/*
888ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
8892c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
8902c255e9dSRobert Watson 	 * in sync: we should have both or neither.
891ea3fc8e4SJohn Baldwin 	 */
8922c255e9dSRobert Watson 	if (vp == NULL) {
8932c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
894df8bae1dSRodney W. Grimes 		return;
8952c255e9dSRobert Watson 	}
8962c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
8972c255e9dSRobert Watson 
898ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
8998b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
900ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
901df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
902df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
903df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
904df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
905df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
906df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
907df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
908df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
909ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
910ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
911ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
912ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
913ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
914df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
915ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
916ea3fc8e4SJohn Baldwin 	}
917ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
918d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
919d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
920ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
921ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
922ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
923b92584a6SJohn Baldwin 	}
9242c255e9dSRobert Watson 
92533f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
926f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
927cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
928ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
929467a273cSRobert Watson #ifdef MAC
93030d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
931467a273cSRobert Watson 	if (error == 0)
932467a273cSRobert Watson #endif
933ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
934b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
935f2a2857bSKirk McKusick 	vn_finished_write(mp);
936704c9f00SJohn Baldwin 	vrele(vp);
93733f19beeSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
938df8bae1dSRodney W. Grimes 	if (!error)
939df8bae1dSRodney W. Grimes 		return;
940df8bae1dSRodney W. Grimes 	/*
941ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
942ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
943ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
944df8bae1dSRodney W. Grimes 	 */
945df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
946df8bae1dSRodney W. Grimes 	    error);
947ea3fc8e4SJohn Baldwin 	vrele_count = 0;
948ea3fc8e4SJohn Baldwin 	/*
949ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
950ea3fc8e4SJohn Baldwin 	 * system.
951ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
952ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
953ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
954ea3fc8e4SJohn Baldwin 	 */
955a5881ea5SJohn Baldwin 	cred = NULL;
9561005a129SJohn Baldwin 	sx_slock(&allproc_lock);
9574f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
958ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
959a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
960ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
961a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
962df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
963a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
964a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
965ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
966ea3fc8e4SJohn Baldwin 			vrele_count++;
967df8bae1dSRodney W. Grimes 		}
968ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
969a5881ea5SJohn Baldwin 		if (cred != NULL) {
970a5881ea5SJohn Baldwin 			crfree(cred);
971a5881ea5SJohn Baldwin 			cred = NULL;
972a5881ea5SJohn Baldwin 		}
973df8bae1dSRodney W. Grimes 	}
9741005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
9752c255e9dSRobert Watson 
976ea3fc8e4SJohn Baldwin 	/*
9772c255e9dSRobert Watson 	 * We can't clear any pending requests in threads that have cached
9782c255e9dSRobert Watson 	 * them but not yet committed them, as those are per-thread.  The
9792c255e9dSRobert Watson 	 * thread will have to clear it itself on system call return.
980ea3fc8e4SJohn Baldwin 	 */
98133f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
982ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
983ea3fc8e4SJohn Baldwin 		vrele(vp);
98433f19beeSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
985df8bae1dSRodney W. Grimes }
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes /*
988df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
989df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
990df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
991df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
992df8bae1dSRodney W. Grimes  * so, only root may further change it.
993df8bae1dSRodney W. Grimes  */
99487b6de2bSPoul-Henning Kamp static int
995a7ff7443SJohn Baldwin ktrcanset(td, targetp)
996a7ff7443SJohn Baldwin 	struct thread *td;
997a7ff7443SJohn Baldwin 	struct proc *targetp;
998df8bae1dSRodney W. Grimes {
999df8bae1dSRodney W. Grimes 
1000a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1001a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
100232f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
100375c13541SPoul-Henning Kamp 		return (0);
1004a0f75161SRobert Watson 
1005f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1006a0f75161SRobert Watson 		return (0);
1007a0f75161SRobert Watson 
1008df8bae1dSRodney W. Grimes 	return (1);
1009df8bae1dSRodney W. Grimes }
1010df8bae1dSRodney W. Grimes 
1011db6a20e2SGarrett Wollman #endif /* KTRACE */
1012