No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / kern_ktrace.c
blob44ac0fccd85feae9a66655fa41ddfba29932976a
1 /* $NetBSD: kern_ktrace.c,v 1.149 2009/08/05 19:53:42 dsl Exp $ */
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
38 * are met:
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
58 * SUCH DAMAGE.
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>
68 #include <sys/proc.h>
69 #include <sys/file.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>
75 #include <sys/kmem.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>
83 #include <sys/sa.h>
84 #include <sys/syscallargs.h>
87 * TODO:
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.
94 struct ktrace_entry {
95 TAILQ_ENTRY(ktrace_entry) kte_list;
96 struct ktr_header kte_kth;
97 void *kte_buf;
98 size_t kte_bufsz;
99 #define KTE_SPACE 32
100 uint8_t kte_space[KTE_SPACE];
103 struct ktr_desc {
104 TAILQ_ENTRY(ktr_desc) ktd_list;
105 int ktd_flags;
106 #define KTDF_WAIT 0x0001
107 #define KTDF_DONE 0x0002
108 #define KTDF_BLOCKING 0x0004
109 #define KTDF_INTERACTIVE 0x0008
110 int ktd_error;
111 #define KTDE_ENOMEM 0x0001
112 #define KTDE_ENOSPC 0x0002
113 int ktd_errcnt;
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;
129 kcondvar_t ktd_cv;
132 static int ktealloc(struct ktrace_entry **, void **, lwp_t *, int,
133 size_t);
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,
137 struct ktr_desc *);
138 static int ktrsetchildren(lwp_t *, struct proc *, int, int,
139 struct ktr_desc *);
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);
160 * Default vaules.
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;
178 int ktrace_on;
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;
184 static void
185 ktd_wakeup(struct ktr_desc *ktd)
188 callout_stop(&ktd->ktd_wakch);
189 cv_signal(&ktd->ktd_cv);
192 static void
193 ktd_callout(void *arg)
196 mutex_enter(&ktrace_lock);
197 ktd_wakeup(arg);
198 mutex_exit(&ktrace_lock);
201 static void
202 ktd_logerrl(struct ktr_desc *ktd, int error)
205 ktd->ktd_error |= error;
206 ktd->ktd_errcnt++;
209 #if 0
210 static void
211 ktd_logerr(struct proc *p, int error)
213 struct ktr_desc *ktd;
215 KASSERT(mutex_owned(&ktrace_lock));
217 ktd = p->p_tracep;
218 if (ktd == NULL)
219 return;
221 ktd_logerrl(ktd, error);
223 #endif
225 static inline int
226 ktrenter(lwp_t *l)
229 if ((l->l_pflag & LP_KTRACTIVE) != 0)
230 return 1;
231 l->l_pflag |= LP_KTRACTIVE;
232 return 0;
235 static inline void
236 ktrexit(lwp_t *l)
239 l->l_pflag &= ~LP_KTRACTIVE;
242 static int
243 ktrace_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
244 void *arg0, void *arg1, void *arg2, void *arg3)
246 struct proc *p;
247 int result;
248 enum kauth_process_req req;
250 result = KAUTH_RESULT_DEFER;
251 p = arg0;
253 if (action != KAUTH_PROCESS_KTRACE)
254 return result;
256 req = (enum kauth_process_req)(unsigned long)arg1;
258 /* Privileged; secmodel should handle these. */
259 if (req == KAUTH_REQ_PROCESS_KTRACE_PERSISTENT)
260 return result;
262 if ((p->p_traceflag & KTRFAC_PERSISTENT) ||
263 (p->p_flag & PK_SUGID))
264 return result;
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;
272 return result;
276 * Initialise the ktrace system.
278 void
279 ktrinit(void)
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.
293 void
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);
302 ktrace_on--;
303 if (--ktd->ktd_ref <= 0) {
304 ktd->ktd_flags |= KTDF_DONE;
305 cv_signal(&ktd->ktd_cv);
309 void
310 ktdref(struct ktr_desc *ktd)
313 KASSERT(mutex_owned(&ktrace_lock));
315 ktd->ktd_ref++;
316 ktrace_on++;
319 struct ktr_desc *
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)) {
329 ktdref(ktd);
330 break;
334 return (ktd);
337 void
338 ktraddentry(lwp_t *l, struct ktrace_entry *kte, int flags)
340 struct proc *p = l->l_proc;
341 struct ktr_desc *ktd;
342 #ifdef DEBUG
343 struct timeval t1, t2;
344 #endif
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);
352 ktrexit(l);
353 ktremul();
354 (void)ktrenter(l);
355 mutex_enter(&ktrace_lock);
358 /* Tracing may have been cancelled. */
359 ktd = p->p_tracep;
360 if (ktd == NULL)
361 goto freekte;
364 * Bump reference count so that the object will remain while
365 * we are here. Note that the trace is controlled by other
366 * process.
368 ktdref(ktd);
370 if (ktd->ktd_flags & KTDF_DONE)
371 goto relktd;
373 if (ktd->ktd_qcount > ktd_maxentry) {
374 ktd_logerrl(ktd, KTDE_ENOSPC);
375 goto relktd;
377 TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
378 ktd->ktd_qcount++;
379 if (ktd->ktd_flags & KTDF_BLOCKING)
380 goto skip_sync;
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.
389 do {
390 ktd->ktd_flags |= KTDF_WAIT;
391 ktd_wakeup(ktd);
392 #ifdef DEBUG
393 getmicrouptime(&t1);
394 #endif
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");
404 break;
406 #ifdef DEBUG
407 getmicrouptime(&t2);
408 timersub(&t2, &t1, &t2);
409 if (t2.tv_sec > 0)
410 log(LOG_NOTICE,
411 "ktrace long wait: %lld.%06ld\n",
412 (long long)t2.tv_sec, (long)t2.tv_usec);
413 #endif
414 } while (p->p_tracep == ktd &&
415 (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
416 else {
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,
424 ktd_callout, ktd);
427 skip_sync:
428 ktdrel(ktd);
429 mutex_exit(&ktrace_lock);
430 ktrexit(l);
431 return;
433 relktd:
434 ktdrel(ktd);
436 freekte:
437 mutex_exit(&ktrace_lock);
438 ktefree(kte);
439 ktrexit(l);
442 void
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)));
467 void
468 ktrderef(struct proc *p)
470 struct ktr_desc *ktd = p->p_tracep;
472 KASSERT(mutex_owned(&ktrace_lock));
474 p->p_traceflag = 0;
475 if (ktd == NULL)
476 return;
477 p->p_tracep = NULL;
479 cv_broadcast(&ktd->ktd_sync_cv);
480 ktdrel(ktd);
483 void
484 ktradref(struct proc *p)
486 struct ktr_desc *ktd = p->p_tracep;
488 KASSERT(mutex_owned(&ktrace_lock));
490 ktdref(ktd);
494 ktrderefall(struct ktr_desc *ktd, int auth)
496 lwp_t *curl = curlwp;
497 struct proc *p;
498 int error = 0;
500 mutex_enter(proc_lock);
501 PROCLIST_FOREACH(p, &allproc) {
502 if ((p->p_flag & PK_MARKER) != 0 || p->p_tracep != ktd)
503 continue;
504 mutex_enter(p->p_lock);
505 mutex_enter(&ktrace_lock);
506 if (p->p_tracep == ktd) {
507 if (!auth || ktrcanset(curl, p))
508 ktrderef(p);
509 else
510 error = EPERM;
512 mutex_exit(&ktrace_lock);
513 mutex_exit(p->p_lock);
515 mutex_exit(proc_lock);
517 return error;
521 ktealloc(struct ktrace_entry **ktep, void **bufp, lwp_t *l, int type,
522 size_t sz)
524 struct proc *p = l->l_proc;
525 struct ktrace_entry *kte;
526 struct ktr_header *kth;
527 struct timespec ts;
528 void *buf;
530 if (ktrenter(l))
531 return EAGAIN;
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);
537 ktrexit(l);
538 return ENOMEM;
540 } else
541 buf = kte->kte_space;
543 kte->kte_bufsz = sz;
544 kte->kte_buf = buf;
546 kth = &kte->kte_kth;
547 (void)memset(kth, 0, sizeof(*kth));
548 kth->ktr_len = sz;
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);
554 nanotime(&ts);
555 switch (KTRFAC_VERSION(p->p_traceflag)) {
556 case 0:
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;
560 break;
561 case 1:
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;
565 break;
566 case 2:
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;
570 break;
571 default:
572 break;
575 *ktep = kte;
576 *bufp = buf;
578 return 0;
581 void
582 ktr_syscall(register_t code, const register_t args[], int narg)
584 lwp_t *l = curlwp;
585 struct proc *p = l->l_proc;
586 struct ktrace_entry *kte;
587 struct ktr_syscall *ktp;
588 register_t *argp;
589 size_t len;
590 u_int i;
592 if (!KTRPOINT(p, KTR_SYSCALL))
593 return;
595 len = sizeof(struct ktr_syscall) + narg * sizeof argp[0];
597 if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSCALL, len))
598 return;
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++)
604 *argp++ = args[i];
606 ktraddentry(l, kte, KTA_WAITOK);
609 void
610 ktr_sysret(register_t code, int error, register_t *retval)
612 lwp_t *l = curlwp;
613 struct ktrace_entry *kte;
614 struct ktr_sysret *ktp;
616 if (!KTRPOINT(l->l_proc, KTR_SYSRET))
617 return;
619 if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSRET,
620 sizeof(struct ktr_sysret)))
621 return;
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);
632 void
633 ktr_namei(const char *path, size_t pathlen)
635 lwp_t *l = curlwp;
637 if (!KTRPOINT(l->l_proc, KTR_NAMEI))
638 return;
640 ktr_kmem(l, KTR_NAMEI, path, pathlen);
643 void
644 ktr_namei2(const char *eroot, size_t erootlen,
645 const char *path, size_t pathlen)
647 lwp_t *l = curlwp;
648 struct ktrace_entry *kte;
649 void *buf;
651 if (!KTRPOINT(l->l_proc, KTR_NAMEI))
652 return;
654 if (ktealloc(&kte, &buf, l, KTR_NAMEI, erootlen + pathlen))
655 return;
656 memcpy(buf, eroot, erootlen);
657 buf = (char *)buf + erootlen;
658 memcpy(buf, path, pathlen);
659 ktraddentry(l, kte, KTA_WAITOK);
662 void
663 ktr_emul(void)
665 lwp_t *l = curlwp;
666 const char *emul = l->l_proc->p_emul->e_name;
668 if (!KTRPOINT(l->l_proc, KTR_EMUL))
669 return;
671 ktr_kmem(l, KTR_EMUL, emul, strlen(emul));
674 void
675 ktr_execarg(const void *bf, size_t len)
677 lwp_t *l = curlwp;
679 if (!KTRPOINT(l->l_proc, KTR_EXEC_ARG))
680 return;
682 ktr_kmem(l, KTR_EXEC_ARG, bf, len);
685 void
686 ktr_execenv(const void *bf, size_t len)
688 lwp_t *l = curlwp;
690 if (!KTRPOINT(l->l_proc, KTR_EXEC_ENV))
691 return;
693 ktr_kmem(l, KTR_EXEC_ENV, bf, len);
696 static void
697 ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
699 struct ktrace_entry *kte;
700 void *buf;
702 if (ktealloc(&kte, &buf, l, type, len))
703 return;
704 memcpy(buf, bf, len);
705 ktraddentry(l, kte, KTA_WAITOK);
708 static void
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;
714 char *cp;
716 next:
717 buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
719 if (ktealloc(&kte, (void *)&ktp, l, KTR_GENIO, buflen))
720 return;
722 ktp->ktr_fd = fd;
723 ktp->ktr_rw = rw;
725 cp = (void *)(ktp + 1);
726 buflen -= sizeof(struct ktr_genio);
727 kte->kte_kth.ktr_len = sizeof(struct ktr_genio);
729 while (buflen > 0) {
730 cnt = min(iov->iov_len, buflen);
731 if (copyin(iov->iov_base, cp, cnt) != 0)
732 goto out;
733 kte->kte_kth.ktr_len += cnt;
734 cp += cnt;
735 buflen -= cnt;
736 resid -= cnt;
737 iov->iov_len -= cnt;
738 if (iov->iov_len == 0)
739 iov++;
740 else
741 iov->iov_base = (char *)iov->iov_base + cnt;
745 * Don't push so many entry at once. It will cause kmem map
746 * shortage.
748 ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
749 if (resid > 0) {
750 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
751 (void)ktrenter(l);
752 preempt();
753 ktrexit(l);
756 goto next;
759 return;
761 out:
762 ktefree(kte);
763 ktrexit(l);
766 void
767 ktr_genio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
769 lwp_t *l = curlwp;
770 struct iovec iov;
772 if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
773 return;
774 iov.iov_base = __UNCONST(addr);
775 iov.iov_len = len;
776 ktr_io(l, fd, rw, &iov, len);
779 void
780 ktr_geniov(int fd, enum uio_rw rw, struct iovec *iov, size_t len, int error)
782 lwp_t *l = curlwp;
784 if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
785 return;
786 ktr_io(l, fd, rw, iov, len);
789 void
790 ktr_mibio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
792 lwp_t *l = curlwp;
793 struct iovec iov;
795 if (!KTRPOINT(l->l_proc, KTR_MIB) || error != 0)
796 return;
797 iov.iov_base = __UNCONST(addr);
798 iov.iov_len = len;
799 ktr_io(l, fd, rw, &iov, len);
802 void
803 ktr_psig(int sig, sig_t action, const sigset_t *mask,
804 const ksiginfo_t *ksi)
806 struct ktrace_entry *kte;
807 lwp_t *l = curlwp;
808 struct {
809 struct ktr_psig kp;
810 siginfo_t si;
811 } *kbuf;
813 if (!KTRPOINT(l->l_proc, KTR_PSIG))
814 return;
816 if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf)))
817 return;
819 kbuf->kp.signo = (char)sig;
820 kbuf->kp.action = action;
821 kbuf->kp.mask = *mask;
823 if (ksi) {
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);
828 } else {
829 kbuf->kp.code = 0;
830 kte->kte_kth.ktr_len = sizeof(struct ktr_psig);
833 ktraddentry(l, kte, KTA_WAITOK);
836 void
837 ktr_csw(int out, int user)
839 lwp_t *l = curlwp;
840 struct proc *p = l->l_proc;
841 struct ktrace_entry *kte;
842 struct ktr_csw *kc;
844 if (!KTRPOINT(p, KTR_CSW))
845 return;
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)
852 return;
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.
863 if (out) {
864 struct timespec ts;
865 if (ktrenter(l))
866 return;
868 nanotime(&l->l_ktrcsw);
869 l->l_pflag |= LP_KTRCSW;
870 nanotime(&ts);
871 if (user)
872 l->l_pflag |= LP_KTRCSWUSER;
873 else
874 l->l_pflag &= ~LP_KTRCSWUSER;
876 ktrexit(l);
877 return;
881 * On the way back in, we need to record twice: once for entry, and
882 * once for exit.
884 if ((l->l_pflag & LP_KTRCSW) != 0) {
885 struct timespec *ts;
886 l->l_pflag &= ~LP_KTRCSW;
888 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
889 return;
891 kc->out = 1;
892 kc->user = ((l->l_pflag & LP_KTRCSWUSER) != 0);
894 ts = &l->l_ktrcsw;
895 switch (KTRFAC_VERSION(p->p_traceflag)) {
896 case 0:
897 kte->kte_kth.ktr_otv.tv_sec = ts->tv_sec;
898 kte->kte_kth.ktr_otv.tv_usec = ts->tv_nsec / 1000;
899 break;
900 case 1:
901 kte->kte_kth.ktr_ots.tv_sec = ts->tv_sec;
902 kte->kte_kth.ktr_ots.tv_nsec = ts->tv_nsec;
903 break;
904 case 2:
905 kte->kte_kth.ktr_ts.tv_sec = ts->tv_sec;
906 kte->kte_kth.ktr_ts.tv_nsec = ts->tv_nsec;
907 break;
908 default:
909 break;
912 ktraddentry(l, kte, KTA_WAITOK);
915 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
916 return;
918 kc->out = 0;
919 kc->user = user;
921 ktraddentry(l, kte, KTA_WAITOK);
924 bool
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;
935 lwp_t *l = curlwp;
936 void *user_dta;
937 int error;
939 if (!KTRPOINT(l->l_proc, KTR_USER))
940 return 0;
942 if (len > KTR_USER_MAXLEN)
943 return ENOSPC;
945 error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
946 if (error != 0)
947 return error;
949 if (ustr) {
950 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
951 ktp->ktr_id[0] = '\0';
952 } else
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)
958 len = 0;
960 ktraddentry(l, kte, KTA_WAITOK);
961 return error;
964 void
965 ktr_kuser(const char *id, void *addr, size_t len)
967 struct ktrace_entry *kte;
968 struct ktr_user *ktp;
969 lwp_t *l = curlwp;
970 int error;
972 if (!KTRPOINT(l->l_proc, KTR_USER))
973 return;
975 if (len > KTR_USER_MAXLEN)
976 return;
978 error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
979 if (error != 0)
980 return;
982 strlcpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
984 memcpy(ktp + 1, addr, len);
986 ktraddentry(l, kte, KTA_WAITOK);
989 void
990 ktr_mmsg(const void *msgh, size_t size)
992 lwp_t *l = curlwp;
994 if (!KTRPOINT(l->l_proc, KTR_MMSG))
995 return;
997 ktr_kmem(l, KTR_MMSG, msgh, size);
1000 void
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;
1006 lwp_t *l = curlwp;
1008 if (!KTRPOINT(l->l_proc, KTR_MOOL))
1009 return;
1011 if (ktealloc(&kte, (void *)&kp, l, KTR_MOOL, size + sizeof(*kp)))
1012 return;
1014 kp->uaddr = uaddr;
1015 kp->size = size;
1016 bf = kp + 1; /* Skip uaddr and size */
1017 (void)memcpy(bf, kaddr, size);
1019 ktraddentry(l, kte, KTA_WAITOK);
1022 void
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;
1028 size_t len, sz;
1029 struct sa_t **sapp;
1030 int i;
1032 if (!KTRPOINT(l->l_proc, KTR_SAUPCALL))
1033 return;
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))
1039 return;
1041 ktp->ktr_type = type;
1042 ktp->ktr_nevent = nevent;
1043 ktp->ktr_nint = nint;
1044 ktp->ktr_sas = sas;
1045 ktp->ktr_ap = ap;
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);
1053 sapp++;
1056 kte->kte_kth.ktr_len = len;
1057 ktraddentry(l, kte, KTA_WAITOK);
1060 void
1061 ktr_mib(const int *name, u_int namelen)
1063 struct ktrace_entry *kte;
1064 int *namep;
1065 size_t size;
1066 lwp_t *l = curlwp;
1068 if (!KTRPOINT(l->l_proc, KTR_MIB))
1069 return;
1071 size = namelen * sizeof(*name);
1073 if (ktealloc(&kte, (void *)&namep, l, KTR_MIB, size))
1074 return;
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)
1086 struct proc *curp;
1087 struct proc *p;
1088 struct pgrp *pg;
1089 struct ktr_desc *ktd = NULL;
1090 int ret = 0;
1091 int error = 0;
1092 int descend;
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);
1109 if (ktd == NULL)
1110 goto done;
1111 error = ktrderefall(ktd, 1);
1112 goto done;
1114 case KTROP_SET:
1115 mutex_enter(&ktrace_lock);
1116 ktd = ktd_lookup(fp);
1117 mutex_exit(&ktrace_lock);
1118 if (ktd == NULL) {
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");
1124 ktd->ktd_flags = 0;
1125 ktd->ktd_qcount = 0;
1126 ktd->ktd_error = 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);
1131 ktd->ktd_ref = 0;
1132 ktd->ktd_fp = fp;
1133 mutex_enter(&ktrace_lock);
1134 ktdref(ktd);
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);
1145 fp->f_count++;
1146 mutex_exit(&fp->f_lock);
1147 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
1148 ktrace_thread, ktd, &ktd->ktd_lwp, "ktrace");
1149 if (error != 0) {
1150 kmem_free(ktd, sizeof(*ktd));
1151 mutex_enter(&fp->f_lock);
1152 fp->f_count--;
1153 mutex_exit(&fp->f_lock);
1154 goto done;
1157 mutex_enter(&ktrace_lock);
1158 if (ktd_lookup(fp) != NULL) {
1159 ktdrel(ktd);
1160 ktd = NULL;
1161 } else
1162 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
1163 if (ktd == NULL)
1164 cv_wait(&lbolt, &ktrace_lock);
1165 mutex_exit(&ktrace_lock);
1166 if (ktd == NULL)
1167 goto done;
1169 break;
1171 case KTROP_CLEAR:
1172 break;
1176 * need something to (un)trace (XXX - why is this here?)
1178 if (!facs) {
1179 error = EINVAL;
1180 goto done;
1184 * do it
1186 mutex_enter(proc_lock);
1187 if (pid < 0) {
1189 * by process group
1191 pg = pg_find(-pid, PFIND_LOCKED);
1192 if (pg == NULL)
1193 error = ESRCH;
1194 else {
1195 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1196 if (descend)
1197 ret |= ktrsetchildren(curl, p, ops,
1198 facs, ktd);
1199 else
1200 ret |= ktrops(curl, p, ops, facs,
1201 ktd);
1205 } else {
1207 * by pid
1209 p = p_find(pid, PFIND_LOCKED);
1210 if (p == NULL)
1211 error = ESRCH;
1212 else if (descend)
1213 ret |= ktrsetchildren(curl, p, ops, facs, ktd);
1214 else
1215 ret |= ktrops(curl, p, ops, facs, ktd);
1217 mutex_exit(proc_lock);
1218 if (error == 0 && !ret)
1219 error = EPERM;
1220 done:
1221 if (ktd != NULL) {
1222 mutex_enter(&ktrace_lock);
1223 if (error != 0) {
1225 * Wakeup the thread so that it can be die if we
1226 * can't trace any process.
1228 ktd_wakeup(ktd);
1230 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE)
1231 ktdrel(ktd);
1232 mutex_exit(&ktrace_lock);
1234 ktrexit(curl);
1235 return (error);
1239 * fktrace system call
1241 /* ARGSUSED */
1243 sys_fktrace(struct lwp *l, const struct sys_fktrace_args *uap, register_t *retval)
1245 /* {
1246 syscallarg(int) fd;
1247 syscallarg(int) ops;
1248 syscallarg(int) facs;
1249 syscallarg(int) pid;
1250 } */
1251 file_t *fp;
1252 int error, fd;
1254 fd = SCARG(uap, fd);
1255 if ((fp = fd_getfile(fd)) == NULL)
1256 return (EBADF);
1257 if ((fp->f_flag & FWRITE) == 0)
1258 error = EBADF;
1259 else
1260 error = ktrace_common(l, SCARG(uap, ops),
1261 SCARG(uap, facs), SCARG(uap, pid), fp);
1262 fd_putfile(fd);
1263 return error;
1267 * ktrace system call
1269 /* ARGSUSED */
1271 sys_ktrace(struct lwp *l, const struct sys_ktrace_args *uap, register_t *retval)
1273 /* {
1274 syscallarg(const char *) fname;
1275 syscallarg(int) ops;
1276 syscallarg(int) facs;
1277 syscallarg(int) pid;
1278 } */
1279 struct vnode *vp = NULL;
1280 file_t *fp = NULL;
1281 struct nameidata nd;
1282 int error = 0;
1283 int fd;
1285 if (ktrenter(l))
1286 return EAGAIN;
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) {
1294 ktrexit(l);
1295 return (error);
1297 vp = nd.ni_vp;
1298 VOP_UNLOCK(vp, 0);
1299 if (vp->v_type != VREG) {
1300 vn_close(vp, FREAD|FWRITE, l->l_cred);
1301 ktrexit(l);
1302 return (EACCES);
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);
1311 ktrexit(l);
1312 return error;
1314 fp->f_flag = FWRITE;
1315 fp->f_type = DTYPE_VNODE;
1316 fp->f_ops = &vnops;
1317 fp->f_data = (void *)vp;
1318 vp = NULL;
1320 error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
1321 SCARG(uap, pid), fp);
1322 if (fp != NULL) {
1323 if (error != 0) {
1324 /* File unused. */
1325 fd_abort(curproc, fp, fd);
1326 } else {
1327 /* File was used. */
1328 fd_abort(curproc, NULL, fd);
1331 return (error);
1335 ktrops(lwp_t *curl, struct proc *p, int ops, int facs,
1336 struct ktr_desc *ktd)
1338 int vers = ops & KTRFAC_VER_MASK;
1339 int error = 0;
1341 mutex_enter(p->p_lock);
1342 mutex_enter(&ktrace_lock);
1344 if (!ktrcanset(curl, p))
1345 goto out;
1347 switch (vers) {
1348 case KTRFACv0:
1349 case KTRFACv1:
1350 case KTRFACv2:
1351 break;
1352 default:
1353 error = EINVAL;
1354 goto out;
1357 if (KTROP(ops) == KTROP_SET) {
1358 if (p->p_tracep != ktd) {
1360 * if trace file already in use, relinquish
1362 ktrderef(p);
1363 p->p_tracep = ktd;
1364 ktradref(p);
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,
1369 NULL) == 0)
1370 p->p_traceflag |= KTRFAC_PERSISTENT;
1371 } else {
1372 /* KTROP_CLEAR */
1373 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
1374 /* no more tracing */
1375 ktrderef(p);
1379 if (p->p_traceflag)
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);
1391 #endif
1393 out:
1394 mutex_exit(&ktrace_lock);
1395 mutex_exit(p->p_lock);
1397 return (1);
1401 ktrsetchildren(lwp_t *curl, struct proc *top, int ops, int facs,
1402 struct ktr_desc *ktd)
1404 struct proc *p;
1405 int ret = 0;
1407 KASSERT(mutex_owned(proc_lock));
1409 p = top;
1410 for (;;) {
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);
1419 continue;
1421 for (;;) {
1422 if (p == top)
1423 return (ret);
1424 if (LIST_NEXT(p, p_sibling) != NULL) {
1425 p = LIST_NEXT(p, p_sibling);
1426 break;
1428 p = p->p_pptr;
1431 /*NOTREACHED*/
1434 void
1435 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
1437 size_t hlen;
1438 struct uio auio;
1439 struct iovec aiov[64], *iov;
1440 struct ktrace_entry *top = kte;
1441 struct ktr_header *kth;
1442 file_t *fp = ktd->ktd_fp;
1443 int error;
1444 next:
1445 auio.uio_iov = iov = &aiov[0];
1446 auio.uio_offset = 0;
1447 auio.uio_rw = UIO_WRITE;
1448 auio.uio_resid = 0;
1449 auio.uio_iovcnt = 0;
1450 UIO_SETUP_SYSSPACE(&auio);
1451 do {
1452 struct timespec ts;
1453 lwpid_t lid;
1454 kth = &kte->kte_kth;
1456 hlen = sizeof(struct ktr_header);
1457 switch (kth->ktr_version) {
1458 case 0:
1459 ts = kth->ktr_time;
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));
1466 break;
1467 case 1:
1468 ts = kth->ktr_time;
1469 lid = kth->ktr_lid;
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));
1476 break;
1478 iov->iov_base = (void *)kth;
1479 iov++->iov_len = hlen;
1480 auio.uio_resid += hlen;
1481 auio.uio_iovcnt++;
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;
1486 auio.uio_iovcnt++;
1488 } while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
1489 auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
1491 again:
1492 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
1493 fp->f_cred, FOF_UPDATE_OFFSET);
1494 switch (error) {
1496 case 0:
1497 if (auio.uio_resid > 0)
1498 goto again;
1499 if (kte != NULL)
1500 goto next;
1501 break;
1503 case EWOULDBLOCK:
1504 kpause("ktrzzz", false, 1, NULL);
1505 goto again;
1507 default:
1509 * If error encountered, give up tracing on this
1510 * vnode. Don't report EPIPE as this can easily
1511 * happen with fktrace()/ktruss.
1513 #ifndef DEBUG
1514 if (error != EPIPE)
1515 #endif
1516 log(LOG_NOTICE,
1517 "ktrace write failed, errno %d, tracing stopped\n",
1518 error);
1519 (void)ktrderefall(ktd, 0);
1522 while ((kte = top) != NULL) {
1523 top = TAILQ_NEXT(top, kte_list);
1524 ktefree(kte);
1528 void
1529 ktrace_thread(void *arg)
1531 struct ktr_desc *ktd = arg;
1532 file_t *fp = ktd->ktd_fp;
1533 struct ktrace_entry *kte;
1534 int ktrerr, errcnt;
1536 mutex_enter(&ktrace_lock);
1537 for (;;) {
1538 kte = TAILQ_FIRST(&ktd->ktd_queue);
1539 if (kte == NULL) {
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)
1545 break;
1546 cv_wait(&ktd->ktd_cv, &ktrace_lock);
1547 continue;
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);
1556 if (ktrerr) {
1557 log(LOG_NOTICE,
1558 "ktrace failed, fp %p, error 0x%x, total %d\n",
1559 fp, ktrerr, errcnt);
1561 ktrwrite(ktd, kte);
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.
1574 closef(fp);
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));
1583 kthread_exit(0);
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)
1603 return (1);
1605 return (0);
1609 * Put user defined entry to ktrace records.
1612 sys_utrace(struct lwp *l, const struct sys_utrace_args *uap, register_t *retval)
1614 /* {
1615 syscallarg(const char *) label;
1616 syscallarg(void *) addr;
1617 syscallarg(size_t) len;
1618 } */
1620 return ktruser(SCARG(uap, label), SCARG(uap, addr),
1621 SCARG(uap, len), 1);