xref: /freebsd/sys/kern/kern_ktrace.c (revision d977a58307dab28b66126385a720990133df2409)
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;
67d977a583SRobert Watson 	void	*ktr_buffer;
68ea3fc8e4SJohn Baldwin 	struct	ucred *ktr_cred;
69ea3fc8e4SJohn Baldwin 	struct	vnode *ktr_vp;
70ea3fc8e4SJohn Baldwin 	union {
71ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
72ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
73ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
74ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
75ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
76ea3fc8e4SJohn Baldwin 	} ktr_data;
77ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
78ea3fc8e4SJohn Baldwin };
79ea3fc8e4SJohn Baldwin 
80ea3fc8e4SJohn Baldwin static int data_lengths[] = {
81ea3fc8e4SJohn Baldwin 	0,					/* none */
82ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
83ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
84ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
85ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
86ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
87ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
88ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
89ea3fc8e4SJohn Baldwin };
90ea3fc8e4SJohn Baldwin 
91ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo;
92ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
93ea3fc8e4SJohn Baldwin 
945ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
9512301fc3SJohn Baldwin 
968b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
9712301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
9812301fc3SJohn Baldwin 
998b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
10012301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
10112301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
10212301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
103ea3fc8e4SJohn Baldwin 
104ea3fc8e4SJohn Baldwin static int print_message = 1;
105ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
106f4114c3dSJohn Baldwin static struct cv ktrace_cv;
107ea3fc8e4SJohn Baldwin 
108ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
109ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
1108b149b51SJohn Baldwin static u_int ktrace_resize_pool(u_int newsize);
111ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
112ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req);
113ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
114ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy);
115ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req);
116a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
117a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
118a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
11998d93822SBruce Evans 
120ea3fc8e4SJohn Baldwin static void
121ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
122df8bae1dSRodney W. Grimes {
123ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
124ea3fc8e4SJohn Baldwin 	int i;
125df8bae1dSRodney W. Grimes 
126ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
127f4114c3dSJohn Baldwin 	cv_init(&ktrace_cv, "ktrace");
128ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_todo);
129ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
130ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
131a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
132ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
133ea3fc8e4SJohn Baldwin 	}
134316ec49aSScott Long 	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
135ea3fc8e4SJohn Baldwin }
136ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
137ea3fc8e4SJohn Baldwin 
138ea3fc8e4SJohn Baldwin static int
139ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
140ea3fc8e4SJohn Baldwin {
141ea3fc8e4SJohn Baldwin 	struct thread *td;
1428b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
143ea3fc8e4SJohn Baldwin 	int error;
144ea3fc8e4SJohn Baldwin 
145ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
146ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
147ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
148ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
149ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
1508b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
151ea3fc8e4SJohn Baldwin 	}
152ea3fc8e4SJohn Baldwin 
1538b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
154ea3fc8e4SJohn Baldwin 	if (error)
155ea3fc8e4SJohn Baldwin 		return (error);
156ea3fc8e4SJohn Baldwin 	td = curthread;
1575e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
158ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
159ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
160ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
161ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1625e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
1638b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
164ea3fc8e4SJohn Baldwin 	if (error)
165ea3fc8e4SJohn Baldwin 		return (error);
166a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
167ea3fc8e4SJohn Baldwin 		return (ENOSPC);
168ea3fc8e4SJohn Baldwin 	return (0);
169ea3fc8e4SJohn Baldwin }
17012301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
171ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
172ea3fc8e4SJohn Baldwin 
1738b149b51SJohn Baldwin static u_int
1748b149b51SJohn Baldwin ktrace_resize_pool(u_int newsize)
175ea3fc8e4SJohn Baldwin {
176ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
177a5896914SJoseph Koshy 	int bound;
178ea3fc8e4SJohn Baldwin 
179ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
180ea3fc8e4SJohn Baldwin 	print_message = 1;
181a5896914SJoseph Koshy 	bound = newsize - ktr_requestpool;
182a5896914SJoseph Koshy 	if (bound == 0)
183a5896914SJoseph Koshy 		return (ktr_requestpool);
184a5896914SJoseph Koshy 	if (bound < 0)
185ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
186a5896914SJoseph Koshy 		while (bound++ < 0) {
187ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
188ea3fc8e4SJohn Baldwin 			if (req == NULL)
189ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
190ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
191ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
192ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
193ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
194ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
195ea3fc8e4SJohn Baldwin 		}
196ea3fc8e4SJohn Baldwin 	else
197ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
198a5896914SJoseph Koshy 		while (bound-- > 0) {
199ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
200ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
201a163d034SWarner Losh 			    M_WAITOK);
202ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
203ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
204ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
205ea3fc8e4SJohn Baldwin 		}
206ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
207ea3fc8e4SJohn Baldwin }
208ea3fc8e4SJohn Baldwin 
209ea3fc8e4SJohn Baldwin static struct ktr_request *
210ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
211ea3fc8e4SJohn Baldwin {
212ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
213ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
214ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
215ea3fc8e4SJohn Baldwin 	int pm;
216ea3fc8e4SJohn Baldwin 
2175e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
218ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
219ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
220ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
2215e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
222ea3fc8e4SJohn Baldwin 		return (NULL);
223ea3fc8e4SJohn Baldwin 	}
224ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
225ea3fc8e4SJohn Baldwin 	if (req != NULL) {
226ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
227ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
22875768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
22975768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
23075768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
23175768576SJohn Baldwin 		}
232a5881ea5SJohn Baldwin 		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
233a5881ea5SJohn Baldwin 		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
234a5881ea5SJohn Baldwin 		req->ktr_vp = p->p_tracevp;
235a5881ea5SJohn Baldwin 		VREF(p->p_tracevp);
236a5881ea5SJohn Baldwin 		req->ktr_cred = crhold(p->p_tracecred);
237ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
238ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
239ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
240ea3fc8e4SJohn Baldwin 		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
241d977a583SRobert Watson 		req->ktr_header.ktr_unused = 0;
242d977a583SRobert Watson 		req->ktr_buffer = NULL;
243ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
244ea3fc8e4SJohn Baldwin 	} else {
24575768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
246ea3fc8e4SJohn Baldwin 		pm = print_message;
247ea3fc8e4SJohn Baldwin 		print_message = 0;
248ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
249ea3fc8e4SJohn Baldwin 		if (pm)
250ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
2515e26dcb5SJohn Baldwin 		td->td_pflags &= ~TDP_INKTRACE;
252ea3fc8e4SJohn Baldwin 	}
253ea3fc8e4SJohn Baldwin 	return (req);
254ea3fc8e4SJohn Baldwin }
255ea3fc8e4SJohn Baldwin 
256ea3fc8e4SJohn Baldwin static void
257ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req)
258ea3fc8e4SJohn Baldwin {
259ea3fc8e4SJohn Baldwin 
260ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
261ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
262f4114c3dSJohn Baldwin 	cv_signal(&ktrace_cv);
263ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
2645e26dcb5SJohn Baldwin 	curthread->td_pflags &= ~TDP_INKTRACE;
265ea3fc8e4SJohn Baldwin }
266ea3fc8e4SJohn Baldwin 
267ea3fc8e4SJohn Baldwin static void
268ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
269ea3fc8e4SJohn Baldwin {
270ea3fc8e4SJohn Baldwin 
271ea3fc8e4SJohn Baldwin 	crfree(req->ktr_cred);
272fbd140c7SJohn Baldwin 	if (req->ktr_vp != NULL) {
273ea3fc8e4SJohn Baldwin 		mtx_lock(&Giant);
274ea3fc8e4SJohn Baldwin 		vrele(req->ktr_vp);
275ea3fc8e4SJohn Baldwin 		mtx_unlock(&Giant);
276fbd140c7SJohn Baldwin 	}
277d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
278d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
279ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
280ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
281ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
282ea3fc8e4SJohn Baldwin }
283ea3fc8e4SJohn Baldwin 
284ea3fc8e4SJohn Baldwin static void
285ea3fc8e4SJohn Baldwin ktr_loop(void *dummy)
286ea3fc8e4SJohn Baldwin {
287ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
288ea3fc8e4SJohn Baldwin 	struct thread *td;
289ea3fc8e4SJohn Baldwin 	struct ucred *cred;
290ea3fc8e4SJohn Baldwin 
291ea3fc8e4SJohn Baldwin 	/* Only cache these values once. */
292ea3fc8e4SJohn Baldwin 	td = curthread;
293ea3fc8e4SJohn Baldwin 	cred = td->td_ucred;
294ea3fc8e4SJohn Baldwin 	for (;;) {
295ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
296f4114c3dSJohn Baldwin 		while (STAILQ_EMPTY(&ktr_todo))
297f4114c3dSJohn Baldwin 			cv_wait(&ktrace_cv, &ktrace_mtx);
298ea3fc8e4SJohn Baldwin 		req = STAILQ_FIRST(&ktr_todo);
299ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
300ea3fc8e4SJohn Baldwin 		KASSERT(req != NULL, ("got a NULL request"));
301ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
302ea3fc8e4SJohn Baldwin 		/*
303ea3fc8e4SJohn Baldwin 		 * It is not enough just to pass the cached cred
304ea3fc8e4SJohn Baldwin 		 * to the VOP's in ktr_writerequest().  Some VFS
305ea3fc8e4SJohn Baldwin 		 * operations use curthread->td_ucred, so we need
306ea3fc8e4SJohn Baldwin 		 * to modify our thread's credentials as well.
307ea3fc8e4SJohn Baldwin 		 * Evil.
308ea3fc8e4SJohn Baldwin 		 */
309ea3fc8e4SJohn Baldwin 		td->td_ucred = req->ktr_cred;
310ea3fc8e4SJohn Baldwin 		ktr_writerequest(req);
311ea3fc8e4SJohn Baldwin 		td->td_ucred = cred;
312ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
313ea3fc8e4SJohn Baldwin 	}
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
316356861dbSMatthew Dillon /*
317356861dbSMatthew Dillon  * MPSAFE
318356861dbSMatthew Dillon  */
31926f9a767SRodney W. Grimes void
320ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
32171ddfdbbSDmitrij Tejblum 	int code, narg;
32271ddfdbbSDmitrij Tejblum 	register_t args[];
323df8bae1dSRodney W. Grimes {
324ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
325df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
326ea3fc8e4SJohn Baldwin 	size_t buflen;
3274b3aac3dSJohn Baldwin 	char *buf = NULL;
328df8bae1dSRodney W. Grimes 
3294b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3304b3aac3dSJohn Baldwin 	if (buflen > 0) {
331a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3324b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3334b3aac3dSJohn Baldwin 	}
334ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
33550c22331SPoul-Henning Kamp 	if (req == NULL) {
33650c22331SPoul-Henning Kamp 		if (buf != NULL)
33750c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
338ea3fc8e4SJohn Baldwin 		return;
33950c22331SPoul-Henning Kamp 	}
340ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
341df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
342df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
343ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
344ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
345d977a583SRobert Watson 		req->ktr_buffer = buf;
346ea3fc8e4SJohn Baldwin 	}
347ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
348df8bae1dSRodney W. Grimes }
349df8bae1dSRodney W. Grimes 
350356861dbSMatthew Dillon /*
351356861dbSMatthew Dillon  * MPSAFE
352356861dbSMatthew Dillon  */
35326f9a767SRodney W. Grimes void
354ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
35571ddfdbbSDmitrij Tejblum 	int code, error;
35671ddfdbbSDmitrij Tejblum 	register_t retval;
357df8bae1dSRodney W. Grimes {
358ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
359ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
360df8bae1dSRodney W. Grimes 
361ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
362ea3fc8e4SJohn Baldwin 	if (req == NULL)
363ea3fc8e4SJohn Baldwin 		return;
364ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
365ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
366ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
367ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
368ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
369df8bae1dSRodney W. Grimes }
370df8bae1dSRodney W. Grimes 
37126f9a767SRodney W. Grimes void
372ea3fc8e4SJohn Baldwin ktrnamei(path)
373df8bae1dSRodney W. Grimes 	char *path;
374df8bae1dSRodney W. Grimes {
375ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
376ea3fc8e4SJohn Baldwin 	int namelen;
3774b3aac3dSJohn Baldwin 	char *buf = NULL;
378df8bae1dSRodney W. Grimes 
3794b3aac3dSJohn Baldwin 	namelen = strlen(path);
3804b3aac3dSJohn Baldwin 	if (namelen > 0) {
381a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
3824b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
3834b3aac3dSJohn Baldwin 	}
384ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
38550c22331SPoul-Henning Kamp 	if (req == NULL) {
38650c22331SPoul-Henning Kamp 		if (buf != NULL)
38750c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
388ea3fc8e4SJohn Baldwin 		return;
38950c22331SPoul-Henning Kamp 	}
390ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
391ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
392d977a583SRobert Watson 		req->ktr_buffer = buf;
393ea3fc8e4SJohn Baldwin 	}
394ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
397ea3fc8e4SJohn Baldwin /*
398ea3fc8e4SJohn Baldwin  * Since the uio may not stay valid, we can not hand off this request to
399ea3fc8e4SJohn Baldwin  * the thread and need to process it synchronously.  However, we wish to
400ea3fc8e4SJohn Baldwin  * keep the relative order of records in a trace file correct, so we
401ea3fc8e4SJohn Baldwin  * do put this request on the queue (if it isn't empty) and then block.
402ea3fc8e4SJohn Baldwin  * The ktrace thread waks us back up when it is time for this event to
403ea3fc8e4SJohn Baldwin  * be posted and blocks until we have completed writing out the event
404ea3fc8e4SJohn Baldwin  * and woken it back up.
405ea3fc8e4SJohn Baldwin  */
40626f9a767SRodney W. Grimes void
407ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
408df8bae1dSRodney W. Grimes 	int fd;
409df8bae1dSRodney W. Grimes 	enum uio_rw rw;
41042ebfbf2SBrian Feldman 	struct uio *uio;
41142ebfbf2SBrian Feldman 	int error;
412df8bae1dSRodney W. Grimes {
413ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
414ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
415b92584a6SJohn Baldwin 	int datalen;
416b92584a6SJohn Baldwin 	char *buf;
417df8bae1dSRodney W. Grimes 
418552afd9cSPoul-Henning Kamp 	if (error) {
419552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
420df8bae1dSRodney W. Grimes 		return;
421552afd9cSPoul-Henning Kamp 	}
422b92584a6SJohn Baldwin 	uio->uio_offset = 0;
423b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
424b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
425a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
426552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
427552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
428552afd9cSPoul-Henning Kamp 	if (error) {
429b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
430ea3fc8e4SJohn Baldwin 		return;
431b92584a6SJohn Baldwin 	}
432b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
433b92584a6SJohn Baldwin 	if (req == NULL) {
434b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
435b92584a6SJohn Baldwin 		return;
436b92584a6SJohn Baldwin 	}
437ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
438ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
439ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
440b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
441d977a583SRobert Watson 	req->ktr_buffer = buf;
442ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
443df8bae1dSRodney W. Grimes }
444df8bae1dSRodney W. Grimes 
44526f9a767SRodney W. Grimes void
446ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
447a93fdaacSMarcel Moolenaar 	int sig;
448df8bae1dSRodney W. Grimes 	sig_t action;
4492c42a146SMarcel Moolenaar 	sigset_t *mask;
450a93fdaacSMarcel Moolenaar 	int code;
451df8bae1dSRodney W. Grimes {
452ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
453ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
454df8bae1dSRodney W. Grimes 
455ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
456ea3fc8e4SJohn Baldwin 	if (req == NULL)
457ea3fc8e4SJohn Baldwin 		return;
458ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
459ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
460ea3fc8e4SJohn Baldwin 	kp->action = action;
461ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
462ea3fc8e4SJohn Baldwin 	kp->code = code;
463ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
464df8bae1dSRodney W. Grimes }
465df8bae1dSRodney W. Grimes 
46626f9a767SRodney W. Grimes void
467ea3fc8e4SJohn Baldwin ktrcsw(out, user)
468df8bae1dSRodney W. Grimes 	int out, user;
469df8bae1dSRodney W. Grimes {
470ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
471ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
472df8bae1dSRodney W. Grimes 
473ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
474ea3fc8e4SJohn Baldwin 	if (req == NULL)
475ea3fc8e4SJohn Baldwin 		return;
476ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
477ea3fc8e4SJohn Baldwin 	kc->out = out;
478ea3fc8e4SJohn Baldwin 	kc->user = user;
479ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
480df8bae1dSRodney W. Grimes }
48164cc6a13SJohn Baldwin #endif /* KTRACE */
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes /* Interface and common routines */
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes /*
486df8bae1dSRodney W. Grimes  * ktrace system call
48764cc6a13SJohn Baldwin  *
48864cc6a13SJohn Baldwin  * MPSAFE
489df8bae1dSRodney W. Grimes  */
490d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
491df8bae1dSRodney W. Grimes struct ktrace_args {
492df8bae1dSRodney W. Grimes 	char	*fname;
493df8bae1dSRodney W. Grimes 	int	ops;
494df8bae1dSRodney W. Grimes 	int	facs;
495df8bae1dSRodney W. Grimes 	int	pid;
496df8bae1dSRodney W. Grimes };
497d2d3e875SBruce Evans #endif
498df8bae1dSRodney W. Grimes /* ARGSUSED */
49926f9a767SRodney W. Grimes int
500b40ce416SJulian Elischer ktrace(td, uap)
501b40ce416SJulian Elischer 	struct thread *td;
502df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
503df8bae1dSRodney W. Grimes {
504db6a20e2SGarrett Wollman #ifdef KTRACE
505df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
506df8bae1dSRodney W. Grimes 	register struct proc *p;
507df8bae1dSRodney W. Grimes 	struct pgrp *pg;
508df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
509df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
510df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
511400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
512e6796b67SKirk McKusick 	int flags, error = 0;
513df8bae1dSRodney W. Grimes 	struct nameidata nd;
514a5881ea5SJohn Baldwin 	struct ucred *cred;
515df8bae1dSRodney W. Grimes 
51664cc6a13SJohn Baldwin 	/*
51764cc6a13SJohn Baldwin 	 * Need something to (un)trace.
51864cc6a13SJohn Baldwin 	 */
51964cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
52064cc6a13SJohn Baldwin 		return (EINVAL);
52164cc6a13SJohn Baldwin 
5225e26dcb5SJohn Baldwin 	td->td_pflags |= TDP_INKTRACE;
523df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
524df8bae1dSRodney W. Grimes 		/*
525df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
526df8bae1dSRodney W. Grimes 		 */
527b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
528e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
52964cc6a13SJohn Baldwin 		mtx_lock(&Giant);
5307c89f162SPoul-Henning Kamp 		error = vn_open(&nd, &flags, 0, -1);
531797f2d22SPoul-Henning Kamp 		if (error) {
53264cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5335e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
534df8bae1dSRodney W. Grimes 			return (error);
535df8bae1dSRodney W. Grimes 		}
536762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
537df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
538b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
539df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
540a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
54164cc6a13SJohn Baldwin 			mtx_unlock(&Giant);
5425e26dcb5SJohn Baldwin 			td->td_pflags &= ~TDP_INKTRACE;
543df8bae1dSRodney W. Grimes 			return (EACCES);
544df8bae1dSRodney W. Grimes 		}
54564cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
546df8bae1dSRodney W. Grimes 	}
547df8bae1dSRodney W. Grimes 	/*
54879deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
549df8bae1dSRodney W. Grimes 	 */
550df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
5511005a129SJohn Baldwin 		sx_slock(&allproc_lock);
5522e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
553a7ff7443SJohn Baldwin 			PROC_LOCK(p);
554a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
555ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
556ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
557a5881ea5SJohn Baldwin 					cred = p->p_tracecred;
558a5881ea5SJohn Baldwin 					p->p_tracecred = NULL;
559a5881ea5SJohn Baldwin 					p->p_tracevp = NULL;
560df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
561ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
562a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
56364cc6a13SJohn Baldwin 					mtx_lock(&Giant);
564df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
565a5881ea5SJohn Baldwin 						cred, td);
56664cc6a13SJohn Baldwin 					mtx_unlock(&Giant);
567a5881ea5SJohn Baldwin 					crfree(cred);
56879deba82SMatthew Dillon 				} else {
569a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
570df8bae1dSRodney W. Grimes 					error = EPERM;
571df8bae1dSRodney W. Grimes 				}
572a7ff7443SJohn Baldwin 			} else
573a7ff7443SJohn Baldwin 				PROC_UNLOCK(p);
57479deba82SMatthew Dillon 		}
5751005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
576df8bae1dSRodney W. Grimes 		goto done;
577df8bae1dSRodney W. Grimes 	}
578df8bae1dSRodney W. Grimes 	/*
579df8bae1dSRodney W. Grimes 	 * do it
580df8bae1dSRodney W. Grimes 	 */
58164cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
582df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
583df8bae1dSRodney W. Grimes 		/*
584df8bae1dSRodney W. Grimes 		 * by process group
585df8bae1dSRodney W. Grimes 		 */
586df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
587df8bae1dSRodney W. Grimes 		if (pg == NULL) {
588ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
589df8bae1dSRodney W. Grimes 			error = ESRCH;
590df8bae1dSRodney W. Grimes 			goto done;
591df8bae1dSRodney W. Grimes 		}
592f591779bSSeigo Tanimura 		/*
593f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
594ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
595f591779bSSeigo Tanimura 		 */
596f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
597400a74bfSPawel Jakub Dawidek 		nfound = 0;
598400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
599400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
600400a74bfSPawel Jakub Dawidek 			if (p_cansee(td, p) != 0) {
601400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
602400a74bfSPawel Jakub Dawidek 				continue;
603400a74bfSPawel Jakub Dawidek 			}
604400a74bfSPawel Jakub Dawidek 			PROC_UNLOCK(p);
605400a74bfSPawel Jakub Dawidek 			nfound++;
606df8bae1dSRodney W. Grimes 			if (descend)
607a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
608df8bae1dSRodney W. Grimes 			else
609a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
610400a74bfSPawel Jakub Dawidek 		}
611400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
612400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
613400a74bfSPawel Jakub Dawidek 			error = ESRCH;
614400a74bfSPawel Jakub Dawidek 			goto done;
615400a74bfSPawel Jakub Dawidek 		}
616df8bae1dSRodney W. Grimes 	} else {
617df8bae1dSRodney W. Grimes 		/*
618df8bae1dSRodney W. Grimes 		 * by pid
619df8bae1dSRodney W. Grimes 		 */
620df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
621df8bae1dSRodney W. Grimes 		if (p == NULL) {
62264cc6a13SJohn Baldwin 			sx_sunlock(&proctree_lock);
623df8bae1dSRodney W. Grimes 			error = ESRCH;
624df8bae1dSRodney W. Grimes 			goto done;
625df8bae1dSRodney W. Grimes 		}
6264eb7c9f6SPawel Jakub Dawidek 		error = p_cansee(td, p);
62764cc6a13SJohn Baldwin 		/*
62864cc6a13SJohn Baldwin 		 * The slock of the proctree lock will keep this process
62964cc6a13SJohn Baldwin 		 * from going away, so unlocking the proc here is ok.
63064cc6a13SJohn Baldwin 		 */
63133a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
632b0d9aeddSPawel Jakub Dawidek 		if (error) {
633b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
6344eb7c9f6SPawel Jakub Dawidek 			goto done;
635b0d9aeddSPawel Jakub Dawidek 		}
636df8bae1dSRodney W. Grimes 		if (descend)
637a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
638df8bae1dSRodney W. Grimes 		else
639a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
640df8bae1dSRodney W. Grimes 	}
64164cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
642df8bae1dSRodney W. Grimes 	if (!ret)
643df8bae1dSRodney W. Grimes 		error = EPERM;
644df8bae1dSRodney W. Grimes done:
64564cc6a13SJohn Baldwin 	if (vp != NULL) {
64664cc6a13SJohn Baldwin 		mtx_lock(&Giant);
647a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
64864cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
64964cc6a13SJohn Baldwin 	}
6505e26dcb5SJohn Baldwin 	td->td_pflags &= ~TDP_INKTRACE;
651df8bae1dSRodney W. Grimes 	return (error);
65264cc6a13SJohn Baldwin #else /* !KTRACE */
65364cc6a13SJohn Baldwin 	return (ENOSYS);
65464cc6a13SJohn Baldwin #endif /* KTRACE */
655df8bae1dSRodney W. Grimes }
656df8bae1dSRodney W. Grimes 
657e6c4b9baSPoul-Henning Kamp /*
658e6c4b9baSPoul-Henning Kamp  * utrace system call
65964cc6a13SJohn Baldwin  *
66064cc6a13SJohn Baldwin  * MPSAFE
661e6c4b9baSPoul-Henning Kamp  */
662e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
663e6c4b9baSPoul-Henning Kamp int
664b40ce416SJulian Elischer utrace(td, uap)
665b40ce416SJulian Elischer 	struct thread *td;
666e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
667e6c4b9baSPoul-Henning Kamp {
668b40ce416SJulian Elischer 
669e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
670ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
6717f05b035SAlfred Perlstein 	void *cp;
672c9e7d28eSJohn Baldwin 	int error;
673e6c4b9baSPoul-Henning Kamp 
674c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
675c9e7d28eSJohn Baldwin 		return (0);
676bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6770bad156aSAlfred Perlstein 		return (EINVAL);
678a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
679c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
68050c22331SPoul-Henning Kamp 	if (error) {
68150c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
682c9e7d28eSJohn Baldwin 		return (error);
68350c22331SPoul-Henning Kamp 	}
684ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
68550c22331SPoul-Henning Kamp 	if (req == NULL) {
68650c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
687b10221ffSJoseph Koshy 		return (ENOMEM);
68850c22331SPoul-Henning Kamp 	}
689d977a583SRobert Watson 	req->ktr_buffer = cp;
690ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
691ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
692e6c4b9baSPoul-Henning Kamp 	return (0);
69364cc6a13SJohn Baldwin #else /* !KTRACE */
694e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
69564cc6a13SJohn Baldwin #endif /* KTRACE */
696e6c4b9baSPoul-Henning Kamp }
697e6c4b9baSPoul-Henning Kamp 
698db6a20e2SGarrett Wollman #ifdef KTRACE
69987b6de2bSPoul-Henning Kamp static int
700a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
701a7ff7443SJohn Baldwin 	struct thread *td;
702a7ff7443SJohn Baldwin 	struct proc *p;
703df8bae1dSRodney W. Grimes 	int ops, facs;
704df8bae1dSRodney W. Grimes 	struct vnode *vp;
705df8bae1dSRodney W. Grimes {
706ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
707a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
708df8bae1dSRodney W. Grimes 
709a7ff7443SJohn Baldwin 	PROC_LOCK(p);
710a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
711a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
712df8bae1dSRodney W. Grimes 		return (0);
713a7ff7443SJohn Baldwin 	}
714ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
715df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
716a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
717df8bae1dSRodney W. Grimes 			/*
718a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
719df8bae1dSRodney W. Grimes 			 */
720a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
721ea3fc8e4SJohn Baldwin 			VREF(vp);
722a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
723a5881ea5SJohn Baldwin 		}
724a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
725a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
726a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
727df8bae1dSRodney W. Grimes 		}
728df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
729a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
730df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
731df8bae1dSRodney W. Grimes 	} else {
732df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
733df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
734df8bae1dSRodney W. Grimes 			/* no more tracing */
735df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
736a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
737a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
738a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
739a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
740a7ff7443SJohn Baldwin 		}
741a7ff7443SJohn Baldwin 	}
742ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
743a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
74464cc6a13SJohn Baldwin 	if (tracevp != NULL) {
74564cc6a13SJohn Baldwin 		mtx_lock(&Giant);
746ea3fc8e4SJohn Baldwin 		vrele(tracevp);
74764cc6a13SJohn Baldwin 		mtx_unlock(&Giant);
74864cc6a13SJohn Baldwin 	}
749a5881ea5SJohn Baldwin 	if (tracecred != NULL)
750a5881ea5SJohn Baldwin 		crfree(tracecred);
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	return (1);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
75587b6de2bSPoul-Henning Kamp static int
756a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
757a7ff7443SJohn Baldwin 	struct thread *td;
758a7ff7443SJohn Baldwin 	struct proc *top;
759df8bae1dSRodney W. Grimes 	int ops, facs;
760df8bae1dSRodney W. Grimes 	struct vnode *vp;
761df8bae1dSRodney W. Grimes {
762df8bae1dSRodney W. Grimes 	register struct proc *p;
763df8bae1dSRodney W. Grimes 	register int ret = 0;
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	p = top;
76664cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
767df8bae1dSRodney W. Grimes 	for (;;) {
768a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
769df8bae1dSRodney W. Grimes 		/*
770df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
771df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
772df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
773df8bae1dSRodney W. Grimes 		 */
7742e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
7752e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
776df8bae1dSRodney W. Grimes 		else for (;;) {
77764cc6a13SJohn Baldwin 			if (p == top)
778df8bae1dSRodney W. Grimes 				return (ret);
7792e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7802e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
781df8bae1dSRodney W. Grimes 				break;
782df8bae1dSRodney W. Grimes 			}
783b75356e1SJeffrey Hsu 			p = p->p_pptr;
784df8bae1dSRodney W. Grimes 		}
785df8bae1dSRodney W. Grimes 	}
786df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
787df8bae1dSRodney W. Grimes }
788df8bae1dSRodney W. Grimes 
78987b6de2bSPoul-Henning Kamp static void
790ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
791df8bae1dSRodney W. Grimes {
792ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
793ea3fc8e4SJohn Baldwin 	struct vnode *vp;
794ea3fc8e4SJohn Baldwin 	struct proc *p;
795ea3fc8e4SJohn Baldwin 	struct thread *td;
796ea3fc8e4SJohn Baldwin 	struct ucred *cred;
797df8bae1dSRodney W. Grimes 	struct uio auio;
798ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
799f2a2857bSKirk McKusick 	struct mount *mp;
800ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
801df8bae1dSRodney W. Grimes 	int error;
802df8bae1dSRodney W. Grimes 
803ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
804ea3fc8e4SJohn Baldwin 	/*
805ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
806ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
807ea3fc8e4SJohn Baldwin 	 */
808df8bae1dSRodney W. Grimes 	if (vp == NULL)
809df8bae1dSRodney W. Grimes 		return;
810ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
8118b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
812ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
813ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
814ea3fc8e4SJohn Baldwin 	td = curthread;
815df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
816df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
817df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
818df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
819df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
820df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
821df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
822df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
823ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
824ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
825ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
826ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
827ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
828df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
829ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
830ea3fc8e4SJohn Baldwin 	}
831ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
832d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
833d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
834ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
835ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
836ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
837b92584a6SJohn Baldwin 	}
838ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
839f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
840b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
841ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
842467a273cSRobert Watson #ifdef MAC
843177142e4SRobert Watson 	error = mac_check_vnode_write(cred, NOCRED, vp);
844467a273cSRobert Watson 	if (error == 0)
845467a273cSRobert Watson #endif
846ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
847b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
848f2a2857bSKirk McKusick 	vn_finished_write(mp);
849ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
850df8bae1dSRodney W. Grimes 	if (!error)
851df8bae1dSRodney W. Grimes 		return;
852df8bae1dSRodney W. Grimes 	/*
853ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
854ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
855ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
856df8bae1dSRodney W. Grimes 	 */
857df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
858df8bae1dSRodney W. Grimes 	    error);
859ea3fc8e4SJohn Baldwin 	vrele_count = 0;
860ea3fc8e4SJohn Baldwin 	/*
861ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
862ea3fc8e4SJohn Baldwin 	 * system.
863ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
864ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
865ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
866ea3fc8e4SJohn Baldwin 	 */
867a5881ea5SJohn Baldwin 	cred = NULL;
8681005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8692e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
870ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
871a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
872ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
873a5881ea5SJohn Baldwin 			p->p_tracevp = NULL;
874df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
875a5881ea5SJohn Baldwin 			cred = p->p_tracecred;
876a5881ea5SJohn Baldwin 			p->p_tracecred = NULL;
877ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
878ea3fc8e4SJohn Baldwin 			vrele_count++;
879df8bae1dSRodney W. Grimes 		}
880ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
881a5881ea5SJohn Baldwin 		if (cred != NULL) {
882a5881ea5SJohn Baldwin 			crfree(cred);
883a5881ea5SJohn Baldwin 			cred = NULL;
884a5881ea5SJohn Baldwin 		}
885df8bae1dSRodney W. Grimes 	}
8861005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
887ea3fc8e4SJohn Baldwin 	/*
888ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
889ea3fc8e4SJohn Baldwin 	 */
890ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
891ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
892ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
893ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
894ea3fc8e4SJohn Baldwin 			vrele_count++;
895ea3fc8e4SJohn Baldwin 		}
896ea3fc8e4SJohn Baldwin 	}
897ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
898ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
899ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
900ea3fc8e4SJohn Baldwin 		vrele(vp);
901ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
902df8bae1dSRodney W. Grimes }
903df8bae1dSRodney W. Grimes 
904df8bae1dSRodney W. Grimes /*
905df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
906df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
907df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
908df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
909df8bae1dSRodney W. Grimes  * so, only root may further change it.
910df8bae1dSRodney W. Grimes  */
91187b6de2bSPoul-Henning Kamp static int
912a7ff7443SJohn Baldwin ktrcanset(td, targetp)
913a7ff7443SJohn Baldwin 	struct thread *td;
914a7ff7443SJohn Baldwin 	struct proc *targetp;
915df8bae1dSRodney W. Grimes {
916df8bae1dSRodney W. Grimes 
917a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
918a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
91956f21b9dSColin Percival 	    suser_cred(td->td_ucred, SUSER_ALLOWJAIL))
92075c13541SPoul-Henning Kamp 		return (0);
921a0f75161SRobert Watson 
922f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
923a0f75161SRobert Watson 		return (0);
924a0f75161SRobert Watson 
925df8bae1dSRodney W. Grimes 	return (1);
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
928db6a20e2SGarrett Wollman #endif /* KTRACE */
929