xref: /freebsd/sys/kern/kern_ktrace.c (revision 3080f82b8b644809d8731c4f5c72f1530c400eb3)
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 
115*3080f82bSMark Johnston static const 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
213c6d31b83SKonstantin Belousov ast_ktrace(struct thread *td, int tda __unused)
214c6d31b83SKonstantin Belousov {
215c6d31b83SKonstantin Belousov 	KTRUSERRET(td);
216c6d31b83SKonstantin Belousov }
217c6d31b83SKonstantin Belousov 
218c6d31b83SKonstantin Belousov static void
219ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
220df8bae1dSRodney W. Grimes {
221ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
222ea3fc8e4SJohn Baldwin 	int i;
223df8bae1dSRodney W. Grimes 
224ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
2252c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
226ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
227ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
2285c18bf9dSMark Johnston 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK |
2295c18bf9dSMark Johnston 		    M_ZERO);
230ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
231ea3fc8e4SJohn Baldwin 	}
2324a662c90SKonstantin Belousov 	ast_register(TDA_KTRACE, ASTR_ASTF_REQUIRED, 0, ast_ktrace);
233ea3fc8e4SJohn Baldwin }
234ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
235ea3fc8e4SJohn Baldwin 
236ea3fc8e4SJohn Baldwin static int
237ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
238ea3fc8e4SJohn Baldwin {
239ea3fc8e4SJohn Baldwin 	struct thread *td;
2408b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
241ea3fc8e4SJohn Baldwin 	int error;
242ea3fc8e4SJohn Baldwin 
243ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
244ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
245ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2468b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
247ea3fc8e4SJohn Baldwin 	}
248ea3fc8e4SJohn Baldwin 
2498b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
250ea3fc8e4SJohn Baldwin 	if (error)
251ea3fc8e4SJohn Baldwin 		return (error);
252ea3fc8e4SJohn Baldwin 	td = curthread;
2532c255e9dSRobert Watson 	ktrace_enter(td);
254ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
255b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2562c255e9dSRobert Watson 	ktrace_exit(td);
2578b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
258ea3fc8e4SJohn Baldwin 	if (error)
259ea3fc8e4SJohn Baldwin 		return (error);
260a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
261ea3fc8e4SJohn Baldwin 		return (ENOSPC);
262ea3fc8e4SJohn Baldwin 	return (0);
263ea3fc8e4SJohn Baldwin }
2647029da5cSPawel Biernacki SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool,
2657029da5cSPawel Biernacki     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &ktr_requestpool, 0,
2667029da5cSPawel Biernacki     sysctl_kern_ktrace_request_pool, "IU",
267a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
268ea3fc8e4SJohn Baldwin 
2698b149b51SJohn Baldwin static u_int
270b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
271ea3fc8e4SJohn Baldwin {
272b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
273ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
274a5896914SJoseph Koshy 	int bound;
275ea3fc8e4SJohn Baldwin 
276ea3fc8e4SJohn Baldwin 	print_message = 1;
277b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
278a5896914SJoseph Koshy 	if (bound == 0)
279a5896914SJoseph Koshy 		return (ktr_requestpool);
280b4c20e5eSDmitry Chagin 	if (bound < 0) {
281b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
282ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
283a5896914SJoseph Koshy 		while (bound++ < 0) {
284ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
285ea3fc8e4SJohn Baldwin 			if (req == NULL)
286b4c20e5eSDmitry Chagin 				break;
287ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
288ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
289ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
290ea3fc8e4SJohn Baldwin 		}
291b4c20e5eSDmitry Chagin 	} else {
292ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
293b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
294a5896914SJoseph Koshy 		while (bound-- > 0) {
295ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
2965c18bf9dSMark Johnston 			    M_WAITOK | M_ZERO);
297b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
298ea3fc8e4SJohn Baldwin 		}
299b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
300b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
301b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
302b4c20e5eSDmitry Chagin 	}
303b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
304ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
305ea3fc8e4SJohn Baldwin }
306ea3fc8e4SJohn Baldwin 
3075ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
3085ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
3095ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
3105ca4819dSJohn Baldwin 
311ea3fc8e4SJohn Baldwin static struct ktr_request *
31222ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
313ea3fc8e4SJohn Baldwin {
314ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
315ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
316ea3fc8e4SJohn Baldwin 	int pm;
317ea3fc8e4SJohn Baldwin 
318c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
319ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
320c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
321ea3fc8e4SJohn Baldwin 		return (NULL);
322ea3fc8e4SJohn Baldwin 	}
323ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
324ea3fc8e4SJohn Baldwin 	if (req != NULL) {
325ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
326ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
32775768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
32875768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
32975768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
33075768576SJohn Baldwin 		}
331c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
332fc90f3a2SDmitry Chagin 		nanotime(&req->ktr_header.ktr_time);
333fc90f3a2SDmitry Chagin 		req->ktr_header.ktr_type |= KTR_VERSIONED;
334ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3352bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
336fc90f3a2SDmitry Chagin 		req->ktr_header.ktr_cpu = PCPU_GET(cpuid);
337fc90f3a2SDmitry Chagin 		req->ktr_header.ktr_version = KTR_VERSION1;
3385ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3395ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
340d977a583SRobert Watson 		req->ktr_buffer = NULL;
341ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
342ea3fc8e4SJohn Baldwin 	} else {
34375768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
344ea3fc8e4SJohn Baldwin 		pm = print_message;
345ea3fc8e4SJohn Baldwin 		print_message = 0;
346ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
347ea3fc8e4SJohn Baldwin 		if (pm)
348ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
349ea3fc8e4SJohn Baldwin 	}
350ea3fc8e4SJohn Baldwin 	return (req);
351ea3fc8e4SJohn Baldwin }
352ea3fc8e4SJohn Baldwin 
3537705d4b2SDmitry Chagin static struct ktr_request *
3547705d4b2SDmitry Chagin ktr_getrequest(int type)
3557705d4b2SDmitry Chagin {
3567705d4b2SDmitry Chagin 	struct thread *td = curthread;
3577705d4b2SDmitry Chagin 	struct ktr_request *req;
3587705d4b2SDmitry Chagin 
3597705d4b2SDmitry Chagin 	ktrace_enter(td);
36022ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3617705d4b2SDmitry Chagin 	if (req == NULL)
3627705d4b2SDmitry Chagin 		ktrace_exit(td);
3637705d4b2SDmitry Chagin 
3647705d4b2SDmitry Chagin 	return (req);
3657705d4b2SDmitry Chagin }
3667705d4b2SDmitry Chagin 
3672c255e9dSRobert Watson /*
3682c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3692c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3702c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3712c255e9dSRobert Watson  * later.
3722c255e9dSRobert Watson  */
373ea3fc8e4SJohn Baldwin static void
3742c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
375ea3fc8e4SJohn Baldwin {
376ea3fc8e4SJohn Baldwin 
377ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3782c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
379ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
380c6d31b83SKonstantin Belousov 	ast_sched(td, TDA_KTRACE);
3812c255e9dSRobert Watson }
3822c255e9dSRobert Watson 
3832c255e9dSRobert Watson /*
3842c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3852c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3862c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3872c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
388a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3892c255e9dSRobert Watson  */
3902c255e9dSRobert Watson static void
3912c255e9dSRobert Watson ktr_drain(struct thread *td)
3922c255e9dSRobert Watson {
3932c255e9dSRobert Watson 	struct ktr_request *queued_req;
3942c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3952c255e9dSRobert Watson 
3962c255e9dSRobert Watson 	ktrace_assert(td);
3972c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3982c255e9dSRobert Watson 
3992b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
4002c255e9dSRobert Watson 
4012c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
4022c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
4032c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
4042c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
4052c255e9dSRobert Watson 
4062c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
4072c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
4082c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
4092c255e9dSRobert Watson 			ktr_freerequest(queued_req);
4102c255e9dSRobert Watson 		}
4112c255e9dSRobert Watson 	}
4122c255e9dSRobert Watson }
4132c255e9dSRobert Watson 
4142c255e9dSRobert Watson /*
4152c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
4162c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
4172c255e9dSRobert Watson  * been cached in the thread.
4182c255e9dSRobert Watson  */
4192c255e9dSRobert Watson static void
42022ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
4212c255e9dSRobert Watson {
4222c255e9dSRobert Watson 
4232c255e9dSRobert Watson 	ktrace_assert(td);
4242c255e9dSRobert Watson 
4252c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
4262c255e9dSRobert Watson 	ktr_drain(td);
4272c255e9dSRobert Watson 	ktr_writerequest(td, req);
4282c255e9dSRobert Watson 	ktr_freerequest(req);
4292c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
4302c255e9dSRobert Watson 	ktrace_exit(td);
431ea3fc8e4SJohn Baldwin }
432ea3fc8e4SJohn Baldwin 
433ea3fc8e4SJohn Baldwin static void
434ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
435ea3fc8e4SJohn Baldwin {
436ea3fc8e4SJohn Baldwin 
437d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
438d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
439d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
440d680caabSJohn Baldwin }
441d680caabSJohn Baldwin 
442d680caabSJohn Baldwin static void
443d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
444d680caabSJohn Baldwin {
445d680caabSJohn Baldwin 
446d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
447d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
448d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
449ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
450d680caabSJohn Baldwin }
451d680caabSJohn Baldwin 
4521762f674SKonstantin Belousov static void
4531762f674SKonstantin Belousov ktr_io_params_ref(struct ktr_io_params *kiop)
4541762f674SKonstantin Belousov {
4551762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4561762f674SKonstantin Belousov 	kiop->refs++;
4571762f674SKonstantin Belousov }
4581762f674SKonstantin Belousov 
4591762f674SKonstantin Belousov static struct ktr_io_params *
4601762f674SKonstantin Belousov ktr_io_params_rele(struct ktr_io_params *kiop)
4611762f674SKonstantin Belousov {
4621762f674SKonstantin Belousov 	mtx_assert(&ktrace_mtx, MA_OWNED);
4631762f674SKonstantin Belousov 	if (kiop == NULL)
4641762f674SKonstantin Belousov 		return (NULL);
4651762f674SKonstantin Belousov 	KASSERT(kiop->refs > 0, ("kiop ref == 0 %p", kiop));
4661762f674SKonstantin Belousov 	return (--(kiop->refs) == 0 ? kiop : NULL);
4671762f674SKonstantin Belousov }
4681762f674SKonstantin Belousov 
4691762f674SKonstantin Belousov void
4701762f674SKonstantin Belousov ktr_io_params_free(struct ktr_io_params *kiop)
4711762f674SKonstantin Belousov {
4721762f674SKonstantin Belousov 	if (kiop == NULL)
4731762f674SKonstantin Belousov 		return;
4741762f674SKonstantin Belousov 
4751762f674SKonstantin Belousov 	MPASS(kiop->refs == 0);
4761762f674SKonstantin Belousov 	vn_close(kiop->vp, FWRITE, kiop->cr, curthread);
4771762f674SKonstantin Belousov 	crfree(kiop->cr);
4781762f674SKonstantin Belousov 	free(kiop, M_KTRACE);
4791762f674SKonstantin Belousov }
4801762f674SKonstantin Belousov 
4811762f674SKonstantin Belousov static struct ktr_io_params *
4821762f674SKonstantin Belousov ktr_io_params_alloc(struct thread *td, struct vnode *vp)
4831762f674SKonstantin Belousov {
4841762f674SKonstantin Belousov 	struct ktr_io_params *res;
4851762f674SKonstantin Belousov 
4861762f674SKonstantin Belousov 	res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
4871762f674SKonstantin Belousov 	res->vp = vp;
4881762f674SKonstantin Belousov 	res->cr = crhold(td->td_ucred);
48902645b88SKonstantin Belousov 	res->lim = lim_cur(td, RLIMIT_FSIZE);
4901762f674SKonstantin Belousov 	res->refs = 1;
4911762f674SKonstantin Belousov 	return (res);
4921762f674SKonstantin Belousov }
4931762f674SKonstantin Belousov 
494d680caabSJohn Baldwin /*
495d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
496d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
497d680caabSJohn Baldwin  * vnode and credentials.
498d680caabSJohn Baldwin  */
4991762f674SKonstantin Belousov static struct ktr_io_params *
5001762f674SKonstantin Belousov ktr_freeproc(struct proc *p)
501d680caabSJohn Baldwin {
5021762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
503d680caabSJohn Baldwin 	struct ktr_request *req;
504d680caabSJohn Baldwin 
505d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
506d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
5071762f674SKonstantin Belousov 	kiop = ktr_io_params_rele(p->p_ktrioparms);
5081762f674SKonstantin Belousov 	p->p_ktrioparms = NULL;
509d680caabSJohn Baldwin 	p->p_traceflag = 0;
510d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
511d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
512d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
513d680caabSJohn Baldwin 	}
5141762f674SKonstantin Belousov 	return (kiop);
5151762f674SKonstantin Belousov }
5161762f674SKonstantin Belousov 
5171762f674SKonstantin Belousov struct vnode *
5181762f674SKonstantin Belousov ktr_get_tracevp(struct proc *p, bool ref)
5191762f674SKonstantin Belousov {
5201762f674SKonstantin Belousov 	struct vnode *vp;
5211762f674SKonstantin Belousov 
5221762f674SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
5231762f674SKonstantin Belousov 
5241762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL) {
5251762f674SKonstantin Belousov 		vp = p->p_ktrioparms->vp;
5261762f674SKonstantin Belousov 		if (ref)
5271762f674SKonstantin Belousov 			vrefact(vp);
5281762f674SKonstantin Belousov 	} else {
5291762f674SKonstantin Belousov 		vp = NULL;
5301762f674SKonstantin Belousov 	}
5311762f674SKonstantin Belousov 	return (vp);
532ea3fc8e4SJohn Baldwin }
533ea3fc8e4SJohn Baldwin 
53426f9a767SRodney W. Grimes void
535b1ad6a90SBrooks Davis ktrsyscall(int code, int narg, syscallarg_t args[])
536df8bae1dSRodney W. Grimes {
537ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
538df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
539ea3fc8e4SJohn Baldwin 	size_t buflen;
5404b3aac3dSJohn Baldwin 	char *buf = NULL;
541df8bae1dSRodney W. Grimes 
542ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
543ad738f37SMatt Macy 		return;
544ad738f37SMatt Macy 
5454b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
5464b3aac3dSJohn Baldwin 	if (buflen > 0) {
547a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
5484b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
5494b3aac3dSJohn Baldwin 	}
550ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
55150c22331SPoul-Henning Kamp 	if (req == NULL) {
55250c22331SPoul-Henning Kamp 		if (buf != NULL)
55350c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
554ea3fc8e4SJohn Baldwin 		return;
55550c22331SPoul-Henning Kamp 	}
556ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
557df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
558df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
559ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
560ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
561d977a583SRobert Watson 		req->ktr_buffer = buf;
562ea3fc8e4SJohn Baldwin 	}
5632c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
564df8bae1dSRodney W. Grimes }
565df8bae1dSRodney W. Grimes 
56626f9a767SRodney W. Grimes void
567039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
568df8bae1dSRodney W. Grimes {
569ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
570ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
571df8bae1dSRodney W. Grimes 
572ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
573ad738f37SMatt Macy 		return;
574ad738f37SMatt Macy 
575ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
576ea3fc8e4SJohn Baldwin 	if (req == NULL)
577ea3fc8e4SJohn Baldwin 		return;
578ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
579ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
580ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
5815a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
5822c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
5832c255e9dSRobert Watson }
5842c255e9dSRobert Watson 
5852c255e9dSRobert Watson /*
586d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
587d680caabSJohn Baldwin  *
588d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
589d680caabSJohn Baldwin  */
5901762f674SKonstantin Belousov struct ktr_io_params *
5911762f674SKonstantin Belousov ktrprocexec(struct proc *p)
592d680caabSJohn Baldwin {
5931762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
594d680caabSJohn Baldwin 
595d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
5961762f674SKonstantin Belousov 
5971762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
5981762f674SKonstantin Belousov 	if (kiop == NULL || priv_check_cred(kiop->cr, PRIV_DEBUG_DIFFCRED))
5991762f674SKonstantin Belousov 		return (NULL);
6001762f674SKonstantin Belousov 
601d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
6021762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
603d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6041762f674SKonstantin Belousov 	return (kiop);
605d680caabSJohn Baldwin }
606d680caabSJohn Baldwin 
607d680caabSJohn Baldwin /*
608d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
609d680caabSJohn Baldwin  * and disable tracing.
6102c255e9dSRobert Watson  */
6112c255e9dSRobert Watson void
6122c255e9dSRobert Watson ktrprocexit(struct thread *td)
6132c255e9dSRobert Watson {
6147705d4b2SDmitry Chagin 	struct ktr_request *req;
615d680caabSJohn Baldwin 	struct proc *p;
6161762f674SKonstantin Belousov 	struct ktr_io_params *kiop;
617d680caabSJohn Baldwin 
618d680caabSJohn Baldwin 	p = td->td_proc;
619d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
620d680caabSJohn Baldwin 		return;
6212c255e9dSRobert Watson 
6222c255e9dSRobert Watson 	ktrace_enter(td);
62322ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
62422ec0406SDmitry Chagin 	if (req != NULL)
62522ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
6262c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6272c255e9dSRobert Watson 	ktr_drain(td);
6282c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
629d680caabSJohn Baldwin 	PROC_LOCK(p);
630d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
6311762f674SKonstantin Belousov 	kiop = ktr_freeproc(p);
632d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
633d680caabSJohn Baldwin 	PROC_UNLOCK(p);
6341762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
6352c255e9dSRobert Watson 	ktrace_exit(td);
6362c255e9dSRobert Watson }
6372c255e9dSRobert Watson 
6387705d4b2SDmitry Chagin static void
63922ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
6407705d4b2SDmitry Chagin {
6417705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
6427705d4b2SDmitry Chagin 	struct ktr_request *req;
643de60a5f3SDmitry Chagin 	struct thread *td2;
6447705d4b2SDmitry Chagin 
6457705d4b2SDmitry Chagin 	ktrace_assert(td);
6467705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
64722ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
6487705d4b2SDmitry Chagin 	if (req == NULL)
6497705d4b2SDmitry Chagin 		return;
6507705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
6517705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
65222ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
6537705d4b2SDmitry Chagin }
6547705d4b2SDmitry Chagin 
6557705d4b2SDmitry Chagin void
6567705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
6577705d4b2SDmitry Chagin {
6587705d4b2SDmitry Chagin 	struct thread *td = curthread;
6597705d4b2SDmitry Chagin 
6607705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
6617705d4b2SDmitry Chagin 		return;
6627705d4b2SDmitry Chagin 
6637705d4b2SDmitry Chagin 	ktrace_enter(td);
66422ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
6657705d4b2SDmitry Chagin 	ktrace_exit(td);
6667705d4b2SDmitry Chagin }
6677705d4b2SDmitry Chagin 
6682c255e9dSRobert Watson /*
669d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
670d680caabSJohn Baldwin  */
671d680caabSJohn Baldwin void
672d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
673d680caabSJohn Baldwin {
674d680caabSJohn Baldwin 
6751762f674SKonstantin Belousov 	MPASS(p2->p_ktrioparms == NULL);
6767c34b35bSMateusz Guzik 	MPASS(p2->p_traceflag == 0);
6777c34b35bSMateusz Guzik 
6787c34b35bSMateusz Guzik 	if (p1->p_traceflag == 0)
6797c34b35bSMateusz Guzik 		return;
6807c34b35bSMateusz Guzik 
6817705d4b2SDmitry Chagin 	PROC_LOCK(p1);
682d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
683d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
684d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
6851762f674SKonstantin Belousov 		if ((p2->p_ktrioparms = p1->p_ktrioparms) != NULL)
6861762f674SKonstantin Belousov 			p1->p_ktrioparms->refs++;
687d680caabSJohn Baldwin 	}
688d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
6897705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
6907705d4b2SDmitry Chagin 
6917705d4b2SDmitry Chagin 	ktrprocctor(p2);
692d680caabSJohn Baldwin }
693d680caabSJohn Baldwin 
694d680caabSJohn Baldwin /*
6952c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
6962c255e9dSRobert Watson  * system call.
6972c255e9dSRobert Watson  */
6982c255e9dSRobert Watson void
6992c255e9dSRobert Watson ktruserret(struct thread *td)
7002c255e9dSRobert Watson {
7012c255e9dSRobert Watson 
7022c255e9dSRobert Watson 	ktrace_enter(td);
7032c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
7042c255e9dSRobert Watson 	ktr_drain(td);
7052c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
7062c255e9dSRobert Watson 	ktrace_exit(td);
707df8bae1dSRodney W. Grimes }
708df8bae1dSRodney W. Grimes 
70926f9a767SRodney W. Grimes void
710e4b16f2fSMark Johnston ktrnamei(const char *path)
711df8bae1dSRodney W. Grimes {
712ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
713ea3fc8e4SJohn Baldwin 	int namelen;
7144b3aac3dSJohn Baldwin 	char *buf = NULL;
715df8bae1dSRodney W. Grimes 
7164b3aac3dSJohn Baldwin 	namelen = strlen(path);
7174b3aac3dSJohn Baldwin 	if (namelen > 0) {
718a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
7194b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
7204b3aac3dSJohn Baldwin 	}
721ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
72250c22331SPoul-Henning Kamp 	if (req == NULL) {
72350c22331SPoul-Henning Kamp 		if (buf != NULL)
72450c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
725ea3fc8e4SJohn Baldwin 		return;
72650c22331SPoul-Henning Kamp 	}
727ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
728ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
729d977a583SRobert Watson 		req->ktr_buffer = buf;
730ea3fc8e4SJohn Baldwin 	}
7312c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
732df8bae1dSRodney W. Grimes }
733df8bae1dSRodney W. Grimes 
73426f9a767SRodney W. Grimes void
735039644ecSEd Maste ktrsysctl(int *name, u_int namelen)
736a56be37eSJohn Baldwin {
737a56be37eSJohn Baldwin 	struct ktr_request *req;
738a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
739a56be37eSJohn Baldwin 	char *mibname;
740a56be37eSJohn Baldwin 	size_t mibnamelen;
741a56be37eSJohn Baldwin 	int error;
742a56be37eSJohn Baldwin 
743a56be37eSJohn Baldwin 	/* Lookup name of mib. */
744a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
745a56be37eSJohn Baldwin 	mib[0] = 0;
746a56be37eSJohn Baldwin 	mib[1] = 1;
747a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
748a56be37eSJohn Baldwin 	mibnamelen = 128;
749a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
750a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
751a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
752a56be37eSJohn Baldwin 	if (error) {
753a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
754a56be37eSJohn Baldwin 		return;
755a56be37eSJohn Baldwin 	}
756a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
757a56be37eSJohn Baldwin 	if (req == NULL) {
758a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
759a56be37eSJohn Baldwin 		return;
760a56be37eSJohn Baldwin 	}
761a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
762a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
763a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
764a56be37eSJohn Baldwin }
765a56be37eSJohn Baldwin 
766a56be37eSJohn Baldwin void
767039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
768df8bae1dSRodney W. Grimes {
769ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
770ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
771b92584a6SJohn Baldwin 	int datalen;
772b92584a6SJohn Baldwin 	char *buf;
773df8bae1dSRodney W. Grimes 
774552afd9cSPoul-Henning Kamp 	if (error) {
775552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
776df8bae1dSRodney W. Grimes 		return;
777552afd9cSPoul-Henning Kamp 	}
778b92584a6SJohn Baldwin 	uio->uio_offset = 0;
779b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
780526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
781a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
782552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
783552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
784552afd9cSPoul-Henning Kamp 	if (error) {
785b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
786ea3fc8e4SJohn Baldwin 		return;
787b92584a6SJohn Baldwin 	}
788b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
789b92584a6SJohn Baldwin 	if (req == NULL) {
790b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
791b92584a6SJohn Baldwin 		return;
792b92584a6SJohn Baldwin 	}
793ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
794ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
795ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
796b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
797d977a583SRobert Watson 	req->ktr_buffer = buf;
7982c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
799df8bae1dSRodney W. Grimes }
800df8bae1dSRodney W. Grimes 
80126f9a767SRodney W. Grimes void
802039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
803df8bae1dSRodney W. Grimes {
80422ec0406SDmitry Chagin 	struct thread *td = curthread;
805ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
806ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
807df8bae1dSRodney W. Grimes 
808ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
809ea3fc8e4SJohn Baldwin 	if (req == NULL)
810ea3fc8e4SJohn Baldwin 		return;
811ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
812ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
813ea3fc8e4SJohn Baldwin 	kp->action = action;
814ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
815ea3fc8e4SJohn Baldwin 	kp->code = code;
81622ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
81722ec0406SDmitry Chagin 	ktrace_exit(td);
818df8bae1dSRodney W. Grimes }
819df8bae1dSRodney W. Grimes 
82026f9a767SRodney W. Grimes void
821039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg)
822df8bae1dSRodney W. Grimes {
82322ec0406SDmitry Chagin 	struct thread *td = curthread;
824ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
825ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
826df8bae1dSRodney W. Grimes 
827ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
828ad738f37SMatt Macy 		return;
829ad738f37SMatt Macy 
830ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
831ea3fc8e4SJohn Baldwin 	if (req == NULL)
832ea3fc8e4SJohn Baldwin 		return;
833ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
834ea3fc8e4SJohn Baldwin 	kc->out = out;
835ea3fc8e4SJohn Baldwin 	kc->user = user;
83688bf5036SJohn Baldwin 	if (wmesg != NULL)
83788bf5036SJohn Baldwin 		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
83888bf5036SJohn Baldwin 	else
83988bf5036SJohn Baldwin 		bzero(kc->wmesg, sizeof(kc->wmesg));
84022ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
84122ec0406SDmitry Chagin 	ktrace_exit(td);
842df8bae1dSRodney W. Grimes }
84360e15db9SDag-Erling Smørgrav 
84460e15db9SDag-Erling Smørgrav void
845ffb66079SJohn Baldwin ktrstruct(const char *name, const void *data, size_t datalen)
84660e15db9SDag-Erling Smørgrav {
84760e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
8484dd3a21fSMateusz Guzik 	char *buf;
8494dd3a21fSMateusz Guzik 	size_t buflen, namelen;
85060e15db9SDag-Erling Smørgrav 
851ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
852ad738f37SMatt Macy 		return;
853ad738f37SMatt Macy 
8544dd3a21fSMateusz Guzik 	if (data == NULL)
85560e15db9SDag-Erling Smørgrav 		datalen = 0;
8564dd3a21fSMateusz Guzik 	namelen = strlen(name) + 1;
8574dd3a21fSMateusz Guzik 	buflen = namelen + datalen;
85860e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
859a3052d6eSJohn Baldwin 	strcpy(buf, name);
8604dd3a21fSMateusz Guzik 	bcopy(data, buf + namelen, datalen);
86160e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
86260e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
86360e15db9SDag-Erling Smørgrav 		return;
86460e15db9SDag-Erling Smørgrav 	}
86560e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
86660e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
86760e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
86860e15db9SDag-Erling Smørgrav }
869c601ad8eSDag-Erling Smørgrav 
870c601ad8eSDag-Erling Smørgrav void
8710a1427c5SMateusz Guzik ktrstruct_error(const char *name, const void *data, size_t datalen, int error)
8720a1427c5SMateusz Guzik {
8730a1427c5SMateusz Guzik 
8740a1427c5SMateusz Guzik 	if (error == 0)
8750a1427c5SMateusz Guzik 		ktrstruct(name, data, datalen);
8760a1427c5SMateusz Guzik }
8770a1427c5SMateusz Guzik 
8780a1427c5SMateusz Guzik void
879ffb66079SJohn Baldwin ktrstructarray(const char *name, enum uio_seg seg, const void *data,
880ffb66079SJohn Baldwin     int num_items, size_t struct_size)
881ffb66079SJohn Baldwin {
882ffb66079SJohn Baldwin 	struct ktr_request *req;
883ffb66079SJohn Baldwin 	struct ktr_struct_array *ksa;
884ffb66079SJohn Baldwin 	char *buf;
885ffb66079SJohn Baldwin 	size_t buflen, datalen, namelen;
886ffb66079SJohn Baldwin 	int max_items;
887ffb66079SJohn Baldwin 
888ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
889ad738f37SMatt Macy 		return;
890f8851007SMark Johnston 	if (num_items < 0)
891f8851007SMark Johnston 		return;
892ad738f37SMatt Macy 
893ffb66079SJohn Baldwin 	/* Trim array length to genio size. */
894ffb66079SJohn Baldwin 	max_items = ktr_geniosize / struct_size;
895ffb66079SJohn Baldwin 	if (num_items > max_items) {
896ffb66079SJohn Baldwin 		if (max_items == 0)
897ffb66079SJohn Baldwin 			num_items = 1;
898ffb66079SJohn Baldwin 		else
899ffb66079SJohn Baldwin 			num_items = max_items;
900ffb66079SJohn Baldwin 	}
901ffb66079SJohn Baldwin 	datalen = num_items * struct_size;
902ffb66079SJohn Baldwin 
903ffb66079SJohn Baldwin 	if (data == NULL)
904ffb66079SJohn Baldwin 		datalen = 0;
905ffb66079SJohn Baldwin 
906ffb66079SJohn Baldwin 	namelen = strlen(name) + 1;
907ffb66079SJohn Baldwin 	buflen = namelen + datalen;
908ffb66079SJohn Baldwin 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
909ffb66079SJohn Baldwin 	strcpy(buf, name);
910ffb66079SJohn Baldwin 	if (seg == UIO_SYSSPACE)
911ffb66079SJohn Baldwin 		bcopy(data, buf + namelen, datalen);
912ffb66079SJohn Baldwin 	else {
913ffb66079SJohn Baldwin 		if (copyin(data, buf + namelen, datalen) != 0) {
914ffb66079SJohn Baldwin 			free(buf, M_KTRACE);
915ffb66079SJohn Baldwin 			return;
916ffb66079SJohn Baldwin 		}
917ffb66079SJohn Baldwin 	}
918ffb66079SJohn Baldwin 	if ((req = ktr_getrequest(KTR_STRUCT_ARRAY)) == NULL) {
919ffb66079SJohn Baldwin 		free(buf, M_KTRACE);
920ffb66079SJohn Baldwin 		return;
921ffb66079SJohn Baldwin 	}
922ffb66079SJohn Baldwin 	ksa = &req->ktr_data.ktr_struct_array;
923ffb66079SJohn Baldwin 	ksa->struct_size = struct_size;
924ffb66079SJohn Baldwin 	req->ktr_buffer = buf;
925ffb66079SJohn Baldwin 	req->ktr_header.ktr_len = buflen;
926ffb66079SJohn Baldwin 	ktr_submitrequest(curthread, req);
927ffb66079SJohn Baldwin }
928ffb66079SJohn Baldwin 
929ffb66079SJohn Baldwin void
930039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
931039644ecSEd Maste     const cap_rights_t *held)
932c601ad8eSDag-Erling Smørgrav {
933c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
934c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
935c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
936c601ad8eSDag-Erling Smørgrav 
937ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
938ad738f37SMatt Macy 		return;
939ad738f37SMatt Macy 
940c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
941c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
942c601ad8eSDag-Erling Smørgrav 		return;
943c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
944e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
9453fded357SPawel Jakub Dawidek 	if (needed != NULL)
9467008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
9473fded357SPawel Jakub Dawidek 	else
9483fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
9493fded357SPawel Jakub Dawidek 	if (held != NULL)
9507008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
9513fded357SPawel Jakub Dawidek 	else
9523fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
953c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
954c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
955c601ad8eSDag-Erling Smørgrav }
95635818d2eSJohn Baldwin 
95735818d2eSJohn Baldwin void
958039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
95935818d2eSJohn Baldwin {
96035818d2eSJohn Baldwin 	struct thread *td = curthread;
96135818d2eSJohn Baldwin 	struct ktr_request *req;
96235818d2eSJohn Baldwin 	struct ktr_fault *kf;
96335818d2eSJohn Baldwin 
964ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
965ad738f37SMatt Macy 		return;
966ad738f37SMatt Macy 
96735818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
96835818d2eSJohn Baldwin 	if (req == NULL)
96935818d2eSJohn Baldwin 		return;
97035818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
97135818d2eSJohn Baldwin 	kf->vaddr = vaddr;
97235818d2eSJohn Baldwin 	kf->type = type;
97335818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
97435818d2eSJohn Baldwin 	ktrace_exit(td);
97535818d2eSJohn Baldwin }
97635818d2eSJohn Baldwin 
97735818d2eSJohn Baldwin void
978039644ecSEd Maste ktrfaultend(int result)
97935818d2eSJohn Baldwin {
98035818d2eSJohn Baldwin 	struct thread *td = curthread;
98135818d2eSJohn Baldwin 	struct ktr_request *req;
98235818d2eSJohn Baldwin 	struct ktr_faultend *kf;
98335818d2eSJohn Baldwin 
984ad738f37SMatt Macy 	if (__predict_false(curthread->td_pflags & TDP_INKTRACE))
985ad738f37SMatt Macy 		return;
986ad738f37SMatt Macy 
98735818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
98835818d2eSJohn Baldwin 	if (req == NULL)
98935818d2eSJohn Baldwin 		return;
99035818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
99135818d2eSJohn Baldwin 	kf->result = result;
99235818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
99335818d2eSJohn Baldwin 	ktrace_exit(td);
99435818d2eSJohn Baldwin }
99564cc6a13SJohn Baldwin #endif /* KTRACE */
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes /* Interface and common routines */
998df8bae1dSRodney W. Grimes 
999d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1000df8bae1dSRodney W. Grimes struct ktrace_args {
1001df8bae1dSRodney W. Grimes 	char	*fname;
1002df8bae1dSRodney W. Grimes 	int	ops;
1003df8bae1dSRodney W. Grimes 	int	facs;
1004df8bae1dSRodney W. Grimes 	int	pid;
1005df8bae1dSRodney W. Grimes };
1006d2d3e875SBruce Evans #endif
1007df8bae1dSRodney W. Grimes /* ARGSUSED */
100826f9a767SRodney W. Grimes int
1009039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
1010df8bae1dSRodney W. Grimes {
1011db6a20e2SGarrett Wollman #ifdef KTRACE
1012039644ecSEd Maste 	struct vnode *vp = NULL;
1013039644ecSEd Maste 	struct proc *p;
1014df8bae1dSRodney W. Grimes 	struct pgrp *pg;
1015df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
1016df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
1017df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
1018f3851b23SMark Johnston 	int ret = 0;
10195050aa86SKonstantin Belousov 	int flags, error = 0;
1020df8bae1dSRodney W. Grimes 	struct nameidata nd;
10211762f674SKonstantin Belousov 	struct ktr_io_params *kiop, *old_kiop;
1022df8bae1dSRodney W. Grimes 
102364cc6a13SJohn Baldwin 	/*
102464cc6a13SJohn Baldwin 	 * Need something to (un)trace.
102564cc6a13SJohn Baldwin 	 */
102664cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
102764cc6a13SJohn Baldwin 		return (EINVAL);
102864cc6a13SJohn Baldwin 
10291762f674SKonstantin Belousov 	kiop = NULL;
1030df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
1031df8bae1dSRodney W. Grimes 		/*
1032df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
1033df8bae1dSRodney W. Grimes 		 */
10347e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname);
1035e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
10369e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
1037e4b16f2fSMark Johnston 		if (error)
1038df8bae1dSRodney W. Grimes 			return (error);
1039bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
1040df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
1041b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
1042df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
1043a854ed98SJohn Baldwin 			(void)vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
1044df8bae1dSRodney W. Grimes 			return (EACCES);
1045df8bae1dSRodney W. Grimes 		}
10461762f674SKonstantin Belousov 		kiop = ktr_io_params_alloc(td, vp);
1047df8bae1dSRodney W. Grimes 	}
1048e4b16f2fSMark Johnston 
1049df8bae1dSRodney W. Grimes 	/*
105079deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
1051df8bae1dSRodney W. Grimes 	 */
1052e4b16f2fSMark Johnston 	ktrace_enter(td);
1053df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
10541762f674SKonstantin Belousov restart:
10551005a129SJohn Baldwin 		sx_slock(&allproc_lock);
10564f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
10571762f674SKonstantin Belousov 			old_kiop = NULL;
1058a7ff7443SJohn Baldwin 			PROC_LOCK(p);
10591762f674SKonstantin Belousov 			if (p->p_ktrioparms != NULL &&
10601762f674SKonstantin Belousov 			    p->p_ktrioparms->vp == vp) {
1061ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
1062ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
10631762f674SKonstantin Belousov 					old_kiop = ktr_freeproc(p);
1064ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
106551fd6380SMike Pritchard 				} else
1066df8bae1dSRodney W. Grimes 					error = EPERM;
1067df8bae1dSRodney W. Grimes 			}
1068a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
10691762f674SKonstantin Belousov 			if (old_kiop != NULL) {
10701762f674SKonstantin Belousov 				sx_sunlock(&allproc_lock);
10711762f674SKonstantin Belousov 				ktr_io_params_free(old_kiop);
10721762f674SKonstantin Belousov 				goto restart;
10731762f674SKonstantin Belousov 			}
107479deba82SMatthew Dillon 		}
10751005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
1076df8bae1dSRodney W. Grimes 		goto done;
1077df8bae1dSRodney W. Grimes 	}
1078df8bae1dSRodney W. Grimes 	/*
1079df8bae1dSRodney W. Grimes 	 * do it
1080df8bae1dSRodney W. Grimes 	 */
108164cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
1082df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
1083df8bae1dSRodney W. Grimes 		/*
1084df8bae1dSRodney W. Grimes 		 * by process group
1085df8bae1dSRodney W. Grimes 		 */
1086df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
1087df8bae1dSRodney W. Grimes 		if (pg == NULL) {
1088ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
1089df8bae1dSRodney W. Grimes 			error = ESRCH;
1090df8bae1dSRodney W. Grimes 			goto done;
1091df8bae1dSRodney W. Grimes 		}
1092f3851b23SMark Johnston 
1093f591779bSSeigo Tanimura 		/*
1094f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
1095ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
1096f591779bSSeigo Tanimura 		 */
1097f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
1098f3851b23SMark Johnston 		if (LIST_EMPTY(&pg->pg_members)) {
1099f3851b23SMark Johnston 			sx_sunlock(&proctree_lock);
1100f3851b23SMark Johnston 			error = ESRCH;
1101f3851b23SMark Johnston 			goto done;
1102f3851b23SMark Johnston 		}
1103400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1104400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
1105df8bae1dSRodney W. Grimes 			if (descend)
11061762f674SKonstantin Belousov 				ret |= ktrsetchildren(td, p, ops, facs, kiop);
1107df8bae1dSRodney W. Grimes 			else
11081762f674SKonstantin Belousov 				ret |= ktrops(td, p, ops, facs, kiop);
1109400a74bfSPawel Jakub Dawidek 		}
1110df8bae1dSRodney W. Grimes 	} else {
1111df8bae1dSRodney W. Grimes 		/*
1112df8bae1dSRodney W. Grimes 		 * by pid
1113df8bae1dSRodney W. Grimes 		 */
1114df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
1115f3851b23SMark Johnston 		if (p == NULL) {
1116df8bae1dSRodney W. Grimes 			error = ESRCH;
1117b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
11184eb7c9f6SPawel Jakub Dawidek 			goto done;
1119b0d9aeddSPawel Jakub Dawidek 		}
1120df8bae1dSRodney W. Grimes 		if (descend)
11211762f674SKonstantin Belousov 			ret |= ktrsetchildren(td, p, ops, facs, kiop);
1122df8bae1dSRodney W. Grimes 		else
11231762f674SKonstantin Belousov 			ret |= ktrops(td, p, ops, facs, kiop);
1124df8bae1dSRodney W. Grimes 	}
112564cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
1126df8bae1dSRodney W. Grimes 	if (!ret)
1127df8bae1dSRodney W. Grimes 		error = EPERM;
1128df8bae1dSRodney W. Grimes done:
11291762f674SKonstantin Belousov 	if (kiop != NULL) {
11301762f674SKonstantin Belousov 		mtx_lock(&ktrace_mtx);
11311762f674SKonstantin Belousov 		kiop = ktr_io_params_rele(kiop);
11321762f674SKonstantin Belousov 		mtx_unlock(&ktrace_mtx);
11331762f674SKonstantin Belousov 		ktr_io_params_free(kiop);
11341762f674SKonstantin Belousov 	}
11352c255e9dSRobert Watson 	ktrace_exit(td);
1136df8bae1dSRodney W. Grimes 	return (error);
113764cc6a13SJohn Baldwin #else /* !KTRACE */
113864cc6a13SJohn Baldwin 	return (ENOSYS);
113964cc6a13SJohn Baldwin #endif /* KTRACE */
1140df8bae1dSRodney W. Grimes }
1141df8bae1dSRodney W. Grimes 
1142e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
1143e6c4b9baSPoul-Henning Kamp int
1144039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
1145e6c4b9baSPoul-Henning Kamp {
1146b40ce416SJulian Elischer 
1147e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
1148ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
11497f05b035SAlfred Perlstein 	void *cp;
1150c9e7d28eSJohn Baldwin 	int error;
1151e6c4b9baSPoul-Henning Kamp 
1152c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
1153c9e7d28eSJohn Baldwin 		return (0);
1154bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
11550bad156aSAlfred Perlstein 		return (EINVAL);
1156a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
1157c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
115850c22331SPoul-Henning Kamp 	if (error) {
115950c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1160c9e7d28eSJohn Baldwin 		return (error);
116150c22331SPoul-Henning Kamp 	}
1162ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
116350c22331SPoul-Henning Kamp 	if (req == NULL) {
116450c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1165b10221ffSJoseph Koshy 		return (ENOMEM);
116650c22331SPoul-Henning Kamp 	}
1167d977a583SRobert Watson 	req->ktr_buffer = cp;
1168ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
11692c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1170e6c4b9baSPoul-Henning Kamp 	return (0);
117164cc6a13SJohn Baldwin #else /* !KTRACE */
1172e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
117364cc6a13SJohn Baldwin #endif /* KTRACE */
1174e6c4b9baSPoul-Henning Kamp }
1175e6c4b9baSPoul-Henning Kamp 
1176db6a20e2SGarrett Wollman #ifdef KTRACE
117787b6de2bSPoul-Henning Kamp static int
11781762f674SKonstantin Belousov ktrops(struct thread *td, struct proc *p, int ops, int facs,
11791762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1180df8bae1dSRodney W. Grimes {
11811762f674SKonstantin Belousov 	struct ktr_io_params *old_kiop;
1182df8bae1dSRodney W. Grimes 
1183fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1184a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1185a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1186df8bae1dSRodney W. Grimes 		return (0);
1187a7ff7443SJohn Baldwin 	}
1188283e60fbSMark Johnston 	if ((ops == KTROP_SET && p->p_state == PRS_NEW) ||
1189283e60fbSMark Johnston 	    p_cansee(td, p) != 0) {
1190f3851b23SMark Johnston 		/*
1191f3851b23SMark Johnston 		 * Disallow setting trace points if the process is being born.
1192f3851b23SMark Johnston 		 * This avoids races with trace point inheritance in
1193f3851b23SMark Johnston 		 * ktrprocfork().
1194f3851b23SMark Johnston 		 */
1195f3851b23SMark Johnston 		PROC_UNLOCK(p);
1196f3851b23SMark Johnston 		return (0);
1197f3851b23SMark Johnston 	}
1198f3851b23SMark Johnston 	if ((p->p_flag & P_WEXIT) != 0) {
1199f3851b23SMark Johnston 		/*
1200f3851b23SMark Johnston 		 * There's nothing to do if the process is exiting, but avoid
1201f3851b23SMark Johnston 		 * signaling an error.
1202f3851b23SMark Johnston 		 */
1203fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1204fe41d17aSJohn Baldwin 		return (1);
1205fe41d17aSJohn Baldwin 	}
12061762f674SKonstantin Belousov 	old_kiop = NULL;
1207ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1208df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
12091762f674SKonstantin Belousov 		if (p->p_ktrioparms != NULL &&
12101762f674SKonstantin Belousov 		    p->p_ktrioparms->vp != new_kiop->vp) {
12111762f674SKonstantin Belousov 			/* if trace file already in use, relinquish below */
12121762f674SKonstantin Belousov 			old_kiop = ktr_io_params_rele(p->p_ktrioparms);
12131762f674SKonstantin Belousov 			p->p_ktrioparms = NULL;
1214a5881ea5SJohn Baldwin 		}
12151762f674SKonstantin Belousov 		if (p->p_ktrioparms == NULL) {
12161762f674SKonstantin Belousov 			p->p_ktrioparms = new_kiop;
12171762f674SKonstantin Belousov 			ktr_io_params_ref(new_kiop);
1218df8bae1dSRodney W. Grimes 		}
1219df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
122032f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1221df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1222df8bae1dSRodney W. Grimes 	} else {
1223df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1224d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1225df8bae1dSRodney W. Grimes 			/* no more tracing */
12261762f674SKonstantin Belousov 			old_kiop = ktr_freeproc(p);
1227a7ff7443SJohn Baldwin 	}
1228ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
122922ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
123022ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1231a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
12321762f674SKonstantin Belousov 	ktr_io_params_free(old_kiop);
1233df8bae1dSRodney W. Grimes 
1234df8bae1dSRodney W. Grimes 	return (1);
1235df8bae1dSRodney W. Grimes }
1236df8bae1dSRodney W. Grimes 
123787b6de2bSPoul-Henning Kamp static int
1238039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
12391762f674SKonstantin Belousov     struct ktr_io_params *new_kiop)
1240df8bae1dSRodney W. Grimes {
1241039644ecSEd Maste 	struct proc *p;
1242039644ecSEd Maste 	int ret = 0;
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes 	p = top;
1245fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
124664cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1247df8bae1dSRodney W. Grimes 	for (;;) {
12481762f674SKonstantin Belousov 		ret |= ktrops(td, p, ops, facs, new_kiop);
1249df8bae1dSRodney W. Grimes 		/*
1250df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1251df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1252df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1253df8bae1dSRodney W. Grimes 		 */
12542e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
12552e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1256df8bae1dSRodney W. Grimes 		else for (;;) {
125764cc6a13SJohn Baldwin 			if (p == top)
1258df8bae1dSRodney W. Grimes 				return (ret);
12592e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
12602e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1261df8bae1dSRodney W. Grimes 				break;
1262df8bae1dSRodney W. Grimes 			}
1263b75356e1SJeffrey Hsu 			p = p->p_pptr;
1264df8bae1dSRodney W. Grimes 		}
1265fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1266df8bae1dSRodney W. Grimes 	}
1267df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1268df8bae1dSRodney W. Grimes }
1269df8bae1dSRodney W. Grimes 
127087b6de2bSPoul-Henning Kamp static void
12712c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1272df8bae1dSRodney W. Grimes {
1273fc369a35SKonstantin Belousov 	struct ktr_io_params *kiop, *kiop1;
1274ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1275ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1276ea3fc8e4SJohn Baldwin 	struct proc *p;
1277ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1278df8bae1dSRodney W. Grimes 	struct uio auio;
1279ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1280f2a2857bSKirk McKusick 	struct mount *mp;
128102645b88SKonstantin Belousov 	off_t lim;
1282a6144f71SKonstantin Belousov 	int datalen, buflen;
12835050aa86SKonstantin Belousov 	int error;
1284df8bae1dSRodney W. Grimes 
12851762f674SKonstantin Belousov 	p = td->td_proc;
12861762f674SKonstantin Belousov 
12872c255e9dSRobert Watson 	/*
1288fc369a35SKonstantin Belousov 	 * We reference the kiop for use in I/O in case ktrace is
12892c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
12902c255e9dSRobert Watson 	 */
12912c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
12921762f674SKonstantin Belousov 	kiop = p->p_ktrioparms;
12932c255e9dSRobert Watson 
1294ea3fc8e4SJohn Baldwin 	/*
12951762f674SKonstantin Belousov 	 * If kiop is NULL, it has been cleared out from under this
12961762f674SKonstantin Belousov 	 * request, so just drop it.
1297ea3fc8e4SJohn Baldwin 	 */
12981762f674SKonstantin Belousov 	if (kiop == NULL) {
1299118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1300df8bae1dSRodney W. Grimes 		return;
13012c255e9dSRobert Watson 	}
13021762f674SKonstantin Belousov 
1303fc369a35SKonstantin Belousov 	ktr_io_params_ref(kiop);
13041762f674SKonstantin Belousov 	vp = kiop->vp;
13051762f674SKonstantin Belousov 	cred = kiop->cr;
130602645b88SKonstantin Belousov 	lim = kiop->lim;
13071762f674SKonstantin Belousov 
13082c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1309118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
13102c255e9dSRobert Watson 
1311ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
1312fc90f3a2SDmitry Chagin 	KASSERT(((u_short)kth->ktr_type & ~KTR_TYPE) < nitems(data_lengths),
1313a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
1314fc90f3a2SDmitry Chagin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_TYPE];
1315ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1316df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1317df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1318df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1319df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1320df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1321df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1322df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1323df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1324ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1325ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1326ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1327ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1328ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1329df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1330ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1331ea3fc8e4SJohn Baldwin 	}
1332ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1333d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1334d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1335ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1336ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1337ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1338b92584a6SJohn Baldwin 	}
13392c255e9dSRobert Watson 
1340f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1341cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
134202645b88SKonstantin Belousov 	td->td_ktr_io_lim = lim;
1343467a273cSRobert Watson #ifdef MAC
134430d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1345467a273cSRobert Watson 	if (error == 0)
1346467a273cSRobert Watson #endif
1347ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
1348b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
1349f2a2857bSKirk McKusick 	vn_finished_write(mp);
13501762f674SKonstantin Belousov 	if (error == 0) {
1351fc369a35SKonstantin Belousov 		mtx_lock(&ktrace_mtx);
1352fc369a35SKonstantin Belousov 		kiop = ktr_io_params_rele(kiop);
1353fc369a35SKonstantin Belousov 		mtx_unlock(&ktrace_mtx);
1354fc369a35SKonstantin Belousov 		ktr_io_params_free(kiop);
1355df8bae1dSRodney W. Grimes 		return;
1356118258f5SBjoern A. Zeeb 	}
1357118258f5SBjoern A. Zeeb 
1358df8bae1dSRodney W. Grimes 	/*
1359a6144f71SKonstantin Belousov 	 * If error encountered, give up tracing on this vnode on this
1360a6144f71SKonstantin Belousov 	 * process.  Other processes might still be suitable for
1361a6144f71SKonstantin Belousov 	 * writes to this vnode.
1362df8bae1dSRodney W. Grimes 	 */
1363a6144f71SKonstantin Belousov 	log(LOG_NOTICE,
1364a6144f71SKonstantin Belousov 	    "ktrace write failed, errno %d, tracing stopped for pid %d\n",
1365a6144f71SKonstantin Belousov 	    error, p->p_pid);
13661762f674SKonstantin Belousov 
1367fc369a35SKonstantin Belousov 	kiop1 = NULL;
1368ea3fc8e4SJohn Baldwin 	PROC_LOCK(p);
1369ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
13701762f674SKonstantin Belousov 	if (p->p_ktrioparms != NULL && p->p_ktrioparms->vp == vp)
1371fc369a35SKonstantin Belousov 		kiop1 = ktr_freeproc(p);
1372fc369a35SKonstantin Belousov 	kiop = ktr_io_params_rele(kiop);
1373ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
1374ea3fc8e4SJohn Baldwin 	PROC_UNLOCK(p);
1375fc369a35SKonstantin Belousov 	ktr_io_params_free(kiop1);
13761762f674SKonstantin Belousov 	ktr_io_params_free(kiop);
1377df8bae1dSRodney W. Grimes }
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes /*
1380df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1381df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1382df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1383df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1384df8bae1dSRodney W. Grimes  * so, only root may further change it.
1385df8bae1dSRodney W. Grimes  */
138687b6de2bSPoul-Henning Kamp static int
1387039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1388df8bae1dSRodney W. Grimes {
1389df8bae1dSRodney W. Grimes 
1390a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1391a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
139232f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
139375c13541SPoul-Henning Kamp 		return (0);
1394a0f75161SRobert Watson 
1395f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1396a0f75161SRobert Watson 		return (0);
1397a0f75161SRobert Watson 
1398df8bae1dSRodney W. Grimes 	return (1);
1399df8bae1dSRodney W. Grimes }
1400df8bae1dSRodney W. Grimes 
1401db6a20e2SGarrett Wollman #endif /* KTRACE */
1402