xref: /freebsd/sys/kern/kern_ktrace.c (revision ea3fc8e4cda27cd52db87e18d8b9a46a197c10d9)
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"
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40f23b4c91SGarrett Wollman #include <sys/systm.h>
41ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
42ea3fc8e4SJohn Baldwin #include <sys/jail.h>
43ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
44ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/namei.h>
49ea3fc8e4SJohn Baldwin #include <sys/proc.h>
50ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
53ea3fc8e4SJohn Baldwin #include <sys/sema.h>
541005a129SJohn Baldwin #include <sys/sx.h>
55ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
56df8bae1dSRodney W. Grimes #include <sys/syslog.h>
57ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
58df8bae1dSRodney W. Grimes 
59a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
6055166637SPoul-Henning Kamp 
61db6a20e2SGarrett Wollman #ifdef KTRACE
62ea3fc8e4SJohn Baldwin 
63ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
64ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
65ea3fc8e4SJohn Baldwin #endif
66ea3fc8e4SJohn Baldwin 
67ea3fc8e4SJohn Baldwin struct ktr_request {
68ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
69ea3fc8e4SJohn Baldwin 	struct	ucred *ktr_cred;
70ea3fc8e4SJohn Baldwin 	struct	vnode *ktr_vp;
71ea3fc8e4SJohn Baldwin 	union {
72ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
73ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
74ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
75ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
76ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
77ea3fc8e4SJohn Baldwin 	} ktr_data;
78ea3fc8e4SJohn Baldwin 	int	ktr_synchronous;
79ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
80ea3fc8e4SJohn Baldwin };
81ea3fc8e4SJohn Baldwin 
82ea3fc8e4SJohn Baldwin static int data_lengths[] = {
83ea3fc8e4SJohn Baldwin 	0,					/* none */
84ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
85ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
86ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
87ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
88ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
89ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
90ea3fc8e4SJohn Baldwin 	0					/* KTR_USER */
91ea3fc8e4SJohn Baldwin };
92ea3fc8e4SJohn Baldwin 
93ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_todo;
94ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
95ea3fc8e4SJohn Baldwin 
96ea3fc8e4SJohn Baldwin static uint ktr_requestpool = KTRACE_REQUEST_POOL;
97ea3fc8e4SJohn Baldwin TUNABLE_INT("kern.ktrace_request_pool", &ktr_requestpool);
98ea3fc8e4SJohn Baldwin 
99ea3fc8e4SJohn Baldwin static int print_message = 1;
100ea3fc8e4SJohn Baldwin struct mtx ktrace_mtx;
101ea3fc8e4SJohn Baldwin static struct sema ktrace_sema;
102ea3fc8e4SJohn Baldwin 
103ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
104ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
105ea3fc8e4SJohn Baldwin static uint ktrace_resize_pool(uint newsize);
106ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
107ea3fc8e4SJohn Baldwin static void ktr_submitrequest(struct ktr_request *req);
108ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
109ea3fc8e4SJohn Baldwin static void ktr_loop(void *dummy);
110ea3fc8e4SJohn Baldwin static void ktr_writerequest(struct ktr_request *req);
111a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
112a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
113a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
11498d93822SBruce Evans 
115ea3fc8e4SJohn Baldwin static void
116ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
117df8bae1dSRodney W. Grimes {
118ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
119ea3fc8e4SJohn Baldwin 	int i;
120df8bae1dSRodney W. Grimes 
121ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
122ea3fc8e4SJohn Baldwin 	sema_init(&ktrace_sema, 0, "ktrace");
123ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_todo);
124ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
125ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
126ea3fc8e4SJohn Baldwin 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
127ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
128ea3fc8e4SJohn Baldwin 	}
129ea3fc8e4SJohn Baldwin 	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, "ktrace");
130ea3fc8e4SJohn Baldwin }
131ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
132ea3fc8e4SJohn Baldwin 
133ea3fc8e4SJohn Baldwin static int
134ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
135ea3fc8e4SJohn Baldwin {
136ea3fc8e4SJohn Baldwin 	struct thread *td;
137ea3fc8e4SJohn Baldwin 	uint newsize, oldsize, wantsize;
138ea3fc8e4SJohn Baldwin 	int error;
139ea3fc8e4SJohn Baldwin 
140ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
141ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
142ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
143ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
144ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
145ea3fc8e4SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(uint)));
146ea3fc8e4SJohn Baldwin 	}
147ea3fc8e4SJohn Baldwin 
148ea3fc8e4SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(uint));
149ea3fc8e4SJohn Baldwin 	if (error)
150ea3fc8e4SJohn Baldwin 		return (error);
151ea3fc8e4SJohn Baldwin 	td = curthread;
152ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
153ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
154ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
155ea3fc8e4SJohn Baldwin 	newsize = ktrace_resize_pool(wantsize);
156ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
157ea3fc8e4SJohn Baldwin 	td->td_inktrace = 0;
158ea3fc8e4SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(uint));
159ea3fc8e4SJohn Baldwin 	if (error)
160ea3fc8e4SJohn Baldwin 		return (error);
161ea3fc8e4SJohn Baldwin 	if (newsize != wantsize)
162ea3fc8e4SJohn Baldwin 		return (ENOSPC);
163ea3fc8e4SJohn Baldwin 	return (0);
164ea3fc8e4SJohn Baldwin }
165ea3fc8e4SJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, ktrace_request_pool, CTLTYPE_UINT|CTLFLAG_RW,
166ea3fc8e4SJohn Baldwin     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
167ea3fc8e4SJohn Baldwin 
168ea3fc8e4SJohn Baldwin static uint
169ea3fc8e4SJohn Baldwin ktrace_resize_pool(uint newsize)
170ea3fc8e4SJohn Baldwin {
171ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
172ea3fc8e4SJohn Baldwin 
173ea3fc8e4SJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
174ea3fc8e4SJohn Baldwin 	print_message = 1;
175ea3fc8e4SJohn Baldwin 	if (newsize == ktr_requestpool)
176ea3fc8e4SJohn Baldwin 		return (newsize);
177ea3fc8e4SJohn Baldwin 	if (newsize < ktr_requestpool)
178ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
179ea3fc8e4SJohn Baldwin 		while (ktr_requestpool > newsize) {
180ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
181ea3fc8e4SJohn Baldwin 			if (req == NULL)
182ea3fc8e4SJohn Baldwin 				return (ktr_requestpool);
183ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
184ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
185ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
186ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
187ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
188ea3fc8e4SJohn Baldwin 		}
189ea3fc8e4SJohn Baldwin 	else
190ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
191ea3fc8e4SJohn Baldwin 		while (ktr_requestpool < newsize) {
192ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
193ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
194ea3fc8e4SJohn Baldwin 			    M_WAITOK);
195ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
196ea3fc8e4SJohn Baldwin 			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
197ea3fc8e4SJohn Baldwin 			ktr_requestpool++;
198ea3fc8e4SJohn Baldwin 		}
199ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
200ea3fc8e4SJohn Baldwin }
201ea3fc8e4SJohn Baldwin 
202ea3fc8e4SJohn Baldwin static struct ktr_request *
203ea3fc8e4SJohn Baldwin ktr_getrequest(int type)
204ea3fc8e4SJohn Baldwin {
205ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
206ea3fc8e4SJohn Baldwin 	struct thread *td = curthread;
207ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
208ea3fc8e4SJohn Baldwin 	int pm;
209ea3fc8e4SJohn Baldwin 
210ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
211ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
212ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
213ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
214ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
215ea3fc8e4SJohn Baldwin 		return (NULL);
216ea3fc8e4SJohn Baldwin 	}
217ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
218ea3fc8e4SJohn Baldwin 	if (req != NULL) {
219ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
220ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
221ea3fc8e4SJohn Baldwin 		KASSERT(p->p_tracep != NULL, ("ktrace: no trace vnode"));
222ea3fc8e4SJohn Baldwin 		req->ktr_vp = p->p_tracep;
223ea3fc8e4SJohn Baldwin 		VREF(p->p_tracep);
224ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
225ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
226ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
227ea3fc8e4SJohn Baldwin 		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
228ea3fc8e4SJohn Baldwin 		req->ktr_cred = crhold(td->td_ucred);
229ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = NULL;
230ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
231ea3fc8e4SJohn Baldwin 		req->ktr_synchronous = 0;
232ea3fc8e4SJohn Baldwin 	} else {
233ea3fc8e4SJohn Baldwin 		pm = print_message;
234ea3fc8e4SJohn Baldwin 		print_message = 0;
235ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
236ea3fc8e4SJohn Baldwin 		if (pm)
237ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
238ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
239ea3fc8e4SJohn Baldwin 	}
240ea3fc8e4SJohn Baldwin 	return (req);
241ea3fc8e4SJohn Baldwin }
242ea3fc8e4SJohn Baldwin 
243ea3fc8e4SJohn Baldwin static void
244ea3fc8e4SJohn Baldwin ktr_submitrequest(struct ktr_request *req)
245ea3fc8e4SJohn Baldwin {
246ea3fc8e4SJohn Baldwin 
247ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
248ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
249ea3fc8e4SJohn Baldwin 	sema_post(&ktrace_sema);
250ea3fc8e4SJohn Baldwin 	if (req->ktr_synchronous) {
251ea3fc8e4SJohn Baldwin 		/*
252ea3fc8e4SJohn Baldwin 		 * For a synchronous request, we wait for the ktrace thread
253ea3fc8e4SJohn Baldwin 		 * to get to our item in the todo list and wake us up.  Then
254ea3fc8e4SJohn Baldwin 		 * we write the request out ourselves and wake the ktrace
255ea3fc8e4SJohn Baldwin 		 * thread back up.
256ea3fc8e4SJohn Baldwin 		 */
257ea3fc8e4SJohn Baldwin 		msleep(req, &ktrace_mtx, curthread->td_priority, "ktrsync", 0);
258ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
259ea3fc8e4SJohn Baldwin 		ktr_writerequest(req);
260ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
261ea3fc8e4SJohn Baldwin 		wakeup(req);
262ea3fc8e4SJohn Baldwin 	}
263ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
264ea3fc8e4SJohn Baldwin 	curthread->td_inktrace = 0;
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);
272ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
273ea3fc8e4SJohn Baldwin 	vrele(req->ktr_vp);
274ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
275ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
276ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
277ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
278ea3fc8e4SJohn Baldwin }
279ea3fc8e4SJohn Baldwin 
280ea3fc8e4SJohn Baldwin static void
281ea3fc8e4SJohn Baldwin ktr_loop(void *dummy)
282ea3fc8e4SJohn Baldwin {
283ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
284ea3fc8e4SJohn Baldwin 	struct thread *td;
285ea3fc8e4SJohn Baldwin 	struct ucred *cred;
286ea3fc8e4SJohn Baldwin 
287ea3fc8e4SJohn Baldwin 	/* Only cache these values once. */
288ea3fc8e4SJohn Baldwin 	td = curthread;
289ea3fc8e4SJohn Baldwin 	cred = td->td_ucred;
290ea3fc8e4SJohn Baldwin 	for (;;) {
291ea3fc8e4SJohn Baldwin 		sema_wait(&ktrace_sema);
292ea3fc8e4SJohn Baldwin 		mtx_lock(&ktrace_mtx);
293ea3fc8e4SJohn Baldwin 		req = STAILQ_FIRST(&ktr_todo);
294ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
295ea3fc8e4SJohn Baldwin 		KASSERT(req != NULL, ("got a NULL request"));
296ea3fc8e4SJohn Baldwin 		if (req->ktr_synchronous) {
297ea3fc8e4SJohn Baldwin 			wakeup(req);
298ea3fc8e4SJohn Baldwin 			msleep(req, &ktrace_mtx, curthread->td_priority,
299ea3fc8e4SJohn Baldwin 			    "ktrwait", 0);
300ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
301ea3fc8e4SJohn Baldwin 		} else {
302ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
303ea3fc8e4SJohn Baldwin 			/*
304ea3fc8e4SJohn Baldwin 			 * It is not enough just to pass the cached cred
305ea3fc8e4SJohn Baldwin 			 * to the VOP's in ktr_writerequest().  Some VFS
306ea3fc8e4SJohn Baldwin 			 * operations use curthread->td_ucred, so we need
307ea3fc8e4SJohn Baldwin 			 * to modify our thread's credentials as well.
308ea3fc8e4SJohn Baldwin 			 * Evil.
309ea3fc8e4SJohn Baldwin 			 */
310ea3fc8e4SJohn Baldwin 			td->td_ucred = req->ktr_cred;
311ea3fc8e4SJohn Baldwin 			ktr_writerequest(req);
312ea3fc8e4SJohn Baldwin 			td->td_ucred = cred;
313ea3fc8e4SJohn Baldwin 		}
314ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
315ea3fc8e4SJohn Baldwin 	}
316df8bae1dSRodney W. Grimes }
317df8bae1dSRodney W. Grimes 
318356861dbSMatthew Dillon /*
319356861dbSMatthew Dillon  * MPSAFE
320356861dbSMatthew Dillon  */
32126f9a767SRodney W. Grimes void
322ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
32371ddfdbbSDmitrij Tejblum 	int code, narg;
32471ddfdbbSDmitrij Tejblum 	register_t args[];
325df8bae1dSRodney W. Grimes {
326ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
327df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
328ea3fc8e4SJohn Baldwin 	size_t buflen;
329df8bae1dSRodney W. Grimes 
330ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
331ea3fc8e4SJohn Baldwin 	if (req == NULL)
332ea3fc8e4SJohn Baldwin 		return;
333ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
334df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
335df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
336ea3fc8e4SJohn Baldwin 	buflen = sizeof(register_t) * narg;
337ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
338ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = malloc(buflen, M_KTRACE, M_WAITOK);
339ea3fc8e4SJohn Baldwin 		bcopy(args, req->ktr_header.ktr_buffer, buflen);
340ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
341ea3fc8e4SJohn Baldwin 	}
342ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
343df8bae1dSRodney W. Grimes }
344df8bae1dSRodney W. Grimes 
345356861dbSMatthew Dillon /*
346356861dbSMatthew Dillon  * MPSAFE
347356861dbSMatthew Dillon  */
34826f9a767SRodney W. Grimes void
349ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
35071ddfdbbSDmitrij Tejblum 	int code, error;
35171ddfdbbSDmitrij Tejblum 	register_t retval;
352df8bae1dSRodney W. Grimes {
353ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
354ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
355df8bae1dSRodney W. Grimes 
356ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
357ea3fc8e4SJohn Baldwin 	if (req == NULL)
358ea3fc8e4SJohn Baldwin 		return;
359ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
360ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
361ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
362ea3fc8e4SJohn Baldwin 	ktp->ktr_retval = retval;		/* what about val2 ? */
363ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
364df8bae1dSRodney W. Grimes }
365df8bae1dSRodney W. Grimes 
36626f9a767SRodney W. Grimes void
367ea3fc8e4SJohn Baldwin ktrnamei(path)
368df8bae1dSRodney W. Grimes 	char *path;
369df8bae1dSRodney W. Grimes {
370ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
371ea3fc8e4SJohn Baldwin 	int namelen;
372df8bae1dSRodney W. Grimes 
373ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
374ea3fc8e4SJohn Baldwin 	if (req == NULL)
375ea3fc8e4SJohn Baldwin 		return;
376ea3fc8e4SJohn Baldwin 	namelen = strlen(path);
377ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
378ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
379ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = malloc(namelen, M_KTRACE,
380ea3fc8e4SJohn Baldwin 		    M_WAITOK);
381ea3fc8e4SJohn Baldwin 		bcopy(path, req->ktr_header.ktr_buffer, namelen);
382ea3fc8e4SJohn Baldwin 	}
383ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
384df8bae1dSRodney W. Grimes }
385df8bae1dSRodney W. Grimes 
386ea3fc8e4SJohn Baldwin /*
387ea3fc8e4SJohn Baldwin  * Since the uio may not stay valid, we can not hand off this request to
388ea3fc8e4SJohn Baldwin  * the thread and need to process it synchronously.  However, we wish to
389ea3fc8e4SJohn Baldwin  * keep the relative order of records in a trace file correct, so we
390ea3fc8e4SJohn Baldwin  * do put this request on the queue (if it isn't empty) and then block.
391ea3fc8e4SJohn Baldwin  * The ktrace thread waks us back up when it is time for this event to
392ea3fc8e4SJohn Baldwin  * be posted and blocks until we have completed writing out the event
393ea3fc8e4SJohn Baldwin  * and woken it back up.
394ea3fc8e4SJohn Baldwin  */
39526f9a767SRodney W. Grimes void
396ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
397df8bae1dSRodney W. Grimes 	int fd;
398df8bae1dSRodney W. Grimes 	enum uio_rw rw;
39942ebfbf2SBrian Feldman 	struct uio *uio;
40042ebfbf2SBrian Feldman 	int error;
401df8bae1dSRodney W. Grimes {
402ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
403ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
404df8bae1dSRodney W. Grimes 
405df8bae1dSRodney W. Grimes 	if (error)
406df8bae1dSRodney W. Grimes 		return;
407ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
408ea3fc8e4SJohn Baldwin 	if (req == NULL)
409ea3fc8e4SJohn Baldwin 		return;
410ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
411ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
412ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
413ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_buffer = uio;
41442ebfbf2SBrian Feldman 	uio->uio_offset = 0;
4159d1cfdceSBrian Feldman 	uio->uio_rw = UIO_WRITE;
416ea3fc8e4SJohn Baldwin 	req->ktr_synchronous = 1;
417ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
418df8bae1dSRodney W. Grimes }
419df8bae1dSRodney W. Grimes 
42026f9a767SRodney W. Grimes void
421ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
422a93fdaacSMarcel Moolenaar 	int sig;
423df8bae1dSRodney W. Grimes 	sig_t action;
4242c42a146SMarcel Moolenaar 	sigset_t *mask;
425a93fdaacSMarcel Moolenaar 	int code;
426df8bae1dSRodney W. Grimes {
427ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
428ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
429df8bae1dSRodney W. Grimes 
430ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
431ea3fc8e4SJohn Baldwin 	if (req == NULL)
432ea3fc8e4SJohn Baldwin 		return;
433ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
434ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
435ea3fc8e4SJohn Baldwin 	kp->action = action;
436ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
437ea3fc8e4SJohn Baldwin 	kp->code = code;
438ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
439df8bae1dSRodney W. Grimes }
440df8bae1dSRodney W. Grimes 
44126f9a767SRodney W. Grimes void
442ea3fc8e4SJohn Baldwin ktrcsw(out, user)
443df8bae1dSRodney W. Grimes 	int out, user;
444df8bae1dSRodney W. Grimes {
445ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
446ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
447df8bae1dSRodney W. Grimes 
448ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
449ea3fc8e4SJohn Baldwin 	if (req == NULL)
450ea3fc8e4SJohn Baldwin 		return;
451ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
452ea3fc8e4SJohn Baldwin 	kc->out = out;
453ea3fc8e4SJohn Baldwin 	kc->user = user;
454ea3fc8e4SJohn Baldwin 	ktr_submitrequest(req);
455df8bae1dSRodney W. Grimes }
456db6a20e2SGarrett Wollman #endif
457df8bae1dSRodney W. Grimes 
458df8bae1dSRodney W. Grimes /* Interface and common routines */
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes /*
461df8bae1dSRodney W. Grimes  * ktrace system call
462df8bae1dSRodney W. Grimes  */
463d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
464df8bae1dSRodney W. Grimes struct ktrace_args {
465df8bae1dSRodney W. Grimes 	char	*fname;
466df8bae1dSRodney W. Grimes 	int	ops;
467df8bae1dSRodney W. Grimes 	int	facs;
468df8bae1dSRodney W. Grimes 	int	pid;
469df8bae1dSRodney W. Grimes };
470d2d3e875SBruce Evans #endif
471df8bae1dSRodney W. Grimes /* ARGSUSED */
47226f9a767SRodney W. Grimes int
473b40ce416SJulian Elischer ktrace(td, uap)
474b40ce416SJulian Elischer 	struct thread *td;
475df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
476df8bae1dSRodney W. Grimes {
477db6a20e2SGarrett Wollman #ifdef KTRACE
478df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
479df8bae1dSRodney W. Grimes 	register struct proc *p;
480df8bae1dSRodney W. Grimes 	struct pgrp *pg;
481df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
482df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
483df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
484df8bae1dSRodney W. Grimes 	int ret = 0;
485e6796b67SKirk McKusick 	int flags, error = 0;
486df8bae1dSRodney W. Grimes 	struct nameidata nd;
487df8bae1dSRodney W. Grimes 
488ea3fc8e4SJohn Baldwin 	td->td_inktrace = 1;
489df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
490df8bae1dSRodney W. Grimes 		/*
491df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
492df8bae1dSRodney W. Grimes 		 */
493b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
494e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
495e6796b67SKirk McKusick 		error = vn_open(&nd, &flags, 0);
496797f2d22SPoul-Henning Kamp 		if (error) {
497ea3fc8e4SJohn Baldwin 			td->td_inktrace = 0;
498df8bae1dSRodney W. Grimes 			return (error);
499df8bae1dSRodney W. Grimes 		}
500762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
501df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
502b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
503df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
504a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
505ea3fc8e4SJohn Baldwin 			td->td_inktrace = 0;
506df8bae1dSRodney W. Grimes 			return (EACCES);
507df8bae1dSRodney W. Grimes 		}
508df8bae1dSRodney W. Grimes 	}
509df8bae1dSRodney W. Grimes 	/*
51079deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
511df8bae1dSRodney W. Grimes 	 */
512df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
5131005a129SJohn Baldwin 		sx_slock(&allproc_lock);
5142e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
515a7ff7443SJohn Baldwin 			PROC_LOCK(p);
516df8bae1dSRodney W. Grimes 			if (p->p_tracep == vp) {
517ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
518ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
519df8bae1dSRodney W. Grimes 					p->p_tracep = NULL;
520df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
521ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
522a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
523df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
524a854ed98SJohn Baldwin 						td->td_ucred, td);
52579deba82SMatthew Dillon 				} else {
526a7ff7443SJohn Baldwin 					PROC_UNLOCK(p);
527df8bae1dSRodney W. Grimes 					error = EPERM;
528df8bae1dSRodney W. Grimes 				}
529a7ff7443SJohn Baldwin 			} else
530a7ff7443SJohn Baldwin 				PROC_UNLOCK(p);
53179deba82SMatthew Dillon 		}
5321005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
533df8bae1dSRodney W. Grimes 		goto done;
534df8bae1dSRodney W. Grimes 	}
535df8bae1dSRodney W. Grimes 	/*
536df8bae1dSRodney W. Grimes 	 * need something to (un)trace (XXX - why is this here?)
537df8bae1dSRodney W. Grimes 	 */
538df8bae1dSRodney W. Grimes 	if (!facs) {
539df8bae1dSRodney W. Grimes 		error = EINVAL;
540df8bae1dSRodney W. Grimes 		goto done;
541df8bae1dSRodney W. Grimes 	}
542df8bae1dSRodney W. Grimes 	/*
543df8bae1dSRodney W. Grimes 	 * do it
544df8bae1dSRodney W. Grimes 	 */
545df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
546df8bae1dSRodney W. Grimes 		/*
547df8bae1dSRodney W. Grimes 		 * by process group
548df8bae1dSRodney W. Grimes 		 */
549ba626c1dSJohn Baldwin 		sx_slock(&proctree_lock);
550df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
551df8bae1dSRodney W. Grimes 		if (pg == NULL) {
552ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
553df8bae1dSRodney W. Grimes 			error = ESRCH;
554df8bae1dSRodney W. Grimes 			goto done;
555df8bae1dSRodney W. Grimes 		}
556f591779bSSeigo Tanimura 		/*
557f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
558ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
559f591779bSSeigo Tanimura 		 */
560f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
5612e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
562df8bae1dSRodney W. Grimes 			if (descend)
563a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
564df8bae1dSRodney W. Grimes 			else
565a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
566ba626c1dSJohn Baldwin 		sx_sunlock(&proctree_lock);
567df8bae1dSRodney W. Grimes 	} else {
568df8bae1dSRodney W. Grimes 		/*
569df8bae1dSRodney W. Grimes 		 * by pid
570df8bae1dSRodney W. Grimes 		 */
571df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
572df8bae1dSRodney W. Grimes 		if (p == NULL) {
573df8bae1dSRodney W. Grimes 			error = ESRCH;
574df8bae1dSRodney W. Grimes 			goto done;
575df8bae1dSRodney W. Grimes 		}
57633a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
577a7ff7443SJohn Baldwin 		/* XXX: UNLOCK above has a race */
578df8bae1dSRodney W. Grimes 		if (descend)
579a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
580df8bae1dSRodney W. Grimes 		else
581a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
582df8bae1dSRodney W. Grimes 	}
583df8bae1dSRodney W. Grimes 	if (!ret)
584df8bae1dSRodney W. Grimes 		error = EPERM;
585df8bae1dSRodney W. Grimes done:
586df8bae1dSRodney W. Grimes 	if (vp != NULL)
587a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
588ea3fc8e4SJohn Baldwin 	td->td_inktrace = 0;
589df8bae1dSRodney W. Grimes 	return (error);
590db6a20e2SGarrett Wollman #else
591db6a20e2SGarrett Wollman 	return ENOSYS;
592db6a20e2SGarrett Wollman #endif
593df8bae1dSRodney W. Grimes }
594df8bae1dSRodney W. Grimes 
595e6c4b9baSPoul-Henning Kamp /*
596e6c4b9baSPoul-Henning Kamp  * utrace system call
597e6c4b9baSPoul-Henning Kamp  */
598e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
599e6c4b9baSPoul-Henning Kamp int
600b40ce416SJulian Elischer utrace(td, uap)
601b40ce416SJulian Elischer 	struct thread *td;
602e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
603e6c4b9baSPoul-Henning Kamp {
604b40ce416SJulian Elischer 
605e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
606ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
607e6c4b9baSPoul-Henning Kamp 	register caddr_t cp;
608e6c4b9baSPoul-Henning Kamp 
609bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
6100bad156aSAlfred Perlstein 		return (EINVAL);
611ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
612ea3fc8e4SJohn Baldwin 	if (req == NULL)
613ea3fc8e4SJohn Baldwin 		return (0);
614d920a829SPoul-Henning Kamp 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
615e6c4b9baSPoul-Henning Kamp 	if (!copyin(uap->addr, cp, uap->len)) {
616ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_buffer = cp;
617ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = uap->len;
618ea3fc8e4SJohn Baldwin 		ktr_submitrequest(req);
619ea3fc8e4SJohn Baldwin 	} else {
620ea3fc8e4SJohn Baldwin 		ktr_freerequest(req);
621ea3fc8e4SJohn Baldwin 		td->td_inktrace = 0;
622e6c4b9baSPoul-Henning Kamp 	}
623e6c4b9baSPoul-Henning Kamp 	return (0);
624e6c4b9baSPoul-Henning Kamp #else
625e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
626e6c4b9baSPoul-Henning Kamp #endif
627e6c4b9baSPoul-Henning Kamp }
628e6c4b9baSPoul-Henning Kamp 
629db6a20e2SGarrett Wollman #ifdef KTRACE
63087b6de2bSPoul-Henning Kamp static int
631a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
632a7ff7443SJohn Baldwin 	struct thread *td;
633a7ff7443SJohn Baldwin 	struct proc *p;
634df8bae1dSRodney W. Grimes 	int ops, facs;
635df8bae1dSRodney W. Grimes 	struct vnode *vp;
636df8bae1dSRodney W. Grimes {
637ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
638df8bae1dSRodney W. Grimes 
639a7ff7443SJohn Baldwin 	PROC_LOCK(p);
640a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
641a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
642df8bae1dSRodney W. Grimes 		return (0);
643a7ff7443SJohn Baldwin 	}
644ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
645df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
646df8bae1dSRodney W. Grimes 		if (p->p_tracep != vp) {
647df8bae1dSRodney W. Grimes 			/*
648a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
649df8bae1dSRodney W. Grimes 			 */
650ea3fc8e4SJohn Baldwin 			tracevp = p->p_tracep;
651ea3fc8e4SJohn Baldwin 			VREF(vp);
652ea3fc8e4SJohn Baldwin 			p->p_tracep = vp;
653df8bae1dSRodney W. Grimes 		}
654df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
655a7ff7443SJohn Baldwin 		if (td->td_ucred->cr_uid == 0)
656df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
657df8bae1dSRodney W. Grimes 	} else {
658df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
659df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
660df8bae1dSRodney W. Grimes 			/* no more tracing */
661df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
662ea3fc8e4SJohn Baldwin 			tracevp = p->p_tracep;
663df8bae1dSRodney W. Grimes 			p->p_tracep = NULL;
664a7ff7443SJohn Baldwin 		}
665a7ff7443SJohn Baldwin 	}
666ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
667a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
668ea3fc8e4SJohn Baldwin 	if (tracevp != NULL)
669ea3fc8e4SJohn Baldwin 		vrele(tracevp);
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	return (1);
672df8bae1dSRodney W. Grimes }
673df8bae1dSRodney W. Grimes 
67487b6de2bSPoul-Henning Kamp static int
675a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
676a7ff7443SJohn Baldwin 	struct thread *td;
677a7ff7443SJohn Baldwin 	struct proc *top;
678df8bae1dSRodney W. Grimes 	int ops, facs;
679df8bae1dSRodney W. Grimes 	struct vnode *vp;
680df8bae1dSRodney W. Grimes {
681df8bae1dSRodney W. Grimes 	register struct proc *p;
682df8bae1dSRodney W. Grimes 	register int ret = 0;
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes 	p = top;
6851005a129SJohn Baldwin 	sx_slock(&proctree_lock);
686df8bae1dSRodney W. Grimes 	for (;;) {
687a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
688df8bae1dSRodney W. Grimes 		/*
689df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
690df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
691df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
692df8bae1dSRodney W. Grimes 		 */
6932e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
6942e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
695df8bae1dSRodney W. Grimes 		else for (;;) {
69698f03f90SJake Burkholder 			if (p == top) {
6971005a129SJohn Baldwin 				sx_sunlock(&proctree_lock);
698df8bae1dSRodney W. Grimes 				return (ret);
69998f03f90SJake Burkholder 			}
7002e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
7012e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
702df8bae1dSRodney W. Grimes 				break;
703df8bae1dSRodney W. Grimes 			}
704b75356e1SJeffrey Hsu 			p = p->p_pptr;
705df8bae1dSRodney W. Grimes 		}
706df8bae1dSRodney W. Grimes 	}
707df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
708df8bae1dSRodney W. Grimes }
709df8bae1dSRodney W. Grimes 
71087b6de2bSPoul-Henning Kamp static void
711ea3fc8e4SJohn Baldwin ktr_writerequest(struct ktr_request *req)
712df8bae1dSRodney W. Grimes {
713ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
714ea3fc8e4SJohn Baldwin 	struct vnode *vp;
715ea3fc8e4SJohn Baldwin 	struct uio *uio = NULL;
716ea3fc8e4SJohn Baldwin 	struct proc *p;
717ea3fc8e4SJohn Baldwin 	struct thread *td;
718ea3fc8e4SJohn Baldwin 	struct ucred *cred;
719df8bae1dSRodney W. Grimes 	struct uio auio;
720ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
721f2a2857bSKirk McKusick 	struct mount *mp;
722ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
723df8bae1dSRodney W. Grimes 	int error;
724df8bae1dSRodney W. Grimes 
725ea3fc8e4SJohn Baldwin 	vp = req->ktr_vp;
726ea3fc8e4SJohn Baldwin 	/*
727ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
728ea3fc8e4SJohn Baldwin 	 * request, so just drop it.
729ea3fc8e4SJohn Baldwin 	 */
730df8bae1dSRodney W. Grimes 	if (vp == NULL)
731df8bae1dSRodney W. Grimes 		return;
732ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
733ea3fc8e4SJohn Baldwin 	datalen = data_lengths[kth->ktr_type];
734ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
735ea3fc8e4SJohn Baldwin 	cred = req->ktr_cred;
736ea3fc8e4SJohn Baldwin 	td = curthread;
737df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
738df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
739df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
740df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
741df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
742df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
743df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
744df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
745ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
746ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
747ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
748ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
749ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
750df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
751ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
752ea3fc8e4SJohn Baldwin 	}
753ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
754ea3fc8e4SJohn Baldwin 		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
755ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
756ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
757ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
758ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
759ea3fc8e4SJohn Baldwin 	} else
760ea3fc8e4SJohn Baldwin 		uio = kth->ktr_buffer;
761ea3fc8e4SJohn Baldwin 	KASSERT((uio == NULL) ^ (kth->ktr_type == KTR_GENIO),
762ea3fc8e4SJohn Baldwin 	    ("ktrace: uio and genio mismatch"));
76342ebfbf2SBrian Feldman 	if (uio != NULL)
76442ebfbf2SBrian Feldman 		kth->ktr_len += uio->uio_resid;
765ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
766f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
767b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
768ea3fc8e4SJohn Baldwin 	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
769ea3fc8e4SJohn Baldwin 	error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
77042ebfbf2SBrian Feldman 	if (error == 0 && uio != NULL) {
771ea3fc8e4SJohn Baldwin 		(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
772ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, cred);
77342ebfbf2SBrian Feldman 	}
774b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
775f2a2857bSKirk McKusick 	vn_finished_write(mp);
776ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
777ea3fc8e4SJohn Baldwin 	if (buflen != 0)
778ea3fc8e4SJohn Baldwin 		free(kth->ktr_buffer, M_KTRACE);
779df8bae1dSRodney W. Grimes 	if (!error)
780df8bae1dSRodney W. Grimes 		return;
781df8bae1dSRodney W. Grimes 	/*
782ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
783ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
784ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
785df8bae1dSRodney W. Grimes 	 */
786df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
787df8bae1dSRodney W. Grimes 	    error);
788ea3fc8e4SJohn Baldwin 	vrele_count = 0;
789ea3fc8e4SJohn Baldwin 	/*
790ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
791ea3fc8e4SJohn Baldwin 	 * system.
792ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
793ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
794ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
795ea3fc8e4SJohn Baldwin 	 */
7961005a129SJohn Baldwin 	sx_slock(&allproc_lock);
7972e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
798ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
799df8bae1dSRodney W. Grimes 		if (p->p_tracep == vp) {
800ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
801df8bae1dSRodney W. Grimes 			p->p_tracep = NULL;
802df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
803ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
804ea3fc8e4SJohn Baldwin 			vrele_count++;
805df8bae1dSRodney W. Grimes 		}
806ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
807df8bae1dSRodney W. Grimes 	}
8081005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
809ea3fc8e4SJohn Baldwin 	/*
810ea3fc8e4SJohn Baldwin 	 * Second, clear this vnode from any pending requests.
811ea3fc8e4SJohn Baldwin 	 */
812ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
813ea3fc8e4SJohn Baldwin 	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
814ea3fc8e4SJohn Baldwin 		if (req->ktr_vp == vp) {
815ea3fc8e4SJohn Baldwin 			req->ktr_vp = NULL;
816ea3fc8e4SJohn Baldwin 			vrele_count++;
817ea3fc8e4SJohn Baldwin 		}
818ea3fc8e4SJohn Baldwin 	}
819ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
820ea3fc8e4SJohn Baldwin 	mtx_lock(&Giant);
821ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
822ea3fc8e4SJohn Baldwin 		vrele(vp);
823ea3fc8e4SJohn Baldwin 	mtx_unlock(&Giant);
824df8bae1dSRodney W. Grimes }
825df8bae1dSRodney W. Grimes 
826df8bae1dSRodney W. Grimes /*
827df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
828df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
829df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
830df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
831df8bae1dSRodney W. Grimes  * so, only root may further change it.
832df8bae1dSRodney W. Grimes  */
83387b6de2bSPoul-Henning Kamp static int
834a7ff7443SJohn Baldwin ktrcanset(td, targetp)
835a7ff7443SJohn Baldwin 	struct thread *td;
836a7ff7443SJohn Baldwin 	struct proc *targetp;
837df8bae1dSRodney W. Grimes {
838df8bae1dSRodney W. Grimes 
839a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
840a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
841a7ff7443SJohn Baldwin 	    suser_cred(td->td_ucred, PRISON_ROOT))
84275c13541SPoul-Henning Kamp 		return (0);
843a0f75161SRobert Watson 
844f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
845a0f75161SRobert Watson 		return (0);
846a0f75161SRobert Watson 
847df8bae1dSRodney W. Grimes 	return (1);
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
850db6a20e2SGarrett Wollman #endif /* KTRACE */
851