xref: /freebsd/sys/kern/kern_ktrace.c (revision af3b2549c4ba2ef00a7cbb4cb6836598bf0aefbe)
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>
404a144410SRobert Watson #include <sys/capsicum.h>
41f23b4c91SGarrett Wollman #include <sys/systm.h>
42ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
43ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
44ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
45fb919e4dSMark Murray #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
48033eb86eSJeff Roberson #include <sys/mount.h>
49df8bae1dSRodney W. Grimes #include <sys/namei.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51ea3fc8e4SJohn Baldwin #include <sys/proc.h>
52ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5460e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5560e15db9SDag-Erling Smørgrav #include <sys/stat.h>
56df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
571005a129SJohn Baldwin #include <sys/sx.h>
58ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
597705d4b2SDmitry Chagin #include <sys/sysent.h>
60df8bae1dSRodney W. Grimes #include <sys/syslog.h>
61ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
62df8bae1dSRodney W. Grimes 
63aed55708SRobert Watson #include <security/mac/mac_framework.h>
64aed55708SRobert Watson 
652c255e9dSRobert Watson /*
662c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
672c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
682c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
692c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
702c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
712c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
722c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
732c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
742c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
752c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
762c255e9dSRobert Watson  *
772c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
782c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
792c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
802c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
812c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
822c255e9dSRobert Watson  */
832c255e9dSRobert Watson 
84a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8555166637SPoul-Henning Kamp 
86db6a20e2SGarrett Wollman #ifdef KTRACE
87ea3fc8e4SJohn Baldwin 
88de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
89de5b1952SAlexander Leidinger 
90ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
91ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
92ea3fc8e4SJohn Baldwin #endif
93ea3fc8e4SJohn Baldwin 
94ea3fc8e4SJohn Baldwin struct ktr_request {
95ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
96d977a583SRobert Watson 	void	*ktr_buffer;
97ea3fc8e4SJohn Baldwin 	union {
987705d4b2SDmitry Chagin 		struct	ktr_proc_ctor ktr_proc_ctor;
99c601ad8eSDag-Erling Smørgrav 		struct	ktr_cap_fail ktr_cap_fail;
100ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
101ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
102ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
103ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
104ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
10535818d2eSJohn Baldwin 		struct	ktr_fault ktr_fault;
10635818d2eSJohn Baldwin 		struct	ktr_faultend ktr_faultend;
107ea3fc8e4SJohn Baldwin 	} ktr_data;
108ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
109ea3fc8e4SJohn Baldwin };
110ea3fc8e4SJohn Baldwin 
111ea3fc8e4SJohn Baldwin static int data_lengths[] = {
112093e059cSJilles Tjoelker 	[KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
113093e059cSJilles Tjoelker 	[KTR_SYSRET] = sizeof(struct ktr_sysret),
114093e059cSJilles Tjoelker 	[KTR_NAMEI] = 0,
115093e059cSJilles Tjoelker 	[KTR_GENIO] = sizeof(struct ktr_genio),
116093e059cSJilles Tjoelker 	[KTR_PSIG] = sizeof(struct ktr_psig),
117093e059cSJilles Tjoelker 	[KTR_CSW] = sizeof(struct ktr_csw),
118093e059cSJilles Tjoelker 	[KTR_USER] = 0,
119093e059cSJilles Tjoelker 	[KTR_STRUCT] = 0,
120093e059cSJilles Tjoelker 	[KTR_SYSCTL] = 0,
121093e059cSJilles Tjoelker 	[KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
122093e059cSJilles Tjoelker 	[KTR_PROCDTOR] = 0,
123093e059cSJilles Tjoelker 	[KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
124093e059cSJilles Tjoelker 	[KTR_FAULT] = sizeof(struct ktr_fault),
125093e059cSJilles Tjoelker 	[KTR_FAULTEND] = sizeof(struct 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;
136*af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
13712301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
138ea3fc8e4SJohn Baldwin 
139ea3fc8e4SJohn Baldwin static int print_message = 1;
140d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1412c255e9dSRobert Watson static struct sx ktrace_sx;
142ea3fc8e4SJohn Baldwin 
143ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
144ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
145b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
14622ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
147ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1482c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
149d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc,
150d680caabSJohn Baldwin     struct vnode **vp);
151ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
152d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1532c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
154a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
155a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
156a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
15722ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
15898d93822SBruce Evans 
1592c255e9dSRobert Watson /*
1602c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1612c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1622c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1632c255e9dSRobert Watson  * suppressed.
1642c255e9dSRobert Watson  */
1652c255e9dSRobert Watson static void
1662c255e9dSRobert Watson ktrace_enter(struct thread *td)
1672c255e9dSRobert Watson {
1682c255e9dSRobert Watson 
1692c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1702c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1712c255e9dSRobert Watson }
1722c255e9dSRobert Watson 
1732c255e9dSRobert Watson static void
1742c255e9dSRobert Watson ktrace_exit(struct thread *td)
1752c255e9dSRobert Watson {
1762c255e9dSRobert Watson 
1772c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1782c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1792c255e9dSRobert Watson }
1802c255e9dSRobert Watson 
1812c255e9dSRobert Watson static void
1822c255e9dSRobert Watson ktrace_assert(struct thread *td)
1832c255e9dSRobert Watson {
1842c255e9dSRobert Watson 
1852c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1862c255e9dSRobert Watson }
1872c255e9dSRobert Watson 
188ea3fc8e4SJohn Baldwin static void
189ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
190df8bae1dSRodney W. Grimes {
191ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
192ea3fc8e4SJohn Baldwin 	int i;
193df8bae1dSRodney W. Grimes 
194ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1952c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
196ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
197ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
198a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
199ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
200ea3fc8e4SJohn Baldwin 	}
201ea3fc8e4SJohn Baldwin }
202ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
203ea3fc8e4SJohn Baldwin 
204ea3fc8e4SJohn Baldwin static int
205ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
206ea3fc8e4SJohn Baldwin {
207ea3fc8e4SJohn Baldwin 	struct thread *td;
2088b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
209ea3fc8e4SJohn Baldwin 	int error;
210ea3fc8e4SJohn Baldwin 
211ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
212ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
213ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2148b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
215ea3fc8e4SJohn Baldwin 	}
216ea3fc8e4SJohn Baldwin 
2178b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
218ea3fc8e4SJohn Baldwin 	if (error)
219ea3fc8e4SJohn Baldwin 		return (error);
220ea3fc8e4SJohn Baldwin 	td = curthread;
2212c255e9dSRobert Watson 	ktrace_enter(td);
222ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
223b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2242c255e9dSRobert Watson 	ktrace_exit(td);
2258b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
226ea3fc8e4SJohn Baldwin 	if (error)
227ea3fc8e4SJohn Baldwin 		return (error);
228a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
229ea3fc8e4SJohn Baldwin 		return (ENOSPC);
230ea3fc8e4SJohn Baldwin 	return (0);
231ea3fc8e4SJohn Baldwin }
23212301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
233a0c87b74SGavin Atkinson     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
234a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
235ea3fc8e4SJohn Baldwin 
2368b149b51SJohn Baldwin static u_int
237b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
238ea3fc8e4SJohn Baldwin {
239b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
240ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
241a5896914SJoseph Koshy 	int bound;
242ea3fc8e4SJohn Baldwin 
243ea3fc8e4SJohn Baldwin 	print_message = 1;
244b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
245a5896914SJoseph Koshy 	if (bound == 0)
246a5896914SJoseph Koshy 		return (ktr_requestpool);
247b4c20e5eSDmitry Chagin 	if (bound < 0) {
248b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
249ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
250a5896914SJoseph Koshy 		while (bound++ < 0) {
251ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
252ea3fc8e4SJohn Baldwin 			if (req == NULL)
253b4c20e5eSDmitry Chagin 				break;
254ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
255ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
256ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
257ea3fc8e4SJohn Baldwin 		}
258b4c20e5eSDmitry Chagin 	} else {
259ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
260b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
261a5896914SJoseph Koshy 		while (bound-- > 0) {
262ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
263a163d034SWarner Losh 			    M_WAITOK);
264b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
265ea3fc8e4SJohn Baldwin 		}
266b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
267b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
268b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
269b4c20e5eSDmitry Chagin 	}
270b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
271ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
272ea3fc8e4SJohn Baldwin }
273ea3fc8e4SJohn Baldwin 
2745ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2755ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2765ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2775ca4819dSJohn Baldwin 
278ea3fc8e4SJohn Baldwin static struct ktr_request *
27922ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
280ea3fc8e4SJohn Baldwin {
281ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
282ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
283ea3fc8e4SJohn Baldwin 	int pm;
284ea3fc8e4SJohn Baldwin 
285c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
286ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
287c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
288ea3fc8e4SJohn Baldwin 		return (NULL);
289ea3fc8e4SJohn Baldwin 	}
290ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
291ea3fc8e4SJohn Baldwin 	if (req != NULL) {
292ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
293ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
29475768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
29575768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
29675768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
29775768576SJohn Baldwin 		}
298c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
299ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
300ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3012bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3025ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3035ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
304d977a583SRobert Watson 		req->ktr_buffer = NULL;
305ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
306ea3fc8e4SJohn Baldwin 	} else {
30775768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
308ea3fc8e4SJohn Baldwin 		pm = print_message;
309ea3fc8e4SJohn Baldwin 		print_message = 0;
310ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
311ea3fc8e4SJohn Baldwin 		if (pm)
312ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
313ea3fc8e4SJohn Baldwin 	}
314ea3fc8e4SJohn Baldwin 	return (req);
315ea3fc8e4SJohn Baldwin }
316ea3fc8e4SJohn Baldwin 
3177705d4b2SDmitry Chagin static struct ktr_request *
3187705d4b2SDmitry Chagin ktr_getrequest(int type)
3197705d4b2SDmitry Chagin {
3207705d4b2SDmitry Chagin 	struct thread *td = curthread;
3217705d4b2SDmitry Chagin 	struct ktr_request *req;
3227705d4b2SDmitry Chagin 
3237705d4b2SDmitry Chagin 	ktrace_enter(td);
32422ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3257705d4b2SDmitry Chagin 	if (req == NULL)
3267705d4b2SDmitry Chagin 		ktrace_exit(td);
3277705d4b2SDmitry Chagin 
3287705d4b2SDmitry Chagin 	return (req);
3297705d4b2SDmitry Chagin }
3307705d4b2SDmitry Chagin 
3312c255e9dSRobert Watson /*
3322c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3332c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3342c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3352c255e9dSRobert Watson  * later.
3362c255e9dSRobert Watson  */
337ea3fc8e4SJohn Baldwin static void
3382c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
339ea3fc8e4SJohn Baldwin {
340ea3fc8e4SJohn Baldwin 
341ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3422c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
343ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3442c255e9dSRobert Watson }
3452c255e9dSRobert Watson 
3462c255e9dSRobert Watson /*
3472c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3482c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3492c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3502c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
351a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3522c255e9dSRobert Watson  */
3532c255e9dSRobert Watson static void
3542c255e9dSRobert Watson ktr_drain(struct thread *td)
3552c255e9dSRobert Watson {
3562c255e9dSRobert Watson 	struct ktr_request *queued_req;
3572c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3582c255e9dSRobert Watson 
3592c255e9dSRobert Watson 	ktrace_assert(td);
3602c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3612c255e9dSRobert Watson 
3622b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3632c255e9dSRobert Watson 
3642c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3652c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3662c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3672c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3682c255e9dSRobert Watson 
3692c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3702c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3712c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3722c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3732c255e9dSRobert Watson 		}
3742c255e9dSRobert Watson 	}
3752c255e9dSRobert Watson }
3762c255e9dSRobert Watson 
3772c255e9dSRobert Watson /*
3782c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3792c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3802c255e9dSRobert Watson  * been cached in the thread.
3812c255e9dSRobert Watson  */
3822c255e9dSRobert Watson static void
38322ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
3842c255e9dSRobert Watson {
3852c255e9dSRobert Watson 
3862c255e9dSRobert Watson 	ktrace_assert(td);
3872c255e9dSRobert Watson 
3882c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3892c255e9dSRobert Watson 	ktr_drain(td);
3902c255e9dSRobert Watson 	ktr_writerequest(td, req);
3912c255e9dSRobert Watson 	ktr_freerequest(req);
3922c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3932c255e9dSRobert Watson 	ktrace_exit(td);
394ea3fc8e4SJohn Baldwin }
395ea3fc8e4SJohn Baldwin 
396ea3fc8e4SJohn Baldwin static void
397ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
398ea3fc8e4SJohn Baldwin {
399ea3fc8e4SJohn Baldwin 
400d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
401d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
402d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
403d680caabSJohn Baldwin }
404d680caabSJohn Baldwin 
405d680caabSJohn Baldwin static void
406d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
407d680caabSJohn Baldwin {
408d680caabSJohn Baldwin 
409d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
410d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
411d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
412ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
413d680caabSJohn Baldwin }
414d680caabSJohn Baldwin 
415d680caabSJohn Baldwin /*
416d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
417d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
418d680caabSJohn Baldwin  * vnode and credentials.
419d680caabSJohn Baldwin  */
420d680caabSJohn Baldwin static void
421d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
422d680caabSJohn Baldwin {
423d680caabSJohn Baldwin 	struct ktr_request *req;
424d680caabSJohn Baldwin 
425d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
426d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
427d680caabSJohn Baldwin 	*uc = p->p_tracecred;
428d680caabSJohn Baldwin 	p->p_tracecred = NULL;
429d680caabSJohn Baldwin 	if (vp != NULL)
430d680caabSJohn Baldwin 		*vp = p->p_tracevp;
431d680caabSJohn Baldwin 	p->p_tracevp = NULL;
432d680caabSJohn Baldwin 	p->p_traceflag = 0;
433d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
434d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
435d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
436d680caabSJohn Baldwin 	}
437ea3fc8e4SJohn Baldwin }
438ea3fc8e4SJohn Baldwin 
43926f9a767SRodney W. Grimes void
440ea3fc8e4SJohn Baldwin ktrsyscall(code, narg, args)
44171ddfdbbSDmitrij Tejblum 	int code, narg;
44271ddfdbbSDmitrij Tejblum 	register_t args[];
443df8bae1dSRodney W. Grimes {
444ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
445df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
446ea3fc8e4SJohn Baldwin 	size_t buflen;
4474b3aac3dSJohn Baldwin 	char *buf = NULL;
448df8bae1dSRodney W. Grimes 
4494b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
4504b3aac3dSJohn Baldwin 	if (buflen > 0) {
451a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
4524b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
4534b3aac3dSJohn Baldwin 	}
454ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
45550c22331SPoul-Henning Kamp 	if (req == NULL) {
45650c22331SPoul-Henning Kamp 		if (buf != NULL)
45750c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
458ea3fc8e4SJohn Baldwin 		return;
45950c22331SPoul-Henning Kamp 	}
460ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
461df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
462df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
463ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
464ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
465d977a583SRobert Watson 		req->ktr_buffer = buf;
466ea3fc8e4SJohn Baldwin 	}
4672c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
47026f9a767SRodney W. Grimes void
471ea3fc8e4SJohn Baldwin ktrsysret(code, error, retval)
47271ddfdbbSDmitrij Tejblum 	int code, error;
47371ddfdbbSDmitrij Tejblum 	register_t retval;
474df8bae1dSRodney W. Grimes {
475ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
476ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
477df8bae1dSRodney W. Grimes 
478ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
479ea3fc8e4SJohn Baldwin 	if (req == NULL)
480ea3fc8e4SJohn Baldwin 		return;
481ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
482ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
483ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
4845a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
4852c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4862c255e9dSRobert Watson }
4872c255e9dSRobert Watson 
4882c255e9dSRobert Watson /*
489d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
490d680caabSJohn Baldwin  *
491d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
492d680caabSJohn Baldwin  */
493d680caabSJohn Baldwin void
494d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
495d680caabSJohn Baldwin {
496d680caabSJohn Baldwin 
497d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
498d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
499d680caabSJohn Baldwin 	ktr_freeproc(p, uc, vp);
500d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
501d680caabSJohn Baldwin }
502d680caabSJohn Baldwin 
503d680caabSJohn Baldwin /*
504d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
505d680caabSJohn Baldwin  * and disable tracing.
5062c255e9dSRobert Watson  */
5072c255e9dSRobert Watson void
5082c255e9dSRobert Watson ktrprocexit(struct thread *td)
5092c255e9dSRobert Watson {
5107705d4b2SDmitry Chagin 	struct ktr_request *req;
511d680caabSJohn Baldwin 	struct proc *p;
512d680caabSJohn Baldwin 	struct ucred *cred;
513d680caabSJohn Baldwin 	struct vnode *vp;
514d680caabSJohn Baldwin 
515d680caabSJohn Baldwin 	p = td->td_proc;
516d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
517d680caabSJohn Baldwin 		return;
5182c255e9dSRobert Watson 
5192c255e9dSRobert Watson 	ktrace_enter(td);
52022ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
52122ec0406SDmitry Chagin 	if (req != NULL)
52222ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
5232c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
5242c255e9dSRobert Watson 	ktr_drain(td);
5252c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
526d680caabSJohn Baldwin 	PROC_LOCK(p);
527d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
528d680caabSJohn Baldwin 	ktr_freeproc(p, &cred, &vp);
529d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
530d680caabSJohn Baldwin 	PROC_UNLOCK(p);
5315050aa86SKonstantin Belousov 	if (vp != NULL)
532d680caabSJohn Baldwin 		vrele(vp);
533d680caabSJohn Baldwin 	if (cred != NULL)
534d680caabSJohn Baldwin 		crfree(cred);
5352c255e9dSRobert Watson 	ktrace_exit(td);
5362c255e9dSRobert Watson }
5372c255e9dSRobert Watson 
5387705d4b2SDmitry Chagin static void
53922ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
5407705d4b2SDmitry Chagin {
5417705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
5427705d4b2SDmitry Chagin 	struct ktr_request *req;
543de60a5f3SDmitry Chagin 	struct thread *td2;
5447705d4b2SDmitry Chagin 
5457705d4b2SDmitry Chagin 	ktrace_assert(td);
5467705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
54722ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
5487705d4b2SDmitry Chagin 	if (req == NULL)
5497705d4b2SDmitry Chagin 		return;
5507705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
5517705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
55222ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
5537705d4b2SDmitry Chagin }
5547705d4b2SDmitry Chagin 
5557705d4b2SDmitry Chagin void
5567705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
5577705d4b2SDmitry Chagin {
5587705d4b2SDmitry Chagin 	struct thread *td = curthread;
5597705d4b2SDmitry Chagin 
5607705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
5617705d4b2SDmitry Chagin 		return;
5627705d4b2SDmitry Chagin 
5637705d4b2SDmitry Chagin 	ktrace_enter(td);
56422ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
5657705d4b2SDmitry Chagin 	ktrace_exit(td);
5667705d4b2SDmitry Chagin }
5677705d4b2SDmitry Chagin 
5682c255e9dSRobert Watson /*
569d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
570d680caabSJohn Baldwin  */
571d680caabSJohn Baldwin void
572d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
573d680caabSJohn Baldwin {
574d680caabSJohn Baldwin 
5757705d4b2SDmitry Chagin 	PROC_LOCK(p1);
576d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
577d680caabSJohn Baldwin 	KASSERT(p2->p_tracevp == NULL, ("new process has a ktrace vnode"));
578d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
579d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
580d680caabSJohn Baldwin 		if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
581d680caabSJohn Baldwin 			VREF(p2->p_tracevp);
582d680caabSJohn Baldwin 			KASSERT(p1->p_tracecred != NULL,
583d680caabSJohn Baldwin 			    ("ktrace vnode with no cred"));
584d680caabSJohn Baldwin 			p2->p_tracecred = crhold(p1->p_tracecred);
585d680caabSJohn Baldwin 		}
586d680caabSJohn Baldwin 	}
587d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5887705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
5897705d4b2SDmitry Chagin 
5907705d4b2SDmitry Chagin 	ktrprocctor(p2);
591d680caabSJohn Baldwin }
592d680caabSJohn Baldwin 
593d680caabSJohn Baldwin /*
5942c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
5952c255e9dSRobert Watson  * system call.
5962c255e9dSRobert Watson  */
5972c255e9dSRobert Watson void
5982c255e9dSRobert Watson ktruserret(struct thread *td)
5992c255e9dSRobert Watson {
6002c255e9dSRobert Watson 
6012c255e9dSRobert Watson 	ktrace_enter(td);
6022c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6032c255e9dSRobert Watson 	ktr_drain(td);
6042c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6052c255e9dSRobert Watson 	ktrace_exit(td);
606df8bae1dSRodney W. Grimes }
607df8bae1dSRodney W. Grimes 
60826f9a767SRodney W. Grimes void
609ea3fc8e4SJohn Baldwin ktrnamei(path)
610df8bae1dSRodney W. Grimes 	char *path;
611df8bae1dSRodney W. Grimes {
612ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
613ea3fc8e4SJohn Baldwin 	int namelen;
6144b3aac3dSJohn Baldwin 	char *buf = NULL;
615df8bae1dSRodney W. Grimes 
6164b3aac3dSJohn Baldwin 	namelen = strlen(path);
6174b3aac3dSJohn Baldwin 	if (namelen > 0) {
618a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
6194b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
6204b3aac3dSJohn Baldwin 	}
621ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
62250c22331SPoul-Henning Kamp 	if (req == NULL) {
62350c22331SPoul-Henning Kamp 		if (buf != NULL)
62450c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
625ea3fc8e4SJohn Baldwin 		return;
62650c22331SPoul-Henning Kamp 	}
627ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
628ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
629d977a583SRobert Watson 		req->ktr_buffer = buf;
630ea3fc8e4SJohn Baldwin 	}
6312c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
632df8bae1dSRodney W. Grimes }
633df8bae1dSRodney W. Grimes 
63426f9a767SRodney W. Grimes void
635a56be37eSJohn Baldwin ktrsysctl(name, namelen)
636a56be37eSJohn Baldwin 	int *name;
637a56be37eSJohn Baldwin 	u_int namelen;
638a56be37eSJohn Baldwin {
639a56be37eSJohn Baldwin 	struct ktr_request *req;
640a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
641a56be37eSJohn Baldwin 	char *mibname;
642a56be37eSJohn Baldwin 	size_t mibnamelen;
643a56be37eSJohn Baldwin 	int error;
644a56be37eSJohn Baldwin 
645a56be37eSJohn Baldwin 	/* Lookup name of mib. */
646a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
647a56be37eSJohn Baldwin 	mib[0] = 0;
648a56be37eSJohn Baldwin 	mib[1] = 1;
649a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
650a56be37eSJohn Baldwin 	mibnamelen = 128;
651a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
652a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
653a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
654a56be37eSJohn Baldwin 	if (error) {
655a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
656a56be37eSJohn Baldwin 		return;
657a56be37eSJohn Baldwin 	}
658a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
659a56be37eSJohn Baldwin 	if (req == NULL) {
660a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
661a56be37eSJohn Baldwin 		return;
662a56be37eSJohn Baldwin 	}
663a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
664a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
665a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
666a56be37eSJohn Baldwin }
667a56be37eSJohn Baldwin 
668a56be37eSJohn Baldwin void
669ea3fc8e4SJohn Baldwin ktrgenio(fd, rw, uio, error)
670df8bae1dSRodney W. Grimes 	int fd;
671df8bae1dSRodney W. Grimes 	enum uio_rw rw;
67242ebfbf2SBrian Feldman 	struct uio *uio;
67342ebfbf2SBrian Feldman 	int error;
674df8bae1dSRodney W. Grimes {
675ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
676ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
677b92584a6SJohn Baldwin 	int datalen;
678b92584a6SJohn Baldwin 	char *buf;
679df8bae1dSRodney W. Grimes 
680552afd9cSPoul-Henning Kamp 	if (error) {
681552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
682df8bae1dSRodney W. Grimes 		return;
683552afd9cSPoul-Henning Kamp 	}
684b92584a6SJohn Baldwin 	uio->uio_offset = 0;
685b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
686526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
687a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
688552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
689552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
690552afd9cSPoul-Henning Kamp 	if (error) {
691b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
692ea3fc8e4SJohn Baldwin 		return;
693b92584a6SJohn Baldwin 	}
694b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
695b92584a6SJohn Baldwin 	if (req == NULL) {
696b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
697b92584a6SJohn Baldwin 		return;
698b92584a6SJohn Baldwin 	}
699ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
700ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
701ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
702b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
703d977a583SRobert Watson 	req->ktr_buffer = buf;
7042c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
705df8bae1dSRodney W. Grimes }
706df8bae1dSRodney W. Grimes 
70726f9a767SRodney W. Grimes void
708ea3fc8e4SJohn Baldwin ktrpsig(sig, action, mask, code)
709a93fdaacSMarcel Moolenaar 	int sig;
710df8bae1dSRodney W. Grimes 	sig_t action;
7112c42a146SMarcel Moolenaar 	sigset_t *mask;
712a93fdaacSMarcel Moolenaar 	int code;
713df8bae1dSRodney W. Grimes {
71422ec0406SDmitry Chagin 	struct thread *td = curthread;
715ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
716ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
717df8bae1dSRodney W. Grimes 
718ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
719ea3fc8e4SJohn Baldwin 	if (req == NULL)
720ea3fc8e4SJohn Baldwin 		return;
721ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
722ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
723ea3fc8e4SJohn Baldwin 	kp->action = action;
724ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
725ea3fc8e4SJohn Baldwin 	kp->code = code;
72622ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
72722ec0406SDmitry Chagin 	ktrace_exit(td);
728df8bae1dSRodney W. Grimes }
729df8bae1dSRodney W. Grimes 
73026f9a767SRodney W. Grimes void
73188bf5036SJohn Baldwin ktrcsw(out, user, wmesg)
732df8bae1dSRodney W. Grimes 	int out, user;
73388bf5036SJohn Baldwin 	const char *wmesg;
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;
74588bf5036SJohn Baldwin 	if (wmesg != NULL)
74688bf5036SJohn Baldwin 		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
74788bf5036SJohn Baldwin 	else
74888bf5036SJohn Baldwin 		bzero(kc->wmesg, sizeof(kc->wmesg));
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;
7817008be5bSPawel Jakub Dawidek 	const cap_rights_t *needed;
7827008be5bSPawel Jakub Dawidek 	const 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;
7933fded357SPawel Jakub Dawidek 	if (needed != NULL)
7947008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
7953fded357SPawel Jakub Dawidek 	else
7963fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
7973fded357SPawel Jakub Dawidek 	if (held != NULL)
7987008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
7993fded357SPawel Jakub Dawidek 	else
8003fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
801c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
802c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
803c601ad8eSDag-Erling Smørgrav }
80435818d2eSJohn Baldwin 
80535818d2eSJohn Baldwin void
80635818d2eSJohn Baldwin ktrfault(vaddr, type)
80735818d2eSJohn Baldwin 	vm_offset_t vaddr;
80835818d2eSJohn Baldwin 	int type;
80935818d2eSJohn Baldwin {
81035818d2eSJohn Baldwin 	struct thread *td = curthread;
81135818d2eSJohn Baldwin 	struct ktr_request *req;
81235818d2eSJohn Baldwin 	struct ktr_fault *kf;
81335818d2eSJohn Baldwin 
81435818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
81535818d2eSJohn Baldwin 	if (req == NULL)
81635818d2eSJohn Baldwin 		return;
81735818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
81835818d2eSJohn Baldwin 	kf->vaddr = vaddr;
81935818d2eSJohn Baldwin 	kf->type = type;
82035818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
82135818d2eSJohn Baldwin 	ktrace_exit(td);
82235818d2eSJohn Baldwin }
82335818d2eSJohn Baldwin 
82435818d2eSJohn Baldwin void
82535818d2eSJohn Baldwin ktrfaultend(result)
82635818d2eSJohn Baldwin 	int result;
82735818d2eSJohn Baldwin {
82835818d2eSJohn Baldwin 	struct thread *td = curthread;
82935818d2eSJohn Baldwin 	struct ktr_request *req;
83035818d2eSJohn Baldwin 	struct ktr_faultend *kf;
83135818d2eSJohn Baldwin 
83235818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
83335818d2eSJohn Baldwin 	if (req == NULL)
83435818d2eSJohn Baldwin 		return;
83535818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
83635818d2eSJohn Baldwin 	kf->result = result;
83735818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
83835818d2eSJohn Baldwin 	ktrace_exit(td);
83935818d2eSJohn Baldwin }
84064cc6a13SJohn Baldwin #endif /* KTRACE */
841df8bae1dSRodney W. Grimes 
842df8bae1dSRodney W. Grimes /* Interface and common routines */
843df8bae1dSRodney W. Grimes 
844d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
845df8bae1dSRodney W. Grimes struct ktrace_args {
846df8bae1dSRodney W. Grimes 	char	*fname;
847df8bae1dSRodney W. Grimes 	int	ops;
848df8bae1dSRodney W. Grimes 	int	facs;
849df8bae1dSRodney W. Grimes 	int	pid;
850df8bae1dSRodney W. Grimes };
851d2d3e875SBruce Evans #endif
852df8bae1dSRodney W. Grimes /* ARGSUSED */
85326f9a767SRodney W. Grimes int
8548451d0ddSKip Macy sys_ktrace(td, uap)
855b40ce416SJulian Elischer 	struct thread *td;
856df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
857df8bae1dSRodney W. Grimes {
858db6a20e2SGarrett Wollman #ifdef KTRACE
859df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
860df8bae1dSRodney W. Grimes 	register struct proc *p;
861df8bae1dSRodney W. Grimes 	struct pgrp *pg;
862df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
863df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
864df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
865400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
8665050aa86SKonstantin Belousov 	int flags, error = 0;
867df8bae1dSRodney W. Grimes 	struct nameidata nd;
868a5881ea5SJohn Baldwin 	struct ucred *cred;
869df8bae1dSRodney W. Grimes 
87064cc6a13SJohn Baldwin 	/*
87164cc6a13SJohn Baldwin 	 * Need something to (un)trace.
87264cc6a13SJohn Baldwin 	 */
87364cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
87464cc6a13SJohn Baldwin 		return (EINVAL);
87564cc6a13SJohn Baldwin 
8762c255e9dSRobert Watson 	ktrace_enter(td);
877df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
878df8bae1dSRodney W. Grimes 		/*
879df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
880df8bae1dSRodney W. Grimes 		 */
8815050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
882e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
8839e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
884797f2d22SPoul-Henning Kamp 		if (error) {
8852c255e9dSRobert Watson 			ktrace_exit(td);
886df8bae1dSRodney W. Grimes 			return (error);
887df8bae1dSRodney W. Grimes 		}
888762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
889df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
89022db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
891df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
892a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
8932c255e9dSRobert Watson 			ktrace_exit(td);
894df8bae1dSRodney W. Grimes 			return (EACCES);
895df8bae1dSRodney W. Grimes 		}
896df8bae1dSRodney W. Grimes 	}
897df8bae1dSRodney W. Grimes 	/*
89879deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
899df8bae1dSRodney W. Grimes 	 */
900df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
90151fd6380SMike Pritchard 		int vrele_count;
90251fd6380SMike Pritchard 
90351fd6380SMike Pritchard 		vrele_count = 0;
9041005a129SJohn Baldwin 		sx_slock(&allproc_lock);
9054f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
906a7ff7443SJohn Baldwin 			PROC_LOCK(p);
907a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
908ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
909ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
910d680caabSJohn Baldwin 					ktr_freeproc(p, &cred, NULL);
911ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
91251fd6380SMike Pritchard 					vrele_count++;
913a5881ea5SJohn Baldwin 					crfree(cred);
91451fd6380SMike Pritchard 				} else
915df8bae1dSRodney W. Grimes 					error = EPERM;
916df8bae1dSRodney W. Grimes 			}
917a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
91879deba82SMatthew Dillon 		}
9191005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
92051fd6380SMike Pritchard 		if (vrele_count > 0) {
92151fd6380SMike Pritchard 			while (vrele_count-- > 0)
92251fd6380SMike Pritchard 				vrele(vp);
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:
9885050aa86SKonstantin Belousov 	if (vp != NULL)
989a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
9902c255e9dSRobert Watson 	ktrace_exit(td);
991df8bae1dSRodney W. Grimes 	return (error);
99264cc6a13SJohn Baldwin #else /* !KTRACE */
99364cc6a13SJohn Baldwin 	return (ENOSYS);
99464cc6a13SJohn Baldwin #endif /* KTRACE */
995df8bae1dSRodney W. Grimes }
996df8bae1dSRodney W. Grimes 
997e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
998e6c4b9baSPoul-Henning Kamp int
9998451d0ddSKip Macy sys_utrace(td, uap)
1000b40ce416SJulian Elischer 	struct thread *td;
1001e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
1002e6c4b9baSPoul-Henning Kamp {
1003b40ce416SJulian Elischer 
1004e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1005ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
10067f05b035SAlfred Perlstein 	void *cp;
1007c9e7d28eSJohn Baldwin 	int error;
1008e6c4b9baSPoul-Henning Kamp 
1009c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1010c9e7d28eSJohn Baldwin 		return (0);
1011bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
10120bad156aSAlfred Perlstein 		return (EINVAL);
1013a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1014c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
101550c22331SPoul-Henning Kamp 	if (error) {
101650c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1017c9e7d28eSJohn Baldwin 		return (error);
101850c22331SPoul-Henning Kamp 	}
1019ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
102050c22331SPoul-Henning Kamp 	if (req == NULL) {
102150c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1022b10221ffSJoseph Koshy 		return (ENOMEM);
102350c22331SPoul-Henning Kamp 	}
1024d977a583SRobert Watson 	req->ktr_buffer = cp;
1025ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
10262c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1027e6c4b9baSPoul-Henning Kamp 	return (0);
102864cc6a13SJohn Baldwin #else /* !KTRACE */
1029e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
103064cc6a13SJohn Baldwin #endif /* KTRACE */
1031e6c4b9baSPoul-Henning Kamp }
1032e6c4b9baSPoul-Henning Kamp 
1033db6a20e2SGarrett Wollman #ifdef KTRACE
103487b6de2bSPoul-Henning Kamp static int
1035a7ff7443SJohn Baldwin ktrops(td, p, ops, facs, vp)
1036a7ff7443SJohn Baldwin 	struct thread *td;
1037a7ff7443SJohn Baldwin 	struct proc *p;
1038df8bae1dSRodney W. Grimes 	int ops, facs;
1039df8bae1dSRodney W. Grimes 	struct vnode *vp;
1040df8bae1dSRodney W. Grimes {
1041ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
1042a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
1043df8bae1dSRodney W. Grimes 
1044fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1045a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1046a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1047df8bae1dSRodney W. Grimes 		return (0);
1048a7ff7443SJohn Baldwin 	}
1049fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1050fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1051fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1052fe41d17aSJohn Baldwin 		return (1);
1053fe41d17aSJohn Baldwin 	}
1054ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1055df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
1056a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
1057df8bae1dSRodney W. Grimes 			/*
1058a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
1059df8bae1dSRodney W. Grimes 			 */
1060a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
1061ea3fc8e4SJohn Baldwin 			VREF(vp);
1062a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
1063a5881ea5SJohn Baldwin 		}
1064a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
1065a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
1066a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
1067df8bae1dSRodney W. Grimes 		}
1068df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
106932f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1070df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1071df8bae1dSRodney W. Grimes 	} else {
1072df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1073d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1074df8bae1dSRodney W. Grimes 			/* no more tracing */
1075d680caabSJohn Baldwin 			ktr_freeproc(p, &tracecred, &tracevp);
1076a7ff7443SJohn Baldwin 	}
1077ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
107822ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
107922ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1080a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
10815050aa86SKonstantin Belousov 	if (tracevp != NULL)
1082ea3fc8e4SJohn Baldwin 		vrele(tracevp);
1083a5881ea5SJohn Baldwin 	if (tracecred != NULL)
1084a5881ea5SJohn Baldwin 		crfree(tracecred);
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes 	return (1);
1087df8bae1dSRodney W. Grimes }
1088df8bae1dSRodney W. Grimes 
108987b6de2bSPoul-Henning Kamp static int
1090a7ff7443SJohn Baldwin ktrsetchildren(td, top, ops, facs, vp)
1091a7ff7443SJohn Baldwin 	struct thread *td;
1092a7ff7443SJohn Baldwin 	struct proc *top;
1093df8bae1dSRodney W. Grimes 	int ops, facs;
1094df8bae1dSRodney W. Grimes 	struct vnode *vp;
1095df8bae1dSRodney W. Grimes {
1096df8bae1dSRodney W. Grimes 	register struct proc *p;
1097df8bae1dSRodney W. Grimes 	register int ret = 0;
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes 	p = top;
1100fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
110164cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1102df8bae1dSRodney W. Grimes 	for (;;) {
1103a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
1104df8bae1dSRodney W. Grimes 		/*
1105df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1106df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1107df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1108df8bae1dSRodney W. Grimes 		 */
11092e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
11102e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1111df8bae1dSRodney W. Grimes 		else for (;;) {
111264cc6a13SJohn Baldwin 			if (p == top)
1113df8bae1dSRodney W. Grimes 				return (ret);
11142e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
11152e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1116df8bae1dSRodney W. Grimes 				break;
1117df8bae1dSRodney W. Grimes 			}
1118b75356e1SJeffrey Hsu 			p = p->p_pptr;
1119df8bae1dSRodney W. Grimes 		}
1120fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1121df8bae1dSRodney W. Grimes 	}
1122df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1123df8bae1dSRodney W. Grimes }
1124df8bae1dSRodney W. Grimes 
112587b6de2bSPoul-Henning Kamp static void
11262c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1127df8bae1dSRodney W. Grimes {
1128ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1129ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1130ea3fc8e4SJohn Baldwin 	struct proc *p;
1131ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1132df8bae1dSRodney W. Grimes 	struct uio auio;
1133ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1134f2a2857bSKirk McKusick 	struct mount *mp;
1135ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
11365050aa86SKonstantin Belousov 	int error;
1137df8bae1dSRodney W. Grimes 
11382c255e9dSRobert Watson 	/*
11392c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
11402c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
11412c255e9dSRobert Watson 	 *
11422c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
11432c255e9dSRobert Watson 	 * the vnode has been closed.
11442c255e9dSRobert Watson 	 */
11452c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
11462c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
11472c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
11482c255e9dSRobert Watson 
1149ea3fc8e4SJohn Baldwin 	/*
1150ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
11512c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
11522c255e9dSRobert Watson 	 * in sync: we should have both or neither.
1153ea3fc8e4SJohn Baldwin 	 */
11542c255e9dSRobert Watson 	if (vp == NULL) {
11552c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1156118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1157df8bae1dSRodney W. Grimes 		return;
11582c255e9dSRobert Watson 	}
1159118258f5SBjoern A. Zeeb 	VREF(vp);
11602c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1161118258f5SBjoern A. Zeeb 	crhold(cred);
1162118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
11632c255e9dSRobert Watson 
1164ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
1165a56be37eSJohn Baldwin 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) <
1166a56be37eSJohn Baldwin 	    sizeof(data_lengths) / sizeof(data_lengths[0]),
1167a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
11688b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1169ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1170df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1171df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1172df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1173df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1174df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1175df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1176df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1177df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1178ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1179ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1180ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1181ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1182ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1183df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1184ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1185ea3fc8e4SJohn Baldwin 	}
1186ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1187d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1188d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1189ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1190ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1191ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1192b92584a6SJohn Baldwin 	}
11932c255e9dSRobert Watson 
1194f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1195cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1196467a273cSRobert Watson #ifdef MAC
119730d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1198467a273cSRobert Watson 	if (error == 0)
1199467a273cSRobert Watson #endif
1200ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
120122db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
1202f2a2857bSKirk McKusick 	vn_finished_write(mp);
1203118258f5SBjoern A. Zeeb 	crfree(cred);
1204118258f5SBjoern A. Zeeb 	if (!error) {
1205704c9f00SJohn Baldwin 		vrele(vp);
1206df8bae1dSRodney W. Grimes 		return;
1207118258f5SBjoern A. Zeeb 	}
1208118258f5SBjoern A. Zeeb 
1209df8bae1dSRodney W. Grimes 	/*
1210ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1211ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1212ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1213118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1214118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1215118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1216118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1217df8bae1dSRodney W. Grimes 	 */
1218df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1219df8bae1dSRodney W. Grimes 	    error);
1220118258f5SBjoern A. Zeeb 	vrele_count = 1;
1221ea3fc8e4SJohn Baldwin 	/*
1222ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1223ea3fc8e4SJohn Baldwin 	 * system.
1224ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1225ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1226ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1227ea3fc8e4SJohn Baldwin 	 */
1228a5881ea5SJohn Baldwin 	cred = NULL;
12291005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12304f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1231ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1232a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1233ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1234d680caabSJohn Baldwin 			ktr_freeproc(p, &cred, NULL);
1235ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1236ea3fc8e4SJohn Baldwin 			vrele_count++;
1237df8bae1dSRodney W. Grimes 		}
1238ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1239a5881ea5SJohn Baldwin 		if (cred != NULL) {
1240a5881ea5SJohn Baldwin 			crfree(cred);
1241a5881ea5SJohn Baldwin 			cred = NULL;
1242a5881ea5SJohn Baldwin 		}
1243df8bae1dSRodney W. Grimes 	}
12441005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12452c255e9dSRobert Watson 
1246ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1247ea3fc8e4SJohn Baldwin 		vrele(vp);
1248df8bae1dSRodney W. Grimes }
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes /*
1251df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1252df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1253df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1254df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1255df8bae1dSRodney W. Grimes  * so, only root may further change it.
1256df8bae1dSRodney W. Grimes  */
125787b6de2bSPoul-Henning Kamp static int
1258a7ff7443SJohn Baldwin ktrcanset(td, targetp)
1259a7ff7443SJohn Baldwin 	struct thread *td;
1260a7ff7443SJohn Baldwin 	struct proc *targetp;
1261df8bae1dSRodney W. Grimes {
1262df8bae1dSRodney W. Grimes 
1263a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1264a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
126532f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
126675c13541SPoul-Henning Kamp 		return (0);
1267a0f75161SRobert Watson 
1268f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1269a0f75161SRobert Watson 		return (0);
1270a0f75161SRobert Watson 
1271df8bae1dSRodney W. Grimes 	return (1);
1272df8bae1dSRodney W. Grimes }
1273df8bae1dSRodney W. Grimes 
1274db6a20e2SGarrett Wollman #endif /* KTRACE */
1275