kern_ktrace.c revision 147576
1139804Simp/*-
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
301541Srgrimes */
311541Srgrimes
32116182Sobrien#include <sys/cdefs.h>
33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_ktrace.c 147576 2005-06-24 12:05:24Z pjd $");
34116182Sobrien
3513203Swollman#include "opt_ktrace.h"
36101123Srwatson#include "opt_mac.h"
371541Srgrimes
381541Srgrimes#include <sys/param.h>
392112Swollman#include <sys/systm.h>
4097993Sjhb#include <sys/fcntl.h>
4197993Sjhb#include <sys/kernel.h>
4297993Sjhb#include <sys/kthread.h>
4376166Smarkm#include <sys/lock.h>
4476166Smarkm#include <sys/mutex.h>
45101123Srwatson#include <sys/mac.h>
4697993Sjhb#include <sys/malloc.h>
4797993Sjhb#include <sys/namei.h>
481541Srgrimes#include <sys/proc.h>
4997993Sjhb#include <sys/unistd.h>
501541Srgrimes#include <sys/vnode.h>
511541Srgrimes#include <sys/ktrace.h>
5274927Sjhb#include <sys/sx.h>
5397993Sjhb#include <sys/sysctl.h>
541541Srgrimes#include <sys/syslog.h>
5597993Sjhb#include <sys/sysproto.h>
561541Srgrimes
5730354Sphkstatic MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
5830309Sphk
5913203Swollman#ifdef KTRACE
6012577Sbde
6197993Sjhb#ifndef KTRACE_REQUEST_POOL
6297993Sjhb#define	KTRACE_REQUEST_POOL	100
6397993Sjhb#endif
6412819Sphk
6597993Sjhbstruct ktr_request {
6697993Sjhb	struct	ktr_header ktr_header;
6797993Sjhb	struct	ucred *ktr_cred;
6897993Sjhb	struct	vnode *ktr_vp;
6997993Sjhb	union {
7097993Sjhb		struct	ktr_syscall ktr_syscall;
7197993Sjhb		struct	ktr_sysret ktr_sysret;
7297993Sjhb		struct	ktr_genio ktr_genio;
7397993Sjhb		struct	ktr_psig ktr_psig;
7497993Sjhb		struct	ktr_csw ktr_csw;
7597993Sjhb	} ktr_data;
7697993Sjhb	STAILQ_ENTRY(ktr_request) ktr_list;
7797993Sjhb};
7897993Sjhb
7997993Sjhbstatic int data_lengths[] = {
8097993Sjhb	0,					/* none */
8197993Sjhb	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
8297993Sjhb	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
8397993Sjhb	0,					/* KTR_NAMEI */
8497993Sjhb	sizeof(struct ktr_genio),		/* KTR_GENIO */
8597993Sjhb	sizeof(struct ktr_psig),		/* KTR_PSIG */
8697993Sjhb	sizeof(struct ktr_csw),			/* KTR_CSW */
8797993Sjhb	0					/* KTR_USER */
8897993Sjhb};
8997993Sjhb
9097993Sjhbstatic STAILQ_HEAD(, ktr_request) ktr_todo;
9197993Sjhbstatic STAILQ_HEAD(, ktr_request) ktr_free;
9297993Sjhb
93141633Sphkstatic SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
94103234Sjhb
95118607Sjhbstatic u_int ktr_requestpool = KTRACE_REQUEST_POOL;
96103234SjhbTUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
9797993Sjhb
98118607Sjhbstatic u_int ktr_geniosize = PAGE_SIZE;
99103234SjhbTUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
100103234SjhbSYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
101103234Sjhb    0, "Maximum size of genio event payload");
102103234Sjhb
10397993Sjhbstatic int print_message = 1;
10497993Sjhbstruct mtx ktrace_mtx;
105126295Sjhbstatic struct cv ktrace_cv;
10697993Sjhb
10797993Sjhbstatic void ktrace_init(void *dummy);
10897993Sjhbstatic int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
109118607Sjhbstatic u_int ktrace_resize_pool(u_int newsize);
11097993Sjhbstatic struct ktr_request *ktr_getrequest(int type);
11197993Sjhbstatic void ktr_submitrequest(struct ktr_request *req);
11297993Sjhbstatic void ktr_freerequest(struct ktr_request *req);
11397993Sjhbstatic void ktr_loop(void *dummy);
11497993Sjhbstatic void ktr_writerequest(struct ktr_request *req);
11597993Sjhbstatic int ktrcanset(struct thread *,struct proc *);
11697993Sjhbstatic int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
11797993Sjhbstatic int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
11897993Sjhb
11997993Sjhbstatic void
12097993Sjhbktrace_init(void *dummy)
1211541Srgrimes{
12297993Sjhb	struct ktr_request *req;
12397993Sjhb	int i;
1241541Srgrimes
12597993Sjhb	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
126126295Sjhb	cv_init(&ktrace_cv, "ktrace");
12797993Sjhb	STAILQ_INIT(&ktr_todo);
12897993Sjhb	STAILQ_INIT(&ktr_free);
12997993Sjhb	for (i = 0; i < ktr_requestpool; i++) {
130111119Simp		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
13197993Sjhb		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
13297993Sjhb	}
133104354Sscottl	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
1341541Srgrimes}
13597993SjhbSYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
1361541Srgrimes
13797993Sjhbstatic int
13897993Sjhbsysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
13997993Sjhb{
14097993Sjhb	struct thread *td;
141118607Sjhb	u_int newsize, oldsize, wantsize;
14297993Sjhb	int error;
14397993Sjhb
14497993Sjhb	/* Handle easy read-only case first to avoid warnings from GCC. */
14597993Sjhb	if (!req->newptr) {
14697993Sjhb		mtx_lock(&ktrace_mtx);
14797993Sjhb		oldsize = ktr_requestpool;
14897993Sjhb		mtx_unlock(&ktrace_mtx);
149118607Sjhb		return (SYSCTL_OUT(req, &oldsize, sizeof(u_int)));
15097993Sjhb	}
15197993Sjhb
152118607Sjhb	error = SYSCTL_IN(req, &wantsize, sizeof(u_int));
15397993Sjhb	if (error)
15497993Sjhb		return (error);
15597993Sjhb	td = curthread;
156116101Sjhb	td->td_pflags |= TDP_INKTRACE;
15797993Sjhb	mtx_lock(&ktrace_mtx);
15897993Sjhb	oldsize = ktr_requestpool;
15997993Sjhb	newsize = ktrace_resize_pool(wantsize);
16097993Sjhb	mtx_unlock(&ktrace_mtx);
161116101Sjhb	td->td_pflags &= ~TDP_INKTRACE;
162118607Sjhb	error = SYSCTL_OUT(req, &oldsize, sizeof(u_int));
16397993Sjhb	if (error)
16497993Sjhb		return (error);
165122478Sjkoshy	if (wantsize > oldsize && newsize < wantsize)
16697993Sjhb		return (ENOSPC);
16797993Sjhb	return (0);
16897993Sjhb}
169103234SjhbSYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
17097993Sjhb    &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
17197993Sjhb
172118607Sjhbstatic u_int
173118607Sjhbktrace_resize_pool(u_int newsize)
17497993Sjhb{
17597993Sjhb	struct ktr_request *req;
176122478Sjkoshy	int bound;
17797993Sjhb
17897993Sjhb	mtx_assert(&ktrace_mtx, MA_OWNED);
17997993Sjhb	print_message = 1;
180122478Sjkoshy	bound = newsize - ktr_requestpool;
181122478Sjkoshy	if (bound == 0)
182122478Sjkoshy		return (ktr_requestpool);
183122478Sjkoshy	if (bound < 0)
18497993Sjhb		/* Shrink pool down to newsize if possible. */
185122478Sjkoshy		while (bound++ < 0) {
18697993Sjhb			req = STAILQ_FIRST(&ktr_free);
18797993Sjhb			if (req == NULL)
18897993Sjhb				return (ktr_requestpool);
18997993Sjhb			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
19097993Sjhb			ktr_requestpool--;
19197993Sjhb			mtx_unlock(&ktrace_mtx);
19297993Sjhb			free(req, M_KTRACE);
19397993Sjhb			mtx_lock(&ktrace_mtx);
19497993Sjhb		}
19597993Sjhb	else
19697993Sjhb		/* Grow pool up to newsize. */
197122478Sjkoshy		while (bound-- > 0) {
19897993Sjhb			mtx_unlock(&ktrace_mtx);
19997993Sjhb			req = malloc(sizeof(struct ktr_request), M_KTRACE,
200111119Simp			    M_WAITOK);
20197993Sjhb			mtx_lock(&ktrace_mtx);
20297993Sjhb			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
20397993Sjhb			ktr_requestpool++;
20497993Sjhb		}
20597993Sjhb	return (ktr_requestpool);
20697993Sjhb}
20797993Sjhb
20897993Sjhbstatic struct ktr_request *
20997993Sjhbktr_getrequest(int type)
21097993Sjhb{
21197993Sjhb	struct ktr_request *req;
21297993Sjhb	struct thread *td = curthread;
21397993Sjhb	struct proc *p = td->td_proc;
21497993Sjhb	int pm;
21597993Sjhb
216116101Sjhb	td->td_pflags |= TDP_INKTRACE;
21797993Sjhb	mtx_lock(&ktrace_mtx);
21897993Sjhb	if (!KTRCHECK(td, type)) {
21997993Sjhb		mtx_unlock(&ktrace_mtx);
220116101Sjhb		td->td_pflags &= ~TDP_INKTRACE;
22197993Sjhb		return (NULL);
22297993Sjhb	}
22397993Sjhb	req = STAILQ_FIRST(&ktr_free);
22497993Sjhb	if (req != NULL) {
22597993Sjhb		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
22697993Sjhb		req->ktr_header.ktr_type = type;
227112199Sjhb		if (p->p_traceflag & KTRFAC_DROP) {
228112199Sjhb			req->ktr_header.ktr_type |= KTR_DROP;
229112199Sjhb			p->p_traceflag &= ~KTRFAC_DROP;
230112199Sjhb		}
231112198Sjhb		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
232112198Sjhb		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
233112198Sjhb		req->ktr_vp = p->p_tracevp;
234112198Sjhb		VREF(p->p_tracevp);
235112198Sjhb		req->ktr_cred = crhold(p->p_tracecred);
23697993Sjhb		mtx_unlock(&ktrace_mtx);
23797993Sjhb		microtime(&req->ktr_header.ktr_time);
23897993Sjhb		req->ktr_header.ktr_pid = p->p_pid;
23997993Sjhb		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
24097993Sjhb		req->ktr_header.ktr_buffer = NULL;
24197993Sjhb		req->ktr_header.ktr_len = 0;
24297993Sjhb	} else {
243112199Sjhb		p->p_traceflag |= KTRFAC_DROP;
24497993Sjhb		pm = print_message;
24597993Sjhb		print_message = 0;
24697993Sjhb		mtx_unlock(&ktrace_mtx);
24797993Sjhb		if (pm)
24897993Sjhb			printf("Out of ktrace request objects.\n");
249116101Sjhb		td->td_pflags &= ~TDP_INKTRACE;
25097993Sjhb	}
25197993Sjhb	return (req);
25297993Sjhb}
25397993Sjhb
25497993Sjhbstatic void
25597993Sjhbktr_submitrequest(struct ktr_request *req)
25697993Sjhb{
25797993Sjhb
25897993Sjhb	mtx_lock(&ktrace_mtx);
25997993Sjhb	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
260126295Sjhb	cv_signal(&ktrace_cv);
261118599Sjhb	mtx_unlock(&ktrace_mtx);
262116101Sjhb	curthread->td_pflags &= ~TDP_INKTRACE;
26397993Sjhb}
26497993Sjhb
26597993Sjhbstatic void
26697993Sjhbktr_freerequest(struct ktr_request *req)
26797993Sjhb{
26897993Sjhb
26997993Sjhb	crfree(req->ktr_cred);
270101153Sjhb	if (req->ktr_vp != NULL) {
271101153Sjhb		mtx_lock(&Giant);
272101153Sjhb		vrele(req->ktr_vp);
273101153Sjhb		mtx_unlock(&Giant);
274101153Sjhb	}
275103235Sjhb	if (req->ktr_header.ktr_buffer != NULL)
276103235Sjhb		free(req->ktr_header.ktr_buffer, M_KTRACE);
27797993Sjhb	mtx_lock(&ktrace_mtx);
27897993Sjhb	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
27997993Sjhb	mtx_unlock(&ktrace_mtx);
28097993Sjhb}
28197993Sjhb
28297993Sjhbstatic void
28397993Sjhbktr_loop(void *dummy)
28497993Sjhb{
28597993Sjhb	struct ktr_request *req;
28697993Sjhb	struct thread *td;
28797993Sjhb	struct ucred *cred;
28897993Sjhb
28997993Sjhb	/* Only cache these values once. */
29097993Sjhb	td = curthread;
29197993Sjhb	cred = td->td_ucred;
29297993Sjhb	for (;;) {
29397993Sjhb		mtx_lock(&ktrace_mtx);
294126295Sjhb		while (STAILQ_EMPTY(&ktr_todo))
295126295Sjhb			cv_wait(&ktrace_cv, &ktrace_mtx);
29697993Sjhb		req = STAILQ_FIRST(&ktr_todo);
29797993Sjhb		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
29897993Sjhb		KASSERT(req != NULL, ("got a NULL request"));
299103236Sjhb		mtx_unlock(&ktrace_mtx);
300103236Sjhb		/*
301103236Sjhb		 * It is not enough just to pass the cached cred
302103236Sjhb		 * to the VOP's in ktr_writerequest().  Some VFS
303103236Sjhb		 * operations use curthread->td_ucred, so we need
304103236Sjhb		 * to modify our thread's credentials as well.
305103236Sjhb		 * Evil.
306103236Sjhb		 */
307103236Sjhb		td->td_ucred = req->ktr_cred;
308103236Sjhb		ktr_writerequest(req);
309103236Sjhb		td->td_ucred = cred;
31097993Sjhb		ktr_freerequest(req);
31197993Sjhb	}
31297993Sjhb}
31397993Sjhb
31482585Sdillon/*
31582585Sdillon * MPSAFE
31682585Sdillon */
3171549Srgrimesvoid
31897993Sjhbktrsyscall(code, narg, args)
31947955Sdt	int code, narg;
32047955Sdt	register_t args[];
3211541Srgrimes{
32297993Sjhb	struct ktr_request *req;
32397993Sjhb	struct ktr_syscall *ktp;
32497993Sjhb	size_t buflen;
325103233Sjhb	char *buf = NULL;
3261541Srgrimes
327103233Sjhb	buflen = sizeof(register_t) * narg;
328103233Sjhb	if (buflen > 0) {
329111119Simp		buf = malloc(buflen, M_KTRACE, M_WAITOK);
330103233Sjhb		bcopy(args, buf, buflen);
331103233Sjhb	}
33297993Sjhb	req = ktr_getrequest(KTR_SYSCALL);
333104230Sphk	if (req == NULL) {
334104230Sphk		if (buf != NULL)
335104230Sphk			free(buf, M_KTRACE);
33697993Sjhb		return;
337104230Sphk	}
33897993Sjhb	ktp = &req->ktr_data.ktr_syscall;
3391541Srgrimes	ktp->ktr_code = code;
3401541Srgrimes	ktp->ktr_narg = narg;
34197993Sjhb	if (buflen > 0) {
34297993Sjhb		req->ktr_header.ktr_len = buflen;
343103233Sjhb		req->ktr_header.ktr_buffer = buf;
34497993Sjhb	}
34597993Sjhb	ktr_submitrequest(req);
3461541Srgrimes}
3471541Srgrimes
34882585Sdillon/*
34982585Sdillon * MPSAFE
35082585Sdillon */
3511549Srgrimesvoid
35297993Sjhbktrsysret(code, error, retval)
35347955Sdt	int code, error;
35447955Sdt	register_t retval;
3551541Srgrimes{
35697993Sjhb	struct ktr_request *req;
35797993Sjhb	struct ktr_sysret *ktp;
3581541Srgrimes
35997993Sjhb	req = ktr_getrequest(KTR_SYSRET);
36097993Sjhb	if (req == NULL)
36197993Sjhb		return;
36297993Sjhb	ktp = &req->ktr_data.ktr_sysret;
36397993Sjhb	ktp->ktr_code = code;
36497993Sjhb	ktp->ktr_error = error;
36597993Sjhb	ktp->ktr_retval = retval;		/* what about val2 ? */
36697993Sjhb	ktr_submitrequest(req);
3671541Srgrimes}
3681541Srgrimes
3691549Srgrimesvoid
37097993Sjhbktrnamei(path)
3711541Srgrimes	char *path;
3721541Srgrimes{
37397993Sjhb	struct ktr_request *req;
37497993Sjhb	int namelen;
375103233Sjhb	char *buf = NULL;
3761541Srgrimes
377103233Sjhb	namelen = strlen(path);
378103233Sjhb	if (namelen > 0) {
379111119Simp		buf = malloc(namelen, M_KTRACE, M_WAITOK);
380103233Sjhb		bcopy(path, buf, namelen);
381103233Sjhb	}
38297993Sjhb	req = ktr_getrequest(KTR_NAMEI);
383104230Sphk	if (req == NULL) {
384104230Sphk		if (buf != NULL)
385104230Sphk			free(buf, M_KTRACE);
38697993Sjhb		return;
387104230Sphk	}
38897993Sjhb	if (namelen > 0) {
38997993Sjhb		req->ktr_header.ktr_len = namelen;
390103233Sjhb		req->ktr_header.ktr_buffer = buf;
39197993Sjhb	}
39297993Sjhb	ktr_submitrequest(req);
3931541Srgrimes}
3941541Srgrimes
39597993Sjhb/*
39697993Sjhb * Since the uio may not stay valid, we can not hand off this request to
39797993Sjhb * the thread and need to process it synchronously.  However, we wish to
39897993Sjhb * keep the relative order of records in a trace file correct, so we
39997993Sjhb * do put this request on the queue (if it isn't empty) and then block.
40097993Sjhb * The ktrace thread waks us back up when it is time for this event to
40197993Sjhb * be posted and blocks until we have completed writing out the event
40297993Sjhb * and woken it back up.
40397993Sjhb */
4041549Srgrimesvoid
40597993Sjhbktrgenio(fd, rw, uio, error)
4061541Srgrimes	int fd;
4071541Srgrimes	enum uio_rw rw;
40862378Sgreen	struct uio *uio;
40962378Sgreen	int error;
4101541Srgrimes{
41197993Sjhb	struct ktr_request *req;
41297993Sjhb	struct ktr_genio *ktg;
413103235Sjhb	int datalen;
414103235Sjhb	char *buf;
4158876Srgrimes
416131897Sphk	if (error) {
417131897Sphk		free(uio, M_IOV);
4181541Srgrimes		return;
419131897Sphk	}
420103235Sjhb	uio->uio_offset = 0;
421103235Sjhb	uio->uio_rw = UIO_WRITE;
422103235Sjhb	datalen = imin(uio->uio_resid, ktr_geniosize);
423111119Simp	buf = malloc(datalen, M_KTRACE, M_WAITOK);
424131897Sphk	error = uiomove(buf, datalen, uio);
425131897Sphk	free(uio, M_IOV);
426131897Sphk	if (error) {
427103235Sjhb		free(buf, M_KTRACE);
428103235Sjhb		return;
429103235Sjhb	}
43097993Sjhb	req = ktr_getrequest(KTR_GENIO);
431103235Sjhb	if (req == NULL) {
432103235Sjhb		free(buf, M_KTRACE);
43397993Sjhb		return;
434103235Sjhb	}
43597993Sjhb	ktg = &req->ktr_data.ktr_genio;
43697993Sjhb	ktg->ktr_fd = fd;
43797993Sjhb	ktg->ktr_rw = rw;
438103235Sjhb	req->ktr_header.ktr_len = datalen;
439103235Sjhb	req->ktr_header.ktr_buffer = buf;
44097993Sjhb	ktr_submitrequest(req);
4411541Srgrimes}
4421541Srgrimes
4431549Srgrimesvoid
44497993Sjhbktrpsig(sig, action, mask, code)
44551941Smarcel	int sig;
4461541Srgrimes	sig_t action;
44751791Smarcel	sigset_t *mask;
44851941Smarcel	int code;
4491541Srgrimes{
45097993Sjhb	struct ktr_request *req;
45197993Sjhb	struct ktr_psig	*kp;
4521541Srgrimes
45397993Sjhb	req = ktr_getrequest(KTR_PSIG);
45497993Sjhb	if (req == NULL)
45597993Sjhb		return;
45697993Sjhb	kp = &req->ktr_data.ktr_psig;
45797993Sjhb	kp->signo = (char)sig;
45897993Sjhb	kp->action = action;
45997993Sjhb	kp->mask = *mask;
46097993Sjhb	kp->code = code;
46197993Sjhb	ktr_submitrequest(req);
4621541Srgrimes}
4631541Srgrimes
4641549Srgrimesvoid
46597993Sjhbktrcsw(out, user)
4661541Srgrimes	int out, user;
4671541Srgrimes{
46897993Sjhb	struct ktr_request *req;
46997993Sjhb	struct ktr_csw *kc;
4701541Srgrimes
47197993Sjhb	req = ktr_getrequest(KTR_CSW);
47297993Sjhb	if (req == NULL)
47397993Sjhb		return;
47497993Sjhb	kc = &req->ktr_data.ktr_csw;
47597993Sjhb	kc->out = out;
47697993Sjhb	kc->user = user;
47797993Sjhb	ktr_submitrequest(req);
4781541Srgrimes}
479114026Sjhb#endif /* KTRACE */
4801541Srgrimes
4811541Srgrimes/* Interface and common routines */
4821541Srgrimes
4831541Srgrimes/*
4841541Srgrimes * ktrace system call
485114026Sjhb *
486114026Sjhb * MPSAFE
4871541Srgrimes */
48812221Sbde#ifndef _SYS_SYSPROTO_H_
4891541Srgrimesstruct ktrace_args {
4901541Srgrimes	char	*fname;
4911541Srgrimes	int	ops;
4921541Srgrimes	int	facs;
4931541Srgrimes	int	pid;
4941541Srgrimes};
49512221Sbde#endif
4961541Srgrimes/* ARGSUSED */
4971549Srgrimesint
49883366Sjulianktrace(td, uap)
49983366Sjulian	struct thread *td;
5001541Srgrimes	register struct ktrace_args *uap;
5011541Srgrimes{
50213203Swollman#ifdef KTRACE
5031541Srgrimes	register struct vnode *vp = NULL;
5041541Srgrimes	register struct proc *p;
5051541Srgrimes	struct pgrp *pg;
5061541Srgrimes	int facs = uap->facs & ~KTRFAC_ROOT;
5071541Srgrimes	int ops = KTROP(uap->ops);
5081541Srgrimes	int descend = uap->ops & KTRFLAG_DESCEND;
509147576Spjd	int nfound, ret = 0;
51062550Smckusick	int flags, error = 0;
5111541Srgrimes	struct nameidata nd;
512112198Sjhb	struct ucred *cred;
5131541Srgrimes
514114026Sjhb	/*
515114026Sjhb	 * Need something to (un)trace.
516114026Sjhb	 */
517114026Sjhb	if (ops != KTROP_CLEARFILE && facs == 0)
518114026Sjhb		return (EINVAL);
519114026Sjhb
520116101Sjhb	td->td_pflags |= TDP_INKTRACE;
5211541Srgrimes	if (ops != KTROP_CLEAR) {
5221541Srgrimes		/*
5231541Srgrimes		 * an operation which requires a file argument.
5241541Srgrimes		 */
52583366Sjulian		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
52662550Smckusick		flags = FREAD | FWRITE | O_NOFOLLOW;
527114026Sjhb		mtx_lock(&Giant);
528118094Sphk		error = vn_open(&nd, &flags, 0, -1);
5293308Sphk		if (error) {
530114026Sjhb			mtx_unlock(&Giant);
531116101Sjhb			td->td_pflags &= ~TDP_INKTRACE;
5321541Srgrimes			return (error);
5331541Srgrimes		}
53454655Seivind		NDFREE(&nd, NDF_ONLY_PNBUF);
5351541Srgrimes		vp = nd.ni_vp;
53683366Sjulian		VOP_UNLOCK(vp, 0, td);
5371541Srgrimes		if (vp->v_type != VREG) {
53891406Sjhb			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
539114026Sjhb			mtx_unlock(&Giant);
540116101Sjhb			td->td_pflags &= ~TDP_INKTRACE;
5411541Srgrimes			return (EACCES);
5421541Srgrimes		}
543114026Sjhb		mtx_unlock(&Giant);
5441541Srgrimes	}
5451541Srgrimes	/*
54685397Sdillon	 * Clear all uses of the tracefile.
5471541Srgrimes	 */
5481541Srgrimes	if (ops == KTROP_CLEARFILE) {
54974927Sjhb		sx_slock(&allproc_lock);
55053212Sphk		LIST_FOREACH(p, &allproc, p_list) {
55194618Sjhb			PROC_LOCK(p);
552112198Sjhb			if (p->p_tracevp == vp) {
55397993Sjhb				if (ktrcanset(td, p)) {
55497993Sjhb					mtx_lock(&ktrace_mtx);
555112198Sjhb					cred = p->p_tracecred;
556112198Sjhb					p->p_tracecred = NULL;
557112198Sjhb					p->p_tracevp = NULL;
5581541Srgrimes					p->p_traceflag = 0;
55997993Sjhb					mtx_unlock(&ktrace_mtx);
56094618Sjhb					PROC_UNLOCK(p);
561114026Sjhb					mtx_lock(&Giant);
5621541Srgrimes					(void) vn_close(vp, FREAD|FWRITE,
563112198Sjhb						cred, td);
564114026Sjhb					mtx_unlock(&Giant);
565112198Sjhb					crfree(cred);
56685397Sdillon				} else {
56794618Sjhb					PROC_UNLOCK(p);
5681541Srgrimes					error = EPERM;
56985397Sdillon				}
57094618Sjhb			} else
57194618Sjhb				PROC_UNLOCK(p);
5721541Srgrimes		}
57374927Sjhb		sx_sunlock(&allproc_lock);
5741541Srgrimes		goto done;
5751541Srgrimes	}
5761541Srgrimes	/*
5771541Srgrimes	 * do it
5781541Srgrimes	 */
579114026Sjhb	sx_slock(&proctree_lock);
5801541Srgrimes	if (uap->pid < 0) {
5811541Srgrimes		/*
5821541Srgrimes		 * by process group
5831541Srgrimes		 */
5841541Srgrimes		pg = pgfind(-uap->pid);
5851541Srgrimes		if (pg == NULL) {
58694861Sjhb			sx_sunlock(&proctree_lock);
5871541Srgrimes			error = ESRCH;
5881541Srgrimes			goto done;
5891541Srgrimes		}
59091140Stanimura		/*
59191140Stanimura		 * ktrops() may call vrele(). Lock pg_members
59294861Sjhb		 * by the proctree_lock rather than pg_mtx.
59391140Stanimura		 */
59491140Stanimura		PGRP_UNLOCK(pg);
595147576Spjd		nfound = 0;
596147576Spjd		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
597147576Spjd			PROC_LOCK(p);
598147576Spjd			if (p_cansee(td, p) != 0) {
599147576Spjd				PROC_UNLOCK(p);
600147576Spjd				continue;
601147576Spjd			}
602147576Spjd			PROC_UNLOCK(p);
603147576Spjd			nfound++;
6041541Srgrimes			if (descend)
60594618Sjhb				ret |= ktrsetchildren(td, p, ops, facs, vp);
6068876Srgrimes			else
60794618Sjhb				ret |= ktrops(td, p, ops, facs, vp);
608147576Spjd		}
609147576Spjd		if (nfound == 0) {
610147576Spjd			sx_sunlock(&proctree_lock);
611147576Spjd			error = ESRCH;
612147576Spjd			goto done;
613147576Spjd		}
6141541Srgrimes	} else {
6151541Srgrimes		/*
6161541Srgrimes		 * by pid
6171541Srgrimes		 */
6181541Srgrimes		p = pfind(uap->pid);
6191541Srgrimes		if (p == NULL) {
620114026Sjhb			sx_sunlock(&proctree_lock);
6211541Srgrimes			error = ESRCH;
6221541Srgrimes			goto done;
6231541Srgrimes		}
624147183Spjd		error = p_cansee(td, p);
625114026Sjhb		/*
626114026Sjhb		 * The slock of the proctree lock will keep this process
627114026Sjhb		 * from going away, so unlocking the proc here is ok.
628114026Sjhb		 */
62975893Sjhb		PROC_UNLOCK(p);
630147520Spjd		if (error) {
631147520Spjd			sx_sunlock(&proctree_lock);
632147183Spjd			goto done;
633147520Spjd		}
6341541Srgrimes		if (descend)
63594618Sjhb			ret |= ktrsetchildren(td, p, ops, facs, vp);
6361541Srgrimes		else
63794618Sjhb			ret |= ktrops(td, p, ops, facs, vp);
6381541Srgrimes	}
639114026Sjhb	sx_sunlock(&proctree_lock);
6401541Srgrimes	if (!ret)
6411541Srgrimes		error = EPERM;
6421541Srgrimesdone:
643114026Sjhb	if (vp != NULL) {
644114026Sjhb		mtx_lock(&Giant);
64591406Sjhb		(void) vn_close(vp, FWRITE, td->td_ucred, td);
646114026Sjhb		mtx_unlock(&Giant);
647114026Sjhb	}
648116101Sjhb	td->td_pflags &= ~TDP_INKTRACE;
6491541Srgrimes	return (error);
650114026Sjhb#else /* !KTRACE */
651114026Sjhb	return (ENOSYS);
652114026Sjhb#endif /* KTRACE */
6531541Srgrimes}
6541541Srgrimes
65518398Sphk/*
65618398Sphk * utrace system call
657114026Sjhb *
658114026Sjhb * MPSAFE
65918398Sphk */
66018398Sphk/* ARGSUSED */
66118398Sphkint
66283366Sjulianutrace(td, uap)
66383366Sjulian	struct thread *td;
66418398Sphk	register struct utrace_args *uap;
66518398Sphk{
66683366Sjulian
66713203Swollman#ifdef KTRACE
66897993Sjhb	struct ktr_request *req;
66999009Salfred	void *cp;
670103237Sjhb	int error;
67118398Sphk
672103237Sjhb	if (!KTRPOINT(td, KTR_USER))
673103237Sjhb		return (0);
67470792Salfred	if (uap->len > KTR_USER_MAXLEN)
67570707Salfred		return (EINVAL);
676111119Simp	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
677103237Sjhb	error = copyin(uap->addr, cp, uap->len);
678104230Sphk	if (error) {
679104230Sphk		free(cp, M_KTRACE);
680103237Sjhb		return (error);
681104230Sphk	}
68297993Sjhb	req = ktr_getrequest(KTR_USER);
683104230Sphk	if (req == NULL) {
684104230Sphk		free(cp, M_KTRACE);
685122457Sjkoshy		return (ENOMEM);
686104230Sphk	}
687103237Sjhb	req->ktr_header.ktr_buffer = cp;
688103237Sjhb	req->ktr_header.ktr_len = uap->len;
689103237Sjhb	ktr_submitrequest(req);
69018398Sphk	return (0);
691114026Sjhb#else /* !KTRACE */
69218398Sphk	return (ENOSYS);
693114026Sjhb#endif /* KTRACE */
69418398Sphk}
69518398Sphk
69618398Sphk#ifdef KTRACE
69712819Sphkstatic int
69894618Sjhbktrops(td, p, ops, facs, vp)
69994618Sjhb	struct thread *td;
70094618Sjhb	struct proc *p;
7011541Srgrimes	int ops, facs;
7021541Srgrimes	struct vnode *vp;
7031541Srgrimes{
70497993Sjhb	struct vnode *tracevp = NULL;
705112198Sjhb	struct ucred *tracecred = NULL;
7061541Srgrimes
70794618Sjhb	PROC_LOCK(p);
70894618Sjhb	if (!ktrcanset(td, p)) {
70994618Sjhb		PROC_UNLOCK(p);
7101541Srgrimes		return (0);
71194618Sjhb	}
71297993Sjhb	mtx_lock(&ktrace_mtx);
7131541Srgrimes	if (ops == KTROP_SET) {
714112198Sjhb		if (p->p_tracevp != vp) {
7151541Srgrimes			/*
71694618Sjhb			 * if trace file already in use, relinquish below
7171541Srgrimes			 */
718112198Sjhb			tracevp = p->p_tracevp;
71997993Sjhb			VREF(vp);
720112198Sjhb			p->p_tracevp = vp;
7211541Srgrimes		}
722112198Sjhb		if (p->p_tracecred != td->td_ucred) {
723112198Sjhb			tracecred = p->p_tracecred;
724112198Sjhb			p->p_tracecred = crhold(td->td_ucred);
725112198Sjhb		}
7261541Srgrimes		p->p_traceflag |= facs;
72794618Sjhb		if (td->td_ucred->cr_uid == 0)
7281541Srgrimes			p->p_traceflag |= KTRFAC_ROOT;
7298876Srgrimes	} else {
7301541Srgrimes		/* KTROP_CLEAR */
7311541Srgrimes		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
7321541Srgrimes			/* no more tracing */
7331541Srgrimes			p->p_traceflag = 0;
734112198Sjhb			tracevp = p->p_tracevp;
735112198Sjhb			p->p_tracevp = NULL;
736112198Sjhb			tracecred = p->p_tracecred;
737112198Sjhb			p->p_tracecred = NULL;
7381541Srgrimes		}
7391541Srgrimes	}
74097993Sjhb	mtx_unlock(&ktrace_mtx);
74194618Sjhb	PROC_UNLOCK(p);
742114026Sjhb	if (tracevp != NULL) {
743114026Sjhb		mtx_lock(&Giant);
74497993Sjhb		vrele(tracevp);
745114026Sjhb		mtx_unlock(&Giant);
746114026Sjhb	}
747112198Sjhb	if (tracecred != NULL)
748112198Sjhb		crfree(tracecred);
7491541Srgrimes
7501541Srgrimes	return (1);
7511541Srgrimes}
7521541Srgrimes
75312819Sphkstatic int
75494618Sjhbktrsetchildren(td, top, ops, facs, vp)
75594618Sjhb	struct thread *td;
75694618Sjhb	struct proc *top;
7571541Srgrimes	int ops, facs;
7581541Srgrimes	struct vnode *vp;
7591541Srgrimes{
7601541Srgrimes	register struct proc *p;
7611541Srgrimes	register int ret = 0;
7621541Srgrimes
7631541Srgrimes	p = top;
764114026Sjhb	sx_assert(&proctree_lock, SX_LOCKED);
7651541Srgrimes	for (;;) {
76694618Sjhb		ret |= ktrops(td, p, ops, facs, vp);
7671541Srgrimes		/*
7681541Srgrimes		 * If this process has children, descend to them next,
7691541Srgrimes		 * otherwise do any siblings, and if done with this level,
7701541Srgrimes		 * follow back up the tree (but not past top).
7711541Srgrimes		 */
77253212Sphk		if (!LIST_EMPTY(&p->p_children))
77353212Sphk			p = LIST_FIRST(&p->p_children);
7741541Srgrimes		else for (;;) {
775114026Sjhb			if (p == top)
7761541Srgrimes				return (ret);
77753212Sphk			if (LIST_NEXT(p, p_sibling)) {
77853212Sphk				p = LIST_NEXT(p, p_sibling);
7791541Srgrimes				break;
7801541Srgrimes			}
78114529Shsu			p = p->p_pptr;
7821541Srgrimes		}
7831541Srgrimes	}
7841541Srgrimes	/*NOTREACHED*/
7851541Srgrimes}
7861541Srgrimes
78712819Sphkstatic void
78897993Sjhbktr_writerequest(struct ktr_request *req)
78997993Sjhb{
79097993Sjhb	struct ktr_header *kth;
7911541Srgrimes	struct vnode *vp;
79297993Sjhb	struct proc *p;
79397993Sjhb	struct thread *td;
79497993Sjhb	struct ucred *cred;
7951541Srgrimes	struct uio auio;
79697993Sjhb	struct iovec aiov[3];
79762976Smckusick	struct mount *mp;
79897993Sjhb	int datalen, buflen, vrele_count;
7991541Srgrimes	int error;
8001541Srgrimes
80197993Sjhb	vp = req->ktr_vp;
80297993Sjhb	/*
80397993Sjhb	 * If vp is NULL, the vp has been cleared out from under this
80497993Sjhb	 * request, so just drop it.
80597993Sjhb	 */
8061541Srgrimes	if (vp == NULL)
8071541Srgrimes		return;
80897993Sjhb	kth = &req->ktr_header;
809118607Sjhb	datalen = data_lengths[(u_short)kth->ktr_type & ~KTR_DROP];
81097993Sjhb	buflen = kth->ktr_len;
81197993Sjhb	cred = req->ktr_cred;
81297993Sjhb	td = curthread;
8131541Srgrimes	auio.uio_iov = &aiov[0];
8141541Srgrimes	auio.uio_offset = 0;
8151541Srgrimes	auio.uio_segflg = UIO_SYSSPACE;
8161541Srgrimes	auio.uio_rw = UIO_WRITE;
8171541Srgrimes	aiov[0].iov_base = (caddr_t)kth;
8181541Srgrimes	aiov[0].iov_len = sizeof(struct ktr_header);
8191541Srgrimes	auio.uio_resid = sizeof(struct ktr_header);
8201541Srgrimes	auio.uio_iovcnt = 1;
82197993Sjhb	auio.uio_td = td;
82297993Sjhb	if (datalen != 0) {
82397993Sjhb		aiov[1].iov_base = (caddr_t)&req->ktr_data;
82497993Sjhb		aiov[1].iov_len = datalen;
82597993Sjhb		auio.uio_resid += datalen;
8261541Srgrimes		auio.uio_iovcnt++;
82797993Sjhb		kth->ktr_len += datalen;
8281541Srgrimes	}
82997993Sjhb	if (buflen != 0) {
83097993Sjhb		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
83197993Sjhb		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
83297993Sjhb		aiov[auio.uio_iovcnt].iov_len = buflen;
83397993Sjhb		auio.uio_resid += buflen;
83497993Sjhb		auio.uio_iovcnt++;
835103235Sjhb	}
83697993Sjhb	mtx_lock(&Giant);
83762976Smckusick	vn_start_write(vp, &mp, V_WAIT);
83883366Sjulian	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
83997993Sjhb	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
840101123Srwatson#ifdef MAC
841102129Srwatson	error = mac_check_vnode_write(cred, NOCRED, vp);
842101123Srwatson	if (error == 0)
843101123Srwatson#endif
844101123Srwatson		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
84583366Sjulian	VOP_UNLOCK(vp, 0, td);
84662976Smckusick	vn_finished_write(mp);
84797993Sjhb	mtx_unlock(&Giant);
8481541Srgrimes	if (!error)
8491541Srgrimes		return;
8501541Srgrimes	/*
85197993Sjhb	 * If error encountered, give up tracing on this vnode.  We defer
85297993Sjhb	 * all the vrele()'s on the vnode until after we are finished walking
85397993Sjhb	 * the various lists to avoid needlessly holding locks.
8541541Srgrimes	 */
8551541Srgrimes	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
8561541Srgrimes	    error);
85797993Sjhb	vrele_count = 0;
85897993Sjhb	/*
85997993Sjhb	 * First, clear this vnode from being used by any processes in the
86097993Sjhb	 * system.
86197993Sjhb	 * XXX - If one process gets an EPERM writing to the vnode, should
86297993Sjhb	 * we really do this?  Other processes might have suitable
86397993Sjhb	 * credentials for the operation.
86497993Sjhb	 */
865112198Sjhb	cred = NULL;
86674927Sjhb	sx_slock(&allproc_lock);
86753212Sphk	LIST_FOREACH(p, &allproc, p_list) {
86897993Sjhb		PROC_LOCK(p);
869112198Sjhb		if (p->p_tracevp == vp) {
87097993Sjhb			mtx_lock(&ktrace_mtx);
871112198Sjhb			p->p_tracevp = NULL;
8721541Srgrimes			p->p_traceflag = 0;
873112198Sjhb			cred = p->p_tracecred;
874112198Sjhb			p->p_tracecred = NULL;
87597993Sjhb			mtx_unlock(&ktrace_mtx);
87697993Sjhb			vrele_count++;
8771541Srgrimes		}
87897993Sjhb		PROC_UNLOCK(p);
879112198Sjhb		if (cred != NULL) {
880112198Sjhb			crfree(cred);
881112198Sjhb			cred = NULL;
882112198Sjhb		}
8831541Srgrimes	}
88474927Sjhb	sx_sunlock(&allproc_lock);
88597993Sjhb	/*
88697993Sjhb	 * Second, clear this vnode from any pending requests.
88797993Sjhb	 */
88897993Sjhb	mtx_lock(&ktrace_mtx);
88997993Sjhb	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
89097993Sjhb		if (req->ktr_vp == vp) {
89197993Sjhb			req->ktr_vp = NULL;
89297993Sjhb			vrele_count++;
89397993Sjhb		}
89497993Sjhb	}
89597993Sjhb	mtx_unlock(&ktrace_mtx);
89697993Sjhb	mtx_lock(&Giant);
89797993Sjhb	while (vrele_count-- > 0)
89897993Sjhb		vrele(vp);
89997993Sjhb	mtx_unlock(&Giant);
9001541Srgrimes}
9011541Srgrimes
9021541Srgrimes/*
9031541Srgrimes * Return true if caller has permission to set the ktracing state
9041541Srgrimes * of target.  Essentially, the target can't possess any
9051541Srgrimes * more permissions than the caller.  KTRFAC_ROOT signifies that
9068876Srgrimes * root previously set the tracing status on the target process, and
9071541Srgrimes * so, only root may further change it.
9081541Srgrimes */
90912819Sphkstatic int
91094618Sjhbktrcanset(td, targetp)
91194618Sjhb	struct thread *td;
91294618Sjhb	struct proc *targetp;
9131541Srgrimes{
9141541Srgrimes
91594618Sjhb	PROC_LOCK_ASSERT(targetp, MA_OWNED);
91679335Srwatson	if (targetp->p_traceflag & KTRFAC_ROOT &&
917132653Scperciva	    suser_cred(td->td_ucred, SUSER_ALLOWJAIL))
91846155Sphk		return (0);
9191541Srgrimes
92096886Sjhb	if (p_candebug(td, targetp) != 0)
92179335Srwatson		return (0);
92279335Srwatson
92379335Srwatson	return (1);
9241541Srgrimes}
9251541Srgrimes
92613203Swollman#endif /* KTRACE */
927