1 /* $NetBSD: kern_ktrace.c,v 1.149 2009/08/05 19:53:42 dsl Exp $ */
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.149 2009/08/05 19:53:42 dsl Exp $");
66 #include <sys/param.h>
67 #include <sys/systm.h>
70 #include <sys/namei.h>
71 #include <sys/vnode.h>
72 #include <sys/kernel.h>
73 #include <sys/kthread.h>
74 #include <sys/ktrace.h>
76 #include <sys/syslog.h>
77 #include <sys/filedesc.h>
78 #include <sys/ioctl.h>
79 #include <sys/callout.h>
80 #include <sys/kauth.h>
82 #include <sys/mount.h>
84 #include <sys/syscallargs.h>
88 * - need better error reporting?
89 * - userland utility to sort ktrace.out by timestamp.
90 * - keep minimum information in ktrace_entry when rest of alloc failed.
91 * - per trace control of configurable parameters.
95 TAILQ_ENTRY(ktrace_entry
) kte_list
;
96 struct ktr_header kte_kth
;
100 uint8_t kte_space
[KTE_SPACE
];
104 TAILQ_ENTRY(ktr_desc
) ktd_list
;
106 #define KTDF_WAIT 0x0001
107 #define KTDF_DONE 0x0002
108 #define KTDF_BLOCKING 0x0004
109 #define KTDF_INTERACTIVE 0x0008
111 #define KTDE_ENOMEM 0x0001
112 #define KTDE_ENOSPC 0x0002
114 int ktd_ref
; /* # of reference */
115 int ktd_qcount
; /* # of entry in the queue */
118 * Params to control behaviour.
120 int ktd_delayqcnt
; /* # of entry allowed to delay */
121 int ktd_wakedelay
; /* delay of wakeup in *tick* */
122 int ktd_intrwakdl
; /* ditto, but when interactive */
124 file_t
*ktd_fp
; /* trace output file */
125 lwp_t
*ktd_lwp
; /* our kernel thread */
126 TAILQ_HEAD(, ktrace_entry
) ktd_queue
;
127 callout_t ktd_wakch
; /* delayed wakeup */
128 kcondvar_t ktd_sync_cv
;
132 static int ktealloc(struct ktrace_entry
**, void **, lwp_t
*, int,
134 static void ktrwrite(struct ktr_desc
*, struct ktrace_entry
*);
135 static int ktrace_common(lwp_t
*, int, int, int, file_t
*);
136 static int ktrops(lwp_t
*, struct proc
*, int, int,
138 static int ktrsetchildren(lwp_t
*, struct proc
*, int, int,
140 static int ktrcanset(lwp_t
*, struct proc
*);
141 static int ktrsamefile(file_t
*, file_t
*);
142 static void ktr_kmem(lwp_t
*, int, const void *, size_t);
143 static void ktr_io(lwp_t
*, int, enum uio_rw
, struct iovec
*, size_t);
145 static struct ktr_desc
*
146 ktd_lookup(file_t
*);
147 static void ktdrel(struct ktr_desc
*);
148 static void ktdref(struct ktr_desc
*);
149 static void ktraddentry(lwp_t
*, struct ktrace_entry
*, int);
150 /* Flags for ktraddentry (3rd arg) */
151 #define KTA_NOWAIT 0x0000
152 #define KTA_WAITOK 0x0001
153 #define KTA_LARGE 0x0002
154 static void ktefree(struct ktrace_entry
*);
155 static void ktd_logerrl(struct ktr_desc
*, int);
156 static void ktrace_thread(void *);
157 static int ktrderefall(struct ktr_desc
*, int);
162 #define KTD_MAXENTRY 1000 /* XXX: tune */
163 #define KTD_TIMEOUT 5 /* XXX: tune */
164 #define KTD_DELAYQCNT 100 /* XXX: tune */
165 #define KTD_WAKEDELAY 5000 /* XXX: tune */
166 #define KTD_INTRWAKDL 100 /* XXX: tune */
169 * Patchable variables.
171 int ktd_maxentry
= KTD_MAXENTRY
; /* max # of entry in the queue */
172 int ktd_timeout
= KTD_TIMEOUT
; /* timeout in seconds */
173 int ktd_delayqcnt
= KTD_DELAYQCNT
; /* # of entry allowed to delay */
174 int ktd_wakedelay
= KTD_WAKEDELAY
; /* delay of wakeup in *ms* */
175 int ktd_intrwakdl
= KTD_INTRWAKDL
; /* ditto, but when interactive */
177 kmutex_t ktrace_lock
;
179 static TAILQ_HEAD(, ktr_desc
) ktdq
= TAILQ_HEAD_INITIALIZER(ktdq
);
180 static pool_cache_t kte_cache
;
182 static kauth_listener_t ktrace_listener
;
185 ktd_wakeup(struct ktr_desc
*ktd
)
188 callout_stop(&ktd
->ktd_wakch
);
189 cv_signal(&ktd
->ktd_cv
);
193 ktd_callout(void *arg
)
196 mutex_enter(&ktrace_lock
);
198 mutex_exit(&ktrace_lock
);
202 ktd_logerrl(struct ktr_desc
*ktd
, int error
)
205 ktd
->ktd_error
|= error
;
211 ktd_logerr(struct proc
*p
, int error
)
213 struct ktr_desc
*ktd
;
215 KASSERT(mutex_owned(&ktrace_lock
));
221 ktd_logerrl(ktd
, error
);
229 if ((l
->l_pflag
& LP_KTRACTIVE
) != 0)
231 l
->l_pflag
|= LP_KTRACTIVE
;
239 l
->l_pflag
&= ~LP_KTRACTIVE
;
243 ktrace_listener_cb(kauth_cred_t cred
, kauth_action_t action
, void *cookie
,
244 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
248 enum kauth_process_req req
;
250 result
= KAUTH_RESULT_DEFER
;
253 if (action
!= KAUTH_PROCESS_KTRACE
)
256 req
= (enum kauth_process_req
)(unsigned long)arg1
;
258 /* Privileged; secmodel should handle these. */
259 if (req
== KAUTH_REQ_PROCESS_KTRACE_PERSISTENT
)
262 if ((p
->p_traceflag
& KTRFAC_PERSISTENT
) ||
263 (p
->p_flag
& PK_SUGID
))
266 if (kauth_cred_geteuid(cred
) == kauth_cred_getuid(p
->p_cred
) &&
267 kauth_cred_getuid(cred
) == kauth_cred_getsvuid(p
->p_cred
) &&
268 kauth_cred_getgid(cred
) == kauth_cred_getgid(p
->p_cred
) &&
269 kauth_cred_getgid(cred
) == kauth_cred_getsvgid(p
->p_cred
))
270 result
= KAUTH_RESULT_ALLOW
;
276 * Initialise the ktrace system.
282 mutex_init(&ktrace_lock
, MUTEX_DEFAULT
, IPL_NONE
);
283 kte_cache
= pool_cache_init(sizeof(struct ktrace_entry
), 0, 0, 0,
284 "ktrace", &pool_allocator_nointr
, IPL_NONE
, NULL
, NULL
, NULL
);
286 ktrace_listener
= kauth_listen_scope(KAUTH_SCOPE_PROCESS
,
287 ktrace_listener_cb
, NULL
);
291 * Release a reference. Called with ktrace_lock held.
294 ktdrel(struct ktr_desc
*ktd
)
297 KASSERT(mutex_owned(&ktrace_lock
));
299 KDASSERT(ktd
->ktd_ref
!= 0);
300 KASSERT(ktd
->ktd_ref
> 0);
301 KASSERT(ktrace_on
> 0);
303 if (--ktd
->ktd_ref
<= 0) {
304 ktd
->ktd_flags
|= KTDF_DONE
;
305 cv_signal(&ktd
->ktd_cv
);
310 ktdref(struct ktr_desc
*ktd
)
313 KASSERT(mutex_owned(&ktrace_lock
));
320 ktd_lookup(file_t
*fp
)
322 struct ktr_desc
*ktd
;
324 KASSERT(mutex_owned(&ktrace_lock
));
326 for (ktd
= TAILQ_FIRST(&ktdq
); ktd
!= NULL
;
327 ktd
= TAILQ_NEXT(ktd
, ktd_list
)) {
328 if (ktrsamefile(ktd
->ktd_fp
, fp
)) {
338 ktraddentry(lwp_t
*l
, struct ktrace_entry
*kte
, int flags
)
340 struct proc
*p
= l
->l_proc
;
341 struct ktr_desc
*ktd
;
343 struct timeval t1
, t2
;
346 mutex_enter(&ktrace_lock
);
348 if (p
->p_traceflag
& KTRFAC_TRC_EMUL
) {
349 /* Add emulation trace before first entry for this process */
350 p
->p_traceflag
&= ~KTRFAC_TRC_EMUL
;
351 mutex_exit(&ktrace_lock
);
355 mutex_enter(&ktrace_lock
);
358 /* Tracing may have been cancelled. */
364 * Bump reference count so that the object will remain while
365 * we are here. Note that the trace is controlled by other
370 if (ktd
->ktd_flags
& KTDF_DONE
)
373 if (ktd
->ktd_qcount
> ktd_maxentry
) {
374 ktd_logerrl(ktd
, KTDE_ENOSPC
);
377 TAILQ_INSERT_TAIL(&ktd
->ktd_queue
, kte
, kte_list
);
379 if (ktd
->ktd_flags
& KTDF_BLOCKING
)
382 if (flags
& KTA_WAITOK
&&
383 (/* flags & KTA_LARGE */0 || ktd
->ktd_flags
& KTDF_WAIT
||
384 ktd
->ktd_qcount
> ktd_maxentry
>> 1))
386 * Sync with writer thread since we're requesting rather
387 * big one or many requests are pending.
390 ktd
->ktd_flags
|= KTDF_WAIT
;
395 if (cv_timedwait(&ktd
->ktd_sync_cv
, &ktrace_lock
,
396 ktd_timeout
* hz
) != 0) {
397 ktd
->ktd_flags
|= KTDF_BLOCKING
;
399 * Maybe the writer thread is blocking
400 * completely for some reason, but
401 * don't stop target process forever.
403 log(LOG_NOTICE
, "ktrace timeout\n");
408 timersub(&t2
, &t1
, &t2
);
411 "ktrace long wait: %lld.%06ld\n",
412 (long long)t2
.tv_sec
, (long)t2
.tv_usec
);
414 } while (p
->p_tracep
== ktd
&&
415 (ktd
->ktd_flags
& (KTDF_WAIT
| KTDF_DONE
)) == KTDF_WAIT
);
417 /* Schedule delayed wakeup */
418 if (ktd
->ktd_qcount
> ktd
->ktd_delayqcnt
)
419 ktd_wakeup(ktd
); /* Wakeup now */
420 else if (!callout_pending(&ktd
->ktd_wakch
))
421 callout_reset(&ktd
->ktd_wakch
,
422 ktd
->ktd_flags
& KTDF_INTERACTIVE
?
423 ktd
->ktd_intrwakdl
: ktd
->ktd_wakedelay
,
429 mutex_exit(&ktrace_lock
);
437 mutex_exit(&ktrace_lock
);
443 ktefree(struct ktrace_entry
*kte
)
446 if (kte
->kte_buf
!= kte
->kte_space
)
447 kmem_free(kte
->kte_buf
, kte
->kte_bufsz
);
448 pool_cache_put(kte_cache
, kte
);
452 * "deep" compare of two files for the purposes of clearing a trace.
453 * Returns true if they're the same open file, or if they point at the
454 * same underlying vnode/socket.
458 ktrsamefile(file_t
*f1
, file_t
*f2
)
461 return ((f1
== f2
) ||
462 ((f1
!= NULL
) && (f2
!= NULL
) &&
463 (f1
->f_type
== f2
->f_type
) &&
464 (f1
->f_data
== f2
->f_data
)));
468 ktrderef(struct proc
*p
)
470 struct ktr_desc
*ktd
= p
->p_tracep
;
472 KASSERT(mutex_owned(&ktrace_lock
));
479 cv_broadcast(&ktd
->ktd_sync_cv
);
484 ktradref(struct proc
*p
)
486 struct ktr_desc
*ktd
= p
->p_tracep
;
488 KASSERT(mutex_owned(&ktrace_lock
));
494 ktrderefall(struct ktr_desc
*ktd
, int auth
)
496 lwp_t
*curl
= curlwp
;
500 mutex_enter(proc_lock
);
501 PROCLIST_FOREACH(p
, &allproc
) {
502 if ((p
->p_flag
& PK_MARKER
) != 0 || p
->p_tracep
!= ktd
)
504 mutex_enter(p
->p_lock
);
505 mutex_enter(&ktrace_lock
);
506 if (p
->p_tracep
== ktd
) {
507 if (!auth
|| ktrcanset(curl
, p
))
512 mutex_exit(&ktrace_lock
);
513 mutex_exit(p
->p_lock
);
515 mutex_exit(proc_lock
);
521 ktealloc(struct ktrace_entry
**ktep
, void **bufp
, lwp_t
*l
, int type
,
524 struct proc
*p
= l
->l_proc
;
525 struct ktrace_entry
*kte
;
526 struct ktr_header
*kth
;
533 kte
= pool_cache_get(kte_cache
, PR_WAITOK
);
534 if (sz
> sizeof(kte
->kte_space
)) {
535 if ((buf
= kmem_alloc(sz
, KM_SLEEP
)) == NULL
) {
536 pool_cache_put(kte_cache
, kte
);
541 buf
= kte
->kte_space
;
547 (void)memset(kth
, 0, sizeof(*kth
));
549 kth
->ktr_type
= type
;
550 kth
->ktr_pid
= p
->p_pid
;
551 memcpy(kth
->ktr_comm
, p
->p_comm
, MAXCOMLEN
);
552 kth
->ktr_version
= KTRFAC_VERSION(p
->p_traceflag
);
555 switch (KTRFAC_VERSION(p
->p_traceflag
)) {
557 /* This is the original format */
558 kth
->ktr_otv
.tv_sec
= ts
.tv_sec
;
559 kth
->ktr_otv
.tv_usec
= ts
.tv_nsec
/ 1000;
562 kth
->ktr_olid
= l
->l_lid
;
563 kth
->ktr_ots
.tv_sec
= ts
.tv_sec
;
564 kth
->ktr_ots
.tv_nsec
= ts
.tv_nsec
;
567 kth
->ktr_lid
= l
->l_lid
;
568 kth
->ktr_ts
.tv_sec
= ts
.tv_sec
;
569 kth
->ktr_ts
.tv_nsec
= ts
.tv_nsec
;
582 ktr_syscall(register_t code
, const register_t args
[], int narg
)
585 struct proc
*p
= l
->l_proc
;
586 struct ktrace_entry
*kte
;
587 struct ktr_syscall
*ktp
;
592 if (!KTRPOINT(p
, KTR_SYSCALL
))
595 len
= sizeof(struct ktr_syscall
) + narg
* sizeof argp
[0];
597 if (ktealloc(&kte
, (void *)&ktp
, l
, KTR_SYSCALL
, len
))
600 ktp
->ktr_code
= code
;
601 ktp
->ktr_argsize
= narg
* sizeof argp
[0];
602 argp
= (register_t
*)(ktp
+ 1);
603 for (i
= 0; i
< narg
; i
++)
606 ktraddentry(l
, kte
, KTA_WAITOK
);
610 ktr_sysret(register_t code
, int error
, register_t
*retval
)
613 struct ktrace_entry
*kte
;
614 struct ktr_sysret
*ktp
;
616 if (!KTRPOINT(l
->l_proc
, KTR_SYSRET
))
619 if (ktealloc(&kte
, (void *)&ktp
, l
, KTR_SYSRET
,
620 sizeof(struct ktr_sysret
)))
623 ktp
->ktr_code
= code
;
624 ktp
->ktr_eosys
= 0; /* XXX unused */
625 ktp
->ktr_error
= error
;
626 ktp
->ktr_retval
= retval
? retval
[0] : 0;
627 ktp
->ktr_retval_1
= retval
? retval
[1] : 0;
629 ktraddentry(l
, kte
, KTA_WAITOK
);
633 ktr_namei(const char *path
, size_t pathlen
)
637 if (!KTRPOINT(l
->l_proc
, KTR_NAMEI
))
640 ktr_kmem(l
, KTR_NAMEI
, path
, pathlen
);
644 ktr_namei2(const char *eroot
, size_t erootlen
,
645 const char *path
, size_t pathlen
)
648 struct ktrace_entry
*kte
;
651 if (!KTRPOINT(l
->l_proc
, KTR_NAMEI
))
654 if (ktealloc(&kte
, &buf
, l
, KTR_NAMEI
, erootlen
+ pathlen
))
656 memcpy(buf
, eroot
, erootlen
);
657 buf
= (char *)buf
+ erootlen
;
658 memcpy(buf
, path
, pathlen
);
659 ktraddentry(l
, kte
, KTA_WAITOK
);
666 const char *emul
= l
->l_proc
->p_emul
->e_name
;
668 if (!KTRPOINT(l
->l_proc
, KTR_EMUL
))
671 ktr_kmem(l
, KTR_EMUL
, emul
, strlen(emul
));
675 ktr_execarg(const void *bf
, size_t len
)
679 if (!KTRPOINT(l
->l_proc
, KTR_EXEC_ARG
))
682 ktr_kmem(l
, KTR_EXEC_ARG
, bf
, len
);
686 ktr_execenv(const void *bf
, size_t len
)
690 if (!KTRPOINT(l
->l_proc
, KTR_EXEC_ENV
))
693 ktr_kmem(l
, KTR_EXEC_ENV
, bf
, len
);
697 ktr_kmem(lwp_t
*l
, int type
, const void *bf
, size_t len
)
699 struct ktrace_entry
*kte
;
702 if (ktealloc(&kte
, &buf
, l
, type
, len
))
704 memcpy(buf
, bf
, len
);
705 ktraddentry(l
, kte
, KTA_WAITOK
);
709 ktr_io(lwp_t
*l
, int fd
, enum uio_rw rw
, struct iovec
*iov
, size_t len
)
711 struct ktrace_entry
*kte
;
712 struct ktr_genio
*ktp
;
713 size_t resid
= len
, cnt
, buflen
;
717 buflen
= min(PAGE_SIZE
, resid
+ sizeof(struct ktr_genio
));
719 if (ktealloc(&kte
, (void *)&ktp
, l
, KTR_GENIO
, buflen
))
725 cp
= (void *)(ktp
+ 1);
726 buflen
-= sizeof(struct ktr_genio
);
727 kte
->kte_kth
.ktr_len
= sizeof(struct ktr_genio
);
730 cnt
= min(iov
->iov_len
, buflen
);
731 if (copyin(iov
->iov_base
, cp
, cnt
) != 0)
733 kte
->kte_kth
.ktr_len
+= cnt
;
738 if (iov
->iov_len
== 0)
741 iov
->iov_base
= (char *)iov
->iov_base
+ cnt
;
745 * Don't push so many entry at once. It will cause kmem map
748 ktraddentry(l
, kte
, KTA_WAITOK
| KTA_LARGE
);
750 if (curcpu()->ci_schedstate
.spc_flags
& SPCF_SHOULDYIELD
) {
767 ktr_genio(int fd
, enum uio_rw rw
, const void *addr
, size_t len
, int error
)
772 if (!KTRPOINT(l
->l_proc
, KTR_GENIO
) || error
!= 0)
774 iov
.iov_base
= __UNCONST(addr
);
776 ktr_io(l
, fd
, rw
, &iov
, len
);
780 ktr_geniov(int fd
, enum uio_rw rw
, struct iovec
*iov
, size_t len
, int error
)
784 if (!KTRPOINT(l
->l_proc
, KTR_GENIO
) || error
!= 0)
786 ktr_io(l
, fd
, rw
, iov
, len
);
790 ktr_mibio(int fd
, enum uio_rw rw
, const void *addr
, size_t len
, int error
)
795 if (!KTRPOINT(l
->l_proc
, KTR_MIB
) || error
!= 0)
797 iov
.iov_base
= __UNCONST(addr
);
799 ktr_io(l
, fd
, rw
, &iov
, len
);
803 ktr_psig(int sig
, sig_t action
, const sigset_t
*mask
,
804 const ksiginfo_t
*ksi
)
806 struct ktrace_entry
*kte
;
813 if (!KTRPOINT(l
->l_proc
, KTR_PSIG
))
816 if (ktealloc(&kte
, (void *)&kbuf
, l
, KTR_PSIG
, sizeof(*kbuf
)))
819 kbuf
->kp
.signo
= (char)sig
;
820 kbuf
->kp
.action
= action
;
821 kbuf
->kp
.mask
= *mask
;
824 kbuf
->kp
.code
= KSI_TRAPCODE(ksi
);
825 (void)memset(&kbuf
->si
, 0, sizeof(kbuf
->si
));
826 kbuf
->si
._info
= ksi
->ksi_info
;
827 kte
->kte_kth
.ktr_len
= sizeof(*kbuf
);
830 kte
->kte_kth
.ktr_len
= sizeof(struct ktr_psig
);
833 ktraddentry(l
, kte
, KTA_WAITOK
);
837 ktr_csw(int out
, int user
)
840 struct proc
*p
= l
->l_proc
;
841 struct ktrace_entry
*kte
;
844 if (!KTRPOINT(p
, KTR_CSW
))
848 * Don't record context switches resulting from blocking on
849 * locks; it's too easy to get duff results.
851 if (l
->l_syncobj
== &mutex_syncobj
|| l
->l_syncobj
== &rw_syncobj
)
855 * We can't sleep if we're already going to sleep (if original
856 * condition is met during sleep, we hang up).
858 * XXX This is not ideal: it would be better to maintain a pool
859 * of ktes and actually push this to the kthread when context
860 * switch happens, however given the points where we are called
861 * from that is difficult to do.
868 nanotime(&l
->l_ktrcsw
);
869 l
->l_pflag
|= LP_KTRCSW
;
872 l
->l_pflag
|= LP_KTRCSWUSER
;
874 l
->l_pflag
&= ~LP_KTRCSWUSER
;
881 * On the way back in, we need to record twice: once for entry, and
884 if ((l
->l_pflag
& LP_KTRCSW
) != 0) {
886 l
->l_pflag
&= ~LP_KTRCSW
;
888 if (ktealloc(&kte
, (void *)&kc
, l
, KTR_CSW
, sizeof(*kc
)))
892 kc
->user
= ((l
->l_pflag
& LP_KTRCSWUSER
) != 0);
895 switch (KTRFAC_VERSION(p
->p_traceflag
)) {
897 kte
->kte_kth
.ktr_otv
.tv_sec
= ts
->tv_sec
;
898 kte
->kte_kth
.ktr_otv
.tv_usec
= ts
->tv_nsec
/ 1000;
901 kte
->kte_kth
.ktr_ots
.tv_sec
= ts
->tv_sec
;
902 kte
->kte_kth
.ktr_ots
.tv_nsec
= ts
->tv_nsec
;
905 kte
->kte_kth
.ktr_ts
.tv_sec
= ts
->tv_sec
;
906 kte
->kte_kth
.ktr_ts
.tv_nsec
= ts
->tv_nsec
;
912 ktraddentry(l
, kte
, KTA_WAITOK
);
915 if (ktealloc(&kte
, (void *)&kc
, l
, KTR_CSW
, sizeof(*kc
)))
921 ktraddentry(l
, kte
, KTA_WAITOK
);
925 ktr_point(int fac_bit
)
927 return curlwp
->l_proc
->p_traceflag
& fac_bit
;
931 ktruser(const char *id
, void *addr
, size_t len
, int ustr
)
933 struct ktrace_entry
*kte
;
934 struct ktr_user
*ktp
;
939 if (!KTRPOINT(l
->l_proc
, KTR_USER
))
942 if (len
> KTR_USER_MAXLEN
)
945 error
= ktealloc(&kte
, (void *)&ktp
, l
, KTR_USER
, sizeof(*ktp
) + len
);
950 if (copyinstr(id
, ktp
->ktr_id
, KTR_USER_MAXIDLEN
, NULL
) != 0)
951 ktp
->ktr_id
[0] = '\0';
953 strncpy(ktp
->ktr_id
, id
, KTR_USER_MAXIDLEN
);
954 ktp
->ktr_id
[KTR_USER_MAXIDLEN
-1] = '\0';
956 user_dta
= (void *)(ktp
+ 1);
957 if ((error
= copyin(addr
, (void *)user_dta
, len
)) != 0)
960 ktraddentry(l
, kte
, KTA_WAITOK
);
965 ktr_kuser(const char *id
, void *addr
, size_t len
)
967 struct ktrace_entry
*kte
;
968 struct ktr_user
*ktp
;
972 if (!KTRPOINT(l
->l_proc
, KTR_USER
))
975 if (len
> KTR_USER_MAXLEN
)
978 error
= ktealloc(&kte
, (void *)&ktp
, l
, KTR_USER
, sizeof(*ktp
) + len
);
982 strlcpy(ktp
->ktr_id
, id
, KTR_USER_MAXIDLEN
);
984 memcpy(ktp
+ 1, addr
, len
);
986 ktraddentry(l
, kte
, KTA_WAITOK
);
990 ktr_mmsg(const void *msgh
, size_t size
)
994 if (!KTRPOINT(l
->l_proc
, KTR_MMSG
))
997 ktr_kmem(l
, KTR_MMSG
, msgh
, size
);
1001 ktr_mool(const void *kaddr
, size_t size
, const void *uaddr
)
1003 struct ktrace_entry
*kte
;
1004 struct ktr_mool
*kp
;
1005 struct ktr_mool
*bf
;
1008 if (!KTRPOINT(l
->l_proc
, KTR_MOOL
))
1011 if (ktealloc(&kte
, (void *)&kp
, l
, KTR_MOOL
, size
+ sizeof(*kp
)))
1016 bf
= kp
+ 1; /* Skip uaddr and size */
1017 (void)memcpy(bf
, kaddr
, size
);
1019 ktraddentry(l
, kte
, KTA_WAITOK
);
1023 ktr_saupcall(struct lwp
*l
, int type
, int nevent
, int nint
, void *sas
,
1024 void *ap
, void *ksas
)
1026 struct ktrace_entry
*kte
;
1027 struct ktr_saupcall
*ktp
;
1032 if (!KTRPOINT(l
->l_proc
, KTR_SAUPCALL
))
1035 len
= sizeof(struct ktr_saupcall
);
1036 sz
= len
+ sizeof(struct sa_t
) * (nevent
+ nint
+ 1);
1038 if (ktealloc(&kte
, (void *)&ktp
, l
, KTR_SAUPCALL
, sz
))
1041 ktp
->ktr_type
= type
;
1042 ktp
->ktr_nevent
= nevent
;
1043 ktp
->ktr_nint
= nint
;
1047 /* Copy the sa_t's */
1048 sapp
= (struct sa_t
**) ksas
;
1050 for (i
= nevent
+ nint
; i
>= 0; i
--) {
1051 memcpy((char *)ktp
+ len
, *sapp
, sizeof(struct sa_t
));
1052 len
+= sizeof(struct sa_t
);
1056 kte
->kte_kth
.ktr_len
= len
;
1057 ktraddentry(l
, kte
, KTA_WAITOK
);
1061 ktr_mib(const int *name
, u_int namelen
)
1063 struct ktrace_entry
*kte
;
1068 if (!KTRPOINT(l
->l_proc
, KTR_MIB
))
1071 size
= namelen
* sizeof(*name
);
1073 if (ktealloc(&kte
, (void *)&namep
, l
, KTR_MIB
, size
))
1076 (void)memcpy(namep
, name
, namelen
* sizeof(*name
));
1078 ktraddentry(l
, kte
, KTA_WAITOK
);
1081 /* Interface and common routines */
1084 ktrace_common(lwp_t
*curl
, int ops
, int facs
, int pid
, file_t
*fp
)
1089 struct ktr_desc
*ktd
= NULL
;
1094 curp
= curl
->l_proc
;
1095 descend
= ops
& KTRFLAG_DESCEND
;
1096 facs
= facs
& ~((unsigned) KTRFAC_PERSISTENT
);
1098 (void)ktrenter(curl
);
1100 switch (KTROP(ops
)) {
1102 case KTROP_CLEARFILE
:
1104 * Clear all uses of the tracefile
1106 mutex_enter(&ktrace_lock
);
1107 ktd
= ktd_lookup(fp
);
1108 mutex_exit(&ktrace_lock
);
1111 error
= ktrderefall(ktd
, 1);
1115 mutex_enter(&ktrace_lock
);
1116 ktd
= ktd_lookup(fp
);
1117 mutex_exit(&ktrace_lock
);
1119 ktd
= kmem_alloc(sizeof(*ktd
), KM_SLEEP
);
1120 TAILQ_INIT(&ktd
->ktd_queue
);
1121 callout_init(&ktd
->ktd_wakch
, CALLOUT_MPSAFE
);
1122 cv_init(&ktd
->ktd_cv
, "ktrwait");
1123 cv_init(&ktd
->ktd_sync_cv
, "ktrsync");
1125 ktd
->ktd_qcount
= 0;
1127 ktd
->ktd_errcnt
= 0;
1128 ktd
->ktd_delayqcnt
= ktd_delayqcnt
;
1129 ktd
->ktd_wakedelay
= mstohz(ktd_wakedelay
);
1130 ktd
->ktd_intrwakdl
= mstohz(ktd_intrwakdl
);
1133 mutex_enter(&ktrace_lock
);
1135 mutex_exit(&ktrace_lock
);
1138 * XXX: not correct. needs an way to detect
1139 * whether ktruss or ktrace.
1141 if (fp
->f_type
== DTYPE_PIPE
)
1142 ktd
->ktd_flags
|= KTDF_INTERACTIVE
;
1144 mutex_enter(&fp
->f_lock
);
1146 mutex_exit(&fp
->f_lock
);
1147 error
= kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
,
1148 ktrace_thread
, ktd
, &ktd
->ktd_lwp
, "ktrace");
1150 kmem_free(ktd
, sizeof(*ktd
));
1151 mutex_enter(&fp
->f_lock
);
1153 mutex_exit(&fp
->f_lock
);
1157 mutex_enter(&ktrace_lock
);
1158 if (ktd_lookup(fp
) != NULL
) {
1162 TAILQ_INSERT_TAIL(&ktdq
, ktd
, ktd_list
);
1164 cv_wait(&lbolt
, &ktrace_lock
);
1165 mutex_exit(&ktrace_lock
);
1176 * need something to (un)trace (XXX - why is this here?)
1186 mutex_enter(proc_lock
);
1191 pg
= pg_find(-pid
, PFIND_LOCKED
);
1195 LIST_FOREACH(p
, &pg
->pg_members
, p_pglist
) {
1197 ret
|= ktrsetchildren(curl
, p
, ops
,
1200 ret
|= ktrops(curl
, p
, ops
, facs
,
1209 p
= p_find(pid
, PFIND_LOCKED
);
1213 ret
|= ktrsetchildren(curl
, p
, ops
, facs
, ktd
);
1215 ret
|= ktrops(curl
, p
, ops
, facs
, ktd
);
1217 mutex_exit(proc_lock
);
1218 if (error
== 0 && !ret
)
1222 mutex_enter(&ktrace_lock
);
1225 * Wakeup the thread so that it can be die if we
1226 * can't trace any process.
1230 if (KTROP(ops
) == KTROP_SET
|| KTROP(ops
) == KTROP_CLEARFILE
)
1232 mutex_exit(&ktrace_lock
);
1239 * fktrace system call
1243 sys_fktrace(struct lwp
*l
, const struct sys_fktrace_args
*uap
, register_t
*retval
)
1247 syscallarg(int) ops;
1248 syscallarg(int) facs;
1249 syscallarg(int) pid;
1254 fd
= SCARG(uap
, fd
);
1255 if ((fp
= fd_getfile(fd
)) == NULL
)
1257 if ((fp
->f_flag
& FWRITE
) == 0)
1260 error
= ktrace_common(l
, SCARG(uap
, ops
),
1261 SCARG(uap
, facs
), SCARG(uap
, pid
), fp
);
1267 * ktrace system call
1271 sys_ktrace(struct lwp
*l
, const struct sys_ktrace_args
*uap
, register_t
*retval
)
1274 syscallarg(const char *) fname;
1275 syscallarg(int) ops;
1276 syscallarg(int) facs;
1277 syscallarg(int) pid;
1279 struct vnode
*vp
= NULL
;
1281 struct nameidata nd
;
1288 if (KTROP(SCARG(uap
, ops
)) != KTROP_CLEAR
) {
1290 * an operation which requires a file argument.
1292 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_USERSPACE
, SCARG(uap
, fname
));
1293 if ((error
= vn_open(&nd
, FREAD
|FWRITE
, 0)) != 0) {
1299 if (vp
->v_type
!= VREG
) {
1300 vn_close(vp
, FREAD
|FWRITE
, l
->l_cred
);
1305 * This uses up a file descriptor slot in the
1306 * tracing process for the duration of this syscall.
1307 * This is not expected to be a problem.
1309 if ((error
= fd_allocfile(&fp
, &fd
)) != 0) {
1310 vn_close(vp
, FWRITE
, l
->l_cred
);
1314 fp
->f_flag
= FWRITE
;
1315 fp
->f_type
= DTYPE_VNODE
;
1317 fp
->f_data
= (void *)vp
;
1320 error
= ktrace_common(l
, SCARG(uap
, ops
), SCARG(uap
, facs
),
1321 SCARG(uap
, pid
), fp
);
1325 fd_abort(curproc
, fp
, fd
);
1327 /* File was used. */
1328 fd_abort(curproc
, NULL
, fd
);
1335 ktrops(lwp_t
*curl
, struct proc
*p
, int ops
, int facs
,
1336 struct ktr_desc
*ktd
)
1338 int vers
= ops
& KTRFAC_VER_MASK
;
1341 mutex_enter(p
->p_lock
);
1342 mutex_enter(&ktrace_lock
);
1344 if (!ktrcanset(curl
, p
))
1357 if (KTROP(ops
) == KTROP_SET
) {
1358 if (p
->p_tracep
!= ktd
) {
1360 * if trace file already in use, relinquish
1366 p
->p_traceflag
|= facs
;
1367 if (kauth_authorize_process(curl
->l_cred
, KAUTH_PROCESS_KTRACE
,
1368 p
, KAUTH_ARG(KAUTH_REQ_PROCESS_KTRACE_PERSISTENT
), NULL
,
1370 p
->p_traceflag
|= KTRFAC_PERSISTENT
;
1373 if (((p
->p_traceflag
&= ~facs
) & KTRFAC_MASK
) == 0) {
1374 /* no more tracing */
1380 p
->p_traceflag
|= vers
;
1382 * Emit an emulation record, every time there is a ktrace
1383 * change/attach request.
1385 if (KTRPOINT(p
, KTR_EMUL
))
1386 p
->p_traceflag
|= KTRFAC_TRC_EMUL
;
1388 p
->p_trace_enabled
= trace_is_enabled(p
);
1389 #ifdef __HAVE_SYSCALL_INTERN
1390 (*p
->p_emul
->e_syscall_intern
)(p
);
1394 mutex_exit(&ktrace_lock
);
1395 mutex_exit(p
->p_lock
);
1401 ktrsetchildren(lwp_t
*curl
, struct proc
*top
, int ops
, int facs
,
1402 struct ktr_desc
*ktd
)
1407 KASSERT(mutex_owned(proc_lock
));
1411 ret
|= ktrops(curl
, p
, ops
, facs
, ktd
);
1413 * If this process has children, descend to them next,
1414 * otherwise do any siblings, and if done with this level,
1415 * follow back up the tree (but not past top).
1417 if (LIST_FIRST(&p
->p_children
) != NULL
) {
1418 p
= LIST_FIRST(&p
->p_children
);
1424 if (LIST_NEXT(p
, p_sibling
) != NULL
) {
1425 p
= LIST_NEXT(p
, p_sibling
);
1435 ktrwrite(struct ktr_desc
*ktd
, struct ktrace_entry
*kte
)
1439 struct iovec aiov
[64], *iov
;
1440 struct ktrace_entry
*top
= kte
;
1441 struct ktr_header
*kth
;
1442 file_t
*fp
= ktd
->ktd_fp
;
1445 auio
.uio_iov
= iov
= &aiov
[0];
1446 auio
.uio_offset
= 0;
1447 auio
.uio_rw
= UIO_WRITE
;
1449 auio
.uio_iovcnt
= 0;
1450 UIO_SETUP_SYSSPACE(&auio
);
1454 kth
= &kte
->kte_kth
;
1456 hlen
= sizeof(struct ktr_header
);
1457 switch (kth
->ktr_version
) {
1461 kth
->ktr_otv
.tv_sec
= ts
.tv_sec
;
1462 kth
->ktr_otv
.tv_usec
= ts
.tv_nsec
/ 1000;
1463 kth
->ktr_unused
= NULL
;
1464 hlen
-= sizeof(kth
->_v
) -
1465 MAX(sizeof(kth
->_v
._v0
), sizeof(kth
->_v
._v1
));
1471 kth
->ktr_ots
.tv_sec
= ts
.tv_sec
;
1472 kth
->ktr_ots
.tv_nsec
= ts
.tv_nsec
;
1473 kth
->ktr_olid
= lid
;
1474 hlen
-= sizeof(kth
->_v
) -
1475 MAX(sizeof(kth
->_v
._v0
), sizeof(kth
->_v
._v1
));
1478 iov
->iov_base
= (void *)kth
;
1479 iov
++->iov_len
= hlen
;
1480 auio
.uio_resid
+= hlen
;
1482 if (kth
->ktr_len
> 0) {
1483 iov
->iov_base
= kte
->kte_buf
;
1484 iov
++->iov_len
= kth
->ktr_len
;
1485 auio
.uio_resid
+= kth
->ktr_len
;
1488 } while ((kte
= TAILQ_NEXT(kte
, kte_list
)) != NULL
&&
1489 auio
.uio_iovcnt
< sizeof(aiov
) / sizeof(aiov
[0]) - 1);
1492 error
= (*fp
->f_ops
->fo_write
)(fp
, &fp
->f_offset
, &auio
,
1493 fp
->f_cred
, FOF_UPDATE_OFFSET
);
1497 if (auio
.uio_resid
> 0)
1504 kpause("ktrzzz", false, 1, NULL
);
1509 * If error encountered, give up tracing on this
1510 * vnode. Don't report EPIPE as this can easily
1511 * happen with fktrace()/ktruss.
1517 "ktrace write failed, errno %d, tracing stopped\n",
1519 (void)ktrderefall(ktd
, 0);
1522 while ((kte
= top
) != NULL
) {
1523 top
= TAILQ_NEXT(top
, kte_list
);
1529 ktrace_thread(void *arg
)
1531 struct ktr_desc
*ktd
= arg
;
1532 file_t
*fp
= ktd
->ktd_fp
;
1533 struct ktrace_entry
*kte
;
1536 mutex_enter(&ktrace_lock
);
1538 kte
= TAILQ_FIRST(&ktd
->ktd_queue
);
1540 if (ktd
->ktd_flags
& KTDF_WAIT
) {
1541 ktd
->ktd_flags
&= ~(KTDF_WAIT
| KTDF_BLOCKING
);
1542 cv_broadcast(&ktd
->ktd_sync_cv
);
1544 if (ktd
->ktd_ref
== 0)
1546 cv_wait(&ktd
->ktd_cv
, &ktrace_lock
);
1549 TAILQ_INIT(&ktd
->ktd_queue
);
1550 ktd
->ktd_qcount
= 0;
1551 ktrerr
= ktd
->ktd_error
;
1552 errcnt
= ktd
->ktd_errcnt
;
1553 ktd
->ktd_error
= ktd
->ktd_errcnt
= 0;
1554 mutex_exit(&ktrace_lock
);
1558 "ktrace failed, fp %p, error 0x%x, total %d\n",
1559 fp
, ktrerr
, errcnt
);
1562 mutex_enter(&ktrace_lock
);
1565 TAILQ_REMOVE(&ktdq
, ktd
, ktd_list
);
1566 mutex_exit(&ktrace_lock
);
1569 * ktrace file descriptor can't be watched (are not visible to
1570 * userspace), so no kqueue stuff here
1571 * XXX: The above comment is wrong, because the fktrace file
1572 * descriptor is available in userland.
1576 cv_destroy(&ktd
->ktd_sync_cv
);
1577 cv_destroy(&ktd
->ktd_cv
);
1579 callout_stop(&ktd
->ktd_wakch
);
1580 callout_destroy(&ktd
->ktd_wakch
);
1581 kmem_free(ktd
, sizeof(*ktd
));
1587 * Return true if caller has permission to set the ktracing state
1588 * of target. Essentially, the target can't possess any
1589 * more permissions than the caller. KTRFAC_PERSISTENT signifies that
1590 * the tracing will persist on sugid processes during exec; it is only
1591 * settable by a process with appropriate credentials.
1593 * TODO: check groups. use caller effective gid.
1596 ktrcanset(lwp_t
*calll
, struct proc
*targetp
)
1598 KASSERT(mutex_owned(targetp
->p_lock
));
1599 KASSERT(mutex_owned(&ktrace_lock
));
1601 if (kauth_authorize_process(calll
->l_cred
, KAUTH_PROCESS_KTRACE
,
1602 targetp
, NULL
, NULL
, NULL
) == 0)
1609 * Put user defined entry to ktrace records.
1612 sys_utrace(struct lwp
*l
, const struct sys_utrace_args
*uap
, register_t
*retval
)
1615 syscallarg(const char *) label;
1616 syscallarg(void *) addr;
1617 syscallarg(size_t) len;
1620 return ktruser(SCARG(uap
, label
), SCARG(uap
, addr
),
1621 SCARG(uap
, len
), 1);