Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.44
      1  1.44  sommerfe /*	$NetBSD: kern_ktrace.c,v 1.44 2000/05/29 22:04:11 sommerfeld Exp $	*/
      2  1.11       cgd 
      3   1.1       cgd /*
      4   1.9       cgd  * Copyright (c) 1989, 1993
      5   1.9       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.25      fvdl  *	@(#)kern_ktrace.c	8.5 (Berkeley) 5/14/95
     36   1.1       cgd  */
     37  1.29   thorpej 
     38  1.29   thorpej #include "opt_ktrace.h"
     39   1.1       cgd 
     40   1.9       cgd #ifdef KTRACE
     41   1.9       cgd 
     42   1.7   mycroft #include <sys/param.h>
     43  1.13       cgd #include <sys/systm.h>
     44   1.7   mycroft #include <sys/proc.h>
     45   1.7   mycroft #include <sys/file.h>
     46   1.7   mycroft #include <sys/namei.h>
     47   1.7   mycroft #include <sys/vnode.h>
     48   1.7   mycroft #include <sys/ktrace.h>
     49   1.7   mycroft #include <sys/malloc.h>
     50   1.7   mycroft #include <sys/syslog.h>
     51  1.28  christos #include <sys/filedesc.h>
     52  1.42  sommerfe #include <sys/ioctl.h>
     53   1.1       cgd 
     54  1.13       cgd #include <sys/mount.h>
     55  1.13       cgd #include <sys/syscallargs.h>
     56  1.22  christos 
     57  1.42  sommerfe int	ktrace_common __P((struct proc *, int, int, int, struct file *));
     58  1.39   thorpej void	ktrinitheader __P((struct ktr_header *, struct proc *, int));
     59  1.42  sommerfe int	ktrops __P((struct proc *, struct proc *, int, int, struct file *));
     60  1.42  sommerfe int	ktrsetchildren __P((struct proc *, struct proc *, int, int,
     61  1.42  sommerfe     struct file *));
     62  1.42  sommerfe int	ktrwrite __P((struct proc *, struct ktr_header *));
     63  1.39   thorpej int	ktrcanset __P((struct proc *, struct proc *));
     64  1.44  sommerfe int	ktrsamefile __P((struct file *, struct file *));
     65  1.44  sommerfe 
     66  1.44  sommerfe /*
     67  1.44  sommerfe  * "deep" compare of two files for the purposes of clearing a trace.
     68  1.44  sommerfe  * Returns true if they're the same open file, or if they point at the
     69  1.44  sommerfe  * same underlying vnode/socket.
     70  1.44  sommerfe  */
     71  1.44  sommerfe 
     72  1.44  sommerfe int
     73  1.44  sommerfe ktrsamefile (f1, f2)
     74  1.44  sommerfe 	struct file *f1, *f2;
     75  1.44  sommerfe {
     76  1.44  sommerfe 	return ((f1 == f2) ||
     77  1.44  sommerfe 	    ((f1->f_type == f2->f_type) &&
     78  1.44  sommerfe 		(f1->f_data == f2->f_data)));
     79  1.44  sommerfe }
     80  1.22  christos 
     81  1.28  christos void
     82  1.28  christos ktrderef(p)
     83  1.28  christos 	struct proc *p;
     84  1.28  christos {
     85  1.42  sommerfe 	struct file *fp = p->p_tracep;
     86  1.42  sommerfe 	p->p_traceflag = 0;
     87  1.42  sommerfe 	if (fp == NULL)
     88  1.28  christos 		return;
     89  1.42  sommerfe 	FILE_USE(fp);
     90  1.42  sommerfe 	closef(fp, NULL);
     91  1.28  christos 
     92  1.28  christos 	p->p_tracep = NULL;
     93  1.28  christos }
     94  1.28  christos 
     95  1.28  christos void
     96  1.28  christos ktradref(p)
     97  1.28  christos 	struct proc *p;
     98  1.28  christos {
     99  1.42  sommerfe 	struct file *fp = p->p_tracep;
    100  1.28  christos 
    101  1.42  sommerfe 	fp->f_count++;
    102  1.28  christos }
    103  1.28  christos 
    104  1.39   thorpej void
    105  1.39   thorpej ktrinitheader(kth, p, type)
    106  1.39   thorpej 	struct ktr_header *kth;
    107  1.39   thorpej 	struct proc *p;
    108   1.4    andrew 	int type;
    109   1.1       cgd {
    110   1.1       cgd 
    111  1.39   thorpej 	memset(kth, 0, sizeof(*kth));
    112   1.1       cgd 	kth->ktr_type = type;
    113   1.1       cgd 	microtime(&kth->ktr_time);
    114   1.1       cgd 	kth->ktr_pid = p->p_pid;
    115  1.32     perry 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    116   1.1       cgd }
    117   1.1       cgd 
    118  1.17       cgd void
    119  1.42  sommerfe ktrsyscall(p, code, argsize, args)
    120  1.42  sommerfe 	struct proc *p;
    121  1.16   mycroft 	register_t code;
    122  1.16   mycroft 	size_t argsize;
    123  1.16   mycroft 	register_t args[];
    124   1.1       cgd {
    125  1.39   thorpej 	struct ktr_header kth;
    126  1.39   thorpej 	struct ktr_syscall *ktp;
    127  1.17       cgd 	register_t *argp;
    128  1.39   thorpej 	size_t len = sizeof(struct ktr_syscall) + argsize;
    129  1.17       cgd 	int i;
    130   1.1       cgd 
    131   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    132  1.39   thorpej 	ktrinitheader(&kth, p, KTR_SYSCALL);
    133  1.39   thorpej 	ktp = malloc(len, M_TEMP, M_WAITOK);
    134   1.1       cgd 	ktp->ktr_code = code;
    135  1.17       cgd 	ktp->ktr_argsize = argsize;
    136  1.17       cgd 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    137  1.31     perry 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    138   1.1       cgd 		*argp++ = args[i];
    139  1.39   thorpej 	kth.ktr_buf = (caddr_t)ktp;
    140  1.39   thorpej 	kth.ktr_len = len;
    141  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    142  1.39   thorpej 	free(ktp, M_TEMP);
    143   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    144   1.1       cgd }
    145   1.1       cgd 
    146  1.17       cgd void
    147  1.42  sommerfe ktrsysret(p, code, error, retval)
    148  1.42  sommerfe 	struct proc *p;
    149  1.16   mycroft 	register_t code;
    150  1.16   mycroft 	int error;
    151  1.16   mycroft 	register_t retval;
    152   1.1       cgd {
    153  1.39   thorpej 	struct ktr_header kth;
    154   1.1       cgd 	struct ktr_sysret ktp;
    155   1.1       cgd 
    156   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    157  1.39   thorpej 	ktrinitheader(&kth, p, KTR_SYSRET);
    158   1.1       cgd 	ktp.ktr_code = code;
    159  1.34    kleink 	ktp.ktr_eosys = 0;			/* XXX unused */
    160   1.1       cgd 	ktp.ktr_error = error;
    161   1.1       cgd 	ktp.ktr_retval = retval;		/* what about val2 ? */
    162   1.1       cgd 
    163  1.39   thorpej 	kth.ktr_buf = (caddr_t)&ktp;
    164  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_sysret);
    165   1.1       cgd 
    166  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    167   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    168   1.1       cgd }
    169   1.1       cgd 
    170  1.17       cgd void
    171  1.42  sommerfe ktrnamei(p, path)
    172  1.42  sommerfe 	struct proc *p;
    173   1.1       cgd 	char *path;
    174   1.1       cgd {
    175  1.39   thorpej 	struct ktr_header kth;
    176   1.1       cgd 
    177   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    178  1.39   thorpej 	ktrinitheader(&kth, p, KTR_NAMEI);
    179  1.39   thorpej 	kth.ktr_len = strlen(path);
    180  1.39   thorpej 	kth.ktr_buf = path;
    181  1.18  christos 
    182  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    183  1.18  christos 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    184  1.18  christos }
    185  1.18  christos 
    186  1.18  christos void
    187  1.42  sommerfe ktremul(p)
    188  1.28  christos 	struct proc *p;
    189  1.18  christos {
    190  1.39   thorpej 	struct ktr_header kth;
    191  1.42  sommerfe 	char *emul = p->p_emul->e_name;
    192  1.18  christos 
    193  1.18  christos 	p->p_traceflag |= KTRFAC_ACTIVE;
    194  1.39   thorpej 	ktrinitheader(&kth, p, KTR_EMUL);
    195  1.39   thorpej 	kth.ktr_len = strlen(emul);
    196  1.39   thorpej 	kth.ktr_buf = emul;
    197   1.1       cgd 
    198  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    199   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    200   1.1       cgd }
    201   1.1       cgd 
    202  1.17       cgd void
    203  1.42  sommerfe ktrgenio(p, fd, rw, iov, len, error)
    204  1.42  sommerfe 	struct proc *p;
    205   1.1       cgd 	int fd;
    206   1.1       cgd 	enum uio_rw rw;
    207  1.28  christos 	struct iovec *iov;
    208   1.4    andrew 	int len, error;
    209   1.1       cgd {
    210  1.39   thorpej 	struct ktr_header kth;
    211  1.28  christos 	struct ktr_genio *ktp;
    212  1.28  christos 	caddr_t cp;
    213  1.28  christos 	int resid = len, cnt;
    214  1.39   thorpej 	int buflen;
    215  1.39   thorpej 
    216   1.1       cgd 	if (error)
    217   1.1       cgd 		return;
    218  1.39   thorpej 
    219   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    220  1.39   thorpej 
    221  1.39   thorpej 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    222  1.39   thorpej 
    223  1.39   thorpej 	ktrinitheader(&kth, p, KTR_GENIO);
    224  1.39   thorpej 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    225   1.1       cgd 	ktp->ktr_fd = fd;
    226   1.1       cgd 	ktp->ktr_rw = rw;
    227  1.39   thorpej 
    228  1.39   thorpej 	kth.ktr_buf = (caddr_t)ktp;
    229  1.39   thorpej 
    230  1.31     perry 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    231  1.39   thorpej 	buflen -= sizeof(struct ktr_genio);
    232  1.39   thorpej 
    233   1.1       cgd 	while (resid > 0) {
    234  1.41   thorpej 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    235  1.39   thorpej 			preempt(NULL);
    236  1.39   thorpej 
    237  1.39   thorpej 		cnt = min(iov->iov_len, buflen);
    238  1.39   thorpej 		if (cnt > resid)
    239   1.1       cgd 			cnt = resid;
    240  1.39   thorpej 		if (copyin(iov->iov_base, cp, cnt))
    241  1.39   thorpej 			break;
    242  1.39   thorpej 
    243  1.39   thorpej 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    244  1.39   thorpej 
    245  1.42  sommerfe 		if (__predict_false(ktrwrite(p, &kth) != 0))
    246  1.39   thorpej 			break;
    247  1.39   thorpej 
    248  1.39   thorpej 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    249  1.39   thorpej 		iov->iov_len -= cnt;
    250  1.39   thorpej 
    251  1.39   thorpej 		if (iov->iov_len == 0)
    252  1.39   thorpej 			iov++;
    253  1.39   thorpej 
    254   1.1       cgd 		resid -= cnt;
    255   1.1       cgd 	}
    256   1.1       cgd 
    257  1.39   thorpej 	free(ktp, M_TEMP);
    258   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    259   1.1       cgd }
    260   1.1       cgd 
    261  1.17       cgd void
    262  1.42  sommerfe ktrpsig(p, sig, action, mask, code)
    263  1.42  sommerfe 	struct proc *p;
    264   1.9       cgd 	int sig;
    265   1.9       cgd 	sig_t action;
    266  1.33   mycroft 	sigset_t *mask;
    267  1.33   mycroft 	int code;
    268   1.1       cgd {
    269  1.39   thorpej 	struct ktr_header kth;
    270   1.1       cgd 	struct ktr_psig	kp;
    271   1.1       cgd 
    272   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    273  1.39   thorpej 	ktrinitheader(&kth, p, KTR_PSIG);
    274   1.1       cgd 	kp.signo = (char)sig;
    275   1.1       cgd 	kp.action = action;
    276  1.33   mycroft 	kp.mask = *mask;
    277   1.1       cgd 	kp.code = code;
    278  1.39   thorpej 	kth.ktr_buf = (caddr_t)&kp;
    279  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_psig);
    280   1.1       cgd 
    281  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    282   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    283   1.9       cgd }
    284   1.9       cgd 
    285  1.17       cgd void
    286  1.42  sommerfe ktrcsw(p, out, user)
    287  1.42  sommerfe 	struct proc *p;
    288   1.9       cgd 	int out, user;
    289   1.9       cgd {
    290  1.39   thorpej 	struct ktr_header kth;
    291  1.39   thorpej 	struct ktr_csw kc;
    292   1.9       cgd 
    293   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    294  1.39   thorpej 	ktrinitheader(&kth, p, KTR_CSW);
    295   1.9       cgd 	kc.out = out;
    296   1.9       cgd 	kc.user = user;
    297  1.39   thorpej 	kth.ktr_buf = (caddr_t)&kc;
    298  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_csw);
    299   1.9       cgd 
    300  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    301   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    302   1.1       cgd }
    303   1.1       cgd 
    304   1.1       cgd /* Interface and common routines */
    305   1.1       cgd 
    306  1.17       cgd int
    307  1.42  sommerfe ktrace_common (curp, ops, facs, pid, fp)
    308  1.28  christos 	struct proc *curp;
    309  1.42  sommerfe 	int ops, facs, pid;
    310  1.42  sommerfe 	struct file *fp;
    311  1.28  christos {
    312  1.42  sommerfe 	int ret = 0;
    313  1.42  sommerfe 	int error = 0;
    314  1.42  sommerfe 	int one = 1;
    315  1.42  sommerfe 	int descend;
    316  1.28  christos 	struct proc *p;
    317  1.28  christos 	struct pgrp *pg;
    318  1.28  christos 
    319  1.28  christos 	curp->p_traceflag |= KTRFAC_ACTIVE;
    320  1.42  sommerfe 	descend = ops & KTRFLAG_DESCEND;
    321  1.42  sommerfe 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    322  1.28  christos 
    323  1.28  christos 	/*
    324  1.28  christos 	 * Clear all uses of the tracefile
    325  1.28  christos 	 */
    326  1.28  christos 	if (KTROP(ops) == KTROP_CLEARFILE) {
    327  1.37   thorpej 		proclist_lock_read();
    328  1.39   thorpej 		for (p = LIST_FIRST(&allproc); p != NULL;
    329  1.39   thorpej 		     p = LIST_NEXT(p, p_list)) {
    330  1.44  sommerfe 			if (ktrsamefile(p->p_tracep, fp)) {
    331  1.28  christos 				if (ktrcanset(curp, p))
    332  1.28  christos 					ktrderef(p);
    333  1.28  christos 				else
    334  1.28  christos 					error = EPERM;
    335  1.28  christos 			}
    336  1.28  christos 		}
    337  1.36   thorpej 		proclist_unlock_read();
    338  1.28  christos 		goto done;
    339  1.28  christos 	}
    340  1.42  sommerfe 
    341  1.42  sommerfe 	/*
    342  1.42  sommerfe 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    343  1.42  sommerfe 	 */
    344  1.42  sommerfe 
    345  1.43  sommerfe 	if (fp != NULL) {
    346  1.43  sommerfe 		fp->f_flag |= FNONBLOCK;
    347  1.43  sommerfe 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    348  1.43  sommerfe 	}
    349  1.42  sommerfe 
    350  1.28  christos 	/*
    351  1.28  christos 	 * need something to (un)trace (XXX - why is this here?)
    352  1.28  christos 	 */
    353  1.28  christos 	if (!facs) {
    354  1.28  christos 		error = EINVAL;
    355  1.28  christos 		goto done;
    356  1.28  christos 	}
    357  1.28  christos 	/*
    358  1.28  christos 	 * do it
    359  1.28  christos 	 */
    360  1.42  sommerfe 	if (pid < 0) {
    361  1.28  christos 		/*
    362  1.28  christos 		 * by process group
    363  1.28  christos 		 */
    364  1.42  sommerfe 		pg = pgfind(-pid);
    365  1.28  christos 		if (pg == NULL) {
    366  1.28  christos 			error = ESRCH;
    367  1.28  christos 			goto done;
    368  1.28  christos 		}
    369  1.39   thorpej 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    370  1.39   thorpej 		     p = LIST_NEXT(p, p_pglist)) {
    371  1.28  christos 			if (descend)
    372  1.28  christos 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    373  1.28  christos 			else
    374  1.28  christos 				ret |= ktrops(curp, p, ops, facs, fp);
    375  1.39   thorpej 		}
    376  1.28  christos 
    377  1.28  christos 	} else {
    378  1.28  christos 		/*
    379  1.28  christos 		 * by pid
    380  1.28  christos 		 */
    381  1.42  sommerfe 		p = pfind(pid);
    382  1.28  christos 		if (p == NULL) {
    383  1.28  christos 			error = ESRCH;
    384  1.28  christos 			goto done;
    385  1.28  christos 		}
    386  1.28  christos 		if (descend)
    387  1.28  christos 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    388  1.28  christos 		else
    389  1.28  christos 			ret |= ktrops(curp, p, ops, facs, fp);
    390  1.28  christos 	}
    391  1.28  christos 	if (!ret)
    392  1.28  christos 		error = EPERM;
    393  1.28  christos done:
    394  1.28  christos 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    395  1.28  christos 	return (error);
    396  1.28  christos }
    397  1.28  christos 
    398  1.28  christos /*
    399  1.28  christos  * ktrace system call
    400  1.28  christos  */
    401  1.28  christos /* ARGSUSED */
    402  1.28  christos int
    403  1.42  sommerfe sys_fktrace(curp, v, retval)
    404  1.42  sommerfe 	struct proc *curp;
    405  1.42  sommerfe 	void *v;
    406  1.42  sommerfe 	register_t *retval;
    407  1.42  sommerfe {
    408  1.42  sommerfe 	struct sys_fktrace_args /* {
    409  1.42  sommerfe 		syscallarg(int) fd;
    410  1.42  sommerfe 		syscallarg(int) ops;
    411  1.42  sommerfe 		syscallarg(int) facs;
    412  1.42  sommerfe 		syscallarg(int) pid;
    413  1.42  sommerfe 	} */ *uap = v;
    414  1.42  sommerfe 	struct file *fp = NULL;
    415  1.42  sommerfe 	struct filedesc *fdp = curp->p_fd;
    416  1.42  sommerfe 
    417  1.42  sommerfe 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    418  1.42  sommerfe 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    419  1.42  sommerfe 	    (fp->f_flag & FWRITE) == 0)
    420  1.42  sommerfe 		return (EBADF);
    421  1.42  sommerfe 
    422  1.42  sommerfe 	return ktrace_common(curp, SCARG(uap, ops),
    423  1.42  sommerfe 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    424  1.42  sommerfe }
    425  1.42  sommerfe 
    426  1.42  sommerfe /*
    427  1.42  sommerfe  * ktrace system call
    428  1.42  sommerfe  */
    429  1.42  sommerfe /* ARGSUSED */
    430  1.42  sommerfe int
    431  1.20   mycroft sys_ktrace(curp, v, retval)
    432   1.1       cgd 	struct proc *curp;
    433  1.19   thorpej 	void *v;
    434  1.19   thorpej 	register_t *retval;
    435  1.19   thorpej {
    436  1.28  christos 	struct sys_ktrace_args /* {
    437  1.24   mycroft 		syscallarg(const char *) fname;
    438  1.13       cgd 		syscallarg(int) ops;
    439  1.13       cgd 		syscallarg(int) facs;
    440  1.13       cgd 		syscallarg(int) pid;
    441  1.19   thorpej 	} */ *uap = v;
    442  1.28  christos 	struct vnode *vp = NULL;
    443  1.42  sommerfe 	struct file *fp = NULL;
    444  1.42  sommerfe 	int fd;
    445  1.42  sommerfe 	int ops = SCARG(uap, ops);
    446   1.1       cgd 	int error = 0;
    447   1.1       cgd 	struct nameidata nd;
    448   1.1       cgd 
    449  1.42  sommerfe 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    450  1.42  sommerfe 
    451   1.9       cgd 	curp->p_traceflag |= KTRFAC_ACTIVE;
    452   1.1       cgd 	if (ops != KTROP_CLEAR) {
    453   1.1       cgd 		/*
    454   1.1       cgd 		 * an operation which requires a file argument.
    455   1.1       cgd 		 */
    456  1.13       cgd 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    457  1.13       cgd 		    curp);
    458  1.22  christos 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    459   1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    460   1.1       cgd 			return (error);
    461   1.9       cgd 		}
    462   1.1       cgd 		vp = nd.ni_vp;
    463  1.25      fvdl 		VOP_UNLOCK(vp, 0);
    464   1.1       cgd 		if (vp->v_type != VREG) {
    465   1.1       cgd 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    466   1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    467   1.1       cgd 			return (EACCES);
    468   1.1       cgd 		}
    469   1.1       cgd 		/*
    470  1.42  sommerfe 		 * XXX This uses up a file descriptor slot in the
    471  1.42  sommerfe 		 * tracing process for the duration of this syscall.
    472  1.42  sommerfe 		 * This is not expected to be a problem.  If
    473  1.42  sommerfe 		 * falloc(NULL, ...) DTRT we could skip that part, but
    474  1.42  sommerfe 		 * that would require changing its interface to allow
    475  1.42  sommerfe 		 * the caller to pass in a ucred..
    476  1.42  sommerfe 		 *
    477  1.42  sommerfe 		 * This will FILE_USE the fp it returns, if any.
    478  1.42  sommerfe 		 * Keep it in use until we return.
    479   1.1       cgd 		 */
    480  1.42  sommerfe 		if ((error = falloc(curp, &fp, &fd)) != 0)
    481   1.1       cgd 			goto done;
    482  1.42  sommerfe 
    483  1.42  sommerfe 		fp->f_flag = FWRITE|FAPPEND;
    484  1.42  sommerfe 		fp->f_type = DTYPE_VNODE;
    485  1.42  sommerfe 		fp->f_ops = &vnops;
    486  1.42  sommerfe 		fp->f_data = (caddr_t)vp;
    487  1.42  sommerfe 		vp = NULL;
    488  1.42  sommerfe 	}
    489  1.42  sommerfe 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    490  1.42  sommerfe 	    SCARG(uap, pid), fp);
    491  1.42  sommerfe done:
    492   1.1       cgd 	if (vp != NULL)
    493   1.1       cgd 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    494  1.42  sommerfe 	if (fp != NULL) {
    495  1.44  sommerfe 		FILE_UNUSE(fp, curp);	/* release file */
    496  1.42  sommerfe 		fdrelease(curp, fd); 	/* release fd table slot */
    497  1.42  sommerfe 	}
    498   1.1       cgd 	return (error);
    499   1.1       cgd }
    500   1.1       cgd 
    501   1.4    andrew int
    502  1.42  sommerfe ktrops(curp, p, ops, facs, fp)
    503   1.9       cgd 	struct proc *p, *curp;
    504   1.4    andrew 	int ops, facs;
    505  1.42  sommerfe 	struct file *fp;
    506   1.1       cgd {
    507   1.1       cgd 
    508   1.1       cgd 	if (!ktrcanset(curp, p))
    509   1.1       cgd 		return (0);
    510  1.28  christos 	if (KTROP(ops) == KTROP_SET) {
    511  1.42  sommerfe 		if (p->p_tracep != fp) {
    512   1.1       cgd 			/*
    513   1.1       cgd 			 * if trace file already in use, relinquish
    514   1.1       cgd 			 */
    515  1.28  christos 			ktrderef(p);
    516  1.42  sommerfe 			p->p_tracep = fp;
    517  1.28  christos 			ktradref(p);
    518   1.1       cgd 		}
    519   1.1       cgd 		p->p_traceflag |= facs;
    520   1.1       cgd 		if (curp->p_ucred->cr_uid == 0)
    521   1.1       cgd 			p->p_traceflag |= KTRFAC_ROOT;
    522   1.1       cgd 	} else {
    523   1.1       cgd 		/* KTROP_CLEAR */
    524   1.1       cgd 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    525   1.1       cgd 			/* no more tracing */
    526  1.28  christos 			ktrderef(p);
    527   1.1       cgd 		}
    528   1.1       cgd 	}
    529  1.21  christos 
    530  1.21  christos 	/*
    531  1.21  christos 	 * Emit an emulation record, every time there is a ktrace
    532  1.21  christos 	 * change/attach request.
    533  1.21  christos 	 */
    534  1.21  christos 	if (KTRPOINT(p, KTR_EMUL))
    535  1.42  sommerfe 		ktremul(p);
    536   1.1       cgd 
    537   1.1       cgd 	return (1);
    538   1.1       cgd }
    539   1.1       cgd 
    540  1.22  christos int
    541  1.42  sommerfe ktrsetchildren(curp, top, ops, facs, fp)
    542   1.1       cgd 	struct proc *curp, *top;
    543   1.4    andrew 	int ops, facs;
    544  1.42  sommerfe 	struct file *fp;
    545   1.1       cgd {
    546  1.28  christos 	struct proc *p;
    547  1.28  christos 	int ret = 0;
    548   1.1       cgd 
    549   1.1       cgd 	p = top;
    550   1.1       cgd 	for (;;) {
    551  1.42  sommerfe 		ret |= ktrops(curp, p, ops, facs, fp);
    552   1.1       cgd 		/*
    553   1.1       cgd 		 * If this process has children, descend to them next,
    554   1.1       cgd 		 * otherwise do any siblings, and if done with this level,
    555   1.1       cgd 		 * follow back up the tree (but not past top).
    556   1.1       cgd 		 */
    557  1.39   thorpej 		if (LIST_FIRST(&p->p_children) != NULL)
    558  1.39   thorpej 			p = LIST_FIRST(&p->p_children);
    559   1.1       cgd 		else for (;;) {
    560   1.1       cgd 			if (p == top)
    561   1.1       cgd 				return (ret);
    562  1.39   thorpej 			if (LIST_NEXT(p, p_sibling) != NULL) {
    563  1.39   thorpej 				p = LIST_NEXT(p, p_sibling);
    564   1.1       cgd 				break;
    565   1.1       cgd 			}
    566  1.12   mycroft 			p = p->p_pptr;
    567   1.1       cgd 		}
    568   1.1       cgd 	}
    569   1.1       cgd 	/*NOTREACHED*/
    570   1.1       cgd }
    571   1.1       cgd 
    572  1.39   thorpej int
    573  1.42  sommerfe ktrwrite(p, kth)
    574  1.28  christos 	struct proc *p;
    575  1.28  christos 	struct ktr_header *kth;
    576   1.1       cgd {
    577   1.1       cgd 	struct uio auio;
    578   1.1       cgd 	struct iovec aiov[2];
    579  1.42  sommerfe 	int error, tries;
    580  1.42  sommerfe 	struct file *fp = p->p_tracep;
    581   1.1       cgd 
    582  1.42  sommerfe 	if (fp == NULL)
    583  1.42  sommerfe 		return 0;
    584  1.42  sommerfe 
    585   1.1       cgd 	auio.uio_iov = &aiov[0];
    586   1.1       cgd 	auio.uio_offset = 0;
    587   1.1       cgd 	auio.uio_segflg = UIO_SYSSPACE;
    588   1.1       cgd 	auio.uio_rw = UIO_WRITE;
    589   1.1       cgd 	aiov[0].iov_base = (caddr_t)kth;
    590   1.1       cgd 	aiov[0].iov_len = sizeof(struct ktr_header);
    591   1.1       cgd 	auio.uio_resid = sizeof(struct ktr_header);
    592   1.1       cgd 	auio.uio_iovcnt = 1;
    593   1.1       cgd 	auio.uio_procp = (struct proc *)0;
    594   1.1       cgd 	if (kth->ktr_len > 0) {
    595   1.1       cgd 		auio.uio_iovcnt++;
    596   1.1       cgd 		aiov[1].iov_base = kth->ktr_buf;
    597   1.1       cgd 		aiov[1].iov_len = kth->ktr_len;
    598   1.1       cgd 		auio.uio_resid += kth->ktr_len;
    599   1.1       cgd 	}
    600  1.28  christos 
    601  1.42  sommerfe 	FILE_USE(fp);
    602  1.42  sommerfe 
    603  1.42  sommerfe 	tries = 0;
    604  1.42  sommerfe 	do {
    605  1.30   thorpej 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    606  1.30   thorpej 		    fp->f_cred, FOF_UPDATE_OFFSET);
    607  1.42  sommerfe 		tries++;
    608  1.42  sommerfe 		if (error == EWOULDBLOCK)
    609  1.42  sommerfe 		  	yield();
    610  1.42  sommerfe 	} while ((error == EWOULDBLOCK) && (tries < 3));
    611  1.42  sommerfe 	FILE_UNUSE(fp, NULL);
    612  1.28  christos 
    613  1.40   thorpej 	if (__predict_true(error == 0))
    614  1.39   thorpej 		return (0);
    615   1.1       cgd 	/*
    616  1.38   darrenr 	 * If error encountered, give up tracing on this vnode.  Don't report
    617  1.38   darrenr 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    618   1.1       cgd 	 */
    619  1.38   darrenr 	if (error != EPIPE)
    620  1.38   darrenr 		log(LOG_NOTICE,
    621  1.38   darrenr 		    "ktrace write failed, errno %d, tracing stopped\n",
    622  1.38   darrenr 		    error);
    623  1.37   thorpej 	proclist_lock_read();
    624  1.39   thorpej 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    625  1.44  sommerfe 		if (ktrsamefile(p->p_tracep, fp))
    626  1.28  christos 			ktrderef(p);
    627   1.1       cgd 	}
    628  1.36   thorpej 	proclist_unlock_read();
    629  1.39   thorpej 
    630  1.39   thorpej 	return (error);
    631   1.1       cgd }
    632   1.1       cgd 
    633   1.1       cgd /*
    634   1.1       cgd  * Return true if caller has permission to set the ktracing state
    635   1.1       cgd  * of target.  Essentially, the target can't possess any
    636   1.1       cgd  * more permissions than the caller.  KTRFAC_ROOT signifies that
    637   1.1       cgd  * root previously set the tracing status on the target process, and
    638   1.1       cgd  * so, only root may further change it.
    639   1.1       cgd  *
    640   1.1       cgd  * TODO: check groups.  use caller effective gid.
    641   1.1       cgd  */
    642  1.22  christos int
    643   1.1       cgd ktrcanset(callp, targetp)
    644   1.1       cgd 	struct proc *callp, *targetp;
    645   1.1       cgd {
    646  1.28  christos 	struct pcred *caller = callp->p_cred;
    647  1.28  christos 	struct pcred *target = targetp->p_cred;
    648   1.1       cgd 
    649   1.1       cgd 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    650   1.1       cgd 	     target->p_ruid == target->p_svuid &&
    651   1.1       cgd 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    652   1.1       cgd 	     target->p_rgid == target->p_svgid &&
    653   1.1       cgd 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
    654   1.1       cgd 	     caller->pc_ucred->cr_uid == 0)
    655   1.1       cgd 		return (1);
    656   1.1       cgd 
    657   1.1       cgd 	return (0);
    658   1.1       cgd }
    659   1.9       cgd 
    660   1.9       cgd #endif
    661