Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.62
      1  1.62      manu /*	$NetBSD: kern_ktrace.c,v 1.62 2002/12/09 21:29:21 manu 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.55     lukem 
     38  1.55     lukem #include <sys/cdefs.h>
     39  1.62      manu __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.62 2002/12/09 21:29:21 manu Exp $");
     40  1.29   thorpej 
     41  1.29   thorpej #include "opt_ktrace.h"
     42  1.62      manu #include "opt_compat_mach.h"
     43   1.1       cgd 
     44   1.7   mycroft #include <sys/param.h>
     45  1.13       cgd #include <sys/systm.h>
     46   1.7   mycroft #include <sys/proc.h>
     47   1.7   mycroft #include <sys/file.h>
     48   1.7   mycroft #include <sys/namei.h>
     49   1.7   mycroft #include <sys/vnode.h>
     50   1.7   mycroft #include <sys/ktrace.h>
     51   1.7   mycroft #include <sys/malloc.h>
     52   1.7   mycroft #include <sys/syslog.h>
     53  1.28  christos #include <sys/filedesc.h>
     54  1.42  sommerfe #include <sys/ioctl.h>
     55   1.1       cgd 
     56  1.13       cgd #include <sys/mount.h>
     57  1.13       cgd #include <sys/syscallargs.h>
     58  1.22  christos 
     59  1.62      manu #ifdef COMPAT_MACH
     60  1.62      manu #include <compat/mach/mach_types.h>
     61  1.62      manu #include <compat/mach/mach_message.h>
     62  1.62      manu #endif
     63  1.62      manu 
     64  1.51  jdolecek #ifdef KTRACE
     65  1.51  jdolecek 
     66  1.47   thorpej int	ktrace_common(struct proc *, int, int, int, struct file *);
     67  1.47   thorpej void	ktrinitheader(struct ktr_header *, struct proc *, int);
     68  1.47   thorpej int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     69  1.47   thorpej int	ktrsetchildren(struct proc *, struct proc *, int, int,
     70  1.47   thorpej 	    struct file *);
     71  1.47   thorpej int	ktrwrite(struct proc *, struct ktr_header *);
     72  1.47   thorpej int	ktrcanset(struct proc *, struct proc *);
     73  1.47   thorpej int	ktrsamefile(struct file *, struct file *);
     74  1.44  sommerfe 
     75  1.44  sommerfe /*
     76  1.44  sommerfe  * "deep" compare of two files for the purposes of clearing a trace.
     77  1.44  sommerfe  * Returns true if they're the same open file, or if they point at the
     78  1.44  sommerfe  * same underlying vnode/socket.
     79  1.44  sommerfe  */
     80  1.44  sommerfe 
     81  1.44  sommerfe int
     82  1.62      manu ktrsamefile(f1, f2)
     83  1.62      manu 	struct file *f1;
     84  1.62      manu 	struct file *f2;
     85  1.44  sommerfe {
     86  1.44  sommerfe 	return ((f1 == f2) ||
     87  1.45  sommerfe 	    ((f1 != NULL) && (f2 != NULL) &&
     88  1.45  sommerfe 		(f1->f_type == f2->f_type) &&
     89  1.44  sommerfe 		(f1->f_data == f2->f_data)));
     90  1.44  sommerfe }
     91  1.22  christos 
     92  1.28  christos void
     93  1.62      manu ktrderef(p)
     94  1.62      manu 	struct proc *p;
     95  1.28  christos {
     96  1.42  sommerfe 	struct file *fp = p->p_tracep;
     97  1.42  sommerfe 	p->p_traceflag = 0;
     98  1.42  sommerfe 	if (fp == NULL)
     99  1.28  christos 		return;
    100  1.42  sommerfe 	FILE_USE(fp);
    101  1.59  jdolecek 
    102  1.59  jdolecek 	/*
    103  1.59  jdolecek 	 * ktrace file descriptor can't be watched (are not visible to
    104  1.59  jdolecek 	 * userspace), so no kqueue stuff here
    105  1.59  jdolecek 	 */
    106  1.42  sommerfe 	closef(fp, NULL);
    107  1.28  christos 
    108  1.28  christos 	p->p_tracep = NULL;
    109  1.28  christos }
    110  1.28  christos 
    111  1.28  christos void
    112  1.62      manu ktradref(p)
    113  1.62      manu 	struct proc *p;
    114  1.28  christos {
    115  1.42  sommerfe 	struct file *fp = p->p_tracep;
    116  1.28  christos 
    117  1.42  sommerfe 	fp->f_count++;
    118  1.28  christos }
    119  1.28  christos 
    120  1.39   thorpej void
    121  1.62      manu ktrinitheader(kth, p, type)
    122  1.62      manu 	struct ktr_header *kth;
    123  1.62      manu 	struct proc *p;
    124  1.62      manu 	int type;
    125   1.1       cgd {
    126   1.1       cgd 
    127  1.39   thorpej 	memset(kth, 0, sizeof(*kth));
    128   1.1       cgd 	kth->ktr_type = type;
    129   1.1       cgd 	microtime(&kth->ktr_time);
    130   1.1       cgd 	kth->ktr_pid = p->p_pid;
    131  1.32     perry 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    132   1.1       cgd }
    133   1.1       cgd 
    134  1.17       cgd void
    135  1.62      manu ktrsyscall(p, code, realcode, args)
    136  1.62      manu 	struct proc *p;
    137  1.62      manu 	register_t code;
    138  1.62      manu 	register_t realcode;
    139  1.62      manu 	register_t args[];
    140   1.1       cgd {
    141  1.39   thorpej 	struct ktr_header kth;
    142  1.39   thorpej 	struct ktr_syscall *ktp;
    143  1.17       cgd 	register_t *argp;
    144  1.57      fvdl 	int argsize;
    145  1.57      fvdl 	size_t len;
    146  1.60   thorpej 	u_int i;
    147  1.57      fvdl 
    148  1.57      fvdl 	argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
    149  1.57      fvdl 	len = sizeof(struct ktr_syscall) + argsize;
    150   1.1       cgd 
    151   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    152  1.39   thorpej 	ktrinitheader(&kth, p, KTR_SYSCALL);
    153  1.39   thorpej 	ktp = malloc(len, M_TEMP, M_WAITOK);
    154  1.61      manu 	ktp->ktr_code = realcode;
    155  1.17       cgd 	ktp->ktr_argsize = argsize;
    156  1.17       cgd 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    157  1.31     perry 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    158   1.1       cgd 		*argp++ = args[i];
    159  1.39   thorpej 	kth.ktr_buf = (caddr_t)ktp;
    160  1.39   thorpej 	kth.ktr_len = len;
    161  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    162  1.39   thorpej 	free(ktp, M_TEMP);
    163   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    164   1.1       cgd }
    165   1.1       cgd 
    166  1.17       cgd void
    167  1.62      manu ktrsysret(p, code, error, retval)
    168  1.62      manu 	struct proc *p;
    169  1.62      manu 	register_t code;
    170  1.62      manu 	int error;
    171  1.62      manu 	register_t retval;
    172   1.1       cgd {
    173  1.39   thorpej 	struct ktr_header kth;
    174   1.1       cgd 	struct ktr_sysret ktp;
    175   1.1       cgd 
    176   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    177  1.39   thorpej 	ktrinitheader(&kth, p, KTR_SYSRET);
    178   1.1       cgd 	ktp.ktr_code = code;
    179  1.34    kleink 	ktp.ktr_eosys = 0;			/* XXX unused */
    180   1.1       cgd 	ktp.ktr_error = error;
    181   1.1       cgd 	ktp.ktr_retval = retval;		/* what about val2 ? */
    182   1.1       cgd 
    183  1.39   thorpej 	kth.ktr_buf = (caddr_t)&ktp;
    184  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_sysret);
    185   1.1       cgd 
    186  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    187   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    188   1.1       cgd }
    189   1.1       cgd 
    190  1.17       cgd void
    191  1.62      manu ktrnamei(p, path)
    192  1.62      manu 	struct proc *p;
    193  1.62      manu 	char *path;
    194   1.1       cgd {
    195  1.39   thorpej 	struct ktr_header kth;
    196   1.1       cgd 
    197   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    198  1.39   thorpej 	ktrinitheader(&kth, p, KTR_NAMEI);
    199  1.39   thorpej 	kth.ktr_len = strlen(path);
    200  1.39   thorpej 	kth.ktr_buf = path;
    201  1.18  christos 
    202  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    203  1.18  christos 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    204  1.18  christos }
    205  1.18  christos 
    206  1.18  christos void
    207  1.62      manu ktremul(p)
    208  1.62      manu 	struct proc *p;
    209  1.18  christos {
    210  1.39   thorpej 	struct ktr_header kth;
    211  1.50       scw 	const char *emul = p->p_emul->e_name;
    212  1.18  christos 
    213  1.18  christos 	p->p_traceflag |= KTRFAC_ACTIVE;
    214  1.39   thorpej 	ktrinitheader(&kth, p, KTR_EMUL);
    215  1.39   thorpej 	kth.ktr_len = strlen(emul);
    216  1.50       scw 	kth.ktr_buf = (caddr_t)emul;
    217   1.1       cgd 
    218  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    219   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    220   1.1       cgd }
    221   1.1       cgd 
    222  1.17       cgd void
    223  1.62      manu ktrgenio(p, fd, rw, iov, len, error)
    224  1.62      manu 	struct proc *p;
    225  1.62      manu 	int fd;
    226  1.62      manu 	enum uio_rw rw;
    227  1.62      manu 	struct iovec *iov;
    228  1.62      manu 	int len;
    229  1.62      manu 	int error;
    230   1.1       cgd {
    231  1.39   thorpej 	struct ktr_header kth;
    232  1.28  christos 	struct ktr_genio *ktp;
    233  1.28  christos 	caddr_t cp;
    234  1.28  christos 	int resid = len, cnt;
    235  1.39   thorpej 	int buflen;
    236  1.39   thorpej 
    237   1.1       cgd 	if (error)
    238   1.1       cgd 		return;
    239  1.39   thorpej 
    240   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    241  1.39   thorpej 
    242  1.39   thorpej 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    243  1.39   thorpej 
    244  1.39   thorpej 	ktrinitheader(&kth, p, KTR_GENIO);
    245  1.39   thorpej 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    246   1.1       cgd 	ktp->ktr_fd = fd;
    247   1.1       cgd 	ktp->ktr_rw = rw;
    248  1.39   thorpej 
    249  1.39   thorpej 	kth.ktr_buf = (caddr_t)ktp;
    250  1.39   thorpej 
    251  1.31     perry 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    252  1.39   thorpej 	buflen -= sizeof(struct ktr_genio);
    253  1.39   thorpej 
    254   1.1       cgd 	while (resid > 0) {
    255  1.46   thorpej 		KDASSERT(p->p_cpu != NULL);
    256  1.46   thorpej 		KDASSERT(p->p_cpu == curcpu());
    257  1.46   thorpej 		if (p->p_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    258  1.39   thorpej 			preempt(NULL);
    259  1.39   thorpej 
    260  1.39   thorpej 		cnt = min(iov->iov_len, buflen);
    261  1.39   thorpej 		if (cnt > resid)
    262   1.1       cgd 			cnt = resid;
    263  1.39   thorpej 		if (copyin(iov->iov_base, cp, cnt))
    264  1.39   thorpej 			break;
    265  1.39   thorpej 
    266  1.39   thorpej 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    267  1.39   thorpej 
    268  1.42  sommerfe 		if (__predict_false(ktrwrite(p, &kth) != 0))
    269  1.39   thorpej 			break;
    270  1.39   thorpej 
    271  1.39   thorpej 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    272  1.39   thorpej 		iov->iov_len -= cnt;
    273  1.39   thorpej 
    274  1.39   thorpej 		if (iov->iov_len == 0)
    275  1.39   thorpej 			iov++;
    276  1.39   thorpej 
    277   1.1       cgd 		resid -= cnt;
    278   1.1       cgd 	}
    279   1.1       cgd 
    280  1.39   thorpej 	free(ktp, M_TEMP);
    281   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    282   1.1       cgd }
    283   1.1       cgd 
    284  1.17       cgd void
    285  1.62      manu ktrpsig(p, sig, action, mask, code)
    286  1.62      manu 	struct proc *p;
    287  1.62      manu 	int sig;
    288  1.62      manu 	sig_t action;
    289  1.62      manu 	sigset_t *mask;
    290  1.62      manu 	int code;
    291   1.1       cgd {
    292  1.39   thorpej 	struct ktr_header kth;
    293   1.1       cgd 	struct ktr_psig	kp;
    294   1.1       cgd 
    295   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    296  1.39   thorpej 	ktrinitheader(&kth, p, KTR_PSIG);
    297   1.1       cgd 	kp.signo = (char)sig;
    298   1.1       cgd 	kp.action = action;
    299  1.33   mycroft 	kp.mask = *mask;
    300   1.1       cgd 	kp.code = code;
    301  1.39   thorpej 	kth.ktr_buf = (caddr_t)&kp;
    302  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_psig);
    303   1.1       cgd 
    304  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    305   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    306   1.9       cgd }
    307   1.9       cgd 
    308  1.17       cgd void
    309  1.62      manu ktrcsw(p, out, user)
    310  1.62      manu 	struct proc *p;
    311  1.62      manu 	int out;
    312  1.62      manu 	int user;
    313   1.9       cgd {
    314  1.39   thorpej 	struct ktr_header kth;
    315  1.39   thorpej 	struct ktr_csw kc;
    316   1.9       cgd 
    317   1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    318  1.39   thorpej 	ktrinitheader(&kth, p, KTR_CSW);
    319   1.9       cgd 	kc.out = out;
    320   1.9       cgd 	kc.user = user;
    321  1.39   thorpej 	kth.ktr_buf = (caddr_t)&kc;
    322  1.39   thorpej 	kth.ktr_len = sizeof(struct ktr_csw);
    323   1.9       cgd 
    324  1.42  sommerfe 	(void) ktrwrite(p, &kth);
    325   1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    326   1.1       cgd }
    327   1.1       cgd 
    328  1.51  jdolecek void
    329  1.51  jdolecek ktruser(p, id, addr, len, ustr)
    330  1.51  jdolecek 	struct proc *p;
    331  1.51  jdolecek 	const char *id;
    332  1.51  jdolecek 	void *addr;
    333  1.51  jdolecek 	size_t len;
    334  1.51  jdolecek 	int ustr;
    335  1.51  jdolecek {
    336  1.51  jdolecek 	struct ktr_header kth;
    337  1.51  jdolecek 	struct ktr_user *ktp;
    338  1.51  jdolecek 	caddr_t user_dta;
    339  1.51  jdolecek 
    340  1.51  jdolecek 	p->p_traceflag |= KTRFAC_ACTIVE;
    341  1.51  jdolecek 	ktrinitheader(&kth, p, KTR_USER);
    342  1.51  jdolecek 	ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
    343  1.51  jdolecek 	if (ustr) {
    344  1.51  jdolecek 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    345  1.51  jdolecek 			ktp->ktr_id[0] = '\0';
    346  1.51  jdolecek 	} else
    347  1.51  jdolecek 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    348  1.51  jdolecek 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    349  1.51  jdolecek 
    350  1.51  jdolecek 	user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
    351  1.51  jdolecek 	if (copyin(addr, (void *) user_dta, len) != 0)
    352  1.51  jdolecek 		len = 0;
    353  1.51  jdolecek 
    354  1.51  jdolecek 	kth.ktr_buf = (void *)ktp;
    355  1.51  jdolecek 	kth.ktr_len = sizeof(struct ktr_user) + len;
    356  1.51  jdolecek 	(void) ktrwrite(p, &kth);
    357  1.51  jdolecek 
    358  1.51  jdolecek 	free(ktp, M_TEMP);
    359  1.51  jdolecek 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    360  1.51  jdolecek 
    361  1.51  jdolecek }
    362  1.51  jdolecek 
    363  1.62      manu #ifdef COMPAT_MACH
    364  1.62      manu void
    365  1.62      manu ktrmmsg(p, msgh, size)
    366  1.62      manu 	struct proc *p;
    367  1.62      manu 	const char *msgh;
    368  1.62      manu 	size_t size;
    369  1.62      manu {
    370  1.62      manu 	struct ktr_header kth;
    371  1.62      manu 	struct ktr_mmsg	*kp;
    372  1.62      manu 	int error;
    373  1.62      manu 
    374  1.62      manu 	p->p_traceflag |= KTRFAC_ACTIVE;
    375  1.62      manu 	ktrinitheader(&kth, p, KTR_MMSG);
    376  1.62      manu 
    377  1.62      manu 	kp = (struct ktr_mmsg *)malloc(size, M_TEMP, M_WAITOK);
    378  1.62      manu 	if ((error = copyin(msgh, kp, size)) != 0)
    379  1.62      manu 		size = 0; /* Still log a message, but empty */
    380  1.62      manu 
    381  1.62      manu 	kth.ktr_buf = (caddr_t)kp;
    382  1.62      manu 	kth.ktr_len = size;
    383  1.62      manu 	(void) ktrwrite(p, &kth);
    384  1.62      manu 	free(kp, M_TEMP);
    385  1.62      manu 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    386  1.62      manu }
    387  1.62      manu #endif /* COMPAT_MACH */
    388  1.62      manu 
    389   1.1       cgd /* Interface and common routines */
    390   1.1       cgd 
    391  1.17       cgd int
    392  1.62      manu ktrace_common(curp, ops, facs, pid, fp)
    393  1.62      manu 	struct proc *curp;
    394  1.62      manu 	int ops;
    395  1.62      manu 	int facs;
    396  1.62      manu 	int pid;
    397  1.62      manu 	struct file *fp;
    398  1.28  christos {
    399  1.42  sommerfe 	int ret = 0;
    400  1.42  sommerfe 	int error = 0;
    401  1.42  sommerfe 	int one = 1;
    402  1.42  sommerfe 	int descend;
    403  1.28  christos 	struct proc *p;
    404  1.28  christos 	struct pgrp *pg;
    405  1.28  christos 
    406  1.28  christos 	curp->p_traceflag |= KTRFAC_ACTIVE;
    407  1.42  sommerfe 	descend = ops & KTRFLAG_DESCEND;
    408  1.42  sommerfe 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    409  1.28  christos 
    410  1.28  christos 	/*
    411  1.28  christos 	 * Clear all uses of the tracefile
    412  1.28  christos 	 */
    413  1.28  christos 	if (KTROP(ops) == KTROP_CLEARFILE) {
    414  1.37   thorpej 		proclist_lock_read();
    415  1.39   thorpej 		for (p = LIST_FIRST(&allproc); p != NULL;
    416  1.39   thorpej 		     p = LIST_NEXT(p, p_list)) {
    417  1.44  sommerfe 			if (ktrsamefile(p->p_tracep, fp)) {
    418  1.28  christos 				if (ktrcanset(curp, p))
    419  1.28  christos 					ktrderef(p);
    420  1.28  christos 				else
    421  1.28  christos 					error = EPERM;
    422  1.28  christos 			}
    423  1.28  christos 		}
    424  1.36   thorpej 		proclist_unlock_read();
    425  1.28  christos 		goto done;
    426  1.28  christos 	}
    427  1.42  sommerfe 
    428  1.42  sommerfe 	/*
    429  1.42  sommerfe 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    430  1.42  sommerfe 	 */
    431  1.42  sommerfe 
    432  1.43  sommerfe 	if (fp != NULL) {
    433  1.43  sommerfe 		fp->f_flag |= FNONBLOCK;
    434  1.43  sommerfe 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    435  1.43  sommerfe 	}
    436  1.42  sommerfe 
    437  1.28  christos 	/*
    438  1.28  christos 	 * need something to (un)trace (XXX - why is this here?)
    439  1.28  christos 	 */
    440  1.28  christos 	if (!facs) {
    441  1.28  christos 		error = EINVAL;
    442  1.28  christos 		goto done;
    443  1.28  christos 	}
    444  1.28  christos 	/*
    445  1.28  christos 	 * do it
    446  1.28  christos 	 */
    447  1.42  sommerfe 	if (pid < 0) {
    448  1.28  christos 		/*
    449  1.28  christos 		 * by process group
    450  1.28  christos 		 */
    451  1.42  sommerfe 		pg = pgfind(-pid);
    452  1.28  christos 		if (pg == NULL) {
    453  1.28  christos 			error = ESRCH;
    454  1.28  christos 			goto done;
    455  1.28  christos 		}
    456  1.39   thorpej 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    457  1.39   thorpej 		     p = LIST_NEXT(p, p_pglist)) {
    458  1.28  christos 			if (descend)
    459  1.28  christos 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    460  1.28  christos 			else
    461  1.28  christos 				ret |= ktrops(curp, p, ops, facs, fp);
    462  1.39   thorpej 		}
    463  1.28  christos 
    464  1.28  christos 	} else {
    465  1.28  christos 		/*
    466  1.28  christos 		 * by pid
    467  1.28  christos 		 */
    468  1.42  sommerfe 		p = pfind(pid);
    469  1.28  christos 		if (p == NULL) {
    470  1.28  christos 			error = ESRCH;
    471  1.28  christos 			goto done;
    472  1.28  christos 		}
    473  1.28  christos 		if (descend)
    474  1.28  christos 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    475  1.28  christos 		else
    476  1.28  christos 			ret |= ktrops(curp, p, ops, facs, fp);
    477  1.28  christos 	}
    478  1.28  christos 	if (!ret)
    479  1.28  christos 		error = EPERM;
    480  1.28  christos done:
    481  1.28  christos 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    482  1.28  christos 	return (error);
    483  1.28  christos }
    484  1.28  christos 
    485  1.28  christos /*
    486  1.28  christos  * ktrace system call
    487  1.28  christos  */
    488  1.28  christos /* ARGSUSED */
    489  1.28  christos int
    490  1.62      manu sys_fktrace(curp, v, retval)
    491  1.62      manu 	struct proc *curp;
    492  1.62      manu 	void *v;
    493  1.62      manu 	register_t *retval;
    494  1.42  sommerfe {
    495  1.42  sommerfe 	struct sys_fktrace_args /* {
    496  1.42  sommerfe 		syscallarg(int) fd;
    497  1.42  sommerfe 		syscallarg(int) ops;
    498  1.42  sommerfe 		syscallarg(int) facs;
    499  1.42  sommerfe 		syscallarg(int) pid;
    500  1.42  sommerfe 	} */ *uap = v;
    501  1.42  sommerfe 	struct file *fp = NULL;
    502  1.42  sommerfe 	struct filedesc *fdp = curp->p_fd;
    503  1.42  sommerfe 
    504  1.54   thorpej 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    505  1.54   thorpej 		return (EBADF);
    506  1.54   thorpej 
    507  1.54   thorpej 	if ((fp->f_flag & FWRITE) == 0)
    508  1.42  sommerfe 		return (EBADF);
    509  1.42  sommerfe 
    510  1.42  sommerfe 	return ktrace_common(curp, SCARG(uap, ops),
    511  1.42  sommerfe 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    512  1.42  sommerfe }
    513  1.42  sommerfe 
    514  1.42  sommerfe /*
    515  1.42  sommerfe  * ktrace system call
    516  1.42  sommerfe  */
    517  1.42  sommerfe /* ARGSUSED */
    518  1.42  sommerfe int
    519  1.62      manu sys_ktrace(curp, v, retval)
    520  1.62      manu 	struct proc *curp;
    521  1.62      manu 	void *v;
    522  1.62      manu 	register_t *retval;
    523  1.19   thorpej {
    524  1.28  christos 	struct sys_ktrace_args /* {
    525  1.24   mycroft 		syscallarg(const char *) fname;
    526  1.13       cgd 		syscallarg(int) ops;
    527  1.13       cgd 		syscallarg(int) facs;
    528  1.13       cgd 		syscallarg(int) pid;
    529  1.19   thorpej 	} */ *uap = v;
    530  1.28  christos 	struct vnode *vp = NULL;
    531  1.42  sommerfe 	struct file *fp = NULL;
    532  1.42  sommerfe 	int fd;
    533  1.42  sommerfe 	int ops = SCARG(uap, ops);
    534   1.1       cgd 	int error = 0;
    535   1.1       cgd 	struct nameidata nd;
    536   1.1       cgd 
    537  1.42  sommerfe 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    538  1.42  sommerfe 
    539   1.9       cgd 	curp->p_traceflag |= KTRFAC_ACTIVE;
    540   1.1       cgd 	if (ops != KTROP_CLEAR) {
    541   1.1       cgd 		/*
    542   1.1       cgd 		 * an operation which requires a file argument.
    543   1.1       cgd 		 */
    544  1.13       cgd 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    545  1.13       cgd 		    curp);
    546  1.22  christos 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    547   1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    548   1.1       cgd 			return (error);
    549   1.9       cgd 		}
    550   1.1       cgd 		vp = nd.ni_vp;
    551  1.25      fvdl 		VOP_UNLOCK(vp, 0);
    552   1.1       cgd 		if (vp->v_type != VREG) {
    553   1.1       cgd 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    554   1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    555   1.1       cgd 			return (EACCES);
    556   1.1       cgd 		}
    557   1.1       cgd 		/*
    558  1.42  sommerfe 		 * XXX This uses up a file descriptor slot in the
    559  1.42  sommerfe 		 * tracing process for the duration of this syscall.
    560  1.42  sommerfe 		 * This is not expected to be a problem.  If
    561  1.42  sommerfe 		 * falloc(NULL, ...) DTRT we could skip that part, but
    562  1.42  sommerfe 		 * that would require changing its interface to allow
    563  1.42  sommerfe 		 * the caller to pass in a ucred..
    564  1.42  sommerfe 		 *
    565  1.42  sommerfe 		 * This will FILE_USE the fp it returns, if any.
    566  1.42  sommerfe 		 * Keep it in use until we return.
    567   1.1       cgd 		 */
    568  1.42  sommerfe 		if ((error = falloc(curp, &fp, &fd)) != 0)
    569   1.1       cgd 			goto done;
    570  1.42  sommerfe 
    571  1.42  sommerfe 		fp->f_flag = FWRITE|FAPPEND;
    572  1.42  sommerfe 		fp->f_type = DTYPE_VNODE;
    573  1.42  sommerfe 		fp->f_ops = &vnops;
    574  1.42  sommerfe 		fp->f_data = (caddr_t)vp;
    575  1.54   thorpej 		FILE_SET_MATURE(fp);
    576  1.42  sommerfe 		vp = NULL;
    577  1.42  sommerfe 	}
    578  1.42  sommerfe 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    579  1.42  sommerfe 	    SCARG(uap, pid), fp);
    580  1.42  sommerfe done:
    581   1.1       cgd 	if (vp != NULL)
    582   1.1       cgd 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    583  1.42  sommerfe 	if (fp != NULL) {
    584  1.44  sommerfe 		FILE_UNUSE(fp, curp);	/* release file */
    585  1.42  sommerfe 		fdrelease(curp, fd); 	/* release fd table slot */
    586  1.42  sommerfe 	}
    587   1.1       cgd 	return (error);
    588   1.1       cgd }
    589   1.1       cgd 
    590   1.4    andrew int
    591  1.62      manu ktrops(curp, p, ops, facs, fp)
    592  1.62      manu 	struct proc *curp;
    593  1.62      manu 	struct proc *p;
    594  1.62      manu 	int ops;
    595  1.62      manu 	int facs;
    596  1.62      manu 	struct file *fp;
    597   1.1       cgd {
    598   1.1       cgd 
    599   1.1       cgd 	if (!ktrcanset(curp, p))
    600   1.1       cgd 		return (0);
    601  1.28  christos 	if (KTROP(ops) == KTROP_SET) {
    602  1.42  sommerfe 		if (p->p_tracep != fp) {
    603   1.1       cgd 			/*
    604   1.1       cgd 			 * if trace file already in use, relinquish
    605   1.1       cgd 			 */
    606  1.28  christos 			ktrderef(p);
    607  1.42  sommerfe 			p->p_tracep = fp;
    608  1.28  christos 			ktradref(p);
    609   1.1       cgd 		}
    610   1.1       cgd 		p->p_traceflag |= facs;
    611   1.1       cgd 		if (curp->p_ucred->cr_uid == 0)
    612   1.1       cgd 			p->p_traceflag |= KTRFAC_ROOT;
    613   1.1       cgd 	} else {
    614   1.1       cgd 		/* KTROP_CLEAR */
    615   1.1       cgd 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    616   1.1       cgd 			/* no more tracing */
    617  1.28  christos 			ktrderef(p);
    618   1.1       cgd 		}
    619   1.1       cgd 	}
    620  1.21  christos 
    621  1.21  christos 	/*
    622  1.21  christos 	 * Emit an emulation record, every time there is a ktrace
    623  1.21  christos 	 * change/attach request.
    624  1.21  christos 	 */
    625  1.21  christos 	if (KTRPOINT(p, KTR_EMUL))
    626  1.42  sommerfe 		ktremul(p);
    627  1.49    martin #ifdef __HAVE_SYSCALL_INTERN
    628  1.48   mycroft 	(*p->p_emul->e_syscall_intern)(p);
    629  1.49    martin #endif
    630   1.1       cgd 
    631   1.1       cgd 	return (1);
    632   1.1       cgd }
    633   1.1       cgd 
    634  1.22  christos int
    635  1.62      manu ktrsetchildren(curp, top, ops, facs, fp)
    636  1.62      manu 	struct proc *curp;
    637  1.62      manu 	struct proc *top;
    638  1.62      manu 	int ops;
    639  1.62      manu 	int facs;
    640  1.62      manu 	struct file *fp;
    641   1.1       cgd {
    642  1.28  christos 	struct proc *p;
    643  1.28  christos 	int ret = 0;
    644   1.1       cgd 
    645   1.1       cgd 	p = top;
    646   1.1       cgd 	for (;;) {
    647  1.42  sommerfe 		ret |= ktrops(curp, p, ops, facs, fp);
    648   1.1       cgd 		/*
    649   1.1       cgd 		 * If this process has children, descend to them next,
    650   1.1       cgd 		 * otherwise do any siblings, and if done with this level,
    651   1.1       cgd 		 * follow back up the tree (but not past top).
    652   1.1       cgd 		 */
    653  1.39   thorpej 		if (LIST_FIRST(&p->p_children) != NULL)
    654  1.39   thorpej 			p = LIST_FIRST(&p->p_children);
    655   1.1       cgd 		else for (;;) {
    656   1.1       cgd 			if (p == top)
    657   1.1       cgd 				return (ret);
    658  1.39   thorpej 			if (LIST_NEXT(p, p_sibling) != NULL) {
    659  1.39   thorpej 				p = LIST_NEXT(p, p_sibling);
    660   1.1       cgd 				break;
    661   1.1       cgd 			}
    662  1.12   mycroft 			p = p->p_pptr;
    663   1.1       cgd 		}
    664   1.1       cgd 	}
    665   1.1       cgd 	/*NOTREACHED*/
    666   1.1       cgd }
    667   1.1       cgd 
    668  1.39   thorpej int
    669  1.62      manu ktrwrite(p, kth)
    670  1.62      manu 	struct proc *p;
    671  1.62      manu 	struct ktr_header *kth;
    672   1.1       cgd {
    673   1.1       cgd 	struct uio auio;
    674   1.1       cgd 	struct iovec aiov[2];
    675  1.42  sommerfe 	int error, tries;
    676  1.42  sommerfe 	struct file *fp = p->p_tracep;
    677   1.1       cgd 
    678  1.42  sommerfe 	if (fp == NULL)
    679  1.42  sommerfe 		return 0;
    680  1.42  sommerfe 
    681   1.1       cgd 	auio.uio_iov = &aiov[0];
    682   1.1       cgd 	auio.uio_offset = 0;
    683   1.1       cgd 	auio.uio_segflg = UIO_SYSSPACE;
    684   1.1       cgd 	auio.uio_rw = UIO_WRITE;
    685   1.1       cgd 	aiov[0].iov_base = (caddr_t)kth;
    686   1.1       cgd 	aiov[0].iov_len = sizeof(struct ktr_header);
    687   1.1       cgd 	auio.uio_resid = sizeof(struct ktr_header);
    688   1.1       cgd 	auio.uio_iovcnt = 1;
    689   1.1       cgd 	auio.uio_procp = (struct proc *)0;
    690   1.1       cgd 	if (kth->ktr_len > 0) {
    691   1.1       cgd 		auio.uio_iovcnt++;
    692   1.1       cgd 		aiov[1].iov_base = kth->ktr_buf;
    693   1.1       cgd 		aiov[1].iov_len = kth->ktr_len;
    694   1.1       cgd 		auio.uio_resid += kth->ktr_len;
    695   1.1       cgd 	}
    696  1.28  christos 
    697  1.42  sommerfe 	FILE_USE(fp);
    698  1.42  sommerfe 
    699  1.42  sommerfe 	tries = 0;
    700  1.42  sommerfe 	do {
    701  1.30   thorpej 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    702  1.30   thorpej 		    fp->f_cred, FOF_UPDATE_OFFSET);
    703  1.42  sommerfe 		tries++;
    704  1.42  sommerfe 		if (error == EWOULDBLOCK)
    705  1.42  sommerfe 		  	yield();
    706  1.42  sommerfe 	} while ((error == EWOULDBLOCK) && (tries < 3));
    707  1.42  sommerfe 	FILE_UNUSE(fp, NULL);
    708  1.28  christos 
    709  1.40   thorpej 	if (__predict_true(error == 0))
    710  1.39   thorpej 		return (0);
    711   1.1       cgd 	/*
    712  1.38   darrenr 	 * If error encountered, give up tracing on this vnode.  Don't report
    713  1.38   darrenr 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    714   1.1       cgd 	 */
    715  1.38   darrenr 	if (error != EPIPE)
    716  1.38   darrenr 		log(LOG_NOTICE,
    717  1.38   darrenr 		    "ktrace write failed, errno %d, tracing stopped\n",
    718  1.38   darrenr 		    error);
    719  1.37   thorpej 	proclist_lock_read();
    720  1.39   thorpej 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    721  1.44  sommerfe 		if (ktrsamefile(p->p_tracep, fp))
    722  1.28  christos 			ktrderef(p);
    723   1.1       cgd 	}
    724  1.36   thorpej 	proclist_unlock_read();
    725  1.39   thorpej 
    726  1.39   thorpej 	return (error);
    727   1.1       cgd }
    728   1.1       cgd 
    729   1.1       cgd /*
    730   1.1       cgd  * Return true if caller has permission to set the ktracing state
    731   1.1       cgd  * of target.  Essentially, the target can't possess any
    732   1.1       cgd  * more permissions than the caller.  KTRFAC_ROOT signifies that
    733   1.1       cgd  * root previously set the tracing status on the target process, and
    734   1.1       cgd  * so, only root may further change it.
    735   1.1       cgd  *
    736   1.1       cgd  * TODO: check groups.  use caller effective gid.
    737   1.1       cgd  */
    738  1.22  christos int
    739  1.62      manu ktrcanset(callp, targetp)
    740  1.62      manu 	struct proc *callp;
    741  1.62      manu 	struct proc *targetp;
    742   1.1       cgd {
    743  1.28  christos 	struct pcred *caller = callp->p_cred;
    744  1.28  christos 	struct pcred *target = targetp->p_cred;
    745   1.1       cgd 
    746   1.1       cgd 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    747   1.1       cgd 	     target->p_ruid == target->p_svuid &&
    748   1.1       cgd 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    749   1.1       cgd 	     target->p_rgid == target->p_svgid &&
    750  1.58    itojun 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    751  1.58    itojun 	     (targetp->p_flag & P_SUGID) == 0) ||
    752   1.1       cgd 	     caller->pc_ucred->cr_uid == 0)
    753   1.1       cgd 		return (1);
    754   1.1       cgd 
    755   1.1       cgd 	return (0);
    756   1.1       cgd }
    757  1.47   thorpej #endif /* KTRACE */
    758  1.51  jdolecek 
    759  1.51  jdolecek /*
    760  1.51  jdolecek  * Put user defined entry to ktrace records.
    761  1.51  jdolecek  */
    762  1.51  jdolecek int
    763  1.51  jdolecek sys_utrace(p, v, retval)
    764  1.51  jdolecek 	struct proc *p;
    765  1.51  jdolecek 	void *v;
    766  1.51  jdolecek 	register_t *retval;
    767  1.51  jdolecek {
    768  1.51  jdolecek #ifdef KTRACE
    769  1.51  jdolecek 	struct sys_utrace_args /* {
    770  1.52  jdolecek 		syscallarg(const char *) label;
    771  1.51  jdolecek 		syscallarg(void *) addr;
    772  1.51  jdolecek 		syscallarg(size_t) len;
    773  1.51  jdolecek 	} */ *uap = v;
    774  1.51  jdolecek 
    775  1.51  jdolecek 	if (!KTRPOINT(p, KTR_USER))
    776  1.51  jdolecek 		return (0);
    777  1.53  jdolecek 
    778  1.53  jdolecek 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    779  1.53  jdolecek 		return (EINVAL);
    780  1.51  jdolecek 
    781  1.52  jdolecek 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    782  1.51  jdolecek 
    783  1.51  jdolecek 	return (0);
    784  1.51  jdolecek #else /* !KTRACE */
    785  1.51  jdolecek 	return ENOSYS;
    786  1.51  jdolecek #endif /* KTRACE */
    787  1.51  jdolecek }
    788