xref: /freebsd/sys/kern/kern_ktrace.c (revision 552afd9c120e6b11dbb1acf319bd71a4e9f2b913)
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35db6a20e2SGarrett Wollman #include "opt_ktrace.h"
36467a273cSRobert Watson #include "opt_mac.h"
37df8bae1dSRodney W. Grimes 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39f23b4c91SGarrett Wollman #include <sys/systm.h>
40ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
41ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
42ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44fb919e4dSMark Murray #include <sys/mutex.h>
45467a273cSRobert Watson #include <sys/mac.h>
46ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48ea3fc8e4SJohn Baldwin #include <sys/proc.h>
49ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
521005a129SJohn Baldwin #include <sys/sx.h>
53ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
54df8bae1dSRodney W. Grimes #include <sys/syslog.h>
55ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
56df8bae1dSRodney W. Grimes 
57a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
5855166637SPoul-Henning Kamp 
59db6a20e2SGarrett Wollman #ifdef KTRACE
60ea3fc8e4SJohn Baldwin 
61ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
62ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
63ea3fc8e4SJohn Baldwin #endif
64ea3fc8e4SJohn Baldwin 
65ea3fc8e4SJohn Baldwin struct ktr_request {
66ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
67ea3fc8e4SJohn Baldwin 	struct	ucred *ktr_cred;
68ea3fc8e4SJohn Baldwin 	struct	vnode *ktr_vp;
69ea3fc8e4SJohn Baldwin 	union {
70ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
71ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
72ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
73ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
74ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
75ea3fc8e4SJohn Baldwin 	} ktr_data;
76ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
77ea3fc8e4SJohn Baldwin };
78ea3fc8e4SJohn Baldwin 
79ea3fc8e4SJohn Baldwin static int data_lengths[] = {
80ea3fc8e4SJohn Baldwin 	0,					/* none */
81ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
82ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
83ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
84ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
85ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
86ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
87ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
88ea3fc8e4SJohn Baldwin };
89ea3fc8e4SJohn Baldwin 
90ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo;
91ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
92ea3fc8e4SJohn Baldwin 
9312301fc3SJohn Baldwin SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
9412301fc3SJohn Baldwin 
958b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
9612301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
9712301fc3SJohn Baldwin 
988b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
9912301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
10012301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
10112301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
102ea3fc8e4SJohn Baldwin 
103ea3fc8e4SJohn Baldwin static int print_message = 1;
104ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
105f4114c3dSJohn Baldwin static struct cv ktrace_cv;
106ea3fc8e4SJohn Baldwin 
107ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
108ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
1098b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize);
110ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
111ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req);
112ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
113ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy);
114ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req);
115a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
116a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
117a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
11898d93822SBruce Evans 
119ea3fc8e4SJohn Baldwin static void
120ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
121df8bae1dSRodney W. Grimes {
122ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
123ea3fc8e4SJohn Baldwin 	int i;
124df8bae1dSRodney W. Grimes 
125ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
126f4114c3dSJohn Baldwin 	cv_init(&ktrace_cv, "ktrace");
127ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_todo);
128ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
129ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
130a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
131ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
132ea3fc8e4SJohn Baldwin 	}
133316ec49aSScott Long 	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
134ea3fc8e4SJohn Baldwin }
135ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
136ea3fc8e4SJohn Baldwin 
137ea3fc8e4SJohn Baldwin static int
138ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
139ea3fc8e4SJohn Baldwin {
140ea3fc8e4SJohn Baldwin 	struct thread *td;
1418b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
142ea3fc8e4SJohn Baldwin 	int error;
143ea3fc8e4SJohn Baldwin 
144ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
145ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
146ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
147ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
148ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
1498b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
150ea3fc8e4SJohn Baldwin 	}
151ea3fc8e4SJohn Baldwin 
1528b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
153ea3fc8e4SJohn Baldwin 	if (error)
154ea3fc8e4SJohn Baldwin 		return (error);
155ea3fc8e4SJohn Baldwin 	td = curthread;
1565e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
157ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
158ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
159ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
160ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1615e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
1628b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
163ea3fc8e4SJohn Baldwin 	if (error)
164ea3fc8e4SJohn Baldwin 		return (error);
165a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
166ea3fc8e4SJohn Baldwin 		return (ENOSPC);
167ea3fc8e4SJohn Baldwin 	return (0);
168ea3fc8e4SJohn Baldwin }
16912301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
170ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
171ea3fc8e4SJohn Baldwin 
1728b149b51SJohn Baldwin static u_int
1738b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize)
174ea3fc8e4SJohn Baldwin {
175ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
176a5896914SJoseph Koshy 	int bound;
177ea3fc8e4SJohn Baldwin 
178ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
179ea3fc8e4SJohn Baldwin 	print_message = 1;
180a5896914SJoseph Koshy 	bound = newsize - ktr_requestpool;
181a5896914SJoseph Koshy 	if (bound == 0)
182a5896914SJoseph Koshy 		return (ktr_requestpool);
183a5896914SJoseph Koshy 	if (bound < 0)
184ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
185a5896914SJoseph Koshy 		while (bound++ < 0) {
186ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
187ea3fc8e4SJohn Baldwin 			if (req == NULL)
188ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
189ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
190ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
191ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
192ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
193ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
194ea3fc8e4SJohn Baldwin 		}
195ea3fc8e4SJohn Baldwin 	else
196ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
197a5896914SJoseph Koshy 		while (bound-- > 0) {
198ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
199ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
200a163d034SWarner Losh 			    M_WAITOK);
201ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
202ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
203ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
204ea3fc8e4SJohn Baldwin 		}
205ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
206ea3fc8e4SJohn Baldwin }
207ea3fc8e4SJohn Baldwin 
208ea3fc8e4SJohn Baldwin static struct ktr_request *
209ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
210ea3fc8e4SJohn Baldwin {
211ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
212ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
213ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
214ea3fc8e4SJohn Baldwin 	int pm;
215ea3fc8e4SJohn Baldwin 
2165e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
217ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
218ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
219ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
2205e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
221ea3fc8e4SJohn Baldwin 		return (NULL);
222ea3fc8e4SJohn Baldwin 	}
223ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
224ea3fc8e4SJohn Baldwin 	if (req != NULL) {
225ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
226ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
22775768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
22875768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
22975768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
23075768576SJohn Baldwin 		}
231a5881ea5SJohn Baldwin 		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
232a5881ea5SJohn Baldwin 		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
233a5881ea5SJohn Baldwin 		req->ktr_vp = p->p_tracevp;
234a5881ea5SJohn Baldwin 		VREF(p->p_tracevp);
235a5881ea5SJohn Baldwin 		req->ktr_cred = crhold(p->p_tracecred);
236ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
237ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
238ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
239ea3fc8e4SJohn Baldwin 		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
240ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = NULL;
241ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
242ea3fc8e4SJohn Baldwin 	} else {
24375768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
244ea3fc8e4SJohn Baldwin 		pm = print_message;
245ea3fc8e4SJohn Baldwin 		print_message = 0;
246ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
247ea3fc8e4SJohn Baldwin 		if (pm)
248ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
2495e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
250ea3fc8e4SJohn Baldwin 	}
251ea3fc8e4SJohn Baldwin 	return (req);
252ea3fc8e4SJohn Baldwin }
253ea3fc8e4SJohn Baldwin 
254ea3fc8e4SJohn Baldwin static void
255ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req)
256ea3fc8e4SJohn Baldwin {
257ea3fc8e4SJohn Baldwin 
258ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
259ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
260f4114c3dSJohn Baldwin 	cv_signal(&ktrace_cv);
261ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
2625e26dcb5SJohn Baldwin 	curthread->td_pflags &= ~TDP_INKTRACE;
263ea3fc8e4SJohn Baldwin }
264ea3fc8e4SJohn Baldwin 
265ea3fc8e4SJohn Baldwin static void
266ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
267ea3fc8e4SJohn Baldwin {
268ea3fc8e4SJohn Baldwin 
269ea3fc8e4SJohn Baldwin 	crfree(req->ktr_cred);
270fbd140c7SJohn Baldwin 	if (req->ktr_vp != NULL) {
271ea3fc8e4SJohn Baldwin 		mtx_lock(&Giant);
272ea3fc8e4SJohn Baldwin 		vrele(req->ktr_vp);
273ea3fc8e4SJohn Baldwin 		mtx_unlock(&Giant);
274fbd140c7SJohn Baldwin 	}
275b92584a6SJohn Baldwin 	if (req->ktr_header.ktr_buffer != NULL)
276b92584a6SJohn Baldwin 		free(req->ktr_header.ktr_buffer, M_KTRACE);
277ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
278ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
279ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
280ea3fc8e4SJohn Baldwin }
281ea3fc8e4SJohn Baldwin 
282ea3fc8e4SJohn Baldwin static void
283ea3fc8e4SJohn Baldwin ktr_loop(void *dummy)
284ea3fc8e4SJohn Baldwin {
285ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
286ea3fc8e4SJohn Baldwin 	struct thread *td;
287ea3fc8e4SJohn Baldwin 	struct ucred *cred;
288ea3fc8e4SJohn Baldwin 
289ea3fc8e4SJohn Baldwin 	/* Only cache these values once. */
290ea3fc8e4SJohn Baldwin 	td = curthread;
291ea3fc8e4SJohn Baldwin 	cred = td->td_ucred;
292ea3fc8e4SJohn Baldwin 	for (;;) {
293ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
294f4114c3dSJohn Baldwin 		while (STAILQ_EMPTY(&ktr_todo))
295f4114c3dSJohn Baldwin 			cv_wait(&ktrace_cv, &ktrace_mtx);
296ea3fc8e4SJohn Baldwin 		req = STAILQ_FIRST(&ktr_todo);
297ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
298ea3fc8e4SJohn Baldwin 		KASSERT(req != NULL, ("got a NULL request"));
299ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
300ea3fc8e4SJohn Baldwin 		/*
301ea3fc8e4SJohn Baldwin 		 * It is not enough just to pass the cached cred
302ea3fc8e4SJohn Baldwin 		 * to the VOP's in ktr_writerequest().  Some VFS
303ea3fc8e4SJohn Baldwin 		 * operations use curthread->td_ucred, so we need
304ea3fc8e4SJohn Baldwin 		 * to modify our thread's credentials as well.
305ea3fc8e4SJohn Baldwin 		 * Evil.
306ea3fc8e4SJohn Baldwin 		 */
307ea3fc8e4SJohn Baldwin 		td->td_ucred = req->ktr_cred;
308ea3fc8e4SJohn Baldwin 		ktr_writerequest(req);
309ea3fc8e4SJohn Baldwin 		td->td_ucred = cred;
310ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
311ea3fc8e4SJohn Baldwin 	}
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314356861dbSMatthew Dillon /*
315356861dbSMatthew Dillon  * MPSAFE
316356861dbSMatthew Dillon  */
31726f9a767SRodney W. Grimes void
318ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
31971ddfdbbSDmitrij Tejblum 	int code, narg;
32071ddfdbbSDmitrij Tejblum 	register_t args[];
321df8bae1dSRodney W. Grimes {
322ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
323df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
324ea3fc8e4SJohn Baldwin 	size_t buflen;
3254b3aac3dSJohn Baldwin 	char *buf = NULL;
326df8bae1dSRodney W. Grimes 
3274b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3284b3aac3dSJohn Baldwin 	if (buflen > 0) {
329a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3304b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3314b3aac3dSJohn Baldwin 	}
332ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
33350c22331SPoul-Henning Kamp 	if (req == NULL) {
33450c22331SPoul-Henning Kamp 		if (buf != NULL)
33550c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
336ea3fc8e4SJohn Baldwin 		return;
33750c22331SPoul-Henning Kamp 	}
338ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
339df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
340df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
341ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
342ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
3434b3aac3dSJohn Baldwin 		req->ktr_header.ktr_buffer = buf;
344ea3fc8e4SJohn Baldwin 	}
345ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
346df8bae1dSRodney W. Grimes }
347df8bae1dSRodney W. Grimes 
348356861dbSMatthew Dillon /*
349356861dbSMatthew Dillon  * MPSAFE
350356861dbSMatthew Dillon  */
35126f9a767SRodney W. Grimes void
352ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
35371ddfdbbSDmitrij Tejblum 	int code, error;
35471ddfdbbSDmitrij Tejblum 	register_t retval;
355df8bae1dSRodney W. Grimes {
356ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
357ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
358df8bae1dSRodney W. Grimes 
359ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
360ea3fc8e4SJohn Baldwin 	if (req == NULL)
361ea3fc8e4SJohn Baldwin 		return;
362ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
363ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
364ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
365ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
366ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
367df8bae1dSRodney W. Grimes }
368df8bae1dSRodney W. Grimes 
36926f9a767SRodney W. Grimes void
370ea3fc8e4SJohn Baldwin ktrnamei(path)
371df8bae1dSRodney W. Grimes 	char *path;
372df8bae1dSRodney W. Grimes {
373ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
374ea3fc8e4SJohn Baldwin 	int namelen;
3754b3aac3dSJohn Baldwin 	char *buf = NULL;
376df8bae1dSRodney W. Grimes 
3774b3aac3dSJohn Baldwin 	namelen = strlen(path);
3784b3aac3dSJohn Baldwin 	if (namelen > 0) {
379a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
3804b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
3814b3aac3dSJohn Baldwin 	}
382ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
38350c22331SPoul-Henning Kamp 	if (req == NULL) {
38450c22331SPoul-Henning Kamp 		if (buf != NULL)
38550c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
386ea3fc8e4SJohn Baldwin 		return;
38750c22331SPoul-Henning Kamp 	}
388ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
389ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
3904b3aac3dSJohn Baldwin 		req->ktr_header.ktr_buffer = buf;
391ea3fc8e4SJohn Baldwin 	}
392ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395ea3fc8e4SJohn Baldwin /*
396ea3fc8e4SJohn Baldwin  * Since the uio may not stay valid, we can not hand off this request to
397ea3fc8e4SJohn Baldwin  * the thread and need to process it synchronously.  However, we wish to
398ea3fc8e4SJohn Baldwin  * keep the relative order of records in a trace file correct, so we
399ea3fc8e4SJohn Baldwin  * do put this request on the queue (if it isn't empty) and then block.
400ea3fc8e4SJohn Baldwin  * The ktrace thread waks us back up when it is time for this event to
401ea3fc8e4SJohn Baldwin  * be posted and blocks until we have completed writing out the event
402ea3fc8e4SJohn Baldwin  * and woken it back up.
403ea3fc8e4SJohn Baldwin  */
40426f9a767SRodney W. Grimes void
405ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
406df8bae1dSRodney W. Grimes 	int fd;
407df8bae1dSRodney W. Grimes 	enum uio_rw rw;
40842ebfbf2SBrian Feldman 	struct uio *uio;
40942ebfbf2SBrian Feldman 	int error;
410df8bae1dSRodney W. Grimes {
411ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
412ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
413b92584a6SJohn Baldwin 	int datalen;
414b92584a6SJohn Baldwin 	char *buf;
415df8bae1dSRodney W. Grimes 
416552afd9cSPoul-Henning Kamp 	if (error) {
417552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
418df8bae1dSRodney W. Grimes 		return;
419552afd9cSPoul-Henning Kamp 	}
420b92584a6SJohn Baldwin 	uio->uio_offset = 0;
421b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
422b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
423a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
424552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
425552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
426552afd9cSPoul-Henning Kamp 	if (error) {
427b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
428ea3fc8e4SJohn Baldwin 		return;
429b92584a6SJohn Baldwin 	}
430b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
431b92584a6SJohn Baldwin 	if (req == NULL) {
432b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
433b92584a6SJohn Baldwin 		return;
434b92584a6SJohn Baldwin 	}
435ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
436ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
437ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
438b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
439b92584a6SJohn Baldwin 	req->ktr_header.ktr_buffer = buf;
440ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
441df8bae1dSRodney W. Grimes }
442df8bae1dSRodney W. Grimes 
44326f9a767SRodney W. Grimes void
444ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
445a93fdaacSMarcel Moolenaar 	int sig;
446df8bae1dSRodney W. Grimes 	sig_t action;
4472c42a146SMarcel Moolenaar 	sigset_t *mask;
448a93fdaacSMarcel Moolenaar 	int code;
449df8bae1dSRodney W. Grimes {
450ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
451ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
452df8bae1dSRodney W. Grimes 
453ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
454ea3fc8e4SJohn Baldwin 	if (req == NULL)
455ea3fc8e4SJohn Baldwin 		return;
456ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
457ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
458ea3fc8e4SJohn Baldwin 	kp->action = action;
459ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
460ea3fc8e4SJohn Baldwin 	kp->code = code;
461ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
46426f9a767SRodney W. Grimes void
465ea3fc8e4SJohn Baldwin ktrcsw(out, user)
466df8bae1dSRodney W. Grimes 	int out, user;
467df8bae1dSRodney W. Grimes {
468ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
469ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
470df8bae1dSRodney W. Grimes 
471ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
472ea3fc8e4SJohn Baldwin 	if (req == NULL)
473ea3fc8e4SJohn Baldwin 		return;
474ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
475ea3fc8e4SJohn Baldwin 	kc->out = out;
476ea3fc8e4SJohn Baldwin 	kc->user = user;
477ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
478df8bae1dSRodney W. Grimes }
47964cc6a13SJohn Baldwin #endif /* KTRACE */
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes /* Interface and common routines */
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes /*
484df8bae1dSRodney W. Grimes  * ktrace system call
48564cc6a13SJohn Baldwin  *
48664cc6a13SJohn Baldwin  * MPSAFE
487df8bae1dSRodney W. Grimes  */
488d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
489df8bae1dSRodney W. Grimes struct ktrace_args {
490df8bae1dSRodney W. Grimes 	char	*fname;
491df8bae1dSRodney W. Grimes 	int	ops;
492df8bae1dSRodney W. Grimes 	int	facs;
493df8bae1dSRodney W. Grimes 	int	pid;
494df8bae1dSRodney W. Grimes };
495d2d3e875SBruce Evans #endif
496df8bae1dSRodney W. Grimes /* ARGSUSED */
49726f9a767SRodney W. Grimes int
498b40ce416SJulian Elischer ktrace(td, uap)
499b40ce416SJulian Elischer 	struct thread *td;
500df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
501df8bae1dSRodney W. Grimes {
502db6a20e2SGarrett Wollman #ifdef KTRACE
503df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
504df8bae1dSRodney W. Grimes 	register struct proc *p;
505df8bae1dSRodney W. Grimes 	struct pgrp *pg;
506df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
507df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
508df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
509df8bae1dSRodney W. Grimes 	int ret = 0;
510e6796b67SKirk McKusick 	int flags, error = 0;
511df8bae1dSRodney W. Grimes 	struct nameidata nd;
512a5881ea5SJohn Baldwin 	struct ucred *cred;
513df8bae1dSRodney W. Grimes 
51464cc6a13SJohn Baldwin 	/*
51564cc6a13SJohn Baldwin 	 * Need something to (un)trace.
51664cc6a13SJohn Baldwin 	 */
51764cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
51864cc6a13SJohn Baldwin 		return (EINVAL);
51964cc6a13SJohn Baldwin 
5205e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
521df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
522df8bae1dSRodney W. Grimes 		/*
523df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
524df8bae1dSRodney W. Grimes 		 */
525b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
526e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
52764cc6a13SJohn Baldwin 		mtx_lock(&Giant);
5287c89f162SPoul-Henning Kamp 		error = vn_open(&nd, &flags, 0, -1);
529797f2d22SPoul-Henning Kamp 		if (error) {
53064cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5315e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
532df8bae1dSRodney W. Grimes 			return (error);
533df8bae1dSRodney W. Grimes 		}
534762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
535df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
536b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
537df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
538a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
53964cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5405e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
541df8bae1dSRodney W. Grimes 			return (EACCES);
542df8bae1dSRodney W. Grimes 		}
54364cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
544df8bae1dSRodney W. Grimes 	}
545df8bae1dSRodney W. Grimes 	/*
54679deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
547df8bae1dSRodney W. Grimes 	 */
548df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
5491005a129SJohn Baldwin 		sx_slock(&allproc_lock);
5502e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
551a7ff7443SJohn Baldwin 			PROC_LOCK(p);
552a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
553ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
554ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
555a5881ea5SJohn Baldwin 					cred = p->p_tracecred;
556a5881ea5SJohn Baldwin 					p->p_tracecred = NULL;
557a5881ea5SJohn Baldwin 					p->p_tracevp = NULL;
558df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
559ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
560a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
56164cc6a13SJohn Baldwin 					mtx_lock(&Giant);
562df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
563a5881ea5SJohn Baldwin 						cred, td);
56464cc6a13SJohn Baldwin 					mtx_unlock(&Giant);
565a5881ea5SJohn Baldwin 					crfree(cred);
56679deba82SMatthew Dillon 				} else {
567a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
568df8bae1dSRodney W. Grimes 					error = EPERM;
569df8bae1dSRodney W. Grimes 				}
570a7ff7443SJohn Baldwin 			} else
571a7ff7443SJohn Baldwin 				PROC_UNLOCK(p);
57279deba82SMatthew Dillon 		}
5731005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
574df8bae1dSRodney W. Grimes 		goto done;
575df8bae1dSRodney W. Grimes 	}
576df8bae1dSRodney W. Grimes 	/*
577df8bae1dSRodney W. Grimes 	 * do it
578df8bae1dSRodney W. Grimes 	 */
57964cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
580df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
581df8bae1dSRodney W. Grimes 		/*
582df8bae1dSRodney W. Grimes 		 * by process group
583df8bae1dSRodney W. Grimes 		 */
584df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
585df8bae1dSRodney W. Grimes 		if (pg == NULL) {
586ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
587df8bae1dSRodney W. Grimes 			error = ESRCH;
588df8bae1dSRodney W. Grimes 			goto done;
589df8bae1dSRodney W. Grimes 		}
590f591779bSSeigo Tanimura 		/*
591f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
592ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
593f591779bSSeigo Tanimura 		 */
594f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
5952e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
596df8bae1dSRodney W. Grimes 			if (descend)
597a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
598df8bae1dSRodney W. Grimes 			else
599a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
600df8bae1dSRodney W. Grimes 	} else {
601df8bae1dSRodney W. Grimes 		/*
602df8bae1dSRodney W. Grimes 		 * by pid
603df8bae1dSRodney W. Grimes 		 */
604df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
605df8bae1dSRodney W. Grimes 		if (p == NULL) {
60664cc6a13SJohn Baldwin 			sx_sunlock(&proctree_lock);
607df8bae1dSRodney W. Grimes 			error = ESRCH;
608df8bae1dSRodney W. Grimes 			goto done;
609df8bae1dSRodney W. Grimes 		}
61064cc6a13SJohn Baldwin 		/*
61164cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
61264cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
61364cc6a13SJohn Baldwin 		 */
61433a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
615df8bae1dSRodney W. Grimes 		if (descend)
616a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
617df8bae1dSRodney W. Grimes 		else
618a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
619df8bae1dSRodney W. Grimes 	}
62064cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
621df8bae1dSRodney W. Grimes 	if (!ret)
622df8bae1dSRodney W. Grimes 		error = EPERM;
623df8bae1dSRodney W. Grimes done:
62464cc6a13SJohn Baldwin 	if (vp != NULL) {
62564cc6a13SJohn Baldwin 		mtx_lock(&Giant);
626a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
62764cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
62864cc6a13SJohn Baldwin 	}
6295e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
630df8bae1dSRodney W. Grimes 	return (error);
63164cc6a13SJohn Baldwin #else /* !KTRACE */
63264cc6a13SJohn Baldwin 	return (ENOSYS);
63364cc6a13SJohn Baldwin #endif /* KTRACE */
634df8bae1dSRodney W. Grimes }
635df8bae1dSRodney W. Grimes 
636e6c4b9baSPoul-Henning Kamp /*
637e6c4b9baSPoul-Henning Kamp  * utrace system call
63864cc6a13SJohn Baldwin  *
63964cc6a13SJohn Baldwin  * MPSAFE
640e6c4b9baSPoul-Henning Kamp  */
641e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
642e6c4b9baSPoul-Henning Kamp int
643b40ce416SJulian Elischer utrace(td, uap)
644b40ce416SJulian Elischer 	struct thread *td;
645e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
646e6c4b9baSPoul-Henning Kamp {
647b40ce416SJulian Elischer 
648e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
649ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
6507f05b035SAlfred Perlstein 	void *cp;
651c9e7d28eSJohn Baldwin 	int error;
652e6c4b9baSPoul-Henning Kamp 
653c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
654c9e7d28eSJohn Baldwin 		return (0);
655bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6560bad156aSAlfred Perlstein 		return (EINVAL);
657a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
658c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
65950c22331SPoul-Henning Kamp 	if (error) {
66050c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
661c9e7d28eSJohn Baldwin 		return (error);
66250c22331SPoul-Henning Kamp 	}
663ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
66450c22331SPoul-Henning Kamp 	if (req == NULL) {
66550c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
666b10221ffSJoseph Koshy 		return (ENOMEM);
66750c22331SPoul-Henning Kamp 	}
668ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_buffer = cp;
669ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
670ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
671e6c4b9baSPoul-Henning Kamp 	return (0);
67264cc6a13SJohn Baldwin #else /* !KTRACE */
673e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
67464cc6a13SJohn Baldwin #endif /* KTRACE */
675e6c4b9baSPoul-Henning Kamp }
676e6c4b9baSPoul-Henning Kamp 
677db6a20e2SGarrett Wollman #ifdef KTRACE
67887b6de2bSPoul-Henning Kamp static int
679a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
680a7ff7443SJohn Baldwin 	struct thread *td;
681a7ff7443SJohn Baldwin 	struct proc *p;
682df8bae1dSRodney W. Grimes 	int ops, facs;
683df8bae1dSRodney W. Grimes 	struct vnode *vp;
684df8bae1dSRodney W. Grimes {
685ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
686a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
687df8bae1dSRodney W. Grimes 
688a7ff7443SJohn Baldwin 	PROC_LOCK(p);
689a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
690a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
691df8bae1dSRodney W. Grimes 		return (0);
692a7ff7443SJohn Baldwin 	}
693ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
694df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
695a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
696df8bae1dSRodney W. Grimes 			/*
697a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
698df8bae1dSRodney W. Grimes 			 */
699a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
700ea3fc8e4SJohn Baldwin 			VREF(vp);
701a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
702a5881ea5SJohn Baldwin 		}
703a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
704a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
705a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
706df8bae1dSRodney W. Grimes 		}
707df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
708a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
709df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
710df8bae1dSRodney W. Grimes 	} else {
711df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
712df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
713df8bae1dSRodney W. Grimes 			/* no more tracing */
714df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
715a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
716a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
717a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
718a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
719a7ff7443SJohn Baldwin 		}
720a7ff7443SJohn Baldwin 	}
721ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
722a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
72364cc6a13SJohn Baldwin 	if (tracevp != NULL) {
72464cc6a13SJohn Baldwin 		mtx_lock(&Giant);
725ea3fc8e4SJohn Baldwin 		vrele(tracevp);
72664cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
72764cc6a13SJohn Baldwin 	}
728a5881ea5SJohn Baldwin 	if (tracecred != NULL)
729a5881ea5SJohn Baldwin 		crfree(tracecred);
730df8bae1dSRodney W. Grimes 
731df8bae1dSRodney W. Grimes 	return (1);
732df8bae1dSRodney W. Grimes }
733df8bae1dSRodney W. Grimes 
73487b6de2bSPoul-Henning Kamp static int
735a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
736a7ff7443SJohn Baldwin 	struct thread *td;
737a7ff7443SJohn Baldwin 	struct proc *top;
738df8bae1dSRodney W. Grimes 	int ops, facs;
739df8bae1dSRodney W. Grimes 	struct vnode *vp;
740df8bae1dSRodney W. Grimes {
741df8bae1dSRodney W. Grimes 	register struct proc *p;
742df8bae1dSRodney W. Grimes 	register int ret = 0;
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes 	p = top;
74564cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
746df8bae1dSRodney W. Grimes 	for (;;) {
747a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
748df8bae1dSRodney W. Grimes 		/*
749df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
750df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
751df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
752df8bae1dSRodney W. Grimes 		 */
7532e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
7542e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
755df8bae1dSRodney W. Grimes 		else for (;;) {
75664cc6a13SJohn Baldwin 			if (p == top)
757df8bae1dSRodney W. Grimes 				return (ret);
7582e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7592e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
760df8bae1dSRodney W. Grimes 				break;
761df8bae1dSRodney W. Grimes 			}
762b75356e1SJeffrey Hsu 			p = p->p_pptr;
763df8bae1dSRodney W. Grimes 		}
764df8bae1dSRodney W. Grimes 	}
765df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
766df8bae1dSRodney W. Grimes }
767df8bae1dSRodney W. Grimes 
76887b6de2bSPoul-Henning Kamp static void
769ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
770df8bae1dSRodney W. Grimes {
771ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
772ea3fc8e4SJohn Baldwin 	struct vnode *vp;
773ea3fc8e4SJohn Baldwin 	struct proc *p;
774ea3fc8e4SJohn Baldwin 	struct thread *td;
775ea3fc8e4SJohn Baldwin 	struct ucred *cred;
776df8bae1dSRodney W. Grimes 	struct uio auio;
777ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
778f2a2857bSKirk McKusick 	struct mount *mp;
779ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
780df8bae1dSRodney W. Grimes 	int error;
781df8bae1dSRodney W. Grimes 
782ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
783ea3fc8e4SJohn Baldwin 	/*
784ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
785ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
786ea3fc8e4SJohn Baldwin 	 */
787df8bae1dSRodney W. Grimes 	if (vp == NULL)
788df8bae1dSRodney W. Grimes 		return;
789ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
7908b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
791ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
792ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
793ea3fc8e4SJohn Baldwin 	td = curthread;
794df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
795df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
796df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
797df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
798df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
799df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
800df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
801df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
802ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
803ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
804ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
805ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
806ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
807df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
808ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
809ea3fc8e4SJohn Baldwin 	}
810ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
811ea3fc8e4SJohn Baldwin 		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
812ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
813ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
814ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
815ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
816b92584a6SJohn Baldwin 	}
817ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
818f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
819b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
820ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
821467a273cSRobert Watson #ifdef MAC
822177142e4SRobert Watson 	error = mac_check_vnode_write(cred, NOCRED, vp);
823467a273cSRobert Watson 	if (error == 0)
824467a273cSRobert Watson #endif
825ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
826b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
827f2a2857bSKirk McKusick 	vn_finished_write(mp);
828ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
829df8bae1dSRodney W. Grimes 	if (!error)
830df8bae1dSRodney W. Grimes 		return;
831df8bae1dSRodney W. Grimes 	/*
832ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
833ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
834ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
835df8bae1dSRodney W. Grimes 	 */
836df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
837df8bae1dSRodney W. Grimes 	    error);
838ea3fc8e4SJohn Baldwin 	vrele_count = 0;
839ea3fc8e4SJohn Baldwin 	/*
840ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
841ea3fc8e4SJohn Baldwin 	 * system.
842ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
843ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
844ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
845ea3fc8e4SJohn Baldwin 	 */
846a5881ea5SJohn Baldwin 	cred = NULL;
8471005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8482e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
849ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
850a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
851ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
852a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
853df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
854a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
855a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
856ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
857ea3fc8e4SJohn Baldwin 			vrele_count++;
858df8bae1dSRodney W. Grimes 		}
859ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
860a5881ea5SJohn Baldwin 		if (cred != NULL) {
861a5881ea5SJohn Baldwin 			crfree(cred);
862a5881ea5SJohn Baldwin 			cred = NULL;
863a5881ea5SJohn Baldwin 		}
864df8bae1dSRodney W. Grimes 	}
8651005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
866ea3fc8e4SJohn Baldwin 	/*
867ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
868ea3fc8e4SJohn Baldwin 	 */
869ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
870ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
871ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
872ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
873ea3fc8e4SJohn Baldwin 			vrele_count++;
874ea3fc8e4SJohn Baldwin 		}
875ea3fc8e4SJohn Baldwin 	}
876ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
877ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
878ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
879ea3fc8e4SJohn Baldwin 		vrele(vp);
880ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
881df8bae1dSRodney W. Grimes }
882df8bae1dSRodney W. Grimes 
883df8bae1dSRodney W. Grimes /*
884df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
885df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
886df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
887df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
888df8bae1dSRodney W. Grimes  * so, only root may further change it.
889df8bae1dSRodney W. Grimes  */
89087b6de2bSPoul-Henning Kamp static int
891a7ff7443SJohn Baldwin ktrcanset(td, targetp)
892a7ff7443SJohn Baldwin 	struct thread *td;
893a7ff7443SJohn Baldwin 	struct proc *targetp;
894df8bae1dSRodney W. Grimes {
895df8bae1dSRodney W. Grimes 
896a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
897a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
898a7ff7443SJohn Baldwin 	    suser_cred(td->td_ucred, PRISON_ROOT))
89975c13541SPoul-Henning Kamp 		return (0);
900a0f75161SRobert Watson 
901f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
902a0f75161SRobert Watson 		return (0);
903a0f75161SRobert Watson 
904df8bae1dSRodney W. Grimes 	return (1);
905df8bae1dSRodney W. Grimes }
906df8bae1dSRodney W. Grimes 
907db6a20e2SGarrett Wollman #endif /* KTRACE */
908