xref: /freebsd/sys/kern/kern_ktrace.c (revision 0a1427c5ab7b7524590dd98fa6ed822d6c9d6fa1)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
52c255e9dSRobert Watson  *	The Regents of the University of California.
62c255e9dSRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
72c255e9dSRobert Watson  * All rights reserved.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1769a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
39db6a20e2SGarrett Wollman #include "opt_ktrace.h"
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
424a144410SRobert Watson #include <sys/capsicum.h>
43f23b4c91SGarrett Wollman #include <sys/systm.h>
44ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
45ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
46ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/mutex.h>
49ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
50033eb86eSJeff Roberson #include <sys/mount.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53ea3fc8e4SJohn Baldwin #include <sys/proc.h>
54ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5660e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5760e15db9SDag-Erling Smørgrav #include <sys/stat.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
591005a129SJohn Baldwin #include <sys/sx.h>
60ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
617705d4b2SDmitry Chagin #include <sys/sysent.h>
62df8bae1dSRodney W. Grimes #include <sys/syslog.h>
63ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
64df8bae1dSRodney W. Grimes 
65aed55708SRobert Watson #include <security/mac/mac_framework.h>
66aed55708SRobert Watson 
672c255e9dSRobert Watson /*
682c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
692c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
702c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
712c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
722c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
732c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
742c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
752c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
762c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
772c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
782c255e9dSRobert Watson  *
792c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
802c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
812c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
822c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
832c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
842c255e9dSRobert Watson  */
852c255e9dSRobert Watson 
86a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8755166637SPoul-Henning Kamp 
88db6a20e2SGarrett Wollman #ifdef KTRACE
89ea3fc8e4SJohn Baldwin 
90de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
91de5b1952SAlexander Leidinger 
92ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
93ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
94ea3fc8e4SJohn Baldwin #endif
95ea3fc8e4SJohn Baldwin 
96ea3fc8e4SJohn Baldwin struct ktr_request {
97ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
98d977a583SRobert Watson 	void	*ktr_buffer;
99ea3fc8e4SJohn Baldwin 	union {
1007705d4b2SDmitry Chagin 		struct	ktr_proc_ctor ktr_proc_ctor;
101c601ad8eSDag-Erling Smørgrav 		struct	ktr_cap_fail ktr_cap_fail;
102ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
103ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
104ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
105ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
106ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
10735818d2eSJohn Baldwin 		struct	ktr_fault ktr_fault;
10835818d2eSJohn Baldwin 		struct	ktr_faultend ktr_faultend;
109ffb66079SJohn Baldwin 		struct  ktr_struct_array ktr_struct_array;
110ea3fc8e4SJohn Baldwin 	} ktr_data;
111ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
112ea3fc8e4SJohn Baldwin };
113ea3fc8e4SJohn Baldwin 
114ea3fc8e4SJohn Baldwin static int data_lengths[] = {
115093e059cSJilles Tjoelker 	[KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
116093e059cSJilles Tjoelker 	[KTR_SYSRET] = sizeof(struct ktr_sysret),
117093e059cSJilles Tjoelker 	[KTR_NAMEI] = 0,
118093e059cSJilles Tjoelker 	[KTR_GENIO] = sizeof(struct ktr_genio),
119093e059cSJilles Tjoelker 	[KTR_PSIG] = sizeof(struct ktr_psig),
120093e059cSJilles Tjoelker 	[KTR_CSW] = sizeof(struct ktr_csw),
121093e059cSJilles Tjoelker 	[KTR_USER] = 0,
122093e059cSJilles Tjoelker 	[KTR_STRUCT] = 0,
123093e059cSJilles Tjoelker 	[KTR_SYSCTL] = 0,
124093e059cSJilles Tjoelker 	[KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
125093e059cSJilles Tjoelker 	[KTR_PROCDTOR] = 0,
126093e059cSJilles Tjoelker 	[KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
127093e059cSJilles Tjoelker 	[KTR_FAULT] = sizeof(struct ktr_fault),
128093e059cSJilles Tjoelker 	[KTR_FAULTEND] = sizeof(struct ktr_faultend),
129ffb66079SJohn Baldwin 	[KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
130ea3fc8e4SJohn Baldwin };
131ea3fc8e4SJohn Baldwin 
132ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
133ea3fc8e4SJohn Baldwin 
1345ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
13512301fc3SJohn Baldwin 
1368b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
13712301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
13812301fc3SJohn Baldwin 
1391e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE;
140af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
14112301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
142ea3fc8e4SJohn Baldwin 
143ea3fc8e4SJohn Baldwin static int print_message = 1;
144d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1452c255e9dSRobert Watson static struct sx ktrace_sx;
146ea3fc8e4SJohn Baldwin 
147ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
148ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
149b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
15022ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
151ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1522c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
153d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc,
154d680caabSJohn Baldwin     struct vnode **vp);
155ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
156d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1572c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
158a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
159a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
160a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
16122ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
16298d93822SBruce Evans 
1632c255e9dSRobert Watson /*
1642c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1652c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1662c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1672c255e9dSRobert Watson  * suppressed.
1682c255e9dSRobert Watson  */
1692c255e9dSRobert Watson static void
1702c255e9dSRobert Watson ktrace_enter(struct thread *td)
1712c255e9dSRobert Watson {
1722c255e9dSRobert Watson 
1732c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1742c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1752c255e9dSRobert Watson }
1762c255e9dSRobert Watson 
1772c255e9dSRobert Watson static void
1782c255e9dSRobert Watson ktrace_exit(struct thread *td)
1792c255e9dSRobert Watson {
1802c255e9dSRobert Watson 
1812c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1822c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1832c255e9dSRobert Watson }
1842c255e9dSRobert Watson 
1852c255e9dSRobert Watson static void
1862c255e9dSRobert Watson ktrace_assert(struct thread *td)
1872c255e9dSRobert Watson {
1882c255e9dSRobert Watson 
1892c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1902c255e9dSRobert Watson }
1912c255e9dSRobert Watson 
192ea3fc8e4SJohn Baldwin static void
193ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
194df8bae1dSRodney W. Grimes {
195ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
196ea3fc8e4SJohn Baldwin 	int i;
197df8bae1dSRodney W. Grimes 
198ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1992c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
200ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
201ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
202a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
203ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
204ea3fc8e4SJohn Baldwin 	}
205ea3fc8e4SJohn Baldwin }
206ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
207ea3fc8e4SJohn Baldwin 
208ea3fc8e4SJohn Baldwin static int
209ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
210ea3fc8e4SJohn Baldwin {
211ea3fc8e4SJohn Baldwin 	struct thread *td;
2128b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
213ea3fc8e4SJohn Baldwin 	int error;
214ea3fc8e4SJohn Baldwin 
215ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
216ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
217ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2188b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
219ea3fc8e4SJohn Baldwin 	}
220ea3fc8e4SJohn Baldwin 
2218b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
222ea3fc8e4SJohn Baldwin 	if (error)
223ea3fc8e4SJohn Baldwin 		return (error);
224ea3fc8e4SJohn Baldwin 	td = curthread;
2252c255e9dSRobert Watson 	ktrace_enter(td);
226ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
227b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2282c255e9dSRobert Watson 	ktrace_exit(td);
2298b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
230ea3fc8e4SJohn Baldwin 	if (error)
231ea3fc8e4SJohn Baldwin 		return (error);
232a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
233ea3fc8e4SJohn Baldwin 		return (ENOSPC);
234ea3fc8e4SJohn Baldwin 	return (0);
235ea3fc8e4SJohn Baldwin }
23612301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
237a0c87b74SGavin Atkinson     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
238a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
239ea3fc8e4SJohn Baldwin 
2408b149b51SJohn Baldwin static u_int
241b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
242ea3fc8e4SJohn Baldwin {
243b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
244ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
245a5896914SJoseph Koshy 	int bound;
246ea3fc8e4SJohn Baldwin 
247ea3fc8e4SJohn Baldwin 	print_message = 1;
248b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
249a5896914SJoseph Koshy 	if (bound == 0)
250a5896914SJoseph Koshy 		return (ktr_requestpool);
251b4c20e5eSDmitry Chagin 	if (bound < 0) {
252b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
253ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
254a5896914SJoseph Koshy 		while (bound++ < 0) {
255ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
256ea3fc8e4SJohn Baldwin 			if (req == NULL)
257b4c20e5eSDmitry Chagin 				break;
258ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
259ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
260ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
261ea3fc8e4SJohn Baldwin 		}
262b4c20e5eSDmitry Chagin 	} else {
263ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
264b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
265a5896914SJoseph Koshy 		while (bound-- > 0) {
266ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
267a163d034SWarner Losh 			    M_WAITOK);
268b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
269ea3fc8e4SJohn Baldwin 		}
270b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
271b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
272b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
273b4c20e5eSDmitry Chagin 	}
274b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
275ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
276ea3fc8e4SJohn Baldwin }
277ea3fc8e4SJohn Baldwin 
2785ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2795ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2805ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2815ca4819dSJohn Baldwin 
282ea3fc8e4SJohn Baldwin static struct ktr_request *
28322ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
284ea3fc8e4SJohn Baldwin {
285ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
286ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
287ea3fc8e4SJohn Baldwin 	int pm;
288ea3fc8e4SJohn Baldwin 
289c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
290ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
291c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
292ea3fc8e4SJohn Baldwin 		return (NULL);
293ea3fc8e4SJohn Baldwin 	}
294ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
295ea3fc8e4SJohn Baldwin 	if (req != NULL) {
296ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
297ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
29875768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
29975768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
30075768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
30175768576SJohn Baldwin 		}
302c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
303ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
304ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3052bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3065ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3075ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
308d977a583SRobert Watson 		req->ktr_buffer = NULL;
309ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
310ea3fc8e4SJohn Baldwin 	} else {
31175768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
312ea3fc8e4SJohn Baldwin 		pm = print_message;
313ea3fc8e4SJohn Baldwin 		print_message = 0;
314ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
315ea3fc8e4SJohn Baldwin 		if (pm)
316ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
317ea3fc8e4SJohn Baldwin 	}
318ea3fc8e4SJohn Baldwin 	return (req);
319ea3fc8e4SJohn Baldwin }
320ea3fc8e4SJohn Baldwin 
3217705d4b2SDmitry Chagin static struct ktr_request *
3227705d4b2SDmitry Chagin ktr_getrequest(int type)
3237705d4b2SDmitry Chagin {
3247705d4b2SDmitry Chagin 	struct thread *td = curthread;
3257705d4b2SDmitry Chagin 	struct ktr_request *req;
3267705d4b2SDmitry Chagin 
3277705d4b2SDmitry Chagin 	ktrace_enter(td);
32822ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3297705d4b2SDmitry Chagin 	if (req == NULL)
3307705d4b2SDmitry Chagin 		ktrace_exit(td);
3317705d4b2SDmitry Chagin 
3327705d4b2SDmitry Chagin 	return (req);
3337705d4b2SDmitry Chagin }
3347705d4b2SDmitry Chagin 
3352c255e9dSRobert Watson /*
3362c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3372c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3382c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3392c255e9dSRobert Watson  * later.
3402c255e9dSRobert Watson  */
341ea3fc8e4SJohn Baldwin static void
3422c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
343ea3fc8e4SJohn Baldwin {
344ea3fc8e4SJohn Baldwin 
345ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3462c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
347ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3482c255e9dSRobert Watson }
3492c255e9dSRobert Watson 
3502c255e9dSRobert Watson /*
3512c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3522c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3532c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3542c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
355a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3562c255e9dSRobert Watson  */
3572c255e9dSRobert Watson static void
3582c255e9dSRobert Watson ktr_drain(struct thread *td)
3592c255e9dSRobert Watson {
3602c255e9dSRobert Watson 	struct ktr_request *queued_req;
3612c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3622c255e9dSRobert Watson 
3632c255e9dSRobert Watson 	ktrace_assert(td);
3642c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3652c255e9dSRobert Watson 
3662b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3672c255e9dSRobert Watson 
3682c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3692c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3702c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3712c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3722c255e9dSRobert Watson 
3732c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3742c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3752c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3762c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3772c255e9dSRobert Watson 		}
3782c255e9dSRobert Watson 	}
3792c255e9dSRobert Watson }
3802c255e9dSRobert Watson 
3812c255e9dSRobert Watson /*
3822c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3832c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3842c255e9dSRobert Watson  * been cached in the thread.
3852c255e9dSRobert Watson  */
3862c255e9dSRobert Watson static void
38722ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
3882c255e9dSRobert Watson {
3892c255e9dSRobert Watson 
3902c255e9dSRobert Watson 	ktrace_assert(td);
3912c255e9dSRobert Watson 
3922c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3932c255e9dSRobert Watson 	ktr_drain(td);
3942c255e9dSRobert Watson 	ktr_writerequest(td, req);
3952c255e9dSRobert Watson 	ktr_freerequest(req);
3962c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3972c255e9dSRobert Watson 	ktrace_exit(td);
398ea3fc8e4SJohn Baldwin }
399ea3fc8e4SJohn Baldwin 
400ea3fc8e4SJohn Baldwin static void
401ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
402ea3fc8e4SJohn Baldwin {
403ea3fc8e4SJohn Baldwin 
404d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
405d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
406d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
407d680caabSJohn Baldwin }
408d680caabSJohn Baldwin 
409d680caabSJohn Baldwin static void
410d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
411d680caabSJohn Baldwin {
412d680caabSJohn Baldwin 
413d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
414d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
415d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
416ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
417d680caabSJohn Baldwin }
418d680caabSJohn Baldwin 
419d680caabSJohn Baldwin /*
420d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
421d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
422d680caabSJohn Baldwin  * vnode and credentials.
423d680caabSJohn Baldwin  */
424d680caabSJohn Baldwin static void
425d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
426d680caabSJohn Baldwin {
427d680caabSJohn Baldwin 	struct ktr_request *req;
428d680caabSJohn Baldwin 
429d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
430d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
431d680caabSJohn Baldwin 	*uc = p->p_tracecred;
432d680caabSJohn Baldwin 	p->p_tracecred = NULL;
433d680caabSJohn Baldwin 	if (vp != NULL)
434d680caabSJohn Baldwin 		*vp = p->p_tracevp;
435d680caabSJohn Baldwin 	p->p_tracevp = NULL;
436d680caabSJohn Baldwin 	p->p_traceflag = 0;
437d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
438d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
439d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
440d680caabSJohn Baldwin 	}
441ea3fc8e4SJohn Baldwin }
442ea3fc8e4SJohn Baldwin 
44326f9a767SRodney W. Grimes void
444039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[])
445df8bae1dSRodney W. Grimes {
446ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
447df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
448ea3fc8e4SJohn Baldwin 	size_t buflen;
4494b3aac3dSJohn Baldwin 	char *buf = NULL;
450df8bae1dSRodney W. Grimes 
451ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
452ad738f37SMatt Macy 		return;
453ad738f37SMatt Macy 
4544b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
4554b3aac3dSJohn Baldwin 	if (buflen > 0) {
456a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
4574b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
4584b3aac3dSJohn Baldwin 	}
459ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
46050c22331SPoul-Henning Kamp 	if (req == NULL) {
46150c22331SPoul-Henning Kamp 		if (buf != NULL)
46250c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
463ea3fc8e4SJohn Baldwin 		return;
46450c22331SPoul-Henning Kamp 	}
465ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
466df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
467df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
468ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
469ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
470d977a583SRobert Watson 		req->ktr_buffer = buf;
471ea3fc8e4SJohn Baldwin 	}
4722c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
473df8bae1dSRodney W. Grimes }
474df8bae1dSRodney W. Grimes 
47526f9a767SRodney W. Grimes void
476039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
477df8bae1dSRodney W. Grimes {
478ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
479ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
480df8bae1dSRodney W. Grimes 
481ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
482ad738f37SMatt Macy 		return;
483ad738f37SMatt Macy 
484ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
485ea3fc8e4SJohn Baldwin 	if (req == NULL)
486ea3fc8e4SJohn Baldwin 		return;
487ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
488ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
489ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
4905a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
4912c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4922c255e9dSRobert Watson }
4932c255e9dSRobert Watson 
4942c255e9dSRobert Watson /*
495d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
496d680caabSJohn Baldwin  *
497d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
498d680caabSJohn Baldwin  */
499d680caabSJohn Baldwin void
500d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
501d680caabSJohn Baldwin {
502d680caabSJohn Baldwin 
503d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
504d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
505d680caabSJohn Baldwin 	ktr_freeproc(p, uc, vp);
506d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
507d680caabSJohn Baldwin }
508d680caabSJohn Baldwin 
509d680caabSJohn Baldwin /*
510d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
511d680caabSJohn Baldwin  * and disable tracing.
5122c255e9dSRobert Watson  */
5132c255e9dSRobert Watson void
5142c255e9dSRobert Watson ktrprocexit(struct thread *td)
5152c255e9dSRobert Watson {
5167705d4b2SDmitry Chagin 	struct ktr_request *req;
517d680caabSJohn Baldwin 	struct proc *p;
518d680caabSJohn Baldwin 	struct ucred *cred;
519d680caabSJohn Baldwin 	struct vnode *vp;
520d680caabSJohn Baldwin 
521d680caabSJohn Baldwin 	p = td->td_proc;
522d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
523d680caabSJohn Baldwin 		return;
5242c255e9dSRobert Watson 
5252c255e9dSRobert Watson 	ktrace_enter(td);
52622ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
52722ec0406SDmitry Chagin 	if (req != NULL)
52822ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
5292c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
5302c255e9dSRobert Watson 	ktr_drain(td);
5312c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
532d680caabSJohn Baldwin 	PROC_LOCK(p);
533d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
534d680caabSJohn Baldwin 	ktr_freeproc(p, &cred, &vp);
535d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
536d680caabSJohn Baldwin 	PROC_UNLOCK(p);
5375050aa86SKonstantin Belousov 	if (vp != NULL)
538d680caabSJohn Baldwin 		vrele(vp);
539d680caabSJohn Baldwin 	if (cred != NULL)
540d680caabSJohn Baldwin 		crfree(cred);
5412c255e9dSRobert Watson 	ktrace_exit(td);
5422c255e9dSRobert Watson }
5432c255e9dSRobert Watson 
5447705d4b2SDmitry Chagin static void
54522ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
5467705d4b2SDmitry Chagin {
5477705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
5487705d4b2SDmitry Chagin 	struct ktr_request *req;
549de60a5f3SDmitry Chagin 	struct thread *td2;
5507705d4b2SDmitry Chagin 
5517705d4b2SDmitry Chagin 	ktrace_assert(td);
5527705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
55322ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
5547705d4b2SDmitry Chagin 	if (req == NULL)
5557705d4b2SDmitry Chagin 		return;
5567705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
5577705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
55822ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
5597705d4b2SDmitry Chagin }
5607705d4b2SDmitry Chagin 
5617705d4b2SDmitry Chagin void
5627705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
5637705d4b2SDmitry Chagin {
5647705d4b2SDmitry Chagin 	struct thread *td = curthread;
5657705d4b2SDmitry Chagin 
5667705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
5677705d4b2SDmitry Chagin 		return;
5687705d4b2SDmitry Chagin 
5697705d4b2SDmitry Chagin 	ktrace_enter(td);
57022ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
5717705d4b2SDmitry Chagin 	ktrace_exit(td);
5727705d4b2SDmitry Chagin }
5737705d4b2SDmitry Chagin 
5742c255e9dSRobert Watson /*
575d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
576d680caabSJohn Baldwin  */
577d680caabSJohn Baldwin void
578d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
579d680caabSJohn Baldwin {
580d680caabSJohn Baldwin 
5817c34b35bSMateusz Guzik 	MPASS(p2->p_tracevp == NULL);
5827c34b35bSMateusz Guzik 	MPASS(p2->p_traceflag == 0);
5837c34b35bSMateusz Guzik 
5847c34b35bSMateusz Guzik 	if (p1->p_traceflag == 0)
5857c34b35bSMateusz Guzik 		return;
5867c34b35bSMateusz Guzik 
5877705d4b2SDmitry Chagin 	PROC_LOCK(p1);
588d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
589d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
590d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
591d680caabSJohn Baldwin 		if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
592d680caabSJohn Baldwin 			VREF(p2->p_tracevp);
593d680caabSJohn Baldwin 			KASSERT(p1->p_tracecred != NULL,
594d680caabSJohn Baldwin 			    ("ktrace vnode with no cred"));
595d680caabSJohn Baldwin 			p2->p_tracecred = crhold(p1->p_tracecred);
596d680caabSJohn Baldwin 		}
597d680caabSJohn Baldwin 	}
598d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5997705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
6007705d4b2SDmitry Chagin 
6017705d4b2SDmitry Chagin 	ktrprocctor(p2);
602d680caabSJohn Baldwin }
603d680caabSJohn Baldwin 
604d680caabSJohn Baldwin /*
6052c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
6062c255e9dSRobert Watson  * system call.
6072c255e9dSRobert Watson  */
6082c255e9dSRobert Watson void
6092c255e9dSRobert Watson ktruserret(struct thread *td)
6102c255e9dSRobert Watson {
6112c255e9dSRobert Watson 
6122c255e9dSRobert Watson 	ktrace_enter(td);
6132c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6142c255e9dSRobert Watson 	ktr_drain(td);
6152c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6162c255e9dSRobert Watson 	ktrace_exit(td);
617df8bae1dSRodney W. Grimes }
618df8bae1dSRodney W. Grimes 
61926f9a767SRodney W. Grimes void
620ea3fc8e4SJohn Baldwin ktrnamei(path)
621df8bae1dSRodney W. Grimes 	char *path;
622df8bae1dSRodney W. Grimes {
623ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
624ea3fc8e4SJohn Baldwin 	int namelen;
6254b3aac3dSJohn Baldwin 	char *buf = NULL;
626df8bae1dSRodney W. Grimes 
6274b3aac3dSJohn Baldwin 	namelen = strlen(path);
6284b3aac3dSJohn Baldwin 	if (namelen > 0) {
629a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
6304b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
6314b3aac3dSJohn Baldwin 	}
632ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
63350c22331SPoul-Henning Kamp 	if (req == NULL) {
63450c22331SPoul-Henning Kamp 		if (buf != NULL)
63550c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
636ea3fc8e4SJohn Baldwin 		return;
63750c22331SPoul-Henning Kamp 	}
638ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
639ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
640d977a583SRobert Watson 		req->ktr_buffer = buf;
641ea3fc8e4SJohn Baldwin 	}
6422c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
643df8bae1dSRodney W. Grimes }
644df8bae1dSRodney W. Grimes 
64526f9a767SRodney W. Grimes void
646039644ecSEd Maste ktrsysctl(int *name, u_int namelen)
647a56be37eSJohn Baldwin {
648a56be37eSJohn Baldwin 	struct ktr_request *req;
649a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
650a56be37eSJohn Baldwin 	char *mibname;
651a56be37eSJohn Baldwin 	size_t mibnamelen;
652a56be37eSJohn Baldwin 	int error;
653a56be37eSJohn Baldwin 
654a56be37eSJohn Baldwin 	/* Lookup name of mib. */
655a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
656a56be37eSJohn Baldwin 	mib[0] = 0;
657a56be37eSJohn Baldwin 	mib[1] = 1;
658a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
659a56be37eSJohn Baldwin 	mibnamelen = 128;
660a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
661a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
662a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
663a56be37eSJohn Baldwin 	if (error) {
664a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
665a56be37eSJohn Baldwin 		return;
666a56be37eSJohn Baldwin 	}
667a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
668a56be37eSJohn Baldwin 	if (req == NULL) {
669a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
670a56be37eSJohn Baldwin 		return;
671a56be37eSJohn Baldwin 	}
672a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
673a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
674a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
675a56be37eSJohn Baldwin }
676a56be37eSJohn Baldwin 
677a56be37eSJohn Baldwin void
678039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
679df8bae1dSRodney W. Grimes {
680ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
681ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
682b92584a6SJohn Baldwin 	int datalen;
683b92584a6SJohn Baldwin 	char *buf;
684df8bae1dSRodney W. Grimes 
685552afd9cSPoul-Henning Kamp 	if (error) {
686552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
687df8bae1dSRodney W. Grimes 		return;
688552afd9cSPoul-Henning Kamp 	}
689b92584a6SJohn Baldwin 	uio->uio_offset = 0;
690b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
691526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
692a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
693552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
694552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
695552afd9cSPoul-Henning Kamp 	if (error) {
696b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
697ea3fc8e4SJohn Baldwin 		return;
698b92584a6SJohn Baldwin 	}
699b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
700b92584a6SJohn Baldwin 	if (req == NULL) {
701b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
702b92584a6SJohn Baldwin 		return;
703b92584a6SJohn Baldwin 	}
704ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
705ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
706ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
707b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
708d977a583SRobert Watson 	req->ktr_buffer = buf;
7092c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
710df8bae1dSRodney W. Grimes }
711df8bae1dSRodney W. Grimes 
71226f9a767SRodney W. Grimes void
713039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, 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
732039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg)
733df8bae1dSRodney W. Grimes {
73422ec0406SDmitry Chagin 	struct thread *td = curthread;
735ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
736ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
737df8bae1dSRodney W. Grimes 
738ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
739ad738f37SMatt Macy 		return;
740ad738f37SMatt Macy 
741ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
742ea3fc8e4SJohn Baldwin 	if (req == NULL)
743ea3fc8e4SJohn Baldwin 		return;
744ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
745ea3fc8e4SJohn Baldwin 	kc->out = out;
746ea3fc8e4SJohn Baldwin 	kc->user = user;
74788bf5036SJohn Baldwin 	if (wmesg != NULL)
74888bf5036SJohn Baldwin 		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
74988bf5036SJohn Baldwin 	else
75088bf5036SJohn Baldwin 		bzero(kc->wmesg, sizeof(kc->wmesg));
75122ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
75222ec0406SDmitry Chagin 	ktrace_exit(td);
753df8bae1dSRodney W. Grimes }
75460e15db9SDag-Erling Smørgrav 
75560e15db9SDag-Erling Smørgrav void
756ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen)
75760e15db9SDag-Erling Smørgrav {
75860e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
7594dd3a21fSMateusz Guzik 	char *buf;
7604dd3a21fSMateusz Guzik 	size_t buflen, namelen;
76160e15db9SDag-Erling Smørgrav 
762ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
763ad738f37SMatt Macy 		return;
764ad738f37SMatt Macy 
7654dd3a21fSMateusz Guzik 	if (data == NULL)
76660e15db9SDag-Erling Smørgrav 		datalen = 0;
7674dd3a21fSMateusz Guzik 	namelen = strlen(name) + 1;
7684dd3a21fSMateusz Guzik 	buflen = namelen + datalen;
76960e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
770a3052d6eSJohn Baldwin 	strcpy(buf, name);
7714dd3a21fSMateusz Guzik 	bcopy(data, buf + namelen, datalen);
77260e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
77360e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
77460e15db9SDag-Erling Smørgrav 		return;
77560e15db9SDag-Erling Smørgrav 	}
77660e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
77760e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
77860e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
77960e15db9SDag-Erling Smørgrav }
780c601ad8eSDag-Erling Smørgrav 
781c601ad8eSDag-Erling Smørgrav void
782*0a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
783*0a1427c5SMateusz Guzik {
784*0a1427c5SMateusz Guzik 
785*0a1427c5SMateusz Guzik 	if (error == 0)
786*0a1427c5SMateusz Guzik 		ktrstruct(name, data, datalen);
787*0a1427c5SMateusz Guzik }
788*0a1427c5SMateusz Guzik 
789*0a1427c5SMateusz Guzik void
790ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data,
791ffb66079SJohn Baldwin     int num_items, size_t struct_size)
792ffb66079SJohn Baldwin {
793ffb66079SJohn Baldwin 	struct ktr_request *req;
794ffb66079SJohn Baldwin 	struct ktr_struct_array *ksa;
795ffb66079SJohn Baldwin 	char *buf;
796ffb66079SJohn Baldwin 	size_t buflen, datalen, namelen;
797ffb66079SJohn Baldwin 	int max_items;
798ffb66079SJohn Baldwin 
799ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
800ad738f37SMatt Macy 		return;
801ad738f37SMatt Macy 
802ffb66079SJohn Baldwin 	/* Trim array length to genio size. */
803ffb66079SJohn Baldwin 	max_items = ktr_geniosize / struct_size;
804ffb66079SJohn Baldwin 	if (num_items > max_items) {
805ffb66079SJohn Baldwin 		if (max_items == 0)
806ffb66079SJohn Baldwin 			num_items = 1;
807ffb66079SJohn Baldwin 		else
808ffb66079SJohn Baldwin 			num_items = max_items;
809ffb66079SJohn Baldwin 	}
810ffb66079SJohn Baldwin 	datalen = num_items * struct_size;
811ffb66079SJohn Baldwin 
812ffb66079SJohn Baldwin 	if (data == NULL)
813ffb66079SJohn Baldwin 		datalen = 0;
814ffb66079SJohn Baldwin 
815ffb66079SJohn Baldwin 	namelen = strlen(name) + 1;
816ffb66079SJohn Baldwin 	buflen = namelen + datalen;
817ffb66079SJohn Baldwin 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
818ffb66079SJohn Baldwin 	strcpy(buf, name);
819ffb66079SJohn Baldwin 	if (seg == UIO_SYSSPACE)
820ffb66079SJohn Baldwin 		bcopy(data, buf + namelen, datalen);
821ffb66079SJohn Baldwin 	else {
822ffb66079SJohn Baldwin 		if (copyin(data, buf + namelen, datalen) != 0) {
823ffb66079SJohn Baldwin 			free(buf, M_KTRACE);
824ffb66079SJohn Baldwin 			return;
825ffb66079SJohn Baldwin 		}
826ffb66079SJohn Baldwin 	}
827ffb66079SJohn Baldwin 	if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
828ffb66079SJohn Baldwin 		free(buf, M_KTRACE);
829ffb66079SJohn Baldwin 		return;
830ffb66079SJohn Baldwin 	}
831ffb66079SJohn Baldwin 	ksa = &req->ktr_data.ktr_struct_array;
832ffb66079SJohn Baldwin 	ksa->struct_size = struct_size;
833ffb66079SJohn Baldwin 	req->ktr_buffer = buf;
834ffb66079SJohn Baldwin 	req->ktr_header.ktr_len = buflen;
835ffb66079SJohn Baldwin 	ktr_submitrequest(curthread, req);
836ffb66079SJohn Baldwin }
837ffb66079SJohn Baldwin 
838ffb66079SJohn Baldwin void
839039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
840039644ecSEd Maste     const cap_rights_t *held)
841c601ad8eSDag-Erling Smørgrav {
842c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
843c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
844c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
845c601ad8eSDag-Erling Smørgrav 
846ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
847ad738f37SMatt Macy 		return;
848ad738f37SMatt Macy 
849c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
850c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
851c601ad8eSDag-Erling Smørgrav 		return;
852c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
853e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
8543fded357SPawel Jakub Dawidek 	if (needed != NULL)
8557008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
8563fded357SPawel Jakub Dawidek 	else
8573fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
8583fded357SPawel Jakub Dawidek 	if (held != NULL)
8597008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
8603fded357SPawel Jakub Dawidek 	else
8613fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
862c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
863c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
864c601ad8eSDag-Erling Smørgrav }
86535818d2eSJohn Baldwin 
86635818d2eSJohn Baldwin void
867039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
86835818d2eSJohn Baldwin {
86935818d2eSJohn Baldwin 	struct thread *td = curthread;
87035818d2eSJohn Baldwin 	struct ktr_request *req;
87135818d2eSJohn Baldwin 	struct ktr_fault *kf;
87235818d2eSJohn Baldwin 
873ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
874ad738f37SMatt Macy 		return;
875ad738f37SMatt Macy 
87635818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
87735818d2eSJohn Baldwin 	if (req == NULL)
87835818d2eSJohn Baldwin 		return;
87935818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
88035818d2eSJohn Baldwin 	kf->vaddr = vaddr;
88135818d2eSJohn Baldwin 	kf->type = type;
88235818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
88335818d2eSJohn Baldwin 	ktrace_exit(td);
88435818d2eSJohn Baldwin }
88535818d2eSJohn Baldwin 
88635818d2eSJohn Baldwin void
887039644ecSEd Maste ktrfaultend(int result)
88835818d2eSJohn Baldwin {
88935818d2eSJohn Baldwin 	struct thread *td = curthread;
89035818d2eSJohn Baldwin 	struct ktr_request *req;
89135818d2eSJohn Baldwin 	struct ktr_faultend *kf;
89235818d2eSJohn Baldwin 
893ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
894ad738f37SMatt Macy 		return;
895ad738f37SMatt Macy 
89635818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
89735818d2eSJohn Baldwin 	if (req == NULL)
89835818d2eSJohn Baldwin 		return;
89935818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
90035818d2eSJohn Baldwin 	kf->result = result;
90135818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
90235818d2eSJohn Baldwin 	ktrace_exit(td);
90335818d2eSJohn Baldwin }
90464cc6a13SJohn Baldwin #endif /* KTRACE */
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes /* Interface and common routines */
907df8bae1dSRodney W. Grimes 
908d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
909df8bae1dSRodney W. Grimes struct ktrace_args {
910df8bae1dSRodney W. Grimes 	char	*fname;
911df8bae1dSRodney W. Grimes 	int	ops;
912df8bae1dSRodney W. Grimes 	int	facs;
913df8bae1dSRodney W. Grimes 	int	pid;
914df8bae1dSRodney W. Grimes };
915d2d3e875SBruce Evans #endif
916df8bae1dSRodney W. Grimes /* ARGSUSED */
91726f9a767SRodney W. Grimes int
918039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
919df8bae1dSRodney W. Grimes {
920db6a20e2SGarrett Wollman #ifdef KTRACE
921039644ecSEd Maste 	struct vnode *vp = NULL;
922039644ecSEd Maste 	struct proc *p;
923df8bae1dSRodney W. Grimes 	struct pgrp *pg;
924df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
925df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
926df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
927400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
9285050aa86SKonstantin Belousov 	int flags, error = 0;
929df8bae1dSRodney W. Grimes 	struct nameidata nd;
930a5881ea5SJohn Baldwin 	struct ucred *cred;
931df8bae1dSRodney W. Grimes 
93264cc6a13SJohn Baldwin 	/*
93364cc6a13SJohn Baldwin 	 * Need something to (un)trace.
93464cc6a13SJohn Baldwin 	 */
93564cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
93664cc6a13SJohn Baldwin 		return (EINVAL);
93764cc6a13SJohn Baldwin 
9382c255e9dSRobert Watson 	ktrace_enter(td);
939df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
940df8bae1dSRodney W. Grimes 		/*
941df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
942df8bae1dSRodney W. Grimes 		 */
9435050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
944e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
9459e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
946797f2d22SPoul-Henning Kamp 		if (error) {
9472c255e9dSRobert Watson 			ktrace_exit(td);
948df8bae1dSRodney W. Grimes 			return (error);
949df8bae1dSRodney W. Grimes 		}
950762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
951df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
952b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
953df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
954a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
9552c255e9dSRobert Watson 			ktrace_exit(td);
956df8bae1dSRodney W. Grimes 			return (EACCES);
957df8bae1dSRodney W. Grimes 		}
958df8bae1dSRodney W. Grimes 	}
959df8bae1dSRodney W. Grimes 	/*
96079deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
961df8bae1dSRodney W. Grimes 	 */
962df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
96351fd6380SMike Pritchard 		int vrele_count;
96451fd6380SMike Pritchard 
96551fd6380SMike Pritchard 		vrele_count = 0;
9661005a129SJohn Baldwin 		sx_slock(&allproc_lock);
9674f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
968a7ff7443SJohn Baldwin 			PROC_LOCK(p);
969a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
970ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
971ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
972d680caabSJohn Baldwin 					ktr_freeproc(p, &cred, NULL);
973ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
97451fd6380SMike Pritchard 					vrele_count++;
975a5881ea5SJohn Baldwin 					crfree(cred);
97651fd6380SMike Pritchard 				} else
977df8bae1dSRodney W. Grimes 					error = EPERM;
978df8bae1dSRodney W. Grimes 			}
979a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
98079deba82SMatthew Dillon 		}
9811005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
98251fd6380SMike Pritchard 		if (vrele_count > 0) {
98351fd6380SMike Pritchard 			while (vrele_count-- > 0)
98451fd6380SMike Pritchard 				vrele(vp);
98551fd6380SMike Pritchard 		}
986df8bae1dSRodney W. Grimes 		goto done;
987df8bae1dSRodney W. Grimes 	}
988df8bae1dSRodney W. Grimes 	/*
989df8bae1dSRodney W. Grimes 	 * do it
990df8bae1dSRodney W. Grimes 	 */
99164cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
992df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
993df8bae1dSRodney W. Grimes 		/*
994df8bae1dSRodney W. Grimes 		 * by process group
995df8bae1dSRodney W. Grimes 		 */
996df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
997df8bae1dSRodney W. Grimes 		if (pg == NULL) {
998ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
999df8bae1dSRodney W. Grimes 			error = ESRCH;
1000df8bae1dSRodney W. Grimes 			goto done;
1001df8bae1dSRodney W. Grimes 		}
1002f591779bSSeigo Tanimura 		/*
1003f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
1004ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
1005f591779bSSeigo Tanimura 		 */
1006f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
1007400a74bfSPawel Jakub Dawidek 		nfound = 0;
1008400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1009400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
1010e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW ||
1011e806d352SJohn Baldwin 			    p_cansee(td, p) != 0) {
1012400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
1013400a74bfSPawel Jakub Dawidek 				continue;
1014400a74bfSPawel Jakub Dawidek 			}
1015400a74bfSPawel Jakub Dawidek 			nfound++;
1016df8bae1dSRodney W. Grimes 			if (descend)
1017a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
1018df8bae1dSRodney W. Grimes 			else
1019a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
1020400a74bfSPawel Jakub Dawidek 		}
1021400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
1022400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
1023400a74bfSPawel Jakub Dawidek 			error = ESRCH;
1024400a74bfSPawel Jakub Dawidek 			goto done;
1025400a74bfSPawel Jakub Dawidek 		}
1026df8bae1dSRodney W. Grimes 	} else {
1027df8bae1dSRodney W. Grimes 		/*
1028df8bae1dSRodney W. Grimes 		 * by pid
1029df8bae1dSRodney W. Grimes 		 */
1030df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
1031fe41d17aSJohn Baldwin 		if (p == NULL)
1032df8bae1dSRodney W. Grimes 			error = ESRCH;
1033fe41d17aSJohn Baldwin 		else
10344eb7c9f6SPawel Jakub Dawidek 			error = p_cansee(td, p);
1035b0d9aeddSPawel Jakub Dawidek 		if (error) {
1036fe41d17aSJohn Baldwin 			if (p != NULL)
1037fe41d17aSJohn Baldwin 				PROC_UNLOCK(p);
1038b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
10394eb7c9f6SPawel Jakub Dawidek 			goto done;
1040b0d9aeddSPawel Jakub Dawidek 		}
1041df8bae1dSRodney W. Grimes 		if (descend)
1042a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
1043df8bae1dSRodney W. Grimes 		else
1044a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
1045df8bae1dSRodney W. Grimes 	}
104664cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
1047df8bae1dSRodney W. Grimes 	if (!ret)
1048df8bae1dSRodney W. Grimes 		error = EPERM;
1049df8bae1dSRodney W. Grimes done:
10505050aa86SKonstantin Belousov 	if (vp != NULL)
1051a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
10522c255e9dSRobert Watson 	ktrace_exit(td);
1053df8bae1dSRodney W. Grimes 	return (error);
105464cc6a13SJohn Baldwin #else /* !KTRACE */
105564cc6a13SJohn Baldwin 	return (ENOSYS);
105664cc6a13SJohn Baldwin #endif /* KTRACE */
1057df8bae1dSRodney W. Grimes }
1058df8bae1dSRodney W. Grimes 
1059e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1060e6c4b9baSPoul-Henning Kamp int
1061039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
1062e6c4b9baSPoul-Henning Kamp {
1063b40ce416SJulian Elischer 
1064e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1065ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
10667f05b035SAlfred Perlstein 	void *cp;
1067c9e7d28eSJohn Baldwin 	int error;
1068e6c4b9baSPoul-Henning Kamp 
1069c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1070c9e7d28eSJohn Baldwin 		return (0);
1071bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
10720bad156aSAlfred Perlstein 		return (EINVAL);
1073a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1074c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
107550c22331SPoul-Henning Kamp 	if (error) {
107650c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1077c9e7d28eSJohn Baldwin 		return (error);
107850c22331SPoul-Henning Kamp 	}
1079ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
108050c22331SPoul-Henning Kamp 	if (req == NULL) {
108150c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1082b10221ffSJoseph Koshy 		return (ENOMEM);
108350c22331SPoul-Henning Kamp 	}
1084d977a583SRobert Watson 	req->ktr_buffer = cp;
1085ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
10862c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1087e6c4b9baSPoul-Henning Kamp 	return (0);
108864cc6a13SJohn Baldwin #else /* !KTRACE */
1089e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
109064cc6a13SJohn Baldwin #endif /* KTRACE */
1091e6c4b9baSPoul-Henning Kamp }
1092e6c4b9baSPoul-Henning Kamp 
1093db6a20e2SGarrett Wollman #ifdef KTRACE
109487b6de2bSPoul-Henning Kamp static int
1095039644ecSEd Maste ktrops(struct thread *td, struct proc *p, int ops, int facs, struct vnode *vp)
1096df8bae1dSRodney W. Grimes {
1097ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
1098a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
1099df8bae1dSRodney W. Grimes 
1100fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1101a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1102a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1103df8bae1dSRodney W. Grimes 		return (0);
1104a7ff7443SJohn Baldwin 	}
1105fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1106fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1107fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1108fe41d17aSJohn Baldwin 		return (1);
1109fe41d17aSJohn Baldwin 	}
1110ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1111df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
1112a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
1113df8bae1dSRodney W. Grimes 			/*
1114a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
1115df8bae1dSRodney W. Grimes 			 */
1116a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
1117ea3fc8e4SJohn Baldwin 			VREF(vp);
1118a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
1119a5881ea5SJohn Baldwin 		}
1120a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
1121a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
1122a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
1123df8bae1dSRodney W. Grimes 		}
1124df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
112532f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1126df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1127df8bae1dSRodney W. Grimes 	} else {
1128df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1129d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1130df8bae1dSRodney W. Grimes 			/* no more tracing */
1131d680caabSJohn Baldwin 			ktr_freeproc(p, &tracecred, &tracevp);
1132a7ff7443SJohn Baldwin 	}
1133ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
113422ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
113522ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1136a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
11375050aa86SKonstantin Belousov 	if (tracevp != NULL)
1138ea3fc8e4SJohn Baldwin 		vrele(tracevp);
1139a5881ea5SJohn Baldwin 	if (tracecred != NULL)
1140a5881ea5SJohn Baldwin 		crfree(tracecred);
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes 	return (1);
1143df8bae1dSRodney W. Grimes }
1144df8bae1dSRodney W. Grimes 
114587b6de2bSPoul-Henning Kamp static int
1146039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1147039644ecSEd Maste     struct vnode *vp)
1148df8bae1dSRodney W. Grimes {
1149039644ecSEd Maste 	struct proc *p;
1150039644ecSEd Maste 	int ret = 0;
1151df8bae1dSRodney W. Grimes 
1152df8bae1dSRodney W. Grimes 	p = top;
1153fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
115464cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1155df8bae1dSRodney W. Grimes 	for (;;) {
1156a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
1157df8bae1dSRodney W. Grimes 		/*
1158df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1159df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1160df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1161df8bae1dSRodney W. Grimes 		 */
11622e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
11632e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1164df8bae1dSRodney W. Grimes 		else for (;;) {
116564cc6a13SJohn Baldwin 			if (p == top)
1166df8bae1dSRodney W. Grimes 				return (ret);
11672e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
11682e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1169df8bae1dSRodney W. Grimes 				break;
1170df8bae1dSRodney W. Grimes 			}
1171b75356e1SJeffrey Hsu 			p = p->p_pptr;
1172df8bae1dSRodney W. Grimes 		}
1173fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1174df8bae1dSRodney W. Grimes 	}
1175df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1176df8bae1dSRodney W. Grimes }
1177df8bae1dSRodney W. Grimes 
117887b6de2bSPoul-Henning Kamp static void
11792c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1180df8bae1dSRodney W. Grimes {
1181ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1182ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1183ea3fc8e4SJohn Baldwin 	struct proc *p;
1184ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1185df8bae1dSRodney W. Grimes 	struct uio auio;
1186ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1187f2a2857bSKirk McKusick 	struct mount *mp;
1188ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
11895050aa86SKonstantin Belousov 	int error;
1190df8bae1dSRodney W. Grimes 
11912c255e9dSRobert Watson 	/*
11922c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
11932c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
11942c255e9dSRobert Watson 	 *
11952c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
11962c255e9dSRobert Watson 	 * the vnode has been closed.
11972c255e9dSRobert Watson 	 */
11982c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
11992c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
12002c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
12012c255e9dSRobert Watson 
1202ea3fc8e4SJohn Baldwin 	/*
1203ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
12042c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
12052c255e9dSRobert Watson 	 * in sync: we should have both or neither.
1206ea3fc8e4SJohn Baldwin 	 */
12072c255e9dSRobert Watson 	if (vp == NULL) {
12082c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1209118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1210df8bae1dSRodney W. Grimes 		return;
12112c255e9dSRobert Watson 	}
1212118258f5SBjoern A. Zeeb 	VREF(vp);
12132c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1214118258f5SBjoern A. Zeeb 	crhold(cred);
1215118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
12162c255e9dSRobert Watson 
1217ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
121802abd400SPedro F. Giffuni 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1219a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
12208b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1221ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1222df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1223df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1224df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1225df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1226df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1227df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1228df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1229df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1230ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1231ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1232ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1233ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1234ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1235df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1236ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1237ea3fc8e4SJohn Baldwin 	}
1238ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1239d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1240d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1241ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1242ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1243ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1244b92584a6SJohn Baldwin 	}
12452c255e9dSRobert Watson 
1246f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1247cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1248467a273cSRobert Watson #ifdef MAC
124930d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1250467a273cSRobert Watson 	if (error == 0)
1251467a273cSRobert Watson #endif
1252ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1253b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
1254f2a2857bSKirk McKusick 	vn_finished_write(mp);
1255118258f5SBjoern A. Zeeb 	crfree(cred);
1256118258f5SBjoern A. Zeeb 	if (!error) {
1257704c9f00SJohn Baldwin 		vrele(vp);
1258df8bae1dSRodney W. Grimes 		return;
1259118258f5SBjoern A. Zeeb 	}
1260118258f5SBjoern A. Zeeb 
1261df8bae1dSRodney W. Grimes 	/*
1262ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1263ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1264ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1265118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1266118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1267118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1268118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1269df8bae1dSRodney W. Grimes 	 */
1270df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1271df8bae1dSRodney W. Grimes 	    error);
1272118258f5SBjoern A. Zeeb 	vrele_count = 1;
1273ea3fc8e4SJohn Baldwin 	/*
1274ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1275ea3fc8e4SJohn Baldwin 	 * system.
1276ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1277ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1278ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1279ea3fc8e4SJohn Baldwin 	 */
1280a5881ea5SJohn Baldwin 	cred = NULL;
12811005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12824f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1283ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1284a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1285ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1286d680caabSJohn Baldwin 			ktr_freeproc(p, &cred, NULL);
1287ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1288ea3fc8e4SJohn Baldwin 			vrele_count++;
1289df8bae1dSRodney W. Grimes 		}
1290ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1291a5881ea5SJohn Baldwin 		if (cred != NULL) {
1292a5881ea5SJohn Baldwin 			crfree(cred);
1293a5881ea5SJohn Baldwin 			cred = NULL;
1294a5881ea5SJohn Baldwin 		}
1295df8bae1dSRodney W. Grimes 	}
12961005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12972c255e9dSRobert Watson 
1298ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1299ea3fc8e4SJohn Baldwin 		vrele(vp);
1300df8bae1dSRodney W. Grimes }
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes /*
1303df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1304df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1305df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1306df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1307df8bae1dSRodney W. Grimes  * so, only root may further change it.
1308df8bae1dSRodney W. Grimes  */
130987b6de2bSPoul-Henning Kamp static int
1310039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1311df8bae1dSRodney W. Grimes {
1312df8bae1dSRodney W. Grimes 
1313a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1314a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
131532f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
131675c13541SPoul-Henning Kamp 		return (0);
1317a0f75161SRobert Watson 
1318f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1319a0f75161SRobert Watson 		return (0);
1320a0f75161SRobert Watson 
1321df8bae1dSRodney W. Grimes 	return (1);
1322df8bae1dSRodney W. Grimes }
1323df8bae1dSRodney W. Grimes 
1324db6a20e2SGarrett Wollman #endif /* KTRACE */
1325