xref: /freebsd/sys/kern/kern_ktrace.c (revision 5a01b726729eeb8b0397a804fc9830f39a2f032c)
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;
104ea3fc8e4SJohn Baldwin 	} ktr_data;
105ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
106ea3fc8e4SJohn Baldwin };
107ea3fc8e4SJohn Baldwin 
108ea3fc8e4SJohn Baldwin static int data_lengths[] = {
109ea3fc8e4SJohn Baldwin 	0,					/* none */
110ea3fc8e4SJohn Baldwin 	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
111ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
112ea3fc8e4SJohn Baldwin 	0,					/* KTR_NAMEI */
113ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_genio),		/* KTR_GENIO */
114ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_psig),		/* KTR_PSIG */
115ea3fc8e4SJohn Baldwin 	sizeof(struct ktr_csw),			/* KTR_CSW */
11660e15db9SDag-Erling Smørgrav 	0,					/* KTR_USER */
11760e15db9SDag-Erling Smørgrav 	0,					/* KTR_STRUCT */
118a56be37eSJohn Baldwin 	0,					/* KTR_SYSCTL */
1197705d4b2SDmitry Chagin 	sizeof(struct ktr_proc_ctor),		/* KTR_PROCCTOR */
1207705d4b2SDmitry Chagin 	0,					/* KTR_PROCDTOR */
121c601ad8eSDag-Erling Smørgrav 	sizeof(struct ktr_cap_fail),		/* KTR_CAPFAIL */
122ea3fc8e4SJohn Baldwin };
123ea3fc8e4SJohn Baldwin 
124ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
125ea3fc8e4SJohn Baldwin 
1265ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
12712301fc3SJohn Baldwin 
1288b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
12912301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
13012301fc3SJohn Baldwin 
1318b149b51SJohn Baldwin static u_int ktr_geniosize = PAGE_SIZE;
13212301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
13312301fc3SJohn Baldwin SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
13412301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
135ea3fc8e4SJohn Baldwin 
136ea3fc8e4SJohn Baldwin static int print_message = 1;
137d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1382c255e9dSRobert Watson static struct sx ktrace_sx;
139ea3fc8e4SJohn Baldwin 
140ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
141ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
142b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
14322ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
144ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1452c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
146d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc,
147d680caabSJohn Baldwin     struct vnode **vp);
148ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
149d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1502c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
151a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
152a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
153a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
15422ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
15598d93822SBruce Evans 
1562c255e9dSRobert Watson /*
1572c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1582c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1592c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1602c255e9dSRobert Watson  * suppressed.
1612c255e9dSRobert Watson  */
1622c255e9dSRobert Watson static void
1632c255e9dSRobert Watson ktrace_enter(struct thread *td)
1642c255e9dSRobert Watson {
1652c255e9dSRobert Watson 
1662c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1672c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1682c255e9dSRobert Watson }
1692c255e9dSRobert Watson 
1702c255e9dSRobert Watson static void
1712c255e9dSRobert Watson ktrace_exit(struct thread *td)
1722c255e9dSRobert Watson {
1732c255e9dSRobert Watson 
1742c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1752c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1762c255e9dSRobert Watson }
1772c255e9dSRobert Watson 
1782c255e9dSRobert Watson static void
1792c255e9dSRobert Watson ktrace_assert(struct thread *td)
1802c255e9dSRobert Watson {
1812c255e9dSRobert Watson 
1822c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1832c255e9dSRobert Watson }
1842c255e9dSRobert Watson 
185ea3fc8e4SJohn Baldwin static void
186ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
187df8bae1dSRodney W. Grimes {
188ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
189ea3fc8e4SJohn Baldwin 	int i;
190df8bae1dSRodney W. Grimes 
191ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1922c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
193ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
194ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
195a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
196ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
197ea3fc8e4SJohn Baldwin 	}
198ea3fc8e4SJohn Baldwin }
199ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
200ea3fc8e4SJohn Baldwin 
201ea3fc8e4SJohn Baldwin static int
202ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
203ea3fc8e4SJohn Baldwin {
204ea3fc8e4SJohn Baldwin 	struct thread *td;
2058b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
206ea3fc8e4SJohn Baldwin 	int error;
207ea3fc8e4SJohn Baldwin 
208ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
209ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
210ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2118b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
212ea3fc8e4SJohn Baldwin 	}
213ea3fc8e4SJohn Baldwin 
2148b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
215ea3fc8e4SJohn Baldwin 	if (error)
216ea3fc8e4SJohn Baldwin 		return (error);
217ea3fc8e4SJohn Baldwin 	td = curthread;
2182c255e9dSRobert Watson 	ktrace_enter(td);
219ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
220b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2212c255e9dSRobert Watson 	ktrace_exit(td);
2228b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
223ea3fc8e4SJohn Baldwin 	if (error)
224ea3fc8e4SJohn Baldwin 		return (error);
225a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
226ea3fc8e4SJohn Baldwin 		return (ENOSPC);
227ea3fc8e4SJohn Baldwin 	return (0);
228ea3fc8e4SJohn Baldwin }
22912301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
230a0c87b74SGavin Atkinson     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
231a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
232ea3fc8e4SJohn Baldwin 
2338b149b51SJohn Baldwin static u_int
234b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
235ea3fc8e4SJohn Baldwin {
236b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
237ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
238a5896914SJoseph Koshy 	int bound;
239ea3fc8e4SJohn Baldwin 
240ea3fc8e4SJohn Baldwin 	print_message = 1;
241b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
242a5896914SJoseph Koshy 	if (bound == 0)
243a5896914SJoseph Koshy 		return (ktr_requestpool);
244b4c20e5eSDmitry Chagin 	if (bound < 0) {
245b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
246ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
247a5896914SJoseph Koshy 		while (bound++ < 0) {
248ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
249ea3fc8e4SJohn Baldwin 			if (req == NULL)
250b4c20e5eSDmitry Chagin 				break;
251ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
252ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
253ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
254ea3fc8e4SJohn Baldwin 		}
255b4c20e5eSDmitry Chagin 	} else {
256ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
257b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
258a5896914SJoseph Koshy 		while (bound-- > 0) {
259ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
260a163d034SWarner Losh 			    M_WAITOK);
261b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
262ea3fc8e4SJohn Baldwin 		}
263b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
264b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
265b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
266b4c20e5eSDmitry Chagin 	}
267b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
268ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
269ea3fc8e4SJohn Baldwin }
270ea3fc8e4SJohn Baldwin 
2715ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2725ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2735ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2745ca4819dSJohn Baldwin 
275ea3fc8e4SJohn Baldwin static struct ktr_request *
27622ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
277ea3fc8e4SJohn Baldwin {
278ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
279ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
280ea3fc8e4SJohn Baldwin 	int pm;
281ea3fc8e4SJohn Baldwin 
282c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
283ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
284c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
285ea3fc8e4SJohn Baldwin 		return (NULL);
286ea3fc8e4SJohn Baldwin 	}
287ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
288ea3fc8e4SJohn Baldwin 	if (req != NULL) {
289ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
290ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
29175768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
29275768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
29375768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
29475768576SJohn Baldwin 		}
295c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
296ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
297ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
2982bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
2995ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3005ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
301d977a583SRobert Watson 		req->ktr_buffer = NULL;
302ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
303ea3fc8e4SJohn Baldwin 	} else {
30475768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
305ea3fc8e4SJohn Baldwin 		pm = print_message;
306ea3fc8e4SJohn Baldwin 		print_message = 0;
307ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
308ea3fc8e4SJohn Baldwin 		if (pm)
309ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
310ea3fc8e4SJohn Baldwin 	}
311ea3fc8e4SJohn Baldwin 	return (req);
312ea3fc8e4SJohn Baldwin }
313ea3fc8e4SJohn Baldwin 
3147705d4b2SDmitry Chagin static struct ktr_request *
3157705d4b2SDmitry Chagin ktr_getrequest(int type)
3167705d4b2SDmitry Chagin {
3177705d4b2SDmitry Chagin 	struct thread *td = curthread;
3187705d4b2SDmitry Chagin 	struct ktr_request *req;
3197705d4b2SDmitry Chagin 
3207705d4b2SDmitry Chagin 	ktrace_enter(td);
32122ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3227705d4b2SDmitry Chagin 	if (req == NULL)
3237705d4b2SDmitry Chagin 		ktrace_exit(td);
3247705d4b2SDmitry Chagin 
3257705d4b2SDmitry Chagin 	return (req);
3267705d4b2SDmitry Chagin }
3277705d4b2SDmitry Chagin 
3282c255e9dSRobert Watson /*
3292c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3302c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3312c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3322c255e9dSRobert Watson  * later.
3332c255e9dSRobert Watson  */
334ea3fc8e4SJohn Baldwin static void
3352c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
336ea3fc8e4SJohn Baldwin {
337ea3fc8e4SJohn Baldwin 
338ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3392c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
340ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3412c255e9dSRobert Watson }
3422c255e9dSRobert Watson 
3432c255e9dSRobert Watson /*
3442c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3452c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3462c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3472c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
348a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3492c255e9dSRobert Watson  */
3502c255e9dSRobert Watson static void
3512c255e9dSRobert Watson ktr_drain(struct thread *td)
3522c255e9dSRobert Watson {
3532c255e9dSRobert Watson 	struct ktr_request *queued_req;
3542c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3552c255e9dSRobert Watson 
3562c255e9dSRobert Watson 	ktrace_assert(td);
3572c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3582c255e9dSRobert Watson 
3592b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3602c255e9dSRobert Watson 
3612c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3622c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3632c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3642c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3652c255e9dSRobert Watson 
3662c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3672c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3682c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3692c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3702c255e9dSRobert Watson 		}
3712c255e9dSRobert Watson 	}
3722c255e9dSRobert Watson }
3732c255e9dSRobert Watson 
3742c255e9dSRobert Watson /*
3752c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3762c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3772c255e9dSRobert Watson  * been cached in the thread.
3782c255e9dSRobert Watson  */
3792c255e9dSRobert Watson static void
38022ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
3812c255e9dSRobert Watson {
3822c255e9dSRobert Watson 
3832c255e9dSRobert Watson 	ktrace_assert(td);
3842c255e9dSRobert Watson 
3852c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3862c255e9dSRobert Watson 	ktr_drain(td);
3872c255e9dSRobert Watson 	ktr_writerequest(td, req);
3882c255e9dSRobert Watson 	ktr_freerequest(req);
3892c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3902c255e9dSRobert Watson 	ktrace_exit(td);
391ea3fc8e4SJohn Baldwin }
392ea3fc8e4SJohn Baldwin 
393ea3fc8e4SJohn Baldwin static void
394ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
395ea3fc8e4SJohn Baldwin {
396ea3fc8e4SJohn Baldwin 
397d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
398d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
399d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
400d680caabSJohn Baldwin }
401d680caabSJohn Baldwin 
402d680caabSJohn Baldwin static void
403d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
404d680caabSJohn Baldwin {
405d680caabSJohn Baldwin 
406d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
407d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
408d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
409ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
410d680caabSJohn Baldwin }
411d680caabSJohn Baldwin 
412d680caabSJohn Baldwin /*
413d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
414d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
415d680caabSJohn Baldwin  * vnode and credentials.
416d680caabSJohn Baldwin  */
417d680caabSJohn Baldwin static void
418d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
419d680caabSJohn Baldwin {
420d680caabSJohn Baldwin 	struct ktr_request *req;
421d680caabSJohn Baldwin 
422d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
423d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
424d680caabSJohn Baldwin 	*uc = p->p_tracecred;
425d680caabSJohn Baldwin 	p->p_tracecred = NULL;
426d680caabSJohn Baldwin 	if (vp != NULL)
427d680caabSJohn Baldwin 		*vp = p->p_tracevp;
428d680caabSJohn Baldwin 	p->p_tracevp = NULL;
429d680caabSJohn Baldwin 	p->p_traceflag = 0;
430d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
431d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
432d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
433d680caabSJohn Baldwin 	}
434ea3fc8e4SJohn Baldwin }
435ea3fc8e4SJohn Baldwin 
43626f9a767SRodney W. Grimes void
437ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
43871ddfdbbSDmitrij Tejblum 	int code, narg;
43971ddfdbbSDmitrij Tejblum 	register_t args[];
440df8bae1dSRodney W. Grimes {
441ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
442df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
443ea3fc8e4SJohn Baldwin 	size_t buflen;
4444b3aac3dSJohn Baldwin 	char *buf = NULL;
445df8bae1dSRodney W. Grimes 
4464b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
4474b3aac3dSJohn Baldwin 	if (buflen > 0) {
448a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
4494b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
4504b3aac3dSJohn Baldwin 	}
451ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
45250c22331SPoul-Henning Kamp 	if (req == NULL) {
45350c22331SPoul-Henning Kamp 		if (buf != NULL)
45450c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
455ea3fc8e4SJohn Baldwin 		return;
45650c22331SPoul-Henning Kamp 	}
457ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
458df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
459df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
460ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
461ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
462d977a583SRobert Watson 		req->ktr_buffer = buf;
463ea3fc8e4SJohn Baldwin 	}
4642c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
465df8bae1dSRodney W. Grimes }
466df8bae1dSRodney W. Grimes 
46726f9a767SRodney W. Grimes void
468ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
46971ddfdbbSDmitrij Tejblum 	int code, error;
47071ddfdbbSDmitrij Tejblum 	register_t retval;
471df8bae1dSRodney W. Grimes {
472ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
473ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
474df8bae1dSRodney W. Grimes 
475ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
476ea3fc8e4SJohn Baldwin 	if (req == NULL)
477ea3fc8e4SJohn Baldwin 		return;
478ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
479ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
480ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
481*5a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
4822c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4832c255e9dSRobert Watson }
4842c255e9dSRobert Watson 
4852c255e9dSRobert Watson /*
486d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
487d680caabSJohn Baldwin  *
488d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
489d680caabSJohn Baldwin  */
490d680caabSJohn Baldwin void
491d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
492d680caabSJohn Baldwin {
493d680caabSJohn Baldwin 
494d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
495d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
496d680caabSJohn Baldwin 	ktr_freeproc(p, uc, vp);
497d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
498d680caabSJohn Baldwin }
499d680caabSJohn Baldwin 
500d680caabSJohn Baldwin /*
501d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
502d680caabSJohn Baldwin  * and disable tracing.
5032c255e9dSRobert Watson  */
5042c255e9dSRobert Watson void
5052c255e9dSRobert Watson ktrprocexit(struct thread *td)
5062c255e9dSRobert Watson {
5077705d4b2SDmitry Chagin 	struct ktr_request *req;
508d680caabSJohn Baldwin 	struct proc *p;
509d680caabSJohn Baldwin 	struct ucred *cred;
510d680caabSJohn Baldwin 	struct vnode *vp;
511d680caabSJohn Baldwin 	int vfslocked;
512d680caabSJohn Baldwin 
513d680caabSJohn Baldwin 	p = td->td_proc;
514d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
515d680caabSJohn Baldwin 		return;
5162c255e9dSRobert Watson 
5172c255e9dSRobert Watson 	ktrace_enter(td);
51822ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
51922ec0406SDmitry Chagin 	if (req != NULL)
52022ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
5212c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
5222c255e9dSRobert Watson 	ktr_drain(td);
5232c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
524d680caabSJohn Baldwin 	PROC_LOCK(p);
525d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
526d680caabSJohn Baldwin 	ktr_freeproc(p, &cred, &vp);
527d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
528d680caabSJohn Baldwin 	PROC_UNLOCK(p);
529d680caabSJohn Baldwin 	if (vp != NULL) {
530d680caabSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
531d680caabSJohn Baldwin 		vrele(vp);
532d680caabSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
533d680caabSJohn Baldwin 	}
534d680caabSJohn Baldwin 	if (cred != NULL)
535d680caabSJohn Baldwin 		crfree(cred);
5362c255e9dSRobert Watson 	ktrace_exit(td);
5372c255e9dSRobert Watson }
5382c255e9dSRobert Watson 
5397705d4b2SDmitry Chagin static void
54022ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
5417705d4b2SDmitry Chagin {
5427705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
5437705d4b2SDmitry Chagin 	struct ktr_request *req;
544de60a5f3SDmitry Chagin 	struct thread *td2;
5457705d4b2SDmitry Chagin 
5467705d4b2SDmitry Chagin 	ktrace_assert(td);
5477705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
54822ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
5497705d4b2SDmitry Chagin 	if (req == NULL)
5507705d4b2SDmitry Chagin 		return;
5517705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
5527705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
55322ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
5547705d4b2SDmitry Chagin }
5557705d4b2SDmitry Chagin 
5567705d4b2SDmitry Chagin void
5577705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
5587705d4b2SDmitry Chagin {
5597705d4b2SDmitry Chagin 	struct thread *td = curthread;
5607705d4b2SDmitry Chagin 
5617705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
5627705d4b2SDmitry Chagin 		return;
5637705d4b2SDmitry Chagin 
5647705d4b2SDmitry Chagin 	ktrace_enter(td);
56522ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
5667705d4b2SDmitry Chagin 	ktrace_exit(td);
5677705d4b2SDmitry Chagin }
5687705d4b2SDmitry Chagin 
5692c255e9dSRobert Watson /*
570d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
571d680caabSJohn Baldwin  */
572d680caabSJohn Baldwin void
573d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
574d680caabSJohn Baldwin {
575d680caabSJohn Baldwin 
5767705d4b2SDmitry Chagin 	PROC_LOCK(p1);
577d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
578d680caabSJohn Baldwin 	KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
579d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
580d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
581d680caabSJohn Baldwin 		if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
582d680caabSJohn Baldwin 			VREF(p2->p_tracevp);
583d680caabSJohn Baldwin 			KASSERT(p1->p_tracecred != NULL,
584d680caabSJohn Baldwin 			    ("ktrace vnode with no cred"));
585d680caabSJohn Baldwin 			p2->p_tracecred = crhold(p1->p_tracecred);
586d680caabSJohn Baldwin 		}
587d680caabSJohn Baldwin 	}
588d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5897705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
5907705d4b2SDmitry Chagin 
5917705d4b2SDmitry Chagin 	ktrprocctor(p2);
592d680caabSJohn Baldwin }
593d680caabSJohn Baldwin 
594d680caabSJohn Baldwin /*
5952c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
5962c255e9dSRobert Watson  * system call.
5972c255e9dSRobert Watson  */
5982c255e9dSRobert Watson void
5992c255e9dSRobert Watson ktruserret(struct thread *td)
6002c255e9dSRobert Watson {
6012c255e9dSRobert Watson 
6022c255e9dSRobert Watson 	ktrace_enter(td);
6032c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6042c255e9dSRobert Watson 	ktr_drain(td);
6052c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6062c255e9dSRobert Watson 	ktrace_exit(td);
607df8bae1dSRodney W. Grimes }
608df8bae1dSRodney W. Grimes 
60926f9a767SRodney W. Grimes void
610ea3fc8e4SJohn Baldwin ktrnamei(path)
611df8bae1dSRodney W. Grimes 	char *path;
612df8bae1dSRodney W. Grimes {
613ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
614ea3fc8e4SJohn Baldwin 	int namelen;
6154b3aac3dSJohn Baldwin 	char *buf = NULL;
616df8bae1dSRodney W. Grimes 
6174b3aac3dSJohn Baldwin 	namelen = strlen(path);
6184b3aac3dSJohn Baldwin 	if (namelen > 0) {
619a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
6204b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
6214b3aac3dSJohn Baldwin 	}
622ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
62350c22331SPoul-Henning Kamp 	if (req == NULL) {
62450c22331SPoul-Henning Kamp 		if (buf != NULL)
62550c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
626ea3fc8e4SJohn Baldwin 		return;
62750c22331SPoul-Henning Kamp 	}
628ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
629ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
630d977a583SRobert Watson 		req->ktr_buffer = buf;
631ea3fc8e4SJohn Baldwin 	}
6322c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
63526f9a767SRodney W. Grimes void
636a56be37eSJohn Baldwin ktrsysctl(name, namelen)
637a56be37eSJohn Baldwin 	int *name;
638a56be37eSJohn Baldwin 	u_int namelen;
639a56be37eSJohn Baldwin {
640a56be37eSJohn Baldwin 	struct ktr_request *req;
641a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
642a56be37eSJohn Baldwin 	char *mibname;
643a56be37eSJohn Baldwin 	size_t mibnamelen;
644a56be37eSJohn Baldwin 	int error;
645a56be37eSJohn Baldwin 
646a56be37eSJohn Baldwin 	/* Lookup name of mib. */
647a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
648a56be37eSJohn Baldwin 	mib[0] = 0;
649a56be37eSJohn Baldwin 	mib[1] = 1;
650a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
651a56be37eSJohn Baldwin 	mibnamelen = 128;
652a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
653a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
654a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
655a56be37eSJohn Baldwin 	if (error) {
656a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
657a56be37eSJohn Baldwin 		return;
658a56be37eSJohn Baldwin 	}
659a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
660a56be37eSJohn Baldwin 	if (req == NULL) {
661a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
662a56be37eSJohn Baldwin 		return;
663a56be37eSJohn Baldwin 	}
664a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
665a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
666a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
667a56be37eSJohn Baldwin }
668a56be37eSJohn Baldwin 
669a56be37eSJohn Baldwin void
670ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
671df8bae1dSRodney W. Grimes 	int fd;
672df8bae1dSRodney W. Grimes 	enum uio_rw rw;
67342ebfbf2SBrian Feldman 	struct uio *uio;
67442ebfbf2SBrian Feldman 	int error;
675df8bae1dSRodney W. Grimes {
676ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
677ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
678b92584a6SJohn Baldwin 	int datalen;
679b92584a6SJohn Baldwin 	char *buf;
680df8bae1dSRodney W. Grimes 
681552afd9cSPoul-Henning Kamp 	if (error) {
682552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
683df8bae1dSRodney W. Grimes 		return;
684552afd9cSPoul-Henning Kamp 	}
685b92584a6SJohn Baldwin 	uio->uio_offset = 0;
686b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
687b92584a6SJohn Baldwin 	datalen = imin(uio->uio_resid, ktr_geniosize);
688a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
689552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
690552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
691552afd9cSPoul-Henning Kamp 	if (error) {
692b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
693ea3fc8e4SJohn Baldwin 		return;
694b92584a6SJohn Baldwin 	}
695b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
696b92584a6SJohn Baldwin 	if (req == NULL) {
697b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
698b92584a6SJohn Baldwin 		return;
699b92584a6SJohn Baldwin 	}
700ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
701ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
702ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
703b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
704d977a583SRobert Watson 	req->ktr_buffer = buf;
7052c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
706df8bae1dSRodney W. Grimes }
707df8bae1dSRodney W. Grimes 
70826f9a767SRodney W. Grimes void
709ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
710a93fdaacSMarcel Moolenaar 	int sig;
711df8bae1dSRodney W. Grimes 	sig_t action;
7122c42a146SMarcel Moolenaar 	sigset_t *mask;
713a93fdaacSMarcel Moolenaar 	int code;
714df8bae1dSRodney W. Grimes {
71522ec0406SDmitry Chagin 	struct thread *td = curthread;
716ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
717ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
718df8bae1dSRodney W. Grimes 
719ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
720ea3fc8e4SJohn Baldwin 	if (req == NULL)
721ea3fc8e4SJohn Baldwin 		return;
722ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
723ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
724ea3fc8e4SJohn Baldwin 	kp->action = action;
725ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
726ea3fc8e4SJohn Baldwin 	kp->code = code;
72722ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
72822ec0406SDmitry Chagin 	ktrace_exit(td);
729df8bae1dSRodney W. Grimes }
730df8bae1dSRodney W. Grimes 
73126f9a767SRodney W. Grimes void
732ea3fc8e4SJohn Baldwin ktrcsw(out, user)
733df8bae1dSRodney W. Grimes 	int out, user;
734df8bae1dSRodney W. Grimes {
73522ec0406SDmitry Chagin 	struct thread *td = curthread;
736ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
737ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
738df8bae1dSRodney W. Grimes 
739ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
740ea3fc8e4SJohn Baldwin 	if (req == NULL)
741ea3fc8e4SJohn Baldwin 		return;
742ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
743ea3fc8e4SJohn Baldwin 	kc->out = out;
744ea3fc8e4SJohn Baldwin 	kc->user = user;
74522ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
74622ec0406SDmitry Chagin 	ktrace_exit(td);
747df8bae1dSRodney W. Grimes }
74860e15db9SDag-Erling Smørgrav 
74960e15db9SDag-Erling Smørgrav void
750a3052d6eSJohn Baldwin ktrstruct(name, data, datalen)
75160e15db9SDag-Erling Smørgrav 	const char *name;
75260e15db9SDag-Erling Smørgrav 	void *data;
75360e15db9SDag-Erling Smørgrav 	size_t datalen;
75460e15db9SDag-Erling Smørgrav {
75560e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
75660e15db9SDag-Erling Smørgrav 	char *buf = NULL;
75760e15db9SDag-Erling Smørgrav 	size_t buflen;
75860e15db9SDag-Erling Smørgrav 
75960e15db9SDag-Erling Smørgrav 	if (!data)
76060e15db9SDag-Erling Smørgrav 		datalen = 0;
761a3052d6eSJohn Baldwin 	buflen = strlen(name) + 1 + datalen;
76260e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
763a3052d6eSJohn Baldwin 	strcpy(buf, name);
764a3052d6eSJohn Baldwin 	bcopy(data, buf + strlen(name) + 1, datalen);
76560e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
76660e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
76760e15db9SDag-Erling Smørgrav 		return;
76860e15db9SDag-Erling Smørgrav 	}
76960e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
77060e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
77160e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
77260e15db9SDag-Erling Smørgrav }
773c601ad8eSDag-Erling Smørgrav 
774c601ad8eSDag-Erling Smørgrav void
775e141be6fSDag-Erling Smørgrav ktrcapfail(type, needed, held)
776e141be6fSDag-Erling Smørgrav 	enum ktr_cap_fail_type type;
777c601ad8eSDag-Erling Smørgrav 	cap_rights_t needed;
778c601ad8eSDag-Erling Smørgrav 	cap_rights_t held;
779c601ad8eSDag-Erling Smørgrav {
780c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
781c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
782c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
783c601ad8eSDag-Erling Smørgrav 
784c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
785c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
786c601ad8eSDag-Erling Smørgrav 		return;
787c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
788e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
789c601ad8eSDag-Erling Smørgrav 	kcf->cap_needed = needed;
790c601ad8eSDag-Erling Smørgrav 	kcf->cap_held = held;
791c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
792c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
793c601ad8eSDag-Erling Smørgrav }
79464cc6a13SJohn Baldwin #endif /* KTRACE */
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes /* Interface and common routines */
797df8bae1dSRodney W. Grimes 
798d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
799df8bae1dSRodney W. Grimes struct ktrace_args {
800df8bae1dSRodney W. Grimes 	char	*fname;
801df8bae1dSRodney W. Grimes 	int	ops;
802df8bae1dSRodney W. Grimes 	int	facs;
803df8bae1dSRodney W. Grimes 	int	pid;
804df8bae1dSRodney W. Grimes };
805d2d3e875SBruce Evans #endif
806df8bae1dSRodney W. Grimes /* ARGSUSED */
80726f9a767SRodney W. Grimes int
8088451d0ddSKip Macy sys_ktrace(td, uap)
809b40ce416SJulian Elischer 	struct thread *td;
810df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
811df8bae1dSRodney W. Grimes {
812db6a20e2SGarrett Wollman #ifdef KTRACE
813df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
814df8bae1dSRodney W. Grimes 	register struct proc *p;
815df8bae1dSRodney W. Grimes 	struct pgrp *pg;
816df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
817df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
818df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
819400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
82033f19beeSJohn Baldwin 	int flags, error = 0, vfslocked;
821df8bae1dSRodney W. Grimes 	struct nameidata nd;
822a5881ea5SJohn Baldwin 	struct ucred *cred;
823df8bae1dSRodney W. Grimes 
82464cc6a13SJohn Baldwin 	/*
82564cc6a13SJohn Baldwin 	 * Need something to (un)trace.
82664cc6a13SJohn Baldwin 	 */
82764cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
82864cc6a13SJohn Baldwin 		return (EINVAL);
82964cc6a13SJohn Baldwin 
8302c255e9dSRobert Watson 	ktrace_enter(td);
831df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
832df8bae1dSRodney W. Grimes 		/*
833df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
834df8bae1dSRodney W. Grimes 		 */
83533f19beeSJohn Baldwin 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_USERSPACE,
83633f19beeSJohn Baldwin 		    uap->fname, td);
837e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
8389e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
839797f2d22SPoul-Henning Kamp 		if (error) {
8402c255e9dSRobert Watson 			ktrace_exit(td);
841df8bae1dSRodney W. Grimes 			return (error);
842df8bae1dSRodney W. Grimes 		}
84333f19beeSJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
844762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
845df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
84622db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
847df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
848a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
84933f19beeSJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
8502c255e9dSRobert Watson 			ktrace_exit(td);
851df8bae1dSRodney W. Grimes 			return (EACCES);
852df8bae1dSRodney W. Grimes 		}
85333f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
854df8bae1dSRodney W. Grimes 	}
855df8bae1dSRodney W. Grimes 	/*
85679deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
857df8bae1dSRodney W. Grimes 	 */
858df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
85951fd6380SMike Pritchard 		int vrele_count;
86051fd6380SMike Pritchard 
86151fd6380SMike Pritchard 		vrele_count = 0;
8621005a129SJohn Baldwin 		sx_slock(&allproc_lock);
8634f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
864a7ff7443SJohn Baldwin 			PROC_LOCK(p);
865a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
866ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
867ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
868d680caabSJohn Baldwin 					ktr_freeproc(p, &cred, NULL);
869ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
87051fd6380SMike Pritchard 					vrele_count++;
871a5881ea5SJohn Baldwin 					crfree(cred);
87251fd6380SMike Pritchard 				} else
873df8bae1dSRodney W. Grimes 					error = EPERM;
874df8bae1dSRodney W. Grimes 			}
875a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
87679deba82SMatthew Dillon 		}
8771005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
87851fd6380SMike Pritchard 		if (vrele_count > 0) {
87951fd6380SMike Pritchard 			vfslocked = VFS_LOCK_GIANT(vp->v_mount);
88051fd6380SMike Pritchard 			while (vrele_count-- > 0)
88151fd6380SMike Pritchard 				vrele(vp);
88251fd6380SMike Pritchard 			VFS_UNLOCK_GIANT(vfslocked);
88351fd6380SMike Pritchard 		}
884df8bae1dSRodney W. Grimes 		goto done;
885df8bae1dSRodney W. Grimes 	}
886df8bae1dSRodney W. Grimes 	/*
887df8bae1dSRodney W. Grimes 	 * do it
888df8bae1dSRodney W. Grimes 	 */
88964cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
890df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
891df8bae1dSRodney W. Grimes 		/*
892df8bae1dSRodney W. Grimes 		 * by process group
893df8bae1dSRodney W. Grimes 		 */
894df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
895df8bae1dSRodney W. Grimes 		if (pg == NULL) {
896ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
897df8bae1dSRodney W. Grimes 			error = ESRCH;
898df8bae1dSRodney W. Grimes 			goto done;
899df8bae1dSRodney W. Grimes 		}
900f591779bSSeigo Tanimura 		/*
901f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
902ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
903f591779bSSeigo Tanimura 		 */
904f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
905400a74bfSPawel Jakub Dawidek 		nfound = 0;
906400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
907400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
908e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW ||
909e806d352SJohn Baldwin 			    p_cansee(td, p) != 0) {
910400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
911400a74bfSPawel Jakub Dawidek 				continue;
912400a74bfSPawel Jakub Dawidek 			}
913400a74bfSPawel Jakub Dawidek 			nfound++;
914df8bae1dSRodney W. Grimes 			if (descend)
915a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
916df8bae1dSRodney W. Grimes 			else
917a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
918400a74bfSPawel Jakub Dawidek 		}
919400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
920400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
921400a74bfSPawel Jakub Dawidek 			error = ESRCH;
922400a74bfSPawel Jakub Dawidek 			goto done;
923400a74bfSPawel Jakub Dawidek 		}
924df8bae1dSRodney W. Grimes 	} else {
925df8bae1dSRodney W. Grimes 		/*
926df8bae1dSRodney W. Grimes 		 * by pid
927df8bae1dSRodney W. Grimes 		 */
928df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
929fe41d17aSJohn Baldwin 		if (p == NULL)
930df8bae1dSRodney W. Grimes 			error = ESRCH;
931fe41d17aSJohn Baldwin 		else
9324eb7c9f6SPawel Jakub Dawidek 			error = p_cansee(td, p);
933b0d9aeddSPawel Jakub Dawidek 		if (error) {
934fe41d17aSJohn Baldwin 			if (p != NULL)
935fe41d17aSJohn Baldwin 				PROC_UNLOCK(p);
936b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
9374eb7c9f6SPawel Jakub Dawidek 			goto done;
938b0d9aeddSPawel Jakub Dawidek 		}
939df8bae1dSRodney W. Grimes 		if (descend)
940a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
941df8bae1dSRodney W. Grimes 		else
942a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
943df8bae1dSRodney W. Grimes 	}
94464cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
945df8bae1dSRodney W. Grimes 	if (!ret)
946df8bae1dSRodney W. Grimes 		error = EPERM;
947df8bae1dSRodney W. Grimes done:
94864cc6a13SJohn Baldwin 	if (vp != NULL) {
94933f19beeSJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
950a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
95133f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
95264cc6a13SJohn Baldwin 	}
9532c255e9dSRobert Watson 	ktrace_exit(td);
954df8bae1dSRodney W. Grimes 	return (error);
95564cc6a13SJohn Baldwin #else /* !KTRACE */
95664cc6a13SJohn Baldwin 	return (ENOSYS);
95764cc6a13SJohn Baldwin #endif /* KTRACE */
958df8bae1dSRodney W. Grimes }
959df8bae1dSRodney W. Grimes 
960e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
961e6c4b9baSPoul-Henning Kamp int
9628451d0ddSKip Macy sys_utrace(td, uap)
963b40ce416SJulian Elischer 	struct thread *td;
964e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
965e6c4b9baSPoul-Henning Kamp {
966b40ce416SJulian Elischer 
967e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
968ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
9697f05b035SAlfred Perlstein 	void *cp;
970c9e7d28eSJohn Baldwin 	int error;
971e6c4b9baSPoul-Henning Kamp 
972c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
973c9e7d28eSJohn Baldwin 		return (0);
974bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
9750bad156aSAlfred Perlstein 		return (EINVAL);
976a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
977c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
97850c22331SPoul-Henning Kamp 	if (error) {
97950c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
980c9e7d28eSJohn Baldwin 		return (error);
98150c22331SPoul-Henning Kamp 	}
982ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
98350c22331SPoul-Henning Kamp 	if (req == NULL) {
98450c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
985b10221ffSJoseph Koshy 		return (ENOMEM);
98650c22331SPoul-Henning Kamp 	}
987d977a583SRobert Watson 	req->ktr_buffer = cp;
988ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
9892c255e9dSRobert Watson 	ktr_submitrequest(td, req);
990e6c4b9baSPoul-Henning Kamp 	return (0);
99164cc6a13SJohn Baldwin #else /* !KTRACE */
992e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
99364cc6a13SJohn Baldwin #endif /* KTRACE */
994e6c4b9baSPoul-Henning Kamp }
995e6c4b9baSPoul-Henning Kamp 
996db6a20e2SGarrett Wollman #ifdef KTRACE
99787b6de2bSPoul-Henning Kamp static int
998a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
999a7ff7443SJohn Baldwin 	struct thread *td;
1000a7ff7443SJohn Baldwin 	struct proc *p;
1001df8bae1dSRodney W. Grimes 	int ops, facs;
1002df8bae1dSRodney W. Grimes 	struct vnode *vp;
1003df8bae1dSRodney W. Grimes {
1004ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
1005a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
1006df8bae1dSRodney W. Grimes 
1007fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1008a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1009a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1010df8bae1dSRodney W. Grimes 		return (0);
1011a7ff7443SJohn Baldwin 	}
1012fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1013fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1014fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1015fe41d17aSJohn Baldwin 		return (1);
1016fe41d17aSJohn Baldwin 	}
1017ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1018df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
1019a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
1020df8bae1dSRodney W. Grimes 			/*
1021a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
1022df8bae1dSRodney W. Grimes 			 */
1023a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
1024ea3fc8e4SJohn Baldwin 			VREF(vp);
1025a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
1026a5881ea5SJohn Baldwin 		}
1027a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
1028a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
1029a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
1030df8bae1dSRodney W. Grimes 		}
1031df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
103232f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1033df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1034df8bae1dSRodney W. Grimes 	} else {
1035df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1036d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1037df8bae1dSRodney W. Grimes 			/* no more tracing */
1038d680caabSJohn Baldwin 			ktr_freeproc(p, &tracecred, &tracevp);
1039a7ff7443SJohn Baldwin 	}
1040ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
104122ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
104222ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1043a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
104464cc6a13SJohn Baldwin 	if (tracevp != NULL) {
1045033eb86eSJeff Roberson 		int vfslocked;
1046033eb86eSJeff Roberson 
1047033eb86eSJeff Roberson 		vfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
1048ea3fc8e4SJohn Baldwin 		vrele(tracevp);
1049033eb86eSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
105064cc6a13SJohn Baldwin 	}
1051a5881ea5SJohn Baldwin 	if (tracecred != NULL)
1052a5881ea5SJohn Baldwin 		crfree(tracecred);
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	return (1);
1055df8bae1dSRodney W. Grimes }
1056df8bae1dSRodney W. Grimes 
105787b6de2bSPoul-Henning Kamp static int
1058a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
1059a7ff7443SJohn Baldwin 	struct thread *td;
1060a7ff7443SJohn Baldwin 	struct proc *top;
1061df8bae1dSRodney W. Grimes 	int ops, facs;
1062df8bae1dSRodney W. Grimes 	struct vnode *vp;
1063df8bae1dSRodney W. Grimes {
1064df8bae1dSRodney W. Grimes 	register struct proc *p;
1065df8bae1dSRodney W. Grimes 	register int ret = 0;
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 	p = top;
1068fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
106964cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1070df8bae1dSRodney W. Grimes 	for (;;) {
1071a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
1072df8bae1dSRodney W. Grimes 		/*
1073df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1074df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1075df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1076df8bae1dSRodney W. Grimes 		 */
10772e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
10782e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1079df8bae1dSRodney W. Grimes 		else for (;;) {
108064cc6a13SJohn Baldwin 			if (p == top)
1081df8bae1dSRodney W. Grimes 				return (ret);
10822e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
10832e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1084df8bae1dSRodney W. Grimes 				break;
1085df8bae1dSRodney W. Grimes 			}
1086b75356e1SJeffrey Hsu 			p = p->p_pptr;
1087df8bae1dSRodney W. Grimes 		}
1088fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1089df8bae1dSRodney W. Grimes 	}
1090df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1091df8bae1dSRodney W. Grimes }
1092df8bae1dSRodney W. Grimes 
109387b6de2bSPoul-Henning Kamp static void
10942c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1095df8bae1dSRodney W. Grimes {
1096ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1097ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1098ea3fc8e4SJohn Baldwin 	struct proc *p;
1099ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1100df8bae1dSRodney W. Grimes 	struct uio auio;
1101ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1102f2a2857bSKirk McKusick 	struct mount *mp;
1103ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
110433f19beeSJohn Baldwin 	int error, vfslocked;
1105df8bae1dSRodney W. Grimes 
11062c255e9dSRobert Watson 	/*
11072c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
11082c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
11092c255e9dSRobert Watson 	 *
11102c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
11112c255e9dSRobert Watson 	 * the vnode has been closed.
11122c255e9dSRobert Watson 	 */
11132c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
11142c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
11152c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
11162c255e9dSRobert Watson 
1117ea3fc8e4SJohn Baldwin 	/*
1118ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
11192c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
11202c255e9dSRobert Watson 	 * in sync: we should have both or neither.
1121ea3fc8e4SJohn Baldwin 	 */
11222c255e9dSRobert Watson 	if (vp == NULL) {
11232c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1124118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1125df8bae1dSRodney W. Grimes 		return;
11262c255e9dSRobert Watson 	}
1127118258f5SBjoern A. Zeeb 	VREF(vp);
11282c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1129118258f5SBjoern A. Zeeb 	crhold(cred);
1130118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
11312c255e9dSRobert Watson 
1132ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
1133a56be37eSJohn Baldwin 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1134a56be37eSJohn Baldwin 	    sizeof(data_lengths) / sizeof(data_lengths[0]),
1135a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
11368b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1137ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1138df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1139df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1140df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1141df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1142df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1143df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1144df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1145df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1146ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1147ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1148ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1149ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1150ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1151df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1152ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1153ea3fc8e4SJohn Baldwin 	}
1154ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1155d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1156d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1157ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1158ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1159ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1160b92584a6SJohn Baldwin 	}
11612c255e9dSRobert Watson 
116233f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1163f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1164cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1165467a273cSRobert Watson #ifdef MAC
116630d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1167467a273cSRobert Watson 	if (error == 0)
1168467a273cSRobert Watson #endif
1169ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
117022db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
1171f2a2857bSKirk McKusick 	vn_finished_write(mp);
1172118258f5SBjoern A. Zeeb 	crfree(cred);
1173118258f5SBjoern A. Zeeb 	if (!error) {
1174704c9f00SJohn Baldwin 		vrele(vp);
117533f19beeSJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
1176df8bae1dSRodney W. Grimes 		return;
1177118258f5SBjoern A. Zeeb 	}
1178118258f5SBjoern A. Zeeb 	VFS_UNLOCK_GIANT(vfslocked);
1179118258f5SBjoern A. Zeeb 
1180df8bae1dSRodney W. Grimes 	/*
1181ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1182ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1183ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1184118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1185118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1186118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1187118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1188df8bae1dSRodney W. Grimes 	 */
1189df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1190df8bae1dSRodney W. Grimes 	    error);
1191118258f5SBjoern A. Zeeb 	vrele_count = 1;
1192ea3fc8e4SJohn Baldwin 	/*
1193ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1194ea3fc8e4SJohn Baldwin 	 * system.
1195ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1196ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1197ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1198ea3fc8e4SJohn Baldwin 	 */
1199a5881ea5SJohn Baldwin 	cred = NULL;
12001005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12014f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1202ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1203a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1204ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1205d680caabSJohn Baldwin 			ktr_freeproc(p, &cred, NULL);
1206ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1207ea3fc8e4SJohn Baldwin 			vrele_count++;
1208df8bae1dSRodney W. Grimes 		}
1209ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1210a5881ea5SJohn Baldwin 		if (cred != NULL) {
1211a5881ea5SJohn Baldwin 			crfree(cred);
1212a5881ea5SJohn Baldwin 			cred = NULL;
1213a5881ea5SJohn Baldwin 		}
1214df8bae1dSRodney W. Grimes 	}
12151005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12162c255e9dSRobert Watson 
121733f19beeSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1218ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1219ea3fc8e4SJohn Baldwin 		vrele(vp);
122033f19beeSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes /*
1224df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1225df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1226df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1227df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1228df8bae1dSRodney W. Grimes  * so, only root may further change it.
1229df8bae1dSRodney W. Grimes  */
123087b6de2bSPoul-Henning Kamp static int
1231a7ff7443SJohn Baldwin ktrcanset(td, targetp)
1232a7ff7443SJohn Baldwin 	struct thread *td;
1233a7ff7443SJohn Baldwin 	struct proc *targetp;
1234df8bae1dSRodney W. Grimes {
1235df8bae1dSRodney W. Grimes 
1236a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1237a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
123832f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
123975c13541SPoul-Henning Kamp 		return (0);
1240a0f75161SRobert Watson 
1241f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1242a0f75161SRobert Watson 		return (0);
1243a0f75161SRobert Watson 
1244df8bae1dSRodney W. Grimes 	return (1);
1245df8bae1dSRodney W. Grimes }
1246df8bae1dSRodney W. Grimes 
1247db6a20e2SGarrett Wollman #endif /* KTRACE */
1248