xref: /freebsd/sys/kern/kern_ktrace.c (revision 4eb7c9f6c9ae4f0d2be3f286548b36f945fb7628)
19454b2d8SWarner Losh /*-
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 
935ece08f5SPoul-Henning Kamp static 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 		}
6104eb7c9f6SPawel Jakub Dawidek 		error = p_cansee(td, p);
61164cc6a13SJohn Baldwin 		/*
61264cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
61364cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
61464cc6a13SJohn Baldwin 		 */
61533a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
6164eb7c9f6SPawel Jakub Dawidek 		if (error)
6174eb7c9f6SPawel Jakub Dawidek 			goto done;
618df8bae1dSRodney W. Grimes 		if (descend)
619a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
620df8bae1dSRodney W. Grimes 		else
621a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
622df8bae1dSRodney W. Grimes 	}
62364cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
624df8bae1dSRodney W. Grimes 	if (!ret)
625df8bae1dSRodney W. Grimes 		error = EPERM;
626df8bae1dSRodney W. Grimes done:
62764cc6a13SJohn Baldwin 	if (vp != NULL) {
62864cc6a13SJohn Baldwin 		mtx_lock(&Giant);
629a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
63064cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
63164cc6a13SJohn Baldwin 	}
6325e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
633df8bae1dSRodney W. Grimes 	return (error);
63464cc6a13SJohn Baldwin #else /* !KTRACE */
63564cc6a13SJohn Baldwin 	return (ENOSYS);
63664cc6a13SJohn Baldwin #endif /* KTRACE */
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
639e6c4b9baSPoul-Henning Kamp /*
640e6c4b9baSPoul-Henning Kamp  * utrace system call
64164cc6a13SJohn Baldwin  *
64264cc6a13SJohn Baldwin  * MPSAFE
643e6c4b9baSPoul-Henning Kamp  */
644e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
645e6c4b9baSPoul-Henning Kamp int
646b40ce416SJulian Elischer utrace(td, uap)
647b40ce416SJulian Elischer 	struct thread *td;
648e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
649e6c4b9baSPoul-Henning Kamp {
650b40ce416SJulian Elischer 
651e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
652ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
6537f05b035SAlfred Perlstein 	void *cp;
654c9e7d28eSJohn Baldwin 	int error;
655e6c4b9baSPoul-Henning Kamp 
656c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
657c9e7d28eSJohn Baldwin 		return (0);
658bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6590bad156aSAlfred Perlstein 		return (EINVAL);
660a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
661c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
66250c22331SPoul-Henning Kamp 	if (error) {
66350c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
664c9e7d28eSJohn Baldwin 		return (error);
66550c22331SPoul-Henning Kamp 	}
666ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
66750c22331SPoul-Henning Kamp 	if (req == NULL) {
66850c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
669b10221ffSJoseph Koshy 		return (ENOMEM);
67050c22331SPoul-Henning Kamp 	}
671ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_buffer = cp;
672ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
673ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
674e6c4b9baSPoul-Henning Kamp 	return (0);
67564cc6a13SJohn Baldwin #else /* !KTRACE */
676e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
67764cc6a13SJohn Baldwin #endif /* KTRACE */
678e6c4b9baSPoul-Henning Kamp }
679e6c4b9baSPoul-Henning Kamp 
680db6a20e2SGarrett Wollman #ifdef KTRACE
68187b6de2bSPoul-Henning Kamp static int
682a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
683a7ff7443SJohn Baldwin 	struct thread *td;
684a7ff7443SJohn Baldwin 	struct proc *p;
685df8bae1dSRodney W. Grimes 	int ops, facs;
686df8bae1dSRodney W. Grimes 	struct vnode *vp;
687df8bae1dSRodney W. Grimes {
688ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
689a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
690df8bae1dSRodney W. Grimes 
691a7ff7443SJohn Baldwin 	PROC_LOCK(p);
692a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
693a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
694df8bae1dSRodney W. Grimes 		return (0);
695a7ff7443SJohn Baldwin 	}
696ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
697df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
698a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
699df8bae1dSRodney W. Grimes 			/*
700a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
701df8bae1dSRodney W. Grimes 			 */
702a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
703ea3fc8e4SJohn Baldwin 			VREF(vp);
704a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
705a5881ea5SJohn Baldwin 		}
706a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
707a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
708a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
709df8bae1dSRodney W. Grimes 		}
710df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
711a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
712df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
713df8bae1dSRodney W. Grimes 	} else {
714df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
715df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
716df8bae1dSRodney W. Grimes 			/* no more tracing */
717df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
718a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
719a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
720a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
721a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
722a7ff7443SJohn Baldwin 		}
723a7ff7443SJohn Baldwin 	}
724ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
725a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
72664cc6a13SJohn Baldwin 	if (tracevp != NULL) {
72764cc6a13SJohn Baldwin 		mtx_lock(&Giant);
728ea3fc8e4SJohn Baldwin 		vrele(tracevp);
72964cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
73064cc6a13SJohn Baldwin 	}
731a5881ea5SJohn Baldwin 	if (tracecred != NULL)
732a5881ea5SJohn Baldwin 		crfree(tracecred);
733df8bae1dSRodney W. Grimes 
734df8bae1dSRodney W. Grimes 	return (1);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
73787b6de2bSPoul-Henning Kamp static int
738a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
739a7ff7443SJohn Baldwin 	struct thread *td;
740a7ff7443SJohn Baldwin 	struct proc *top;
741df8bae1dSRodney W. Grimes 	int ops, facs;
742df8bae1dSRodney W. Grimes 	struct vnode *vp;
743df8bae1dSRodney W. Grimes {
744df8bae1dSRodney W. Grimes 	register struct proc *p;
745df8bae1dSRodney W. Grimes 	register int ret = 0;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	p = top;
74864cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
749df8bae1dSRodney W. Grimes 	for (;;) {
750a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
751df8bae1dSRodney W. Grimes 		/*
752df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
753df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
754df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
755df8bae1dSRodney W. Grimes 		 */
7562e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
7572e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
758df8bae1dSRodney W. Grimes 		else for (;;) {
75964cc6a13SJohn Baldwin 			if (p == top)
760df8bae1dSRodney W. Grimes 				return (ret);
7612e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7622e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
763df8bae1dSRodney W. Grimes 				break;
764df8bae1dSRodney W. Grimes 			}
765b75356e1SJeffrey Hsu 			p = p->p_pptr;
766df8bae1dSRodney W. Grimes 		}
767df8bae1dSRodney W. Grimes 	}
768df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
769df8bae1dSRodney W. Grimes }
770df8bae1dSRodney W. Grimes 
77187b6de2bSPoul-Henning Kamp static void
772ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
773df8bae1dSRodney W. Grimes {
774ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
775ea3fc8e4SJohn Baldwin 	struct vnode *vp;
776ea3fc8e4SJohn Baldwin 	struct proc *p;
777ea3fc8e4SJohn Baldwin 	struct thread *td;
778ea3fc8e4SJohn Baldwin 	struct ucred *cred;
779df8bae1dSRodney W. Grimes 	struct uio auio;
780ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
781f2a2857bSKirk McKusick 	struct mount *mp;
782ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
783df8bae1dSRodney W. Grimes 	int error;
784df8bae1dSRodney W. Grimes 
785ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
786ea3fc8e4SJohn Baldwin 	/*
787ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
788ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
789ea3fc8e4SJohn Baldwin 	 */
790df8bae1dSRodney W. Grimes 	if (vp == NULL)
791df8bae1dSRodney W. Grimes 		return;
792ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
7938b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
794ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
795ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
796ea3fc8e4SJohn Baldwin 	td = curthread;
797df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
798df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
799df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
800df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
801df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
802df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
803df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
804df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
805ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
806ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
807ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
808ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
809ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
810df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
811ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
812ea3fc8e4SJohn Baldwin 	}
813ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
814ea3fc8e4SJohn Baldwin 		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
815ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
816ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
817ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
818ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
819b92584a6SJohn Baldwin 	}
820ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
821f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
822b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
823ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
824467a273cSRobert Watson #ifdef MAC
825177142e4SRobert Watson 	error = mac_check_vnode_write(cred, NOCRED, vp);
826467a273cSRobert Watson 	if (error == 0)
827467a273cSRobert Watson #endif
828ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
829b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
830f2a2857bSKirk McKusick 	vn_finished_write(mp);
831ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
832df8bae1dSRodney W. Grimes 	if (!error)
833df8bae1dSRodney W. Grimes 		return;
834df8bae1dSRodney W. Grimes 	/*
835ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
836ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
837ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
838df8bae1dSRodney W. Grimes 	 */
839df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
840df8bae1dSRodney W. Grimes 	    error);
841ea3fc8e4SJohn Baldwin 	vrele_count = 0;
842ea3fc8e4SJohn Baldwin 	/*
843ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
844ea3fc8e4SJohn Baldwin 	 * system.
845ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
846ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
847ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
848ea3fc8e4SJohn Baldwin 	 */
849a5881ea5SJohn Baldwin 	cred = NULL;
8501005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8512e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
852ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
853a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
854ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
855a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
856df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
857a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
858a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
859ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
860ea3fc8e4SJohn Baldwin 			vrele_count++;
861df8bae1dSRodney W. Grimes 		}
862ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
863a5881ea5SJohn Baldwin 		if (cred != NULL) {
864a5881ea5SJohn Baldwin 			crfree(cred);
865a5881ea5SJohn Baldwin 			cred = NULL;
866a5881ea5SJohn Baldwin 		}
867df8bae1dSRodney W. Grimes 	}
8681005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
869ea3fc8e4SJohn Baldwin 	/*
870ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
871ea3fc8e4SJohn Baldwin 	 */
872ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
873ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
874ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
875ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
876ea3fc8e4SJohn Baldwin 			vrele_count++;
877ea3fc8e4SJohn Baldwin 		}
878ea3fc8e4SJohn Baldwin 	}
879ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
880ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
881ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
882ea3fc8e4SJohn Baldwin 		vrele(vp);
883ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
884df8bae1dSRodney W. Grimes }
885df8bae1dSRodney W. Grimes 
886df8bae1dSRodney W. Grimes /*
887df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
888df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
889df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
890df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
891df8bae1dSRodney W. Grimes  * so, only root may further change it.
892df8bae1dSRodney W. Grimes  */
89387b6de2bSPoul-Henning Kamp static int
894a7ff7443SJohn Baldwin ktrcanset(td, targetp)
895a7ff7443SJohn Baldwin 	struct thread *td;
896a7ff7443SJohn Baldwin 	struct proc *targetp;
897df8bae1dSRodney W. Grimes {
898df8bae1dSRodney W. Grimes 
899a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
900a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
90156f21b9dSColin Percival 	    suser_cred(td->td_ucred, SUSER_ALLOWJAIL))
90275c13541SPoul-Henning Kamp 		return (0);
903a0f75161SRobert Watson 
904f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
905a0f75161SRobert Watson 		return (0);
906a0f75161SRobert Watson 
907df8bae1dSRodney W. Grimes 	return (1);
908df8bae1dSRodney W. Grimes }
909df8bae1dSRodney W. Grimes 
910db6a20e2SGarrett Wollman #endif /* KTRACE */
911