xref: /freebsd/sys/kern/kern_ktrace.c (revision ea2b64c2413355ac0d5fc6ff597342e9437a34d4)
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 
145*ea2b64c2SKonstantin Belousov /*
146*ea2b64c2SKonstantin Belousov  * Allow to not to send signal to traced process, in which context the
147*ea2b64c2SKonstantin Belousov  * ktr record is written.  The limit is applied from the process that
148*ea2b64c2SKonstantin Belousov  * set up ktrace, so killing the traced process is not completely fair.
149*ea2b64c2SKonstantin Belousov  */
150*ea2b64c2SKonstantin Belousov int ktr_filesize_limit_signal = 0;
151*ea2b64c2SKonstantin Belousov SYSCTL_INT(_kern_ktrace, OID_AUTO, filesize_limit_signal, CTLFLAG_RWTUN,
152*ea2b64c2SKonstantin Belousov     &ktr_filesize_limit_signal, 0,
153*ea2b64c2SKonstantin Belousov     "Send SIGXFSZ to the traced process when the log size limit is exceeded");
154*ea2b64c2SKonstantin 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++) {
222a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
223ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
224ea3fc8e4SJohn Baldwin 	}
225ea3fc8e4SJohn Baldwin }
226ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
227ea3fc8e4SJohn Baldwin 
228ea3fc8e4SJohn Baldwin static int
229ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
230ea3fc8e4SJohn Baldwin {
231ea3fc8e4SJohn Baldwin 	struct thread *td;
2328b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
233ea3fc8e4SJohn Baldwin 	int error;
234ea3fc8e4SJohn Baldwin 
235ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
236ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
237ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2388b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
239ea3fc8e4SJohn Baldwin 	}
240ea3fc8e4SJohn Baldwin 
2418b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
242ea3fc8e4SJohn Baldwin 	if (error)
243ea3fc8e4SJohn Baldwin 		return (error);
244ea3fc8e4SJohn Baldwin 	td = curthread;
2452c255e9dSRobert Watson 	ktrace_enter(td);
246ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
247b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2482c255e9dSRobert Watson 	ktrace_exit(td);
2498b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
250ea3fc8e4SJohn Baldwin 	if (error)
251ea3fc8e4SJohn Baldwin 		return (error);
252a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
253ea3fc8e4SJohn Baldwin 		return (ENOSPC);
254ea3fc8e4SJohn Baldwin 	return (0);
255ea3fc8e4SJohn Baldwin }
2567029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
2577029da5cSPawel Biernacki     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
2587029da5cSPawel Biernacki     sysctl_kern_ktrace_request_pool, "IU",
259a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
260ea3fc8e4SJohn Baldwin 
2618b149b51SJohn Baldwin static u_int
262b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
263ea3fc8e4SJohn Baldwin {
264b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
265ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
266a5896914SJoseph Koshy 	int bound;
267ea3fc8e4SJohn Baldwin 
268ea3fc8e4SJohn Baldwin 	print_message = 1;
269b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
270a5896914SJoseph Koshy 	if (bound == 0)
271a5896914SJoseph Koshy 		return (ktr_requestpool);
272b4c20e5eSDmitry Chagin 	if (bound < 0) {
273b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
274ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
275a5896914SJoseph Koshy 		while (bound++ < 0) {
276ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
277ea3fc8e4SJohn Baldwin 			if (req == NULL)
278b4c20e5eSDmitry Chagin 				break;
279ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
280ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
281ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
282ea3fc8e4SJohn Baldwin 		}
283b4c20e5eSDmitry Chagin 	} else {
284ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
285b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
286a5896914SJoseph Koshy 		while (bound-- > 0) {
287ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
288a163d034SWarner Losh 			    M_WAITOK);
289b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
290ea3fc8e4SJohn Baldwin 		}
291b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
292b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
293b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
294b4c20e5eSDmitry Chagin 	}
295b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
296ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
297ea3fc8e4SJohn Baldwin }
298ea3fc8e4SJohn Baldwin 
2995ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
3005ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
3015ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
3025ca4819dSJohn Baldwin 
303ea3fc8e4SJohn Baldwin static struct ktr_request *
30422ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
305ea3fc8e4SJohn Baldwin {
306ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
307ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
308ea3fc8e4SJohn Baldwin 	int pm;
309ea3fc8e4SJohn Baldwin 
310c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
311ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
312c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
313ea3fc8e4SJohn Baldwin 		return (NULL);
314ea3fc8e4SJohn Baldwin 	}
315ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
316ea3fc8e4SJohn Baldwin 	if (req != NULL) {
317ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
318ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
31975768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
32075768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
32175768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
32275768576SJohn Baldwin 		}
323c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
324ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
325ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3262bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3275ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3285ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
329d977a583SRobert Watson 		req->ktr_buffer = NULL;
330ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
331ea3fc8e4SJohn Baldwin 	} else {
33275768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
333ea3fc8e4SJohn Baldwin 		pm = print_message;
334ea3fc8e4SJohn Baldwin 		print_message = 0;
335ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
336ea3fc8e4SJohn Baldwin 		if (pm)
337ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
338ea3fc8e4SJohn Baldwin 	}
339ea3fc8e4SJohn Baldwin 	return (req);
340ea3fc8e4SJohn Baldwin }
341ea3fc8e4SJohn Baldwin 
3427705d4b2SDmitry Chagin static struct ktr_request *
3437705d4b2SDmitry Chagin ktr_getrequest(int type)
3447705d4b2SDmitry Chagin {
3457705d4b2SDmitry Chagin 	struct thread *td = curthread;
3467705d4b2SDmitry Chagin 	struct ktr_request *req;
3477705d4b2SDmitry Chagin 
3487705d4b2SDmitry Chagin 	ktrace_enter(td);
34922ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3507705d4b2SDmitry Chagin 	if (req == NULL)
3517705d4b2SDmitry Chagin 		ktrace_exit(td);
3527705d4b2SDmitry Chagin 
3537705d4b2SDmitry Chagin 	return (req);
3547705d4b2SDmitry Chagin }
3557705d4b2SDmitry Chagin 
3562c255e9dSRobert Watson /*
3572c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3582c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3592c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3602c255e9dSRobert Watson  * later.
3612c255e9dSRobert Watson  */
362ea3fc8e4SJohn Baldwin static void
3632c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
364ea3fc8e4SJohn Baldwin {
365ea3fc8e4SJohn Baldwin 
366ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3672c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
368ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
36946588778SEdward Tomasz Napierala 	thread_lock(td);
37046588778SEdward Tomasz Napierala 	td->td_flags |= TDF_ASTPENDING;
37146588778SEdward Tomasz Napierala 	thread_unlock(td);
3722c255e9dSRobert Watson }
3732c255e9dSRobert Watson 
3742c255e9dSRobert Watson /*
3752c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3762c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3772c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3782c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
379a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3802c255e9dSRobert Watson  */
3812c255e9dSRobert Watson static void
3822c255e9dSRobert Watson ktr_drain(struct thread *td)
3832c255e9dSRobert Watson {
3842c255e9dSRobert Watson 	struct ktr_request *queued_req;
3852c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3862c255e9dSRobert Watson 
3872c255e9dSRobert Watson 	ktrace_assert(td);
3882c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3892c255e9dSRobert Watson 
3902b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3912c255e9dSRobert Watson 
3922c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3932c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3942c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3952c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3962c255e9dSRobert Watson 
3972c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3982c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3992c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
4002c255e9dSRobert Watson 			ktr_freerequest(queued_req);
4012c255e9dSRobert Watson 		}
4022c255e9dSRobert Watson 	}
4032c255e9dSRobert Watson }
4042c255e9dSRobert Watson 
4052c255e9dSRobert Watson /*
4062c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
4072c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
4082c255e9dSRobert Watson  * been cached in the thread.
4092c255e9dSRobert Watson  */
4102c255e9dSRobert Watson static void
41122ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
4122c255e9dSRobert Watson {
4132c255e9dSRobert Watson 
4142c255e9dSRobert Watson 	ktrace_assert(td);
4152c255e9dSRobert Watson 
4162c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4172c255e9dSRobert Watson 	ktr_drain(td);
4182c255e9dSRobert Watson 	ktr_writerequest(td, req);
4192c255e9dSRobert Watson 	ktr_freerequest(req);
4202c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4212c255e9dSRobert Watson 	ktrace_exit(td);
422ea3fc8e4SJohn Baldwin }
423ea3fc8e4SJohn Baldwin 
424ea3fc8e4SJohn Baldwin static void
425ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
426ea3fc8e4SJohn Baldwin {
427ea3fc8e4SJohn Baldwin 
428d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
429d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
430d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
431d680caabSJohn Baldwin }
432d680caabSJohn Baldwin 
433d680caabSJohn Baldwin static void
434d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
435d680caabSJohn Baldwin {
436d680caabSJohn Baldwin 
437d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
438d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
439d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
440ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
441d680caabSJohn Baldwin }
442d680caabSJohn Baldwin 
4431762f674SKonstantin Belousov static void
4441762f674SKonstantin Belousov ktr_io_params_ref(struct ktr_io_params *kiop)
4451762f674SKonstantin Belousov {
4461762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4471762f674SKonstantin Belousov 	kiop->refs++;
4481762f674SKonstantin Belousov }
4491762f674SKonstantin Belousov 
4501762f674SKonstantin Belousov static struct ktr_io_params *
4511762f674SKonstantin Belousov ktr_io_params_rele(struct ktr_io_params *kiop)
4521762f674SKonstantin Belousov {
4531762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4541762f674SKonstantin Belousov 	if (kiop == NULL)
4551762f674SKonstantin Belousov 		return (NULL);
4561762f674SKonstantin Belousov 	KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
4571762f674SKonstantin Belousov 	return (--(kiop->refs) == 0 ? kiop : NULL);
4581762f674SKonstantin Belousov }
4591762f674SKonstantin Belousov 
4601762f674SKonstantin Belousov void
4611762f674SKonstantin Belousov ktr_io_params_free(struct ktr_io_params *kiop)
4621762f674SKonstantin Belousov {
4631762f674SKonstantin Belousov 	if (kiop == NULL)
4641762f674SKonstantin Belousov 		return;
4651762f674SKonstantin Belousov 
4661762f674SKonstantin Belousov 	MPASS(kiop->refs == 0);
4671762f674SKonstantin Belousov 	vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
4681762f674SKonstantin Belousov 	crfree(kiop->cr);
4691762f674SKonstantin Belousov 	free(kiop, M_KTRACE);
4701762f674SKonstantin Belousov }
4711762f674SKonstantin Belousov 
4721762f674SKonstantin Belousov static struct ktr_io_params *
4731762f674SKonstantin Belousov ktr_io_params_alloc(struct thread *td, struct vnode *vp)
4741762f674SKonstantin Belousov {
4751762f674SKonstantin Belousov 	struct ktr_io_params *res;
4761762f674SKonstantin Belousov 
4771762f674SKonstantin Belousov 	res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
4781762f674SKonstantin Belousov 	res->vp = vp;
4791762f674SKonstantin Belousov 	res->cr = crhold(td->td_ucred);
48002645b88SKonstantin Belousov 	res->lim = lim_cur(td, RLIMIT_FSIZE);
4811762f674SKonstantin Belousov 	res->refs = 1;
4821762f674SKonstantin Belousov 	return (res);
4831762f674SKonstantin Belousov }
4841762f674SKonstantin Belousov 
485d680caabSJohn Baldwin /*
486d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
487d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
488d680caabSJohn Baldwin  * vnode and credentials.
489d680caabSJohn Baldwin  */
4901762f674SKonstantin Belousov static struct ktr_io_params *
4911762f674SKonstantin Belousov ktr_freeproc(struct proc *p)
492d680caabSJohn Baldwin {
4931762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
494d680caabSJohn Baldwin 	struct ktr_request *req;
495d680caabSJohn Baldwin 
496d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
497d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
4981762f674SKonstantin Belousov 	kiop = ktr_io_params_rele(p->p_ktrioparms);
4991762f674SKonstantin Belousov 	p->p_ktrioparms = NULL;
500d680caabSJohn Baldwin 	p->p_traceflag = 0;
501d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
502d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
503d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
504d680caabSJohn Baldwin 	}
5051762f674SKonstantin Belousov 	return (kiop);
5061762f674SKonstantin Belousov }
5071762f674SKonstantin Belousov 
5081762f674SKonstantin Belousov struct vnode *
5091762f674SKonstantin Belousov ktr_get_tracevp(struct proc *p, bool ref)
5101762f674SKonstantin Belousov {
5111762f674SKonstantin Belousov 	struct vnode *vp;
5121762f674SKonstantin Belousov 
5131762f674SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
5141762f674SKonstantin Belousov 
5151762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL) {
5161762f674SKonstantin Belousov 		vp = p->p_ktrioparms->vp;
5171762f674SKonstantin Belousov 		if (ref)
5181762f674SKonstantin Belousov 			vrefact(vp);
5191762f674SKonstantin Belousov 	} else {
5201762f674SKonstantin Belousov 		vp = NULL;
5211762f674SKonstantin Belousov 	}
5221762f674SKonstantin Belousov 	return (vp);
523ea3fc8e4SJohn Baldwin }
524ea3fc8e4SJohn Baldwin 
52526f9a767SRodney W. Grimes void
526039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[])
527df8bae1dSRodney W. Grimes {
528ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
529df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
530ea3fc8e4SJohn Baldwin 	size_t buflen;
5314b3aac3dSJohn Baldwin 	char *buf = NULL;
532df8bae1dSRodney W. Grimes 
533ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
534ad738f37SMatt Macy 		return;
535ad738f37SMatt Macy 
5364b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
5374b3aac3dSJohn Baldwin 	if (buflen > 0) {
538a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
5394b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
5404b3aac3dSJohn Baldwin 	}
541ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
54250c22331SPoul-Henning Kamp 	if (req == NULL) {
54350c22331SPoul-Henning Kamp 		if (buf != NULL)
54450c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
545ea3fc8e4SJohn Baldwin 		return;
54650c22331SPoul-Henning Kamp 	}
547ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
548df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
549df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
550ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
551ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
552d977a583SRobert Watson 		req->ktr_buffer = buf;
553ea3fc8e4SJohn Baldwin 	}
5542c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
555df8bae1dSRodney W. Grimes }
556df8bae1dSRodney W. Grimes 
55726f9a767SRodney W. Grimes void
558039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
559df8bae1dSRodney W. Grimes {
560ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
561ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
562df8bae1dSRodney W. Grimes 
563ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
564ad738f37SMatt Macy 		return;
565ad738f37SMatt Macy 
566ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
567ea3fc8e4SJohn Baldwin 	if (req == NULL)
568ea3fc8e4SJohn Baldwin 		return;
569ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
570ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
571ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
5725a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
5732c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
5742c255e9dSRobert Watson }
5752c255e9dSRobert Watson 
5762c255e9dSRobert Watson /*
577d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
578d680caabSJohn Baldwin  *
579d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
580d680caabSJohn Baldwin  */
5811762f674SKonstantin Belousov struct ktr_io_params *
5821762f674SKonstantin Belousov ktrprocexec(struct proc *p)
583d680caabSJohn Baldwin {
5841762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
585d680caabSJohn Baldwin 
586d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
5871762f674SKonstantin Belousov 
5881762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
5891762f674SKonstantin Belousov 	if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
5901762f674SKonstantin Belousov 		return (NULL);
5911762f674SKonstantin Belousov 
592d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
5931762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
594d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5951762f674SKonstantin Belousov 	return (kiop);
596d680caabSJohn Baldwin }
597d680caabSJohn Baldwin 
598d680caabSJohn Baldwin /*
599d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
600d680caabSJohn Baldwin  * and disable tracing.
6012c255e9dSRobert Watson  */
6022c255e9dSRobert Watson void
6032c255e9dSRobert Watson ktrprocexit(struct thread *td)
6042c255e9dSRobert Watson {
6057705d4b2SDmitry Chagin 	struct ktr_request *req;
606d680caabSJohn Baldwin 	struct proc *p;
6071762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
608d680caabSJohn Baldwin 
609d680caabSJohn Baldwin 	p = td->td_proc;
610d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
611d680caabSJohn Baldwin 		return;
6122c255e9dSRobert Watson 
6132c255e9dSRobert Watson 	ktrace_enter(td);
61422ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
61522ec0406SDmitry Chagin 	if (req != NULL)
61622ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
6172c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6182c255e9dSRobert Watson 	ktr_drain(td);
6192c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
620d680caabSJohn Baldwin 	PROC_LOCK(p);
621d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
6221762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
623d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
624d680caabSJohn Baldwin 	PROC_UNLOCK(p);
6251762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
6262c255e9dSRobert Watson 	ktrace_exit(td);
6272c255e9dSRobert Watson }
6282c255e9dSRobert Watson 
6297705d4b2SDmitry Chagin static void
63022ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
6317705d4b2SDmitry Chagin {
6327705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
6337705d4b2SDmitry Chagin 	struct ktr_request *req;
634de60a5f3SDmitry Chagin 	struct thread *td2;
6357705d4b2SDmitry Chagin 
6367705d4b2SDmitry Chagin 	ktrace_assert(td);
6377705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
63822ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
6397705d4b2SDmitry Chagin 	if (req == NULL)
6407705d4b2SDmitry Chagin 		return;
6417705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
6427705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
64322ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
6447705d4b2SDmitry Chagin }
6457705d4b2SDmitry Chagin 
6467705d4b2SDmitry Chagin void
6477705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
6487705d4b2SDmitry Chagin {
6497705d4b2SDmitry Chagin 	struct thread *td = curthread;
6507705d4b2SDmitry Chagin 
6517705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
6527705d4b2SDmitry Chagin 		return;
6537705d4b2SDmitry Chagin 
6547705d4b2SDmitry Chagin 	ktrace_enter(td);
65522ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
6567705d4b2SDmitry Chagin 	ktrace_exit(td);
6577705d4b2SDmitry Chagin }
6587705d4b2SDmitry Chagin 
6592c255e9dSRobert Watson /*
660d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
661d680caabSJohn Baldwin  */
662d680caabSJohn Baldwin void
663d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
664d680caabSJohn Baldwin {
665d680caabSJohn Baldwin 
6661762f674SKonstantin Belousov 	MPASS(p2->p_ktrioparms == NULL);
6677c34b35bSMateusz Guzik 	MPASS(p2->p_traceflag == 0);
6687c34b35bSMateusz Guzik 
6697c34b35bSMateusz Guzik 	if (p1->p_traceflag == 0)
6707c34b35bSMateusz Guzik 		return;
6717c34b35bSMateusz Guzik 
6727705d4b2SDmitry Chagin 	PROC_LOCK(p1);
673d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
674d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
675d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
6761762f674SKonstantin Belousov 		if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
6771762f674SKonstantin Belousov 			p1->p_ktrioparms->refs++;
678d680caabSJohn Baldwin 	}
679d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6807705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
6817705d4b2SDmitry Chagin 
6827705d4b2SDmitry Chagin 	ktrprocctor(p2);
683d680caabSJohn Baldwin }
684d680caabSJohn Baldwin 
685d680caabSJohn Baldwin /*
6862c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
6872c255e9dSRobert Watson  * system call.
6882c255e9dSRobert Watson  */
6892c255e9dSRobert Watson void
6902c255e9dSRobert Watson ktruserret(struct thread *td)
6912c255e9dSRobert Watson {
6922c255e9dSRobert Watson 
6932c255e9dSRobert Watson 	ktrace_enter(td);
6942c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6952c255e9dSRobert Watson 	ktr_drain(td);
6962c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6972c255e9dSRobert Watson 	ktrace_exit(td);
698df8bae1dSRodney W. Grimes }
699df8bae1dSRodney W. Grimes 
70026f9a767SRodney W. Grimes void
701ea3fc8e4SJohn Baldwin ktrnamei(path)
702df8bae1dSRodney W. Grimes 	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;
882ad738f37SMatt Macy 
883ffb66079SJohn Baldwin 	/* Trim array length to genio size. */
884ffb66079SJohn Baldwin 	max_items = ktr_geniosize / struct_size;
885ffb66079SJohn Baldwin 	if (num_items > max_items) {
886ffb66079SJohn Baldwin 		if (max_items == 0)
887ffb66079SJohn Baldwin 			num_items = 1;
888ffb66079SJohn Baldwin 		else
889ffb66079SJohn Baldwin 			num_items = max_items;
890ffb66079SJohn Baldwin 	}
891ffb66079SJohn Baldwin 	datalen = num_items * struct_size;
892ffb66079SJohn Baldwin 
893ffb66079SJohn Baldwin 	if (data == NULL)
894ffb66079SJohn Baldwin 		datalen = 0;
895ffb66079SJohn Baldwin 
896ffb66079SJohn Baldwin 	namelen = strlen(name) + 1;
897ffb66079SJohn Baldwin 	buflen = namelen + datalen;
898ffb66079SJohn Baldwin 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
899ffb66079SJohn Baldwin 	strcpy(buf, name);
900ffb66079SJohn Baldwin 	if (seg == UIO_SYSSPACE)
901ffb66079SJohn Baldwin 		bcopy(data, buf + namelen, datalen);
902ffb66079SJohn Baldwin 	else {
903ffb66079SJohn Baldwin 		if (copyin(data, buf + namelen, datalen) != 0) {
904ffb66079SJohn Baldwin 			free(buf, M_KTRACE);
905ffb66079SJohn Baldwin 			return;
906ffb66079SJohn Baldwin 		}
907ffb66079SJohn Baldwin 	}
908ffb66079SJohn Baldwin 	if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
909ffb66079SJohn Baldwin 		free(buf, M_KTRACE);
910ffb66079SJohn Baldwin 		return;
911ffb66079SJohn Baldwin 	}
912ffb66079SJohn Baldwin 	ksa = &req->ktr_data.ktr_struct_array;
913ffb66079SJohn Baldwin 	ksa->struct_size = struct_size;
914ffb66079SJohn Baldwin 	req->ktr_buffer = buf;
915ffb66079SJohn Baldwin 	req->ktr_header.ktr_len = buflen;
916ffb66079SJohn Baldwin 	ktr_submitrequest(curthread, req);
917ffb66079SJohn Baldwin }
918ffb66079SJohn Baldwin 
919ffb66079SJohn Baldwin void
920039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
921039644ecSEd Maste     const cap_rights_t *held)
922c601ad8eSDag-Erling Smørgrav {
923c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
924c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
925c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
926c601ad8eSDag-Erling Smørgrav 
927ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
928ad738f37SMatt Macy 		return;
929ad738f37SMatt Macy 
930c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
931c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
932c601ad8eSDag-Erling Smørgrav 		return;
933c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
934e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
9353fded357SPawel Jakub Dawidek 	if (needed != NULL)
9367008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
9373fded357SPawel Jakub Dawidek 	else
9383fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
9393fded357SPawel Jakub Dawidek 	if (held != NULL)
9407008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
9413fded357SPawel Jakub Dawidek 	else
9423fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
943c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
944c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
945c601ad8eSDag-Erling Smørgrav }
94635818d2eSJohn Baldwin 
94735818d2eSJohn Baldwin void
948039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
94935818d2eSJohn Baldwin {
95035818d2eSJohn Baldwin 	struct thread *td = curthread;
95135818d2eSJohn Baldwin 	struct ktr_request *req;
95235818d2eSJohn Baldwin 	struct ktr_fault *kf;
95335818d2eSJohn Baldwin 
954ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
955ad738f37SMatt Macy 		return;
956ad738f37SMatt Macy 
95735818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
95835818d2eSJohn Baldwin 	if (req == NULL)
95935818d2eSJohn Baldwin 		return;
96035818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
96135818d2eSJohn Baldwin 	kf->vaddr = vaddr;
96235818d2eSJohn Baldwin 	kf->type = type;
96335818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
96435818d2eSJohn Baldwin 	ktrace_exit(td);
96535818d2eSJohn Baldwin }
96635818d2eSJohn Baldwin 
96735818d2eSJohn Baldwin void
968039644ecSEd Maste ktrfaultend(int result)
96935818d2eSJohn Baldwin {
97035818d2eSJohn Baldwin 	struct thread *td = curthread;
97135818d2eSJohn Baldwin 	struct ktr_request *req;
97235818d2eSJohn Baldwin 	struct ktr_faultend *kf;
97335818d2eSJohn Baldwin 
974ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
975ad738f37SMatt Macy 		return;
976ad738f37SMatt Macy 
97735818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
97835818d2eSJohn Baldwin 	if (req == NULL)
97935818d2eSJohn Baldwin 		return;
98035818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
98135818d2eSJohn Baldwin 	kf->result = result;
98235818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
98335818d2eSJohn Baldwin 	ktrace_exit(td);
98435818d2eSJohn Baldwin }
98564cc6a13SJohn Baldwin #endif /* KTRACE */
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes /* Interface and common routines */
988df8bae1dSRodney W. Grimes 
989d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
990df8bae1dSRodney W. Grimes struct ktrace_args {
991df8bae1dSRodney W. Grimes 	char	*fname;
992df8bae1dSRodney W. Grimes 	int	ops;
993df8bae1dSRodney W. Grimes 	int	facs;
994df8bae1dSRodney W. Grimes 	int	pid;
995df8bae1dSRodney W. Grimes };
996d2d3e875SBruce Evans #endif
997df8bae1dSRodney W. Grimes /* ARGSUSED */
99826f9a767SRodney W. Grimes int
999039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
1000df8bae1dSRodney W. Grimes {
1001db6a20e2SGarrett Wollman #ifdef KTRACE
1002039644ecSEd Maste 	struct vnode *vp = NULL;
1003039644ecSEd Maste 	struct proc *p;
1004df8bae1dSRodney W. Grimes 	struct pgrp *pg;
1005df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
1006df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
1007df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
1008400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
10095050aa86SKonstantin Belousov 	int flags, error = 0;
1010df8bae1dSRodney W. Grimes 	struct nameidata nd;
10111762f674SKonstantin Belousov 	struct ktr_io_params *kiop, *old_kiop;
1012df8bae1dSRodney W. Grimes 
101364cc6a13SJohn Baldwin 	/*
101464cc6a13SJohn Baldwin 	 * Need something to (un)trace.
101564cc6a13SJohn Baldwin 	 */
101664cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
101764cc6a13SJohn Baldwin 		return (EINVAL);
101864cc6a13SJohn Baldwin 
10191762f674SKonstantin Belousov 	kiop = NULL;
10202c255e9dSRobert Watson 	ktrace_enter(td);
1021df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
1022df8bae1dSRodney W. Grimes 		/*
1023df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
1024df8bae1dSRodney W. Grimes 		 */
10255050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
1026e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
10279e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
1028797f2d22SPoul-Henning Kamp 		if (error) {
10292c255e9dSRobert Watson 			ktrace_exit(td);
1030df8bae1dSRodney W. Grimes 			return (error);
1031df8bae1dSRodney W. Grimes 		}
1032762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
1033df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
1034b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
1035df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
1036a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
10372c255e9dSRobert Watson 			ktrace_exit(td);
1038df8bae1dSRodney W. Grimes 			return (EACCES);
1039df8bae1dSRodney W. Grimes 		}
10401762f674SKonstantin Belousov 		kiop = ktr_io_params_alloc(td, vp);
1041df8bae1dSRodney W. Grimes 	}
1042df8bae1dSRodney W. Grimes 	/*
104379deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
1044df8bae1dSRodney W. Grimes 	 */
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 		}
1084f591779bSSeigo Tanimura 		/*
1085f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
1086ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
1087f591779bSSeigo Tanimura 		 */
1088f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
1089400a74bfSPawel Jakub Dawidek 		nfound = 0;
1090400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1091400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
1092e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW ||
1093e806d352SJohn Baldwin 			    p_cansee(td, p) != 0) {
1094400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
1095400a74bfSPawel Jakub Dawidek 				continue;
1096400a74bfSPawel Jakub Dawidek 			}
1097400a74bfSPawel Jakub Dawidek 			nfound++;
1098df8bae1dSRodney W. Grimes 			if (descend)
10991762f674SKonstantin Belousov 				ret |= ktrsetchildren(td, p, ops, facs, kiop);
1100df8bae1dSRodney W. Grimes 			else
11011762f674SKonstantin Belousov 				ret |= ktrops(td, p, ops, facs, kiop);
1102400a74bfSPawel Jakub Dawidek 		}
1103400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
1104400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
1105400a74bfSPawel Jakub Dawidek 			error = ESRCH;
1106400a74bfSPawel Jakub Dawidek 			goto done;
1107400a74bfSPawel Jakub Dawidek 		}
1108df8bae1dSRodney W. Grimes 	} else {
1109df8bae1dSRodney W. Grimes 		/*
1110df8bae1dSRodney W. Grimes 		 * by pid
1111df8bae1dSRodney W. Grimes 		 */
1112df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
1113fe41d17aSJohn Baldwin 		if (p == NULL)
1114df8bae1dSRodney W. Grimes 			error = ESRCH;
1115fe41d17aSJohn Baldwin 		else
11164eb7c9f6SPawel Jakub Dawidek 			error = p_cansee(td, p);
1117b0d9aeddSPawel Jakub Dawidek 		if (error) {
1118fe41d17aSJohn Baldwin 			if (p != NULL)
1119fe41d17aSJohn Baldwin 				PROC_UNLOCK(p);
1120b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
11214eb7c9f6SPawel Jakub Dawidek 			goto done;
1122b0d9aeddSPawel Jakub Dawidek 		}
1123df8bae1dSRodney W. Grimes 		if (descend)
11241762f674SKonstantin Belousov 			ret |= ktrsetchildren(td, p, ops, facs, kiop);
1125df8bae1dSRodney W. Grimes 		else
11261762f674SKonstantin Belousov 			ret |= ktrops(td, p, ops, facs, kiop);
1127df8bae1dSRodney W. Grimes 	}
112864cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
1129df8bae1dSRodney W. Grimes 	if (!ret)
1130df8bae1dSRodney W. Grimes 		error = EPERM;
1131df8bae1dSRodney W. Grimes done:
11321762f674SKonstantin Belousov 	if (kiop != NULL) {
11331762f674SKonstantin Belousov 		mtx_lock(&ktrace_mtx);
11341762f674SKonstantin Belousov 		kiop = ktr_io_params_rele(kiop);
11351762f674SKonstantin Belousov 		mtx_unlock(&ktrace_mtx);
11361762f674SKonstantin Belousov 		ktr_io_params_free(kiop);
11371762f674SKonstantin Belousov 	}
11382c255e9dSRobert Watson 	ktrace_exit(td);
1139df8bae1dSRodney W. Grimes 	return (error);
114064cc6a13SJohn Baldwin #else /* !KTRACE */
114164cc6a13SJohn Baldwin 	return (ENOSYS);
114264cc6a13SJohn Baldwin #endif /* KTRACE */
1143df8bae1dSRodney W. Grimes }
1144df8bae1dSRodney W. Grimes 
1145e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1146e6c4b9baSPoul-Henning Kamp int
1147039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
1148e6c4b9baSPoul-Henning Kamp {
1149b40ce416SJulian Elischer 
1150e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1151ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
11527f05b035SAlfred Perlstein 	void *cp;
1153c9e7d28eSJohn Baldwin 	int error;
1154e6c4b9baSPoul-Henning Kamp 
1155c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1156c9e7d28eSJohn Baldwin 		return (0);
1157bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
11580bad156aSAlfred Perlstein 		return (EINVAL);
1159a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1160c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
116150c22331SPoul-Henning Kamp 	if (error) {
116250c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1163c9e7d28eSJohn Baldwin 		return (error);
116450c22331SPoul-Henning Kamp 	}
1165ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
116650c22331SPoul-Henning Kamp 	if (req == NULL) {
116750c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1168b10221ffSJoseph Koshy 		return (ENOMEM);
116950c22331SPoul-Henning Kamp 	}
1170d977a583SRobert Watson 	req->ktr_buffer = cp;
1171ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
11722c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1173e6c4b9baSPoul-Henning Kamp 	return (0);
117464cc6a13SJohn Baldwin #else /* !KTRACE */
1175e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
117664cc6a13SJohn Baldwin #endif /* KTRACE */
1177e6c4b9baSPoul-Henning Kamp }
1178e6c4b9baSPoul-Henning Kamp 
1179db6a20e2SGarrett Wollman #ifdef KTRACE
118087b6de2bSPoul-Henning Kamp static int
11811762f674SKonstantin Belousov ktrops(struct thread *td, struct proc *p, int ops, int facs,
11821762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1183df8bae1dSRodney W. Grimes {
11841762f674SKonstantin Belousov 	struct ktr_io_params *old_kiop;
1185df8bae1dSRodney W. Grimes 
1186fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1187a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1188a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1189df8bae1dSRodney W. Grimes 		return (0);
1190a7ff7443SJohn Baldwin 	}
1191fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1192fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1193fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1194fe41d17aSJohn Baldwin 		return (1);
1195fe41d17aSJohn Baldwin 	}
11961762f674SKonstantin Belousov 	old_kiop = NULL;
1197ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1198df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
11991762f674SKonstantin Belousov 		if (p->p_ktrioparms != NULL &&
12001762f674SKonstantin Belousov 		    p->p_ktrioparms->vp != new_kiop->vp) {
12011762f674SKonstantin Belousov 			/* if trace file already in use, relinquish below */
12021762f674SKonstantin Belousov 			old_kiop = ktr_io_params_rele(p->p_ktrioparms);
12031762f674SKonstantin Belousov 			p->p_ktrioparms = NULL;
1204a5881ea5SJohn Baldwin 		}
12051762f674SKonstantin Belousov 		if (p->p_ktrioparms == NULL) {
12061762f674SKonstantin Belousov 			p->p_ktrioparms = new_kiop;
12071762f674SKonstantin Belousov 			ktr_io_params_ref(new_kiop);
1208df8bae1dSRodney W. Grimes 		}
1209df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
121032f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1211df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1212df8bae1dSRodney W. Grimes 	} else {
1213df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1214d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1215df8bae1dSRodney W. Grimes 			/* no more tracing */
12161762f674SKonstantin Belousov 			old_kiop = ktr_freeproc(p);
1217a7ff7443SJohn Baldwin 	}
1218ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
121922ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
122022ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1221a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
12221762f674SKonstantin Belousov 	ktr_io_params_free(old_kiop);
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes 	return (1);
1225df8bae1dSRodney W. Grimes }
1226df8bae1dSRodney W. Grimes 
122787b6de2bSPoul-Henning Kamp static int
1228039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
12291762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1230df8bae1dSRodney W. Grimes {
1231039644ecSEd Maste 	struct proc *p;
1232039644ecSEd Maste 	int ret = 0;
1233df8bae1dSRodney W. Grimes 
1234df8bae1dSRodney W. Grimes 	p = top;
1235fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
123664cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1237df8bae1dSRodney W. Grimes 	for (;;) {
12381762f674SKonstantin Belousov 		ret |= ktrops(td, p, ops, facs, new_kiop);
1239df8bae1dSRodney W. Grimes 		/*
1240df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1241df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1242df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1243df8bae1dSRodney W. Grimes 		 */
12442e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
12452e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1246df8bae1dSRodney W. Grimes 		else for (;;) {
124764cc6a13SJohn Baldwin 			if (p == top)
1248df8bae1dSRodney W. Grimes 				return (ret);
12492e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
12502e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1251df8bae1dSRodney W. Grimes 				break;
1252df8bae1dSRodney W. Grimes 			}
1253b75356e1SJeffrey Hsu 			p = p->p_pptr;
1254df8bae1dSRodney W. Grimes 		}
1255fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1256df8bae1dSRodney W. Grimes 	}
1257df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1258df8bae1dSRodney W. Grimes }
1259df8bae1dSRodney W. Grimes 
126087b6de2bSPoul-Henning Kamp static void
12612c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1262df8bae1dSRodney W. Grimes {
12631762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
1264ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1265ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1266ea3fc8e4SJohn Baldwin 	struct proc *p;
1267ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1268df8bae1dSRodney W. Grimes 	struct uio auio;
1269ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1270f2a2857bSKirk McKusick 	struct mount *mp;
127102645b88SKonstantin Belousov 	off_t lim;
1272a6144f71SKonstantin Belousov 	int datalen, buflen;
12735050aa86SKonstantin Belousov 	int error;
1274df8bae1dSRodney W. Grimes 
12751762f674SKonstantin Belousov 	p = td->td_proc;
12761762f674SKonstantin Belousov 
12772c255e9dSRobert Watson 	/*
12782c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
12792c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
12802c255e9dSRobert Watson 	 *
12812c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
12822c255e9dSRobert Watson 	 * the vnode has been closed.
12832c255e9dSRobert Watson 	 */
12842c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
12851762f674SKonstantin Belousov 
12861762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
12872c255e9dSRobert Watson 
1288ea3fc8e4SJohn Baldwin 	/*
12891762f674SKonstantin Belousov 	 * If kiop is NULL, it has been cleared out from under this
12901762f674SKonstantin Belousov 	 * request, so just drop it.
1291ea3fc8e4SJohn Baldwin 	 */
12921762f674SKonstantin Belousov 	if (kiop == NULL) {
1293118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1294df8bae1dSRodney W. Grimes 		return;
12952c255e9dSRobert Watson 	}
12961762f674SKonstantin Belousov 
12971762f674SKonstantin Belousov 	vp = kiop->vp;
12981762f674SKonstantin Belousov 	cred = kiop->cr;
129902645b88SKonstantin Belousov 	lim = kiop->lim;
13001762f674SKonstantin Belousov 
13011762f674SKonstantin Belousov 	vrefact(vp);
13022c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1303118258f5SBjoern A. Zeeb 	crhold(cred);
1304118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
13052c255e9dSRobert Watson 
1306ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
130702abd400SPedro F. Giffuni 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1308a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
13098b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1310ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1311df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1312df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1313df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1314df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1315df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1316df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1317df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1318df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1319ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1320ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1321ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1322ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1323ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1324df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1325ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1326ea3fc8e4SJohn Baldwin 	}
1327ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1328d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1329d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1330ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1331ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1332ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1333b92584a6SJohn Baldwin 	}
13342c255e9dSRobert Watson 
1335f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1336cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
133702645b88SKonstantin Belousov 	td->td_ktr_io_lim = lim;
1338467a273cSRobert Watson #ifdef MAC
133930d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1340467a273cSRobert Watson 	if (error == 0)
1341467a273cSRobert Watson #endif
1342ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1343b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
1344f2a2857bSKirk McKusick 	vn_finished_write(mp);
1345118258f5SBjoern A. Zeeb 	crfree(cred);
13461762f674SKonstantin Belousov 	if (error == 0) {
1347704c9f00SJohn Baldwin 		vrele(vp);
1348df8bae1dSRodney W. Grimes 		return;
1349118258f5SBjoern A. Zeeb 	}
1350118258f5SBjoern A. Zeeb 
1351df8bae1dSRodney W. Grimes 	/*
1352a6144f71SKonstantin Belousov 	 * If error encountered, give up tracing on this vnode on this
1353a6144f71SKonstantin Belousov 	 * process.  Other processes might still be suitable for
1354a6144f71SKonstantin Belousov 	 * writes to this vnode.
1355df8bae1dSRodney W. Grimes 	 */
1356a6144f71SKonstantin Belousov 	log(LOG_NOTICE,
1357a6144f71SKonstantin Belousov 	    "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1358a6144f71SKonstantin Belousov 	    error, p->p_pid);
13591762f674SKonstantin Belousov 
1360ea3fc8e4SJohn Baldwin 	PROC_LOCK(p);
1361ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
13621762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
13631762f674SKonstantin Belousov 		kiop = ktr_freeproc(p);
1364ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1365ea3fc8e4SJohn Baldwin 	PROC_UNLOCK(p);
13661762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
1367ea3fc8e4SJohn Baldwin 	vrele(vp);
1368df8bae1dSRodney W. Grimes }
1369df8bae1dSRodney W. Grimes 
1370df8bae1dSRodney W. Grimes /*
1371df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1372df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1373df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1374df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1375df8bae1dSRodney W. Grimes  * so, only root may further change it.
1376df8bae1dSRodney W. Grimes  */
137787b6de2bSPoul-Henning Kamp static int
1378039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1379df8bae1dSRodney W. Grimes {
1380df8bae1dSRodney W. Grimes 
1381a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1382a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
138332f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
138475c13541SPoul-Henning Kamp 		return (0);
1385a0f75161SRobert Watson 
1386f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1387a0f75161SRobert Watson 		return (0);
1388a0f75161SRobert Watson 
1389df8bae1dSRodney W. Grimes 	return (1);
1390df8bae1dSRodney W. Grimes }
1391df8bae1dSRodney W. Grimes 
1392db6a20e2SGarrett Wollman #endif /* KTRACE */
1393