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.19 2005/07/28 18:10:23 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 vnode
*, struct ktr_header
*, struct uio
*);
60 static int ktrcanset (struct proc
*,struct proc
*);
61 static int ktrsetchildren (struct proc
*,struct proc
*,int,int,struct vnode
*);
62 static int ktrops (struct proc
*,struct proc
*,int,int,struct vnode
*);
65 static struct ktr_header
*
66 ktrgetheader(int type
)
68 struct ktr_header
*kth
;
69 struct proc
*p
= curproc
; /* XXX */
71 MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
74 microtime(&kth
->ktr_time
);
75 kth
->ktr_pid
= p
->p_pid
;
76 bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
+ 1);
81 ktrsyscall(struct vnode
*vp
, int code
, int narg
, register_t args
[])
83 struct ktr_header
*kth
;
84 struct ktr_syscall
*ktp
;
85 int len
= offsetof(struct ktr_syscall
, ktr_args
) +
86 (narg
* sizeof(register_t
));
87 struct proc
*p
= curproc
; /* XXX */
91 p
->p_traceflag
|= KTRFAC_ACTIVE
;
92 kth
= ktrgetheader(KTR_SYSCALL
);
93 MALLOC(ktp
, struct ktr_syscall
*, len
, M_KTRACE
, M_WAITOK
);
96 argp
= &ktp
->ktr_args
[0];
97 for (i
= 0; i
< narg
; i
++)
99 kth
->ktr_buf
= (caddr_t
)ktp
;
101 ktrwrite(vp
, kth
, NULL
);
104 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
108 ktrsysret(struct vnode
*vp
, int code
, int error
, register_t retval
)
110 struct ktr_header
*kth
;
111 struct ktr_sysret ktp
;
112 struct proc
*p
= curproc
; /* XXX */
114 p
->p_traceflag
|= KTRFAC_ACTIVE
;
115 kth
= ktrgetheader(KTR_SYSRET
);
117 ktp
.ktr_error
= error
;
118 ktp
.ktr_retval
= retval
; /* what about val2 ? */
120 kth
->ktr_buf
= (caddr_t
)&ktp
;
121 kth
->ktr_len
= sizeof(struct ktr_sysret
);
123 ktrwrite(vp
, kth
, NULL
);
125 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
129 ktrnamei(struct vnode
*vp
, char *path
)
131 struct ktr_header
*kth
;
132 struct proc
*p
= curproc
; /* XXX */
135 * don't let vp get ripped out from under us
139 p
->p_traceflag
|= KTRFAC_ACTIVE
;
140 kth
= ktrgetheader(KTR_NAMEI
);
141 kth
->ktr_len
= strlen(path
);
144 ktrwrite(vp
, kth
, NULL
);
148 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
152 ktrgenio(struct vnode
*vp
, int fd
, enum uio_rw rw
, struct uio
*uio
, int error
)
154 struct ktr_header
*kth
;
155 struct ktr_genio ktg
;
156 struct proc
*p
= curproc
; /* XXX */
161 * don't let p_tracep get ripped out from under us
165 p
->p_traceflag
|= KTRFAC_ACTIVE
;
166 kth
= ktrgetheader(KTR_GENIO
);
169 kth
->ktr_buf
= (caddr_t
)&ktg
;
170 kth
->ktr_len
= sizeof(struct ktr_genio
);
172 uio
->uio_rw
= UIO_WRITE
;
174 ktrwrite(vp
, kth
, uio
);
178 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
182 ktrpsig(struct vnode
*vp
, int sig
, sig_t action
, sigset_t
*mask
, int code
)
184 struct ktr_header
*kth
;
186 struct proc
*p
= curproc
; /* XXX */
189 * don't let vp get ripped out from under us
193 p
->p_traceflag
|= KTRFAC_ACTIVE
;
194 kth
= ktrgetheader(KTR_PSIG
);
195 kp
.signo
= (char)sig
;
199 kth
->ktr_buf
= (caddr_t
)&kp
;
200 kth
->ktr_len
= sizeof (struct ktr_psig
);
202 ktrwrite(vp
, kth
, NULL
);
206 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
210 ktrcsw(struct vnode
*vp
, int out
, int user
)
212 struct ktr_header
*kth
;
214 struct proc
*p
= curproc
; /* XXX */
217 * don't let vp get ripped out from under us
221 p
->p_traceflag
|= KTRFAC_ACTIVE
;
222 kth
= ktrgetheader(KTR_CSW
);
225 kth
->ktr_buf
= (caddr_t
)&kc
;
226 kth
->ktr_len
= sizeof (struct ktr_csw
);
228 ktrwrite(vp
, kth
, NULL
);
232 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
236 /* Interface and common routines */
243 ktrace(struct ktrace_args
*uap
)
246 struct thread
*td
= curthread
;
247 struct proc
*curp
= td
->td_proc
;
248 struct vnode
*vp
= NULL
;
251 int facs
= uap
->facs
& ~KTRFAC_ROOT
;
252 int ops
= KTROP(uap
->ops
);
253 int descend
= uap
->ops
& KTRFLAG_DESCEND
;
256 struct nlookupdata nd
;
258 curp
->p_traceflag
|= KTRFAC_ACTIVE
;
259 if (ops
!= KTROP_CLEAR
) {
261 * an operation which requires a file argument.
263 error
= nlookup_init(&nd
, uap
->fname
,
264 UIO_USERSPACE
, NLC_LOCKVP
);
266 error
= vn_open(&nd
, NULL
, FREAD
|FWRITE
|O_NOFOLLOW
, 0);
267 if (error
== 0 && nd
.nl_open_vp
->v_type
!= VREG
)
270 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
275 nd
.nl_open_vp
= NULL
;
277 VOP_UNLOCK(vp
, 0, td
);
280 * Clear all uses of the tracefile. XXX umm, what happens to the
281 * loop if vn_close() blocks?
283 if (ops
== KTROP_CLEARFILE
) {
284 FOREACH_PROC_IN_SYSTEM(p
) {
285 if (p
->p_tracep
== vp
) {
286 if (ktrcanset(curp
, p
) && p
->p_tracep
== vp
) {
289 vn_close(vp
, FREAD
|FWRITE
, td
);
298 * need something to (un)trace (XXX - why is this here?)
311 pg
= pgfind(-uap
->pid
);
316 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
) {
318 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
320 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
332 ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
334 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
340 vn_close(vp
, FWRITE
, td
);
341 curp
->p_traceflag
&= ~KTRFAC_ACTIVE
;
353 utrace(struct utrace_args
*uap
)
356 struct ktr_header
*kth
;
357 struct thread
*td
= curthread
; /* XXX */
358 struct proc
*p
= td
->td_proc
;
362 if (!KTRPOINT(td
, KTR_USER
))
364 if (uap
->len
> KTR_USER_MAXLEN
)
366 p
->p_traceflag
|= KTRFAC_ACTIVE
;
368 * don't let p_tracep get ripped out from under us while we are
371 if ((vp
= p
->p_tracep
) != NULL
)
373 kth
= ktrgetheader(KTR_USER
);
374 MALLOC(cp
, caddr_t
, uap
->len
, M_KTRACE
, M_WAITOK
);
375 if (!copyin(uap
->addr
, cp
, uap
->len
)) {
377 kth
->ktr_len
= uap
->len
;
378 ktrwrite(vp
, kth
, NULL
);
384 p
->p_traceflag
&= ~KTRFAC_ACTIVE
;
394 ktrops(struct proc
*curp
, struct proc
*p
, int ops
, int facs
, struct vnode
*vp
)
397 if (!ktrcanset(curp
, p
))
399 if (ops
== KTROP_SET
) {
400 if (p
->p_tracep
!= vp
) {
404 * if trace file already in use, relinquish
407 while ((vtmp
= p
->p_tracep
) != NULL
) {
413 p
->p_traceflag
|= facs
;
414 if (curp
->p_ucred
->cr_uid
== 0)
415 p
->p_traceflag
|= KTRFAC_ROOT
;
418 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
421 /* no more tracing */
423 if ((vtmp
= p
->p_tracep
) != NULL
) {
434 ktrsetchildren(struct proc
*curp
, struct proc
*top
, int ops
, int facs
,
442 ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
444 * If this process has children, descend to them next,
445 * otherwise do any siblings, and if done with this level,
446 * follow back up the tree (but not past top).
448 if (!LIST_EMPTY(&p
->p_children
))
449 p
= LIST_FIRST(&p
->p_children
);
453 if (LIST_NEXT(p
, p_sibling
)) {
454 p
= LIST_NEXT(p
, p_sibling
);
464 ktrwrite(struct vnode
*vp
, struct ktr_header
*kth
, struct uio
*uio
)
467 struct iovec aiov
[2];
468 struct thread
*td
= curthread
;
469 struct proc
*p
= td
->td_proc
; /* XXX */
473 * Since multiple processes may be trying to ktrace, we must vref
474 * the vnode to prevent it from being ripped out from under us in
475 * case another process gets a write error while ktracing and removes
476 * the reference from the proc.
481 auio
.uio_iov
= &aiov
[0];
483 auio
.uio_segflg
= UIO_SYSSPACE
;
484 auio
.uio_rw
= UIO_WRITE
;
485 aiov
[0].iov_base
= (caddr_t
)kth
;
486 aiov
[0].iov_len
= sizeof(struct ktr_header
);
487 auio
.uio_resid
= sizeof(struct ktr_header
);
489 auio
.uio_td
= curthread
;
490 if (kth
->ktr_len
> 0) {
492 aiov
[1].iov_base
= kth
->ktr_buf
;
493 aiov
[1].iov_len
= kth
->ktr_len
;
494 auio
.uio_resid
+= kth
->ktr_len
;
496 kth
->ktr_len
+= uio
->uio_resid
;
498 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
499 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, td
);
500 error
= VOP_WRITE(vp
, &auio
, IO_UNIT
| IO_APPEND
, p
->p_ucred
);
501 if (error
== 0 && uio
!= NULL
) {
502 VOP_LEASE(vp
, td
, p
->p_ucred
, LEASE_WRITE
);
503 error
= VOP_WRITE(vp
, uio
, IO_UNIT
| IO_APPEND
, p
->p_ucred
);
505 VOP_UNLOCK(vp
, 0, td
);
510 * If error encountered, give up tracing on this vnode. XXX what
511 * happens to the loop if vrele() blocks?
513 log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
515 FOREACH_PROC_IN_SYSTEM(p
) {
516 if (p
->p_tracep
== vp
) {
525 * Return true if caller has permission to set the ktracing state
526 * of target. Essentially, the target can't possess any
527 * more permissions than the caller. KTRFAC_ROOT signifies that
528 * root previously set the tracing status on the target process, and
529 * so, only root may further change it.
531 * TODO: check groups. use caller effective gid.
534 ktrcanset(struct proc
*callp
, struct proc
*targetp
)
536 struct ucred
*caller
= callp
->p_ucred
;
537 struct ucred
*target
= targetp
->p_ucred
;
539 if (!PRISON_CHECK(caller
, target
))
541 if ((caller
->cr_uid
== target
->cr_ruid
&&
542 target
->cr_ruid
== target
->cr_svuid
&&
543 caller
->cr_rgid
== target
->cr_rgid
&& /* XXX */
544 target
->cr_rgid
== target
->cr_svgid
&&
545 (targetp
->p_traceflag
& KTRFAC_ROOT
) == 0 &&
546 (targetp
->p_flag
& P_SUGID
) == 0) ||