xref: /freebsd/sys/kern/kern_ktrace.c (revision 5ca4819ddf209124a8ffaad0f44d58cf6c77ec37)
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"
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40f23b4c91SGarrett Wollman #include <sys/systm.h>
41ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
42ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
43ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
46ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
47033eb86eSJeff Roberson #include <sys/mount.h>
48df8bae1dSRodney W. Grimes #include <sys/namei.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50ea3fc8e4SJohn Baldwin #include <sys/proc.h>
51ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5360e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5460e15db9SDag-Erling Smørgrav #include <sys/stat.h>
55df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
561005a129SJohn Baldwin #include <sys/sx.h>
57ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
58df8bae1dSRodney W. Grimes #include <sys/syslog.h>
59ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
60df8bae1dSRodney W. Grimes 
61aed55708SRobert Watson #include <security/mac/mac_framework.h>
62aed55708SRobert Watson 
632c255e9dSRobert Watson /*
642c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
652c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
662c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
672c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
682c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
692c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
702c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
712c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
722c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
732c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
742c255e9dSRobert Watson  *
752c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
762c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
772c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
782c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
792c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
802c255e9dSRobert Watson  */
812c255e9dSRobert Watson 
82a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8355166637SPoul-Henning Kamp 
84db6a20e2SGarrett Wollman #ifdef KTRACE
85ea3fc8e4SJohn Baldwin 
86ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
87ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
88ea3fc8e4SJohn Baldwin #endif
89ea3fc8e4SJohn Baldwin 
90ea3fc8e4SJohn Baldwin struct ktr_request {
91ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
92d977a583SRobert Watson 	void	*ktr_buffer;
93ea3fc8e4SJohn Baldwin 	union {
94ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
95ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
96ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
97ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
98ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
99ea3fc8e4SJohn Baldwin 	} ktr_data;
100ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
101ea3fc8e4SJohn Baldwin };
102ea3fc8e4SJohn Baldwin 
103ea3fc8e4SJohn Baldwin static int data_lengths[] = {
104ea3fc8e4SJohn Baldwin 	0,					/* none */
105ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
106ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
107ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
108ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
109ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
110ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
11160e15db9SDag-Erling Smørgrav 	0,					/* KTR_USER */
11260e15db9SDag-Erling Smørgrav 	0,					/* KTR_STRUCT */
113a56be37eSJohn Baldwin 	0,					/* KTR_SYSCTL */
114ea3fc8e4SJohn Baldwin };
115ea3fc8e4SJohn Baldwin 
116ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
117ea3fc8e4SJohn Baldwin 
1185ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
11912301fc3SJohn Baldwin 
1208b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
12112301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
12212301fc3SJohn Baldwin 
1238b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
12412301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
12512301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
12612301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
127ea3fc8e4SJohn Baldwin 
128ea3fc8e4SJohn Baldwin static int print_message = 1;
129ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
1302c255e9dSRobert Watson static struct sx ktrace_sx;
131ea3fc8e4SJohn Baldwin 
132ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
133ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
1348b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize);
135ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1362c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
137ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
1382c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
139a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
140a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
141a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
14298d93822SBruce Evans 
1432c255e9dSRobert Watson /*
1442c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1452c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1462c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1472c255e9dSRobert Watson  * suppressed.
1482c255e9dSRobert Watson  */
1492c255e9dSRobert Watson static void
1502c255e9dSRobert Watson ktrace_enter(struct thread *td)
1512c255e9dSRobert Watson {
1522c255e9dSRobert Watson 
1532c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1542c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1552c255e9dSRobert Watson }
1562c255e9dSRobert Watson 
1572c255e9dSRobert Watson static void
1582c255e9dSRobert Watson ktrace_exit(struct thread *td)
1592c255e9dSRobert Watson {
1602c255e9dSRobert Watson 
1612c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1622c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1632c255e9dSRobert Watson }
1642c255e9dSRobert Watson 
1652c255e9dSRobert Watson static void
1662c255e9dSRobert Watson ktrace_assert(struct thread *td)
1672c255e9dSRobert Watson {
1682c255e9dSRobert Watson 
1692c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1702c255e9dSRobert Watson }
1712c255e9dSRobert Watson 
172ea3fc8e4SJohn Baldwin static void
173ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
174df8bae1dSRodney W. Grimes {
175ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
176ea3fc8e4SJohn Baldwin 	int i;
177df8bae1dSRodney W. Grimes 
178ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1792c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
180ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
181ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
182a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
183ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
184ea3fc8e4SJohn Baldwin 	}
185ea3fc8e4SJohn Baldwin }
186ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
187ea3fc8e4SJohn Baldwin 
188ea3fc8e4SJohn Baldwin static int
189ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
190ea3fc8e4SJohn Baldwin {
191ea3fc8e4SJohn Baldwin 	struct thread *td;
1928b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
193ea3fc8e4SJohn Baldwin 	int error;
194ea3fc8e4SJohn Baldwin 
195ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
196ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
197ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
198ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
199ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
2008b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
201ea3fc8e4SJohn Baldwin 	}
202ea3fc8e4SJohn Baldwin 
2038b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
204ea3fc8e4SJohn Baldwin 	if (error)
205ea3fc8e4SJohn Baldwin 		return (error);
206ea3fc8e4SJohn Baldwin 	td = curthread;
2072c255e9dSRobert Watson 	ktrace_enter(td);
208ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
209ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
210ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
211ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
2122c255e9dSRobert Watson 	ktrace_exit(td);
2138b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
214ea3fc8e4SJohn Baldwin 	if (error)
215ea3fc8e4SJohn Baldwin 		return (error);
216a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
217ea3fc8e4SJohn Baldwin 		return (ENOSPC);
218ea3fc8e4SJohn Baldwin 	return (0);
219ea3fc8e4SJohn Baldwin }
22012301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
221ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
222ea3fc8e4SJohn Baldwin 
2238b149b51SJohn Baldwin static u_int
2248b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize)
225ea3fc8e4SJohn Baldwin {
226ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
227a5896914SJoseph Koshy 	int bound;
228ea3fc8e4SJohn Baldwin 
229ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
230ea3fc8e4SJohn Baldwin 	print_message = 1;
231a5896914SJoseph Koshy 	bound = newsize - ktr_requestpool;
232a5896914SJoseph Koshy 	if (bound == 0)
233a5896914SJoseph Koshy 		return (ktr_requestpool);
234a5896914SJoseph Koshy 	if (bound < 0)
235ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
236a5896914SJoseph Koshy 		while (bound++ < 0) {
237ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
238ea3fc8e4SJohn Baldwin 			if (req == NULL)
239ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
240ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
241ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
242ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
243ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
244ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
245ea3fc8e4SJohn Baldwin 		}
246ea3fc8e4SJohn Baldwin 	else
247ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
248a5896914SJoseph Koshy 		while (bound-- > 0) {
249ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
250ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
251a163d034SWarner Losh 			    M_WAITOK);
252ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
253ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
254ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
255ea3fc8e4SJohn Baldwin 		}
256ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
257ea3fc8e4SJohn Baldwin }
258ea3fc8e4SJohn Baldwin 
2595ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2605ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2615ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2625ca4819dSJohn Baldwin 
263ea3fc8e4SJohn Baldwin static struct ktr_request *
264ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
265ea3fc8e4SJohn Baldwin {
266ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
267ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
268ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
269ea3fc8e4SJohn Baldwin 	int pm;
270ea3fc8e4SJohn Baldwin 
2712c255e9dSRobert Watson 	ktrace_enter(td);	/* XXX: In caller instead? */
272c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
273ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
274c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
2752c255e9dSRobert Watson 		ktrace_exit(td);
276ea3fc8e4SJohn Baldwin 		return (NULL);
277ea3fc8e4SJohn Baldwin 	}
278ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
279ea3fc8e4SJohn Baldwin 	if (req != NULL) {
280ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
281ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
28275768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
28375768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
28475768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
28575768576SJohn Baldwin 		}
286c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
287ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
288ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
2892bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
2905ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
2915ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
292d977a583SRobert Watson 		req->ktr_buffer = NULL;
293ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
294ea3fc8e4SJohn Baldwin 	} else {
29575768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
296ea3fc8e4SJohn Baldwin 		pm = print_message;
297ea3fc8e4SJohn Baldwin 		print_message = 0;
298ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
299ea3fc8e4SJohn Baldwin 		if (pm)
300ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
3012c255e9dSRobert Watson 		ktrace_exit(td);
302ea3fc8e4SJohn Baldwin 	}
303ea3fc8e4SJohn Baldwin 	return (req);
304ea3fc8e4SJohn Baldwin }
305ea3fc8e4SJohn Baldwin 
3062c255e9dSRobert Watson /*
3072c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3082c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3092c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3102c255e9dSRobert Watson  * later.
3112c255e9dSRobert Watson  */
312ea3fc8e4SJohn Baldwin static void
3132c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
314ea3fc8e4SJohn Baldwin {
315ea3fc8e4SJohn Baldwin 
316ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3172c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
318ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3192c255e9dSRobert Watson 	ktrace_exit(td);
3202c255e9dSRobert Watson }
3212c255e9dSRobert Watson 
3222c255e9dSRobert Watson /*
3232c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3242c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3252c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3262c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
327a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3282c255e9dSRobert Watson  */
3292c255e9dSRobert Watson static void
3302c255e9dSRobert Watson ktr_drain(struct thread *td)
3312c255e9dSRobert Watson {
3322c255e9dSRobert Watson 	struct ktr_request *queued_req;
3332c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3342c255e9dSRobert Watson 
3352c255e9dSRobert Watson 	ktrace_assert(td);
3362c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3372c255e9dSRobert Watson 
3382c255e9dSRobert Watson 	STAILQ_INIT(&local_queue);	/* XXXRW: needed? */
3392c255e9dSRobert Watson 
3402c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3412c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3422c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3432c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3442c255e9dSRobert Watson 
3452c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3462c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3472c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3482c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3492c255e9dSRobert Watson 		}
3502c255e9dSRobert Watson 	}
3512c255e9dSRobert Watson }
3522c255e9dSRobert Watson 
3532c255e9dSRobert Watson /*
3542c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3552c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3562c255e9dSRobert Watson  * been cached in the thread.
3572c255e9dSRobert Watson  */
3582c255e9dSRobert Watson static void
3592c255e9dSRobert Watson ktr_submitrequest(struct thread *td, struct ktr_request *req)
3602c255e9dSRobert Watson {
3612c255e9dSRobert Watson 
3622c255e9dSRobert Watson 	ktrace_assert(td);
3632c255e9dSRobert Watson 
3642c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3652c255e9dSRobert Watson 	ktr_drain(td);
3662c255e9dSRobert Watson 	ktr_writerequest(td, req);
3672c255e9dSRobert Watson 	ktr_freerequest(req);
3682c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3692c255e9dSRobert Watson 
3702c255e9dSRobert Watson 	ktrace_exit(td);
371ea3fc8e4SJohn Baldwin }
372ea3fc8e4SJohn Baldwin 
373ea3fc8e4SJohn Baldwin static void
374ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
375ea3fc8e4SJohn Baldwin {
376ea3fc8e4SJohn Baldwin 
377d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
378d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
379ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
380ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
381ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
382ea3fc8e4SJohn Baldwin }
383ea3fc8e4SJohn Baldwin 
38426f9a767SRodney W. Grimes void
385ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
38671ddfdbbSDmitrij Tejblum 	int code, narg;
38771ddfdbbSDmitrij Tejblum 	register_t args[];
388df8bae1dSRodney W. Grimes {
389ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
390df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
391ea3fc8e4SJohn Baldwin 	size_t buflen;
3924b3aac3dSJohn Baldwin 	char *buf = NULL;
393df8bae1dSRodney W. Grimes 
3944b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3954b3aac3dSJohn Baldwin 	if (buflen > 0) {
396a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3974b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3984b3aac3dSJohn Baldwin 	}
399ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
40050c22331SPoul-Henning Kamp 	if (req == NULL) {
40150c22331SPoul-Henning Kamp 		if (buf != NULL)
40250c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
403ea3fc8e4SJohn Baldwin 		return;
40450c22331SPoul-Henning Kamp 	}
405ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
406df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
407df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
408ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
409ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
410d977a583SRobert Watson 		req->ktr_buffer = buf;
411ea3fc8e4SJohn Baldwin 	}
4122c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
41526f9a767SRodney W. Grimes void
416ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
41771ddfdbbSDmitrij Tejblum 	int code, error;
41871ddfdbbSDmitrij Tejblum 	register_t retval;
419df8bae1dSRodney W. Grimes {
420ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
421ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
422df8bae1dSRodney W. Grimes 
423ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
424ea3fc8e4SJohn Baldwin 	if (req == NULL)
425ea3fc8e4SJohn Baldwin 		return;
426ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
427ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
428ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
429ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
4302c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4312c255e9dSRobert Watson }
4322c255e9dSRobert Watson 
4332c255e9dSRobert Watson /*
4342c255e9dSRobert Watson  * When a process exits, drain per-process asynchronous trace records.
4352c255e9dSRobert Watson  */
4362c255e9dSRobert Watson void
4372c255e9dSRobert Watson ktrprocexit(struct thread *td)
4382c255e9dSRobert Watson {
4392c255e9dSRobert Watson 
4402c255e9dSRobert Watson 	ktrace_enter(td);
4412c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4422c255e9dSRobert Watson 	ktr_drain(td);
4432c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4442c255e9dSRobert Watson 	ktrace_exit(td);
4452c255e9dSRobert Watson }
4462c255e9dSRobert Watson 
4472c255e9dSRobert Watson /*
4482c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
4492c255e9dSRobert Watson  * system call.
4502c255e9dSRobert Watson  */
4512c255e9dSRobert Watson void
4522c255e9dSRobert Watson ktruserret(struct thread *td)
4532c255e9dSRobert Watson {
4542c255e9dSRobert Watson 
4552c255e9dSRobert Watson 	ktrace_enter(td);
4562c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4572c255e9dSRobert Watson 	ktr_drain(td);
4582c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4592c255e9dSRobert Watson 	ktrace_exit(td);
460df8bae1dSRodney W. Grimes }
461df8bae1dSRodney W. Grimes 
46226f9a767SRodney W. Grimes void
463ea3fc8e4SJohn Baldwin ktrnamei(path)
464df8bae1dSRodney W. Grimes 	char *path;
465df8bae1dSRodney W. Grimes {
466ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
467ea3fc8e4SJohn Baldwin 	int namelen;
4684b3aac3dSJohn Baldwin 	char *buf = NULL;
469df8bae1dSRodney W. Grimes 
4704b3aac3dSJohn Baldwin 	namelen = strlen(path);
4714b3aac3dSJohn Baldwin 	if (namelen > 0) {
472a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
4734b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
4744b3aac3dSJohn Baldwin 	}
475ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
47650c22331SPoul-Henning Kamp 	if (req == NULL) {
47750c22331SPoul-Henning Kamp 		if (buf != NULL)
47850c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
479ea3fc8e4SJohn Baldwin 		return;
48050c22331SPoul-Henning Kamp 	}
481ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
482ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
483d977a583SRobert Watson 		req->ktr_buffer = buf;
484ea3fc8e4SJohn Baldwin 	}
4852c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
486df8bae1dSRodney W. Grimes }
487df8bae1dSRodney W. Grimes 
48826f9a767SRodney W. Grimes void
489a56be37eSJohn Baldwin ktrsysctl(name, namelen)
490a56be37eSJohn Baldwin 	int *name;
491a56be37eSJohn Baldwin 	u_int namelen;
492a56be37eSJohn Baldwin {
493a56be37eSJohn Baldwin 	struct ktr_request *req;
494a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
495a56be37eSJohn Baldwin 	char *mibname;
496a56be37eSJohn Baldwin 	size_t mibnamelen;
497a56be37eSJohn Baldwin 	int error;
498a56be37eSJohn Baldwin 
499a56be37eSJohn Baldwin 	/* Lookup name of mib. */
500a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
501a56be37eSJohn Baldwin 	mib[0] = 0;
502a56be37eSJohn Baldwin 	mib[1] = 1;
503a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
504a56be37eSJohn Baldwin 	mibnamelen = 128;
505a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
506a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
507a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
508a56be37eSJohn Baldwin 	if (error) {
509a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
510a56be37eSJohn Baldwin 		return;
511a56be37eSJohn Baldwin 	}
512a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
513a56be37eSJohn Baldwin 	if (req == NULL) {
514a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
515a56be37eSJohn Baldwin 		return;
516a56be37eSJohn Baldwin 	}
517a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
518a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
519a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
520a56be37eSJohn Baldwin }
521a56be37eSJohn Baldwin 
522a56be37eSJohn Baldwin void
523ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
524df8bae1dSRodney W. Grimes 	int fd;
525df8bae1dSRodney W. Grimes 	enum uio_rw rw;
52642ebfbf2SBrian Feldman 	struct uio *uio;
52742ebfbf2SBrian Feldman 	int error;
528df8bae1dSRodney W. Grimes {
529ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
530ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
531b92584a6SJohn Baldwin 	int datalen;
532b92584a6SJohn Baldwin 	char *buf;
533df8bae1dSRodney W. Grimes 
534552afd9cSPoul-Henning Kamp 	if (error) {
535552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
536df8bae1dSRodney W. Grimes 		return;
537552afd9cSPoul-Henning Kamp 	}
538b92584a6SJohn Baldwin 	uio->uio_offset = 0;
539b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
540b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
541a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
542552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
543552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
544552afd9cSPoul-Henning Kamp 	if (error) {
545b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
546ea3fc8e4SJohn Baldwin 		return;
547b92584a6SJohn Baldwin 	}
548b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
549b92584a6SJohn Baldwin 	if (req == NULL) {
550b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
551b92584a6SJohn Baldwin 		return;
552b92584a6SJohn Baldwin 	}
553ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
554ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
555ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
556b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
557d977a583SRobert Watson 	req->ktr_buffer = buf;
5582c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
559df8bae1dSRodney W. Grimes }
560df8bae1dSRodney W. Grimes 
56126f9a767SRodney W. Grimes void
562ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
563a93fdaacSMarcel Moolenaar 	int sig;
564df8bae1dSRodney W. Grimes 	sig_t action;
5652c42a146SMarcel Moolenaar 	sigset_t *mask;
566a93fdaacSMarcel Moolenaar 	int code;
567df8bae1dSRodney W. Grimes {
568ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
569ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
570df8bae1dSRodney W. Grimes 
571ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
572ea3fc8e4SJohn Baldwin 	if (req == NULL)
573ea3fc8e4SJohn Baldwin 		return;
574ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
575ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
576ea3fc8e4SJohn Baldwin 	kp->action = action;
577ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
578ea3fc8e4SJohn Baldwin 	kp->code = code;
5792c255e9dSRobert Watson 	ktr_enqueuerequest(curthread, req);
580df8bae1dSRodney W. Grimes }
581df8bae1dSRodney W. Grimes 
58226f9a767SRodney W. Grimes void
583ea3fc8e4SJohn Baldwin ktrcsw(out, user)
584df8bae1dSRodney W. Grimes 	int out, user;
585df8bae1dSRodney W. Grimes {
586ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
587ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
588df8bae1dSRodney W. Grimes 
589ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
590ea3fc8e4SJohn Baldwin 	if (req == NULL)
591ea3fc8e4SJohn Baldwin 		return;
592ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
593ea3fc8e4SJohn Baldwin 	kc->out = out;
594ea3fc8e4SJohn Baldwin 	kc->user = user;
5952c255e9dSRobert Watson 	ktr_enqueuerequest(curthread, req);
596df8bae1dSRodney W. Grimes }
59760e15db9SDag-Erling Smørgrav 
59860e15db9SDag-Erling Smørgrav void
59960e15db9SDag-Erling Smørgrav ktrstruct(name, namelen, data, datalen)
60060e15db9SDag-Erling Smørgrav 	const char *name;
60160e15db9SDag-Erling Smørgrav 	size_t namelen;
60260e15db9SDag-Erling Smørgrav 	void *data;
60360e15db9SDag-Erling Smørgrav 	size_t datalen;
60460e15db9SDag-Erling Smørgrav {
60560e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
60660e15db9SDag-Erling Smørgrav 	char *buf = NULL;
60760e15db9SDag-Erling Smørgrav 	size_t buflen;
60860e15db9SDag-Erling Smørgrav 
60960e15db9SDag-Erling Smørgrav 	if (!data)
61060e15db9SDag-Erling Smørgrav 		datalen = 0;
61160e15db9SDag-Erling Smørgrav 	buflen = namelen + 1 + datalen;
61260e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
61360e15db9SDag-Erling Smørgrav 	bcopy(name, buf, namelen);
61460e15db9SDag-Erling Smørgrav 	buf[namelen] = '\0';
61560e15db9SDag-Erling Smørgrav 	bcopy(data, buf + namelen + 1, datalen);
61660e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
61760e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
61860e15db9SDag-Erling Smørgrav 		return;
61960e15db9SDag-Erling Smørgrav 	}
62060e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
62160e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
62260e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
62360e15db9SDag-Erling Smørgrav }
62464cc6a13SJohn Baldwin #endif /* KTRACE */
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes /* Interface and common routines */
627df8bae1dSRodney W. Grimes 
628d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
629df8bae1dSRodney W. Grimes struct ktrace_args {
630df8bae1dSRodney W. Grimes 	char	*fname;
631df8bae1dSRodney W. Grimes 	int	ops;
632df8bae1dSRodney W. Grimes 	int	facs;
633df8bae1dSRodney W. Grimes 	int	pid;
634df8bae1dSRodney W. Grimes };
635d2d3e875SBruce Evans #endif
636df8bae1dSRodney W. Grimes /* ARGSUSED */
63726f9a767SRodney W. Grimes int
638b40ce416SJulian Elischer ktrace(td, uap)
639b40ce416SJulian Elischer 	struct thread *td;
640df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
641df8bae1dSRodney W. Grimes {
642db6a20e2SGarrett Wollman #ifdef KTRACE
643df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
644df8bae1dSRodney W. Grimes 	register struct proc *p;
645df8bae1dSRodney W. Grimes 	struct pgrp *pg;
646df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
647df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
648df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
649400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
65033f19beeSJohn Baldwin 	int flags, error = 0, vfslocked;
651df8bae1dSRodney W. Grimes 	struct nameidata nd;
652a5881ea5SJohn Baldwin 	struct ucred *cred;
653df8bae1dSRodney W. Grimes 
65464cc6a13SJohn Baldwin 	/*
65564cc6a13SJohn Baldwin 	 * Need something to (un)trace.
65664cc6a13SJohn Baldwin 	 */
65764cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
65864cc6a13SJohn Baldwin 		return (EINVAL);
65964cc6a13SJohn Baldwin 
6602c255e9dSRobert Watson 	ktrace_enter(td);
661df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
662df8bae1dSRodney W. Grimes 		/*
663df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
664df8bae1dSRodney W. Grimes 		 */
66533f19beeSJohn Baldwin 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
66633f19beeSJohn Baldwin 		    uap->fname, td);
667e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
6689e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
669797f2d22SPoul-Henning Kamp 		if (error) {
6702c255e9dSRobert Watson 			ktrace_exit(td);
671df8bae1dSRodney W. Grimes 			return (error);
672df8bae1dSRodney W. Grimes 		}
67333f19beeSJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
674762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
675df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
67622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
677df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
678a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
67933f19beeSJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
6802c255e9dSRobert Watson 			ktrace_exit(td);
681df8bae1dSRodney W. Grimes 			return (EACCES);
682df8bae1dSRodney W. Grimes 		}
68333f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
684df8bae1dSRodney W. Grimes 	}
685df8bae1dSRodney W. Grimes 	/*
68679deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
687df8bae1dSRodney W. Grimes 	 */
688df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
68951fd6380SMike Pritchard 		int vrele_count;
69051fd6380SMike Pritchard 
69151fd6380SMike Pritchard 		vrele_count = 0;
6921005a129SJohn Baldwin 		sx_slock(&allproc_lock);
6934f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
694a7ff7443SJohn Baldwin 			PROC_LOCK(p);
695a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
696ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
697ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
698a5881ea5SJohn Baldwin 					cred = p->p_tracecred;
699a5881ea5SJohn Baldwin 					p->p_tracecred = NULL;
700a5881ea5SJohn Baldwin 					p->p_tracevp = NULL;
701df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
702ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
70351fd6380SMike Pritchard 					vrele_count++;
704a5881ea5SJohn Baldwin 					crfree(cred);
70551fd6380SMike Pritchard 				} else
706df8bae1dSRodney W. Grimes 					error = EPERM;
707df8bae1dSRodney W. Grimes 			}
708a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
70979deba82SMatthew Dillon 		}
7101005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
71151fd6380SMike Pritchard 		if (vrele_count > 0) {
71251fd6380SMike Pritchard 			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
71351fd6380SMike Pritchard 			while (vrele_count-- > 0)
71451fd6380SMike Pritchard 				vrele(vp);
71551fd6380SMike Pritchard 			VFS_UNLOCK_GIANT(vfslocked);
71651fd6380SMike Pritchard 		}
717df8bae1dSRodney W. Grimes 		goto done;
718df8bae1dSRodney W. Grimes 	}
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * do it
721df8bae1dSRodney W. Grimes 	 */
72264cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
723df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
724df8bae1dSRodney W. Grimes 		/*
725df8bae1dSRodney W. Grimes 		 * by process group
726df8bae1dSRodney W. Grimes 		 */
727df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
728df8bae1dSRodney W. Grimes 		if (pg == NULL) {
729ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
730df8bae1dSRodney W. Grimes 			error = ESRCH;
731df8bae1dSRodney W. Grimes 			goto done;
732df8bae1dSRodney W. Grimes 		}
733f591779bSSeigo Tanimura 		/*
734f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
735ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
736f591779bSSeigo Tanimura 		 */
737f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
738400a74bfSPawel Jakub Dawidek 		nfound = 0;
739400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
740400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
741400a74bfSPawel Jakub Dawidek 			if (p_cansee(td, p) != 0) {
742400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
743400a74bfSPawel Jakub Dawidek 				continue;
744400a74bfSPawel Jakub Dawidek 			}
745400a74bfSPawel Jakub Dawidek 			PROC_UNLOCK(p);
746400a74bfSPawel Jakub Dawidek 			nfound++;
747df8bae1dSRodney W. Grimes 			if (descend)
748a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
749df8bae1dSRodney W. Grimes 			else
750a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
751400a74bfSPawel Jakub Dawidek 		}
752400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
753400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
754400a74bfSPawel Jakub Dawidek 			error = ESRCH;
755400a74bfSPawel Jakub Dawidek 			goto done;
756400a74bfSPawel Jakub Dawidek 		}
757df8bae1dSRodney W. Grimes 	} else {
758df8bae1dSRodney W. Grimes 		/*
759df8bae1dSRodney W. Grimes 		 * by pid
760df8bae1dSRodney W. Grimes 		 */
761df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
762df8bae1dSRodney W. Grimes 		if (p == NULL) {
76364cc6a13SJohn Baldwin 			sx_sunlock(&proctree_lock);
764df8bae1dSRodney W. Grimes 			error = ESRCH;
765df8bae1dSRodney W. Grimes 			goto done;
766df8bae1dSRodney W. Grimes 		}
7674eb7c9f6SPawel Jakub Dawidek 		error = p_cansee(td, p);
76864cc6a13SJohn Baldwin 		/*
76964cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
77064cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
77164cc6a13SJohn Baldwin 		 */
77233a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
773b0d9aeddSPawel Jakub Dawidek 		if (error) {
774b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
7754eb7c9f6SPawel Jakub Dawidek 			goto done;
776b0d9aeddSPawel Jakub Dawidek 		}
777df8bae1dSRodney W. Grimes 		if (descend)
778a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
779df8bae1dSRodney W. Grimes 		else
780a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
781df8bae1dSRodney W. Grimes 	}
78264cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
783df8bae1dSRodney W. Grimes 	if (!ret)
784df8bae1dSRodney W. Grimes 		error = EPERM;
785df8bae1dSRodney W. Grimes done:
78664cc6a13SJohn Baldwin 	if (vp != NULL) {
78733f19beeSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
788a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
78933f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
79064cc6a13SJohn Baldwin 	}
7912c255e9dSRobert Watson 	ktrace_exit(td);
792df8bae1dSRodney W. Grimes 	return (error);
79364cc6a13SJohn Baldwin #else /* !KTRACE */
79464cc6a13SJohn Baldwin 	return (ENOSYS);
79564cc6a13SJohn Baldwin #endif /* KTRACE */
796df8bae1dSRodney W. Grimes }
797df8bae1dSRodney W. Grimes 
798e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
799e6c4b9baSPoul-Henning Kamp int
800b40ce416SJulian Elischer utrace(td, uap)
801b40ce416SJulian Elischer 	struct thread *td;
802e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
803e6c4b9baSPoul-Henning Kamp {
804b40ce416SJulian Elischer 
805e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
806ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
8077f05b035SAlfred Perlstein 	void *cp;
808c9e7d28eSJohn Baldwin 	int error;
809e6c4b9baSPoul-Henning Kamp 
810c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
811c9e7d28eSJohn Baldwin 		return (0);
812bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
8130bad156aSAlfred Perlstein 		return (EINVAL);
814a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
815c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
81650c22331SPoul-Henning Kamp 	if (error) {
81750c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
818c9e7d28eSJohn Baldwin 		return (error);
81950c22331SPoul-Henning Kamp 	}
820ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
82150c22331SPoul-Henning Kamp 	if (req == NULL) {
82250c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
823b10221ffSJoseph Koshy 		return (ENOMEM);
82450c22331SPoul-Henning Kamp 	}
825d977a583SRobert Watson 	req->ktr_buffer = cp;
826ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
8272c255e9dSRobert Watson 	ktr_submitrequest(td, req);
828e6c4b9baSPoul-Henning Kamp 	return (0);
82964cc6a13SJohn Baldwin #else /* !KTRACE */
830e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
83164cc6a13SJohn Baldwin #endif /* KTRACE */
832e6c4b9baSPoul-Henning Kamp }
833e6c4b9baSPoul-Henning Kamp 
834db6a20e2SGarrett Wollman #ifdef KTRACE
83587b6de2bSPoul-Henning Kamp static int
836a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
837a7ff7443SJohn Baldwin 	struct thread *td;
838a7ff7443SJohn Baldwin 	struct proc *p;
839df8bae1dSRodney W. Grimes 	int ops, facs;
840df8bae1dSRodney W. Grimes 	struct vnode *vp;
841df8bae1dSRodney W. Grimes {
842ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
843a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
844df8bae1dSRodney W. Grimes 
845a7ff7443SJohn Baldwin 	PROC_LOCK(p);
846a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
847a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
848df8bae1dSRodney W. Grimes 		return (0);
849a7ff7443SJohn Baldwin 	}
850ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
851df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
852a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
853df8bae1dSRodney W. Grimes 			/*
854a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
855df8bae1dSRodney W. Grimes 			 */
856a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
857ea3fc8e4SJohn Baldwin 			VREF(vp);
858a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
859a5881ea5SJohn Baldwin 		}
860a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
861a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
862a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
863df8bae1dSRodney W. Grimes 		}
864df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
86532f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
866df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
867df8bae1dSRodney W. Grimes 	} else {
868df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
869df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
870df8bae1dSRodney W. Grimes 			/* no more tracing */
871df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
872a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
873a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
874a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
875a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
876a7ff7443SJohn Baldwin 		}
877a7ff7443SJohn Baldwin 	}
878ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
879a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
88064cc6a13SJohn Baldwin 	if (tracevp != NULL) {
881033eb86eSJeff Roberson 		int vfslocked;
882033eb86eSJeff Roberson 
883033eb86eSJeff Roberson 		vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
884ea3fc8e4SJohn Baldwin 		vrele(tracevp);
885033eb86eSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
88664cc6a13SJohn Baldwin 	}
887a5881ea5SJohn Baldwin 	if (tracecred != NULL)
888a5881ea5SJohn Baldwin 		crfree(tracecred);
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes 	return (1);
891df8bae1dSRodney W. Grimes }
892df8bae1dSRodney W. Grimes 
89387b6de2bSPoul-Henning Kamp static int
894a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
895a7ff7443SJohn Baldwin 	struct thread *td;
896a7ff7443SJohn Baldwin 	struct proc *top;
897df8bae1dSRodney W. Grimes 	int ops, facs;
898df8bae1dSRodney W. Grimes 	struct vnode *vp;
899df8bae1dSRodney W. Grimes {
900df8bae1dSRodney W. Grimes 	register struct proc *p;
901df8bae1dSRodney W. Grimes 	register int ret = 0;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 	p = top;
90464cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
905df8bae1dSRodney W. Grimes 	for (;;) {
906a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
907df8bae1dSRodney W. Grimes 		/*
908df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
909df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
910df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
911df8bae1dSRodney W. Grimes 		 */
9122e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
9132e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
914df8bae1dSRodney W. Grimes 		else for (;;) {
91564cc6a13SJohn Baldwin 			if (p == top)
916df8bae1dSRodney W. Grimes 				return (ret);
9172e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
9182e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
919df8bae1dSRodney W. Grimes 				break;
920df8bae1dSRodney W. Grimes 			}
921b75356e1SJeffrey Hsu 			p = p->p_pptr;
922df8bae1dSRodney W. Grimes 		}
923df8bae1dSRodney W. Grimes 	}
924df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
92787b6de2bSPoul-Henning Kamp static void
9282c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
929df8bae1dSRodney W. Grimes {
930ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
931ea3fc8e4SJohn Baldwin 	struct vnode *vp;
932ea3fc8e4SJohn Baldwin 	struct proc *p;
933ea3fc8e4SJohn Baldwin 	struct ucred *cred;
934df8bae1dSRodney W. Grimes 	struct uio auio;
935ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
936f2a2857bSKirk McKusick 	struct mount *mp;
937ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
93833f19beeSJohn Baldwin 	int error, vfslocked;
939df8bae1dSRodney W. Grimes 
9402c255e9dSRobert Watson 	/*
9412c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
9422c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
9432c255e9dSRobert Watson 	 *
9442c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
9452c255e9dSRobert Watson 	 * the vnode has been closed.
9462c255e9dSRobert Watson 	 */
9472c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
9482c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
9492c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
9502c255e9dSRobert Watson 
951ea3fc8e4SJohn Baldwin 	/*
952ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
9532c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
9542c255e9dSRobert Watson 	 * in sync: we should have both or neither.
955ea3fc8e4SJohn Baldwin 	 */
9562c255e9dSRobert Watson 	if (vp == NULL) {
9572c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
958118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
959df8bae1dSRodney W. Grimes 		return;
9602c255e9dSRobert Watson 	}
961118258f5SBjoern A. Zeeb 	VREF(vp);
9622c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
963118258f5SBjoern A. Zeeb 	crhold(cred);
964118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
9652c255e9dSRobert Watson 
966ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
967a56be37eSJohn Baldwin 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
968a56be37eSJohn Baldwin 	    sizeof(data_lengths) / sizeof(data_lengths[0]),
969a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
9708b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
971ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
972df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
973df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
974df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
975df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
976df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
977df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
978df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
979df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
980ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
981ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
982ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
983ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
984ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
985df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
986ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
987ea3fc8e4SJohn Baldwin 	}
988ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
989d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
990d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
991ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
992ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
993ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
994b92584a6SJohn Baldwin 	}
9952c255e9dSRobert Watson 
99633f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
997f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
998cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
999467a273cSRobert Watson #ifdef MAC
100030d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1001467a273cSRobert Watson 	if (error == 0)
1002467a273cSRobert Watson #endif
1003ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
100422db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
1005f2a2857bSKirk McKusick 	vn_finished_write(mp);
1006118258f5SBjoern A. Zeeb 	crfree(cred);
1007118258f5SBjoern A. Zeeb 	if (!error) {
1008704c9f00SJohn Baldwin 		vrele(vp);
100933f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
1010df8bae1dSRodney W. Grimes 		return;
1011118258f5SBjoern A. Zeeb 	}
1012118258f5SBjoern A. Zeeb 	VFS_UNLOCK_GIANT(vfslocked);
1013118258f5SBjoern A. Zeeb 
1014df8bae1dSRodney W. Grimes 	/*
1015ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1016ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1017ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1018118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1019118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1020118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1021118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1022df8bae1dSRodney W. Grimes 	 */
1023df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1024df8bae1dSRodney W. Grimes 	    error);
1025118258f5SBjoern A. Zeeb 	vrele_count = 1;
1026ea3fc8e4SJohn Baldwin 	/*
1027ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1028ea3fc8e4SJohn Baldwin 	 * system.
1029ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1030ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1031ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1032ea3fc8e4SJohn Baldwin 	 */
1033a5881ea5SJohn Baldwin 	cred = NULL;
10341005a129SJohn Baldwin 	sx_slock(&allproc_lock);
10354f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1036ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1037a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1038ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1039a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
1040df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
1041a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
1042a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
1043ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1044ea3fc8e4SJohn Baldwin 			vrele_count++;
1045df8bae1dSRodney W. Grimes 		}
1046ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1047a5881ea5SJohn Baldwin 		if (cred != NULL) {
1048a5881ea5SJohn Baldwin 			crfree(cred);
1049a5881ea5SJohn Baldwin 			cred = NULL;
1050a5881ea5SJohn Baldwin 		}
1051df8bae1dSRodney W. Grimes 	}
10521005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
10532c255e9dSRobert Watson 
1054ea3fc8e4SJohn Baldwin 	/*
10552c255e9dSRobert Watson 	 * We can't clear any pending requests in threads that have cached
10562c255e9dSRobert Watson 	 * them but not yet committed them, as those are per-thread.  The
10572c255e9dSRobert Watson 	 * thread will have to clear it itself on system call return.
1058ea3fc8e4SJohn Baldwin 	 */
105933f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1060ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1061ea3fc8e4SJohn Baldwin 		vrele(vp);
106233f19beeSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
1063df8bae1dSRodney W. Grimes }
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes /*
1066df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1067df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1068df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1069df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1070df8bae1dSRodney W. Grimes  * so, only root may further change it.
1071df8bae1dSRodney W. Grimes  */
107287b6de2bSPoul-Henning Kamp static int
1073a7ff7443SJohn Baldwin ktrcanset(td, targetp)
1074a7ff7443SJohn Baldwin 	struct thread *td;
1075a7ff7443SJohn Baldwin 	struct proc *targetp;
1076df8bae1dSRodney W. Grimes {
1077df8bae1dSRodney W. Grimes 
1078a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1079a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
108032f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
108175c13541SPoul-Henning Kamp 		return (0);
1082a0f75161SRobert Watson 
1083f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1084a0f75161SRobert Watson 		return (0);
1085a0f75161SRobert Watson 
1086df8bae1dSRodney W. Grimes 	return (1);
1087df8bae1dSRodney W. Grimes }
1088df8bae1dSRodney W. Grimes 
1089db6a20e2SGarrett Wollman #endif /* KTRACE */
1090