* Copyright (c) 1989 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* @(#)kern_ktrace.c 7.15 (Berkeley) 6/21/91
register struct ktr_header
*kth
;
struct proc
*p
= curproc
; /* XXX */
MALLOC(kth
, struct ktr_header
*, sizeof (struct ktr_header
),
microtime(&kth
->ktr_time
);
bcopy(p
->p_comm
, kth
->ktr_comm
, MAXCOMLEN
);
ktrsyscall(vp
, code
, narg
, args
)
struct ktr_header
*kth
= ktrgetheader(KTR_SYSCALL
);
register len
= sizeof(struct ktr_syscall
) + (narg
* sizeof(int));
MALLOC(ktp
, struct ktr_syscall
*, len
, M_TEMP
, M_WAITOK
);
argp
= (int *)((char *)ktp
+ sizeof(struct ktr_syscall
));
for (i
= 0; i
< narg
; i
++)
kth
->ktr_buf
= (caddr_t
)ktp
;
ktrsysret(vp
, code
, error
, retval
)
struct ktr_header
*kth
= ktrgetheader(KTR_SYSRET
);
ktp
.ktr_retval
= retval
; /* what about val2 ? */
kth
->ktr_buf
= (caddr_t
)&ktp
;
kth
->ktr_len
= sizeof(struct ktr_sysret
);
struct ktr_header
*kth
= ktrgetheader(KTR_NAMEI
);
kth
->ktr_len
= strlen(path
);
ktrgenio(vp
, fd
, rw
, iov
, len
, error
)
register struct iovec
*iov
;
struct ktr_header
*kth
= ktrgetheader(KTR_GENIO
);
register struct ktr_genio
*ktp
;
register int resid
= len
, cnt
;
MALLOC(ktp
, struct ktr_genio
*, sizeof(struct ktr_genio
) + len
,
cp
= (caddr_t
)((char *)ktp
+ sizeof (struct ktr_genio
));
if ((cnt
= iov
->iov_len
) > resid
)
if (copyin(iov
->iov_base
, cp
, (unsigned)cnt
))
kth
->ktr_buf
= (caddr_t
)ktp
;
kth
->ktr_len
= sizeof (struct ktr_genio
) + len
;
ktrpsig(vp
, sig
, action
, mask
, code
)
struct ktr_header
*kth
= ktrgetheader(KTR_PSIG
);
kth
->ktr_buf
= (caddr_t
)&kp
;
kth
->ktr_len
= sizeof (struct ktr_psig
);
/* Interface and common routines */
ktrace(curp
, uap
, retval
)
register struct vnode
*vp
= NULL
;
int facs
= uap
->facs
& ~KTRFAC_ROOT
;
int ops
= KTROP(uap
->ops
);
int descend
= uap
->ops
& KTRFLAG_DESCEND
;
if (ops
!= KTROP_CLEAR
) {
* an operation which requires a file argument.
nd
.ni_segflg
= UIO_USERSPACE
;
if (error
= vn_open(&nd
, curp
, FREAD
|FWRITE
, 0))
if (vp
->v_type
!= VREG
) {
(void) vn_close(vp
, FREAD
|FWRITE
, curp
->p_ucred
, curp
);
* Clear all uses of the tracefile
if (ops
== KTROP_CLEARFILE
) {
for (p
= allproc
; p
!= NULL
; p
= p
->p_nxt
) {
if (ktrcanset(curp
, p
)) {
(void) vn_close(vp
, FREAD
|FWRITE
,
* need something to (un)trace (XXX - why is this here?)
for (p
= pg
->pg_mem
; p
!= NULL
; p
= p
->p_pgrpnxt
)
ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
ret
|= ktrsetchildren(curp
, p
, ops
, facs
, vp
);
ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
(void) vn_close(vp
, FWRITE
, curp
->p_ucred
, curp
);
ktrops(curp
, p
, ops
, facs
, vp
)
* if trace file already in use, relinquish
if (curp
->p_ucred
->cr_uid
== 0)
p
->p_traceflag
|= KTRFAC_ROOT
;
if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
if (p
->p_tracep
!= NULL
) {
ktrsetchildren(curp
, top
, ops
, facs
, vp
)
ret
|= ktrops(curp
, p
, ops
, facs
, vp
);
* If this process has children, descend to them next,
* otherwise do any siblings, and if done with this level,
* follow back up the tree (but not past top).
register struct ktr_header
*kth
;
register struct proc
*p
= curproc
; /* XXX */
auio
.uio_segflg
= UIO_SYSSPACE
;
aiov
[0].iov_base
= (caddr_t
)kth
;
aiov
[0].iov_len
= sizeof(struct ktr_header
);
auio
.uio_resid
= sizeof(struct ktr_header
);
auio
.uio_procp
= (struct proc
*)0;
aiov
[1].iov_base
= kth
->ktr_buf
;
aiov
[1].iov_len
= kth
->ktr_len
;
auio
.uio_resid
+= kth
->ktr_len
;
error
= VOP_WRITE(vp
, &auio
, IO_UNIT
|IO_APPEND
, p
->p_ucred
);
* If error encountered, give up tracing on this vnode.
log(LOG_NOTICE
, "ktrace write failed, errno %d, tracing stopped\n",
for (p
= allproc
; p
!= NULL
; p
= p
->p_nxt
) {
* Return true if caller has permission to set the ktracing state
* of target. Essentially, the target can't possess any
* more permissions than the caller. KTRFAC_ROOT signifies that
* root previously set the tracing status on the target process, and
* so, only root may further change it.
* TODO: check groups. use caller effective gid.
ktrcanset(callp
, targetp
)
struct proc
*callp
, *targetp
;
register struct pcred
*caller
= callp
->p_cred
;
register struct pcred
*target
= targetp
->p_cred
;
if ((caller
->pc_ucred
->cr_uid
== target
->p_ruid
&&
target
->p_ruid
== target
->p_svuid
&&
caller
->p_rgid
== target
->p_rgid
&& /* XXX */
target
->p_rgid
== target
->p_svgid
&&
(targetp
->p_traceflag
& KTRFAC_ROOT
) == 0) ||
caller
->pc_ucred
->cr_uid
== 0)