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