xref: /freebsd/sys/kern/kern_ktrace.c (revision 7e1d3eefd410ca0fbae5a217422821244c3eeee4)
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>
5402645b88SKonstantin Belousov #include <sys/resourcevar.h>
55ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
56df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5760e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5860e15db9SDag-Erling Smørgrav #include <sys/stat.h>
59df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
601005a129SJohn Baldwin #include <sys/sx.h>
61ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
627705d4b2SDmitry Chagin #include <sys/sysent.h>
63df8bae1dSRodney W. Grimes #include <sys/syslog.h>
64ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
65df8bae1dSRodney W. Grimes 
66aed55708SRobert Watson #include <security/mac/mac_framework.h>
67aed55708SRobert Watson 
682c255e9dSRobert Watson /*
692c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
702c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
712c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
722c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
732c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
742c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
752c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
762c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
772c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
782c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
792c255e9dSRobert Watson  *
802c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
812c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
822c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
832c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
842c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
852c255e9dSRobert Watson  */
862c255e9dSRobert Watson 
87a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8855166637SPoul-Henning Kamp 
89db6a20e2SGarrett Wollman #ifdef KTRACE
90ea3fc8e4SJohn Baldwin 
91de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
92de5b1952SAlexander Leidinger 
93ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
94ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
95ea3fc8e4SJohn Baldwin #endif
96ea3fc8e4SJohn Baldwin 
97ea3fc8e4SJohn Baldwin struct ktr_request {
98ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
99d977a583SRobert Watson 	void	*ktr_buffer;
100ea3fc8e4SJohn Baldwin 	union {
1017705d4b2SDmitry Chagin 		struct	ktr_proc_ctor ktr_proc_ctor;
102c601ad8eSDag-Erling Smørgrav 		struct	ktr_cap_fail ktr_cap_fail;
103ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
104ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
105ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
106ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
107ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
10835818d2eSJohn Baldwin 		struct	ktr_fault ktr_fault;
10935818d2eSJohn Baldwin 		struct	ktr_faultend ktr_faultend;
110ffb66079SJohn Baldwin 		struct  ktr_struct_array ktr_struct_array;
111ea3fc8e4SJohn Baldwin 	} ktr_data;
112ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
113ea3fc8e4SJohn Baldwin };
114ea3fc8e4SJohn Baldwin 
115ea3fc8e4SJohn Baldwin static int data_lengths[] = {
116093e059cSJilles Tjoelker 	[KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
117093e059cSJilles Tjoelker 	[KTR_SYSRET] = sizeof(struct ktr_sysret),
118093e059cSJilles Tjoelker 	[KTR_NAMEI] = 0,
119093e059cSJilles Tjoelker 	[KTR_GENIO] = sizeof(struct ktr_genio),
120093e059cSJilles Tjoelker 	[KTR_PSIG] = sizeof(struct ktr_psig),
121093e059cSJilles Tjoelker 	[KTR_CSW] = sizeof(struct ktr_csw),
122093e059cSJilles Tjoelker 	[KTR_USER] = 0,
123093e059cSJilles Tjoelker 	[KTR_STRUCT] = 0,
124093e059cSJilles Tjoelker 	[KTR_SYSCTL] = 0,
125093e059cSJilles Tjoelker 	[KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
126093e059cSJilles Tjoelker 	[KTR_PROCDTOR] = 0,
127093e059cSJilles Tjoelker 	[KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
128093e059cSJilles Tjoelker 	[KTR_FAULT] = sizeof(struct ktr_fault),
129093e059cSJilles Tjoelker 	[KTR_FAULTEND] = sizeof(struct ktr_faultend),
130ffb66079SJohn Baldwin 	[KTR_STRUCT_ARRAY] = sizeof(struct ktr_struct_array),
131ea3fc8e4SJohn Baldwin };
132ea3fc8e4SJohn Baldwin 
133ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
134ea3fc8e4SJohn Baldwin 
1357029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1367029da5cSPawel Biernacki     "KTRACE options");
13712301fc3SJohn Baldwin 
1388b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
13912301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
14012301fc3SJohn Baldwin 
1411e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE;
142af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
14312301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
144ea3fc8e4SJohn Baldwin 
145ea2b64c2SKonstantin Belousov /*
146ea2b64c2SKonstantin Belousov  * Allow to not to send signal to traced process, in which context the
147ea2b64c2SKonstantin Belousov  * ktr record is written.  The limit is applied from the process that
148ea2b64c2SKonstantin Belousov  * set up ktrace, so killing the traced process is not completely fair.
149ea2b64c2SKonstantin Belousov  */
150ea2b64c2SKonstantin Belousov int ktr_filesize_limit_signal = 0;
151ea2b64c2SKonstantin Belousov SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
152ea2b64c2SKonstantin Belousov     &ktr_filesize_limit_signal, 0,
153ea2b64c2SKonstantin Belousov     "Send SIGXFSZ to the traced process when the log size limit is exceeded");
154ea2b64c2SKonstantin Belousov 
155ea3fc8e4SJohn Baldwin static int print_message = 1;
156d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1572c255e9dSRobert Watson static struct sx ktrace_sx;
158ea3fc8e4SJohn Baldwin 
1591762f674SKonstantin Belousov struct ktr_io_params {
1601762f674SKonstantin Belousov 	struct vnode	*vp;
1611762f674SKonstantin Belousov 	struct ucred	*cr;
16202645b88SKonstantin Belousov 	off_t		lim;
1631762f674SKonstantin Belousov 	u_int		refs;
1641762f674SKonstantin Belousov };
1651762f674SKonstantin Belousov 
166ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
167ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
168b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
16922ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
170ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1712c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
1721762f674SKonstantin Belousov static struct ktr_io_params *ktr_freeproc(struct proc *p);
173ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
174d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1752c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
176a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
1771762f674SKonstantin Belousov static int ktrsetchildren(struct thread *, struct proc *, int, int,
1781762f674SKonstantin Belousov     struct ktr_io_params *);
1791762f674SKonstantin Belousov static int ktrops(struct thread *, struct proc *, int, int,
1801762f674SKonstantin Belousov     struct ktr_io_params *);
18122ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
18298d93822SBruce Evans 
1832c255e9dSRobert Watson /*
1842c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1852c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1862c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1872c255e9dSRobert Watson  * suppressed.
1882c255e9dSRobert Watson  */
1892c255e9dSRobert Watson static void
1902c255e9dSRobert Watson ktrace_enter(struct thread *td)
1912c255e9dSRobert Watson {
1922c255e9dSRobert Watson 
1932c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1942c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1952c255e9dSRobert Watson }
1962c255e9dSRobert Watson 
1972c255e9dSRobert Watson static void
1982c255e9dSRobert Watson ktrace_exit(struct thread *td)
1992c255e9dSRobert Watson {
2002c255e9dSRobert Watson 
2012c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
2022c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
2032c255e9dSRobert Watson }
2042c255e9dSRobert Watson 
2052c255e9dSRobert Watson static void
2062c255e9dSRobert Watson ktrace_assert(struct thread *td)
2072c255e9dSRobert Watson {
2082c255e9dSRobert Watson 
2092c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
2102c255e9dSRobert Watson }
2112c255e9dSRobert Watson 
212ea3fc8e4SJohn Baldwin static void
213ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
214df8bae1dSRodney W. Grimes {
215ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
216ea3fc8e4SJohn Baldwin 	int i;
217df8bae1dSRodney W. Grimes 
218ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
2192c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
220ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
221ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
2225c18bf9dSMark Johnston 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
2235c18bf9dSMark Johnston 		    M_ZERO);
224ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
225ea3fc8e4SJohn Baldwin 	}
226ea3fc8e4SJohn Baldwin }
227ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
228ea3fc8e4SJohn Baldwin 
229ea3fc8e4SJohn Baldwin static int
230ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
231ea3fc8e4SJohn Baldwin {
232ea3fc8e4SJohn Baldwin 	struct thread *td;
2338b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
234ea3fc8e4SJohn Baldwin 	int error;
235ea3fc8e4SJohn Baldwin 
236ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
237ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
238ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2398b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
240ea3fc8e4SJohn Baldwin 	}
241ea3fc8e4SJohn Baldwin 
2428b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
243ea3fc8e4SJohn Baldwin 	if (error)
244ea3fc8e4SJohn Baldwin 		return (error);
245ea3fc8e4SJohn Baldwin 	td = curthread;
2462c255e9dSRobert Watson 	ktrace_enter(td);
247ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
248b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2492c255e9dSRobert Watson 	ktrace_exit(td);
2508b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
251ea3fc8e4SJohn Baldwin 	if (error)
252ea3fc8e4SJohn Baldwin 		return (error);
253a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
254ea3fc8e4SJohn Baldwin 		return (ENOSPC);
255ea3fc8e4SJohn Baldwin 	return (0);
256ea3fc8e4SJohn Baldwin }
2577029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
2587029da5cSPawel Biernacki     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
2597029da5cSPawel Biernacki     sysctl_kern_ktrace_request_pool, "IU",
260a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
261ea3fc8e4SJohn Baldwin 
2628b149b51SJohn Baldwin static u_int
263b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
264ea3fc8e4SJohn Baldwin {
265b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
266ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
267a5896914SJoseph Koshy 	int bound;
268ea3fc8e4SJohn Baldwin 
269ea3fc8e4SJohn Baldwin 	print_message = 1;
270b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
271a5896914SJoseph Koshy 	if (bound == 0)
272a5896914SJoseph Koshy 		return (ktr_requestpool);
273b4c20e5eSDmitry Chagin 	if (bound < 0) {
274b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
275ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
276a5896914SJoseph Koshy 		while (bound++ < 0) {
277ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
278ea3fc8e4SJohn Baldwin 			if (req == NULL)
279b4c20e5eSDmitry Chagin 				break;
280ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
281ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
282ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
283ea3fc8e4SJohn Baldwin 		}
284b4c20e5eSDmitry Chagin 	} else {
285ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
286b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
287a5896914SJoseph Koshy 		while (bound-- > 0) {
288ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
2895c18bf9dSMark Johnston 			    M_WAITOK | M_ZERO);
290b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
291ea3fc8e4SJohn Baldwin 		}
292b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
293b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
294b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
295b4c20e5eSDmitry Chagin 	}
296b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
297ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
298ea3fc8e4SJohn Baldwin }
299ea3fc8e4SJohn Baldwin 
3005ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
3015ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
3025ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
3035ca4819dSJohn Baldwin 
304ea3fc8e4SJohn Baldwin static struct ktr_request *
30522ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
306ea3fc8e4SJohn Baldwin {
307ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
308ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
309ea3fc8e4SJohn Baldwin 	int pm;
310ea3fc8e4SJohn Baldwin 
311c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
312ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
313c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
314ea3fc8e4SJohn Baldwin 		return (NULL);
315ea3fc8e4SJohn Baldwin 	}
316ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
317ea3fc8e4SJohn Baldwin 	if (req != NULL) {
318ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
319ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
32075768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
32175768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
32275768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
32375768576SJohn Baldwin 		}
324c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
325ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
326ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3272bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3285ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3295ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
330d977a583SRobert Watson 		req->ktr_buffer = NULL;
331ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
332ea3fc8e4SJohn Baldwin 	} else {
33375768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
334ea3fc8e4SJohn Baldwin 		pm = print_message;
335ea3fc8e4SJohn Baldwin 		print_message = 0;
336ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
337ea3fc8e4SJohn Baldwin 		if (pm)
338ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
339ea3fc8e4SJohn Baldwin 	}
340ea3fc8e4SJohn Baldwin 	return (req);
341ea3fc8e4SJohn Baldwin }
342ea3fc8e4SJohn Baldwin 
3437705d4b2SDmitry Chagin static struct ktr_request *
3447705d4b2SDmitry Chagin ktr_getrequest(int type)
3457705d4b2SDmitry Chagin {
3467705d4b2SDmitry Chagin 	struct thread *td = curthread;
3477705d4b2SDmitry Chagin 	struct ktr_request *req;
3487705d4b2SDmitry Chagin 
3497705d4b2SDmitry Chagin 	ktrace_enter(td);
35022ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3517705d4b2SDmitry Chagin 	if (req == NULL)
3527705d4b2SDmitry Chagin 		ktrace_exit(td);
3537705d4b2SDmitry Chagin 
3547705d4b2SDmitry Chagin 	return (req);
3557705d4b2SDmitry Chagin }
3567705d4b2SDmitry Chagin 
3572c255e9dSRobert Watson /*
3582c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3592c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3602c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3612c255e9dSRobert Watson  * later.
3622c255e9dSRobert Watson  */
363ea3fc8e4SJohn Baldwin static void
3642c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
365ea3fc8e4SJohn Baldwin {
366ea3fc8e4SJohn Baldwin 
367ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3682c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
369ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
37046588778SEdward Tomasz Napierala 	thread_lock(td);
37146588778SEdward Tomasz Napierala 	td->td_flags |= TDF_ASTPENDING;
37246588778SEdward Tomasz Napierala 	thread_unlock(td);
3732c255e9dSRobert Watson }
3742c255e9dSRobert Watson 
3752c255e9dSRobert Watson /*
3762c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3772c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3782c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3792c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
380a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3812c255e9dSRobert Watson  */
3822c255e9dSRobert Watson static void
3832c255e9dSRobert Watson ktr_drain(struct thread *td)
3842c255e9dSRobert Watson {
3852c255e9dSRobert Watson 	struct ktr_request *queued_req;
3862c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3872c255e9dSRobert Watson 
3882c255e9dSRobert Watson 	ktrace_assert(td);
3892c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3902c255e9dSRobert Watson 
3912b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3922c255e9dSRobert Watson 
3932c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3942c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3952c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3962c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3972c255e9dSRobert Watson 
3982c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3992c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
4002c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
4012c255e9dSRobert Watson 			ktr_freerequest(queued_req);
4022c255e9dSRobert Watson 		}
4032c255e9dSRobert Watson 	}
4042c255e9dSRobert Watson }
4052c255e9dSRobert Watson 
4062c255e9dSRobert Watson /*
4072c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
4082c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
4092c255e9dSRobert Watson  * been cached in the thread.
4102c255e9dSRobert Watson  */
4112c255e9dSRobert Watson static void
41222ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
4132c255e9dSRobert Watson {
4142c255e9dSRobert Watson 
4152c255e9dSRobert Watson 	ktrace_assert(td);
4162c255e9dSRobert Watson 
4172c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4182c255e9dSRobert Watson 	ktr_drain(td);
4192c255e9dSRobert Watson 	ktr_writerequest(td, req);
4202c255e9dSRobert Watson 	ktr_freerequest(req);
4212c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4222c255e9dSRobert Watson 	ktrace_exit(td);
423ea3fc8e4SJohn Baldwin }
424ea3fc8e4SJohn Baldwin 
425ea3fc8e4SJohn Baldwin static void
426ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
427ea3fc8e4SJohn Baldwin {
428ea3fc8e4SJohn Baldwin 
429d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
430d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
431d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
432d680caabSJohn Baldwin }
433d680caabSJohn Baldwin 
434d680caabSJohn Baldwin static void
435d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
436d680caabSJohn Baldwin {
437d680caabSJohn Baldwin 
438d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
439d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
440d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
441ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
442d680caabSJohn Baldwin }
443d680caabSJohn Baldwin 
4441762f674SKonstantin Belousov static void
4451762f674SKonstantin Belousov ktr_io_params_ref(struct ktr_io_params *kiop)
4461762f674SKonstantin Belousov {
4471762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4481762f674SKonstantin Belousov 	kiop->refs++;
4491762f674SKonstantin Belousov }
4501762f674SKonstantin Belousov 
4511762f674SKonstantin Belousov static struct ktr_io_params *
4521762f674SKonstantin Belousov ktr_io_params_rele(struct ktr_io_params *kiop)
4531762f674SKonstantin Belousov {
4541762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4551762f674SKonstantin Belousov 	if (kiop == NULL)
4561762f674SKonstantin Belousov 		return (NULL);
4571762f674SKonstantin Belousov 	KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
4581762f674SKonstantin Belousov 	return (--(kiop->refs) == 0 ? kiop : NULL);
4591762f674SKonstantin Belousov }
4601762f674SKonstantin Belousov 
4611762f674SKonstantin Belousov void
4621762f674SKonstantin Belousov ktr_io_params_free(struct ktr_io_params *kiop)
4631762f674SKonstantin Belousov {
4641762f674SKonstantin Belousov 	if (kiop == NULL)
4651762f674SKonstantin Belousov 		return;
4661762f674SKonstantin Belousov 
4671762f674SKonstantin Belousov 	MPASS(kiop->refs == 0);
4681762f674SKonstantin Belousov 	vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
4691762f674SKonstantin Belousov 	crfree(kiop->cr);
4701762f674SKonstantin Belousov 	free(kiop, M_KTRACE);
4711762f674SKonstantin Belousov }
4721762f674SKonstantin Belousov 
4731762f674SKonstantin Belousov static struct ktr_io_params *
4741762f674SKonstantin Belousov ktr_io_params_alloc(struct thread *td, struct vnode *vp)
4751762f674SKonstantin Belousov {
4761762f674SKonstantin Belousov 	struct ktr_io_params *res;
4771762f674SKonstantin Belousov 
4781762f674SKonstantin Belousov 	res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
4791762f674SKonstantin Belousov 	res->vp = vp;
4801762f674SKonstantin Belousov 	res->cr = crhold(td->td_ucred);
48102645b88SKonstantin Belousov 	res->lim = lim_cur(td, RLIMIT_FSIZE);
4821762f674SKonstantin Belousov 	res->refs = 1;
4831762f674SKonstantin Belousov 	return (res);
4841762f674SKonstantin Belousov }
4851762f674SKonstantin Belousov 
486d680caabSJohn Baldwin /*
487d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
488d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
489d680caabSJohn Baldwin  * vnode and credentials.
490d680caabSJohn Baldwin  */
4911762f674SKonstantin Belousov static struct ktr_io_params *
4921762f674SKonstantin Belousov ktr_freeproc(struct proc *p)
493d680caabSJohn Baldwin {
4941762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
495d680caabSJohn Baldwin 	struct ktr_request *req;
496d680caabSJohn Baldwin 
497d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
498d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
4991762f674SKonstantin Belousov 	kiop = ktr_io_params_rele(p->p_ktrioparms);
5001762f674SKonstantin Belousov 	p->p_ktrioparms = NULL;
501d680caabSJohn Baldwin 	p->p_traceflag = 0;
502d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
503d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
504d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
505d680caabSJohn Baldwin 	}
5061762f674SKonstantin Belousov 	return (kiop);
5071762f674SKonstantin Belousov }
5081762f674SKonstantin Belousov 
5091762f674SKonstantin Belousov struct vnode *
5101762f674SKonstantin Belousov ktr_get_tracevp(struct proc *p, bool ref)
5111762f674SKonstantin Belousov {
5121762f674SKonstantin Belousov 	struct vnode *vp;
5131762f674SKonstantin Belousov 
5141762f674SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
5151762f674SKonstantin Belousov 
5161762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL) {
5171762f674SKonstantin Belousov 		vp = p->p_ktrioparms->vp;
5181762f674SKonstantin Belousov 		if (ref)
5191762f674SKonstantin Belousov 			vrefact(vp);
5201762f674SKonstantin Belousov 	} else {
5211762f674SKonstantin Belousov 		vp = NULL;
5221762f674SKonstantin Belousov 	}
5231762f674SKonstantin Belousov 	return (vp);
524ea3fc8e4SJohn Baldwin }
525ea3fc8e4SJohn Baldwin 
52626f9a767SRodney W. Grimes void
527039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[])
528df8bae1dSRodney W. Grimes {
529ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
530df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
531ea3fc8e4SJohn Baldwin 	size_t buflen;
5324b3aac3dSJohn Baldwin 	char *buf = NULL;
533df8bae1dSRodney W. Grimes 
534ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
535ad738f37SMatt Macy 		return;
536ad738f37SMatt Macy 
5374b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
5384b3aac3dSJohn Baldwin 	if (buflen > 0) {
539a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
5404b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
5414b3aac3dSJohn Baldwin 	}
542ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
54350c22331SPoul-Henning Kamp 	if (req == NULL) {
54450c22331SPoul-Henning Kamp 		if (buf != NULL)
54550c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
546ea3fc8e4SJohn Baldwin 		return;
54750c22331SPoul-Henning Kamp 	}
548ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
549df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
550df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
551ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
552ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
553d977a583SRobert Watson 		req->ktr_buffer = buf;
554ea3fc8e4SJohn Baldwin 	}
5552c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
556df8bae1dSRodney W. Grimes }
557df8bae1dSRodney W. Grimes 
55826f9a767SRodney W. Grimes void
559039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
560df8bae1dSRodney W. Grimes {
561ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
562ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
563df8bae1dSRodney W. Grimes 
564ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
565ad738f37SMatt Macy 		return;
566ad738f37SMatt Macy 
567ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
568ea3fc8e4SJohn Baldwin 	if (req == NULL)
569ea3fc8e4SJohn Baldwin 		return;
570ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
571ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
572ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
5735a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
5742c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
5752c255e9dSRobert Watson }
5762c255e9dSRobert Watson 
5772c255e9dSRobert Watson /*
578d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
579d680caabSJohn Baldwin  *
580d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
581d680caabSJohn Baldwin  */
5821762f674SKonstantin Belousov struct ktr_io_params *
5831762f674SKonstantin Belousov ktrprocexec(struct proc *p)
584d680caabSJohn Baldwin {
5851762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
586d680caabSJohn Baldwin 
587d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
5881762f674SKonstantin Belousov 
5891762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
5901762f674SKonstantin Belousov 	if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
5911762f674SKonstantin Belousov 		return (NULL);
5921762f674SKonstantin Belousov 
593d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
5941762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
595d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5961762f674SKonstantin Belousov 	return (kiop);
597d680caabSJohn Baldwin }
598d680caabSJohn Baldwin 
599d680caabSJohn Baldwin /*
600d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
601d680caabSJohn Baldwin  * and disable tracing.
6022c255e9dSRobert Watson  */
6032c255e9dSRobert Watson void
6042c255e9dSRobert Watson ktrprocexit(struct thread *td)
6052c255e9dSRobert Watson {
6067705d4b2SDmitry Chagin 	struct ktr_request *req;
607d680caabSJohn Baldwin 	struct proc *p;
6081762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
609d680caabSJohn Baldwin 
610d680caabSJohn Baldwin 	p = td->td_proc;
611d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
612d680caabSJohn Baldwin 		return;
6132c255e9dSRobert Watson 
6142c255e9dSRobert Watson 	ktrace_enter(td);
61522ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
61622ec0406SDmitry Chagin 	if (req != NULL)
61722ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
6182c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6192c255e9dSRobert Watson 	ktr_drain(td);
6202c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
621d680caabSJohn Baldwin 	PROC_LOCK(p);
622d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
6231762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
624d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
625d680caabSJohn Baldwin 	PROC_UNLOCK(p);
6261762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
6272c255e9dSRobert Watson 	ktrace_exit(td);
6282c255e9dSRobert Watson }
6292c255e9dSRobert Watson 
6307705d4b2SDmitry Chagin static void
63122ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
6327705d4b2SDmitry Chagin {
6337705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
6347705d4b2SDmitry Chagin 	struct ktr_request *req;
635de60a5f3SDmitry Chagin 	struct thread *td2;
6367705d4b2SDmitry Chagin 
6377705d4b2SDmitry Chagin 	ktrace_assert(td);
6387705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
63922ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
6407705d4b2SDmitry Chagin 	if (req == NULL)
6417705d4b2SDmitry Chagin 		return;
6427705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
6437705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
64422ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
6457705d4b2SDmitry Chagin }
6467705d4b2SDmitry Chagin 
6477705d4b2SDmitry Chagin void
6487705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
6497705d4b2SDmitry Chagin {
6507705d4b2SDmitry Chagin 	struct thread *td = curthread;
6517705d4b2SDmitry Chagin 
6527705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
6537705d4b2SDmitry Chagin 		return;
6547705d4b2SDmitry Chagin 
6557705d4b2SDmitry Chagin 	ktrace_enter(td);
65622ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
6577705d4b2SDmitry Chagin 	ktrace_exit(td);
6587705d4b2SDmitry Chagin }
6597705d4b2SDmitry Chagin 
6602c255e9dSRobert Watson /*
661d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
662d680caabSJohn Baldwin  */
663d680caabSJohn Baldwin void
664d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
665d680caabSJohn Baldwin {
666d680caabSJohn Baldwin 
6671762f674SKonstantin Belousov 	MPASS(p2->p_ktrioparms == NULL);
6687c34b35bSMateusz Guzik 	MPASS(p2->p_traceflag == 0);
6697c34b35bSMateusz Guzik 
6707c34b35bSMateusz Guzik 	if (p1->p_traceflag == 0)
6717c34b35bSMateusz Guzik 		return;
6727c34b35bSMateusz Guzik 
6737705d4b2SDmitry Chagin 	PROC_LOCK(p1);
674d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
675d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
676d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
6771762f674SKonstantin Belousov 		if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
6781762f674SKonstantin Belousov 			p1->p_ktrioparms->refs++;
679d680caabSJohn Baldwin 	}
680d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6817705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
6827705d4b2SDmitry Chagin 
6837705d4b2SDmitry Chagin 	ktrprocctor(p2);
684d680caabSJohn Baldwin }
685d680caabSJohn Baldwin 
686d680caabSJohn Baldwin /*
6872c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
6882c255e9dSRobert Watson  * system call.
6892c255e9dSRobert Watson  */
6902c255e9dSRobert Watson void
6912c255e9dSRobert Watson ktruserret(struct thread *td)
6922c255e9dSRobert Watson {
6932c255e9dSRobert Watson 
6942c255e9dSRobert Watson 	ktrace_enter(td);
6952c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6962c255e9dSRobert Watson 	ktr_drain(td);
6972c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6982c255e9dSRobert Watson 	ktrace_exit(td);
699df8bae1dSRodney W. Grimes }
700df8bae1dSRodney W. Grimes 
70126f9a767SRodney W. Grimes void
702e4b16f2fSMark Johnston ktrnamei(const char *path)
703df8bae1dSRodney W. Grimes {
704ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
705ea3fc8e4SJohn Baldwin 	int namelen;
7064b3aac3dSJohn Baldwin 	char *buf = NULL;
707df8bae1dSRodney W. Grimes 
7084b3aac3dSJohn Baldwin 	namelen = strlen(path);
7094b3aac3dSJohn Baldwin 	if (namelen > 0) {
710a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
7114b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
7124b3aac3dSJohn Baldwin 	}
713ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
71450c22331SPoul-Henning Kamp 	if (req == NULL) {
71550c22331SPoul-Henning Kamp 		if (buf != NULL)
71650c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
717ea3fc8e4SJohn Baldwin 		return;
71850c22331SPoul-Henning Kamp 	}
719ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
720ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
721d977a583SRobert Watson 		req->ktr_buffer = buf;
722ea3fc8e4SJohn Baldwin 	}
7232c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
724df8bae1dSRodney W. Grimes }
725df8bae1dSRodney W. Grimes 
72626f9a767SRodney W. Grimes void
727039644ecSEd Maste ktrsysctl(int *name, u_int namelen)
728a56be37eSJohn Baldwin {
729a56be37eSJohn Baldwin 	struct ktr_request *req;
730a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
731a56be37eSJohn Baldwin 	char *mibname;
732a56be37eSJohn Baldwin 	size_t mibnamelen;
733a56be37eSJohn Baldwin 	int error;
734a56be37eSJohn Baldwin 
735a56be37eSJohn Baldwin 	/* Lookup name of mib. */
736a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
737a56be37eSJohn Baldwin 	mib[0] = 0;
738a56be37eSJohn Baldwin 	mib[1] = 1;
739a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
740a56be37eSJohn Baldwin 	mibnamelen = 128;
741a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
742a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
743a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
744a56be37eSJohn Baldwin 	if (error) {
745a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
746a56be37eSJohn Baldwin 		return;
747a56be37eSJohn Baldwin 	}
748a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
749a56be37eSJohn Baldwin 	if (req == NULL) {
750a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
751a56be37eSJohn Baldwin 		return;
752a56be37eSJohn Baldwin 	}
753a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
754a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
755a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
756a56be37eSJohn Baldwin }
757a56be37eSJohn Baldwin 
758a56be37eSJohn Baldwin void
759039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
760df8bae1dSRodney W. Grimes {
761ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
762ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
763b92584a6SJohn Baldwin 	int datalen;
764b92584a6SJohn Baldwin 	char *buf;
765df8bae1dSRodney W. Grimes 
766552afd9cSPoul-Henning Kamp 	if (error) {
767552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
768df8bae1dSRodney W. Grimes 		return;
769552afd9cSPoul-Henning Kamp 	}
770b92584a6SJohn Baldwin 	uio->uio_offset = 0;
771b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
772526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
773a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
774552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
775552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
776552afd9cSPoul-Henning Kamp 	if (error) {
777b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
778ea3fc8e4SJohn Baldwin 		return;
779b92584a6SJohn Baldwin 	}
780b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
781b92584a6SJohn Baldwin 	if (req == NULL) {
782b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
783b92584a6SJohn Baldwin 		return;
784b92584a6SJohn Baldwin 	}
785ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
786ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
787ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
788b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
789d977a583SRobert Watson 	req->ktr_buffer = buf;
7902c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
791df8bae1dSRodney W. Grimes }
792df8bae1dSRodney W. Grimes 
79326f9a767SRodney W. Grimes void
794039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
795df8bae1dSRodney W. Grimes {
79622ec0406SDmitry Chagin 	struct thread *td = curthread;
797ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
798ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
799df8bae1dSRodney W. Grimes 
800ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
801ea3fc8e4SJohn Baldwin 	if (req == NULL)
802ea3fc8e4SJohn Baldwin 		return;
803ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
804ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
805ea3fc8e4SJohn Baldwin 	kp->action = action;
806ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
807ea3fc8e4SJohn Baldwin 	kp->code = code;
80822ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
80922ec0406SDmitry Chagin 	ktrace_exit(td);
810df8bae1dSRodney W. Grimes }
811df8bae1dSRodney W. Grimes 
81226f9a767SRodney W. Grimes void
813039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg)
814df8bae1dSRodney W. Grimes {
81522ec0406SDmitry Chagin 	struct thread *td = curthread;
816ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
817ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
818df8bae1dSRodney W. Grimes 
819ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
820ad738f37SMatt Macy 		return;
821ad738f37SMatt Macy 
822ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
823ea3fc8e4SJohn Baldwin 	if (req == NULL)
824ea3fc8e4SJohn Baldwin 		return;
825ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
826ea3fc8e4SJohn Baldwin 	kc->out = out;
827ea3fc8e4SJohn Baldwin 	kc->user = user;
82888bf5036SJohn Baldwin 	if (wmesg != NULL)
82988bf5036SJohn Baldwin 		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
83088bf5036SJohn Baldwin 	else
83188bf5036SJohn Baldwin 		bzero(kc->wmesg, sizeof(kc->wmesg));
83222ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
83322ec0406SDmitry Chagin 	ktrace_exit(td);
834df8bae1dSRodney W. Grimes }
83560e15db9SDag-Erling Smørgrav 
83660e15db9SDag-Erling Smørgrav void
837ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen)
83860e15db9SDag-Erling Smørgrav {
83960e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
8404dd3a21fSMateusz Guzik 	char *buf;
8414dd3a21fSMateusz Guzik 	size_t buflen, namelen;
84260e15db9SDag-Erling Smørgrav 
843ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
844ad738f37SMatt Macy 		return;
845ad738f37SMatt Macy 
8464dd3a21fSMateusz Guzik 	if (data == NULL)
84760e15db9SDag-Erling Smørgrav 		datalen = 0;
8484dd3a21fSMateusz Guzik 	namelen = strlen(name) + 1;
8494dd3a21fSMateusz Guzik 	buflen = namelen + datalen;
85060e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
851a3052d6eSJohn Baldwin 	strcpy(buf, name);
8524dd3a21fSMateusz Guzik 	bcopy(data, buf + namelen, datalen);
85360e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
85460e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
85560e15db9SDag-Erling Smørgrav 		return;
85660e15db9SDag-Erling Smørgrav 	}
85760e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
85860e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
85960e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
86060e15db9SDag-Erling Smørgrav }
861c601ad8eSDag-Erling Smørgrav 
862c601ad8eSDag-Erling Smørgrav void
8630a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
8640a1427c5SMateusz Guzik {
8650a1427c5SMateusz Guzik 
8660a1427c5SMateusz Guzik 	if (error == 0)
8670a1427c5SMateusz Guzik 		ktrstruct(name, data, datalen);
8680a1427c5SMateusz Guzik }
8690a1427c5SMateusz Guzik 
8700a1427c5SMateusz Guzik void
871ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data,
872ffb66079SJohn Baldwin     int num_items, size_t struct_size)
873ffb66079SJohn Baldwin {
874ffb66079SJohn Baldwin 	struct ktr_request *req;
875ffb66079SJohn Baldwin 	struct ktr_struct_array *ksa;
876ffb66079SJohn Baldwin 	char *buf;
877ffb66079SJohn Baldwin 	size_t buflen, datalen, namelen;
878ffb66079SJohn Baldwin 	int max_items;
879ffb66079SJohn Baldwin 
880ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
881ad738f37SMatt Macy 		return;
882f8851007SMark Johnston 	if (num_items < 0)
883f8851007SMark Johnston 		return;
884ad738f37SMatt Macy 
885ffb66079SJohn Baldwin 	/* Trim array length to genio size. */
886ffb66079SJohn Baldwin 	max_items = ktr_geniosize / struct_size;
887ffb66079SJohn Baldwin 	if (num_items > max_items) {
888ffb66079SJohn Baldwin 		if (max_items == 0)
889ffb66079SJohn Baldwin 			num_items = 1;
890ffb66079SJohn Baldwin 		else
891ffb66079SJohn Baldwin 			num_items = max_items;
892ffb66079SJohn Baldwin 	}
893ffb66079SJohn Baldwin 	datalen = num_items * struct_size;
894ffb66079SJohn Baldwin 
895ffb66079SJohn Baldwin 	if (data == NULL)
896ffb66079SJohn Baldwin 		datalen = 0;
897ffb66079SJohn Baldwin 
898ffb66079SJohn Baldwin 	namelen = strlen(name) + 1;
899ffb66079SJohn Baldwin 	buflen = namelen + datalen;
900ffb66079SJohn Baldwin 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
901ffb66079SJohn Baldwin 	strcpy(buf, name);
902ffb66079SJohn Baldwin 	if (seg == UIO_SYSSPACE)
903ffb66079SJohn Baldwin 		bcopy(data, buf + namelen, datalen);
904ffb66079SJohn Baldwin 	else {
905ffb66079SJohn Baldwin 		if (copyin(data, buf + namelen, datalen) != 0) {
906ffb66079SJohn Baldwin 			free(buf, M_KTRACE);
907ffb66079SJohn Baldwin 			return;
908ffb66079SJohn Baldwin 		}
909ffb66079SJohn Baldwin 	}
910ffb66079SJohn Baldwin 	if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
911ffb66079SJohn Baldwin 		free(buf, M_KTRACE);
912ffb66079SJohn Baldwin 		return;
913ffb66079SJohn Baldwin 	}
914ffb66079SJohn Baldwin 	ksa = &req->ktr_data.ktr_struct_array;
915ffb66079SJohn Baldwin 	ksa->struct_size = struct_size;
916ffb66079SJohn Baldwin 	req->ktr_buffer = buf;
917ffb66079SJohn Baldwin 	req->ktr_header.ktr_len = buflen;
918ffb66079SJohn Baldwin 	ktr_submitrequest(curthread, req);
919ffb66079SJohn Baldwin }
920ffb66079SJohn Baldwin 
921ffb66079SJohn Baldwin void
922039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
923039644ecSEd Maste     const cap_rights_t *held)
924c601ad8eSDag-Erling Smørgrav {
925c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
926c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
927c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
928c601ad8eSDag-Erling Smørgrav 
929ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
930ad738f37SMatt Macy 		return;
931ad738f37SMatt Macy 
932c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
933c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
934c601ad8eSDag-Erling Smørgrav 		return;
935c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
936e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
9373fded357SPawel Jakub Dawidek 	if (needed != NULL)
9387008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
9393fded357SPawel Jakub Dawidek 	else
9403fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
9413fded357SPawel Jakub Dawidek 	if (held != NULL)
9427008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
9433fded357SPawel Jakub Dawidek 	else
9443fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
945c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
946c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
947c601ad8eSDag-Erling Smørgrav }
94835818d2eSJohn Baldwin 
94935818d2eSJohn Baldwin void
950039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
95135818d2eSJohn Baldwin {
95235818d2eSJohn Baldwin 	struct thread *td = curthread;
95335818d2eSJohn Baldwin 	struct ktr_request *req;
95435818d2eSJohn Baldwin 	struct ktr_fault *kf;
95535818d2eSJohn Baldwin 
956ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
957ad738f37SMatt Macy 		return;
958ad738f37SMatt Macy 
95935818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
96035818d2eSJohn Baldwin 	if (req == NULL)
96135818d2eSJohn Baldwin 		return;
96235818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
96335818d2eSJohn Baldwin 	kf->vaddr = vaddr;
96435818d2eSJohn Baldwin 	kf->type = type;
96535818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
96635818d2eSJohn Baldwin 	ktrace_exit(td);
96735818d2eSJohn Baldwin }
96835818d2eSJohn Baldwin 
96935818d2eSJohn Baldwin void
970039644ecSEd Maste ktrfaultend(int result)
97135818d2eSJohn Baldwin {
97235818d2eSJohn Baldwin 	struct thread *td = curthread;
97335818d2eSJohn Baldwin 	struct ktr_request *req;
97435818d2eSJohn Baldwin 	struct ktr_faultend *kf;
97535818d2eSJohn Baldwin 
976ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
977ad738f37SMatt Macy 		return;
978ad738f37SMatt Macy 
97935818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
98035818d2eSJohn Baldwin 	if (req == NULL)
98135818d2eSJohn Baldwin 		return;
98235818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
98335818d2eSJohn Baldwin 	kf->result = result;
98435818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
98535818d2eSJohn Baldwin 	ktrace_exit(td);
98635818d2eSJohn Baldwin }
98764cc6a13SJohn Baldwin #endif /* KTRACE */
988df8bae1dSRodney W. Grimes 
989df8bae1dSRodney W. Grimes /* Interface and common routines */
990df8bae1dSRodney W. Grimes 
991d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
992df8bae1dSRodney W. Grimes struct ktrace_args {
993df8bae1dSRodney W. Grimes 	char	*fname;
994df8bae1dSRodney W. Grimes 	int	ops;
995df8bae1dSRodney W. Grimes 	int	facs;
996df8bae1dSRodney W. Grimes 	int	pid;
997df8bae1dSRodney W. Grimes };
998d2d3e875SBruce Evans #endif
999df8bae1dSRodney W. Grimes /* ARGSUSED */
100026f9a767SRodney W. Grimes int
1001039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
1002df8bae1dSRodney W. Grimes {
1003db6a20e2SGarrett Wollman #ifdef KTRACE
1004039644ecSEd Maste 	struct vnode *vp = NULL;
1005039644ecSEd Maste 	struct proc *p;
1006df8bae1dSRodney W. Grimes 	struct pgrp *pg;
1007df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
1008df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
1009df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
1010f3851b23SMark Johnston 	int ret = 0;
10115050aa86SKonstantin Belousov 	int flags, error = 0;
1012df8bae1dSRodney W. Grimes 	struct nameidata nd;
10131762f674SKonstantin Belousov 	struct ktr_io_params *kiop, *old_kiop;
1014df8bae1dSRodney W. Grimes 
101564cc6a13SJohn Baldwin 	/*
101664cc6a13SJohn Baldwin 	 * Need something to (un)trace.
101764cc6a13SJohn Baldwin 	 */
101864cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
101964cc6a13SJohn Baldwin 		return (EINVAL);
102064cc6a13SJohn Baldwin 
10211762f674SKonstantin Belousov 	kiop = NULL;
1022df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
1023df8bae1dSRodney W. Grimes 		/*
1024df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
1025df8bae1dSRodney W. Grimes 		 */
1026*7e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1027e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
10289e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
1029e4b16f2fSMark Johnston 		if (error)
1030df8bae1dSRodney W. Grimes 			return (error);
1031762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
1032df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
1033b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
1034df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
1035a854ed98SJohn Baldwin 			(void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1036df8bae1dSRodney W. Grimes 			return (EACCES);
1037df8bae1dSRodney W. Grimes 		}
10381762f674SKonstantin Belousov 		kiop = ktr_io_params_alloc(td, vp);
1039df8bae1dSRodney W. Grimes 	}
1040e4b16f2fSMark Johnston 
1041df8bae1dSRodney W. Grimes 	/*
104279deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
1043df8bae1dSRodney W. Grimes 	 */
1044e4b16f2fSMark Johnston 	ktrace_enter(td);
1045df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
10461762f674SKonstantin Belousov restart:
10471005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10484f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
10491762f674SKonstantin Belousov 			old_kiop = NULL;
1050a7ff7443SJohn Baldwin 			PROC_LOCK(p);
10511762f674SKonstantin Belousov 			if (p->p_ktrioparms != NULL &&
10521762f674SKonstantin Belousov 			    p->p_ktrioparms->vp == vp) {
1053ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
1054ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
10551762f674SKonstantin Belousov 					old_kiop = ktr_freeproc(p);
1056ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
105751fd6380SMike Pritchard 				} else
1058df8bae1dSRodney W. Grimes 					error = EPERM;
1059df8bae1dSRodney W. Grimes 			}
1060a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
10611762f674SKonstantin Belousov 			if (old_kiop != NULL) {
10621762f674SKonstantin Belousov 				sx_sunlock(&allproc_lock);
10631762f674SKonstantin Belousov 				ktr_io_params_free(old_kiop);
10641762f674SKonstantin Belousov 				goto restart;
10651762f674SKonstantin Belousov 			}
106679deba82SMatthew Dillon 		}
10671005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1068df8bae1dSRodney W. Grimes 		goto done;
1069df8bae1dSRodney W. Grimes 	}
1070df8bae1dSRodney W. Grimes 	/*
1071df8bae1dSRodney W. Grimes 	 * do it
1072df8bae1dSRodney W. Grimes 	 */
107364cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
1074df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
1075df8bae1dSRodney W. Grimes 		/*
1076df8bae1dSRodney W. Grimes 		 * by process group
1077df8bae1dSRodney W. Grimes 		 */
1078df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
1079df8bae1dSRodney W. Grimes 		if (pg == NULL) {
1080ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
1081df8bae1dSRodney W. Grimes 			error = ESRCH;
1082df8bae1dSRodney W. Grimes 			goto done;
1083df8bae1dSRodney W. Grimes 		}
1084f3851b23SMark Johnston 
1085f591779bSSeigo Tanimura 		/*
1086f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
1087ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
1088f591779bSSeigo Tanimura 		 */
1089f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
1090f3851b23SMark Johnston 		if (LIST_EMPTY(&pg->pg_members)) {
1091f3851b23SMark Johnston 			sx_sunlock(&proctree_lock);
1092f3851b23SMark Johnston 			error = ESRCH;
1093f3851b23SMark Johnston 			goto done;
1094f3851b23SMark Johnston 		}
1095400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1096400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
1097df8bae1dSRodney W. Grimes 			if (descend)
10981762f674SKonstantin Belousov 				ret |= ktrsetchildren(td, p, ops, facs, kiop);
1099df8bae1dSRodney W. Grimes 			else
11001762f674SKonstantin Belousov 				ret |= ktrops(td, p, ops, facs, kiop);
1101400a74bfSPawel Jakub Dawidek 		}
1102df8bae1dSRodney W. Grimes 	} else {
1103df8bae1dSRodney W. Grimes 		/*
1104df8bae1dSRodney W. Grimes 		 * by pid
1105df8bae1dSRodney W. Grimes 		 */
1106df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
1107f3851b23SMark Johnston 		if (p == NULL) {
1108df8bae1dSRodney W. Grimes 			error = ESRCH;
1109b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
11104eb7c9f6SPawel Jakub Dawidek 			goto done;
1111b0d9aeddSPawel Jakub Dawidek 		}
1112df8bae1dSRodney W. Grimes 		if (descend)
11131762f674SKonstantin Belousov 			ret |= ktrsetchildren(td, p, ops, facs, kiop);
1114df8bae1dSRodney W. Grimes 		else
11151762f674SKonstantin Belousov 			ret |= ktrops(td, p, ops, facs, kiop);
1116df8bae1dSRodney W. Grimes 	}
111764cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
1118df8bae1dSRodney W. Grimes 	if (!ret)
1119df8bae1dSRodney W. Grimes 		error = EPERM;
1120df8bae1dSRodney W. Grimes done:
11211762f674SKonstantin Belousov 	if (kiop != NULL) {
11221762f674SKonstantin Belousov 		mtx_lock(&ktrace_mtx);
11231762f674SKonstantin Belousov 		kiop = ktr_io_params_rele(kiop);
11241762f674SKonstantin Belousov 		mtx_unlock(&ktrace_mtx);
11251762f674SKonstantin Belousov 		ktr_io_params_free(kiop);
11261762f674SKonstantin Belousov 	}
11272c255e9dSRobert Watson 	ktrace_exit(td);
1128df8bae1dSRodney W. Grimes 	return (error);
112964cc6a13SJohn Baldwin #else /* !KTRACE */
113064cc6a13SJohn Baldwin 	return (ENOSYS);
113164cc6a13SJohn Baldwin #endif /* KTRACE */
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
1134e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1135e6c4b9baSPoul-Henning Kamp int
1136039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
1137e6c4b9baSPoul-Henning Kamp {
1138b40ce416SJulian Elischer 
1139e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1140ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
11417f05b035SAlfred Perlstein 	void *cp;
1142c9e7d28eSJohn Baldwin 	int error;
1143e6c4b9baSPoul-Henning Kamp 
1144c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1145c9e7d28eSJohn Baldwin 		return (0);
1146bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
11470bad156aSAlfred Perlstein 		return (EINVAL);
1148a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1149c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
115050c22331SPoul-Henning Kamp 	if (error) {
115150c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1152c9e7d28eSJohn Baldwin 		return (error);
115350c22331SPoul-Henning Kamp 	}
1154ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
115550c22331SPoul-Henning Kamp 	if (req == NULL) {
115650c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1157b10221ffSJoseph Koshy 		return (ENOMEM);
115850c22331SPoul-Henning Kamp 	}
1159d977a583SRobert Watson 	req->ktr_buffer = cp;
1160ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
11612c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1162e6c4b9baSPoul-Henning Kamp 	return (0);
116364cc6a13SJohn Baldwin #else /* !KTRACE */
1164e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
116564cc6a13SJohn Baldwin #endif /* KTRACE */
1166e6c4b9baSPoul-Henning Kamp }
1167e6c4b9baSPoul-Henning Kamp 
1168db6a20e2SGarrett Wollman #ifdef KTRACE
116987b6de2bSPoul-Henning Kamp static int
11701762f674SKonstantin Belousov ktrops(struct thread *td, struct proc *p, int ops, int facs,
11711762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1172df8bae1dSRodney W. Grimes {
11731762f674SKonstantin Belousov 	struct ktr_io_params *old_kiop;
1174df8bae1dSRodney W. Grimes 
1175fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1176a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1177a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1178df8bae1dSRodney W. Grimes 		return (0);
1179a7ff7443SJohn Baldwin 	}
1180283e60fbSMark Johnston 	if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1181283e60fbSMark Johnston 	    p_cansee(td, p) != 0) {
1182f3851b23SMark Johnston 		/*
1183f3851b23SMark Johnston 		 * Disallow setting trace points if the process is being born.
1184f3851b23SMark Johnston 		 * This avoids races with trace point inheritance in
1185f3851b23SMark Johnston 		 * ktrprocfork().
1186f3851b23SMark Johnston 		 */
1187f3851b23SMark Johnston 		PROC_UNLOCK(p);
1188f3851b23SMark Johnston 		return (0);
1189f3851b23SMark Johnston 	}
1190f3851b23SMark Johnston 	if ((p->p_flag & P_WEXIT) != 0) {
1191f3851b23SMark Johnston 		/*
1192f3851b23SMark Johnston 		 * There's nothing to do if the process is exiting, but avoid
1193f3851b23SMark Johnston 		 * signaling an error.
1194f3851b23SMark Johnston 		 */
1195fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1196fe41d17aSJohn Baldwin 		return (1);
1197fe41d17aSJohn Baldwin 	}
11981762f674SKonstantin Belousov 	old_kiop = NULL;
1199ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1200df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
12011762f674SKonstantin Belousov 		if (p->p_ktrioparms != NULL &&
12021762f674SKonstantin Belousov 		    p->p_ktrioparms->vp != new_kiop->vp) {
12031762f674SKonstantin Belousov 			/* if trace file already in use, relinquish below */
12041762f674SKonstantin Belousov 			old_kiop = ktr_io_params_rele(p->p_ktrioparms);
12051762f674SKonstantin Belousov 			p->p_ktrioparms = NULL;
1206a5881ea5SJohn Baldwin 		}
12071762f674SKonstantin Belousov 		if (p->p_ktrioparms == NULL) {
12081762f674SKonstantin Belousov 			p->p_ktrioparms = new_kiop;
12091762f674SKonstantin Belousov 			ktr_io_params_ref(new_kiop);
1210df8bae1dSRodney W. Grimes 		}
1211df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
121232f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1213df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1214df8bae1dSRodney W. Grimes 	} else {
1215df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1216d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1217df8bae1dSRodney W. Grimes 			/* no more tracing */
12181762f674SKonstantin Belousov 			old_kiop = ktr_freeproc(p);
1219a7ff7443SJohn Baldwin 	}
1220ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
122122ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
122222ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1223a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
12241762f674SKonstantin Belousov 	ktr_io_params_free(old_kiop);
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 	return (1);
1227df8bae1dSRodney W. Grimes }
1228df8bae1dSRodney W. Grimes 
122987b6de2bSPoul-Henning Kamp static int
1230039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
12311762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1232df8bae1dSRodney W. Grimes {
1233039644ecSEd Maste 	struct proc *p;
1234039644ecSEd Maste 	int ret = 0;
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes 	p = top;
1237fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
123864cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1239df8bae1dSRodney W. Grimes 	for (;;) {
12401762f674SKonstantin Belousov 		ret |= ktrops(td, p, ops, facs, new_kiop);
1241df8bae1dSRodney W. Grimes 		/*
1242df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1243df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1244df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1245df8bae1dSRodney W. Grimes 		 */
12462e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
12472e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1248df8bae1dSRodney W. Grimes 		else for (;;) {
124964cc6a13SJohn Baldwin 			if (p == top)
1250df8bae1dSRodney W. Grimes 				return (ret);
12512e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
12522e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1253df8bae1dSRodney W. Grimes 				break;
1254df8bae1dSRodney W. Grimes 			}
1255b75356e1SJeffrey Hsu 			p = p->p_pptr;
1256df8bae1dSRodney W. Grimes 		}
1257fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1258df8bae1dSRodney W. Grimes 	}
1259df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1260df8bae1dSRodney W. Grimes }
1261df8bae1dSRodney W. Grimes 
126287b6de2bSPoul-Henning Kamp static void
12632c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1264df8bae1dSRodney W. Grimes {
1265fc369a35SKonstantin Belousov 	struct ktr_io_params *kiop, *kiop1;
1266ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1267ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1268ea3fc8e4SJohn Baldwin 	struct proc *p;
1269ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1270df8bae1dSRodney W. Grimes 	struct uio auio;
1271ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1272f2a2857bSKirk McKusick 	struct mount *mp;
127302645b88SKonstantin Belousov 	off_t lim;
1274a6144f71SKonstantin Belousov 	int datalen, buflen;
12755050aa86SKonstantin Belousov 	int error;
1276df8bae1dSRodney W. Grimes 
12771762f674SKonstantin Belousov 	p = td->td_proc;
12781762f674SKonstantin Belousov 
12792c255e9dSRobert Watson 	/*
1280fc369a35SKonstantin Belousov 	 * We reference the kiop for use in I/O in case ktrace is
12812c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
12822c255e9dSRobert Watson 	 */
12832c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
12841762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
12852c255e9dSRobert Watson 
1286ea3fc8e4SJohn Baldwin 	/*
12871762f674SKonstantin Belousov 	 * If kiop is NULL, it has been cleared out from under this
12881762f674SKonstantin Belousov 	 * request, so just drop it.
1289ea3fc8e4SJohn Baldwin 	 */
12901762f674SKonstantin Belousov 	if (kiop == NULL) {
1291118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1292df8bae1dSRodney W. Grimes 		return;
12932c255e9dSRobert Watson 	}
12941762f674SKonstantin Belousov 
1295fc369a35SKonstantin Belousov 	ktr_io_params_ref(kiop);
12961762f674SKonstantin Belousov 	vp = kiop->vp;
12971762f674SKonstantin Belousov 	cred = kiop->cr;
129802645b88SKonstantin Belousov 	lim = kiop->lim;
12991762f674SKonstantin Belousov 
13002c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1301118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
13022c255e9dSRobert Watson 
1303ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
130402abd400SPedro F. Giffuni 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1305a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
13068b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1307ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1308df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1309df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1310df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1311df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1312df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1313df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1314df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1315df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1316ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1317ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1318ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1319ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1320ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1321df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1322ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1323ea3fc8e4SJohn Baldwin 	}
1324ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1325d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1326d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1327ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1328ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1329ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1330b92584a6SJohn Baldwin 	}
13312c255e9dSRobert Watson 
1332f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1333cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
133402645b88SKonstantin Belousov 	td->td_ktr_io_lim = lim;
1335467a273cSRobert Watson #ifdef MAC
133630d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1337467a273cSRobert Watson 	if (error == 0)
1338467a273cSRobert Watson #endif
1339ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1340b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
1341f2a2857bSKirk McKusick 	vn_finished_write(mp);
13421762f674SKonstantin Belousov 	if (error == 0) {
1343fc369a35SKonstantin Belousov 		mtx_lock(&ktrace_mtx);
1344fc369a35SKonstantin Belousov 		kiop = ktr_io_params_rele(kiop);
1345fc369a35SKonstantin Belousov 		mtx_unlock(&ktrace_mtx);
1346fc369a35SKonstantin Belousov 		ktr_io_params_free(kiop);
1347df8bae1dSRodney W. Grimes 		return;
1348118258f5SBjoern A. Zeeb 	}
1349118258f5SBjoern A. Zeeb 
1350df8bae1dSRodney W. Grimes 	/*
1351a6144f71SKonstantin Belousov 	 * If error encountered, give up tracing on this vnode on this
1352a6144f71SKonstantin Belousov 	 * process.  Other processes might still be suitable for
1353a6144f71SKonstantin Belousov 	 * writes to this vnode.
1354df8bae1dSRodney W. Grimes 	 */
1355a6144f71SKonstantin Belousov 	log(LOG_NOTICE,
1356a6144f71SKonstantin Belousov 	    "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1357a6144f71SKonstantin Belousov 	    error, p->p_pid);
13581762f674SKonstantin Belousov 
1359fc369a35SKonstantin Belousov 	kiop1 = NULL;
1360ea3fc8e4SJohn Baldwin 	PROC_LOCK(p);
1361ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
13621762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1363fc369a35SKonstantin Belousov 		kiop1 = ktr_freeproc(p);
1364fc369a35SKonstantin Belousov 	kiop = ktr_io_params_rele(kiop);
1365ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1366ea3fc8e4SJohn Baldwin 	PROC_UNLOCK(p);
1367fc369a35SKonstantin Belousov 	ktr_io_params_free(kiop1);
13681762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
1369df8bae1dSRodney W. Grimes }
1370df8bae1dSRodney W. Grimes 
1371df8bae1dSRodney W. Grimes /*
1372df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1373df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1374df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1375df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1376df8bae1dSRodney W. Grimes  * so, only root may further change it.
1377df8bae1dSRodney W. Grimes  */
137887b6de2bSPoul-Henning Kamp static int
1379039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1380df8bae1dSRodney W. Grimes {
1381df8bae1dSRodney W. Grimes 
1382a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1383a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
138432f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
138575c13541SPoul-Henning Kamp 		return (0);
1386a0f75161SRobert Watson 
1387f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1388a0f75161SRobert Watson 		return (0);
1389a0f75161SRobert Watson 
1390df8bae1dSRodney W. Grimes 	return (1);
1391df8bae1dSRodney W. Grimes }
1392df8bae1dSRodney W. Grimes 
1393db6a20e2SGarrett Wollman #endif /* KTRACE */
1394