xref: /freebsd/sys/kern/kern_ktrace.c (revision 35818d2e94892c19f667f9be44319c542f6b34d6)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
32c255e9dSRobert Watson  *	The Regents of the University of California.
42c255e9dSRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
52c255e9dSRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
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/kernel.h>
43ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
46ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
47033eb86eSJeff Roberson #include <sys/mount.h>
48df8bae1dSRodney W. Grimes #include <sys/namei.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50ea3fc8e4SJohn Baldwin #include <sys/proc.h>
51ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
52df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5360e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5460e15db9SDag-Erling Smørgrav #include <sys/stat.h>
55df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
561005a129SJohn Baldwin #include <sys/sx.h>
57ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
587705d4b2SDmitry Chagin #include <sys/sysent.h>
59df8bae1dSRodney W. Grimes #include <sys/syslog.h>
60ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
61df8bae1dSRodney W. Grimes 
62aed55708SRobert Watson #include <security/mac/mac_framework.h>
63aed55708SRobert Watson 
642c255e9dSRobert Watson /*
652c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
662c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
672c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
682c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
692c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
702c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
712c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
722c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
732c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
742c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
752c255e9dSRobert Watson  *
762c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
772c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
782c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
792c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
802c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
812c255e9dSRobert Watson  */
822c255e9dSRobert Watson 
83a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8455166637SPoul-Henning Kamp 
85db6a20e2SGarrett Wollman #ifdef KTRACE
86ea3fc8e4SJohn Baldwin 
87de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
88de5b1952SAlexander Leidinger 
89ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
90ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
91ea3fc8e4SJohn Baldwin #endif
92ea3fc8e4SJohn Baldwin 
93ea3fc8e4SJohn Baldwin struct ktr_request {
94ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
95d977a583SRobert Watson 	void	*ktr_buffer;
96ea3fc8e4SJohn Baldwin 	union {
977705d4b2SDmitry Chagin 		struct	ktr_proc_ctor ktr_proc_ctor;
98c601ad8eSDag-Erling Smørgrav 		struct	ktr_cap_fail ktr_cap_fail;
99ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
100ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
101ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
102ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
103ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
104*35818d2eSJohn Baldwin 		struct	ktr_fault ktr_fault;
105*35818d2eSJohn Baldwin 		struct	ktr_faultend ktr_faultend;
106ea3fc8e4SJohn Baldwin 	} ktr_data;
107ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
108ea3fc8e4SJohn Baldwin };
109ea3fc8e4SJohn Baldwin 
110ea3fc8e4SJohn Baldwin static int data_lengths[] = {
111ea3fc8e4SJohn Baldwin 	0,					/* none */
112ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
113ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
114ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
115ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
116ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
117ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
11860e15db9SDag-Erling Smørgrav 	0,					/* KTR_USER */
11960e15db9SDag-Erling Smørgrav 	0,					/* KTR_STRUCT */
120a56be37eSJohn Baldwin 	0,					/* KTR_SYSCTL */
1217705d4b2SDmitry Chagin 	sizeof(struct ktr_proc_ctor),		/* KTR_PROCCTOR */
1227705d4b2SDmitry Chagin 	0,					/* KTR_PROCDTOR */
123c601ad8eSDag-Erling Smørgrav 	sizeof(struct ktr_cap_fail),		/* KTR_CAPFAIL */
124*35818d2eSJohn Baldwin 	sizeof(struct ktr_fault),		/* KTR_FAULT */
125*35818d2eSJohn Baldwin 	sizeof(struct ktr_faultend),		/* KTR_FAULTEND */
126ea3fc8e4SJohn Baldwin };
127ea3fc8e4SJohn Baldwin 
128ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
129ea3fc8e4SJohn Baldwin 
1305ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
13112301fc3SJohn Baldwin 
1328b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
13312301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
13412301fc3SJohn Baldwin 
1358b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
13612301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
13712301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
13812301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
139ea3fc8e4SJohn Baldwin 
140ea3fc8e4SJohn Baldwin static int print_message = 1;
141d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1422c255e9dSRobert Watson static struct sx ktrace_sx;
143ea3fc8e4SJohn Baldwin 
144ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
145ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
146b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
14722ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
148ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1492c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
150d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc,
151d680caabSJohn Baldwin     struct vnode **vp);
152ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
153d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1542c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
155a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
156a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
157a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
15822ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
15998d93822SBruce Evans 
1602c255e9dSRobert Watson /*
1612c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1622c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1632c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1642c255e9dSRobert Watson  * suppressed.
1652c255e9dSRobert Watson  */
1662c255e9dSRobert Watson static void
1672c255e9dSRobert Watson ktrace_enter(struct thread *td)
1682c255e9dSRobert Watson {
1692c255e9dSRobert Watson 
1702c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1712c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1722c255e9dSRobert Watson }
1732c255e9dSRobert Watson 
1742c255e9dSRobert Watson static void
1752c255e9dSRobert Watson ktrace_exit(struct thread *td)
1762c255e9dSRobert Watson {
1772c255e9dSRobert Watson 
1782c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1792c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1802c255e9dSRobert Watson }
1812c255e9dSRobert Watson 
1822c255e9dSRobert Watson static void
1832c255e9dSRobert Watson ktrace_assert(struct thread *td)
1842c255e9dSRobert Watson {
1852c255e9dSRobert Watson 
1862c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1872c255e9dSRobert Watson }
1882c255e9dSRobert Watson 
189ea3fc8e4SJohn Baldwin static void
190ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
191df8bae1dSRodney W. Grimes {
192ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
193ea3fc8e4SJohn Baldwin 	int i;
194df8bae1dSRodney W. Grimes 
195ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1962c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
197ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
198ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
199a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
200ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
201ea3fc8e4SJohn Baldwin 	}
202ea3fc8e4SJohn Baldwin }
203ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
204ea3fc8e4SJohn Baldwin 
205ea3fc8e4SJohn Baldwin static int
206ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
207ea3fc8e4SJohn Baldwin {
208ea3fc8e4SJohn Baldwin 	struct thread *td;
2098b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
210ea3fc8e4SJohn Baldwin 	int error;
211ea3fc8e4SJohn Baldwin 
212ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
213ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
214ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2158b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
216ea3fc8e4SJohn Baldwin 	}
217ea3fc8e4SJohn Baldwin 
2188b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
219ea3fc8e4SJohn Baldwin 	if (error)
220ea3fc8e4SJohn Baldwin 		return (error);
221ea3fc8e4SJohn Baldwin 	td = curthread;
2222c255e9dSRobert Watson 	ktrace_enter(td);
223ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
224b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2252c255e9dSRobert Watson 	ktrace_exit(td);
2268b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
227ea3fc8e4SJohn Baldwin 	if (error)
228ea3fc8e4SJohn Baldwin 		return (error);
229a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
230ea3fc8e4SJohn Baldwin 		return (ENOSPC);
231ea3fc8e4SJohn Baldwin 	return (0);
232ea3fc8e4SJohn Baldwin }
23312301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
234a0c87b74SGavin Atkinson     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
235a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
236ea3fc8e4SJohn Baldwin 
2378b149b51SJohn Baldwin static u_int
238b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
239ea3fc8e4SJohn Baldwin {
240b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
241ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
242a5896914SJoseph Koshy 	int bound;
243ea3fc8e4SJohn Baldwin 
244ea3fc8e4SJohn Baldwin 	print_message = 1;
245b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
246a5896914SJoseph Koshy 	if (bound == 0)
247a5896914SJoseph Koshy 		return (ktr_requestpool);
248b4c20e5eSDmitry Chagin 	if (bound < 0) {
249b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
250ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
251a5896914SJoseph Koshy 		while (bound++ < 0) {
252ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
253ea3fc8e4SJohn Baldwin 			if (req == NULL)
254b4c20e5eSDmitry Chagin 				break;
255ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
256ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
257ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
258ea3fc8e4SJohn Baldwin 		}
259b4c20e5eSDmitry Chagin 	} else {
260ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
261b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
262a5896914SJoseph Koshy 		while (bound-- > 0) {
263ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
264a163d034SWarner Losh 			    M_WAITOK);
265b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
266ea3fc8e4SJohn Baldwin 		}
267b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
268b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
269b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
270b4c20e5eSDmitry Chagin 	}
271b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
272ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
273ea3fc8e4SJohn Baldwin }
274ea3fc8e4SJohn Baldwin 
2755ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2765ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2775ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2785ca4819dSJohn Baldwin 
279ea3fc8e4SJohn Baldwin static struct ktr_request *
28022ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
281ea3fc8e4SJohn Baldwin {
282ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
283ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
284ea3fc8e4SJohn Baldwin 	int pm;
285ea3fc8e4SJohn Baldwin 
286c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
287ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
288c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
289ea3fc8e4SJohn Baldwin 		return (NULL);
290ea3fc8e4SJohn Baldwin 	}
291ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
292ea3fc8e4SJohn Baldwin 	if (req != NULL) {
293ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
294ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
29575768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
29675768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
29775768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
29875768576SJohn Baldwin 		}
299c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
300ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
301ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3022bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3035ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3045ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
305d977a583SRobert Watson 		req->ktr_buffer = NULL;
306ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
307ea3fc8e4SJohn Baldwin 	} else {
30875768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
309ea3fc8e4SJohn Baldwin 		pm = print_message;
310ea3fc8e4SJohn Baldwin 		print_message = 0;
311ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
312ea3fc8e4SJohn Baldwin 		if (pm)
313ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
314ea3fc8e4SJohn Baldwin 	}
315ea3fc8e4SJohn Baldwin 	return (req);
316ea3fc8e4SJohn Baldwin }
317ea3fc8e4SJohn Baldwin 
3187705d4b2SDmitry Chagin static struct ktr_request *
3197705d4b2SDmitry Chagin ktr_getrequest(int type)
3207705d4b2SDmitry Chagin {
3217705d4b2SDmitry Chagin 	struct thread *td = curthread;
3227705d4b2SDmitry Chagin 	struct ktr_request *req;
3237705d4b2SDmitry Chagin 
3247705d4b2SDmitry Chagin 	ktrace_enter(td);
32522ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3267705d4b2SDmitry Chagin 	if (req == NULL)
3277705d4b2SDmitry Chagin 		ktrace_exit(td);
3287705d4b2SDmitry Chagin 
3297705d4b2SDmitry Chagin 	return (req);
3307705d4b2SDmitry Chagin }
3317705d4b2SDmitry Chagin 
3322c255e9dSRobert Watson /*
3332c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3342c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3352c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3362c255e9dSRobert Watson  * later.
3372c255e9dSRobert Watson  */
338ea3fc8e4SJohn Baldwin static void
3392c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
340ea3fc8e4SJohn Baldwin {
341ea3fc8e4SJohn Baldwin 
342ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3432c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
344ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3452c255e9dSRobert Watson }
3462c255e9dSRobert Watson 
3472c255e9dSRobert Watson /*
3482c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3492c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3502c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3512c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
352a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3532c255e9dSRobert Watson  */
3542c255e9dSRobert Watson static void
3552c255e9dSRobert Watson ktr_drain(struct thread *td)
3562c255e9dSRobert Watson {
3572c255e9dSRobert Watson 	struct ktr_request *queued_req;
3582c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3592c255e9dSRobert Watson 
3602c255e9dSRobert Watson 	ktrace_assert(td);
3612c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3622c255e9dSRobert Watson 
3632b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3642c255e9dSRobert Watson 
3652c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3662c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3672c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3682c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3692c255e9dSRobert Watson 
3702c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3712c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3722c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3732c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3742c255e9dSRobert Watson 		}
3752c255e9dSRobert Watson 	}
3762c255e9dSRobert Watson }
3772c255e9dSRobert Watson 
3782c255e9dSRobert Watson /*
3792c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3802c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3812c255e9dSRobert Watson  * been cached in the thread.
3822c255e9dSRobert Watson  */
3832c255e9dSRobert Watson static void
38422ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
3852c255e9dSRobert Watson {
3862c255e9dSRobert Watson 
3872c255e9dSRobert Watson 	ktrace_assert(td);
3882c255e9dSRobert Watson 
3892c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3902c255e9dSRobert Watson 	ktr_drain(td);
3912c255e9dSRobert Watson 	ktr_writerequest(td, req);
3922c255e9dSRobert Watson 	ktr_freerequest(req);
3932c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3942c255e9dSRobert Watson 	ktrace_exit(td);
395ea3fc8e4SJohn Baldwin }
396ea3fc8e4SJohn Baldwin 
397ea3fc8e4SJohn Baldwin static void
398ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
399ea3fc8e4SJohn Baldwin {
400ea3fc8e4SJohn Baldwin 
401d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
402d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
403d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
404d680caabSJohn Baldwin }
405d680caabSJohn Baldwin 
406d680caabSJohn Baldwin static void
407d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
408d680caabSJohn Baldwin {
409d680caabSJohn Baldwin 
410d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
411d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
412d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
413ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
414d680caabSJohn Baldwin }
415d680caabSJohn Baldwin 
416d680caabSJohn Baldwin /*
417d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
418d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
419d680caabSJohn Baldwin  * vnode and credentials.
420d680caabSJohn Baldwin  */
421d680caabSJohn Baldwin static void
422d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
423d680caabSJohn Baldwin {
424d680caabSJohn Baldwin 	struct ktr_request *req;
425d680caabSJohn Baldwin 
426d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
427d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
428d680caabSJohn Baldwin 	*uc = p->p_tracecred;
429d680caabSJohn Baldwin 	p->p_tracecred = NULL;
430d680caabSJohn Baldwin 	if (vp != NULL)
431d680caabSJohn Baldwin 		*vp = p->p_tracevp;
432d680caabSJohn Baldwin 	p->p_tracevp = NULL;
433d680caabSJohn Baldwin 	p->p_traceflag = 0;
434d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
435d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
436d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
437d680caabSJohn Baldwin 	}
438ea3fc8e4SJohn Baldwin }
439ea3fc8e4SJohn Baldwin 
44026f9a767SRodney W. Grimes void
441ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
44271ddfdbbSDmitrij Tejblum 	int code, narg;
44371ddfdbbSDmitrij Tejblum 	register_t args[];
444df8bae1dSRodney W. Grimes {
445ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
446df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
447ea3fc8e4SJohn Baldwin 	size_t buflen;
4484b3aac3dSJohn Baldwin 	char *buf = NULL;
449df8bae1dSRodney W. Grimes 
4504b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
4514b3aac3dSJohn Baldwin 	if (buflen > 0) {
452a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
4534b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
4544b3aac3dSJohn Baldwin 	}
455ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
45650c22331SPoul-Henning Kamp 	if (req == NULL) {
45750c22331SPoul-Henning Kamp 		if (buf != NULL)
45850c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
459ea3fc8e4SJohn Baldwin 		return;
46050c22331SPoul-Henning Kamp 	}
461ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
462df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
463df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
464ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
465ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
466d977a583SRobert Watson 		req->ktr_buffer = buf;
467ea3fc8e4SJohn Baldwin 	}
4682c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
469df8bae1dSRodney W. Grimes }
470df8bae1dSRodney W. Grimes 
47126f9a767SRodney W. Grimes void
472ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
47371ddfdbbSDmitrij Tejblum 	int code, error;
47471ddfdbbSDmitrij Tejblum 	register_t retval;
475df8bae1dSRodney W. Grimes {
476ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
477ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
478df8bae1dSRodney W. Grimes 
479ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
480ea3fc8e4SJohn Baldwin 	if (req == NULL)
481ea3fc8e4SJohn Baldwin 		return;
482ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
483ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
484ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
4855a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
4862c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4872c255e9dSRobert Watson }
4882c255e9dSRobert Watson 
4892c255e9dSRobert Watson /*
490d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
491d680caabSJohn Baldwin  *
492d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
493d680caabSJohn Baldwin  */
494d680caabSJohn Baldwin void
495d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
496d680caabSJohn Baldwin {
497d680caabSJohn Baldwin 
498d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
499d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
500d680caabSJohn Baldwin 	ktr_freeproc(p, uc, vp);
501d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
502d680caabSJohn Baldwin }
503d680caabSJohn Baldwin 
504d680caabSJohn Baldwin /*
505d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
506d680caabSJohn Baldwin  * and disable tracing.
5072c255e9dSRobert Watson  */
5082c255e9dSRobert Watson void
5092c255e9dSRobert Watson ktrprocexit(struct thread *td)
5102c255e9dSRobert Watson {
5117705d4b2SDmitry Chagin 	struct ktr_request *req;
512d680caabSJohn Baldwin 	struct proc *p;
513d680caabSJohn Baldwin 	struct ucred *cred;
514d680caabSJohn Baldwin 	struct vnode *vp;
515d680caabSJohn Baldwin 	int vfslocked;
516d680caabSJohn Baldwin 
517d680caabSJohn Baldwin 	p = td->td_proc;
518d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
519d680caabSJohn Baldwin 		return;
5202c255e9dSRobert Watson 
5212c255e9dSRobert Watson 	ktrace_enter(td);
52222ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
52322ec0406SDmitry Chagin 	if (req != NULL)
52422ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
5252c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
5262c255e9dSRobert Watson 	ktr_drain(td);
5272c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
528d680caabSJohn Baldwin 	PROC_LOCK(p);
529d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
530d680caabSJohn Baldwin 	ktr_freeproc(p, &cred, &vp);
531d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
532d680caabSJohn Baldwin 	PROC_UNLOCK(p);
533d680caabSJohn Baldwin 	if (vp != NULL) {
534d680caabSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
535d680caabSJohn Baldwin 		vrele(vp);
536d680caabSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
537d680caabSJohn Baldwin 	}
538d680caabSJohn Baldwin 	if (cred != NULL)
539d680caabSJohn Baldwin 		crfree(cred);
5402c255e9dSRobert Watson 	ktrace_exit(td);
5412c255e9dSRobert Watson }
5422c255e9dSRobert Watson 
5437705d4b2SDmitry Chagin static void
54422ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
5457705d4b2SDmitry Chagin {
5467705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
5477705d4b2SDmitry Chagin 	struct ktr_request *req;
548de60a5f3SDmitry Chagin 	struct thread *td2;
5497705d4b2SDmitry Chagin 
5507705d4b2SDmitry Chagin 	ktrace_assert(td);
5517705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
55222ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
5537705d4b2SDmitry Chagin 	if (req == NULL)
5547705d4b2SDmitry Chagin 		return;
5557705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
5567705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
55722ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
5587705d4b2SDmitry Chagin }
5597705d4b2SDmitry Chagin 
5607705d4b2SDmitry Chagin void
5617705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
5627705d4b2SDmitry Chagin {
5637705d4b2SDmitry Chagin 	struct thread *td = curthread;
5647705d4b2SDmitry Chagin 
5657705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
5667705d4b2SDmitry Chagin 		return;
5677705d4b2SDmitry Chagin 
5687705d4b2SDmitry Chagin 	ktrace_enter(td);
56922ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
5707705d4b2SDmitry Chagin 	ktrace_exit(td);
5717705d4b2SDmitry Chagin }
5727705d4b2SDmitry Chagin 
5732c255e9dSRobert Watson /*
574d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
575d680caabSJohn Baldwin  */
576d680caabSJohn Baldwin void
577d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
578d680caabSJohn Baldwin {
579d680caabSJohn Baldwin 
5807705d4b2SDmitry Chagin 	PROC_LOCK(p1);
581d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
582d680caabSJohn Baldwin 	KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
583d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
584d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
585d680caabSJohn Baldwin 		if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
586d680caabSJohn Baldwin 			VREF(p2->p_tracevp);
587d680caabSJohn Baldwin 			KASSERT(p1->p_tracecred != NULL,
588d680caabSJohn Baldwin 			    ("ktrace vnode with no cred"));
589d680caabSJohn Baldwin 			p2->p_tracecred = crhold(p1->p_tracecred);
590d680caabSJohn Baldwin 		}
591d680caabSJohn Baldwin 	}
592d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5937705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
5947705d4b2SDmitry Chagin 
5957705d4b2SDmitry Chagin 	ktrprocctor(p2);
596d680caabSJohn Baldwin }
597d680caabSJohn Baldwin 
598d680caabSJohn Baldwin /*
5992c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
6002c255e9dSRobert Watson  * system call.
6012c255e9dSRobert Watson  */
6022c255e9dSRobert Watson void
6032c255e9dSRobert Watson ktruserret(struct thread *td)
6042c255e9dSRobert Watson {
6052c255e9dSRobert Watson 
6062c255e9dSRobert Watson 	ktrace_enter(td);
6072c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6082c255e9dSRobert Watson 	ktr_drain(td);
6092c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6102c255e9dSRobert Watson 	ktrace_exit(td);
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
61326f9a767SRodney W. Grimes void
614ea3fc8e4SJohn Baldwin ktrnamei(path)
615df8bae1dSRodney W. Grimes 	char *path;
616df8bae1dSRodney W. Grimes {
617ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
618ea3fc8e4SJohn Baldwin 	int namelen;
6194b3aac3dSJohn Baldwin 	char *buf = NULL;
620df8bae1dSRodney W. Grimes 
6214b3aac3dSJohn Baldwin 	namelen = strlen(path);
6224b3aac3dSJohn Baldwin 	if (namelen > 0) {
623a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
6244b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
6254b3aac3dSJohn Baldwin 	}
626ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
62750c22331SPoul-Henning Kamp 	if (req == NULL) {
62850c22331SPoul-Henning Kamp 		if (buf != NULL)
62950c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
630ea3fc8e4SJohn Baldwin 		return;
63150c22331SPoul-Henning Kamp 	}
632ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
633ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
634d977a583SRobert Watson 		req->ktr_buffer = buf;
635ea3fc8e4SJohn Baldwin 	}
6362c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
63926f9a767SRodney W. Grimes void
640a56be37eSJohn Baldwin ktrsysctl(name, namelen)
641a56be37eSJohn Baldwin 	int *name;
642a56be37eSJohn Baldwin 	u_int namelen;
643a56be37eSJohn Baldwin {
644a56be37eSJohn Baldwin 	struct ktr_request *req;
645a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
646a56be37eSJohn Baldwin 	char *mibname;
647a56be37eSJohn Baldwin 	size_t mibnamelen;
648a56be37eSJohn Baldwin 	int error;
649a56be37eSJohn Baldwin 
650a56be37eSJohn Baldwin 	/* Lookup name of mib. */
651a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
652a56be37eSJohn Baldwin 	mib[0] = 0;
653a56be37eSJohn Baldwin 	mib[1] = 1;
654a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
655a56be37eSJohn Baldwin 	mibnamelen = 128;
656a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
657a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
658a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
659a56be37eSJohn Baldwin 	if (error) {
660a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
661a56be37eSJohn Baldwin 		return;
662a56be37eSJohn Baldwin 	}
663a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
664a56be37eSJohn Baldwin 	if (req == NULL) {
665a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
666a56be37eSJohn Baldwin 		return;
667a56be37eSJohn Baldwin 	}
668a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
669a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
670a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
671a56be37eSJohn Baldwin }
672a56be37eSJohn Baldwin 
673a56be37eSJohn Baldwin void
674ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
675df8bae1dSRodney W. Grimes 	int fd;
676df8bae1dSRodney W. Grimes 	enum uio_rw rw;
67742ebfbf2SBrian Feldman 	struct uio *uio;
67842ebfbf2SBrian Feldman 	int error;
679df8bae1dSRodney W. Grimes {
680ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
681ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
682b92584a6SJohn Baldwin 	int datalen;
683b92584a6SJohn Baldwin 	char *buf;
684df8bae1dSRodney W. Grimes 
685552afd9cSPoul-Henning Kamp 	if (error) {
686552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
687df8bae1dSRodney W. Grimes 		return;
688552afd9cSPoul-Henning Kamp 	}
689b92584a6SJohn Baldwin 	uio->uio_offset = 0;
690b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
691526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
692a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
693552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
694552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
695552afd9cSPoul-Henning Kamp 	if (error) {
696b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
697ea3fc8e4SJohn Baldwin 		return;
698b92584a6SJohn Baldwin 	}
699b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
700b92584a6SJohn Baldwin 	if (req == NULL) {
701b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
702b92584a6SJohn Baldwin 		return;
703b92584a6SJohn Baldwin 	}
704ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
705ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
706ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
707b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
708d977a583SRobert Watson 	req->ktr_buffer = buf;
7092c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
710df8bae1dSRodney W. Grimes }
711df8bae1dSRodney W. Grimes 
71226f9a767SRodney W. Grimes void
713ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
714a93fdaacSMarcel Moolenaar 	int sig;
715df8bae1dSRodney W. Grimes 	sig_t action;
7162c42a146SMarcel Moolenaar 	sigset_t *mask;
717a93fdaacSMarcel Moolenaar 	int code;
718df8bae1dSRodney W. Grimes {
71922ec0406SDmitry Chagin 	struct thread *td = curthread;
720ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
721ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
722df8bae1dSRodney W. Grimes 
723ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
724ea3fc8e4SJohn Baldwin 	if (req == NULL)
725ea3fc8e4SJohn Baldwin 		return;
726ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
727ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
728ea3fc8e4SJohn Baldwin 	kp->action = action;
729ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
730ea3fc8e4SJohn Baldwin 	kp->code = code;
73122ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
73222ec0406SDmitry Chagin 	ktrace_exit(td);
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
73526f9a767SRodney W. Grimes void
736ea3fc8e4SJohn Baldwin ktrcsw(out, user)
737df8bae1dSRodney W. Grimes 	int out, user;
738df8bae1dSRodney W. Grimes {
73922ec0406SDmitry Chagin 	struct thread *td = curthread;
740ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
741ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
742df8bae1dSRodney W. Grimes 
743ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
744ea3fc8e4SJohn Baldwin 	if (req == NULL)
745ea3fc8e4SJohn Baldwin 		return;
746ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
747ea3fc8e4SJohn Baldwin 	kc->out = out;
748ea3fc8e4SJohn Baldwin 	kc->user = user;
74922ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
75022ec0406SDmitry Chagin 	ktrace_exit(td);
751df8bae1dSRodney W. Grimes }
75260e15db9SDag-Erling Smørgrav 
75360e15db9SDag-Erling Smørgrav void
754a3052d6eSJohn Baldwin ktrstruct(name, data, datalen)
75560e15db9SDag-Erling Smørgrav 	const char *name;
75660e15db9SDag-Erling Smørgrav 	void *data;
75760e15db9SDag-Erling Smørgrav 	size_t datalen;
75860e15db9SDag-Erling Smørgrav {
75960e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
76060e15db9SDag-Erling Smørgrav 	char *buf = NULL;
76160e15db9SDag-Erling Smørgrav 	size_t buflen;
76260e15db9SDag-Erling Smørgrav 
76360e15db9SDag-Erling Smørgrav 	if (!data)
76460e15db9SDag-Erling Smørgrav 		datalen = 0;
765a3052d6eSJohn Baldwin 	buflen = strlen(name) + 1 + datalen;
76660e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
767a3052d6eSJohn Baldwin 	strcpy(buf, name);
768a3052d6eSJohn Baldwin 	bcopy(data, buf + strlen(name) + 1, datalen);
76960e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
77060e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
77160e15db9SDag-Erling Smørgrav 		return;
77260e15db9SDag-Erling Smørgrav 	}
77360e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
77460e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
77560e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
77660e15db9SDag-Erling Smørgrav }
777c601ad8eSDag-Erling Smørgrav 
778c601ad8eSDag-Erling Smørgrav void
779e141be6fSDag-Erling Smørgrav ktrcapfail(type, needed, held)
780e141be6fSDag-Erling Smørgrav 	enum ktr_cap_fail_type type;
781c601ad8eSDag-Erling Smørgrav 	cap_rights_t needed;
782c601ad8eSDag-Erling Smørgrav 	cap_rights_t held;
783c601ad8eSDag-Erling Smørgrav {
784c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
785c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
786c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
787c601ad8eSDag-Erling Smørgrav 
788c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
789c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
790c601ad8eSDag-Erling Smørgrav 		return;
791c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
792e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
793c601ad8eSDag-Erling Smørgrav 	kcf->cap_needed = needed;
794c601ad8eSDag-Erling Smørgrav 	kcf->cap_held = held;
795c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
796c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
797c601ad8eSDag-Erling Smørgrav }
798*35818d2eSJohn Baldwin 
799*35818d2eSJohn Baldwin void
800*35818d2eSJohn Baldwin ktrfault(vaddr, type)
801*35818d2eSJohn Baldwin 	vm_offset_t vaddr;
802*35818d2eSJohn Baldwin 	int type;
803*35818d2eSJohn Baldwin {
804*35818d2eSJohn Baldwin 	struct thread *td = curthread;
805*35818d2eSJohn Baldwin 	struct ktr_request *req;
806*35818d2eSJohn Baldwin 	struct ktr_fault *kf;
807*35818d2eSJohn Baldwin 
808*35818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
809*35818d2eSJohn Baldwin 	if (req == NULL)
810*35818d2eSJohn Baldwin 		return;
811*35818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
812*35818d2eSJohn Baldwin 	kf->vaddr = vaddr;
813*35818d2eSJohn Baldwin 	kf->type = type;
814*35818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
815*35818d2eSJohn Baldwin 	ktrace_exit(td);
816*35818d2eSJohn Baldwin }
817*35818d2eSJohn Baldwin 
818*35818d2eSJohn Baldwin void
819*35818d2eSJohn Baldwin ktrfaultend(result)
820*35818d2eSJohn Baldwin 	int result;
821*35818d2eSJohn Baldwin {
822*35818d2eSJohn Baldwin 	struct thread *td = curthread;
823*35818d2eSJohn Baldwin 	struct ktr_request *req;
824*35818d2eSJohn Baldwin 	struct ktr_faultend *kf;
825*35818d2eSJohn Baldwin 
826*35818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
827*35818d2eSJohn Baldwin 	if (req == NULL)
828*35818d2eSJohn Baldwin 		return;
829*35818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
830*35818d2eSJohn Baldwin 	kf->result = result;
831*35818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
832*35818d2eSJohn Baldwin 	ktrace_exit(td);
833*35818d2eSJohn Baldwin }
83464cc6a13SJohn Baldwin #endif /* KTRACE */
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes /* Interface and common routines */
837df8bae1dSRodney W. Grimes 
838d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
839df8bae1dSRodney W. Grimes struct ktrace_args {
840df8bae1dSRodney W. Grimes 	char	*fname;
841df8bae1dSRodney W. Grimes 	int	ops;
842df8bae1dSRodney W. Grimes 	int	facs;
843df8bae1dSRodney W. Grimes 	int	pid;
844df8bae1dSRodney W. Grimes };
845d2d3e875SBruce Evans #endif
846df8bae1dSRodney W. Grimes /* ARGSUSED */
84726f9a767SRodney W. Grimes int
8488451d0ddSKip Macy sys_ktrace(td, uap)
849b40ce416SJulian Elischer 	struct thread *td;
850df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
851df8bae1dSRodney W. Grimes {
852db6a20e2SGarrett Wollman #ifdef KTRACE
853df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
854df8bae1dSRodney W. Grimes 	register struct proc *p;
855df8bae1dSRodney W. Grimes 	struct pgrp *pg;
856df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
857df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
858df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
859400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
86033f19beeSJohn Baldwin 	int flags, error = 0, vfslocked;
861df8bae1dSRodney W. Grimes 	struct nameidata nd;
862a5881ea5SJohn Baldwin 	struct ucred *cred;
863df8bae1dSRodney W. Grimes 
86464cc6a13SJohn Baldwin 	/*
86564cc6a13SJohn Baldwin 	 * Need something to (un)trace.
86664cc6a13SJohn Baldwin 	 */
86764cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
86864cc6a13SJohn Baldwin 		return (EINVAL);
86964cc6a13SJohn Baldwin 
8702c255e9dSRobert Watson 	ktrace_enter(td);
871df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
872df8bae1dSRodney W. Grimes 		/*
873df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
874df8bae1dSRodney W. Grimes 		 */
87533f19beeSJohn Baldwin 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
87633f19beeSJohn Baldwin 		    uap->fname, td);
877e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
8789e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
879797f2d22SPoul-Henning Kamp 		if (error) {
8802c255e9dSRobert Watson 			ktrace_exit(td);
881df8bae1dSRodney W. Grimes 			return (error);
882df8bae1dSRodney W. Grimes 		}
88333f19beeSJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
884762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
885df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
88622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
887df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
888a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
88933f19beeSJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
8902c255e9dSRobert Watson 			ktrace_exit(td);
891df8bae1dSRodney W. Grimes 			return (EACCES);
892df8bae1dSRodney W. Grimes 		}
89333f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
894df8bae1dSRodney W. Grimes 	}
895df8bae1dSRodney W. Grimes 	/*
89679deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
897df8bae1dSRodney W. Grimes 	 */
898df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
89951fd6380SMike Pritchard 		int vrele_count;
90051fd6380SMike Pritchard 
90151fd6380SMike Pritchard 		vrele_count = 0;
9021005a129SJohn Baldwin 		sx_slock(&allproc_lock);
9034f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
904a7ff7443SJohn Baldwin 			PROC_LOCK(p);
905a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
906ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
907ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
908d680caabSJohn Baldwin 					ktr_freeproc(p, &cred, NULL);
909ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
91051fd6380SMike Pritchard 					vrele_count++;
911a5881ea5SJohn Baldwin 					crfree(cred);
91251fd6380SMike Pritchard 				} else
913df8bae1dSRodney W. Grimes 					error = EPERM;
914df8bae1dSRodney W. Grimes 			}
915a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
91679deba82SMatthew Dillon 		}
9171005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
91851fd6380SMike Pritchard 		if (vrele_count > 0) {
91951fd6380SMike Pritchard 			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
92051fd6380SMike Pritchard 			while (vrele_count-- > 0)
92151fd6380SMike Pritchard 				vrele(vp);
92251fd6380SMike Pritchard 			VFS_UNLOCK_GIANT(vfslocked);
92351fd6380SMike Pritchard 		}
924df8bae1dSRodney W. Grimes 		goto done;
925df8bae1dSRodney W. Grimes 	}
926df8bae1dSRodney W. Grimes 	/*
927df8bae1dSRodney W. Grimes 	 * do it
928df8bae1dSRodney W. Grimes 	 */
92964cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
930df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
931df8bae1dSRodney W. Grimes 		/*
932df8bae1dSRodney W. Grimes 		 * by process group
933df8bae1dSRodney W. Grimes 		 */
934df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
935df8bae1dSRodney W. Grimes 		if (pg == NULL) {
936ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
937df8bae1dSRodney W. Grimes 			error = ESRCH;
938df8bae1dSRodney W. Grimes 			goto done;
939df8bae1dSRodney W. Grimes 		}
940f591779bSSeigo Tanimura 		/*
941f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
942ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
943f591779bSSeigo Tanimura 		 */
944f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
945400a74bfSPawel Jakub Dawidek 		nfound = 0;
946400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
947400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
948e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW ||
949e806d352SJohn Baldwin 			    p_cansee(td, p) != 0) {
950400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
951400a74bfSPawel Jakub Dawidek 				continue;
952400a74bfSPawel Jakub Dawidek 			}
953400a74bfSPawel Jakub Dawidek 			nfound++;
954df8bae1dSRodney W. Grimes 			if (descend)
955a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
956df8bae1dSRodney W. Grimes 			else
957a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
958400a74bfSPawel Jakub Dawidek 		}
959400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
960400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
961400a74bfSPawel Jakub Dawidek 			error = ESRCH;
962400a74bfSPawel Jakub Dawidek 			goto done;
963400a74bfSPawel Jakub Dawidek 		}
964df8bae1dSRodney W. Grimes 	} else {
965df8bae1dSRodney W. Grimes 		/*
966df8bae1dSRodney W. Grimes 		 * by pid
967df8bae1dSRodney W. Grimes 		 */
968df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
969fe41d17aSJohn Baldwin 		if (p == NULL)
970df8bae1dSRodney W. Grimes 			error = ESRCH;
971fe41d17aSJohn Baldwin 		else
9724eb7c9f6SPawel Jakub Dawidek 			error = p_cansee(td, p);
973b0d9aeddSPawel Jakub Dawidek 		if (error) {
974fe41d17aSJohn Baldwin 			if (p != NULL)
975fe41d17aSJohn Baldwin 				PROC_UNLOCK(p);
976b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
9774eb7c9f6SPawel Jakub Dawidek 			goto done;
978b0d9aeddSPawel Jakub Dawidek 		}
979df8bae1dSRodney W. Grimes 		if (descend)
980a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
981df8bae1dSRodney W. Grimes 		else
982a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
983df8bae1dSRodney W. Grimes 	}
98464cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
985df8bae1dSRodney W. Grimes 	if (!ret)
986df8bae1dSRodney W. Grimes 		error = EPERM;
987df8bae1dSRodney W. Grimes done:
98864cc6a13SJohn Baldwin 	if (vp != NULL) {
98933f19beeSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
990a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
99133f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
99264cc6a13SJohn Baldwin 	}
9932c255e9dSRobert Watson 	ktrace_exit(td);
994df8bae1dSRodney W. Grimes 	return (error);
99564cc6a13SJohn Baldwin #else /* !KTRACE */
99664cc6a13SJohn Baldwin 	return (ENOSYS);
99764cc6a13SJohn Baldwin #endif /* KTRACE */
998df8bae1dSRodney W. Grimes }
999df8bae1dSRodney W. Grimes 
1000e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1001e6c4b9baSPoul-Henning Kamp int
10028451d0ddSKip Macy sys_utrace(td, uap)
1003b40ce416SJulian Elischer 	struct thread *td;
1004e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
1005e6c4b9baSPoul-Henning Kamp {
1006b40ce416SJulian Elischer 
1007e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1008ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
10097f05b035SAlfred Perlstein 	void *cp;
1010c9e7d28eSJohn Baldwin 	int error;
1011e6c4b9baSPoul-Henning Kamp 
1012c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1013c9e7d28eSJohn Baldwin 		return (0);
1014bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
10150bad156aSAlfred Perlstein 		return (EINVAL);
1016a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1017c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
101850c22331SPoul-Henning Kamp 	if (error) {
101950c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1020c9e7d28eSJohn Baldwin 		return (error);
102150c22331SPoul-Henning Kamp 	}
1022ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
102350c22331SPoul-Henning Kamp 	if (req == NULL) {
102450c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1025b10221ffSJoseph Koshy 		return (ENOMEM);
102650c22331SPoul-Henning Kamp 	}
1027d977a583SRobert Watson 	req->ktr_buffer = cp;
1028ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
10292c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1030e6c4b9baSPoul-Henning Kamp 	return (0);
103164cc6a13SJohn Baldwin #else /* !KTRACE */
1032e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
103364cc6a13SJohn Baldwin #endif /* KTRACE */
1034e6c4b9baSPoul-Henning Kamp }
1035e6c4b9baSPoul-Henning Kamp 
1036db6a20e2SGarrett Wollman #ifdef KTRACE
103787b6de2bSPoul-Henning Kamp static int
1038a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
1039a7ff7443SJohn Baldwin 	struct thread *td;
1040a7ff7443SJohn Baldwin 	struct proc *p;
1041df8bae1dSRodney W. Grimes 	int ops, facs;
1042df8bae1dSRodney W. Grimes 	struct vnode *vp;
1043df8bae1dSRodney W. Grimes {
1044ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
1045a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
1046df8bae1dSRodney W. Grimes 
1047fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1048a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1049a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1050df8bae1dSRodney W. Grimes 		return (0);
1051a7ff7443SJohn Baldwin 	}
1052fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1053fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1054fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1055fe41d17aSJohn Baldwin 		return (1);
1056fe41d17aSJohn Baldwin 	}
1057ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1058df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
1059a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
1060df8bae1dSRodney W. Grimes 			/*
1061a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
1062df8bae1dSRodney W. Grimes 			 */
1063a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
1064ea3fc8e4SJohn Baldwin 			VREF(vp);
1065a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
1066a5881ea5SJohn Baldwin 		}
1067a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
1068a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
1069a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
1070df8bae1dSRodney W. Grimes 		}
1071df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
107232f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1073df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1074df8bae1dSRodney W. Grimes 	} else {
1075df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1076d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1077df8bae1dSRodney W. Grimes 			/* no more tracing */
1078d680caabSJohn Baldwin 			ktr_freeproc(p, &tracecred, &tracevp);
1079a7ff7443SJohn Baldwin 	}
1080ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
108122ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
108222ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1083a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
108464cc6a13SJohn Baldwin 	if (tracevp != NULL) {
1085033eb86eSJeff Roberson 		int vfslocked;
1086033eb86eSJeff Roberson 
1087033eb86eSJeff Roberson 		vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1088ea3fc8e4SJohn Baldwin 		vrele(tracevp);
1089033eb86eSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
109064cc6a13SJohn Baldwin 	}
1091a5881ea5SJohn Baldwin 	if (tracecred != NULL)
1092a5881ea5SJohn Baldwin 		crfree(tracecred);
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 	return (1);
1095df8bae1dSRodney W. Grimes }
1096df8bae1dSRodney W. Grimes 
109787b6de2bSPoul-Henning Kamp static int
1098a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
1099a7ff7443SJohn Baldwin 	struct thread *td;
1100a7ff7443SJohn Baldwin 	struct proc *top;
1101df8bae1dSRodney W. Grimes 	int ops, facs;
1102df8bae1dSRodney W. Grimes 	struct vnode *vp;
1103df8bae1dSRodney W. Grimes {
1104df8bae1dSRodney W. Grimes 	register struct proc *p;
1105df8bae1dSRodney W. Grimes 	register int ret = 0;
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 	p = top;
1108fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
110964cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1110df8bae1dSRodney W. Grimes 	for (;;) {
1111a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
1112df8bae1dSRodney W. Grimes 		/*
1113df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1114df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1115df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1116df8bae1dSRodney W. Grimes 		 */
11172e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
11182e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1119df8bae1dSRodney W. Grimes 		else for (;;) {
112064cc6a13SJohn Baldwin 			if (p == top)
1121df8bae1dSRodney W. Grimes 				return (ret);
11222e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
11232e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1124df8bae1dSRodney W. Grimes 				break;
1125df8bae1dSRodney W. Grimes 			}
1126b75356e1SJeffrey Hsu 			p = p->p_pptr;
1127df8bae1dSRodney W. Grimes 		}
1128fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1129df8bae1dSRodney W. Grimes 	}
1130df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1131df8bae1dSRodney W. Grimes }
1132df8bae1dSRodney W. Grimes 
113387b6de2bSPoul-Henning Kamp static void
11342c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1135df8bae1dSRodney W. Grimes {
1136ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1137ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1138ea3fc8e4SJohn Baldwin 	struct proc *p;
1139ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1140df8bae1dSRodney W. Grimes 	struct uio auio;
1141ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1142f2a2857bSKirk McKusick 	struct mount *mp;
1143ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
114433f19beeSJohn Baldwin 	int error, vfslocked;
1145df8bae1dSRodney W. Grimes 
11462c255e9dSRobert Watson 	/*
11472c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
11482c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
11492c255e9dSRobert Watson 	 *
11502c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
11512c255e9dSRobert Watson 	 * the vnode has been closed.
11522c255e9dSRobert Watson 	 */
11532c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
11542c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
11552c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
11562c255e9dSRobert Watson 
1157ea3fc8e4SJohn Baldwin 	/*
1158ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
11592c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
11602c255e9dSRobert Watson 	 * in sync: we should have both or neither.
1161ea3fc8e4SJohn Baldwin 	 */
11622c255e9dSRobert Watson 	if (vp == NULL) {
11632c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1164118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1165df8bae1dSRodney W. Grimes 		return;
11662c255e9dSRobert Watson 	}
1167118258f5SBjoern A. Zeeb 	VREF(vp);
11682c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1169118258f5SBjoern A. Zeeb 	crhold(cred);
1170118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
11712c255e9dSRobert Watson 
1172ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
1173a56be37eSJohn Baldwin 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1174a56be37eSJohn Baldwin 	    sizeof(data_lengths) / sizeof(data_lengths[0]),
1175a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
11768b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1177ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1178df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1179df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1180df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1181df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1182df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1183df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1184df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1185df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1186ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1187ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1188ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1189ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1190ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1191df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1192ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1193ea3fc8e4SJohn Baldwin 	}
1194ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1195d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1196d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1197ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1198ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1199ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1200b92584a6SJohn Baldwin 	}
12012c255e9dSRobert Watson 
120233f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1203f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1204cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1205467a273cSRobert Watson #ifdef MAC
120630d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1207467a273cSRobert Watson 	if (error == 0)
1208467a273cSRobert Watson #endif
1209ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
121022db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
1211f2a2857bSKirk McKusick 	vn_finished_write(mp);
1212118258f5SBjoern A. Zeeb 	crfree(cred);
1213118258f5SBjoern A. Zeeb 	if (!error) {
1214704c9f00SJohn Baldwin 		vrele(vp);
121533f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
1216df8bae1dSRodney W. Grimes 		return;
1217118258f5SBjoern A. Zeeb 	}
1218118258f5SBjoern A. Zeeb 	VFS_UNLOCK_GIANT(vfslocked);
1219118258f5SBjoern A. Zeeb 
1220df8bae1dSRodney W. Grimes 	/*
1221ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1222ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1223ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1224118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1225118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1226118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1227118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1228df8bae1dSRodney W. Grimes 	 */
1229df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1230df8bae1dSRodney W. Grimes 	    error);
1231118258f5SBjoern A. Zeeb 	vrele_count = 1;
1232ea3fc8e4SJohn Baldwin 	/*
1233ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1234ea3fc8e4SJohn Baldwin 	 * system.
1235ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1236ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1237ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1238ea3fc8e4SJohn Baldwin 	 */
1239a5881ea5SJohn Baldwin 	cred = NULL;
12401005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12414f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1242ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1243a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1244ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1245d680caabSJohn Baldwin 			ktr_freeproc(p, &cred, NULL);
1246ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1247ea3fc8e4SJohn Baldwin 			vrele_count++;
1248df8bae1dSRodney W. Grimes 		}
1249ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1250a5881ea5SJohn Baldwin 		if (cred != NULL) {
1251a5881ea5SJohn Baldwin 			crfree(cred);
1252a5881ea5SJohn Baldwin 			cred = NULL;
1253a5881ea5SJohn Baldwin 		}
1254df8bae1dSRodney W. Grimes 	}
12551005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12562c255e9dSRobert Watson 
125733f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1258ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1259ea3fc8e4SJohn Baldwin 		vrele(vp);
126033f19beeSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
1261df8bae1dSRodney W. Grimes }
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes /*
1264df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1265df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1266df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1267df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1268df8bae1dSRodney W. Grimes  * so, only root may further change it.
1269df8bae1dSRodney W. Grimes  */
127087b6de2bSPoul-Henning Kamp static int
1271a7ff7443SJohn Baldwin ktrcanset(td, targetp)
1272a7ff7443SJohn Baldwin 	struct thread *td;
1273a7ff7443SJohn Baldwin 	struct proc *targetp;
1274df8bae1dSRodney W. Grimes {
1275df8bae1dSRodney W. Grimes 
1276a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1277a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
127832f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
127975c13541SPoul-Henning Kamp 		return (0);
1280a0f75161SRobert Watson 
1281f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1282a0f75161SRobert Watson 		return (0);
1283a0f75161SRobert Watson 
1284df8bae1dSRodney W. Grimes 	return (1);
1285df8bae1dSRodney W. Grimes }
1286df8bae1dSRodney W. Grimes 
1287db6a20e2SGarrett Wollman #endif /* KTRACE */
1288