xref: /freebsd/sys/kern/kern_ktrace.c (revision b10221ffd96f06223a45a804717ce50f4ff7ed1d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
39db6a20e2SGarrett Wollman #include "opt_ktrace.h"
40467a273cSRobert Watson #include "opt_mac.h"
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43f23b4c91SGarrett Wollman #include <sys/systm.h>
44ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
45ea3fc8e4SJohn Baldwin #include <sys/jail.h>
46ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
47ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
48fb919e4dSMark Murray #include <sys/lock.h>
49fb919e4dSMark Murray #include <sys/mutex.h>
50467a273cSRobert Watson #include <sys/mac.h>
51ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/namei.h>
53ea3fc8e4SJohn Baldwin #include <sys/proc.h>
54ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
56df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
57ea3fc8e4SJohn Baldwin #include <sys/sema.h>
581005a129SJohn Baldwin #include <sys/sx.h>
59ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
60df8bae1dSRodney W. Grimes #include <sys/syslog.h>
61ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
62df8bae1dSRodney W. Grimes 
63a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
6455166637SPoul-Henning Kamp 
65db6a20e2SGarrett Wollman #ifdef KTRACE
66ea3fc8e4SJohn Baldwin 
67ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
68ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
69ea3fc8e4SJohn Baldwin #endif
70ea3fc8e4SJohn Baldwin 
71ea3fc8e4SJohn Baldwin struct ktr_request {
72ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
73ea3fc8e4SJohn Baldwin 	struct	ucred *ktr_cred;
74ea3fc8e4SJohn Baldwin 	struct	vnode *ktr_vp;
75ea3fc8e4SJohn Baldwin 	union {
76ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
77ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
78ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
79ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
80ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
81ea3fc8e4SJohn Baldwin 	} ktr_data;
82ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
83ea3fc8e4SJohn Baldwin };
84ea3fc8e4SJohn Baldwin 
85ea3fc8e4SJohn Baldwin static int data_lengths[] = {
86ea3fc8e4SJohn Baldwin 	0,					/* none */
87ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
88ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
89ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
90ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
91ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
92ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
93ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
94ea3fc8e4SJohn Baldwin };
95ea3fc8e4SJohn Baldwin 
96ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo;
97ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
98ea3fc8e4SJohn Baldwin 
9912301fc3SJohn Baldwin SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
10012301fc3SJohn Baldwin 
1018b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
10212301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
10312301fc3SJohn Baldwin 
1048b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
10512301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
10612301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
10712301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
108ea3fc8e4SJohn Baldwin 
109ea3fc8e4SJohn Baldwin static int print_message = 1;
110ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
111ea3fc8e4SJohn Baldwin static struct sema ktrace_sema;
112ea3fc8e4SJohn Baldwin 
113ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
114ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
1158b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize);
116ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
117ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req);
118ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
119ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy);
120ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req);
121a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
122a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
123a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
12498d93822SBruce Evans 
125ea3fc8e4SJohn Baldwin static void
126ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
127df8bae1dSRodney W. Grimes {
128ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
129ea3fc8e4SJohn Baldwin 	int i;
130df8bae1dSRodney W. Grimes 
131ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
132ea3fc8e4SJohn Baldwin 	sema_init(&ktrace_sema, 0, "ktrace");
133ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_todo);
134ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
135ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
136a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
137ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
138ea3fc8e4SJohn Baldwin 	}
139316ec49aSScott Long 	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
140ea3fc8e4SJohn Baldwin }
141ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
142ea3fc8e4SJohn Baldwin 
143ea3fc8e4SJohn Baldwin static int
144ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
145ea3fc8e4SJohn Baldwin {
146ea3fc8e4SJohn Baldwin 	struct thread *td;
1478b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
148ea3fc8e4SJohn Baldwin 	int error;
149ea3fc8e4SJohn Baldwin 
150ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
151ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
152ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
153ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
154ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
1558b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
156ea3fc8e4SJohn Baldwin 	}
157ea3fc8e4SJohn Baldwin 
1588b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
159ea3fc8e4SJohn Baldwin 	if (error)
160ea3fc8e4SJohn Baldwin 		return (error);
161ea3fc8e4SJohn Baldwin 	td = curthread;
1625e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
163ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
164ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
165ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
166ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1675e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
1688b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
169ea3fc8e4SJohn Baldwin 	if (error)
170ea3fc8e4SJohn Baldwin 		return (error);
171ea3fc8e4SJohn Baldwin 	if (newsize != wantsize)
172ea3fc8e4SJohn Baldwin 		return (ENOSPC);
173ea3fc8e4SJohn Baldwin 	return (0);
174ea3fc8e4SJohn Baldwin }
17512301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
176ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
177ea3fc8e4SJohn Baldwin 
1788b149b51SJohn Baldwin static u_int
1798b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize)
180ea3fc8e4SJohn Baldwin {
181ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
182ea3fc8e4SJohn Baldwin 
183ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
184ea3fc8e4SJohn Baldwin 	print_message = 1;
185ea3fc8e4SJohn Baldwin 	if (newsize == ktr_requestpool)
186ea3fc8e4SJohn Baldwin 		return (newsize);
187ea3fc8e4SJohn Baldwin 	if (newsize < ktr_requestpool)
188ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
189ea3fc8e4SJohn Baldwin 		while (ktr_requestpool > newsize) {
190ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
191ea3fc8e4SJohn Baldwin 			if (req == NULL)
192ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
193ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
194ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
195ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
196ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
197ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
198ea3fc8e4SJohn Baldwin 		}
199ea3fc8e4SJohn Baldwin 	else
200ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
201ea3fc8e4SJohn Baldwin 		while (ktr_requestpool < newsize) {
202ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
203ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
204a163d034SWarner Losh 			    M_WAITOK);
205ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
206ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
207ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
208ea3fc8e4SJohn Baldwin 		}
209ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
210ea3fc8e4SJohn Baldwin }
211ea3fc8e4SJohn Baldwin 
212ea3fc8e4SJohn Baldwin static struct ktr_request *
213ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
214ea3fc8e4SJohn Baldwin {
215ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
216ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
217ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
218ea3fc8e4SJohn Baldwin 	int pm;
219ea3fc8e4SJohn Baldwin 
2205e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
221ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
222ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
223ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
2245e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
225ea3fc8e4SJohn Baldwin 		return (NULL);
226ea3fc8e4SJohn Baldwin 	}
227ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
228ea3fc8e4SJohn Baldwin 	if (req != NULL) {
229ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
230ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
23175768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
23275768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
23375768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
23475768576SJohn Baldwin 		}
235a5881ea5SJohn Baldwin 		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
236a5881ea5SJohn Baldwin 		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
237a5881ea5SJohn Baldwin 		req->ktr_vp = p->p_tracevp;
238a5881ea5SJohn Baldwin 		VREF(p->p_tracevp);
239a5881ea5SJohn Baldwin 		req->ktr_cred = crhold(p->p_tracecred);
240ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
241ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
242ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
243ea3fc8e4SJohn Baldwin 		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
244ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = NULL;
245ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
246ea3fc8e4SJohn Baldwin 	} else {
24775768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
248ea3fc8e4SJohn Baldwin 		pm = print_message;
249ea3fc8e4SJohn Baldwin 		print_message = 0;
250ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
251ea3fc8e4SJohn Baldwin 		if (pm)
252ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
2535e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
254ea3fc8e4SJohn Baldwin 	}
255ea3fc8e4SJohn Baldwin 	return (req);
256ea3fc8e4SJohn Baldwin }
257ea3fc8e4SJohn Baldwin 
258ea3fc8e4SJohn Baldwin static void
259ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req)
260ea3fc8e4SJohn Baldwin {
261ea3fc8e4SJohn Baldwin 
262ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
263ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
264ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
265277576deSJohn Baldwin 	sema_post(&ktrace_sema);
2665e26dcb5SJohn Baldwin 	curthread->td_pflags &= ~TDP_INKTRACE;
267ea3fc8e4SJohn Baldwin }
268ea3fc8e4SJohn Baldwin 
269ea3fc8e4SJohn Baldwin static void
270ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
271ea3fc8e4SJohn Baldwin {
272ea3fc8e4SJohn Baldwin 
273ea3fc8e4SJohn Baldwin 	crfree(req->ktr_cred);
274fbd140c7SJohn Baldwin 	if (req->ktr_vp != NULL) {
275ea3fc8e4SJohn Baldwin 		mtx_lock(&Giant);
276ea3fc8e4SJohn Baldwin 		vrele(req->ktr_vp);
277ea3fc8e4SJohn Baldwin 		mtx_unlock(&Giant);
278fbd140c7SJohn Baldwin 	}
279b92584a6SJohn Baldwin 	if (req->ktr_header.ktr_buffer != NULL)
280b92584a6SJohn Baldwin 		free(req->ktr_header.ktr_buffer, M_KTRACE);
281ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
282ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
283ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
284ea3fc8e4SJohn Baldwin }
285ea3fc8e4SJohn Baldwin 
286ea3fc8e4SJohn Baldwin static void
287ea3fc8e4SJohn Baldwin ktr_loop(void *dummy)
288ea3fc8e4SJohn Baldwin {
289ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
290ea3fc8e4SJohn Baldwin 	struct thread *td;
291ea3fc8e4SJohn Baldwin 	struct ucred *cred;
292ea3fc8e4SJohn Baldwin 
293ea3fc8e4SJohn Baldwin 	/* Only cache these values once. */
294ea3fc8e4SJohn Baldwin 	td = curthread;
295ea3fc8e4SJohn Baldwin 	cred = td->td_ucred;
296ea3fc8e4SJohn Baldwin 	for (;;) {
297ea3fc8e4SJohn Baldwin 		sema_wait(&ktrace_sema);
298ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
299ea3fc8e4SJohn Baldwin 		req = STAILQ_FIRST(&ktr_todo);
300ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
301ea3fc8e4SJohn Baldwin 		KASSERT(req != NULL, ("got a NULL request"));
302ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
303ea3fc8e4SJohn Baldwin 		/*
304ea3fc8e4SJohn Baldwin 		 * It is not enough just to pass the cached cred
305ea3fc8e4SJohn Baldwin 		 * to the VOP's in ktr_writerequest().  Some VFS
306ea3fc8e4SJohn Baldwin 		 * operations use curthread->td_ucred, so we need
307ea3fc8e4SJohn Baldwin 		 * to modify our thread's credentials as well.
308ea3fc8e4SJohn Baldwin 		 * Evil.
309ea3fc8e4SJohn Baldwin 		 */
310ea3fc8e4SJohn Baldwin 		td->td_ucred = req->ktr_cred;
311ea3fc8e4SJohn Baldwin 		ktr_writerequest(req);
312ea3fc8e4SJohn Baldwin 		td->td_ucred = cred;
313ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
314ea3fc8e4SJohn Baldwin 	}
315df8bae1dSRodney W. Grimes }
316df8bae1dSRodney W. Grimes 
317356861dbSMatthew Dillon /*
318356861dbSMatthew Dillon  * MPSAFE
319356861dbSMatthew Dillon  */
32026f9a767SRodney W. Grimes void
321ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
32271ddfdbbSDmitrij Tejblum 	int code, narg;
32371ddfdbbSDmitrij Tejblum 	register_t args[];
324df8bae1dSRodney W. Grimes {
325ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
326df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
327ea3fc8e4SJohn Baldwin 	size_t buflen;
3284b3aac3dSJohn Baldwin 	char *buf = NULL;
329df8bae1dSRodney W. Grimes 
3304b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3314b3aac3dSJohn Baldwin 	if (buflen > 0) {
332a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3334b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3344b3aac3dSJohn Baldwin 	}
335ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
33650c22331SPoul-Henning Kamp 	if (req == NULL) {
33750c22331SPoul-Henning Kamp 		if (buf != NULL)
33850c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
339ea3fc8e4SJohn Baldwin 		return;
34050c22331SPoul-Henning Kamp 	}
341ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
342df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
343df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
344ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
345ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
3464b3aac3dSJohn Baldwin 		req->ktr_header.ktr_buffer = buf;
347ea3fc8e4SJohn Baldwin 	}
348ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
349df8bae1dSRodney W. Grimes }
350df8bae1dSRodney W. Grimes 
351356861dbSMatthew Dillon /*
352356861dbSMatthew Dillon  * MPSAFE
353356861dbSMatthew Dillon  */
35426f9a767SRodney W. Grimes void
355ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
35671ddfdbbSDmitrij Tejblum 	int code, error;
35771ddfdbbSDmitrij Tejblum 	register_t retval;
358df8bae1dSRodney W. Grimes {
359ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
360ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
361df8bae1dSRodney W. Grimes 
362ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
363ea3fc8e4SJohn Baldwin 	if (req == NULL)
364ea3fc8e4SJohn Baldwin 		return;
365ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
366ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
367ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
368ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
369ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
370df8bae1dSRodney W. Grimes }
371df8bae1dSRodney W. Grimes 
37226f9a767SRodney W. Grimes void
373ea3fc8e4SJohn Baldwin ktrnamei(path)
374df8bae1dSRodney W. Grimes 	char *path;
375df8bae1dSRodney W. Grimes {
376ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
377ea3fc8e4SJohn Baldwin 	int namelen;
3784b3aac3dSJohn Baldwin 	char *buf = NULL;
379df8bae1dSRodney W. Grimes 
3804b3aac3dSJohn Baldwin 	namelen = strlen(path);
3814b3aac3dSJohn Baldwin 	if (namelen > 0) {
382a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
3834b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
3844b3aac3dSJohn Baldwin 	}
385ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
38650c22331SPoul-Henning Kamp 	if (req == NULL) {
38750c22331SPoul-Henning Kamp 		if (buf != NULL)
38850c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
389ea3fc8e4SJohn Baldwin 		return;
39050c22331SPoul-Henning Kamp 	}
391ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
392ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
3934b3aac3dSJohn Baldwin 		req->ktr_header.ktr_buffer = buf;
394ea3fc8e4SJohn Baldwin 	}
395ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398ea3fc8e4SJohn Baldwin /*
399ea3fc8e4SJohn Baldwin  * Since the uio may not stay valid, we can not hand off this request to
400ea3fc8e4SJohn Baldwin  * the thread and need to process it synchronously.  However, we wish to
401ea3fc8e4SJohn Baldwin  * keep the relative order of records in a trace file correct, so we
402ea3fc8e4SJohn Baldwin  * do put this request on the queue (if it isn't empty) and then block.
403ea3fc8e4SJohn Baldwin  * The ktrace thread waks us back up when it is time for this event to
404ea3fc8e4SJohn Baldwin  * be posted and blocks until we have completed writing out the event
405ea3fc8e4SJohn Baldwin  * and woken it back up.
406ea3fc8e4SJohn Baldwin  */
40726f9a767SRodney W. Grimes void
408ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
409df8bae1dSRodney W. Grimes 	int fd;
410df8bae1dSRodney W. Grimes 	enum uio_rw rw;
41142ebfbf2SBrian Feldman 	struct uio *uio;
41242ebfbf2SBrian Feldman 	int error;
413df8bae1dSRodney W. Grimes {
414ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
415ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
416b92584a6SJohn Baldwin 	int datalen;
417b92584a6SJohn Baldwin 	char *buf;
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	if (error)
420df8bae1dSRodney W. Grimes 		return;
421b92584a6SJohn Baldwin 	uio->uio_offset = 0;
422b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
423b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
424a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
425b92584a6SJohn Baldwin 	if (uiomove(buf, datalen, uio)) {
426b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
427ea3fc8e4SJohn Baldwin 		return;
428b92584a6SJohn Baldwin 	}
429b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
430b92584a6SJohn Baldwin 	if (req == NULL) {
431b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
432b92584a6SJohn Baldwin 		return;
433b92584a6SJohn Baldwin 	}
434ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
435ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
436ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
437b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
438b92584a6SJohn Baldwin 	req->ktr_header.ktr_buffer = buf;
439ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
440df8bae1dSRodney W. Grimes }
441df8bae1dSRodney W. Grimes 
44226f9a767SRodney W. Grimes void
443ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
444a93fdaacSMarcel Moolenaar 	int sig;
445df8bae1dSRodney W. Grimes 	sig_t action;
4462c42a146SMarcel Moolenaar 	sigset_t *mask;
447a93fdaacSMarcel Moolenaar 	int code;
448df8bae1dSRodney W. Grimes {
449ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
450ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
451df8bae1dSRodney W. Grimes 
452ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
453ea3fc8e4SJohn Baldwin 	if (req == NULL)
454ea3fc8e4SJohn Baldwin 		return;
455ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
456ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
457ea3fc8e4SJohn Baldwin 	kp->action = action;
458ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
459ea3fc8e4SJohn Baldwin 	kp->code = code;
460ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
461df8bae1dSRodney W. Grimes }
462df8bae1dSRodney W. Grimes 
46326f9a767SRodney W. Grimes void
464ea3fc8e4SJohn Baldwin ktrcsw(out, user)
465df8bae1dSRodney W. Grimes 	int out, user;
466df8bae1dSRodney W. Grimes {
467ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
468ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
469df8bae1dSRodney W. Grimes 
470ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
471ea3fc8e4SJohn Baldwin 	if (req == NULL)
472ea3fc8e4SJohn Baldwin 		return;
473ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
474ea3fc8e4SJohn Baldwin 	kc->out = out;
475ea3fc8e4SJohn Baldwin 	kc->user = user;
476ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
477df8bae1dSRodney W. Grimes }
47864cc6a13SJohn Baldwin #endif /* KTRACE */
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes /* Interface and common routines */
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes /*
483df8bae1dSRodney W. Grimes  * ktrace system call
48464cc6a13SJohn Baldwin  *
48564cc6a13SJohn Baldwin  * MPSAFE
486df8bae1dSRodney W. Grimes  */
487d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
488df8bae1dSRodney W. Grimes struct ktrace_args {
489df8bae1dSRodney W. Grimes 	char	*fname;
490df8bae1dSRodney W. Grimes 	int	ops;
491df8bae1dSRodney W. Grimes 	int	facs;
492df8bae1dSRodney W. Grimes 	int	pid;
493df8bae1dSRodney W. Grimes };
494d2d3e875SBruce Evans #endif
495df8bae1dSRodney W. Grimes /* ARGSUSED */
49626f9a767SRodney W. Grimes int
497b40ce416SJulian Elischer ktrace(td, uap)
498b40ce416SJulian Elischer 	struct thread *td;
499df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
500df8bae1dSRodney W. Grimes {
501db6a20e2SGarrett Wollman #ifdef KTRACE
502df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
503df8bae1dSRodney W. Grimes 	register struct proc *p;
504df8bae1dSRodney W. Grimes 	struct pgrp *pg;
505df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
506df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
507df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
508df8bae1dSRodney W. Grimes 	int ret = 0;
509e6796b67SKirk McKusick 	int flags, error = 0;
510df8bae1dSRodney W. Grimes 	struct nameidata nd;
511a5881ea5SJohn Baldwin 	struct ucred *cred;
512df8bae1dSRodney W. Grimes 
51364cc6a13SJohn Baldwin 	/*
51464cc6a13SJohn Baldwin 	 * Need something to (un)trace.
51564cc6a13SJohn Baldwin 	 */
51664cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
51764cc6a13SJohn Baldwin 		return (EINVAL);
51864cc6a13SJohn Baldwin 
5195e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
520df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
521df8bae1dSRodney W. Grimes 		/*
522df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
523df8bae1dSRodney W. Grimes 		 */
524b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
525e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
52664cc6a13SJohn Baldwin 		mtx_lock(&Giant);
5277c89f162SPoul-Henning Kamp 		error = vn_open(&nd, &flags, 0, -1);
528797f2d22SPoul-Henning Kamp 		if (error) {
52964cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5305e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
531df8bae1dSRodney W. Grimes 			return (error);
532df8bae1dSRodney W. Grimes 		}
533762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
534df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
535b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
536df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
537a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
53864cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5395e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
540df8bae1dSRodney W. Grimes 			return (EACCES);
541df8bae1dSRodney W. Grimes 		}
54264cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
543df8bae1dSRodney W. Grimes 	}
544df8bae1dSRodney W. Grimes 	/*
54579deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
546df8bae1dSRodney W. Grimes 	 */
547df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
5481005a129SJohn Baldwin 		sx_slock(&allproc_lock);
5492e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
550a7ff7443SJohn Baldwin 			PROC_LOCK(p);
551a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
552ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
553ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
554a5881ea5SJohn Baldwin 					cred = p->p_tracecred;
555a5881ea5SJohn Baldwin 					p->p_tracecred = NULL;
556a5881ea5SJohn Baldwin 					p->p_tracevp = NULL;
557df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
558ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
559a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
56064cc6a13SJohn Baldwin 					mtx_lock(&Giant);
561df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
562a5881ea5SJohn Baldwin 						cred, td);
56364cc6a13SJohn Baldwin 					mtx_unlock(&Giant);
564a5881ea5SJohn Baldwin 					crfree(cred);
56579deba82SMatthew Dillon 				} else {
566a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
567df8bae1dSRodney W. Grimes 					error = EPERM;
568df8bae1dSRodney W. Grimes 				}
569a7ff7443SJohn Baldwin 			} else
570a7ff7443SJohn Baldwin 				PROC_UNLOCK(p);
57179deba82SMatthew Dillon 		}
5721005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
573df8bae1dSRodney W. Grimes 		goto done;
574df8bae1dSRodney W. Grimes 	}
575df8bae1dSRodney W. Grimes 	/*
576df8bae1dSRodney W. Grimes 	 * do it
577df8bae1dSRodney W. Grimes 	 */
57864cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
579df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
580df8bae1dSRodney W. Grimes 		/*
581df8bae1dSRodney W. Grimes 		 * by process group
582df8bae1dSRodney W. Grimes 		 */
583df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
584df8bae1dSRodney W. Grimes 		if (pg == NULL) {
585ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
586df8bae1dSRodney W. Grimes 			error = ESRCH;
587df8bae1dSRodney W. Grimes 			goto done;
588df8bae1dSRodney W. Grimes 		}
589f591779bSSeigo Tanimura 		/*
590f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
591ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
592f591779bSSeigo Tanimura 		 */
593f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
5942e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
595df8bae1dSRodney W. Grimes 			if (descend)
596a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
597df8bae1dSRodney W. Grimes 			else
598a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
599df8bae1dSRodney W. Grimes 	} else {
600df8bae1dSRodney W. Grimes 		/*
601df8bae1dSRodney W. Grimes 		 * by pid
602df8bae1dSRodney W. Grimes 		 */
603df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
604df8bae1dSRodney W. Grimes 		if (p == NULL) {
60564cc6a13SJohn Baldwin 			sx_sunlock(&proctree_lock);
606df8bae1dSRodney W. Grimes 			error = ESRCH;
607df8bae1dSRodney W. Grimes 			goto done;
608df8bae1dSRodney W. Grimes 		}
60964cc6a13SJohn Baldwin 		/*
61064cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
61164cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
61264cc6a13SJohn Baldwin 		 */
61333a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
614df8bae1dSRodney W. Grimes 		if (descend)
615a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
616df8bae1dSRodney W. Grimes 		else
617a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
618df8bae1dSRodney W. Grimes 	}
61964cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
620df8bae1dSRodney W. Grimes 	if (!ret)
621df8bae1dSRodney W. Grimes 		error = EPERM;
622df8bae1dSRodney W. Grimes done:
62364cc6a13SJohn Baldwin 	if (vp != NULL) {
62464cc6a13SJohn Baldwin 		mtx_lock(&Giant);
625a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
62664cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
62764cc6a13SJohn Baldwin 	}
6285e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
629df8bae1dSRodney W. Grimes 	return (error);
63064cc6a13SJohn Baldwin #else /* !KTRACE */
63164cc6a13SJohn Baldwin 	return (ENOSYS);
63264cc6a13SJohn Baldwin #endif /* KTRACE */
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
635e6c4b9baSPoul-Henning Kamp /*
636e6c4b9baSPoul-Henning Kamp  * utrace system call
63764cc6a13SJohn Baldwin  *
63864cc6a13SJohn Baldwin  * MPSAFE
639e6c4b9baSPoul-Henning Kamp  */
640e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
641e6c4b9baSPoul-Henning Kamp int
642b40ce416SJulian Elischer utrace(td, uap)
643b40ce416SJulian Elischer 	struct thread *td;
644e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
645e6c4b9baSPoul-Henning Kamp {
646b40ce416SJulian Elischer 
647e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
648ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
6497f05b035SAlfred Perlstein 	void *cp;
650c9e7d28eSJohn Baldwin 	int error;
651e6c4b9baSPoul-Henning Kamp 
652c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
653c9e7d28eSJohn Baldwin 		return (0);
654bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6550bad156aSAlfred Perlstein 		return (EINVAL);
656a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
657c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
65850c22331SPoul-Henning Kamp 	if (error) {
65950c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
660c9e7d28eSJohn Baldwin 		return (error);
66150c22331SPoul-Henning Kamp 	}
662ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
66350c22331SPoul-Henning Kamp 	if (req == NULL) {
66450c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
665b10221ffSJoseph Koshy 		return (ENOMEM);
66650c22331SPoul-Henning Kamp 	}
667ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_buffer = cp;
668ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
669ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
670e6c4b9baSPoul-Henning Kamp 	return (0);
67164cc6a13SJohn Baldwin #else /* !KTRACE */
672e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
67364cc6a13SJohn Baldwin #endif /* KTRACE */
674e6c4b9baSPoul-Henning Kamp }
675e6c4b9baSPoul-Henning Kamp 
676db6a20e2SGarrett Wollman #ifdef KTRACE
67787b6de2bSPoul-Henning Kamp static int
678a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
679a7ff7443SJohn Baldwin 	struct thread *td;
680a7ff7443SJohn Baldwin 	struct proc *p;
681df8bae1dSRodney W. Grimes 	int ops, facs;
682df8bae1dSRodney W. Grimes 	struct vnode *vp;
683df8bae1dSRodney W. Grimes {
684ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
685a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
686df8bae1dSRodney W. Grimes 
687a7ff7443SJohn Baldwin 	PROC_LOCK(p);
688a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
689a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
690df8bae1dSRodney W. Grimes 		return (0);
691a7ff7443SJohn Baldwin 	}
692ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
693df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
694a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
695df8bae1dSRodney W. Grimes 			/*
696a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
697df8bae1dSRodney W. Grimes 			 */
698a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
699ea3fc8e4SJohn Baldwin 			VREF(vp);
700a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
701a5881ea5SJohn Baldwin 		}
702a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
703a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
704a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
705df8bae1dSRodney W. Grimes 		}
706df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
707a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
708df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
709df8bae1dSRodney W. Grimes 	} else {
710df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
711df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
712df8bae1dSRodney W. Grimes 			/* no more tracing */
713df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
714a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
715a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
716a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
717a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
718a7ff7443SJohn Baldwin 		}
719a7ff7443SJohn Baldwin 	}
720ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
721a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
72264cc6a13SJohn Baldwin 	if (tracevp != NULL) {
72364cc6a13SJohn Baldwin 		mtx_lock(&Giant);
724ea3fc8e4SJohn Baldwin 		vrele(tracevp);
72564cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
72664cc6a13SJohn Baldwin 	}
727a5881ea5SJohn Baldwin 	if (tracecred != NULL)
728a5881ea5SJohn Baldwin 		crfree(tracecred);
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	return (1);
731df8bae1dSRodney W. Grimes }
732df8bae1dSRodney W. Grimes 
73387b6de2bSPoul-Henning Kamp static int
734a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
735a7ff7443SJohn Baldwin 	struct thread *td;
736a7ff7443SJohn Baldwin 	struct proc *top;
737df8bae1dSRodney W. Grimes 	int ops, facs;
738df8bae1dSRodney W. Grimes 	struct vnode *vp;
739df8bae1dSRodney W. Grimes {
740df8bae1dSRodney W. Grimes 	register struct proc *p;
741df8bae1dSRodney W. Grimes 	register int ret = 0;
742df8bae1dSRodney W. Grimes 
743df8bae1dSRodney W. Grimes 	p = top;
74464cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
745df8bae1dSRodney W. Grimes 	for (;;) {
746a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
747df8bae1dSRodney W. Grimes 		/*
748df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
749df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
750df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
751df8bae1dSRodney W. Grimes 		 */
7522e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
7532e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
754df8bae1dSRodney W. Grimes 		else for (;;) {
75564cc6a13SJohn Baldwin 			if (p == top)
756df8bae1dSRodney W. Grimes 				return (ret);
7572e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7582e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
759df8bae1dSRodney W. Grimes 				break;
760df8bae1dSRodney W. Grimes 			}
761b75356e1SJeffrey Hsu 			p = p->p_pptr;
762df8bae1dSRodney W. Grimes 		}
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
765df8bae1dSRodney W. Grimes }
766df8bae1dSRodney W. Grimes 
76787b6de2bSPoul-Henning Kamp static void
768ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
769df8bae1dSRodney W. Grimes {
770ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
771ea3fc8e4SJohn Baldwin 	struct vnode *vp;
772ea3fc8e4SJohn Baldwin 	struct proc *p;
773ea3fc8e4SJohn Baldwin 	struct thread *td;
774ea3fc8e4SJohn Baldwin 	struct ucred *cred;
775df8bae1dSRodney W. Grimes 	struct uio auio;
776ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
777f2a2857bSKirk McKusick 	struct mount *mp;
778ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
779df8bae1dSRodney W. Grimes 	int error;
780df8bae1dSRodney W. Grimes 
781ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
782ea3fc8e4SJohn Baldwin 	/*
783ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
784ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
785ea3fc8e4SJohn Baldwin 	 */
786df8bae1dSRodney W. Grimes 	if (vp == NULL)
787df8bae1dSRodney W. Grimes 		return;
788ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
7898b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
790ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
791ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
792ea3fc8e4SJohn Baldwin 	td = curthread;
793df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
794df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
795df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
796df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
797df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
798df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
799df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
800df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
801ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
802ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
803ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
804ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
805ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
806df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
807ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
808ea3fc8e4SJohn Baldwin 	}
809ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
810ea3fc8e4SJohn Baldwin 		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
811ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
812ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
813ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
814ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
815b92584a6SJohn Baldwin 	}
816ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
817f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
818b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
819ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
820467a273cSRobert Watson #ifdef MAC
821177142e4SRobert Watson 	error = mac_check_vnode_write(cred, NOCRED, vp);
822467a273cSRobert Watson 	if (error == 0)
823467a273cSRobert Watson #endif
824ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
825b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
826f2a2857bSKirk McKusick 	vn_finished_write(mp);
827ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
828df8bae1dSRodney W. Grimes 	if (!error)
829df8bae1dSRodney W. Grimes 		return;
830df8bae1dSRodney W. Grimes 	/*
831ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
832ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
833ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
834df8bae1dSRodney W. Grimes 	 */
835df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
836df8bae1dSRodney W. Grimes 	    error);
837ea3fc8e4SJohn Baldwin 	vrele_count = 0;
838ea3fc8e4SJohn Baldwin 	/*
839ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
840ea3fc8e4SJohn Baldwin 	 * system.
841ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
842ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
843ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
844ea3fc8e4SJohn Baldwin 	 */
845a5881ea5SJohn Baldwin 	cred = NULL;
8461005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8472e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
848ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
849a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
850ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
851a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
852df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
853a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
854a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
855ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
856ea3fc8e4SJohn Baldwin 			vrele_count++;
857df8bae1dSRodney W. Grimes 		}
858ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
859a5881ea5SJohn Baldwin 		if (cred != NULL) {
860a5881ea5SJohn Baldwin 			crfree(cred);
861a5881ea5SJohn Baldwin 			cred = NULL;
862a5881ea5SJohn Baldwin 		}
863df8bae1dSRodney W. Grimes 	}
8641005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
865ea3fc8e4SJohn Baldwin 	/*
866ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
867ea3fc8e4SJohn Baldwin 	 */
868ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
869ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
870ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
871ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
872ea3fc8e4SJohn Baldwin 			vrele_count++;
873ea3fc8e4SJohn Baldwin 		}
874ea3fc8e4SJohn Baldwin 	}
875ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
876ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
877ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
878ea3fc8e4SJohn Baldwin 		vrele(vp);
879ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
880df8bae1dSRodney W. Grimes }
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes /*
883df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
884df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
885df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
886df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
887df8bae1dSRodney W. Grimes  * so, only root may further change it.
888df8bae1dSRodney W. Grimes  */
88987b6de2bSPoul-Henning Kamp static int
890a7ff7443SJohn Baldwin ktrcanset(td, targetp)
891a7ff7443SJohn Baldwin 	struct thread *td;
892a7ff7443SJohn Baldwin 	struct proc *targetp;
893df8bae1dSRodney W. Grimes {
894df8bae1dSRodney W. Grimes 
895a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
896a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
897a7ff7443SJohn Baldwin 	    suser_cred(td->td_ucred, PRISON_ROOT))
89875c13541SPoul-Henning Kamp 		return (0);
899a0f75161SRobert Watson 
900f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
901a0f75161SRobert Watson 		return (0);
902a0f75161SRobert Watson 
903df8bae1dSRodney W. Grimes 	return (1);
904df8bae1dSRodney W. Grimes }
905df8bae1dSRodney W. Grimes 
906db6a20e2SGarrett Wollman #endif /* KTRACE */
907