2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93
37 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
52 #include <sys/syslog.h>
55 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
58 static struct ktr_header *ktrgetheader(int type);
59 static void ktrwrite(struct vnode *, struct ktr_header *, struct uio *);
60 static int ktrcanset(struct thread *, struct proc *);
61 static int ktrsetchildren(struct thread *, struct proc *, int, int, struct vnode *);
62 static int ktrops(struct thread *, struct proc *, int, int, struct vnode *);
65 static struct ktr_header *
69 register struct ktr_header *kth;
70 struct proc *p = curproc; /* XXX */
72 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
75 microtime(&kth->ktr_time);
76 kth->ktr_pid = p->p_pid;
77 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
85 ktrsyscall(vp, code, narg, args)
90 struct ktr_header *kth;
91 struct ktr_syscall *ktp;
92 register int len = offsetof(struct ktr_syscall, ktr_args) +
93 (narg * sizeof(register_t));
94 struct proc *p = curproc; /* XXX */
99 p->p_traceflag |= KTRFAC_ACTIVE;
100 kth = ktrgetheader(KTR_SYSCALL);
101 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
102 ktp->ktr_code = code;
103 ktp->ktr_narg = narg;
104 argp = &ktp->ktr_args[0];
105 for (i = 0; i < narg; i++)
107 kth->ktr_buffer = (caddr_t)ktp;
109 ktrwrite(vp, kth, NULL);
112 p->p_traceflag &= ~KTRFAC_ACTIVE;
120 ktrsysret(vp, code, error, retval)
125 struct ktr_header *kth;
126 struct ktr_sysret ktp;
127 struct proc *p = curproc; /* XXX */
130 p->p_traceflag |= KTRFAC_ACTIVE;
131 kth = ktrgetheader(KTR_SYSRET);
133 ktp.ktr_error = error;
134 ktp.ktr_retval = retval; /* what about val2 ? */
136 kth->ktr_buffer = (caddr_t)&ktp;
137 kth->ktr_len = sizeof(struct ktr_sysret);
139 ktrwrite(vp, kth, NULL);
141 p->p_traceflag &= ~KTRFAC_ACTIVE;
150 struct ktr_header *kth;
151 struct proc *p = curproc; /* XXX */
154 * don't let p_tracep get ripped out from under us
158 p->p_traceflag |= KTRFAC_ACTIVE;
159 kth = ktrgetheader(KTR_NAMEI);
160 kth->ktr_len = strlen(path);
161 kth->ktr_buffer = path;
163 ktrwrite(vp, kth, NULL);
167 p->p_traceflag &= ~KTRFAC_ACTIVE;
171 ktrgenio(vp, fd, rw, uio, error)
178 struct ktr_header *kth;
179 struct ktr_genio ktg;
180 struct proc *p = curproc; /* XXX */
187 * don't let p_tracep get ripped out from under us
191 p->p_traceflag |= KTRFAC_ACTIVE;
192 kth = ktrgetheader(KTR_GENIO);
195 kth->ktr_buffer = (caddr_t)&ktg;
196 kth->ktr_len = sizeof(struct ktr_genio);
198 uio->uio_rw = UIO_WRITE;
200 ktrwrite(vp, kth, uio);
204 p->p_traceflag &= ~KTRFAC_ACTIVE;
209 ktrpsig(vp, sig, action, mask, code)
216 struct ktr_header *kth;
218 struct proc *p = curproc; /* XXX */
221 * don't let vp get ripped out from under us
225 p->p_traceflag |= KTRFAC_ACTIVE;
226 kth = ktrgetheader(KTR_PSIG);
227 kp.signo = (char)sig;
231 kth->ktr_buffer = (caddr_t)&kp;
232 kth->ktr_len = sizeof (struct ktr_psig);
234 ktrwrite(vp, kth, NULL);
238 p->p_traceflag &= ~KTRFAC_ACTIVE;
242 ktrcsw(vp, out, user)
246 struct ktr_header *kth;
248 struct proc *p = curproc; /* XXX */
251 * don't let vp get ripped out from under us
255 p->p_traceflag |= KTRFAC_ACTIVE;
256 kth = ktrgetheader(KTR_CSW);
259 kth->ktr_buffer = (caddr_t)&kc;
260 kth->ktr_len = sizeof (struct ktr_csw);
262 ktrwrite(vp, kth, NULL);
266 p->p_traceflag &= ~KTRFAC_ACTIVE;
270 /* Interface and common routines */
275 #ifndef _SYS_SYSPROTO_H_
287 register struct ktrace_args *uap;
290 struct proc *curp = td->td_proc;
291 register struct vnode *vp = NULL;
292 register struct proc *p;
294 int facs = uap->facs & ~KTRFAC_ROOT;
295 int ops = KTROP(uap->ops);
296 int descend = uap->ops & KTRFLAG_DESCEND;
298 int flags, error = 0;
301 curp->p_traceflag |= KTRFAC_ACTIVE;
302 if (ops != KTROP_CLEAR) {
304 * an operation which requires a file argument.
306 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
307 flags = FREAD | FWRITE | O_NOFOLLOW;
308 error = vn_open(&nd, &flags, 0);
310 curp->p_traceflag &= ~KTRFAC_ACTIVE;
313 NDFREE(&nd, NDF_ONLY_PNBUF);
315 VOP_UNLOCK(vp, 0, td);
316 if (vp->v_type != VREG) {
317 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
318 curp->p_traceflag &= ~KTRFAC_ACTIVE;
323 * Clear all uses of the tracefile.
325 if (ops == KTROP_CLEARFILE) {
326 sx_slock(&allproc_lock);
327 LIST_FOREACH(p, &allproc, p_list) {
329 if (p->p_tracep == vp) {
330 if (ktrcanset(td, p) && p->p_tracep == vp) {
334 (void) vn_close(vp, FREAD|FWRITE,
343 sx_sunlock(&allproc_lock);
347 * need something to (un)trace (XXX - why is this here?)
360 sx_slock(&proctree_lock);
361 pg = pgfind(-uap->pid);
363 sx_sunlock(&proctree_lock);
368 * ktrops() may call vrele(). Lock pg_members
369 * by the proctree_lock rather than pg_mtx.
372 LIST_FOREACH(p, &pg->pg_members, p_pglist)
374 ret |= ktrsetchildren(td, p, ops, facs, vp);
376 ret |= ktrops(td, p, ops, facs, vp);
377 sx_sunlock(&proctree_lock);
388 /* XXX: UNLOCK above has a race */
390 ret |= ktrsetchildren(td, p, ops, facs, vp);
392 ret |= ktrops(td, p, ops, facs, vp);
398 (void) vn_close(vp, FWRITE, td->td_ucred, td);
399 curp->p_traceflag &= ~KTRFAC_ACTIVE;
413 register struct utrace_args *uap;
417 struct ktr_header *kth;
418 struct proc *p = curproc; /* XXX */
422 if (!KTRPOINT(p, KTR_USER))
424 if (uap->len > KTR_USER_MAXLEN)
426 p->p_traceflag |= KTRFAC_ACTIVE;
427 if ((vp = p->p_tracep) != NULL)
429 kth = ktrgetheader(KTR_USER);
430 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
431 if (!copyin(uap->addr, cp, uap->len)) {
432 kth->ktr_buffer = cp;
433 kth->ktr_len = uap->len;
434 ktrwrite(vp, kth, NULL);
440 p->p_traceflag &= ~KTRFAC_ACTIVE;
450 ktrops(td, p, ops, facs, vp)
456 struct vnode *vtmp = NULL, *newvp = NULL;
459 if (!ktrcanset(td, p)) {
463 if (ops == KTROP_SET) {
464 if (p->p_tracep != vp) {
468 * if trace file already in use, relinquish below
474 p->p_traceflag |= facs;
475 if (td->td_ucred->cr_uid == 0)
476 p->p_traceflag |= KTRFAC_ROOT;
479 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
482 /* no more tracing */
490 /* Release old trace file if requested. */
494 /* Setup new trace file if requested. */
496 * XXX: Doing this before the PROC_UNLOCK above would result in
497 * fewer lock operations but would break old behavior where the
498 * above vrele() would not be traced when changing trace files.
511 ktrsetchildren(td, top, ops, facs, vp)
517 register struct proc *p;
518 register int ret = 0;
521 sx_slock(&proctree_lock);
523 ret |= ktrops(td, p, ops, facs, vp);
525 * If this process has children, descend to them next,
526 * otherwise do any siblings, and if done with this level,
527 * follow back up the tree (but not past top).
529 if (!LIST_EMPTY(&p->p_children))
530 p = LIST_FIRST(&p->p_children);
533 sx_sunlock(&proctree_lock);
536 if (LIST_NEXT(p, p_sibling)) {
537 p = LIST_NEXT(p, p_sibling);
547 ktrwrite(vp, kth, uio)
549 register struct ktr_header *kth;
553 struct iovec aiov[2];
554 struct thread *td = curthread; /* XXX */
555 struct proc *p = td->td_proc; /* XXX */
561 auio.uio_iov = &aiov[0];
563 auio.uio_segflg = UIO_SYSSPACE;
564 auio.uio_rw = UIO_WRITE;
565 aiov[0].iov_base = (caddr_t)kth;
566 aiov[0].iov_len = sizeof(struct ktr_header);
567 auio.uio_resid = sizeof(struct ktr_header);
569 auio.uio_td = curthread;
570 if (kth->ktr_len > 0) {
572 aiov[1].iov_base = kth->ktr_buffer;
573 aiov[1].iov_len = kth->ktr_len;
574 auio.uio_resid += kth->ktr_len;
576 kth->ktr_len += uio->uio_resid;
578 vn_start_write(vp, &mp, V_WAIT);
579 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
580 (void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
581 error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, td->td_ucred);
582 if (error == 0 && uio != NULL) {
583 (void)VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE);
584 error = VOP_WRITE(vp, uio, IO_UNIT | IO_APPEND, td->td_ucred);
586 VOP_UNLOCK(vp, 0, td);
587 vn_finished_write(mp);
591 * If error encountered, give up tracing on this vnode. XXX what
592 * happens to the loop if vrele() blocks?
594 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
596 sx_slock(&allproc_lock);
597 LIST_FOREACH(p, &allproc, p_list) {
598 if (p->p_tracep == vp) {
604 sx_sunlock(&allproc_lock);
608 * Return true if caller has permission to set the ktracing state
609 * of target. Essentially, the target can't possess any
610 * more permissions than the caller. KTRFAC_ROOT signifies that
611 * root previously set the tracing status on the target process, and
612 * so, only root may further change it.
615 ktrcanset(td, targetp)
617 struct proc *targetp;
620 PROC_LOCK_ASSERT(targetp, MA_OWNED);
621 if (targetp->p_traceflag & KTRFAC_ROOT &&
622 suser_cred(td->td_ucred, PRISON_ROOT))
625 if (p_candebug(td, targetp) != 0)