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) { 314b40ce416SJulian Elischer (void) vn_close(vp, FREAD|FWRITE, curp->p_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, 330b40ce416SJulian Elischer p->p_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 */ 353df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 354df8bae1dSRodney W. Grimes if (pg == NULL) { 355df8bae1dSRodney W. Grimes error = ESRCH; 356df8bae1dSRodney W. Grimes goto done; 357df8bae1dSRodney W. Grimes } 3582e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) 359df8bae1dSRodney W. Grimes if (descend) 360df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 361df8bae1dSRodney W. Grimes else 362df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 363df8bae1dSRodney W. Grimes } else { 364df8bae1dSRodney W. Grimes /* 365df8bae1dSRodney W. Grimes * by pid 366df8bae1dSRodney W. Grimes */ 367df8bae1dSRodney W. Grimes p = pfind(uap->pid); 368df8bae1dSRodney W. Grimes if (p == NULL) { 369df8bae1dSRodney W. Grimes error = ESRCH; 370df8bae1dSRodney W. Grimes goto done; 371df8bae1dSRodney W. Grimes } 37233a9ed9dSJohn Baldwin PROC_UNLOCK(p); 373df8bae1dSRodney W. Grimes if (descend) 374df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 375df8bae1dSRodney W. Grimes else 376df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes if (!ret) 379df8bae1dSRodney W. Grimes error = EPERM; 380df8bae1dSRodney W. Grimes done: 381df8bae1dSRodney W. Grimes if (vp != NULL) 382b40ce416SJulian Elischer (void) vn_close(vp, FWRITE, curp->p_ucred, td); 383df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 384df8bae1dSRodney W. Grimes return (error); 385db6a20e2SGarrett Wollman #else 386db6a20e2SGarrett Wollman return ENOSYS; 387db6a20e2SGarrett Wollman #endif 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes 390e6c4b9baSPoul-Henning Kamp /* 391e6c4b9baSPoul-Henning Kamp * utrace system call 392e6c4b9baSPoul-Henning Kamp */ 393e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 394e6c4b9baSPoul-Henning Kamp int 395b40ce416SJulian Elischer utrace(td, uap) 396b40ce416SJulian Elischer struct thread *td; 397e6c4b9baSPoul-Henning Kamp register struct utrace_args *uap; 398e6c4b9baSPoul-Henning Kamp { 399b40ce416SJulian Elischer 400e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 401e6c4b9baSPoul-Henning Kamp struct ktr_header *kth; 402e6c4b9baSPoul-Henning Kamp struct proc *p = curproc; /* XXX */ 40379deba82SMatthew Dillon struct vnode *vp; 404e6c4b9baSPoul-Henning Kamp register caddr_t cp; 405e6c4b9baSPoul-Henning Kamp 406e6c4b9baSPoul-Henning Kamp if (!KTRPOINT(p, KTR_USER)) 407e6c4b9baSPoul-Henning Kamp return (0); 408bdfa4f04SAlfred Perlstein if (uap->len > KTR_USER_MAXLEN) 4090bad156aSAlfred Perlstein return (EINVAL); 410e6c4b9baSPoul-Henning Kamp p->p_traceflag |= KTRFAC_ACTIVE; 41179deba82SMatthew Dillon if ((vp = p->p_tracep) != NULL) 41279deba82SMatthew Dillon VREF(vp); 413e6c4b9baSPoul-Henning Kamp kth = ktrgetheader(KTR_USER); 414d920a829SPoul-Henning Kamp MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK); 415e6c4b9baSPoul-Henning Kamp if (!copyin(uap->addr, cp, uap->len)) { 41662ae6c89SJason Evans kth->ktr_buffer = cp; 417d920a829SPoul-Henning Kamp kth->ktr_len = uap->len; 41879deba82SMatthew Dillon ktrwrite(vp, kth, NULL); 419e6c4b9baSPoul-Henning Kamp } 42079deba82SMatthew Dillon if (vp) 42179deba82SMatthew Dillon vrele(vp); 422e6c4b9baSPoul-Henning Kamp FREE(kth, M_KTRACE); 423d920a829SPoul-Henning Kamp FREE(cp, M_KTRACE); 424e6c4b9baSPoul-Henning Kamp p->p_traceflag &= ~KTRFAC_ACTIVE; 425e6c4b9baSPoul-Henning Kamp 426e6c4b9baSPoul-Henning Kamp return (0); 427e6c4b9baSPoul-Henning Kamp #else 428e6c4b9baSPoul-Henning Kamp return (ENOSYS); 429e6c4b9baSPoul-Henning Kamp #endif 430e6c4b9baSPoul-Henning Kamp } 431e6c4b9baSPoul-Henning Kamp 432db6a20e2SGarrett Wollman #ifdef KTRACE 43387b6de2bSPoul-Henning Kamp static int 434df8bae1dSRodney W. Grimes ktrops(curp, p, ops, facs, vp) 435df8bae1dSRodney W. Grimes struct proc *p, *curp; 436df8bae1dSRodney W. Grimes int ops, facs; 437df8bae1dSRodney W. Grimes struct vnode *vp; 438df8bae1dSRodney W. Grimes { 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes if (!ktrcanset(curp, p)) 441df8bae1dSRodney W. Grimes return (0); 442df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 443df8bae1dSRodney W. Grimes if (p->p_tracep != vp) { 44479deba82SMatthew Dillon struct vnode *vtmp; 44579deba82SMatthew Dillon 446df8bae1dSRodney W. Grimes /* 447df8bae1dSRodney W. Grimes * if trace file already in use, relinquish 448df8bae1dSRodney W. Grimes */ 449df8bae1dSRodney W. Grimes VREF(vp); 45079deba82SMatthew Dillon while ((vtmp = p->p_tracep) != NULL) { 45179deba82SMatthew Dillon p->p_tracep = NULL; 45279deba82SMatthew Dillon vrele(vtmp); 45379deba82SMatthew Dillon } 454df8bae1dSRodney W. Grimes p->p_tracep = vp; 455df8bae1dSRodney W. Grimes } 456df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 457df8bae1dSRodney W. Grimes if (curp->p_ucred->cr_uid == 0) 458df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 459df8bae1dSRodney W. Grimes } else { 460df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 461df8bae1dSRodney W. Grimes if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 46279deba82SMatthew Dillon struct vnode *vtmp; 46379deba82SMatthew Dillon 464df8bae1dSRodney W. Grimes /* no more tracing */ 465df8bae1dSRodney W. Grimes p->p_traceflag = 0; 46679deba82SMatthew Dillon if ((vtmp = p->p_tracep) != NULL) { 467df8bae1dSRodney W. Grimes p->p_tracep = NULL; 46879deba82SMatthew Dillon vrele(vtmp); 469df8bae1dSRodney W. Grimes } 470df8bae1dSRodney W. Grimes } 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes 473df8bae1dSRodney W. Grimes return (1); 474df8bae1dSRodney W. Grimes } 475df8bae1dSRodney W. Grimes 47687b6de2bSPoul-Henning Kamp static int 477df8bae1dSRodney W. Grimes ktrsetchildren(curp, top, ops, facs, vp) 478df8bae1dSRodney W. Grimes struct proc *curp, *top; 479df8bae1dSRodney W. Grimes int ops, facs; 480df8bae1dSRodney W. Grimes struct vnode *vp; 481df8bae1dSRodney W. Grimes { 482df8bae1dSRodney W. Grimes register struct proc *p; 483df8bae1dSRodney W. Grimes register int ret = 0; 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes p = top; 4861005a129SJohn Baldwin sx_slock(&proctree_lock); 487df8bae1dSRodney W. Grimes for (;;) { 488df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 489df8bae1dSRodney W. Grimes /* 490df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 491df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 492df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 493df8bae1dSRodney W. Grimes */ 4942e3c8fcbSPoul-Henning Kamp if (!LIST_EMPTY(&p->p_children)) 4952e3c8fcbSPoul-Henning Kamp p = LIST_FIRST(&p->p_children); 496df8bae1dSRodney W. Grimes else for (;;) { 49798f03f90SJake Burkholder if (p == top) { 4981005a129SJohn Baldwin sx_sunlock(&proctree_lock); 499df8bae1dSRodney W. Grimes return (ret); 50098f03f90SJake Burkholder } 5012e3c8fcbSPoul-Henning Kamp if (LIST_NEXT(p, p_sibling)) { 5022e3c8fcbSPoul-Henning Kamp p = LIST_NEXT(p, p_sibling); 503df8bae1dSRodney W. Grimes break; 504df8bae1dSRodney W. Grimes } 505b75356e1SJeffrey Hsu p = p->p_pptr; 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes /*NOTREACHED*/ 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes 51187b6de2bSPoul-Henning Kamp static void 51242ebfbf2SBrian Feldman ktrwrite(vp, kth, uio) 513df8bae1dSRodney W. Grimes struct vnode *vp; 514df8bae1dSRodney W. Grimes register struct ktr_header *kth; 51542ebfbf2SBrian Feldman struct uio *uio; 516df8bae1dSRodney W. Grimes { 517df8bae1dSRodney W. Grimes struct uio auio; 518df8bae1dSRodney W. Grimes struct iovec aiov[2]; 519b40ce416SJulian Elischer struct thread *td = curthread; /* XXX */ 520b40ce416SJulian Elischer struct proc *p = td->td_proc; /* XXX */ 521f2a2857bSKirk McKusick struct mount *mp; 522df8bae1dSRodney W. Grimes int error; 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes if (vp == NULL) 525df8bae1dSRodney W. Grimes return; 526df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 527df8bae1dSRodney W. Grimes auio.uio_offset = 0; 528df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 529df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 530df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 531df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 532df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 533df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 534b40ce416SJulian Elischer auio.uio_td = curthread; 535df8bae1dSRodney W. Grimes if (kth->ktr_len > 0) { 536df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 53762ae6c89SJason Evans aiov[1].iov_base = kth->ktr_buffer; 538df8bae1dSRodney W. Grimes aiov[1].iov_len = kth->ktr_len; 539df8bae1dSRodney W. Grimes auio.uio_resid += kth->ktr_len; 54042ebfbf2SBrian Feldman if (uio != NULL) 54142ebfbf2SBrian Feldman kth->ktr_len += uio->uio_resid; 542df8bae1dSRodney W. Grimes } 543f2a2857bSKirk McKusick vn_start_write(vp, &mp, V_WAIT); 544b40ce416SJulian Elischer vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 545b40ce416SJulian Elischer (void)VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 546df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, p->p_ucred); 54742ebfbf2SBrian Feldman if (error == 0 && uio != NULL) { 548b40ce416SJulian Elischer (void)VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE); 54942ebfbf2SBrian Feldman error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, p->p_ucred); 55042ebfbf2SBrian Feldman } 551b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 552f2a2857bSKirk McKusick vn_finished_write(mp); 553df8bae1dSRodney W. Grimes if (!error) 554df8bae1dSRodney W. Grimes return; 555df8bae1dSRodney W. Grimes /* 55679deba82SMatthew Dillon * If error encountered, give up tracing on this vnode. XXX what 55779deba82SMatthew Dillon * happens to the loop if vrele() blocks? 558df8bae1dSRodney W. Grimes */ 559df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 560df8bae1dSRodney W. Grimes error); 5611005a129SJohn Baldwin sx_slock(&allproc_lock); 5622e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 563df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 564df8bae1dSRodney W. Grimes p->p_tracep = NULL; 565df8bae1dSRodney W. Grimes p->p_traceflag = 0; 566df8bae1dSRodney W. Grimes vrele(vp); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes } 5691005a129SJohn Baldwin sx_sunlock(&allproc_lock); 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes /* 573df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 574df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 575df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 576df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 577df8bae1dSRodney W. Grimes * so, only root may further change it. 578df8bae1dSRodney W. Grimes */ 57987b6de2bSPoul-Henning Kamp static int 580df8bae1dSRodney W. Grimes ktrcanset(callp, targetp) 581df8bae1dSRodney W. Grimes struct proc *callp, *targetp; 582df8bae1dSRodney W. Grimes { 583df8bae1dSRodney W. Grimes 584a0f75161SRobert Watson if (targetp->p_traceflag & KTRFAC_ROOT && 585a0f75161SRobert Watson suser_xxx(NULL, callp, PRISON_ROOT)) 58675c13541SPoul-Henning Kamp return (0); 587a0f75161SRobert Watson 588a0f75161SRobert Watson if (p_candebug(callp, targetp) != 0) 589a0f75161SRobert Watson return (0); 590a0f75161SRobert Watson 591df8bae1dSRodney W. Grimes return (1); 592df8bae1dSRodney W. Grimes } 593df8bae1dSRodney W. Grimes 594db6a20e2SGarrett Wollman #endif /* KTRACE */ 595