xref: /freebsd/sys/kern/kern_ktrace.c (revision 4b3aac3d4efaec9785a1fc8dc2605a8b59a4f9c4)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37db6a20e2SGarrett Wollman #include "opt_ktrace.h"
38467a273cSRobert Watson #include "opt_mac.h"
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41f23b4c91SGarrett Wollman #include <sys/systm.h>
42ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
43ea3fc8e4SJohn Baldwin #include <sys/jail.h>
44ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
45ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
46fb919e4dSMark Murray #include <sys/lock.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
48467a273cSRobert Watson #include <sys/mac.h>
49ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/namei.h>
51ea3fc8e4SJohn Baldwin #include <sys/proc.h>
52ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
55ea3fc8e4SJohn Baldwin #include <sys/sema.h>
561005a129SJohn Baldwin #include <sys/sx.h>
57ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
58df8bae1dSRodney W. Grimes #include <sys/syslog.h>
59ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
60df8bae1dSRodney W. Grimes 
61a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
6255166637SPoul-Henning Kamp 
63db6a20e2SGarrett Wollman #ifdef KTRACE
64ea3fc8e4SJohn Baldwin 
65ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
66ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
67ea3fc8e4SJohn Baldwin #endif
68ea3fc8e4SJohn Baldwin 
69ea3fc8e4SJohn Baldwin struct ktr_request {
70ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
71ea3fc8e4SJohn Baldwin 	struct	ucred *ktr_cred;
72ea3fc8e4SJohn Baldwin 	struct	vnode *ktr_vp;
73ea3fc8e4SJohn Baldwin 	union {
74ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
75ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
76ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
77ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
78ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
79ea3fc8e4SJohn Baldwin 	} ktr_data;
80ea3fc8e4SJohn Baldwin 	int	ktr_synchronous;
81ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
82ea3fc8e4SJohn Baldwin };
83ea3fc8e4SJohn Baldwin 
84ea3fc8e4SJohn Baldwin static int data_lengths[] = {
85ea3fc8e4SJohn Baldwin 	0,					/* none */
86ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
87ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
88ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
89ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
90ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
91ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
92ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
93ea3fc8e4SJohn Baldwin };
94ea3fc8e4SJohn Baldwin 
95ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo;
96ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
97ea3fc8e4SJohn Baldwin 
98ea3fc8e4SJohn Baldwin static uint ktr_requestpool = KTRACE_REQUEST_POOL;
99ea3fc8e4SJohn Baldwin TUNABLE_INT("kern.ktrace_request_pool", &ktr_requestpool);
100ea3fc8e4SJohn Baldwin 
101ea3fc8e4SJohn Baldwin static int print_message = 1;
102ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
103ea3fc8e4SJohn Baldwin static struct sema ktrace_sema;
104ea3fc8e4SJohn Baldwin 
105ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
106ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
107ea3fc8e4SJohn Baldwin static uint ktrace_resize_pool(uint newsize);
108ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
109ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req);
110ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
111ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy);
112ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req);
113a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
114a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
115a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
11698d93822SBruce Evans 
117ea3fc8e4SJohn Baldwin static void
118ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
119df8bae1dSRodney W. Grimes {
120ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
121ea3fc8e4SJohn Baldwin 	int i;
122df8bae1dSRodney W. Grimes 
123ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
124ea3fc8e4SJohn Baldwin 	sema_init(&ktrace_sema, 0, "ktrace");
125ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_todo);
126ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
127ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
128ea3fc8e4SJohn Baldwin 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
129ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
130ea3fc8e4SJohn Baldwin 	}
131ea3fc8e4SJohn Baldwin 	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, "ktrace");
132ea3fc8e4SJohn Baldwin }
133ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
134ea3fc8e4SJohn Baldwin 
135ea3fc8e4SJohn Baldwin static int
136ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
137ea3fc8e4SJohn Baldwin {
138ea3fc8e4SJohn Baldwin 	struct thread *td;
139ea3fc8e4SJohn Baldwin 	uint newsize, oldsize, wantsize;
140ea3fc8e4SJohn Baldwin 	int error;
141ea3fc8e4SJohn Baldwin 
142ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
143ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
144ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
145ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
146ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
147ea3fc8e4SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(uint)));
148ea3fc8e4SJohn Baldwin 	}
149ea3fc8e4SJohn Baldwin 
150ea3fc8e4SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(uint));
151ea3fc8e4SJohn Baldwin 	if (error)
152ea3fc8e4SJohn Baldwin 		return (error);
153ea3fc8e4SJohn Baldwin 	td = curthread;
154ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
155ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
156ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
157ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
158ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
159ea3fc8e4SJohn Baldwin 	td->td_inktrace = 0;
160ea3fc8e4SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(uint));
161ea3fc8e4SJohn Baldwin 	if (error)
162ea3fc8e4SJohn Baldwin 		return (error);
163ea3fc8e4SJohn Baldwin 	if (newsize != wantsize)
164ea3fc8e4SJohn Baldwin 		return (ENOSPC);
165ea3fc8e4SJohn Baldwin 	return (0);
166ea3fc8e4SJohn Baldwin }
167ea3fc8e4SJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, ktrace_request_pool, CTLTYPE_UINT|CTLFLAG_RW,
168ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
169ea3fc8e4SJohn Baldwin 
170ea3fc8e4SJohn Baldwin static uint
171ea3fc8e4SJohn Baldwin ktrace_resize_pool(uint newsize)
172ea3fc8e4SJohn Baldwin {
173ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
174ea3fc8e4SJohn Baldwin 
175ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
176ea3fc8e4SJohn Baldwin 	print_message = 1;
177ea3fc8e4SJohn Baldwin 	if (newsize == ktr_requestpool)
178ea3fc8e4SJohn Baldwin 		return (newsize);
179ea3fc8e4SJohn Baldwin 	if (newsize < ktr_requestpool)
180ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
181ea3fc8e4SJohn Baldwin 		while (ktr_requestpool > newsize) {
182ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
183ea3fc8e4SJohn Baldwin 			if (req == NULL)
184ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
185ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
186ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
187ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
188ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
189ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
190ea3fc8e4SJohn Baldwin 		}
191ea3fc8e4SJohn Baldwin 	else
192ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
193ea3fc8e4SJohn Baldwin 		while (ktr_requestpool < newsize) {
194ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
195ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
196ea3fc8e4SJohn Baldwin 			    M_WAITOK);
197ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
198ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
199ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
200ea3fc8e4SJohn Baldwin 		}
201ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
202ea3fc8e4SJohn Baldwin }
203ea3fc8e4SJohn Baldwin 
204ea3fc8e4SJohn Baldwin static struct ktr_request *
205ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
206ea3fc8e4SJohn Baldwin {
207ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
208ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
209ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
210ea3fc8e4SJohn Baldwin 	int pm;
211ea3fc8e4SJohn Baldwin 
212ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
213ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
214ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
215ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
216ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
217ea3fc8e4SJohn Baldwin 		return (NULL);
218ea3fc8e4SJohn Baldwin 	}
219ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
220ea3fc8e4SJohn Baldwin 	if (req != NULL) {
221ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
222ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
223ea3fc8e4SJohn Baldwin 		KASSERT(p->p_tracep != NULL, ("ktrace: no trace vnode"));
224ea3fc8e4SJohn Baldwin 		req->ktr_vp = p->p_tracep;
225ea3fc8e4SJohn Baldwin 		VREF(p->p_tracep);
226ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
227ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
228ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
229ea3fc8e4SJohn Baldwin 		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
230ea3fc8e4SJohn Baldwin 		req->ktr_cred = crhold(td->td_ucred);
231ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = NULL;
232ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
233ea3fc8e4SJohn Baldwin 		req->ktr_synchronous = 0;
234ea3fc8e4SJohn Baldwin 	} else {
235ea3fc8e4SJohn Baldwin 		pm = print_message;
236ea3fc8e4SJohn Baldwin 		print_message = 0;
237ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
238ea3fc8e4SJohn Baldwin 		if (pm)
239ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
240ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
241ea3fc8e4SJohn Baldwin 	}
242ea3fc8e4SJohn Baldwin 	return (req);
243ea3fc8e4SJohn Baldwin }
244ea3fc8e4SJohn Baldwin 
245ea3fc8e4SJohn Baldwin static void
246ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req)
247ea3fc8e4SJohn Baldwin {
248ea3fc8e4SJohn Baldwin 
249ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
250ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
251ea3fc8e4SJohn Baldwin 	sema_post(&ktrace_sema);
252ea3fc8e4SJohn Baldwin 	if (req->ktr_synchronous) {
253ea3fc8e4SJohn Baldwin 		/*
254ea3fc8e4SJohn Baldwin 		 * For a synchronous request, we wait for the ktrace thread
255ea3fc8e4SJohn Baldwin 		 * to get to our item in the todo list and wake us up.  Then
256ea3fc8e4SJohn Baldwin 		 * we write the request out ourselves and wake the ktrace
257ea3fc8e4SJohn Baldwin 		 * thread back up.
258ea3fc8e4SJohn Baldwin 		 */
259ea3fc8e4SJohn Baldwin 		msleep(req, &ktrace_mtx, curthread->td_priority, "ktrsync", 0);
260ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
261ea3fc8e4SJohn Baldwin 		ktr_writerequest(req);
262ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
263ea3fc8e4SJohn Baldwin 		wakeup(req);
264ea3fc8e4SJohn Baldwin 	}
265ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
266ea3fc8e4SJohn Baldwin 	curthread->td_inktrace = 0;
267ea3fc8e4SJohn Baldwin }
268ea3fc8e4SJohn Baldwin 
269ea3fc8e4SJohn Baldwin static void
270ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
271ea3fc8e4SJohn Baldwin {
272ea3fc8e4SJohn Baldwin 
273ea3fc8e4SJohn Baldwin 	crfree(req->ktr_cred);
274fbd140c7SJohn Baldwin 	if (req->ktr_vp != NULL) {
275ea3fc8e4SJohn Baldwin 		mtx_lock(&Giant);
276ea3fc8e4SJohn Baldwin 		vrele(req->ktr_vp);
277ea3fc8e4SJohn Baldwin 		mtx_unlock(&Giant);
278fbd140c7SJohn Baldwin 	}
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 		sema_wait(&ktrace_sema);
296ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
297ea3fc8e4SJohn Baldwin 		req = STAILQ_FIRST(&ktr_todo);
298ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
299ea3fc8e4SJohn Baldwin 		KASSERT(req != NULL, ("got a NULL request"));
300ea3fc8e4SJohn Baldwin 		if (req->ktr_synchronous) {
301ea3fc8e4SJohn Baldwin 			wakeup(req);
302ea3fc8e4SJohn Baldwin 			msleep(req, &ktrace_mtx, curthread->td_priority,
303ea3fc8e4SJohn Baldwin 			    "ktrwait", 0);
304ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
305ea3fc8e4SJohn Baldwin 		} else {
306ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
307ea3fc8e4SJohn Baldwin 			/*
308ea3fc8e4SJohn Baldwin 			 * It is not enough just to pass the cached cred
309ea3fc8e4SJohn Baldwin 			 * to the VOP's in ktr_writerequest().  Some VFS
310ea3fc8e4SJohn Baldwin 			 * operations use curthread->td_ucred, so we need
311ea3fc8e4SJohn Baldwin 			 * to modify our thread's credentials as well.
312ea3fc8e4SJohn Baldwin 			 * Evil.
313ea3fc8e4SJohn Baldwin 			 */
314ea3fc8e4SJohn Baldwin 			td->td_ucred = req->ktr_cred;
315ea3fc8e4SJohn Baldwin 			ktr_writerequest(req);
316ea3fc8e4SJohn Baldwin 			td->td_ucred = cred;
317ea3fc8e4SJohn Baldwin 		}
318ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
319ea3fc8e4SJohn Baldwin 	}
320df8bae1dSRodney W. Grimes }
321df8bae1dSRodney W. Grimes 
322356861dbSMatthew Dillon /*
323356861dbSMatthew Dillon  * MPSAFE
324356861dbSMatthew Dillon  */
32526f9a767SRodney W. Grimes void
326ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
32771ddfdbbSDmitrij Tejblum 	int code, narg;
32871ddfdbbSDmitrij Tejblum 	register_t args[];
329df8bae1dSRodney W. Grimes {
330ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
331df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
332ea3fc8e4SJohn Baldwin 	size_t buflen;
3334b3aac3dSJohn Baldwin 	char *buf = NULL;
334df8bae1dSRodney W. Grimes 
3354b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
3364b3aac3dSJohn Baldwin 	if (buflen > 0) {
3374b3aac3dSJohn Baldwin 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
3384b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
3394b3aac3dSJohn Baldwin 	}
340ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
341ea3fc8e4SJohn Baldwin 	if (req == NULL)
342ea3fc8e4SJohn Baldwin 		return;
343ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
344df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
345df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
346ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
347ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
3484b3aac3dSJohn Baldwin 		req->ktr_header.ktr_buffer = buf;
349ea3fc8e4SJohn Baldwin 	}
350ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
351df8bae1dSRodney W. Grimes }
352df8bae1dSRodney W. Grimes 
353356861dbSMatthew Dillon /*
354356861dbSMatthew Dillon  * MPSAFE
355356861dbSMatthew Dillon  */
35626f9a767SRodney W. Grimes void
357ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
35871ddfdbbSDmitrij Tejblum 	int code, error;
35971ddfdbbSDmitrij Tejblum 	register_t retval;
360df8bae1dSRodney W. Grimes {
361ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
362ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
363df8bae1dSRodney W. Grimes 
364ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
365ea3fc8e4SJohn Baldwin 	if (req == NULL)
366ea3fc8e4SJohn Baldwin 		return;
367ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
368ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
369ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
370ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
371ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
372df8bae1dSRodney W. Grimes }
373df8bae1dSRodney W. Grimes 
37426f9a767SRodney W. Grimes void
375ea3fc8e4SJohn Baldwin ktrnamei(path)
376df8bae1dSRodney W. Grimes 	char *path;
377df8bae1dSRodney W. Grimes {
378ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
379ea3fc8e4SJohn Baldwin 	int namelen;
3804b3aac3dSJohn Baldwin 	char *buf = NULL;
381df8bae1dSRodney W. Grimes 
3824b3aac3dSJohn Baldwin 	namelen = strlen(path);
3834b3aac3dSJohn Baldwin 	if (namelen > 0) {
3844b3aac3dSJohn Baldwin 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
3854b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
3864b3aac3dSJohn Baldwin 	}
387ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
388ea3fc8e4SJohn Baldwin 	if (req == NULL)
389ea3fc8e4SJohn Baldwin 		return;
390ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
391ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
3924b3aac3dSJohn Baldwin 		req->ktr_header.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;
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes 	if (error)
417df8bae1dSRodney W. Grimes 		return;
418ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
419ea3fc8e4SJohn Baldwin 	if (req == NULL)
420ea3fc8e4SJohn Baldwin 		return;
421ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
422ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
423ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
424ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_buffer = uio;
42542ebfbf2SBrian Feldman 	uio->uio_offset = 0;
4269d1cfdceSBrian Feldman 	uio->uio_rw = UIO_WRITE;
427ea3fc8e4SJohn Baldwin 	req->ktr_synchronous = 1;
428ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
429df8bae1dSRodney W. Grimes }
430df8bae1dSRodney W. Grimes 
43126f9a767SRodney W. Grimes void
432ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
433a93fdaacSMarcel Moolenaar 	int sig;
434df8bae1dSRodney W. Grimes 	sig_t action;
4352c42a146SMarcel Moolenaar 	sigset_t *mask;
436a93fdaacSMarcel Moolenaar 	int code;
437df8bae1dSRodney W. Grimes {
438ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
439ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
440df8bae1dSRodney W. Grimes 
441ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
442ea3fc8e4SJohn Baldwin 	if (req == NULL)
443ea3fc8e4SJohn Baldwin 		return;
444ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
445ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
446ea3fc8e4SJohn Baldwin 	kp->action = action;
447ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
448ea3fc8e4SJohn Baldwin 	kp->code = code;
449ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
450df8bae1dSRodney W. Grimes }
451df8bae1dSRodney W. Grimes 
45226f9a767SRodney W. Grimes void
453ea3fc8e4SJohn Baldwin ktrcsw(out, user)
454df8bae1dSRodney W. Grimes 	int out, user;
455df8bae1dSRodney W. Grimes {
456ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
457ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
458df8bae1dSRodney W. Grimes 
459ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
460ea3fc8e4SJohn Baldwin 	if (req == NULL)
461ea3fc8e4SJohn Baldwin 		return;
462ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
463ea3fc8e4SJohn Baldwin 	kc->out = out;
464ea3fc8e4SJohn Baldwin 	kc->user = user;
465ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
466df8bae1dSRodney W. Grimes }
467db6a20e2SGarrett Wollman #endif
468df8bae1dSRodney W. Grimes 
469df8bae1dSRodney W. Grimes /* Interface and common routines */
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes /*
472df8bae1dSRodney W. Grimes  * ktrace system call
473df8bae1dSRodney W. Grimes  */
474d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
475df8bae1dSRodney W. Grimes struct ktrace_args {
476df8bae1dSRodney W. Grimes 	char	*fname;
477df8bae1dSRodney W. Grimes 	int	ops;
478df8bae1dSRodney W. Grimes 	int	facs;
479df8bae1dSRodney W. Grimes 	int	pid;
480df8bae1dSRodney W. Grimes };
481d2d3e875SBruce Evans #endif
482df8bae1dSRodney W. Grimes /* ARGSUSED */
48326f9a767SRodney W. Grimes int
484b40ce416SJulian Elischer ktrace(td, uap)
485b40ce416SJulian Elischer 	struct thread *td;
486df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
487df8bae1dSRodney W. Grimes {
488db6a20e2SGarrett Wollman #ifdef KTRACE
489df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
490df8bae1dSRodney W. Grimes 	register struct proc *p;
491df8bae1dSRodney W. Grimes 	struct pgrp *pg;
492df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
493df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
494df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
495df8bae1dSRodney W. Grimes 	int ret = 0;
496e6796b67SKirk McKusick 	int flags, error = 0;
497df8bae1dSRodney W. Grimes 	struct nameidata nd;
498df8bae1dSRodney W. Grimes 
499ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
500df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
501df8bae1dSRodney W. Grimes 		/*
502df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
503df8bae1dSRodney W. Grimes 		 */
504b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
505e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
506e6796b67SKirk McKusick 		error = vn_open(&nd, &flags, 0);
507797f2d22SPoul-Henning Kamp 		if (error) {
508ea3fc8e4SJohn Baldwin 			td->td_inktrace = 0;
509df8bae1dSRodney W. Grimes 			return (error);
510df8bae1dSRodney W. Grimes 		}
511762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
512df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
513b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
514df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
515a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
516ea3fc8e4SJohn Baldwin 			td->td_inktrace = 0;
517df8bae1dSRodney W. Grimes 			return (EACCES);
518df8bae1dSRodney W. Grimes 		}
519df8bae1dSRodney W. Grimes 	}
520df8bae1dSRodney W. Grimes 	/*
52179deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
522df8bae1dSRodney W. Grimes 	 */
523df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
5241005a129SJohn Baldwin 		sx_slock(&allproc_lock);
5252e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
526a7ff7443SJohn Baldwin 			PROC_LOCK(p);
527df8bae1dSRodney W. Grimes 			if (p->p_tracep == vp) {
528ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
529ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
530df8bae1dSRodney W. Grimes 					p->p_tracep = NULL;
531df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
532ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
533a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
534df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
535a854ed98SJohn Baldwin 						td->td_ucred, td);
53679deba82SMatthew Dillon 				} else {
537a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
538df8bae1dSRodney W. Grimes 					error = EPERM;
539df8bae1dSRodney W. Grimes 				}
540a7ff7443SJohn Baldwin 			} else
541a7ff7443SJohn Baldwin 				PROC_UNLOCK(p);
54279deba82SMatthew Dillon 		}
5431005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
544df8bae1dSRodney W. Grimes 		goto done;
545df8bae1dSRodney W. Grimes 	}
546df8bae1dSRodney W. Grimes 	/*
547df8bae1dSRodney W. Grimes 	 * need something to (un)trace (XXX - why is this here?)
548df8bae1dSRodney W. Grimes 	 */
549df8bae1dSRodney W. Grimes 	if (!facs) {
550df8bae1dSRodney W. Grimes 		error = EINVAL;
551df8bae1dSRodney W. Grimes 		goto done;
552df8bae1dSRodney W. Grimes 	}
553df8bae1dSRodney W. Grimes 	/*
554df8bae1dSRodney W. Grimes 	 * do it
555df8bae1dSRodney W. Grimes 	 */
556df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
557df8bae1dSRodney W. Grimes 		/*
558df8bae1dSRodney W. Grimes 		 * by process group
559df8bae1dSRodney W. Grimes 		 */
560ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
561df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
562df8bae1dSRodney W. Grimes 		if (pg == NULL) {
563ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
564df8bae1dSRodney W. Grimes 			error = ESRCH;
565df8bae1dSRodney W. Grimes 			goto done;
566df8bae1dSRodney W. Grimes 		}
567f591779bSSeigo Tanimura 		/*
568f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
569ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
570f591779bSSeigo Tanimura 		 */
571f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
5722e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
573df8bae1dSRodney W. Grimes 			if (descend)
574a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
575df8bae1dSRodney W. Grimes 			else
576a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
577ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
578df8bae1dSRodney W. Grimes 	} else {
579df8bae1dSRodney W. Grimes 		/*
580df8bae1dSRodney W. Grimes 		 * by pid
581df8bae1dSRodney W. Grimes 		 */
582df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
583df8bae1dSRodney W. Grimes 		if (p == NULL) {
584df8bae1dSRodney W. Grimes 			error = ESRCH;
585df8bae1dSRodney W. Grimes 			goto done;
586df8bae1dSRodney W. Grimes 		}
58733a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
588a7ff7443SJohn Baldwin 		/* XXX: UNLOCK above has a race */
589df8bae1dSRodney W. Grimes 		if (descend)
590a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
591df8bae1dSRodney W. Grimes 		else
592a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
593df8bae1dSRodney W. Grimes 	}
594df8bae1dSRodney W. Grimes 	if (!ret)
595df8bae1dSRodney W. Grimes 		error = EPERM;
596df8bae1dSRodney W. Grimes done:
597df8bae1dSRodney W. Grimes 	if (vp != NULL)
598a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
599ea3fc8e4SJohn Baldwin 	td->td_inktrace = 0;
600df8bae1dSRodney W. Grimes 	return (error);
601db6a20e2SGarrett Wollman #else
602db6a20e2SGarrett Wollman 	return ENOSYS;
603db6a20e2SGarrett Wollman #endif
604df8bae1dSRodney W. Grimes }
605df8bae1dSRodney W. Grimes 
606e6c4b9baSPoul-Henning Kamp /*
607e6c4b9baSPoul-Henning Kamp  * utrace system call
608e6c4b9baSPoul-Henning Kamp  */
609e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
610e6c4b9baSPoul-Henning Kamp int
611b40ce416SJulian Elischer utrace(td, uap)
612b40ce416SJulian Elischer 	struct thread *td;
613e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
614e6c4b9baSPoul-Henning Kamp {
615b40ce416SJulian Elischer 
616e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
617ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
6187f05b035SAlfred Perlstein 	void *cp;
619e6c4b9baSPoul-Henning Kamp 
620bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6210bad156aSAlfred Perlstein 		return (EINVAL);
622ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
623ea3fc8e4SJohn Baldwin 	if (req == NULL)
624ea3fc8e4SJohn Baldwin 		return (0);
6257f05b035SAlfred Perlstein 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
626e6c4b9baSPoul-Henning Kamp 	if (!copyin(uap->addr, cp, uap->len)) {
627ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = cp;
628ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = uap->len;
629ea3fc8e4SJohn Baldwin 		ktr_submitrequest(req);
630ea3fc8e4SJohn Baldwin 	} else {
631ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
632ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
633e6c4b9baSPoul-Henning Kamp 	}
634e6c4b9baSPoul-Henning Kamp 	return (0);
635e6c4b9baSPoul-Henning Kamp #else
636e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
637e6c4b9baSPoul-Henning Kamp #endif
638e6c4b9baSPoul-Henning Kamp }
639e6c4b9baSPoul-Henning Kamp 
640db6a20e2SGarrett Wollman #ifdef KTRACE
64187b6de2bSPoul-Henning Kamp static int
642a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
643a7ff7443SJohn Baldwin 	struct thread *td;
644a7ff7443SJohn Baldwin 	struct proc *p;
645df8bae1dSRodney W. Grimes 	int ops, facs;
646df8bae1dSRodney W. Grimes 	struct vnode *vp;
647df8bae1dSRodney W. Grimes {
648ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
649df8bae1dSRodney W. Grimes 
650a7ff7443SJohn Baldwin 	PROC_LOCK(p);
651a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
652a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
653df8bae1dSRodney W. Grimes 		return (0);
654a7ff7443SJohn Baldwin 	}
655ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
656df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
657df8bae1dSRodney W. Grimes 		if (p->p_tracep != vp) {
658df8bae1dSRodney W. Grimes 			/*
659a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
660df8bae1dSRodney W. Grimes 			 */
661ea3fc8e4SJohn Baldwin 			tracevp = p->p_tracep;
662ea3fc8e4SJohn Baldwin 			VREF(vp);
663ea3fc8e4SJohn Baldwin 			p->p_tracep = vp;
664df8bae1dSRodney W. Grimes 		}
665df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
666a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
667df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
668df8bae1dSRodney W. Grimes 	} else {
669df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
670df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
671df8bae1dSRodney W. Grimes 			/* no more tracing */
672df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
673ea3fc8e4SJohn Baldwin 			tracevp = p->p_tracep;
674df8bae1dSRodney W. Grimes 			p->p_tracep = NULL;
675a7ff7443SJohn Baldwin 		}
676a7ff7443SJohn Baldwin 	}
677ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
678a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
679ea3fc8e4SJohn Baldwin 	if (tracevp != NULL)
680ea3fc8e4SJohn Baldwin 		vrele(tracevp);
681df8bae1dSRodney W. Grimes 
682df8bae1dSRodney W. Grimes 	return (1);
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
68587b6de2bSPoul-Henning Kamp static int
686a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
687a7ff7443SJohn Baldwin 	struct thread *td;
688a7ff7443SJohn Baldwin 	struct proc *top;
689df8bae1dSRodney W. Grimes 	int ops, facs;
690df8bae1dSRodney W. Grimes 	struct vnode *vp;
691df8bae1dSRodney W. Grimes {
692df8bae1dSRodney W. Grimes 	register struct proc *p;
693df8bae1dSRodney W. Grimes 	register int ret = 0;
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	p = top;
6961005a129SJohn Baldwin 	sx_slock(&proctree_lock);
697df8bae1dSRodney W. Grimes 	for (;;) {
698a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
699df8bae1dSRodney W. Grimes 		/*
700df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
701df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
702df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
703df8bae1dSRodney W. Grimes 		 */
7042e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
7052e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
706df8bae1dSRodney W. Grimes 		else for (;;) {
70798f03f90SJake Burkholder 			if (p == top) {
7081005a129SJohn Baldwin 				sx_sunlock(&proctree_lock);
709df8bae1dSRodney W. Grimes 				return (ret);
71098f03f90SJake Burkholder 			}
7112e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7122e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
713df8bae1dSRodney W. Grimes 				break;
714df8bae1dSRodney W. Grimes 			}
715b75356e1SJeffrey Hsu 			p = p->p_pptr;
716df8bae1dSRodney W. Grimes 		}
717df8bae1dSRodney W. Grimes 	}
718df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
719df8bae1dSRodney W. Grimes }
720df8bae1dSRodney W. Grimes 
72187b6de2bSPoul-Henning Kamp static void
722ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
723df8bae1dSRodney W. Grimes {
724ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
725ea3fc8e4SJohn Baldwin 	struct vnode *vp;
726ea3fc8e4SJohn Baldwin 	struct uio *uio = NULL;
727ea3fc8e4SJohn Baldwin 	struct proc *p;
728ea3fc8e4SJohn Baldwin 	struct thread *td;
729ea3fc8e4SJohn Baldwin 	struct ucred *cred;
730df8bae1dSRodney W. Grimes 	struct uio auio;
731ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
732f2a2857bSKirk McKusick 	struct mount *mp;
733ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
734df8bae1dSRodney W. Grimes 	int error;
735df8bae1dSRodney W. Grimes 
736ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
737ea3fc8e4SJohn Baldwin 	/*
738ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
739ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
740ea3fc8e4SJohn Baldwin 	 */
741df8bae1dSRodney W. Grimes 	if (vp == NULL)
742df8bae1dSRodney W. Grimes 		return;
743ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
744ea3fc8e4SJohn Baldwin 	datalen = data_lengths[kth->ktr_type];
745ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
746ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
747ea3fc8e4SJohn Baldwin 	td = curthread;
748df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
749df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
750df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
751df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
752df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
753df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
754df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
755df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
756ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
757ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
758ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
759ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
760ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
761df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
762ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
763ea3fc8e4SJohn Baldwin 	}
764ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
765ea3fc8e4SJohn Baldwin 		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
766ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
767ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
768ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
769ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
770ea3fc8e4SJohn Baldwin 	} else
771ea3fc8e4SJohn Baldwin 		uio = kth->ktr_buffer;
772ea3fc8e4SJohn Baldwin 	KASSERT((uio == NULL) ^ (kth->ktr_type == KTR_GENIO),
773ea3fc8e4SJohn Baldwin 	    ("ktrace: uio and genio mismatch"));
77442ebfbf2SBrian Feldman 	if (uio != NULL)
77542ebfbf2SBrian Feldman 		kth->ktr_len += uio->uio_resid;
776ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
777f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
778b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
779ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
780467a273cSRobert Watson #ifdef MAC
781177142e4SRobert Watson 	error = mac_check_vnode_write(cred, NOCRED, vp);
782467a273cSRobert Watson 	if (error == 0)
783467a273cSRobert Watson #endif
784ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
78542ebfbf2SBrian Feldman 	if (error == 0 && uio != NULL) {
786ea3fc8e4SJohn Baldwin 		(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
787ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, cred);
78842ebfbf2SBrian Feldman 	}
789b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
790f2a2857bSKirk McKusick 	vn_finished_write(mp);
791ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
792ea3fc8e4SJohn Baldwin 	if (buflen != 0)
793ea3fc8e4SJohn Baldwin 		free(kth->ktr_buffer, M_KTRACE);
794df8bae1dSRodney W. Grimes 	if (!error)
795df8bae1dSRodney W. Grimes 		return;
796df8bae1dSRodney W. Grimes 	/*
797ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
798ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
799ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
800df8bae1dSRodney W. Grimes 	 */
801df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
802df8bae1dSRodney W. Grimes 	    error);
803ea3fc8e4SJohn Baldwin 	vrele_count = 0;
804ea3fc8e4SJohn Baldwin 	/*
805ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
806ea3fc8e4SJohn Baldwin 	 * system.
807ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
808ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
809ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
810ea3fc8e4SJohn Baldwin 	 */
8111005a129SJohn Baldwin 	sx_slock(&allproc_lock);
8122e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
813ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
814df8bae1dSRodney W. Grimes 		if (p->p_tracep == vp) {
815ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
816df8bae1dSRodney W. Grimes 			p->p_tracep = NULL;
817df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
818ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
819ea3fc8e4SJohn Baldwin 			vrele_count++;
820df8bae1dSRodney W. Grimes 		}
821ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
822df8bae1dSRodney W. Grimes 	}
8231005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
824ea3fc8e4SJohn Baldwin 	/*
825ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
826ea3fc8e4SJohn Baldwin 	 */
827ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
828ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
829ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
830ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
831ea3fc8e4SJohn Baldwin 			vrele_count++;
832ea3fc8e4SJohn Baldwin 		}
833ea3fc8e4SJohn Baldwin 	}
834ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
835ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
836ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
837ea3fc8e4SJohn Baldwin 		vrele(vp);
838ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
839df8bae1dSRodney W. Grimes }
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes /*
842df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
843df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
844df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
845df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
846df8bae1dSRodney W. Grimes  * so, only root may further change it.
847df8bae1dSRodney W. Grimes  */
84887b6de2bSPoul-Henning Kamp static int
849a7ff7443SJohn Baldwin ktrcanset(td, targetp)
850a7ff7443SJohn Baldwin 	struct thread *td;
851a7ff7443SJohn Baldwin 	struct proc *targetp;
852df8bae1dSRodney W. Grimes {
853df8bae1dSRodney W. Grimes 
854a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
855a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
856a7ff7443SJohn Baldwin 	    suser_cred(td->td_ucred, PRISON_ROOT))
85775c13541SPoul-Henning Kamp 		return (0);
858a0f75161SRobert Watson 
859f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
860a0f75161SRobert Watson 		return (0);
861a0f75161SRobert Watson 
862df8bae1dSRodney W. Grimes 	return (1);
863df8bae1dSRodney W. Grimes }
864df8bae1dSRodney W. Grimes 
865db6a20e2SGarrett Wollman #endif /* KTRACE */
866