xref: /freebsd/sys/kern/kern_ktrace.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
19454b2d8SWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
52c255e9dSRobert Watson  *	The Regents of the University of California.
62c255e9dSRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
72c255e9dSRobert Watson  * All rights reserved.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1769a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
39db6a20e2SGarrett Wollman #include "opt_ktrace.h"
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
424a144410SRobert Watson #include <sys/capsicum.h>
43f23b4c91SGarrett Wollman #include <sys/systm.h>
44ea3fc8e4SJohn Baldwin #include <sys/fcntl.h>
45ea3fc8e4SJohn Baldwin #include <sys/kernel.h>
46ea3fc8e4SJohn Baldwin #include <sys/kthread.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/mutex.h>
49ea3fc8e4SJohn Baldwin #include <sys/malloc.h>
50033eb86eSJeff Roberson #include <sys/mount.h>
51df8bae1dSRodney W. Grimes #include <sys/namei.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53ea3fc8e4SJohn Baldwin #include <sys/proc.h>
54ea3fc8e4SJohn Baldwin #include <sys/unistd.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
5660e15db9SDag-Erling Smørgrav #include <sys/socket.h>
5760e15db9SDag-Erling Smørgrav #include <sys/stat.h>
58df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
591005a129SJohn Baldwin #include <sys/sx.h>
60ea3fc8e4SJohn Baldwin #include <sys/sysctl.h>
617705d4b2SDmitry Chagin #include <sys/sysent.h>
62df8bae1dSRodney W. Grimes #include <sys/syslog.h>
63ea3fc8e4SJohn Baldwin #include <sys/sysproto.h>
64df8bae1dSRodney W. Grimes 
65aed55708SRobert Watson #include <security/mac/mac_framework.h>
66aed55708SRobert Watson 
672c255e9dSRobert Watson /*
682c255e9dSRobert Watson  * The ktrace facility allows the tracing of certain key events in user space
692c255e9dSRobert Watson  * processes, such as system calls, signal delivery, context switches, and
702c255e9dSRobert Watson  * user generated events using utrace(2).  It works by streaming event
712c255e9dSRobert Watson  * records and data to a vnode associated with the process using the
722c255e9dSRobert Watson  * ktrace(2) system call.  In general, records can be written directly from
732c255e9dSRobert Watson  * the context that generates the event.  One important exception to this is
742c255e9dSRobert Watson  * during a context switch, where sleeping is not permitted.  To handle this
752c255e9dSRobert Watson  * case, trace events are generated using in-kernel ktr_request records, and
762c255e9dSRobert Watson  * then delivered to disk at a convenient moment -- either immediately, the
772c255e9dSRobert Watson  * next traceable event, at system call return, or at process exit.
782c255e9dSRobert Watson  *
792c255e9dSRobert Watson  * When dealing with multiple threads or processes writing to the same event
802c255e9dSRobert Watson  * log, ordering guarantees are weak: specifically, if an event has multiple
812c255e9dSRobert Watson  * records (i.e., system call enter and return), they may be interlaced with
822c255e9dSRobert Watson  * records from another event.  Process and thread ID information is provided
832c255e9dSRobert Watson  * in the record, and user applications can de-interlace events if required.
842c255e9dSRobert Watson  */
852c255e9dSRobert Watson 
86a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
8755166637SPoul-Henning Kamp 
88db6a20e2SGarrett Wollman #ifdef KTRACE
89ea3fc8e4SJohn Baldwin 
90de5b1952SAlexander Leidinger FEATURE(ktrace, "Kernel support for system-call tracing");
91de5b1952SAlexander Leidinger 
92ea3fc8e4SJohn Baldwin #ifndef KTRACE_REQUEST_POOL
93ea3fc8e4SJohn Baldwin #define	KTRACE_REQUEST_POOL	100
94ea3fc8e4SJohn Baldwin #endif
95ea3fc8e4SJohn Baldwin 
96ea3fc8e4SJohn Baldwin struct ktr_request {
97ea3fc8e4SJohn Baldwin 	struct	ktr_header ktr_header;
98d977a583SRobert Watson 	void	*ktr_buffer;
99ea3fc8e4SJohn Baldwin 	union {
1007705d4b2SDmitry Chagin 		struct	ktr_proc_ctor ktr_proc_ctor;
101c601ad8eSDag-Erling Smørgrav 		struct	ktr_cap_fail ktr_cap_fail;
102ea3fc8e4SJohn Baldwin 		struct	ktr_syscall ktr_syscall;
103ea3fc8e4SJohn Baldwin 		struct	ktr_sysret ktr_sysret;
104ea3fc8e4SJohn Baldwin 		struct	ktr_genio ktr_genio;
105ea3fc8e4SJohn Baldwin 		struct	ktr_psig ktr_psig;
106ea3fc8e4SJohn Baldwin 		struct	ktr_csw ktr_csw;
10735818d2eSJohn Baldwin 		struct	ktr_fault ktr_fault;
10835818d2eSJohn Baldwin 		struct	ktr_faultend ktr_faultend;
109ea3fc8e4SJohn Baldwin 	} ktr_data;
110ea3fc8e4SJohn Baldwin 	STAILQ_ENTRY(ktr_request) ktr_list;
111ea3fc8e4SJohn Baldwin };
112ea3fc8e4SJohn Baldwin 
113ea3fc8e4SJohn Baldwin static int data_lengths[] = {
114093e059cSJilles Tjoelker 	[KTR_SYSCALL] = offsetof(struct ktr_syscall, ktr_args),
115093e059cSJilles Tjoelker 	[KTR_SYSRET] = sizeof(struct ktr_sysret),
116093e059cSJilles Tjoelker 	[KTR_NAMEI] = 0,
117093e059cSJilles Tjoelker 	[KTR_GENIO] = sizeof(struct ktr_genio),
118093e059cSJilles Tjoelker 	[KTR_PSIG] = sizeof(struct ktr_psig),
119093e059cSJilles Tjoelker 	[KTR_CSW] = sizeof(struct ktr_csw),
120093e059cSJilles Tjoelker 	[KTR_USER] = 0,
121093e059cSJilles Tjoelker 	[KTR_STRUCT] = 0,
122093e059cSJilles Tjoelker 	[KTR_SYSCTL] = 0,
123093e059cSJilles Tjoelker 	[KTR_PROCCTOR] = sizeof(struct ktr_proc_ctor),
124093e059cSJilles Tjoelker 	[KTR_PROCDTOR] = 0,
125093e059cSJilles Tjoelker 	[KTR_CAPFAIL] = sizeof(struct ktr_cap_fail),
126093e059cSJilles Tjoelker 	[KTR_FAULT] = sizeof(struct ktr_fault),
127093e059cSJilles Tjoelker 	[KTR_FAULTEND] = sizeof(struct ktr_faultend),
128ea3fc8e4SJohn Baldwin };
129ea3fc8e4SJohn Baldwin 
130ea3fc8e4SJohn Baldwin static STAILQ_HEAD(, ktr_request) ktr_free;
131ea3fc8e4SJohn Baldwin 
1325ece08f5SPoul-Henning Kamp static SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
13312301fc3SJohn Baldwin 
1348b149b51SJohn Baldwin static u_int ktr_requestpool = KTRACE_REQUEST_POOL;
13512301fc3SJohn Baldwin TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
13612301fc3SJohn Baldwin 
1371e4296c9SKonstantin Belousov u_int ktr_geniosize = PAGE_SIZE;
138af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RWTUN, &ktr_geniosize,
13912301fc3SJohn Baldwin     0, "Maximum size of genio event payload");
140ea3fc8e4SJohn Baldwin 
141ea3fc8e4SJohn Baldwin static int print_message = 1;
142d680caabSJohn Baldwin static struct mtx ktrace_mtx;
1432c255e9dSRobert Watson static struct sx ktrace_sx;
144ea3fc8e4SJohn Baldwin 
145ea3fc8e4SJohn Baldwin static void ktrace_init(void *dummy);
146ea3fc8e4SJohn Baldwin static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
147b4c20e5eSDmitry Chagin static u_int ktrace_resize_pool(u_int oldsize, u_int newsize);
14822ec0406SDmitry Chagin static struct ktr_request *ktr_getrequest_entered(struct thread *td, int type);
149ea3fc8e4SJohn Baldwin static struct ktr_request *ktr_getrequest(int type);
1502c255e9dSRobert Watson static void ktr_submitrequest(struct thread *td, struct ktr_request *req);
151d680caabSJohn Baldwin static void ktr_freeproc(struct proc *p, struct ucred **uc,
152d680caabSJohn Baldwin     struct vnode **vp);
153ea3fc8e4SJohn Baldwin static void ktr_freerequest(struct ktr_request *req);
154d680caabSJohn Baldwin static void ktr_freerequest_locked(struct ktr_request *req);
1552c255e9dSRobert Watson static void ktr_writerequest(struct thread *td, struct ktr_request *req);
156a7ff7443SJohn Baldwin static int ktrcanset(struct thread *,struct proc *);
157a7ff7443SJohn Baldwin static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
158a7ff7443SJohn Baldwin static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
15922ec0406SDmitry Chagin static void ktrprocctor_entered(struct thread *, struct proc *);
16098d93822SBruce Evans 
1612c255e9dSRobert Watson /*
1622c255e9dSRobert Watson  * ktrace itself generates events, such as context switches, which we do not
1632c255e9dSRobert Watson  * wish to trace.  Maintain a flag, TDP_INKTRACE, on each thread to determine
1642c255e9dSRobert Watson  * whether or not it is in a region where tracing of events should be
1652c255e9dSRobert Watson  * suppressed.
1662c255e9dSRobert Watson  */
1672c255e9dSRobert Watson static void
1682c255e9dSRobert Watson ktrace_enter(struct thread *td)
1692c255e9dSRobert Watson {
1702c255e9dSRobert Watson 
1712c255e9dSRobert Watson 	KASSERT(!(td->td_pflags & TDP_INKTRACE), ("ktrace_enter: flag set"));
1722c255e9dSRobert Watson 	td->td_pflags |= TDP_INKTRACE;
1732c255e9dSRobert Watson }
1742c255e9dSRobert Watson 
1752c255e9dSRobert Watson static void
1762c255e9dSRobert Watson ktrace_exit(struct thread *td)
1772c255e9dSRobert Watson {
1782c255e9dSRobert Watson 
1792c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_exit: flag not set"));
1802c255e9dSRobert Watson 	td->td_pflags &= ~TDP_INKTRACE;
1812c255e9dSRobert Watson }
1822c255e9dSRobert Watson 
1832c255e9dSRobert Watson static void
1842c255e9dSRobert Watson ktrace_assert(struct thread *td)
1852c255e9dSRobert Watson {
1862c255e9dSRobert Watson 
1872c255e9dSRobert Watson 	KASSERT(td->td_pflags & TDP_INKTRACE, ("ktrace_assert: flag not set"));
1882c255e9dSRobert Watson }
1892c255e9dSRobert Watson 
190ea3fc8e4SJohn Baldwin static void
191ea3fc8e4SJohn Baldwin ktrace_init(void *dummy)
192df8bae1dSRodney W. Grimes {
193ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
194ea3fc8e4SJohn Baldwin 	int i;
195df8bae1dSRodney W. Grimes 
196ea3fc8e4SJohn Baldwin 	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
1972c255e9dSRobert Watson 	sx_init(&ktrace_sx, "ktrace_sx");
198ea3fc8e4SJohn Baldwin 	STAILQ_INIT(&ktr_free);
199ea3fc8e4SJohn Baldwin 	for (i = 0; i < ktr_requestpool; i++) {
200a163d034SWarner Losh 		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
201ea3fc8e4SJohn Baldwin 		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
202ea3fc8e4SJohn Baldwin 	}
203ea3fc8e4SJohn Baldwin }
204ea3fc8e4SJohn Baldwin SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
205ea3fc8e4SJohn Baldwin 
206ea3fc8e4SJohn Baldwin static int
207ea3fc8e4SJohn Baldwin sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
208ea3fc8e4SJohn Baldwin {
209ea3fc8e4SJohn Baldwin 	struct thread *td;
2108b149b51SJohn Baldwin 	u_int newsize, oldsize, wantsize;
211ea3fc8e4SJohn Baldwin 	int error;
212ea3fc8e4SJohn Baldwin 
213ea3fc8e4SJohn Baldwin 	/* Handle easy read-only case first to avoid warnings from GCC. */
214ea3fc8e4SJohn Baldwin 	if (!req->newptr) {
215ea3fc8e4SJohn Baldwin 		oldsize = ktr_requestpool;
2168b149b51SJohn Baldwin 		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
217ea3fc8e4SJohn Baldwin 	}
218ea3fc8e4SJohn Baldwin 
2198b149b51SJohn Baldwin 	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
220ea3fc8e4SJohn Baldwin 	if (error)
221ea3fc8e4SJohn Baldwin 		return (error);
222ea3fc8e4SJohn Baldwin 	td = curthread;
2232c255e9dSRobert Watson 	ktrace_enter(td);
224ea3fc8e4SJohn Baldwin 	oldsize = ktr_requestpool;
225b4c20e5eSDmitry Chagin 	newsize = ktrace_resize_pool(oldsize, wantsize);
2262c255e9dSRobert Watson 	ktrace_exit(td);
2278b149b51SJohn Baldwin 	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
228ea3fc8e4SJohn Baldwin 	if (error)
229ea3fc8e4SJohn Baldwin 		return (error);
230a5896914SJoseph Koshy 	if (wantsize > oldsize && newsize < wantsize)
231ea3fc8e4SJohn Baldwin 		return (ENOSPC);
232ea3fc8e4SJohn Baldwin 	return (0);
233ea3fc8e4SJohn Baldwin }
23412301fc3SJohn Baldwin SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
235a0c87b74SGavin Atkinson     &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU",
236a0c87b74SGavin Atkinson     "Pool buffer size for ktrace(1)");
237ea3fc8e4SJohn Baldwin 
2388b149b51SJohn Baldwin static u_int
239b4c20e5eSDmitry Chagin ktrace_resize_pool(u_int oldsize, u_int newsize)
240ea3fc8e4SJohn Baldwin {
241b4c20e5eSDmitry Chagin 	STAILQ_HEAD(, ktr_request) ktr_new;
242ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
243a5896914SJoseph Koshy 	int bound;
244ea3fc8e4SJohn Baldwin 
245ea3fc8e4SJohn Baldwin 	print_message = 1;
246b4c20e5eSDmitry Chagin 	bound = newsize - oldsize;
247a5896914SJoseph Koshy 	if (bound == 0)
248a5896914SJoseph Koshy 		return (ktr_requestpool);
249b4c20e5eSDmitry Chagin 	if (bound < 0) {
250b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
251ea3fc8e4SJohn Baldwin 		/* Shrink pool down to newsize if possible. */
252a5896914SJoseph Koshy 		while (bound++ < 0) {
253ea3fc8e4SJohn Baldwin 			req = STAILQ_FIRST(&ktr_free);
254ea3fc8e4SJohn Baldwin 			if (req == NULL)
255b4c20e5eSDmitry Chagin 				break;
256ea3fc8e4SJohn Baldwin 			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
257ea3fc8e4SJohn Baldwin 			ktr_requestpool--;
258ea3fc8e4SJohn Baldwin 			free(req, M_KTRACE);
259ea3fc8e4SJohn Baldwin 		}
260b4c20e5eSDmitry Chagin 	} else {
261ea3fc8e4SJohn Baldwin 		/* Grow pool up to newsize. */
262b4c20e5eSDmitry Chagin 		STAILQ_INIT(&ktr_new);
263a5896914SJoseph Koshy 		while (bound-- > 0) {
264ea3fc8e4SJohn Baldwin 			req = malloc(sizeof(struct ktr_request), M_KTRACE,
265a163d034SWarner Losh 			    M_WAITOK);
266b4c20e5eSDmitry Chagin 			STAILQ_INSERT_HEAD(&ktr_new, req, ktr_list);
267ea3fc8e4SJohn Baldwin 		}
268b4c20e5eSDmitry Chagin 		mtx_lock(&ktrace_mtx);
269b4c20e5eSDmitry Chagin 		STAILQ_CONCAT(&ktr_free, &ktr_new);
270b4c20e5eSDmitry Chagin 		ktr_requestpool += (newsize - oldsize);
271b4c20e5eSDmitry Chagin 	}
272b4c20e5eSDmitry Chagin 	mtx_unlock(&ktrace_mtx);
273ea3fc8e4SJohn Baldwin 	return (ktr_requestpool);
274ea3fc8e4SJohn Baldwin }
275ea3fc8e4SJohn Baldwin 
2765ca4819dSJohn Baldwin /* ktr_getrequest() assumes that ktr_comm[] is the same size as td_name[]. */
2775ca4819dSJohn Baldwin CTASSERT(sizeof(((struct ktr_header *)NULL)->ktr_comm) ==
2785ca4819dSJohn Baldwin     (sizeof((struct thread *)NULL)->td_name));
2795ca4819dSJohn Baldwin 
280ea3fc8e4SJohn Baldwin static struct ktr_request *
28122ec0406SDmitry Chagin ktr_getrequest_entered(struct thread *td, int type)
282ea3fc8e4SJohn Baldwin {
283ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
284ea3fc8e4SJohn Baldwin 	struct proc *p = td->td_proc;
285ea3fc8e4SJohn Baldwin 	int pm;
286ea3fc8e4SJohn Baldwin 
287c5c9bd5bSRobert Watson 	mtx_lock(&ktrace_mtx);
288ea3fc8e4SJohn Baldwin 	if (!KTRCHECK(td, type)) {
289c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
290ea3fc8e4SJohn Baldwin 		return (NULL);
291ea3fc8e4SJohn Baldwin 	}
292ea3fc8e4SJohn Baldwin 	req = STAILQ_FIRST(&ktr_free);
293ea3fc8e4SJohn Baldwin 	if (req != NULL) {
294ea3fc8e4SJohn Baldwin 		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
295ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_type = type;
29675768576SJohn Baldwin 		if (p->p_traceflag & KTRFAC_DROP) {
29775768576SJohn Baldwin 			req->ktr_header.ktr_type |= KTR_DROP;
29875768576SJohn Baldwin 			p->p_traceflag &= ~KTRFAC_DROP;
29975768576SJohn Baldwin 		}
300c5c9bd5bSRobert Watson 		mtx_unlock(&ktrace_mtx);
301ea3fc8e4SJohn Baldwin 		microtime(&req->ktr_header.ktr_time);
302ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_pid = p->p_pid;
3032bdeb3f9SRobert Watson 		req->ktr_header.ktr_tid = td->td_tid;
3045ca4819dSJohn Baldwin 		bcopy(td->td_name, req->ktr_header.ktr_comm,
3055ca4819dSJohn Baldwin 		    sizeof(req->ktr_header.ktr_comm));
306d977a583SRobert Watson 		req->ktr_buffer = NULL;
307ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = 0;
308ea3fc8e4SJohn Baldwin 	} else {
30975768576SJohn Baldwin 		p->p_traceflag |= KTRFAC_DROP;
310ea3fc8e4SJohn Baldwin 		pm = print_message;
311ea3fc8e4SJohn Baldwin 		print_message = 0;
312ea3fc8e4SJohn Baldwin 		mtx_unlock(&ktrace_mtx);
313ea3fc8e4SJohn Baldwin 		if (pm)
314ea3fc8e4SJohn Baldwin 			printf("Out of ktrace request objects.\n");
315ea3fc8e4SJohn Baldwin 	}
316ea3fc8e4SJohn Baldwin 	return (req);
317ea3fc8e4SJohn Baldwin }
318ea3fc8e4SJohn Baldwin 
3197705d4b2SDmitry Chagin static struct ktr_request *
3207705d4b2SDmitry Chagin ktr_getrequest(int type)
3217705d4b2SDmitry Chagin {
3227705d4b2SDmitry Chagin 	struct thread *td = curthread;
3237705d4b2SDmitry Chagin 	struct ktr_request *req;
3247705d4b2SDmitry Chagin 
3257705d4b2SDmitry Chagin 	ktrace_enter(td);
32622ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, type);
3277705d4b2SDmitry Chagin 	if (req == NULL)
3287705d4b2SDmitry Chagin 		ktrace_exit(td);
3297705d4b2SDmitry Chagin 
3307705d4b2SDmitry Chagin 	return (req);
3317705d4b2SDmitry Chagin }
3327705d4b2SDmitry Chagin 
3332c255e9dSRobert Watson /*
3342c255e9dSRobert Watson  * Some trace generation environments don't permit direct access to VFS,
3352c255e9dSRobert Watson  * such as during a context switch where sleeping is not allowed.  Under these
3362c255e9dSRobert Watson  * circumstances, queue a request to the thread to be written asynchronously
3372c255e9dSRobert Watson  * later.
3382c255e9dSRobert Watson  */
339ea3fc8e4SJohn Baldwin static void
3402c255e9dSRobert Watson ktr_enqueuerequest(struct thread *td, struct ktr_request *req)
341ea3fc8e4SJohn Baldwin {
342ea3fc8e4SJohn Baldwin 
343ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
3442c255e9dSRobert Watson 	STAILQ_INSERT_TAIL(&td->td_proc->p_ktr, req, ktr_list);
345ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
3462c255e9dSRobert Watson }
3472c255e9dSRobert Watson 
3482c255e9dSRobert Watson /*
3492c255e9dSRobert Watson  * Drain any pending ktrace records from the per-thread queue to disk.  This
3502c255e9dSRobert Watson  * is used both internally before committing other records, and also on
3512c255e9dSRobert Watson  * system call return.  We drain all the ones we can find at the time when
3522c255e9dSRobert Watson  * drain is requested, but don't keep draining after that as those events
353a56be37eSJohn Baldwin  * may be approximately "after" the current event.
3542c255e9dSRobert Watson  */
3552c255e9dSRobert Watson static void
3562c255e9dSRobert Watson ktr_drain(struct thread *td)
3572c255e9dSRobert Watson {
3582c255e9dSRobert Watson 	struct ktr_request *queued_req;
3592c255e9dSRobert Watson 	STAILQ_HEAD(, ktr_request) local_queue;
3602c255e9dSRobert Watson 
3612c255e9dSRobert Watson 	ktrace_assert(td);
3622c255e9dSRobert Watson 	sx_assert(&ktrace_sx, SX_XLOCKED);
3632c255e9dSRobert Watson 
3642b3fb615SJohn Baldwin 	STAILQ_INIT(&local_queue);
3652c255e9dSRobert Watson 
3662c255e9dSRobert Watson 	if (!STAILQ_EMPTY(&td->td_proc->p_ktr)) {
3672c255e9dSRobert Watson 		mtx_lock(&ktrace_mtx);
3682c255e9dSRobert Watson 		STAILQ_CONCAT(&local_queue, &td->td_proc->p_ktr);
3692c255e9dSRobert Watson 		mtx_unlock(&ktrace_mtx);
3702c255e9dSRobert Watson 
3712c255e9dSRobert Watson 		while ((queued_req = STAILQ_FIRST(&local_queue))) {
3722c255e9dSRobert Watson 			STAILQ_REMOVE_HEAD(&local_queue, ktr_list);
3732c255e9dSRobert Watson 			ktr_writerequest(td, queued_req);
3742c255e9dSRobert Watson 			ktr_freerequest(queued_req);
3752c255e9dSRobert Watson 		}
3762c255e9dSRobert Watson 	}
3772c255e9dSRobert Watson }
3782c255e9dSRobert Watson 
3792c255e9dSRobert Watson /*
3802c255e9dSRobert Watson  * Submit a trace record for immediate commit to disk -- to be used only
3812c255e9dSRobert Watson  * where entering VFS is OK.  First drain any pending records that may have
3822c255e9dSRobert Watson  * been cached in the thread.
3832c255e9dSRobert Watson  */
3842c255e9dSRobert Watson static void
38522ec0406SDmitry Chagin ktr_submitrequest(struct thread *td, struct ktr_request *req)
3862c255e9dSRobert Watson {
3872c255e9dSRobert Watson 
3882c255e9dSRobert Watson 	ktrace_assert(td);
3892c255e9dSRobert Watson 
3902c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
3912c255e9dSRobert Watson 	ktr_drain(td);
3922c255e9dSRobert Watson 	ktr_writerequest(td, req);
3932c255e9dSRobert Watson 	ktr_freerequest(req);
3942c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
3952c255e9dSRobert Watson 	ktrace_exit(td);
396ea3fc8e4SJohn Baldwin }
397ea3fc8e4SJohn Baldwin 
398ea3fc8e4SJohn Baldwin static void
399ea3fc8e4SJohn Baldwin ktr_freerequest(struct ktr_request *req)
400ea3fc8e4SJohn Baldwin {
401ea3fc8e4SJohn Baldwin 
402d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
403d680caabSJohn Baldwin 	ktr_freerequest_locked(req);
404d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
405d680caabSJohn Baldwin }
406d680caabSJohn Baldwin 
407d680caabSJohn Baldwin static void
408d680caabSJohn Baldwin ktr_freerequest_locked(struct ktr_request *req)
409d680caabSJohn Baldwin {
410d680caabSJohn Baldwin 
411d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
412d977a583SRobert Watson 	if (req->ktr_buffer != NULL)
413d977a583SRobert Watson 		free(req->ktr_buffer, M_KTRACE);
414ea3fc8e4SJohn Baldwin 	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
415d680caabSJohn Baldwin }
416d680caabSJohn Baldwin 
417d680caabSJohn Baldwin /*
418d680caabSJohn Baldwin  * Disable tracing for a process and release all associated resources.
419d680caabSJohn Baldwin  * The caller is responsible for releasing a reference on the returned
420d680caabSJohn Baldwin  * vnode and credentials.
421d680caabSJohn Baldwin  */
422d680caabSJohn Baldwin static void
423d680caabSJohn Baldwin ktr_freeproc(struct proc *p, struct ucred **uc, struct vnode **vp)
424d680caabSJohn Baldwin {
425d680caabSJohn Baldwin 	struct ktr_request *req;
426d680caabSJohn Baldwin 
427d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
428d680caabSJohn Baldwin 	mtx_assert(&ktrace_mtx, MA_OWNED);
429d680caabSJohn Baldwin 	*uc = p->p_tracecred;
430d680caabSJohn Baldwin 	p->p_tracecred = NULL;
431d680caabSJohn Baldwin 	if (vp != NULL)
432d680caabSJohn Baldwin 		*vp = p->p_tracevp;
433d680caabSJohn Baldwin 	p->p_tracevp = NULL;
434d680caabSJohn Baldwin 	p->p_traceflag = 0;
435d680caabSJohn Baldwin 	while ((req = STAILQ_FIRST(&p->p_ktr)) != NULL) {
436d680caabSJohn Baldwin 		STAILQ_REMOVE_HEAD(&p->p_ktr, ktr_list);
437d680caabSJohn Baldwin 		ktr_freerequest_locked(req);
438d680caabSJohn Baldwin 	}
439ea3fc8e4SJohn Baldwin }
440ea3fc8e4SJohn Baldwin 
44126f9a767SRodney W. Grimes void
442039644ecSEd Maste ktrsyscall(int code, int narg, register_t args[])
443df8bae1dSRodney W. Grimes {
444ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
445df8bae1dSRodney W. Grimes 	struct ktr_syscall *ktp;
446ea3fc8e4SJohn Baldwin 	size_t buflen;
4474b3aac3dSJohn Baldwin 	char *buf = NULL;
448df8bae1dSRodney W. Grimes 
4494b3aac3dSJohn Baldwin 	buflen = sizeof(register_t) * narg;
4504b3aac3dSJohn Baldwin 	if (buflen > 0) {
451a163d034SWarner Losh 		buf = malloc(buflen, M_KTRACE, M_WAITOK);
4524b3aac3dSJohn Baldwin 		bcopy(args, buf, buflen);
4534b3aac3dSJohn Baldwin 	}
454ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSCALL);
45550c22331SPoul-Henning Kamp 	if (req == NULL) {
45650c22331SPoul-Henning Kamp 		if (buf != NULL)
45750c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
458ea3fc8e4SJohn Baldwin 		return;
45950c22331SPoul-Henning Kamp 	}
460ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_syscall;
461df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
462df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
463ea3fc8e4SJohn Baldwin 	if (buflen > 0) {
464ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = buflen;
465d977a583SRobert Watson 		req->ktr_buffer = buf;
466ea3fc8e4SJohn Baldwin 	}
4672c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
47026f9a767SRodney W. Grimes void
471039644ecSEd Maste ktrsysret(int code, int error, register_t retval)
472df8bae1dSRodney W. Grimes {
473ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
474ea3fc8e4SJohn Baldwin 	struct ktr_sysret *ktp;
475df8bae1dSRodney W. Grimes 
476ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_SYSRET);
477ea3fc8e4SJohn Baldwin 	if (req == NULL)
478ea3fc8e4SJohn Baldwin 		return;
479ea3fc8e4SJohn Baldwin 	ktp = &req->ktr_data.ktr_sysret;
480ea3fc8e4SJohn Baldwin 	ktp->ktr_code = code;
481ea3fc8e4SJohn Baldwin 	ktp->ktr_error = error;
4825a01b726SEitan Adler 	ktp->ktr_retval = ((error == 0) ? retval: 0);		/* what about val2 ? */
4832c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
4842c255e9dSRobert Watson }
4852c255e9dSRobert Watson 
4862c255e9dSRobert Watson /*
487d680caabSJohn Baldwin  * When a setuid process execs, disable tracing.
488d680caabSJohn Baldwin  *
489d680caabSJohn Baldwin  * XXX: We toss any pending asynchronous records.
490d680caabSJohn Baldwin  */
491d680caabSJohn Baldwin void
492d680caabSJohn Baldwin ktrprocexec(struct proc *p, struct ucred **uc, struct vnode **vp)
493d680caabSJohn Baldwin {
494d680caabSJohn Baldwin 
495d680caabSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
496d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
497d680caabSJohn Baldwin 	ktr_freeproc(p, uc, vp);
498d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
499d680caabSJohn Baldwin }
500d680caabSJohn Baldwin 
501d680caabSJohn Baldwin /*
502d680caabSJohn Baldwin  * When a process exits, drain per-process asynchronous trace records
503d680caabSJohn Baldwin  * and disable tracing.
5042c255e9dSRobert Watson  */
5052c255e9dSRobert Watson void
5062c255e9dSRobert Watson ktrprocexit(struct thread *td)
5072c255e9dSRobert Watson {
5087705d4b2SDmitry Chagin 	struct ktr_request *req;
509d680caabSJohn Baldwin 	struct proc *p;
510d680caabSJohn Baldwin 	struct ucred *cred;
511d680caabSJohn Baldwin 	struct vnode *vp;
512d680caabSJohn Baldwin 
513d680caabSJohn Baldwin 	p = td->td_proc;
514d680caabSJohn Baldwin 	if (p->p_traceflag == 0)
515d680caabSJohn Baldwin 		return;
5162c255e9dSRobert Watson 
5172c255e9dSRobert Watson 	ktrace_enter(td);
51822ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td, KTR_PROCDTOR);
51922ec0406SDmitry Chagin 	if (req != NULL)
52022ec0406SDmitry Chagin 		ktr_enqueuerequest(td, req);
5212c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
5222c255e9dSRobert Watson 	ktr_drain(td);
5232c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
524d680caabSJohn Baldwin 	PROC_LOCK(p);
525d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
526d680caabSJohn Baldwin 	ktr_freeproc(p, &cred, &vp);
527d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
528d680caabSJohn Baldwin 	PROC_UNLOCK(p);
5295050aa86SKonstantin Belousov 	if (vp != NULL)
530d680caabSJohn Baldwin 		vrele(vp);
531d680caabSJohn Baldwin 	if (cred != NULL)
532d680caabSJohn Baldwin 		crfree(cred);
5332c255e9dSRobert Watson 	ktrace_exit(td);
5342c255e9dSRobert Watson }
5352c255e9dSRobert Watson 
5367705d4b2SDmitry Chagin static void
53722ec0406SDmitry Chagin ktrprocctor_entered(struct thread *td, struct proc *p)
5387705d4b2SDmitry Chagin {
5397705d4b2SDmitry Chagin 	struct ktr_proc_ctor *ktp;
5407705d4b2SDmitry Chagin 	struct ktr_request *req;
541de60a5f3SDmitry Chagin 	struct thread *td2;
5427705d4b2SDmitry Chagin 
5437705d4b2SDmitry Chagin 	ktrace_assert(td);
5447705d4b2SDmitry Chagin 	td2 = FIRST_THREAD_IN_PROC(p);
54522ec0406SDmitry Chagin 	req = ktr_getrequest_entered(td2, KTR_PROCCTOR);
5467705d4b2SDmitry Chagin 	if (req == NULL)
5477705d4b2SDmitry Chagin 		return;
5487705d4b2SDmitry Chagin 	ktp = &req->ktr_data.ktr_proc_ctor;
5497705d4b2SDmitry Chagin 	ktp->sv_flags = p->p_sysent->sv_flags;
55022ec0406SDmitry Chagin 	ktr_enqueuerequest(td2, req);
5517705d4b2SDmitry Chagin }
5527705d4b2SDmitry Chagin 
5537705d4b2SDmitry Chagin void
5547705d4b2SDmitry Chagin ktrprocctor(struct proc *p)
5557705d4b2SDmitry Chagin {
5567705d4b2SDmitry Chagin 	struct thread *td = curthread;
5577705d4b2SDmitry Chagin 
5587705d4b2SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) == 0)
5597705d4b2SDmitry Chagin 		return;
5607705d4b2SDmitry Chagin 
5617705d4b2SDmitry Chagin 	ktrace_enter(td);
56222ec0406SDmitry Chagin 	ktrprocctor_entered(td, p);
5637705d4b2SDmitry Chagin 	ktrace_exit(td);
5647705d4b2SDmitry Chagin }
5657705d4b2SDmitry Chagin 
5662c255e9dSRobert Watson /*
567d680caabSJohn Baldwin  * When a process forks, enable tracing in the new process if needed.
568d680caabSJohn Baldwin  */
569d680caabSJohn Baldwin void
570d680caabSJohn Baldwin ktrprocfork(struct proc *p1, struct proc *p2)
571d680caabSJohn Baldwin {
572d680caabSJohn Baldwin 
5737c34b35bSMateusz Guzik 	MPASS(p2->p_tracevp == NULL);
5747c34b35bSMateusz Guzik 	MPASS(p2->p_traceflag == 0);
5757c34b35bSMateusz Guzik 
5767c34b35bSMateusz Guzik 	if (p1->p_traceflag == 0)
5777c34b35bSMateusz Guzik 		return;
5787c34b35bSMateusz Guzik 
5797705d4b2SDmitry Chagin 	PROC_LOCK(p1);
580d680caabSJohn Baldwin 	mtx_lock(&ktrace_mtx);
581d680caabSJohn Baldwin 	if (p1->p_traceflag & KTRFAC_INHERIT) {
582d680caabSJohn Baldwin 		p2->p_traceflag = p1->p_traceflag;
583d680caabSJohn Baldwin 		if ((p2->p_tracevp = p1->p_tracevp) != NULL) {
584d680caabSJohn Baldwin 			VREF(p2->p_tracevp);
585d680caabSJohn Baldwin 			KASSERT(p1->p_tracecred != NULL,
586d680caabSJohn Baldwin 			    ("ktrace vnode with no cred"));
587d680caabSJohn Baldwin 			p2->p_tracecred = crhold(p1->p_tracecred);
588d680caabSJohn Baldwin 		}
589d680caabSJohn Baldwin 	}
590d680caabSJohn Baldwin 	mtx_unlock(&ktrace_mtx);
5917705d4b2SDmitry Chagin 	PROC_UNLOCK(p1);
5927705d4b2SDmitry Chagin 
5937705d4b2SDmitry Chagin 	ktrprocctor(p2);
594d680caabSJohn Baldwin }
595d680caabSJohn Baldwin 
596d680caabSJohn Baldwin /*
5972c255e9dSRobert Watson  * When a thread returns, drain any asynchronous records generated by the
5982c255e9dSRobert Watson  * system call.
5992c255e9dSRobert Watson  */
6002c255e9dSRobert Watson void
6012c255e9dSRobert Watson ktruserret(struct thread *td)
6022c255e9dSRobert Watson {
6032c255e9dSRobert Watson 
6042c255e9dSRobert Watson 	ktrace_enter(td);
6052c255e9dSRobert Watson 	sx_xlock(&ktrace_sx);
6062c255e9dSRobert Watson 	ktr_drain(td);
6072c255e9dSRobert Watson 	sx_xunlock(&ktrace_sx);
6082c255e9dSRobert Watson 	ktrace_exit(td);
609df8bae1dSRodney W. Grimes }
610df8bae1dSRodney W. Grimes 
61126f9a767SRodney W. Grimes void
612ea3fc8e4SJohn Baldwin ktrnamei(path)
613df8bae1dSRodney W. Grimes 	char *path;
614df8bae1dSRodney W. Grimes {
615ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
616ea3fc8e4SJohn Baldwin 	int namelen;
6174b3aac3dSJohn Baldwin 	char *buf = NULL;
618df8bae1dSRodney W. Grimes 
6194b3aac3dSJohn Baldwin 	namelen = strlen(path);
6204b3aac3dSJohn Baldwin 	if (namelen > 0) {
621a163d034SWarner Losh 		buf = malloc(namelen, M_KTRACE, M_WAITOK);
6224b3aac3dSJohn Baldwin 		bcopy(path, buf, namelen);
6234b3aac3dSJohn Baldwin 	}
624ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_NAMEI);
62550c22331SPoul-Henning Kamp 	if (req == NULL) {
62650c22331SPoul-Henning Kamp 		if (buf != NULL)
62750c22331SPoul-Henning Kamp 			free(buf, M_KTRACE);
628ea3fc8e4SJohn Baldwin 		return;
62950c22331SPoul-Henning Kamp 	}
630ea3fc8e4SJohn Baldwin 	if (namelen > 0) {
631ea3fc8e4SJohn Baldwin 		req->ktr_header.ktr_len = namelen;
632d977a583SRobert Watson 		req->ktr_buffer = buf;
633ea3fc8e4SJohn Baldwin 	}
6342c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
635df8bae1dSRodney W. Grimes }
636df8bae1dSRodney W. Grimes 
63726f9a767SRodney W. Grimes void
638039644ecSEd Maste ktrsysctl(int *name, u_int namelen)
639a56be37eSJohn Baldwin {
640a56be37eSJohn Baldwin 	struct ktr_request *req;
641a56be37eSJohn Baldwin 	u_int mib[CTL_MAXNAME + 2];
642a56be37eSJohn Baldwin 	char *mibname;
643a56be37eSJohn Baldwin 	size_t mibnamelen;
644a56be37eSJohn Baldwin 	int error;
645a56be37eSJohn Baldwin 
646a56be37eSJohn Baldwin 	/* Lookup name of mib. */
647a56be37eSJohn Baldwin 	KASSERT(namelen <= CTL_MAXNAME, ("sysctl MIB too long"));
648a56be37eSJohn Baldwin 	mib[0] = 0;
649a56be37eSJohn Baldwin 	mib[1] = 1;
650a56be37eSJohn Baldwin 	bcopy(name, mib + 2, namelen * sizeof(*name));
651a56be37eSJohn Baldwin 	mibnamelen = 128;
652a56be37eSJohn Baldwin 	mibname = malloc(mibnamelen, M_KTRACE, M_WAITOK);
653a56be37eSJohn Baldwin 	error = kernel_sysctl(curthread, mib, namelen + 2, mibname, &mibnamelen,
654a56be37eSJohn Baldwin 	    NULL, 0, &mibnamelen, 0);
655a56be37eSJohn Baldwin 	if (error) {
656a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
657a56be37eSJohn Baldwin 		return;
658a56be37eSJohn Baldwin 	}
659a56be37eSJohn Baldwin 	req = ktr_getrequest(KTR_SYSCTL);
660a56be37eSJohn Baldwin 	if (req == NULL) {
661a56be37eSJohn Baldwin 		free(mibname, M_KTRACE);
662a56be37eSJohn Baldwin 		return;
663a56be37eSJohn Baldwin 	}
664a56be37eSJohn Baldwin 	req->ktr_header.ktr_len = mibnamelen;
665a56be37eSJohn Baldwin 	req->ktr_buffer = mibname;
666a56be37eSJohn Baldwin 	ktr_submitrequest(curthread, req);
667a56be37eSJohn Baldwin }
668a56be37eSJohn Baldwin 
669a56be37eSJohn Baldwin void
670039644ecSEd Maste ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error)
671df8bae1dSRodney W. Grimes {
672ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
673ea3fc8e4SJohn Baldwin 	struct ktr_genio *ktg;
674b92584a6SJohn Baldwin 	int datalen;
675b92584a6SJohn Baldwin 	char *buf;
676df8bae1dSRodney W. Grimes 
677552afd9cSPoul-Henning Kamp 	if (error) {
678552afd9cSPoul-Henning Kamp 		free(uio, M_IOV);
679df8bae1dSRodney W. Grimes 		return;
680552afd9cSPoul-Henning Kamp 	}
681b92584a6SJohn Baldwin 	uio->uio_offset = 0;
682b92584a6SJohn Baldwin 	uio->uio_rw = UIO_WRITE;
683526d0bd5SKonstantin Belousov 	datalen = MIN(uio->uio_resid, ktr_geniosize);
684a163d034SWarner Losh 	buf = malloc(datalen, M_KTRACE, M_WAITOK);
685552afd9cSPoul-Henning Kamp 	error = uiomove(buf, datalen, uio);
686552afd9cSPoul-Henning Kamp 	free(uio, M_IOV);
687552afd9cSPoul-Henning Kamp 	if (error) {
688b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
689ea3fc8e4SJohn Baldwin 		return;
690b92584a6SJohn Baldwin 	}
691b92584a6SJohn Baldwin 	req = ktr_getrequest(KTR_GENIO);
692b92584a6SJohn Baldwin 	if (req == NULL) {
693b92584a6SJohn Baldwin 		free(buf, M_KTRACE);
694b92584a6SJohn Baldwin 		return;
695b92584a6SJohn Baldwin 	}
696ea3fc8e4SJohn Baldwin 	ktg = &req->ktr_data.ktr_genio;
697ea3fc8e4SJohn Baldwin 	ktg->ktr_fd = fd;
698ea3fc8e4SJohn Baldwin 	ktg->ktr_rw = rw;
699b92584a6SJohn Baldwin 	req->ktr_header.ktr_len = datalen;
700d977a583SRobert Watson 	req->ktr_buffer = buf;
7012c255e9dSRobert Watson 	ktr_submitrequest(curthread, req);
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
70426f9a767SRodney W. Grimes void
705039644ecSEd Maste ktrpsig(int sig, sig_t action, sigset_t *mask, int code)
706df8bae1dSRodney W. Grimes {
70722ec0406SDmitry Chagin 	struct thread *td = curthread;
708ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
709ea3fc8e4SJohn Baldwin 	struct ktr_psig	*kp;
710df8bae1dSRodney W. Grimes 
711ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_PSIG);
712ea3fc8e4SJohn Baldwin 	if (req == NULL)
713ea3fc8e4SJohn Baldwin 		return;
714ea3fc8e4SJohn Baldwin 	kp = &req->ktr_data.ktr_psig;
715ea3fc8e4SJohn Baldwin 	kp->signo = (char)sig;
716ea3fc8e4SJohn Baldwin 	kp->action = action;
717ea3fc8e4SJohn Baldwin 	kp->mask = *mask;
718ea3fc8e4SJohn Baldwin 	kp->code = code;
71922ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
72022ec0406SDmitry Chagin 	ktrace_exit(td);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
72326f9a767SRodney W. Grimes void
724039644ecSEd Maste ktrcsw(int out, int user, const char *wmesg)
725df8bae1dSRodney W. Grimes {
72622ec0406SDmitry Chagin 	struct thread *td = curthread;
727ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
728ea3fc8e4SJohn Baldwin 	struct ktr_csw *kc;
729df8bae1dSRodney W. Grimes 
730ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_CSW);
731ea3fc8e4SJohn Baldwin 	if (req == NULL)
732ea3fc8e4SJohn Baldwin 		return;
733ea3fc8e4SJohn Baldwin 	kc = &req->ktr_data.ktr_csw;
734ea3fc8e4SJohn Baldwin 	kc->out = out;
735ea3fc8e4SJohn Baldwin 	kc->user = user;
73688bf5036SJohn Baldwin 	if (wmesg != NULL)
73788bf5036SJohn Baldwin 		strlcpy(kc->wmesg, wmesg, sizeof(kc->wmesg));
73888bf5036SJohn Baldwin 	else
73988bf5036SJohn Baldwin 		bzero(kc->wmesg, sizeof(kc->wmesg));
74022ec0406SDmitry Chagin 	ktr_enqueuerequest(td, req);
74122ec0406SDmitry Chagin 	ktrace_exit(td);
742df8bae1dSRodney W. Grimes }
74360e15db9SDag-Erling Smørgrav 
74460e15db9SDag-Erling Smørgrav void
745039644ecSEd Maste ktrstruct(const char *name, void *data, size_t datalen)
74660e15db9SDag-Erling Smørgrav {
74760e15db9SDag-Erling Smørgrav 	struct ktr_request *req;
7484dd3a21fSMateusz Guzik 	char *buf;
7494dd3a21fSMateusz Guzik 	size_t buflen, namelen;
75060e15db9SDag-Erling Smørgrav 
7514dd3a21fSMateusz Guzik 	if (data == NULL)
75260e15db9SDag-Erling Smørgrav 		datalen = 0;
7534dd3a21fSMateusz Guzik 	namelen = strlen(name) + 1;
7544dd3a21fSMateusz Guzik 	buflen = namelen + datalen;
75560e15db9SDag-Erling Smørgrav 	buf = malloc(buflen, M_KTRACE, M_WAITOK);
756a3052d6eSJohn Baldwin 	strcpy(buf, name);
7574dd3a21fSMateusz Guzik 	bcopy(data, buf + namelen, datalen);
75860e15db9SDag-Erling Smørgrav 	if ((req = ktr_getrequest(KTR_STRUCT)) == NULL) {
75960e15db9SDag-Erling Smørgrav 		free(buf, M_KTRACE);
76060e15db9SDag-Erling Smørgrav 		return;
76160e15db9SDag-Erling Smørgrav 	}
76260e15db9SDag-Erling Smørgrav 	req->ktr_buffer = buf;
76360e15db9SDag-Erling Smørgrav 	req->ktr_header.ktr_len = buflen;
76460e15db9SDag-Erling Smørgrav 	ktr_submitrequest(curthread, req);
76560e15db9SDag-Erling Smørgrav }
766c601ad8eSDag-Erling Smørgrav 
767c601ad8eSDag-Erling Smørgrav void
768039644ecSEd Maste ktrcapfail(enum ktr_cap_fail_type type, const cap_rights_t *needed,
769039644ecSEd Maste     const cap_rights_t *held)
770c601ad8eSDag-Erling Smørgrav {
771c601ad8eSDag-Erling Smørgrav 	struct thread *td = curthread;
772c601ad8eSDag-Erling Smørgrav 	struct ktr_request *req;
773c601ad8eSDag-Erling Smørgrav 	struct ktr_cap_fail *kcf;
774c601ad8eSDag-Erling Smørgrav 
775c601ad8eSDag-Erling Smørgrav 	req = ktr_getrequest(KTR_CAPFAIL);
776c601ad8eSDag-Erling Smørgrav 	if (req == NULL)
777c601ad8eSDag-Erling Smørgrav 		return;
778c601ad8eSDag-Erling Smørgrav 	kcf = &req->ktr_data.ktr_cap_fail;
779e141be6fSDag-Erling Smørgrav 	kcf->cap_type = type;
7803fded357SPawel Jakub Dawidek 	if (needed != NULL)
7817008be5bSPawel Jakub Dawidek 		kcf->cap_needed = *needed;
7823fded357SPawel Jakub Dawidek 	else
7833fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_needed);
7843fded357SPawel Jakub Dawidek 	if (held != NULL)
7857008be5bSPawel Jakub Dawidek 		kcf->cap_held = *held;
7863fded357SPawel Jakub Dawidek 	else
7873fded357SPawel Jakub Dawidek 		cap_rights_init(&kcf->cap_held);
788c601ad8eSDag-Erling Smørgrav 	ktr_enqueuerequest(td, req);
789c601ad8eSDag-Erling Smørgrav 	ktrace_exit(td);
790c601ad8eSDag-Erling Smørgrav }
79135818d2eSJohn Baldwin 
79235818d2eSJohn Baldwin void
793039644ecSEd Maste ktrfault(vm_offset_t vaddr, int type)
79435818d2eSJohn Baldwin {
79535818d2eSJohn Baldwin 	struct thread *td = curthread;
79635818d2eSJohn Baldwin 	struct ktr_request *req;
79735818d2eSJohn Baldwin 	struct ktr_fault *kf;
79835818d2eSJohn Baldwin 
79935818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULT);
80035818d2eSJohn Baldwin 	if (req == NULL)
80135818d2eSJohn Baldwin 		return;
80235818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_fault;
80335818d2eSJohn Baldwin 	kf->vaddr = vaddr;
80435818d2eSJohn Baldwin 	kf->type = type;
80535818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
80635818d2eSJohn Baldwin 	ktrace_exit(td);
80735818d2eSJohn Baldwin }
80835818d2eSJohn Baldwin 
80935818d2eSJohn Baldwin void
810039644ecSEd Maste ktrfaultend(int result)
81135818d2eSJohn Baldwin {
81235818d2eSJohn Baldwin 	struct thread *td = curthread;
81335818d2eSJohn Baldwin 	struct ktr_request *req;
81435818d2eSJohn Baldwin 	struct ktr_faultend *kf;
81535818d2eSJohn Baldwin 
81635818d2eSJohn Baldwin 	req = ktr_getrequest(KTR_FAULTEND);
81735818d2eSJohn Baldwin 	if (req == NULL)
81835818d2eSJohn Baldwin 		return;
81935818d2eSJohn Baldwin 	kf = &req->ktr_data.ktr_faultend;
82035818d2eSJohn Baldwin 	kf->result = result;
82135818d2eSJohn Baldwin 	ktr_enqueuerequest(td, req);
82235818d2eSJohn Baldwin 	ktrace_exit(td);
82335818d2eSJohn Baldwin }
82464cc6a13SJohn Baldwin #endif /* KTRACE */
825df8bae1dSRodney W. Grimes 
826df8bae1dSRodney W. Grimes /* Interface and common routines */
827df8bae1dSRodney W. Grimes 
828d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
829df8bae1dSRodney W. Grimes struct ktrace_args {
830df8bae1dSRodney W. Grimes 	char	*fname;
831df8bae1dSRodney W. Grimes 	int	ops;
832df8bae1dSRodney W. Grimes 	int	facs;
833df8bae1dSRodney W. Grimes 	int	pid;
834df8bae1dSRodney W. Grimes };
835d2d3e875SBruce Evans #endif
836df8bae1dSRodney W. Grimes /* ARGSUSED */
83726f9a767SRodney W. Grimes int
838039644ecSEd Maste sys_ktrace(struct thread *td, struct ktrace_args *uap)
839df8bae1dSRodney W. Grimes {
840db6a20e2SGarrett Wollman #ifdef KTRACE
841039644ecSEd Maste 	struct vnode *vp = NULL;
842039644ecSEd Maste 	struct proc *p;
843df8bae1dSRodney W. Grimes 	struct pgrp *pg;
844df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
845df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
846df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
847400a74bfSPawel Jakub Dawidek 	int nfound, ret = 0;
8485050aa86SKonstantin Belousov 	int flags, error = 0;
849df8bae1dSRodney W. Grimes 	struct nameidata nd;
850a5881ea5SJohn Baldwin 	struct ucred *cred;
851df8bae1dSRodney W. Grimes 
85264cc6a13SJohn Baldwin 	/*
85364cc6a13SJohn Baldwin 	 * Need something to (un)trace.
85464cc6a13SJohn Baldwin 	 */
85564cc6a13SJohn Baldwin 	if (ops != KTROP_CLEARFILE && facs == 0)
85664cc6a13SJohn Baldwin 		return (EINVAL);
85764cc6a13SJohn Baldwin 
8582c255e9dSRobert Watson 	ktrace_enter(td);
859df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
860df8bae1dSRodney W. Grimes 		/*
861df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
862df8bae1dSRodney W. Grimes 		 */
8635050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
864e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
8659e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
866797f2d22SPoul-Henning Kamp 		if (error) {
8672c255e9dSRobert Watson 			ktrace_exit(td);
868df8bae1dSRodney W. Grimes 			return (error);
869df8bae1dSRodney W. Grimes 		}
870762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
871df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
87222db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
873df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
874a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
8752c255e9dSRobert Watson 			ktrace_exit(td);
876df8bae1dSRodney W. Grimes 			return (EACCES);
877df8bae1dSRodney W. Grimes 		}
878df8bae1dSRodney W. Grimes 	}
879df8bae1dSRodney W. Grimes 	/*
88079deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
881df8bae1dSRodney W. Grimes 	 */
882df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
88351fd6380SMike Pritchard 		int vrele_count;
88451fd6380SMike Pritchard 
88551fd6380SMike Pritchard 		vrele_count = 0;
8861005a129SJohn Baldwin 		sx_slock(&allproc_lock);
8874f506694SXin LI 		FOREACH_PROC_IN_SYSTEM(p) {
888a7ff7443SJohn Baldwin 			PROC_LOCK(p);
889a5881ea5SJohn Baldwin 			if (p->p_tracevp == vp) {
890ea3fc8e4SJohn Baldwin 				if (ktrcanset(td, p)) {
891ea3fc8e4SJohn Baldwin 					mtx_lock(&ktrace_mtx);
892d680caabSJohn Baldwin 					ktr_freeproc(p, &cred, NULL);
893ea3fc8e4SJohn Baldwin 					mtx_unlock(&ktrace_mtx);
89451fd6380SMike Pritchard 					vrele_count++;
895a5881ea5SJohn Baldwin 					crfree(cred);
89651fd6380SMike Pritchard 				} else
897df8bae1dSRodney W. Grimes 					error = EPERM;
898df8bae1dSRodney W. Grimes 			}
899a7ff7443SJohn Baldwin 			PROC_UNLOCK(p);
90079deba82SMatthew Dillon 		}
9011005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
90251fd6380SMike Pritchard 		if (vrele_count > 0) {
90351fd6380SMike Pritchard 			while (vrele_count-- > 0)
90451fd6380SMike Pritchard 				vrele(vp);
90551fd6380SMike Pritchard 		}
906df8bae1dSRodney W. Grimes 		goto done;
907df8bae1dSRodney W. Grimes 	}
908df8bae1dSRodney W. Grimes 	/*
909df8bae1dSRodney W. Grimes 	 * do it
910df8bae1dSRodney W. Grimes 	 */
91164cc6a13SJohn Baldwin 	sx_slock(&proctree_lock);
912df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
913df8bae1dSRodney W. Grimes 		/*
914df8bae1dSRodney W. Grimes 		 * by process group
915df8bae1dSRodney W. Grimes 		 */
916df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
917df8bae1dSRodney W. Grimes 		if (pg == NULL) {
918ba626c1dSJohn Baldwin 			sx_sunlock(&proctree_lock);
919df8bae1dSRodney W. Grimes 			error = ESRCH;
920df8bae1dSRodney W. Grimes 			goto done;
921df8bae1dSRodney W. Grimes 		}
922f591779bSSeigo Tanimura 		/*
923f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
924ba626c1dSJohn Baldwin 		 * by the proctree_lock rather than pg_mtx.
925f591779bSSeigo Tanimura 		 */
926f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
927400a74bfSPawel Jakub Dawidek 		nfound = 0;
928400a74bfSPawel Jakub Dawidek 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
929400a74bfSPawel Jakub Dawidek 			PROC_LOCK(p);
930e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW ||
931e806d352SJohn Baldwin 			    p_cansee(td, p) != 0) {
932400a74bfSPawel Jakub Dawidek 				PROC_UNLOCK(p);
933400a74bfSPawel Jakub Dawidek 				continue;
934400a74bfSPawel Jakub Dawidek 			}
935400a74bfSPawel Jakub Dawidek 			nfound++;
936df8bae1dSRodney W. Grimes 			if (descend)
937a7ff7443SJohn Baldwin 				ret |= ktrsetchildren(td, p, ops, facs, vp);
938df8bae1dSRodney W. Grimes 			else
939a7ff7443SJohn Baldwin 				ret |= ktrops(td, p, ops, facs, vp);
940400a74bfSPawel Jakub Dawidek 		}
941400a74bfSPawel Jakub Dawidek 		if (nfound == 0) {
942400a74bfSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
943400a74bfSPawel Jakub Dawidek 			error = ESRCH;
944400a74bfSPawel Jakub Dawidek 			goto done;
945400a74bfSPawel Jakub Dawidek 		}
946df8bae1dSRodney W. Grimes 	} else {
947df8bae1dSRodney W. Grimes 		/*
948df8bae1dSRodney W. Grimes 		 * by pid
949df8bae1dSRodney W. Grimes 		 */
950df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
951fe41d17aSJohn Baldwin 		if (p == NULL)
952df8bae1dSRodney W. Grimes 			error = ESRCH;
953fe41d17aSJohn Baldwin 		else
9544eb7c9f6SPawel Jakub Dawidek 			error = p_cansee(td, p);
955b0d9aeddSPawel Jakub Dawidek 		if (error) {
956fe41d17aSJohn Baldwin 			if (p != NULL)
957fe41d17aSJohn Baldwin 				PROC_UNLOCK(p);
958b0d9aeddSPawel Jakub Dawidek 			sx_sunlock(&proctree_lock);
9594eb7c9f6SPawel Jakub Dawidek 			goto done;
960b0d9aeddSPawel Jakub Dawidek 		}
961df8bae1dSRodney W. Grimes 		if (descend)
962a7ff7443SJohn Baldwin 			ret |= ktrsetchildren(td, p, ops, facs, vp);
963df8bae1dSRodney W. Grimes 		else
964a7ff7443SJohn Baldwin 			ret |= ktrops(td, p, ops, facs, vp);
965df8bae1dSRodney W. Grimes 	}
96664cc6a13SJohn Baldwin 	sx_sunlock(&proctree_lock);
967df8bae1dSRodney W. Grimes 	if (!ret)
968df8bae1dSRodney W. Grimes 		error = EPERM;
969df8bae1dSRodney W. Grimes done:
9705050aa86SKonstantin Belousov 	if (vp != NULL)
971a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
9722c255e9dSRobert Watson 	ktrace_exit(td);
973df8bae1dSRodney W. Grimes 	return (error);
97464cc6a13SJohn Baldwin #else /* !KTRACE */
97564cc6a13SJohn Baldwin 	return (ENOSYS);
97664cc6a13SJohn Baldwin #endif /* KTRACE */
977df8bae1dSRodney W. Grimes }
978df8bae1dSRodney W. Grimes 
979e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
980e6c4b9baSPoul-Henning Kamp int
981039644ecSEd Maste sys_utrace(struct thread *td, struct utrace_args *uap)
982e6c4b9baSPoul-Henning Kamp {
983b40ce416SJulian Elischer 
984e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
985ea3fc8e4SJohn Baldwin 	struct ktr_request *req;
9867f05b035SAlfred Perlstein 	void *cp;
987c9e7d28eSJohn Baldwin 	int error;
988e6c4b9baSPoul-Henning Kamp 
989c9e7d28eSJohn Baldwin 	if (!KTRPOINT(td, KTR_USER))
990c9e7d28eSJohn Baldwin 		return (0);
991bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
9920bad156aSAlfred Perlstein 		return (EINVAL);
993a163d034SWarner Losh 	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
994c9e7d28eSJohn Baldwin 	error = copyin(uap->addr, cp, uap->len);
99550c22331SPoul-Henning Kamp 	if (error) {
99650c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
997c9e7d28eSJohn Baldwin 		return (error);
99850c22331SPoul-Henning Kamp 	}
999ea3fc8e4SJohn Baldwin 	req = ktr_getrequest(KTR_USER);
100050c22331SPoul-Henning Kamp 	if (req == NULL) {
100150c22331SPoul-Henning Kamp 		free(cp, M_KTRACE);
1002b10221ffSJoseph Koshy 		return (ENOMEM);
100350c22331SPoul-Henning Kamp 	}
1004d977a583SRobert Watson 	req->ktr_buffer = cp;
1005ea3fc8e4SJohn Baldwin 	req->ktr_header.ktr_len = uap->len;
10062c255e9dSRobert Watson 	ktr_submitrequest(td, req);
1007e6c4b9baSPoul-Henning Kamp 	return (0);
100864cc6a13SJohn Baldwin #else /* !KTRACE */
1009e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
101064cc6a13SJohn Baldwin #endif /* KTRACE */
1011e6c4b9baSPoul-Henning Kamp }
1012e6c4b9baSPoul-Henning Kamp 
1013db6a20e2SGarrett Wollman #ifdef KTRACE
101487b6de2bSPoul-Henning Kamp static int
1015039644ecSEd Maste ktrops(struct thread *td, struct proc *p, int ops, int facs, struct vnode *vp)
1016df8bae1dSRodney W. Grimes {
1017ea3fc8e4SJohn Baldwin 	struct vnode *tracevp = NULL;
1018a5881ea5SJohn Baldwin 	struct ucred *tracecred = NULL;
1019df8bae1dSRodney W. Grimes 
1020fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1021a7ff7443SJohn Baldwin 	if (!ktrcanset(td, p)) {
1022a7ff7443SJohn Baldwin 		PROC_UNLOCK(p);
1023df8bae1dSRodney W. Grimes 		return (0);
1024a7ff7443SJohn Baldwin 	}
1025fe41d17aSJohn Baldwin 	if (p->p_flag & P_WEXIT) {
1026fe41d17aSJohn Baldwin 		/* If the process is exiting, just ignore it. */
1027fe41d17aSJohn Baldwin 		PROC_UNLOCK(p);
1028fe41d17aSJohn Baldwin 		return (1);
1029fe41d17aSJohn Baldwin 	}
1030ea3fc8e4SJohn Baldwin 	mtx_lock(&ktrace_mtx);
1031df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
1032a5881ea5SJohn Baldwin 		if (p->p_tracevp != vp) {
1033df8bae1dSRodney W. Grimes 			/*
1034a7ff7443SJohn Baldwin 			 * if trace file already in use, relinquish below
1035df8bae1dSRodney W. Grimes 			 */
1036a5881ea5SJohn Baldwin 			tracevp = p->p_tracevp;
1037ea3fc8e4SJohn Baldwin 			VREF(vp);
1038a5881ea5SJohn Baldwin 			p->p_tracevp = vp;
1039a5881ea5SJohn Baldwin 		}
1040a5881ea5SJohn Baldwin 		if (p->p_tracecred != td->td_ucred) {
1041a5881ea5SJohn Baldwin 			tracecred = p->p_tracecred;
1042a5881ea5SJohn Baldwin 			p->p_tracecred = crhold(td->td_ucred);
1043df8bae1dSRodney W. Grimes 		}
1044df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
104532f9753cSRobert Watson 		if (priv_check(td, PRIV_KTRACE) == 0)
1046df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
1047df8bae1dSRodney W. Grimes 	} else {
1048df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
1049d680caabSJohn Baldwin 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0)
1050df8bae1dSRodney W. Grimes 			/* no more tracing */
1051d680caabSJohn Baldwin 			ktr_freeproc(p, &tracecred, &tracevp);
1052a7ff7443SJohn Baldwin 	}
1053ea3fc8e4SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
105422ec0406SDmitry Chagin 	if ((p->p_traceflag & KTRFAC_MASK) != 0)
105522ec0406SDmitry Chagin 		ktrprocctor_entered(td, p);
1056a7ff7443SJohn Baldwin 	PROC_UNLOCK(p);
10575050aa86SKonstantin Belousov 	if (tracevp != NULL)
1058ea3fc8e4SJohn Baldwin 		vrele(tracevp);
1059a5881ea5SJohn Baldwin 	if (tracecred != NULL)
1060a5881ea5SJohn Baldwin 		crfree(tracecred);
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 	return (1);
1063df8bae1dSRodney W. Grimes }
1064df8bae1dSRodney W. Grimes 
106587b6de2bSPoul-Henning Kamp static int
1066039644ecSEd Maste ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
1067039644ecSEd Maste     struct vnode *vp)
1068df8bae1dSRodney W. Grimes {
1069039644ecSEd Maste 	struct proc *p;
1070039644ecSEd Maste 	int ret = 0;
1071df8bae1dSRodney W. Grimes 
1072df8bae1dSRodney W. Grimes 	p = top;
1073fe41d17aSJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
107464cc6a13SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
1075df8bae1dSRodney W. Grimes 	for (;;) {
1076a7ff7443SJohn Baldwin 		ret |= ktrops(td, p, ops, facs, vp);
1077df8bae1dSRodney W. Grimes 		/*
1078df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
1079df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
1080df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
1081df8bae1dSRodney W. Grimes 		 */
10822e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
10832e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
1084df8bae1dSRodney W. Grimes 		else for (;;) {
108564cc6a13SJohn Baldwin 			if (p == top)
1086df8bae1dSRodney W. Grimes 				return (ret);
10872e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
10882e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
1089df8bae1dSRodney W. Grimes 				break;
1090df8bae1dSRodney W. Grimes 			}
1091b75356e1SJeffrey Hsu 			p = p->p_pptr;
1092df8bae1dSRodney W. Grimes 		}
1093fe41d17aSJohn Baldwin 		PROC_LOCK(p);
1094df8bae1dSRodney W. Grimes 	}
1095df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
1096df8bae1dSRodney W. Grimes }
1097df8bae1dSRodney W. Grimes 
109887b6de2bSPoul-Henning Kamp static void
10992c255e9dSRobert Watson ktr_writerequest(struct thread *td, struct ktr_request *req)
1100df8bae1dSRodney W. Grimes {
1101ea3fc8e4SJohn Baldwin 	struct ktr_header *kth;
1102ea3fc8e4SJohn Baldwin 	struct vnode *vp;
1103ea3fc8e4SJohn Baldwin 	struct proc *p;
1104ea3fc8e4SJohn Baldwin 	struct ucred *cred;
1105df8bae1dSRodney W. Grimes 	struct uio auio;
1106ea3fc8e4SJohn Baldwin 	struct iovec aiov[3];
1107f2a2857bSKirk McKusick 	struct mount *mp;
1108ea3fc8e4SJohn Baldwin 	int datalen, buflen, vrele_count;
11095050aa86SKonstantin Belousov 	int error;
1110df8bae1dSRodney W. Grimes 
11112c255e9dSRobert Watson 	/*
11122c255e9dSRobert Watson 	 * We hold the vnode and credential for use in I/O in case ktrace is
11132c255e9dSRobert Watson 	 * disabled on the process as we write out the request.
11142c255e9dSRobert Watson 	 *
11152c255e9dSRobert Watson 	 * XXXRW: This is not ideal: we could end up performing a write after
11162c255e9dSRobert Watson 	 * the vnode has been closed.
11172c255e9dSRobert Watson 	 */
11182c255e9dSRobert Watson 	mtx_lock(&ktrace_mtx);
11192c255e9dSRobert Watson 	vp = td->td_proc->p_tracevp;
11202c255e9dSRobert Watson 	cred = td->td_proc->p_tracecred;
11212c255e9dSRobert Watson 
1122ea3fc8e4SJohn Baldwin 	/*
1123ea3fc8e4SJohn Baldwin 	 * If vp is NULL, the vp has been cleared out from under this
11242c255e9dSRobert Watson 	 * request, so just drop it.  Make sure the credential and vnode are
11252c255e9dSRobert Watson 	 * in sync: we should have both or neither.
1126ea3fc8e4SJohn Baldwin 	 */
11272c255e9dSRobert Watson 	if (vp == NULL) {
11282c255e9dSRobert Watson 		KASSERT(cred == NULL, ("ktr_writerequest: cred != NULL"));
1129118258f5SBjoern A. Zeeb 		mtx_unlock(&ktrace_mtx);
1130df8bae1dSRodney W. Grimes 		return;
11312c255e9dSRobert Watson 	}
1132118258f5SBjoern A. Zeeb 	VREF(vp);
11332c255e9dSRobert Watson 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
1134118258f5SBjoern A. Zeeb 	crhold(cred);
1135118258f5SBjoern A. Zeeb 	mtx_unlock(&ktrace_mtx);
11362c255e9dSRobert Watson 
1137ea3fc8e4SJohn Baldwin 	kth = &req->ktr_header;
113802abd400SPedro F. Giffuni 	KASSERT(((u_short)kth->ktr_type & ~KTR_DROP) < nitems(data_lengths),
1139a56be37eSJohn Baldwin 	    ("data_lengths array overflow"));
11408b149b51SJohn Baldwin 	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
1141ea3fc8e4SJohn Baldwin 	buflen = kth->ktr_len;
1142df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
1143df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
1144df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
1145df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
1146df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
1147df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
1148df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
1149df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
1150ea3fc8e4SJohn Baldwin 	auio.uio_td = td;
1151ea3fc8e4SJohn Baldwin 	if (datalen != 0) {
1152ea3fc8e4SJohn Baldwin 		aiov[1].iov_base = (caddr_t)&req->ktr_data;
1153ea3fc8e4SJohn Baldwin 		aiov[1].iov_len = datalen;
1154ea3fc8e4SJohn Baldwin 		auio.uio_resid += datalen;
1155df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
1156ea3fc8e4SJohn Baldwin 		kth->ktr_len += datalen;
1157ea3fc8e4SJohn Baldwin 	}
1158ea3fc8e4SJohn Baldwin 	if (buflen != 0) {
1159d977a583SRobert Watson 		KASSERT(req->ktr_buffer != NULL, ("ktrace: nothing to write"));
1160d977a583SRobert Watson 		aiov[auio.uio_iovcnt].iov_base = req->ktr_buffer;
1161ea3fc8e4SJohn Baldwin 		aiov[auio.uio_iovcnt].iov_len = buflen;
1162ea3fc8e4SJohn Baldwin 		auio.uio_resid += buflen;
1163ea3fc8e4SJohn Baldwin 		auio.uio_iovcnt++;
1164b92584a6SJohn Baldwin 	}
11652c255e9dSRobert Watson 
1166f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
1167cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1168467a273cSRobert Watson #ifdef MAC
116930d239bcSRobert Watson 	error = mac_vnode_check_write(cred, NOCRED, vp);
1170467a273cSRobert Watson 	if (error == 0)
1171467a273cSRobert Watson #endif
1172ea3fc8e4SJohn Baldwin 		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
117322db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
1174f2a2857bSKirk McKusick 	vn_finished_write(mp);
1175118258f5SBjoern A. Zeeb 	crfree(cred);
1176118258f5SBjoern A. Zeeb 	if (!error) {
1177704c9f00SJohn Baldwin 		vrele(vp);
1178df8bae1dSRodney W. Grimes 		return;
1179118258f5SBjoern A. Zeeb 	}
1180118258f5SBjoern A. Zeeb 
1181df8bae1dSRodney W. Grimes 	/*
1182ea3fc8e4SJohn Baldwin 	 * If error encountered, give up tracing on this vnode.  We defer
1183ea3fc8e4SJohn Baldwin 	 * all the vrele()'s on the vnode until after we are finished walking
1184ea3fc8e4SJohn Baldwin 	 * the various lists to avoid needlessly holding locks.
1185118258f5SBjoern A. Zeeb 	 * NB: at this point we still hold the vnode reference that must
1186118258f5SBjoern A. Zeeb 	 * not go away as we need the valid vnode to compare with. Thus let
1187118258f5SBjoern A. Zeeb 	 * vrele_count start at 1 and the reference will be freed
1188118258f5SBjoern A. Zeeb 	 * by the loop at the end after our last use of vp.
1189df8bae1dSRodney W. Grimes 	 */
1190df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
1191df8bae1dSRodney W. Grimes 	    error);
1192118258f5SBjoern A. Zeeb 	vrele_count = 1;
1193ea3fc8e4SJohn Baldwin 	/*
1194ea3fc8e4SJohn Baldwin 	 * First, clear this vnode from being used by any processes in the
1195ea3fc8e4SJohn Baldwin 	 * system.
1196ea3fc8e4SJohn Baldwin 	 * XXX - If one process gets an EPERM writing to the vnode, should
1197ea3fc8e4SJohn Baldwin 	 * we really do this?  Other processes might have suitable
1198ea3fc8e4SJohn Baldwin 	 * credentials for the operation.
1199ea3fc8e4SJohn Baldwin 	 */
1200a5881ea5SJohn Baldwin 	cred = NULL;
12011005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12024f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1203ea3fc8e4SJohn Baldwin 		PROC_LOCK(p);
1204a5881ea5SJohn Baldwin 		if (p->p_tracevp == vp) {
1205ea3fc8e4SJohn Baldwin 			mtx_lock(&ktrace_mtx);
1206d680caabSJohn Baldwin 			ktr_freeproc(p, &cred, NULL);
1207ea3fc8e4SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
1208ea3fc8e4SJohn Baldwin 			vrele_count++;
1209df8bae1dSRodney W. Grimes 		}
1210ea3fc8e4SJohn Baldwin 		PROC_UNLOCK(p);
1211a5881ea5SJohn Baldwin 		if (cred != NULL) {
1212a5881ea5SJohn Baldwin 			crfree(cred);
1213a5881ea5SJohn Baldwin 			cred = NULL;
1214a5881ea5SJohn Baldwin 		}
1215df8bae1dSRodney W. Grimes 	}
12161005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
12172c255e9dSRobert Watson 
1218ea3fc8e4SJohn Baldwin 	while (vrele_count-- > 0)
1219ea3fc8e4SJohn Baldwin 		vrele(vp);
1220df8bae1dSRodney W. Grimes }
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes /*
1223df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
1224df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
1225df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
1226df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
1227df8bae1dSRodney W. Grimes  * so, only root may further change it.
1228df8bae1dSRodney W. Grimes  */
122987b6de2bSPoul-Henning Kamp static int
1230039644ecSEd Maste ktrcanset(struct thread *td, struct proc *targetp)
1231df8bae1dSRodney W. Grimes {
1232df8bae1dSRodney W. Grimes 
1233a7ff7443SJohn Baldwin 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
1234a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
123532f9753cSRobert Watson 	    priv_check(td, PRIV_KTRACE))
123675c13541SPoul-Henning Kamp 		return (0);
1237a0f75161SRobert Watson 
1238f44d9e24SJohn Baldwin 	if (p_candebug(td, targetp) != 0)
1239a0f75161SRobert Watson 		return (0);
1240a0f75161SRobert Watson 
1241df8bae1dSRodney W. Grimes 	return (1);
1242df8bae1dSRodney W. Grimes }
1243df8bae1dSRodney W. Grimes 
1244db6a20e2SGarrett Wollman #endif /* KTRACE */
1245