xref: /freebsd/sys/kern/kern_ktrace.c (revision a854ed98931b2e365eddd24cd2a1bb53a3f1828f)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. 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
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37db6a20e2SGarrett Wollman #include "opt_ktrace.h"
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40f23b4c91SGarrett Wollman #include <sys/systm.h>
41fb919e4dSMark Murray #include <sys/lock.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43d2d3e875SBruce Evans #include <sys/sysproto.h>
441c5bb3eaSPeter Wemm #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/proc.h>
463ac4d1efSBruce Evans #include <sys/fcntl.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
511005a129SJohn Baldwin #include <sys/sx.h>
52df8bae1dSRodney W. Grimes #include <sys/syslog.h>
5391421ba2SRobert Watson #include <sys/jail.h>
54df8bae1dSRodney W. Grimes 
55a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
5655166637SPoul-Henning Kamp 
57db6a20e2SGarrett Wollman #ifdef KTRACE
5887b6de2bSPoul-Henning Kamp static struct ktr_header *ktrgetheader __P((int type));
5942ebfbf2SBrian Feldman static void ktrwrite __P((struct vnode *, struct ktr_header *, struct uio *));
6087b6de2bSPoul-Henning Kamp static int ktrcanset __P((struct proc *,struct proc *));
6187b6de2bSPoul-Henning Kamp static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *));
6287b6de2bSPoul-Henning Kamp static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *));
6398d93822SBruce Evans 
6487b6de2bSPoul-Henning Kamp 
6587b6de2bSPoul-Henning Kamp static struct ktr_header *
66df8bae1dSRodney W. Grimes ktrgetheader(type)
67df8bae1dSRodney W. Grimes 	int type;
68df8bae1dSRodney W. Grimes {
69df8bae1dSRodney W. Grimes 	register struct ktr_header *kth;
70df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes 	MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
73d1c4c866SPoul-Henning Kamp 		M_KTRACE, M_WAITOK);
74df8bae1dSRodney W. Grimes 	kth->ktr_type = type;
75df8bae1dSRodney W. Grimes 	microtime(&kth->ktr_time);
76df8bae1dSRodney W. Grimes 	kth->ktr_pid = p->p_pid;
7785fce0e4SMarcel Moolenaar 	bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
78df8bae1dSRodney W. Grimes 	return (kth);
79df8bae1dSRodney W. Grimes }
80df8bae1dSRodney W. Grimes 
81356861dbSMatthew Dillon /*
82356861dbSMatthew Dillon  * MPSAFE
83356861dbSMatthew Dillon  */
8426f9a767SRodney W. Grimes void
85df8bae1dSRodney W. Grimes ktrsyscall(vp, code, narg, args)
86df8bae1dSRodney W. Grimes 	struct vnode *vp;
8771ddfdbbSDmitrij Tejblum 	int code, narg;
8871ddfdbbSDmitrij Tejblum 	register_t args[];
89df8bae1dSRodney W. Grimes {
90df8bae1dSRodney W. Grimes 	struct	ktr_header *kth;
91df8bae1dSRodney W. Grimes 	struct	ktr_syscall *ktp;
9271ddfdbbSDmitrij Tejblum 	register int len = offsetof(struct ktr_syscall, ktr_args) +
9371ddfdbbSDmitrij Tejblum 	    (narg * sizeof(register_t));
94df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
9571ddfdbbSDmitrij Tejblum 	register_t *argp;
9671ddfdbbSDmitrij Tejblum 	int i;
97df8bae1dSRodney W. Grimes 
98356861dbSMatthew Dillon 	mtx_lock(&Giant);
99df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
100df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_SYSCALL);
101d1c4c866SPoul-Henning Kamp 	MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
102df8bae1dSRodney W. Grimes 	ktp->ktr_code = code;
103df8bae1dSRodney W. Grimes 	ktp->ktr_narg = narg;
10471ddfdbbSDmitrij Tejblum 	argp = &ktp->ktr_args[0];
105df8bae1dSRodney W. Grimes 	for (i = 0; i < narg; i++)
106df8bae1dSRodney W. Grimes 		*argp++ = args[i];
10762ae6c89SJason Evans 	kth->ktr_buffer = (caddr_t)ktp;
108df8bae1dSRodney W. Grimes 	kth->ktr_len = len;
10942ebfbf2SBrian Feldman 	ktrwrite(vp, kth, NULL);
110d1c4c866SPoul-Henning Kamp 	FREE(ktp, M_KTRACE);
111d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
112df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
113356861dbSMatthew Dillon 	mtx_unlock(&Giant);
114df8bae1dSRodney W. Grimes }
115df8bae1dSRodney W. Grimes 
116356861dbSMatthew Dillon /*
117356861dbSMatthew Dillon  * MPSAFE
118356861dbSMatthew Dillon  */
11926f9a767SRodney W. Grimes void
120df8bae1dSRodney W. Grimes ktrsysret(vp, code, error, retval)
121df8bae1dSRodney W. Grimes 	struct vnode *vp;
12271ddfdbbSDmitrij Tejblum 	int code, error;
12371ddfdbbSDmitrij Tejblum 	register_t retval;
124df8bae1dSRodney W. Grimes {
125df8bae1dSRodney W. Grimes 	struct ktr_header *kth;
126df8bae1dSRodney W. Grimes 	struct ktr_sysret ktp;
127df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
128df8bae1dSRodney W. Grimes 
129356861dbSMatthew Dillon 	mtx_lock(&Giant);
130df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
131df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_SYSRET);
132df8bae1dSRodney W. Grimes 	ktp.ktr_code = code;
133df8bae1dSRodney W. Grimes 	ktp.ktr_error = error;
134df8bae1dSRodney W. Grimes 	ktp.ktr_retval = retval;		/* what about val2 ? */
135df8bae1dSRodney W. Grimes 
13662ae6c89SJason Evans 	kth->ktr_buffer = (caddr_t)&ktp;
137df8bae1dSRodney W. Grimes 	kth->ktr_len = sizeof(struct ktr_sysret);
138df8bae1dSRodney W. Grimes 
13942ebfbf2SBrian Feldman 	ktrwrite(vp, kth, NULL);
140d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
141df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
142356861dbSMatthew Dillon 	mtx_unlock(&Giant);
143df8bae1dSRodney W. Grimes }
144df8bae1dSRodney W. Grimes 
14526f9a767SRodney W. Grimes void
146df8bae1dSRodney W. Grimes ktrnamei(vp, path)
147df8bae1dSRodney W. Grimes 	struct vnode *vp;
148df8bae1dSRodney W. Grimes 	char *path;
149df8bae1dSRodney W. Grimes {
150df8bae1dSRodney W. Grimes 	struct ktr_header *kth;
151df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
152df8bae1dSRodney W. Grimes 
15379deba82SMatthew Dillon 	/*
15479deba82SMatthew Dillon 	 * don't let p_tracep get ripped out from under us
15579deba82SMatthew Dillon 	 */
15679deba82SMatthew Dillon 	if (vp)
15779deba82SMatthew Dillon 		VREF(vp);
158df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
159df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_NAMEI);
160df8bae1dSRodney W. Grimes 	kth->ktr_len = strlen(path);
16162ae6c89SJason Evans 	kth->ktr_buffer = path;
162df8bae1dSRodney W. Grimes 
16342ebfbf2SBrian Feldman 	ktrwrite(vp, kth, NULL);
16479deba82SMatthew Dillon 	if (vp)
16579deba82SMatthew Dillon 		vrele(vp);
166d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
167df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
17026f9a767SRodney W. Grimes void
17142ebfbf2SBrian Feldman ktrgenio(vp, fd, rw, uio, error)
172df8bae1dSRodney W. Grimes 	struct vnode *vp;
173df8bae1dSRodney W. Grimes 	int fd;
174df8bae1dSRodney W. Grimes 	enum uio_rw rw;
17542ebfbf2SBrian Feldman 	struct uio *uio;
17642ebfbf2SBrian Feldman 	int error;
177df8bae1dSRodney W. Grimes {
178df8bae1dSRodney W. Grimes 	struct ktr_header *kth;
17942ebfbf2SBrian Feldman 	struct ktr_genio ktg;
180df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
181df8bae1dSRodney W. Grimes 
182df8bae1dSRodney W. Grimes 	if (error)
183df8bae1dSRodney W. Grimes 		return;
18479deba82SMatthew Dillon 	/*
18579deba82SMatthew Dillon 	 * don't let p_tracep get ripped out from under us
18679deba82SMatthew Dillon 	 */
18779deba82SMatthew Dillon 	if (vp)
18879deba82SMatthew Dillon 		VREF(vp);
189df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
190df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_GENIO);
19142ebfbf2SBrian Feldman 	ktg.ktr_fd = fd;
19242ebfbf2SBrian Feldman 	ktg.ktr_rw = rw;
19362ae6c89SJason Evans 	kth->ktr_buffer = (caddr_t)&ktg;
19442ebfbf2SBrian Feldman 	kth->ktr_len = sizeof(struct ktr_genio);
19542ebfbf2SBrian Feldman 	uio->uio_offset = 0;
1969d1cfdceSBrian Feldman 	uio->uio_rw = UIO_WRITE;
197df8bae1dSRodney W. Grimes 
19842ebfbf2SBrian Feldman 	ktrwrite(vp, kth, uio);
19979deba82SMatthew Dillon 	if (vp)
20079deba82SMatthew Dillon 		vrele(vp);
201d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
202df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
203df8bae1dSRodney W. Grimes }
204df8bae1dSRodney W. Grimes 
20526f9a767SRodney W. Grimes void
206df8bae1dSRodney W. Grimes ktrpsig(vp, sig, action, mask, code)
207df8bae1dSRodney W. Grimes 	struct vnode *vp;
208a93fdaacSMarcel Moolenaar 	int sig;
209df8bae1dSRodney W. Grimes 	sig_t action;
2102c42a146SMarcel Moolenaar 	sigset_t *mask;
211a93fdaacSMarcel Moolenaar 	int code;
212df8bae1dSRodney W. Grimes {
213df8bae1dSRodney W. Grimes 	struct ktr_header *kth;
214df8bae1dSRodney W. Grimes 	struct ktr_psig	kp;
215df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
216df8bae1dSRodney W. Grimes 
21779deba82SMatthew Dillon 	/*
21879deba82SMatthew Dillon 	 * don't let vp get ripped out from under us
21979deba82SMatthew Dillon 	 */
22079deba82SMatthew Dillon 	if (vp)
22179deba82SMatthew Dillon 		VREF(vp);
222df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
223df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_PSIG);
224df8bae1dSRodney W. Grimes 	kp.signo = (char)sig;
225df8bae1dSRodney W. Grimes 	kp.action = action;
2262c42a146SMarcel Moolenaar 	kp.mask = *mask;
227df8bae1dSRodney W. Grimes 	kp.code = code;
22862ae6c89SJason Evans 	kth->ktr_buffer = (caddr_t)&kp;
229df8bae1dSRodney W. Grimes 	kth->ktr_len = sizeof (struct ktr_psig);
230df8bae1dSRodney W. Grimes 
23142ebfbf2SBrian Feldman 	ktrwrite(vp, kth, NULL);
23279deba82SMatthew Dillon 	if (vp)
23379deba82SMatthew Dillon 		vrele(vp);
234d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
235df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
236df8bae1dSRodney W. Grimes }
237df8bae1dSRodney W. Grimes 
23826f9a767SRodney W. Grimes void
239df8bae1dSRodney W. Grimes ktrcsw(vp, out, user)
240df8bae1dSRodney W. Grimes 	struct vnode *vp;
241df8bae1dSRodney W. Grimes 	int out, user;
242df8bae1dSRodney W. Grimes {
243df8bae1dSRodney W. Grimes 	struct ktr_header *kth;
244df8bae1dSRodney W. Grimes 	struct	ktr_csw kc;
245df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
246df8bae1dSRodney W. Grimes 
24779deba82SMatthew Dillon 	/*
24879deba82SMatthew Dillon 	 * don't let vp get ripped out from under us
24979deba82SMatthew Dillon 	 */
25079deba82SMatthew Dillon 	if (vp)
25179deba82SMatthew Dillon 		VREF(vp);
252df8bae1dSRodney W. Grimes 	p->p_traceflag |= KTRFAC_ACTIVE;
253df8bae1dSRodney W. Grimes 	kth = ktrgetheader(KTR_CSW);
254df8bae1dSRodney W. Grimes 	kc.out = out;
255df8bae1dSRodney W. Grimes 	kc.user = user;
25662ae6c89SJason Evans 	kth->ktr_buffer = (caddr_t)&kc;
257df8bae1dSRodney W. Grimes 	kth->ktr_len = sizeof (struct ktr_csw);
258df8bae1dSRodney W. Grimes 
25942ebfbf2SBrian Feldman 	ktrwrite(vp, kth, NULL);
26079deba82SMatthew Dillon 	if (vp)
26179deba82SMatthew Dillon 		vrele(vp);
262d1c4c866SPoul-Henning Kamp 	FREE(kth, M_KTRACE);
263df8bae1dSRodney W. Grimes 	p->p_traceflag &= ~KTRFAC_ACTIVE;
264df8bae1dSRodney W. Grimes }
265db6a20e2SGarrett Wollman #endif
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes /* Interface and common routines */
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * ktrace system call
271df8bae1dSRodney W. Grimes  */
272d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
273df8bae1dSRodney W. Grimes struct ktrace_args {
274df8bae1dSRodney W. Grimes 	char	*fname;
275df8bae1dSRodney W. Grimes 	int	ops;
276df8bae1dSRodney W. Grimes 	int	facs;
277df8bae1dSRodney W. Grimes 	int	pid;
278df8bae1dSRodney W. Grimes };
279d2d3e875SBruce Evans #endif
280df8bae1dSRodney W. Grimes /* ARGSUSED */
28126f9a767SRodney W. Grimes int
282b40ce416SJulian Elischer ktrace(td, uap)
283b40ce416SJulian Elischer 	struct thread *td;
284df8bae1dSRodney W. Grimes 	register struct ktrace_args *uap;
285df8bae1dSRodney W. Grimes {
286db6a20e2SGarrett Wollman #ifdef KTRACE
287b40ce416SJulian Elischer 	struct proc *curp = td->td_proc;
288df8bae1dSRodney W. Grimes 	register struct vnode *vp = NULL;
289df8bae1dSRodney W. Grimes 	register struct proc *p;
290df8bae1dSRodney W. Grimes 	struct pgrp *pg;
291df8bae1dSRodney W. Grimes 	int facs = uap->facs & ~KTRFAC_ROOT;
292df8bae1dSRodney W. Grimes 	int ops = KTROP(uap->ops);
293df8bae1dSRodney W. Grimes 	int descend = uap->ops & KTRFLAG_DESCEND;
294df8bae1dSRodney W. Grimes 	int ret = 0;
295e6796b67SKirk McKusick 	int flags, error = 0;
296df8bae1dSRodney W. Grimes 	struct nameidata nd;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	curp->p_traceflag |= KTRFAC_ACTIVE;
299df8bae1dSRodney W. Grimes 	if (ops != KTROP_CLEAR) {
300df8bae1dSRodney W. Grimes 		/*
301df8bae1dSRodney W. Grimes 		 * an operation which requires a file argument.
302df8bae1dSRodney W. Grimes 		 */
303b40ce416SJulian Elischer 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
304e6796b67SKirk McKusick 		flags = FREAD | FWRITE | O_NOFOLLOW;
305e6796b67SKirk McKusick 		error = vn_open(&nd, &flags, 0);
306797f2d22SPoul-Henning Kamp 		if (error) {
307df8bae1dSRodney W. Grimes 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
308df8bae1dSRodney W. Grimes 			return (error);
309df8bae1dSRodney W. Grimes 		}
310762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
311df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
312b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
313df8bae1dSRodney W. Grimes 		if (vp->v_type != VREG) {
314a854ed98SJohn Baldwin 			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
315df8bae1dSRodney W. Grimes 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
316df8bae1dSRodney W. Grimes 			return (EACCES);
317df8bae1dSRodney W. Grimes 		}
318df8bae1dSRodney W. Grimes 	}
319df8bae1dSRodney W. Grimes 	/*
32079deba82SMatthew Dillon 	 * Clear all uses of the tracefile.
321df8bae1dSRodney W. Grimes 	 */
322df8bae1dSRodney W. Grimes 	if (ops == KTROP_CLEARFILE) {
3231005a129SJohn Baldwin 		sx_slock(&allproc_lock);
3242e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &allproc, p_list) {
325df8bae1dSRodney W. Grimes 			if (p->p_tracep == vp) {
32679deba82SMatthew Dillon 				if (ktrcanset(curp, p) && p->p_tracep == vp) {
327df8bae1dSRodney W. Grimes 					p->p_tracep = NULL;
328df8bae1dSRodney W. Grimes 					p->p_traceflag = 0;
329df8bae1dSRodney W. Grimes 					(void) vn_close(vp, FREAD|FWRITE,
330a854ed98SJohn Baldwin 						td->td_ucred, td);
33179deba82SMatthew Dillon 				} else {
332df8bae1dSRodney W. Grimes 					error = EPERM;
333df8bae1dSRodney W. Grimes 				}
334df8bae1dSRodney W. Grimes 			}
33579deba82SMatthew Dillon 		}
3361005a129SJohn Baldwin 		sx_sunlock(&allproc_lock);
337df8bae1dSRodney W. Grimes 		goto done;
338df8bae1dSRodney W. Grimes 	}
339df8bae1dSRodney W. Grimes 	/*
340df8bae1dSRodney W. Grimes 	 * need something to (un)trace (XXX - why is this here?)
341df8bae1dSRodney W. Grimes 	 */
342df8bae1dSRodney W. Grimes 	if (!facs) {
343df8bae1dSRodney W. Grimes 		error = EINVAL;
344df8bae1dSRodney W. Grimes 		goto done;
345df8bae1dSRodney W. Grimes 	}
346df8bae1dSRodney W. Grimes 	/*
347df8bae1dSRodney W. Grimes 	 * do it
348df8bae1dSRodney W. Grimes 	 */
349df8bae1dSRodney W. Grimes 	if (uap->pid < 0) {
350df8bae1dSRodney W. Grimes 		/*
351df8bae1dSRodney W. Grimes 		 * by process group
352df8bae1dSRodney W. Grimes 		 */
353f591779bSSeigo Tanimura 		PGRPSESS_SLOCK();
354df8bae1dSRodney W. Grimes 		pg = pgfind(-uap->pid);
355df8bae1dSRodney W. Grimes 		if (pg == NULL) {
356f591779bSSeigo Tanimura 			PGRPSESS_SUNLOCK();
357df8bae1dSRodney W. Grimes 			error = ESRCH;
358df8bae1dSRodney W. Grimes 			goto done;
359df8bae1dSRodney W. Grimes 		}
360f591779bSSeigo Tanimura 		/*
361f591779bSSeigo Tanimura 		 * ktrops() may call vrele(). Lock pg_members
362f591779bSSeigo Tanimura 		 * by the pgrpsess_lock rather than pg_mtx.
363f591779bSSeigo Tanimura 		 */
364f591779bSSeigo Tanimura 		PGRP_UNLOCK(pg);
3652e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(p, &pg->pg_members, p_pglist)
366df8bae1dSRodney W. Grimes 			if (descend)
367df8bae1dSRodney W. Grimes 				ret |= ktrsetchildren(curp, p, ops, facs, vp);
368df8bae1dSRodney W. Grimes 			else
369df8bae1dSRodney W. Grimes 				ret |= ktrops(curp, p, ops, facs, vp);
370f591779bSSeigo Tanimura 		PGRPSESS_SUNLOCK();
371df8bae1dSRodney W. Grimes 	} else {
372df8bae1dSRodney W. Grimes 		/*
373df8bae1dSRodney W. Grimes 		 * by pid
374df8bae1dSRodney W. Grimes 		 */
375df8bae1dSRodney W. Grimes 		p = pfind(uap->pid);
376df8bae1dSRodney W. Grimes 		if (p == NULL) {
377df8bae1dSRodney W. Grimes 			error = ESRCH;
378df8bae1dSRodney W. Grimes 			goto done;
379df8bae1dSRodney W. Grimes 		}
38033a9ed9dSJohn Baldwin 		PROC_UNLOCK(p);
381df8bae1dSRodney W. Grimes 		if (descend)
382df8bae1dSRodney W. Grimes 			ret |= ktrsetchildren(curp, p, ops, facs, vp);
383df8bae1dSRodney W. Grimes 		else
384df8bae1dSRodney W. Grimes 			ret |= ktrops(curp, p, ops, facs, vp);
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 	if (!ret)
387df8bae1dSRodney W. Grimes 		error = EPERM;
388df8bae1dSRodney W. Grimes done:
389df8bae1dSRodney W. Grimes 	if (vp != NULL)
390a854ed98SJohn Baldwin 		(void) vn_close(vp, FWRITE, td->td_ucred, td);
391df8bae1dSRodney W. Grimes 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
392df8bae1dSRodney W. Grimes 	return (error);
393db6a20e2SGarrett Wollman #else
394db6a20e2SGarrett Wollman 	return ENOSYS;
395db6a20e2SGarrett Wollman #endif
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398e6c4b9baSPoul-Henning Kamp /*
399e6c4b9baSPoul-Henning Kamp  * utrace system call
400e6c4b9baSPoul-Henning Kamp  */
401e6c4b9baSPoul-Henning Kamp /* ARGSUSED */
402e6c4b9baSPoul-Henning Kamp int
403b40ce416SJulian Elischer utrace(td, uap)
404b40ce416SJulian Elischer 	struct thread *td;
405e6c4b9baSPoul-Henning Kamp 	register struct utrace_args *uap;
406e6c4b9baSPoul-Henning Kamp {
407b40ce416SJulian Elischer 
408e6c4b9baSPoul-Henning Kamp #ifdef KTRACE
409e6c4b9baSPoul-Henning Kamp 	struct ktr_header *kth;
410e6c4b9baSPoul-Henning Kamp 	struct proc *p = curproc;	/* XXX */
41179deba82SMatthew Dillon 	struct vnode *vp;
412e6c4b9baSPoul-Henning Kamp 	register caddr_t cp;
413e6c4b9baSPoul-Henning Kamp 
414e6c4b9baSPoul-Henning Kamp 	if (!KTRPOINT(p, KTR_USER))
415e6c4b9baSPoul-Henning Kamp 		return (0);
416bdfa4f04SAlfred Perlstein 	if (uap->len > KTR_USER_MAXLEN)
4170bad156aSAlfred Perlstein 		return (EINVAL);
418e6c4b9baSPoul-Henning Kamp 	p->p_traceflag |= KTRFAC_ACTIVE;
41979deba82SMatthew Dillon 	if ((vp = p->p_tracep) != NULL)
42079deba82SMatthew Dillon 		VREF(vp);
421e6c4b9baSPoul-Henning Kamp 	kth = ktrgetheader(KTR_USER);
422d920a829SPoul-Henning Kamp 	MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
423e6c4b9baSPoul-Henning Kamp 	if (!copyin(uap->addr, cp, uap->len)) {
42462ae6c89SJason Evans 		kth->ktr_buffer = cp;
425d920a829SPoul-Henning Kamp 		kth->ktr_len = uap->len;
42679deba82SMatthew Dillon 		ktrwrite(vp, kth, NULL);
427e6c4b9baSPoul-Henning Kamp 	}
42879deba82SMatthew Dillon 	if (vp)
42979deba82SMatthew Dillon 		vrele(vp);
430e6c4b9baSPoul-Henning Kamp 	FREE(kth, M_KTRACE);
431d920a829SPoul-Henning Kamp 	FREE(cp, M_KTRACE);
432e6c4b9baSPoul-Henning Kamp 	p->p_traceflag &= ~KTRFAC_ACTIVE;
433e6c4b9baSPoul-Henning Kamp 
434e6c4b9baSPoul-Henning Kamp 	return (0);
435e6c4b9baSPoul-Henning Kamp #else
436e6c4b9baSPoul-Henning Kamp 	return (ENOSYS);
437e6c4b9baSPoul-Henning Kamp #endif
438e6c4b9baSPoul-Henning Kamp }
439e6c4b9baSPoul-Henning Kamp 
440db6a20e2SGarrett Wollman #ifdef KTRACE
44187b6de2bSPoul-Henning Kamp static int
442df8bae1dSRodney W. Grimes ktrops(curp, p, ops, facs, vp)
443df8bae1dSRodney W. Grimes 	struct proc *p, *curp;
444df8bae1dSRodney W. Grimes 	int ops, facs;
445df8bae1dSRodney W. Grimes 	struct vnode *vp;
446df8bae1dSRodney W. Grimes {
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	if (!ktrcanset(curp, p))
449df8bae1dSRodney W. Grimes 		return (0);
450df8bae1dSRodney W. Grimes 	if (ops == KTROP_SET) {
451df8bae1dSRodney W. Grimes 		if (p->p_tracep != vp) {
45279deba82SMatthew Dillon 			struct vnode *vtmp;
45379deba82SMatthew Dillon 
454df8bae1dSRodney W. Grimes 			/*
455df8bae1dSRodney W. Grimes 			 * if trace file already in use, relinquish
456df8bae1dSRodney W. Grimes 			 */
457df8bae1dSRodney W. Grimes 			VREF(vp);
45879deba82SMatthew Dillon 			while ((vtmp = p->p_tracep) != NULL) {
45979deba82SMatthew Dillon 				p->p_tracep = NULL;
46079deba82SMatthew Dillon 				vrele(vtmp);
46179deba82SMatthew Dillon 			}
462df8bae1dSRodney W. Grimes 			p->p_tracep = vp;
463df8bae1dSRodney W. Grimes 		}
464df8bae1dSRodney W. Grimes 		p->p_traceflag |= facs;
465df8bae1dSRodney W. Grimes 		if (curp->p_ucred->cr_uid == 0)
466df8bae1dSRodney W. Grimes 			p->p_traceflag |= KTRFAC_ROOT;
467df8bae1dSRodney W. Grimes 	} else {
468df8bae1dSRodney W. Grimes 		/* KTROP_CLEAR */
469df8bae1dSRodney W. Grimes 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
47079deba82SMatthew Dillon 			struct vnode *vtmp;
47179deba82SMatthew Dillon 
472df8bae1dSRodney W. Grimes 			/* no more tracing */
473df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
47479deba82SMatthew Dillon 			if ((vtmp = p->p_tracep) != NULL) {
475df8bae1dSRodney W. Grimes 				p->p_tracep = NULL;
47679deba82SMatthew Dillon 				vrele(vtmp);
477df8bae1dSRodney W. Grimes 			}
478df8bae1dSRodney W. Grimes 		}
479df8bae1dSRodney W. Grimes 	}
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 	return (1);
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
48487b6de2bSPoul-Henning Kamp static int
485df8bae1dSRodney W. Grimes ktrsetchildren(curp, top, ops, facs, vp)
486df8bae1dSRodney W. Grimes 	struct proc *curp, *top;
487df8bae1dSRodney W. Grimes 	int ops, facs;
488df8bae1dSRodney W. Grimes 	struct vnode *vp;
489df8bae1dSRodney W. Grimes {
490df8bae1dSRodney W. Grimes 	register struct proc *p;
491df8bae1dSRodney W. Grimes 	register int ret = 0;
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	p = top;
4941005a129SJohn Baldwin 	sx_slock(&proctree_lock);
495df8bae1dSRodney W. Grimes 	for (;;) {
496df8bae1dSRodney W. Grimes 		ret |= ktrops(curp, p, ops, facs, vp);
497df8bae1dSRodney W. Grimes 		/*
498df8bae1dSRodney W. Grimes 		 * If this process has children, descend to them next,
499df8bae1dSRodney W. Grimes 		 * otherwise do any siblings, and if done with this level,
500df8bae1dSRodney W. Grimes 		 * follow back up the tree (but not past top).
501df8bae1dSRodney W. Grimes 		 */
5022e3c8fcbSPoul-Henning Kamp 		if (!LIST_EMPTY(&p->p_children))
5032e3c8fcbSPoul-Henning Kamp 			p = LIST_FIRST(&p->p_children);
504df8bae1dSRodney W. Grimes 		else for (;;) {
50598f03f90SJake Burkholder 			if (p == top) {
5061005a129SJohn Baldwin 				sx_sunlock(&proctree_lock);
507df8bae1dSRodney W. Grimes 				return (ret);
50898f03f90SJake Burkholder 			}
5092e3c8fcbSPoul-Henning Kamp 			if (LIST_NEXT(p, p_sibling)) {
5102e3c8fcbSPoul-Henning Kamp 				p = LIST_NEXT(p, p_sibling);
511df8bae1dSRodney W. Grimes 				break;
512df8bae1dSRodney W. Grimes 			}
513b75356e1SJeffrey Hsu 			p = p->p_pptr;
514df8bae1dSRodney W. Grimes 		}
515df8bae1dSRodney W. Grimes 	}
516df8bae1dSRodney W. Grimes 	/*NOTREACHED*/
517df8bae1dSRodney W. Grimes }
518df8bae1dSRodney W. Grimes 
51987b6de2bSPoul-Henning Kamp static void
52042ebfbf2SBrian Feldman ktrwrite(vp, kth, uio)
521df8bae1dSRodney W. Grimes 	struct vnode *vp;
522df8bae1dSRodney W. Grimes 	register struct ktr_header *kth;
52342ebfbf2SBrian Feldman 	struct uio *uio;
524df8bae1dSRodney W. Grimes {
525df8bae1dSRodney W. Grimes 	struct uio auio;
526df8bae1dSRodney W. Grimes 	struct iovec aiov[2];
527b40ce416SJulian Elischer 	struct thread *td = curthread;	/* XXX */
528b40ce416SJulian Elischer 	struct proc *p = td->td_proc;	/* XXX */
529f2a2857bSKirk McKusick 	struct mount *mp;
530df8bae1dSRodney W. Grimes 	int error;
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes 	if (vp == NULL)
533df8bae1dSRodney W. Grimes 		return;
534df8bae1dSRodney W. Grimes 	auio.uio_iov = &aiov[0];
535df8bae1dSRodney W. Grimes 	auio.uio_offset = 0;
536df8bae1dSRodney W. Grimes 	auio.uio_segflg = UIO_SYSSPACE;
537df8bae1dSRodney W. Grimes 	auio.uio_rw = UIO_WRITE;
538df8bae1dSRodney W. Grimes 	aiov[0].iov_base = (caddr_t)kth;
539df8bae1dSRodney W. Grimes 	aiov[0].iov_len = sizeof(struct ktr_header);
540df8bae1dSRodney W. Grimes 	auio.uio_resid = sizeof(struct ktr_header);
541df8bae1dSRodney W. Grimes 	auio.uio_iovcnt = 1;
542b40ce416SJulian Elischer 	auio.uio_td = curthread;
543df8bae1dSRodney W. Grimes 	if (kth->ktr_len > 0) {
544df8bae1dSRodney W. Grimes 		auio.uio_iovcnt++;
54562ae6c89SJason Evans 		aiov[1].iov_base = kth->ktr_buffer;
546df8bae1dSRodney W. Grimes 		aiov[1].iov_len = kth->ktr_len;
547df8bae1dSRodney W. Grimes 		auio.uio_resid += kth->ktr_len;
54842ebfbf2SBrian Feldman 		if (uio != NULL)
54942ebfbf2SBrian Feldman 			kth->ktr_len += uio->uio_resid;
550df8bae1dSRodney W. Grimes 	}
551f2a2857bSKirk McKusick 	vn_start_write(vp, &mp, V_WAIT);
552b40ce416SJulian Elischer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
553a854ed98SJohn Baldwin 	(void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
554a854ed98SJohn Baldwin 	error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, td->td_ucred);
55542ebfbf2SBrian Feldman 	if (error == 0 && uio != NULL) {
556a854ed98SJohn Baldwin 		(void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
557a854ed98SJohn Baldwin 		error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, td->td_ucred);
55842ebfbf2SBrian Feldman 	}
559b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
560f2a2857bSKirk McKusick 	vn_finished_write(mp);
561df8bae1dSRodney W. Grimes 	if (!error)
562df8bae1dSRodney W. Grimes 		return;
563df8bae1dSRodney W. Grimes 	/*
56479deba82SMatthew Dillon 	 * If error encountered, give up tracing on this vnode.  XXX what
56579deba82SMatthew Dillon 	 * happens to the loop if vrele() blocks?
566df8bae1dSRodney W. Grimes 	 */
567df8bae1dSRodney W. Grimes 	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
568df8bae1dSRodney W. Grimes 	    error);
5691005a129SJohn Baldwin 	sx_slock(&allproc_lock);
5702e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
571df8bae1dSRodney W. Grimes 		if (p->p_tracep == vp) {
572df8bae1dSRodney W. Grimes 			p->p_tracep = NULL;
573df8bae1dSRodney W. Grimes 			p->p_traceflag = 0;
574df8bae1dSRodney W. Grimes 			vrele(vp);
575df8bae1dSRodney W. Grimes 		}
576df8bae1dSRodney W. Grimes 	}
5771005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes /*
581df8bae1dSRodney W. Grimes  * Return true if caller has permission to set the ktracing state
582df8bae1dSRodney W. Grimes  * of target.  Essentially, the target can't possess any
583df8bae1dSRodney W. Grimes  * more permissions than the caller.  KTRFAC_ROOT signifies that
584df8bae1dSRodney W. Grimes  * root previously set the tracing status on the target process, and
585df8bae1dSRodney W. Grimes  * so, only root may further change it.
586df8bae1dSRodney W. Grimes  */
58787b6de2bSPoul-Henning Kamp static int
588df8bae1dSRodney W. Grimes ktrcanset(callp, targetp)
589df8bae1dSRodney W. Grimes 	struct proc *callp, *targetp;
590df8bae1dSRodney W. Grimes {
591df8bae1dSRodney W. Grimes 
592a0f75161SRobert Watson 	if (targetp->p_traceflag & KTRFAC_ROOT &&
593a0f75161SRobert Watson 	    suser_xxx(NULL, callp, PRISON_ROOT))
59475c13541SPoul-Henning Kamp 		return (0);
595a0f75161SRobert Watson 
596a0f75161SRobert Watson 	if (p_candebug(callp, targetp) != 0)
597a0f75161SRobert Watson 		return (0);
598a0f75161SRobert Watson 
599df8bae1dSRodney W. Grimes 	return (1);
600df8bae1dSRodney W. Grimes }
601df8bae1dSRodney W. Grimes 
602db6a20e2SGarrett Wollman #endif /* KTRACE */
603