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