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.24 2006/05/17 20:20:49 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>
55 static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
58 static struct ktr_header *ktrgetheader (int type);
59 static void ktrwrite (struct proc *, struct ktr_header *, struct uio *);
60 static int ktrcanset (struct proc *,struct proc *);
61 static int ktrsetchildren (struct proc *,struct proc *,int,int, ktrace_node_t);
62 static int ktrops (struct proc *,struct proc *,int,int, ktrace_node_t);
64 static struct ktr_header *
65 ktrgetheader(int type)
67 struct ktr_header *kth;
68 struct proc *p = curproc; /* XXX */
70 MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header),
73 microtime(&kth->ktr_time);
74 kth->ktr_pid = p->p_pid;
75 bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN + 1);
80 ktrsyscall(struct proc *p, int code, int narg, register_t args[])
82 struct ktr_header *kth;
83 struct ktr_syscall *ktp;
88 len = offsetof(struct ktr_syscall, ktr_args) +
89 (narg * sizeof(register_t));
92 * Setting the active bit prevents a ktrace recursion from the
95 p->p_traceflag |= KTRFAC_ACTIVE;
96 kth = ktrgetheader(KTR_SYSCALL);
97 MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK);
100 argp = &ktp->ktr_args[0];
101 for (i = 0; i < narg; i++)
103 kth->ktr_buf = (caddr_t)ktp;
105 ktrwrite(p, kth, NULL);
108 p->p_traceflag &= ~KTRFAC_ACTIVE;
112 ktrsysret(struct proc *p, int code, int error, register_t retval)
114 struct ktr_header *kth;
115 struct ktr_sysret ktp;
117 p->p_traceflag |= KTRFAC_ACTIVE;
118 kth = ktrgetheader(KTR_SYSRET);
120 ktp.ktr_error = error;
121 ktp.ktr_retval = retval; /* what about val2 ? */
123 kth->ktr_buf = (caddr_t)&ktp;
124 kth->ktr_len = sizeof(struct ktr_sysret);
126 ktrwrite(p, kth, NULL);
128 p->p_traceflag &= ~KTRFAC_ACTIVE;
132 ktrnamei(struct proc *p, char *path)
134 struct ktr_header *kth;
136 p->p_traceflag |= KTRFAC_ACTIVE;
137 kth = ktrgetheader(KTR_NAMEI);
138 kth->ktr_len = strlen(path);
141 ktrwrite(p, kth, NULL);
143 p->p_traceflag &= ~KTRFAC_ACTIVE;
147 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct uio *uio, int error)
149 struct ktr_header *kth;
150 struct ktr_genio ktg;
154 p->p_traceflag |= KTRFAC_ACTIVE;
155 kth = ktrgetheader(KTR_GENIO);
158 kth->ktr_buf = (caddr_t)&ktg;
159 kth->ktr_len = sizeof(struct ktr_genio);
161 uio->uio_rw = UIO_WRITE;
163 ktrwrite(p, kth, uio);
165 p->p_traceflag &= ~KTRFAC_ACTIVE;
169 ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
171 struct ktr_header *kth;
174 p->p_traceflag |= KTRFAC_ACTIVE;
175 kth = ktrgetheader(KTR_PSIG);
176 kp.signo = (char)sig;
180 kth->ktr_buf = (caddr_t)&kp;
181 kth->ktr_len = sizeof (struct ktr_psig);
183 ktrwrite(p, kth, NULL);
185 p->p_traceflag &= ~KTRFAC_ACTIVE;
189 ktrcsw(struct proc *p, int out, int user)
191 struct ktr_header *kth;
194 p->p_traceflag |= KTRFAC_ACTIVE;
195 kth = ktrgetheader(KTR_CSW);
198 kth->ktr_buf = (caddr_t)&kc;
199 kth->ktr_len = sizeof (struct ktr_csw);
201 ktrwrite(p, kth, NULL);
203 p->p_traceflag &= ~KTRFAC_ACTIVE;
207 /* Interface and common routines */
214 ktrace(struct ktrace_args *uap)
217 struct thread *td = curthread;
218 struct proc *curp = td->td_proc;
221 int facs = uap->facs & ~KTRFAC_ROOT;
222 int ops = KTROP(uap->ops);
223 int descend = uap->ops & KTRFLAG_DESCEND;
226 struct nlookupdata nd;
227 ktrace_node_t tracenode = NULL;
229 curp->p_traceflag |= KTRFAC_ACTIVE;
230 if (ops != KTROP_CLEAR) {
232 * an operation which requires a file argument.
234 error = nlookup_init(&nd, uap->fname,
235 UIO_USERSPACE, NLC_LOCKVP);
237 error = vn_open(&nd, NULL, FREAD|FWRITE|O_NOFOLLOW, 0);
238 if (error == 0 && nd.nl_open_vp->v_type != VREG)
241 curp->p_traceflag &= ~KTRFAC_ACTIVE;
245 MALLOC(tracenode, ktrace_node_t, sizeof (struct ktrace_node),
246 M_KTRACE, M_WAITOK | M_ZERO);
247 tracenode->kn_vp = nd.nl_open_vp;
248 tracenode->kn_refs = 1;
249 nd.nl_open_vp = NULL;
251 VOP_UNLOCK(tracenode->kn_vp, 0);
254 * Clear all uses of the tracefile. Not the most efficient operation
257 if (ops == KTROP_CLEARFILE) {
259 FOREACH_PROC_IN_SYSTEM(p) {
260 if (p->p_tracenode->kn_vp == tracenode->kn_vp) {
261 if (ktrcanset(curp, p)) {
262 ktrdestroy(&p->p_tracenode);
273 * need something to (un)trace (XXX - why is this here?)
286 pg = pgfind(-uap->pid);
291 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
293 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
295 ret |= ktrops(curp, p, ops, facs, tracenode);
307 ret |= ktrsetchildren(curp, p, ops, facs, tracenode);
309 ret |= ktrops(curp, p, ops, facs, tracenode);
315 ktrdestroy(&tracenode);
316 curp->p_traceflag &= ~KTRFAC_ACTIVE;
328 utrace(struct utrace_args *uap)
331 struct ktr_header *kth;
332 struct thread *td = curthread; /* XXX */
333 struct proc *p = td->td_proc;
336 if (!KTRPOINT(td, KTR_USER))
338 if (uap->len > KTR_USER_MAXLEN)
340 p->p_traceflag |= KTRFAC_ACTIVE;
341 kth = ktrgetheader(KTR_USER);
342 MALLOC(cp, caddr_t, uap->len, M_KTRACE, M_WAITOK);
343 if (!copyin(uap->addr, cp, uap->len)) {
345 kth->ktr_len = uap->len;
346 ktrwrite(p, kth, NULL);
350 p->p_traceflag &= ~KTRFAC_ACTIVE;
359 ktrdestroy(struct ktrace_node **tracenodep)
361 ktrace_node_t tracenode;
363 if ((tracenode = *tracenodep) != NULL) {
365 KKASSERT(tracenode->kn_refs > 0);
366 /* XXX not MP safe yet */
367 --tracenode->kn_refs;
368 if (tracenode->kn_refs == 0) {
369 printf("DESTROY %p\n", tracenode);
370 vn_close(tracenode->kn_vp, FREAD|FWRITE);
371 tracenode->kn_vp = NULL;
372 FREE(tracenode, M_KTRACE);
378 ktrinherit(ktrace_node_t tracenode)
380 KKASSERT(tracenode->kn_refs > 0);
381 ++tracenode->kn_refs;
387 ktrops(struct proc *curp, struct proc *p, int ops, int facs,
388 ktrace_node_t tracenode)
390 ktrace_node_t oldnode;
392 if (!ktrcanset(curp, p))
394 if (ops == KTROP_SET) {
395 if ((oldnode = p->p_tracenode) != NULL) {
396 p->p_tracenode = ktrinherit(tracenode);
397 ktrdestroy(&oldnode);
399 p->p_traceflag |= facs;
400 if (curp->p_ucred->cr_uid == 0)
401 p->p_traceflag |= KTRFAC_ROOT;
404 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
405 /* no more tracing */
407 ktrdestroy(&p->p_tracenode);
415 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
416 ktrace_node_t tracenode)
423 ret |= ktrops(curp, p, ops, facs, tracenode);
425 * If this process has children, descend to them next,
426 * otherwise do any siblings, and if done with this level,
427 * follow back up the tree (but not past top).
429 if (!LIST_EMPTY(&p->p_children))
430 p = LIST_FIRST(&p->p_children);
434 if (LIST_NEXT(p, p_sibling)) {
435 p = LIST_NEXT(p, p_sibling);
445 ktrwrite(struct proc *p, struct ktr_header *kth, struct uio *uio)
448 struct iovec aiov[2];
450 ktrace_node_t tracenode;
453 * We have to ref our tracenode to prevent it from being ripped out
454 * from under us while we are trying to use it. p_tracenode can
455 * go away at any time if another process gets a write error.
459 if (p->p_tracenode == NULL)
461 tracenode = ktrinherit(p->p_tracenode);
462 auio.uio_iov = &aiov[0];
464 auio.uio_segflg = UIO_SYSSPACE;
465 auio.uio_rw = UIO_WRITE;
466 aiov[0].iov_base = (caddr_t)kth;
467 aiov[0].iov_len = sizeof(struct ktr_header);
468 auio.uio_resid = sizeof(struct ktr_header);
470 auio.uio_td = curthread;
471 if (kth->ktr_len > 0) {
473 aiov[1].iov_base = kth->ktr_buf;
474 aiov[1].iov_len = kth->ktr_len;
475 auio.uio_resid += kth->ktr_len;
477 kth->ktr_len += uio->uio_resid;
479 vn_lock(tracenode->kn_vp, LK_EXCLUSIVE | LK_RETRY);
480 error = VOP_WRITE(tracenode->kn_vp, &auio,
481 IO_UNIT | IO_APPEND, p->p_ucred);
482 if (error == 0 && uio != NULL) {
483 error = VOP_WRITE(tracenode->kn_vp, uio,
484 IO_UNIT | IO_APPEND, p->p_ucred);
486 VOP_UNLOCK(tracenode->kn_vp, 0);
491 * If an error occured, give up tracing on all processes using this
492 * tracenode. This is not MP safe but is blocking-safe.
494 log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
497 FOREACH_PROC_IN_SYSTEM(p) {
498 if (p->p_tracenode == tracenode) {
499 ktrdestroy(&p->p_tracenode);
504 ktrdestroy(&tracenode);
508 * Return true if caller has permission to set the ktracing state
509 * of target. Essentially, the target can't possess any
510 * more permissions than the caller. KTRFAC_ROOT signifies that
511 * root previously set the tracing status on the target process, and
512 * so, only root may further change it.
514 * TODO: check groups. use caller effective gid.
517 ktrcanset(struct proc *callp, struct proc *targetp)
519 struct ucred *caller = callp->p_ucred;
520 struct ucred *target = targetp->p_ucred;
522 if (!PRISON_CHECK(caller, target))
524 if ((caller->cr_uid == target->cr_ruid &&
525 target->cr_ruid == target->cr_svuid &&
526 caller->cr_rgid == target->cr_rgid && /* XXX */
527 target->cr_rgid == target->cr_svgid &&
528 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
529 (targetp->p_flag & P_SUGID) == 0) ||