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
34 * $FreeBSD: src/sys/kern/kern_ktrace.c,v 1.35.2.6 2002/07/05 22:36:38 darrenr Exp $
35 * $DragonFly: src/sys/kern/kern_ktrace.c,v 1.30 2008/04/14 12:01:50 dillon Exp $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
47 #include <sys/nlookup.h>
48 #include <sys/vnode.h>
49 #include <sys/ktrace.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/sysent.h>
54 #include <vm/vm_zone.h>
56 #include <sys/mplock2.h>
58 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
61 static struct ktr_header *ktrgetheader (int type);
62 static void ktrwrite (struct lwp *, struct ktr_header *, struct uio *);
63 static int ktrcanset (struct thread *,struct proc *);
64 static int ktrsetchildren (struct thread *,struct proc *,int,int, ktrace_node_t);
65 static int ktrops (struct thread *,struct proc *,int,int, ktrace_node_t);
70 static struct ktr_header *
71 ktrgetheader(int type)
73 struct ktr_header *kth;
74 struct proc *p = curproc; /* XXX */
75 struct lwp *lp = curthread->td_lwp;
77 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
80 /* XXX threaded flag is a hack at the moment */
81 kth->ktr_flags = (p->p_nthreads > 1) ? KTRH_THREADED : 0;
82 microtime(&kth->ktr_time);
83 kth->ktr_pid = p->p_pid;
84 kth->ktr_tid = lp->lwp_tid;
85 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
90 ktrsyscall(struct lwp *lp, int code, int narg, register_t args[])
92 struct ktr_header *kth;
93 struct ktr_syscall *ktp;
98 len = offsetof(struct ktr_syscall, ktr_args) +
99 (narg * sizeof(register_t));
102 * Setting the active bit prevents a ktrace recursion from the
103 * ktracing op itself.
105 lp->lwp_traceflag |= KTRFAC_ACTIVE;
106 kth = ktrgetheader(KTR_SYSCALL);
107 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
108 ktp->ktr_code = code;
109 ktp->ktr_narg = narg;
110 argp = &ktp->ktr_args[0];
111 for (i = 0; i < narg; i++)
113 kth->ktr_buf = (caddr_t)ktp;
115 ktrwrite(lp, kth, NULL);
118 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
122 ktrsysret(struct lwp *lp, int code, int error, register_t retval)
124 struct ktr_header *kth;
125 struct ktr_sysret ktp;
127 lp->lwp_traceflag |= KTRFAC_ACTIVE;
128 kth = ktrgetheader(KTR_SYSRET);
130 ktp.ktr_error = error;
132 ktp.ktr_retval = retval; /* what about val2 ? */
136 kth->ktr_buf = (caddr_t)&ktp;
137 kth->ktr_len = sizeof(struct ktr_sysret);
139 ktrwrite(lp, kth, NULL);
141 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
145 ktrnamei(struct lwp *lp, char *path)
147 struct ktr_header *kth;
149 lp->lwp_traceflag |= KTRFAC_ACTIVE;
150 kth = ktrgetheader(KTR_NAMEI);
151 kth->ktr_len = strlen(path);
154 ktrwrite(lp, kth, NULL);
156 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
160 ktrgenio(struct lwp *lp, int fd, enum uio_rw rw, struct uio *uio, int error)
162 struct ktr_header *kth;
163 struct ktr_genio ktg;
167 lp->lwp_traceflag |= KTRFAC_ACTIVE;
168 kth = ktrgetheader(KTR_GENIO);
171 kth->ktr_buf = (caddr_t)&ktg;
172 kth->ktr_len = sizeof(struct ktr_genio);
174 uio->uio_rw = UIO_WRITE;
176 ktrwrite(lp, kth, uio);
178 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
182 ktrpsig(struct lwp *lp, int sig, sig_t action, sigset_t *mask, int code)
184 struct ktr_header *kth;
187 lp->lwp_traceflag |= KTRFAC_ACTIVE;
188 kth = ktrgetheader(KTR_PSIG);
189 kp.signo = (char)sig;
193 kth->ktr_buf = (caddr_t)&kp;
194 kth->ktr_len = sizeof (struct ktr_psig);
196 ktrwrite(lp, kth, NULL);
198 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
202 ktrcsw(struct lwp *lp, int out, int user)
204 struct ktr_header *kth;
207 lp->lwp_traceflag |= KTRFAC_ACTIVE;
208 kth = ktrgetheader(KTR_CSW);
211 kth->ktr_buf = (caddr_t)&kc;
212 kth->ktr_len = sizeof (struct ktr_csw);
214 ktrwrite(lp, kth, NULL);
216 lp->lwp_traceflag &= ~KTRFAC_ACTIVE;
220 /* Interface and common routines */
226 struct ktrace_clear_info {
227 ktrace_node_t tracenode;
232 static int ktrace_clear_callback(struct proc *p, void *data);
240 sys_ktrace(struct ktrace_args *uap)
243 struct ktrace_clear_info info;
244 struct thread *td = curthread;
245 struct proc *curp = td->td_proc;
248 int facs = uap->facs & ~KTRFAC_ROOT;
249 int ops = KTROP(uap->ops);
250 int descend = uap->ops & KTRFLAG_DESCEND;
253 struct nlookupdata nd;
254 ktrace_node_t tracenode = NULL;
257 curp->p_traceflag |= KTRFAC_ACTIVE;
258 if (ops != KTROP_CLEAR) {
260 * an operation which requires a file argument.
262 error = nlookup_init(&nd, uap->fname,
263 UIO_USERSPACE, NLC_LOCKVP);
265 error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
266 if (error == 0 && nd.nl_open_vp->v_type != VREG)
269 curp->p_traceflag &= ~KTRFAC_ACTIVE;
273 MALLOC(tracenode, ktrace_node_t, sizeof (struct ktrace_node),
274 M_KTRACE, M_WAITOK | M_ZERO);
275 tracenode->kn_vp = nd.nl_open_vp;
276 tracenode->kn_refs = 1;
277 nd.nl_open_vp = NULL;
279 vn_unlock(tracenode->kn_vp);
282 * Clear all uses of the tracefile. Not the most efficient operation
285 if (ops == KTROP_CLEARFILE) {
286 info.tracenode = tracenode;
289 allproc_scan(ktrace_clear_callback, &info);
294 * need something to (un)trace (XXX - why is this here?)
305 * By process group. Process group is referenced, preventing
308 pg = pgfind(-uap->pid);
313 lwkt_gettoken(&pg->pg_token);
314 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
317 ret |= ktrsetchildren(td, p, ops, facs, tracenode);
319 ret |= ktrops(td, p, ops, facs, tracenode);
322 lwkt_reltoken(&pg->pg_token);
334 ret |= ktrsetchildren(td, p, ops, facs, tracenode);
336 ret |= ktrops(td, p, ops, facs, tracenode);
343 ktrdestroy(&tracenode);
344 curp->p_traceflag &= ~KTRFAC_ACTIVE;
355 * NOTE: NOT MPSAFE (yet)
358 ktrace_clear_callback(struct proc *p, void *data)
360 struct ktrace_clear_info *info = data;
362 if (p->p_tracenode) {
363 if (info->rootclear) {
364 if (p->p_tracenode == info->tracenode) {
365 ktrdestroy(&p->p_tracenode);
369 if (p->p_tracenode->kn_vp == info->tracenode->kn_vp) {
370 if (ktrcanset(curthread, p)) {
371 ktrdestroy(&p->p_tracenode);
390 sys_utrace(struct utrace_args *uap)
393 struct ktr_header *kth;
394 struct thread *td = curthread; /* XXX */
397 if (!KTRPOINT(td, KTR_USER))
399 if (uap->len > KTR_USER_MAXLEN)
401 td->td_lwp->lwp_traceflag |= KTRFAC_ACTIVE;
402 kth = ktrgetheader(KTR_USER);
403 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
404 if (!copyin(uap->addr, cp, uap->len)) {
406 kth->ktr_len = uap->len;
407 ktrwrite(td->td_lwp, kth, NULL);
411 td->td_lwp->lwp_traceflag &= ~KTRFAC_ACTIVE;
420 ktrdestroy(struct ktrace_node **tracenodep)
422 ktrace_node_t tracenode;
424 if ((tracenode = *tracenodep) != NULL) {
426 KKASSERT(tracenode->kn_refs > 0);
427 if (atomic_fetchadd_int(&tracenode->kn_refs, -1) == 1) {
428 vn_close(tracenode->kn_vp, FREAD|FWRITE);
429 tracenode->kn_vp = NULL;
430 FREE(tracenode, M_KTRACE);
436 * This allows a process to inherit a ref on a tracenode and is also used
437 * as a temporary ref to prevent a tracenode from being destroyed out from
438 * under an active operation.
441 ktrinherit(ktrace_node_t tracenode)
444 KKASSERT(tracenode->kn_refs > 0);
445 atomic_add_int(&tracenode->kn_refs, 1);
452 ktrops(struct thread *td, struct proc *p, int ops, int facs,
453 ktrace_node_t tracenode)
455 ktrace_node_t oldnode;
457 if (!ktrcanset(td, p))
459 if (ops == KTROP_SET) {
460 if ((oldnode = p->p_tracenode) != tracenode) {
461 p->p_tracenode = ktrinherit(tracenode);
462 ktrdestroy(&oldnode);
464 p->p_traceflag |= facs;
465 if (td->td_ucred->cr_uid == 0)
466 p->p_traceflag |= KTRFAC_ROOT;
469 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
470 /* no more tracing */
472 ktrdestroy(&p->p_tracenode);
480 ktrsetchildren(struct thread *td, struct proc *top, int ops, int facs,
481 ktrace_node_t tracenode)
489 lwkt_gettoken(&p->p_token);
492 ret |= ktrops(td, p, ops, facs, tracenode);
495 * If this process has children, descend to them next,
496 * otherwise do any siblings, and if done with this level,
497 * follow back up the tree (but not past top).
499 if ((np = LIST_FIRST(&p->p_children)) != NULL) {
505 if ((np = LIST_NEXT(p, p_sibling)) != NULL) {
511 * recurse up to parent, set p in our inner
512 * loop when doing this. np can be NULL if
513 * we race a reparenting to init (thus 'top'
514 * is skipped past and never encountered).
520 lwkt_reltoken(&p->p_token);
523 lwkt_gettoken(&p->p_token);
526 lwkt_reltoken(&p->p_token);
531 /* Already held, but we need the token too */
532 lwkt_gettoken(&p->p_token);
538 ktrwrite(struct lwp *lp, struct ktr_header *kth, struct uio *uio)
540 struct ktrace_clear_info info;
542 struct iovec aiov[2];
544 ktrace_node_t tracenode;
547 * We have to ref our tracenode to prevent it from being ripped out
548 * from under us while we are trying to use it. p_tracenode can
549 * go away at any time if another process gets a write error.
553 if (lp->lwp_proc->p_tracenode == NULL)
555 tracenode = ktrinherit(lp->lwp_proc->p_tracenode);
556 auio.uio_iov = &aiov[0];
558 auio.uio_segflg = UIO_SYSSPACE;
559 auio.uio_rw = UIO_WRITE;
560 aiov[0].iov_base = (caddr_t)kth;
561 aiov[0].iov_len = sizeof(struct ktr_header);
562 auio.uio_resid = sizeof(struct ktr_header);
564 auio.uio_td = curthread;
565 if (kth->ktr_len > 0) {
567 aiov[1].iov_base = kth->ktr_buf;
568 aiov[1].iov_len = kth->ktr_len;
569 auio.uio_resid += kth->ktr_len;
571 kth->ktr_len += uio->uio_resid;
573 vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY);
574 error = VOP_WRITE(tracenode->kn_vp, &auio,
575 IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred);
576 if (error == 0 && uio != NULL) {
577 error = VOP_WRITE(tracenode->kn_vp, uio,
578 IO_UNIT | IO_APPEND, lp->lwp_thread->td_ucred);
580 vn_unlock(tracenode->kn_vp);
583 * If an error occured, give up tracing on all processes
584 * using this tracenode. This is not MP safe but is
588 "ktrace write failed, errno %d, tracing stopped\n", error);
589 info.tracenode = tracenode;
592 allproc_scan(ktrace_clear_callback, &info);
594 ktrdestroy(&tracenode);
598 * Return true if caller has permission to set the ktracing state
599 * of target. Essentially, the target can't possess any
600 * more permissions than the caller. KTRFAC_ROOT signifies that
601 * root previously set the tracing status on the target process, and
602 * so, only root may further change it.
604 * TODO: check groups. use caller effective gid.
607 ktrcanset(struct thread *calltd, struct proc *targetp)
609 struct ucred *caller = calltd->td_ucred;
610 struct ucred *target = targetp->p_ucred;
612 if (!PRISON_CHECK(caller, target))
614 if ((caller->cr_uid == target->cr_ruid &&
615 target->cr_ruid == target->cr_svuid &&
616 caller->cr_rgid == target->cr_rgid && /* XXX */
617 target->cr_rgid == target->cr_svgid &&
618 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
619 (targetp->p_flags & P_SUGID) == 0) ||