kern_ktrace.c revision 1.124.6.1 1 /* $NetBSD: kern_ktrace.c,v 1.124.6.1 2007/08/16 11:03:31 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.124.6.1 2007/08/16 11:03:31 jmcneill Exp $");
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/proc.h>
76 #include <sys/file.h>
77 #include <sys/namei.h>
78 #include <sys/vnode.h>
79 #include <sys/kernel.h>
80 #include <sys/kthread.h>
81 #include <sys/ktrace.h>
82 #include <sys/kmem.h>
83 #include <sys/syslog.h>
84 #include <sys/filedesc.h>
85 #include <sys/ioctl.h>
86 #include <sys/callout.h>
87 #include <sys/kauth.h>
88
89 #include <sys/mount.h>
90 #include <sys/syscallargs.h>
91
92 /*
93 * TODO:
94 * - need better error reporting?
95 * - userland utility to sort ktrace.out by timestamp.
96 * - keep minimum information in ktrace_entry when rest of alloc failed.
97 * - per trace control of configurable parameters.
98 */
99
100 struct ktrace_entry {
101 TAILQ_ENTRY(ktrace_entry) kte_list;
102 struct ktr_header kte_kth;
103 void *kte_buf;
104 size_t kte_bufsz;
105 #define KTE_SPACE 32
106 uint8_t kte_space[KTE_SPACE];
107 };
108
109 struct ktr_desc {
110 TAILQ_ENTRY(ktr_desc) ktd_list;
111 int ktd_flags;
112 #define KTDF_WAIT 0x0001
113 #define KTDF_DONE 0x0002
114 #define KTDF_BLOCKING 0x0004
115 #define KTDF_INTERACTIVE 0x0008
116 int ktd_error;
117 #define KTDE_ENOMEM 0x0001
118 #define KTDE_ENOSPC 0x0002
119 int ktd_errcnt;
120 int ktd_ref; /* # of reference */
121 int ktd_qcount; /* # of entry in the queue */
122
123 /*
124 * Params to control behaviour.
125 */
126 int ktd_delayqcnt; /* # of entry allowed to delay */
127 int ktd_wakedelay; /* delay of wakeup in *tick* */
128 int ktd_intrwakdl; /* ditto, but when interactive */
129
130 struct file *ktd_fp; /* trace output file */
131 lwp_t *ktd_lwp; /* our kernel thread */
132 TAILQ_HEAD(, ktrace_entry) ktd_queue;
133 callout_t ktd_wakch; /* delayed wakeup */
134 kcondvar_t ktd_sync_cv;
135 kcondvar_t ktd_cv;
136 };
137
138 static int ktealloc(struct ktrace_entry **, void **, lwp_t *, int,
139 size_t);
140 static void ktrwrite(struct ktr_desc *, struct ktrace_entry *);
141 static int ktrace_common(lwp_t *, int, int, int, struct file *);
142 static int ktrops(lwp_t *, struct proc *, int, int,
143 struct ktr_desc *);
144 static int ktrsetchildren(lwp_t *, struct proc *, int, int,
145 struct ktr_desc *);
146 static int ktrcanset(lwp_t *, struct proc *);
147 static int ktrsamefile(struct file *, struct file *);
148 static void ktr_kmem(lwp_t *, int, const void *, size_t);
149 static void ktr_io(lwp_t *, int, enum uio_rw, struct iovec *, size_t);
150
151 static struct ktr_desc *
152 ktd_lookup(struct file *);
153 static void ktdrel(struct ktr_desc *);
154 static void ktdref(struct ktr_desc *);
155 static void ktraddentry(lwp_t *, struct ktrace_entry *, int);
156 /* Flags for ktraddentry (3rd arg) */
157 #define KTA_NOWAIT 0x0000
158 #define KTA_WAITOK 0x0001
159 #define KTA_LARGE 0x0002
160 static void ktefree(struct ktrace_entry *);
161 static void ktd_logerrl(struct ktr_desc *, int);
162 static void ktrace_thread(void *);
163 static int ktrderefall(struct ktr_desc *, int);
164
165 /*
166 * Default vaules.
167 */
168 #define KTD_MAXENTRY 1000 /* XXX: tune */
169 #define KTD_TIMEOUT 5 /* XXX: tune */
170 #define KTD_DELAYQCNT 100 /* XXX: tune */
171 #define KTD_WAKEDELAY 5000 /* XXX: tune */
172 #define KTD_INTRWAKDL 100 /* XXX: tune */
173
174 /*
175 * Patchable variables.
176 */
177 int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */
178 int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */
179 int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */
180 int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */
181 int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */
182
183 kmutex_t ktrace_lock;
184 int ktrace_on;
185 static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq);
186
187 MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer");
188 POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0,
189 "ktepl", &pool_allocator_nointr, IPL_NONE);
190
191 static void
192 ktd_wakeup(struct ktr_desc *ktd)
193 {
194
195 callout_stop(&ktd->ktd_wakch);
196 cv_signal(&ktd->ktd_cv);
197 }
198
199 static void
200 ktd_callout(void *arg)
201 {
202
203 /*
204 * XXXSMP Should be acquiring ktrace_lock, but that
205 * is not yet possible from a callout. For now, we'll
206 * rely on the callout & ktrace thread both holding the
207 * kernel_lock.
208 */
209 ktd_wakeup(arg);
210 }
211
212 static void
213 ktd_logerrl(struct ktr_desc *ktd, int error)
214 {
215
216 ktd->ktd_error |= error;
217 ktd->ktd_errcnt++;
218 }
219
220 #if 0
221 static void
222 ktd_logerr(struct proc *p, int error)
223 {
224 struct ktr_desc *ktd;
225
226 KASSERT(mutex_owned(&ktrace_lock));
227
228 ktd = p->p_tracep;
229 if (ktd == NULL)
230 return;
231
232 ktd_logerrl(ktd, error);
233 }
234 #endif
235
236 static inline int
237 ktrenter(lwp_t *l)
238 {
239
240 if ((l->l_pflag & LP_KTRACTIVE) != 0)
241 return 1;
242 l->l_pflag |= LP_KTRACTIVE;
243 return 0;
244 }
245
246 static inline void
247 ktrexit(lwp_t *l)
248 {
249
250 l->l_pflag &= ~LP_KTRACTIVE;
251 }
252
253 /*
254 * Initialise the ktrace system.
255 */
256 void
257 ktrinit(void)
258 {
259
260 mutex_init(&ktrace_lock, MUTEX_DEFAULT, IPL_NONE);
261 }
262
263 /*
264 * Release a reference. Called with ktrace_lock held.
265 */
266 void
267 ktdrel(struct ktr_desc *ktd)
268 {
269
270 KASSERT(mutex_owned(&ktrace_lock));
271
272 KDASSERT(ktd->ktd_ref != 0);
273 KASSERT(ktd->ktd_ref > 0);
274 KASSERT(ktrace_on > 0);
275 ktrace_on--;
276 if (--ktd->ktd_ref <= 0) {
277 ktd->ktd_flags |= KTDF_DONE;
278 cv_signal(&ktd->ktd_cv);
279 }
280 }
281
282 void
283 ktdref(struct ktr_desc *ktd)
284 {
285
286 KASSERT(mutex_owned(&ktrace_lock));
287
288 ktd->ktd_ref++;
289 ktrace_on++;
290 }
291
292 struct ktr_desc *
293 ktd_lookup(struct file *fp)
294 {
295 struct ktr_desc *ktd;
296
297 KASSERT(mutex_owned(&ktrace_lock));
298
299 for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL;
300 ktd = TAILQ_NEXT(ktd, ktd_list)) {
301 if (ktrsamefile(ktd->ktd_fp, fp)) {
302 ktdref(ktd);
303 break;
304 }
305 }
306
307 return (ktd);
308 }
309
310 void
311 ktraddentry(lwp_t *l, struct ktrace_entry *kte, int flags)
312 {
313 struct proc *p = l->l_proc;
314 struct ktr_desc *ktd;
315 #ifdef DEBUG
316 struct timeval t1, t2;
317 #endif
318
319 mutex_enter(&ktrace_lock);
320
321 if (p->p_traceflag & KTRFAC_TRC_EMUL) {
322 /* Add emulation trace before first entry for this process */
323 p->p_traceflag &= ~KTRFAC_TRC_EMUL;
324 mutex_exit(&ktrace_lock);
325 ktrexit(l);
326 ktremul();
327 (void)ktrenter(l);
328 mutex_enter(&ktrace_lock);
329 }
330
331 /* Tracing may have been cancelled. */
332 ktd = p->p_tracep;
333 if (ktd == NULL)
334 goto freekte;
335
336 /*
337 * Bump reference count so that the object will remain while
338 * we are here. Note that the trace is controlled by other
339 * process.
340 */
341 ktdref(ktd);
342
343 if (ktd->ktd_flags & KTDF_DONE)
344 goto relktd;
345
346 if (ktd->ktd_qcount > ktd_maxentry) {
347 ktd_logerrl(ktd, KTDE_ENOSPC);
348 goto relktd;
349 }
350 TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
351 ktd->ktd_qcount++;
352 if (ktd->ktd_flags & KTDF_BLOCKING)
353 goto skip_sync;
354
355 if (flags & KTA_WAITOK &&
356 (/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT ||
357 ktd->ktd_qcount > ktd_maxentry >> 1))
358 /*
359 * Sync with writer thread since we're requesting rather
360 * big one or many requests are pending.
361 */
362 do {
363 ktd->ktd_flags |= KTDF_WAIT;
364 ktd_wakeup(ktd);
365 #ifdef DEBUG
366 getmicrouptime(&t1);
367 #endif
368 if (cv_timedwait(&ktd->ktd_sync_cv, &ktrace_lock,
369 ktd_timeout * hz) != 0) {
370 ktd->ktd_flags |= KTDF_BLOCKING;
371 /*
372 * Maybe the writer thread is blocking
373 * completely for some reason, but
374 * don't stop target process forever.
375 */
376 log(LOG_NOTICE, "ktrace timeout\n");
377 break;
378 }
379 #ifdef DEBUG
380 getmicrouptime(&t2);
381 timersub(&t2, &t1, &t2);
382 if (t2.tv_sec > 0)
383 log(LOG_NOTICE,
384 "ktrace long wait: %ld.%06ld\n",
385 t2.tv_sec, t2.tv_usec);
386 #endif
387 } while (p->p_tracep == ktd &&
388 (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
389 else {
390 /* Schedule delayed wakeup */
391 if (ktd->ktd_qcount > ktd->ktd_delayqcnt)
392 ktd_wakeup(ktd); /* Wakeup now */
393 else if (!callout_pending(&ktd->ktd_wakch))
394 callout_reset(&ktd->ktd_wakch,
395 ktd->ktd_flags & KTDF_INTERACTIVE ?
396 ktd->ktd_intrwakdl : ktd->ktd_wakedelay,
397 ktd_callout, ktd);
398 }
399
400 skip_sync:
401 ktdrel(ktd);
402 mutex_exit(&ktrace_lock);
403 ktrexit(l);
404 return;
405
406 relktd:
407 ktdrel(ktd);
408
409 freekte:
410 mutex_exit(&ktrace_lock);
411 ktefree(kte);
412 ktrexit(l);
413 }
414
415 void
416 ktefree(struct ktrace_entry *kte)
417 {
418
419 KERNEL_LOCK(1, curlwp); /* XXXSMP */
420 if (kte->kte_buf != kte->kte_space)
421 kmem_free(kte->kte_buf, kte->kte_bufsz);
422 pool_put(&kte_pool, kte);
423 KERNEL_UNLOCK_ONE(curlwp); /* XXXSMP */
424 }
425
426 /*
427 * "deep" compare of two files for the purposes of clearing a trace.
428 * Returns true if they're the same open file, or if they point at the
429 * same underlying vnode/socket.
430 */
431
432 int
433 ktrsamefile(struct file *f1, struct file *f2)
434 {
435
436 return ((f1 == f2) ||
437 ((f1 != NULL) && (f2 != NULL) &&
438 (f1->f_type == f2->f_type) &&
439 (f1->f_data == f2->f_data)));
440 }
441
442 void
443 ktrderef(struct proc *p)
444 {
445 struct ktr_desc *ktd = p->p_tracep;
446
447 KASSERT(mutex_owned(&ktrace_lock));
448
449 p->p_traceflag = 0;
450 if (ktd == NULL)
451 return;
452 p->p_tracep = NULL;
453
454 cv_broadcast(&ktd->ktd_sync_cv);
455 ktdrel(ktd);
456 }
457
458 void
459 ktradref(struct proc *p)
460 {
461 struct ktr_desc *ktd = p->p_tracep;
462
463 KASSERT(mutex_owned(&ktrace_lock));
464
465 ktdref(ktd);
466 }
467
468 int
469 ktrderefall(struct ktr_desc *ktd, int auth)
470 {
471 lwp_t *curl = curlwp;
472 struct proc *p;
473 int error = 0;
474
475 mutex_enter(&proclist_lock);
476 PROCLIST_FOREACH(p, &allproc) {
477 if (p->p_tracep != ktd)
478 continue;
479 mutex_enter(&p->p_mutex);
480 mutex_enter(&ktrace_lock);
481 if (p->p_tracep == ktd) {
482 if (!auth || ktrcanset(curl, p))
483 ktrderef(p);
484 else
485 error = EPERM;
486 }
487 mutex_exit(&ktrace_lock);
488 mutex_exit(&p->p_mutex);
489 }
490 mutex_exit(&proclist_lock);
491
492 return error;
493 }
494
495 int
496 ktealloc(struct ktrace_entry **ktep, void **bufp, lwp_t *l, int type,
497 size_t sz)
498 {
499 struct proc *p = l->l_proc;
500 struct ktrace_entry *kte;
501 struct ktr_header *kth;
502 void *buf;
503
504 if (ktrenter(l))
505 return EAGAIN;
506
507 KERNEL_LOCK(1, l); /* XXXSMP */
508 kte = pool_get(&kte_pool, PR_WAITOK);
509 if (sz > sizeof(kte->kte_space)) {
510 if ((buf = kmem_alloc(sz, KM_SLEEP)) == NULL) {
511 pool_put(&kte_pool, kte);
512 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
513 ktrexit(l);
514 return ENOMEM;
515 }
516 } else
517 buf = kte->kte_space;
518 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
519
520 kte->kte_bufsz = sz;
521 kte->kte_buf = buf;
522
523 kth = &kte->kte_kth;
524 (void)memset(kth, 0, sizeof(*kth));
525 kth->ktr_len = sz;
526 kth->ktr_type = type;
527 kth->ktr_pid = p->p_pid;
528 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
529 kth->ktr_version = KTRFAC_VERSION(p->p_traceflag);
530
531 switch (KTRFAC_VERSION(p->p_traceflag)) {
532 case 0:
533 /* This is the original format */
534 microtime(&kth->ktr_tv);
535 break;
536 case 1:
537 kth->ktr_lid = l->l_lid;
538 nanotime(&kth->ktr_time);
539 break;
540 default:
541 break;
542 }
543
544 *ktep = kte;
545 *bufp = buf;
546
547 return 0;
548 }
549
550 void
551 ktr_syscall(register_t code, register_t realcode,
552 const struct sysent *callp, register_t args[])
553 {
554 lwp_t *l = curlwp;
555 struct proc *p = l->l_proc;
556 struct ktrace_entry *kte;
557 struct ktr_syscall *ktp;
558 register_t *argp;
559 int argsize;
560 size_t len;
561 u_int i;
562
563 if (!KTRPOINT(p, KTR_SYSCALL))
564 return;
565
566 if (callp == NULL)
567 callp = p->p_emul->e_sysent;
568
569 argsize = callp[code].sy_argsize;
570 #ifdef _LP64
571 if (p->p_flag & PK_32)
572 argsize = argsize << 1;
573 #endif
574 len = sizeof(struct ktr_syscall) + argsize;
575
576 if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSCALL, len))
577 return;
578
579 ktp->ktr_code = realcode;
580 ktp->ktr_argsize = argsize;
581 argp = (register_t *)(ktp + 1);
582 for (i = 0; i < (argsize / sizeof(*argp)); i++)
583 *argp++ = args[i];
584
585 ktraddentry(l, kte, KTA_WAITOK);
586 }
587
588 void
589 ktr_sysret(register_t code, int error, register_t *retval)
590 {
591 lwp_t *l = curlwp;
592 struct ktrace_entry *kte;
593 struct ktr_sysret *ktp;
594
595 if (!KTRPOINT(l->l_proc, KTR_SYSRET))
596 return;
597
598 if (ktealloc(&kte, (void *)&ktp, l, KTR_SYSRET,
599 sizeof(struct ktr_sysret)))
600 return;
601
602 ktp->ktr_code = code;
603 ktp->ktr_eosys = 0; /* XXX unused */
604 ktp->ktr_error = error;
605 ktp->ktr_retval = retval ? retval[0] : 0;
606 ktp->ktr_retval_1 = retval ? retval[1] : 0;
607
608 ktraddentry(l, kte, KTA_WAITOK);
609 }
610
611 void
612 ktr_namei(const char *path, size_t pathlen)
613 {
614 lwp_t *l = curlwp;
615
616 if (!KTRPOINT(l->l_proc, KTR_NAMEI))
617 return;
618
619 ktr_kmem(l, KTR_NAMEI, path, pathlen);
620 }
621
622 void
623 ktr_namei2(const char *eroot, size_t erootlen,
624 const char *path, size_t pathlen)
625 {
626 lwp_t *l = curlwp;
627 struct ktrace_entry *kte;
628 void *buf;
629
630 if (!KTRPOINT(l->l_proc, KTR_NAMEI))
631 return;
632
633 if (ktealloc(&kte, &buf, l, KTR_NAMEI, erootlen + pathlen))
634 return;
635 memcpy(buf, eroot, erootlen);
636 buf = (char *)buf + erootlen;
637 memcpy(buf, path, pathlen);
638 ktraddentry(l, kte, KTA_WAITOK);
639 }
640
641 void
642 ktr_emul(void)
643 {
644 lwp_t *l = curlwp;
645 const char *emul = l->l_proc->p_emul->e_name;
646
647 if (!KTRPOINT(l->l_proc, KTR_EMUL))
648 return;
649
650 ktr_kmem(l, KTR_EMUL, emul, strlen(emul));
651 }
652
653 void
654 ktr_execarg(const void *bf, size_t len)
655 {
656 lwp_t *l = curlwp;
657
658 if (!KTRPOINT(l->l_proc, KTR_EXEC_ARG))
659 return;
660
661 ktr_kmem(l, KTR_EXEC_ARG, bf, len);
662 }
663
664 void
665 ktr_execenv(const void *bf, size_t len)
666 {
667 lwp_t *l = curlwp;
668
669 if (!KTRPOINT(l->l_proc, KTR_EXEC_ENV))
670 return;
671
672 ktr_kmem(l, KTR_EXEC_ENV, bf, len);
673 }
674
675 static void
676 ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
677 {
678 struct ktrace_entry *kte;
679 void *buf;
680
681 if (ktealloc(&kte, &buf, l, type, len))
682 return;
683 memcpy(buf, bf, len);
684 ktraddentry(l, kte, KTA_WAITOK);
685 }
686
687 static void
688 ktr_io(lwp_t *l, int fd, enum uio_rw rw, struct iovec *iov, size_t len)
689 {
690 struct ktrace_entry *kte;
691 struct ktr_genio *ktp;
692 size_t resid = len, cnt, buflen;
693 void *cp;
694
695 next:
696 buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
697
698 if (ktealloc(&kte, (void *)&ktp, l, KTR_GENIO, buflen))
699 return;
700
701 ktp->ktr_fd = fd;
702 ktp->ktr_rw = rw;
703
704 cp = (void *)(ktp + 1);
705 buflen -= sizeof(struct ktr_genio);
706 kte->kte_kth.ktr_len = sizeof(struct ktr_genio);
707
708 while (buflen > 0) {
709 cnt = min(iov->iov_len, buflen);
710 if (copyin(iov->iov_base, cp, cnt) != 0)
711 goto out;
712 kte->kte_kth.ktr_len += cnt;
713 buflen -= cnt;
714 resid -= cnt;
715 iov->iov_len -= cnt;
716 if (iov->iov_len == 0)
717 iov++;
718 else
719 iov->iov_base = (char *)iov->iov_base + cnt;
720 }
721
722 /*
723 * Don't push so many entry at once. It will cause kmem map
724 * shortage.
725 */
726 ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
727 if (resid > 0) {
728 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
729 (void)ktrenter(l);
730 preempt();
731 ktrexit(l);
732 }
733
734 goto next;
735 }
736
737 return;
738
739 out:
740 ktefree(kte);
741 ktrexit(l);
742 }
743
744 void
745 ktr_genio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
746 {
747 lwp_t *l = curlwp;
748 struct iovec iov;
749
750 if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
751 return;
752 iov.iov_base = __UNCONST(addr);
753 iov.iov_len = len;
754 ktr_io(l, fd, rw, &iov, len);
755 }
756
757 void
758 ktr_geniov(int fd, enum uio_rw rw, struct iovec *iov, size_t len, int error)
759 {
760 lwp_t *l = curlwp;
761
762 if (!KTRPOINT(l->l_proc, KTR_GENIO) || error != 0)
763 return;
764 ktr_io(l, fd, rw, iov, len);
765 }
766
767 void
768 ktr_mibio(int fd, enum uio_rw rw, const void *addr, size_t len, int error)
769 {
770 lwp_t *l = curlwp;
771 struct iovec iov;
772
773 if (!KTRPOINT(l->l_proc, KTR_MIB) || error != 0)
774 return;
775 iov.iov_base = __UNCONST(addr);
776 iov.iov_len = len;
777 ktr_io(l, fd, rw, &iov, len);
778 }
779
780 void
781 ktr_psig(int sig, sig_t action, const sigset_t *mask,
782 const ksiginfo_t *ksi)
783 {
784 struct ktrace_entry *kte;
785 lwp_t *l = curlwp;
786 struct {
787 struct ktr_psig kp;
788 siginfo_t si;
789 } *kbuf;
790
791 if (!KTRPOINT(l->l_proc, KTR_PSIG))
792 return;
793
794 if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf)))
795 return;
796
797 kbuf->kp.signo = (char)sig;
798 kbuf->kp.action = action;
799 kbuf->kp.mask = *mask;
800
801 if (ksi) {
802 kbuf->kp.code = KSI_TRAPCODE(ksi);
803 (void)memset(&kbuf->si, 0, sizeof(kbuf->si));
804 kbuf->si._info = ksi->ksi_info;
805 kte->kte_kth.ktr_len = sizeof(*kbuf);
806 } else {
807 kbuf->kp.code = 0;
808 kte->kte_kth.ktr_len = sizeof(struct ktr_psig);
809 }
810
811 ktraddentry(l, kte, KTA_WAITOK);
812 }
813
814 void
815 ktr_csw(int out, int user)
816 {
817 lwp_t *l = curlwp;
818 struct proc *p = l->l_proc;
819 struct ktrace_entry *kte;
820 struct ktr_csw *kc;
821
822 if (!KTRPOINT(p, KTR_CSW))
823 return;
824
825 /*
826 * Don't record context switches resulting from blocking on
827 * locks; it's too easy to get duff results.
828 */
829 if (l->l_syncobj == &mutex_syncobj || l->l_syncobj == &rw_syncobj)
830 return;
831
832 /*
833 * We can't sleep if we're already going to sleep (if original
834 * condition is met during sleep, we hang up).
835 *
836 * XXX This is not ideal: it would be better to maintain a pool
837 * of ktes and actually push this to the kthread when context
838 * switch happens, however given the points where we are called
839 * from that is difficult to do.
840 */
841 if (out) {
842 if (ktrenter(l))
843 return;
844
845 switch (KTRFAC_VERSION(p->p_traceflag)) {
846 case 0:
847 /* This is the original format */
848 microtime(&l->l_ktrcsw.tv);
849 l->l_pflag |= LP_KTRCSW;
850 break;
851 case 1:
852 nanotime(&l->l_ktrcsw.ts);
853 l->l_pflag |= LP_KTRCSW;
854 break;
855 default:
856 break;
857 }
858
859 if (user)
860 l->l_pflag |= LP_KTRCSWUSER;
861 else
862 l->l_pflag &= ~LP_KTRCSWUSER;
863
864 ktrexit(l);
865 return;
866 }
867
868 /*
869 * On the way back in, we need to record twice: once for entry, and
870 * once for exit.
871 */
872 if ((l->l_pflag & LP_KTRCSW) != 0) {
873 l->l_pflag &= ~LP_KTRCSW;
874
875 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
876 return;
877
878 kc->out = 1;
879 kc->user = ((l->l_pflag & LP_KTRCSWUSER) != 0);
880
881 switch (KTRFAC_VERSION(p->p_traceflag)) {
882 case 0:
883 /* This is the original format */
884 memcpy(&kte->kte_kth.ktr_tv, &l->l_ktrcsw.tv,
885 sizeof(kte->kte_kth.ktr_tv));
886 break;
887 case 1:
888 memcpy(&kte->kte_kth.ktr_time, &l->l_ktrcsw.ts,
889 sizeof(kte->kte_kth.ktr_time));
890 break;
891 default:
892 break;
893 }
894
895 ktraddentry(l, kte, KTA_WAITOK);
896 }
897
898 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
899 return;
900
901 kc->out = 0;
902 kc->user = user;
903
904 ktraddentry(l, kte, KTA_WAITOK);
905 }
906
907 bool
908 ktrpoint(int fac)
909 {
910
911 if (__predict_false(ktrace_on))
912 return false;
913 return KTRPOINT(curlwp->l_proc, fac);
914 }
915
916 int
917 ktruser(const char *id, void *addr, size_t len, int ustr)
918 {
919 struct ktrace_entry *kte;
920 struct ktr_user *ktp;
921 lwp_t *l = curlwp;
922 void *user_dta;
923 int error;
924
925 if (!KTRPOINT(l->l_proc, KTR_USER))
926 return 0;
927
928 if (len > KTR_USER_MAXLEN)
929 return ENOSPC;
930
931 error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
932 if (error != 0)
933 return error;
934
935 if (ustr) {
936 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
937 ktp->ktr_id[0] = '\0';
938 } else
939 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
940 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
941
942 user_dta = (void *)(ktp + 1);
943 if ((error = copyin(addr, (void *)user_dta, len)) != 0)
944 len = 0;
945
946 ktraddentry(l, kte, KTA_WAITOK);
947 return error;
948 }
949
950 void
951 ktr_kuser(const char *id, void *addr, size_t len)
952 {
953 struct ktrace_entry *kte;
954 struct ktr_user *ktp;
955 lwp_t *l = curlwp;
956 int error;
957
958 if (!KTRPOINT(l->l_proc, KTR_USER))
959 return;
960
961 if (len > KTR_USER_MAXLEN)
962 return;
963
964 error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
965 if (error != 0)
966 return;
967
968 strlcpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
969
970 memcpy(ktp + 1, addr, len);
971
972 ktraddentry(l, kte, KTA_WAITOK);
973 }
974
975 void
976 ktr_mmsg(const void *msgh, size_t size)
977 {
978 lwp_t *l = curlwp;
979
980 if (!KTRPOINT(l->l_proc, KTR_MMSG))
981 return;
982
983 ktr_kmem(l, KTR_MMSG, msgh, size);
984 }
985
986 void
987 ktr_mool(const void *kaddr, size_t size, const void *uaddr)
988 {
989 struct ktrace_entry *kte;
990 struct ktr_mool *kp;
991 struct ktr_mool *bf;
992 lwp_t *l = curlwp;
993
994 if (!KTRPOINT(l->l_proc, KTR_MOOL))
995 return;
996
997 if (ktealloc(&kte, (void *)&kp, l, KTR_MOOL, size + sizeof(*kp)))
998 return;
999
1000 kp->uaddr = uaddr;
1001 kp->size = size;
1002 bf = kp + 1; /* Skip uaddr and size */
1003 (void)memcpy(bf, kaddr, size);
1004
1005 ktraddentry(l, kte, KTA_WAITOK);
1006 }
1007
1008 void
1009 ktr_mib(const int *name, u_int namelen)
1010 {
1011 struct ktrace_entry *kte;
1012 int *namep;
1013 size_t size;
1014 lwp_t *l = curlwp;
1015
1016 if (!KTRPOINT(l->l_proc, KTR_MIB))
1017 return;
1018
1019 size = namelen * sizeof(*name);
1020
1021 if (ktealloc(&kte, (void *)&namep, l, KTR_MIB, size))
1022 return;
1023
1024 (void)memcpy(namep, name, namelen * sizeof(*name));
1025
1026 ktraddentry(l, kte, KTA_WAITOK);
1027 }
1028
1029 /* Interface and common routines */
1030
1031 int
1032 ktrace_common(lwp_t *curl, int ops, int facs, int pid, struct file *fp)
1033 {
1034 struct proc *curp;
1035 struct proc *p;
1036 struct pgrp *pg;
1037 struct ktr_desc *ktd = NULL;
1038 int ret = 0;
1039 int error = 0;
1040 int descend;
1041
1042 curp = curl->l_proc;
1043 descend = ops & KTRFLAG_DESCEND;
1044 facs = facs & ~((unsigned) KTRFAC_ROOT);
1045
1046 (void)ktrenter(curl);
1047
1048 switch (KTROP(ops)) {
1049
1050 case KTROP_CLEARFILE:
1051 /*
1052 * Clear all uses of the tracefile
1053 */
1054 mutex_enter(&ktrace_lock);
1055 ktd = ktd_lookup(fp);
1056 mutex_exit(&ktrace_lock);
1057 if (ktd == NULL)
1058 goto done;
1059 error = ktrderefall(ktd, 1);
1060 goto done;
1061
1062 case KTROP_SET:
1063 mutex_enter(&ktrace_lock);
1064 ktd = ktd_lookup(fp);
1065 mutex_exit(&ktrace_lock);
1066 if (ktd == NULL) {
1067 ktd = kmem_alloc(sizeof(*ktd), KM_SLEEP);
1068 TAILQ_INIT(&ktd->ktd_queue);
1069 callout_init(&ktd->ktd_wakch, 0);
1070 cv_init(&ktd->ktd_cv, "ktrwait");
1071 cv_init(&ktd->ktd_sync_cv, "ktrsync");
1072 ktd->ktd_flags = 0;
1073 ktd->ktd_qcount = 0;
1074 ktd->ktd_error = 0;
1075 ktd->ktd_errcnt = 0;
1076 ktd->ktd_delayqcnt = ktd_delayqcnt;
1077 ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
1078 ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
1079 ktd->ktd_ref = 0;
1080 mutex_enter(&ktrace_lock);
1081 ktdref(ktd);
1082 mutex_exit(&ktrace_lock);
1083
1084 /*
1085 * XXX: not correct. needs an way to detect
1086 * whether ktruss or ktrace.
1087 */
1088 if (fp->f_type == DTYPE_PIPE)
1089 ktd->ktd_flags |= KTDF_INTERACTIVE;
1090
1091 error = kthread_create(PRI_NONE, 0, NULL,
1092 ktrace_thread, ktd, &ktd->ktd_lwp, "ktrace");
1093 if (error != 0) {
1094 kmem_free(ktd, sizeof(*ktd));
1095 goto done;
1096 }
1097
1098 simple_lock(&fp->f_slock);
1099 fp->f_count++;
1100 simple_unlock(&fp->f_slock);
1101 ktd->ktd_fp = fp;
1102
1103 mutex_enter(&ktrace_lock);
1104 if (ktd_lookup(fp) != NULL) {
1105 ktdrel(ktd);
1106 ktd = NULL;
1107 } else
1108 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
1109 if (ktd == NULL)
1110 cv_wait(&lbolt, &ktrace_lock);
1111 mutex_exit(&ktrace_lock);
1112 if (ktd == NULL)
1113 goto done;
1114 }
1115 break;
1116
1117 case KTROP_CLEAR:
1118 break;
1119 }
1120
1121 /*
1122 * need something to (un)trace (XXX - why is this here?)
1123 */
1124 if (!facs) {
1125 error = EINVAL;
1126 goto done;
1127 }
1128
1129 /*
1130 * do it
1131 */
1132 mutex_enter(&proclist_lock);
1133 if (pid < 0) {
1134 /*
1135 * by process group
1136 */
1137 pg = pg_find(-pid, PFIND_LOCKED);
1138 if (pg == NULL)
1139 error = ESRCH;
1140 else {
1141 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1142 if (descend)
1143 ret |= ktrsetchildren(curl, p, ops,
1144 facs, ktd);
1145 else
1146 ret |= ktrops(curl, p, ops, facs,
1147 ktd);
1148 }
1149 }
1150
1151 } else {
1152 /*
1153 * by pid
1154 */
1155 p = p_find(pid, PFIND_LOCKED);
1156 if (p == NULL)
1157 error = ESRCH;
1158 else if (descend)
1159 ret |= ktrsetchildren(curl, p, ops, facs, ktd);
1160 else
1161 ret |= ktrops(curl, p, ops, facs, ktd);
1162 }
1163 mutex_exit(&proclist_lock);
1164 if (error == 0 && !ret)
1165 error = EPERM;
1166 done:
1167 if (ktd != NULL) {
1168 mutex_enter(&ktrace_lock);
1169 if (error != 0) {
1170 /*
1171 * Wakeup the thread so that it can be die if we
1172 * can't trace any process.
1173 */
1174 ktd_wakeup(ktd);
1175 }
1176 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE)
1177 ktdrel(ktd);
1178 mutex_exit(&ktrace_lock);
1179 }
1180 ktrexit(curl);
1181 return (error);
1182 }
1183
1184 /*
1185 * fktrace system call
1186 */
1187 /* ARGSUSED */
1188 int
1189 sys_fktrace(lwp_t *l, void *v, register_t *retval)
1190 {
1191 struct sys_fktrace_args /* {
1192 syscallarg(int) fd;
1193 syscallarg(int) ops;
1194 syscallarg(int) facs;
1195 syscallarg(int) pid;
1196 } */ *uap = v;
1197 struct file *fp = NULL;
1198 struct filedesc *fdp = l->l_proc->p_fd;
1199 int error;
1200
1201 fdp = l->l_proc->p_fd;
1202 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1203 return (EBADF);
1204
1205 FILE_USE(fp);
1206
1207 if ((fp->f_flag & FWRITE) == 0)
1208 error = EBADF;
1209 else
1210 error = ktrace_common(l, SCARG(uap, ops),
1211 SCARG(uap, facs), SCARG(uap, pid), fp);
1212
1213 FILE_UNUSE(fp, l);
1214
1215 return error;
1216 }
1217
1218 /*
1219 * ktrace system call
1220 */
1221 /* ARGSUSED */
1222 int
1223 sys_ktrace(lwp_t *l, void *v, register_t *retval)
1224 {
1225 struct sys_ktrace_args /* {
1226 syscallarg(const char *) fname;
1227 syscallarg(int) ops;
1228 syscallarg(int) facs;
1229 syscallarg(int) pid;
1230 } */ *uap = v;
1231 struct vnode *vp = NULL;
1232 struct file *fp = NULL;
1233 struct nameidata nd;
1234 int error = 0;
1235 int fd;
1236
1237 if (ktrenter(l))
1238 return EAGAIN;
1239
1240 if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) {
1241 /*
1242 * an operation which requires a file argument.
1243 */
1244 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
1245 l);
1246 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
1247 ktrexit(l);
1248 return (error);
1249 }
1250 vp = nd.ni_vp;
1251 VOP_UNLOCK(vp, 0);
1252 if (vp->v_type != VREG) {
1253 (void) vn_close(vp, FREAD|FWRITE, l->l_cred, l);
1254 ktrexit(l);
1255 return (EACCES);
1256 }
1257 /*
1258 * XXX This uses up a file descriptor slot in the
1259 * tracing process for the duration of this syscall.
1260 * This is not expected to be a problem. If
1261 * falloc(NULL, ...) DTRT we could skip that part, but
1262 * that would require changing its interface to allow
1263 * the caller to pass in a ucred..
1264 *
1265 * This will FILE_USE the fp it returns, if any.
1266 * Keep it in use until we return.
1267 */
1268 if ((error = falloc(l, &fp, &fd)) != 0)
1269 goto done;
1270
1271 fp->f_flag = FWRITE;
1272 fp->f_type = DTYPE_VNODE;
1273 fp->f_ops = &vnops;
1274 fp->f_data = (void *)vp;
1275 FILE_SET_MATURE(fp);
1276 vp = NULL;
1277 }
1278 error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
1279 SCARG(uap, pid), fp);
1280 done:
1281 if (vp != NULL)
1282 (void) vn_close(vp, FWRITE, l->l_cred, l);
1283 if (fp != NULL) {
1284 FILE_UNUSE(fp, l); /* release file */
1285 fdrelease(l, fd); /* release fd table slot */
1286 }
1287 return (error);
1288 }
1289
1290 int
1291 ktrops(lwp_t *curl, struct proc *p, int ops, int facs,
1292 struct ktr_desc *ktd)
1293 {
1294 int vers = ops & KTRFAC_VER_MASK;
1295 int error = 0;
1296
1297 mutex_enter(&p->p_mutex);
1298 mutex_enter(&ktrace_lock);
1299
1300 if (!ktrcanset(curl, p))
1301 goto out;
1302
1303 switch (vers) {
1304 case KTRFACv0:
1305 case KTRFACv1:
1306 break;
1307 default:
1308 error = EINVAL;
1309 goto out;
1310 }
1311
1312 if (KTROP(ops) == KTROP_SET) {
1313 if (p->p_tracep != ktd) {
1314 /*
1315 * if trace file already in use, relinquish
1316 */
1317 ktrderef(p);
1318 p->p_tracep = ktd;
1319 ktradref(p);
1320 }
1321 p->p_traceflag |= facs;
1322 if (kauth_authorize_generic(curl->l_cred,
1323 KAUTH_GENERIC_ISSUSER, NULL) == 0)
1324 p->p_traceflag |= KTRFAC_ROOT;
1325 } else {
1326 /* KTROP_CLEAR */
1327 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
1328 /* no more tracing */
1329 ktrderef(p);
1330 }
1331 }
1332
1333 if (p->p_traceflag)
1334 p->p_traceflag |= vers;
1335 /*
1336 * Emit an emulation record, every time there is a ktrace
1337 * change/attach request.
1338 */
1339 if (KTRPOINT(p, KTR_EMUL))
1340 p->p_traceflag |= KTRFAC_TRC_EMUL;
1341 #ifdef __HAVE_SYSCALL_INTERN
1342 (*p->p_emul->e_syscall_intern)(p);
1343 #endif
1344
1345 out:
1346 mutex_exit(&ktrace_lock);
1347 mutex_exit(&p->p_mutex);
1348
1349 return (1);
1350 }
1351
1352 int
1353 ktrsetchildren(lwp_t *curl, struct proc *top, int ops, int facs,
1354 struct ktr_desc *ktd)
1355 {
1356 struct proc *p;
1357 int ret = 0;
1358
1359 KASSERT(mutex_owned(&proclist_lock));
1360
1361 p = top;
1362 for (;;) {
1363 ret |= ktrops(curl, p, ops, facs, ktd);
1364 /*
1365 * If this process has children, descend to them next,
1366 * otherwise do any siblings, and if done with this level,
1367 * follow back up the tree (but not past top).
1368 */
1369 if (LIST_FIRST(&p->p_children) != NULL) {
1370 p = LIST_FIRST(&p->p_children);
1371 continue;
1372 }
1373 for (;;) {
1374 if (p == top)
1375 return (ret);
1376 if (LIST_NEXT(p, p_sibling) != NULL) {
1377 p = LIST_NEXT(p, p_sibling);
1378 break;
1379 }
1380 p = p->p_pptr;
1381 }
1382 }
1383 /*NOTREACHED*/
1384 }
1385
1386 void
1387 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
1388 {
1389 struct uio auio;
1390 struct iovec aiov[64], *iov;
1391 struct ktrace_entry *top = kte;
1392 struct ktr_header *kth;
1393 struct file *fp = ktd->ktd_fp;
1394 int error;
1395 next:
1396 auio.uio_iov = iov = &aiov[0];
1397 auio.uio_offset = 0;
1398 auio.uio_rw = UIO_WRITE;
1399 auio.uio_resid = 0;
1400 auio.uio_iovcnt = 0;
1401 UIO_SETUP_SYSSPACE(&auio);
1402 do {
1403 kth = &kte->kte_kth;
1404
1405 if (kth->ktr_version == 0) {
1406 /*
1407 * Convert back to the old format fields
1408 */
1409 TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time);
1410 kth->ktr_unused = NULL;
1411 }
1412 iov->iov_base = (void *)kth;
1413 iov++->iov_len = sizeof(struct ktr_header);
1414 auio.uio_resid += sizeof(struct ktr_header);
1415 auio.uio_iovcnt++;
1416 if (kth->ktr_len > 0) {
1417 iov->iov_base = kte->kte_buf;
1418 iov++->iov_len = kth->ktr_len;
1419 auio.uio_resid += kth->ktr_len;
1420 auio.uio_iovcnt++;
1421 }
1422 } while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
1423 auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
1424
1425 again:
1426 simple_lock(&fp->f_slock);
1427 FILE_USE(fp);
1428 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
1429 fp->f_cred, FOF_UPDATE_OFFSET);
1430 FILE_UNUSE(fp, NULL);
1431 switch (error) {
1432
1433 case 0:
1434 if (auio.uio_resid > 0)
1435 goto again;
1436 if (kte != NULL)
1437 goto next;
1438 break;
1439
1440 case EWOULDBLOCK:
1441 kpause("ktrzzz", false, 1, NULL);
1442 goto again;
1443
1444 default:
1445 /*
1446 * If error encountered, give up tracing on this
1447 * vnode. Don't report EPIPE as this can easily
1448 * happen with fktrace()/ktruss.
1449 */
1450 #ifndef DEBUG
1451 if (error != EPIPE)
1452 #endif
1453 log(LOG_NOTICE,
1454 "ktrace write failed, errno %d, tracing stopped\n",
1455 error);
1456 (void)ktrderefall(ktd, 0);
1457 }
1458
1459 while ((kte = top) != NULL) {
1460 top = TAILQ_NEXT(top, kte_list);
1461 ktefree(kte);
1462 }
1463 }
1464
1465 void
1466 ktrace_thread(void *arg)
1467 {
1468 struct ktr_desc *ktd = arg;
1469 struct file *fp = ktd->ktd_fp;
1470 struct ktrace_entry *kte;
1471 int ktrerr, errcnt;
1472
1473 mutex_enter(&ktrace_lock);
1474 for (;;) {
1475 kte = TAILQ_FIRST(&ktd->ktd_queue);
1476 if (kte == NULL) {
1477 if (ktd->ktd_flags & KTDF_WAIT) {
1478 ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING);
1479 cv_broadcast(&ktd->ktd_sync_cv);
1480 }
1481 if (ktd->ktd_ref == 0)
1482 break;
1483 cv_wait(&ktd->ktd_cv, &ktrace_lock);
1484 continue;
1485 }
1486 TAILQ_INIT(&ktd->ktd_queue);
1487 ktd->ktd_qcount = 0;
1488 ktrerr = ktd->ktd_error;
1489 errcnt = ktd->ktd_errcnt;
1490 ktd->ktd_error = ktd->ktd_errcnt = 0;
1491 mutex_exit(&ktrace_lock);
1492
1493 if (ktrerr) {
1494 log(LOG_NOTICE,
1495 "ktrace failed, fp %p, error 0x%x, total %d\n",
1496 fp, ktrerr, errcnt);
1497 }
1498 ktrwrite(ktd, kte);
1499 mutex_enter(&ktrace_lock);
1500 }
1501
1502 TAILQ_REMOVE(&ktdq, ktd, ktd_list);
1503 mutex_exit(&ktrace_lock);
1504
1505 simple_lock(&fp->f_slock);
1506 FILE_USE(fp);
1507
1508 /*
1509 * ktrace file descriptor can't be watched (are not visible to
1510 * userspace), so no kqueue stuff here
1511 * XXX: The above comment is wrong, because the fktrace file
1512 * descriptor is available in userland.
1513 */
1514 closef(fp, NULL);
1515
1516 callout_stop(&ktd->ktd_wakch);
1517 callout_destroy(&ktd->ktd_wakch);
1518 kmem_free(ktd, sizeof(*ktd));
1519
1520 kthread_exit(0);
1521 }
1522
1523 /*
1524 * Return true if caller has permission to set the ktracing state
1525 * of target. Essentially, the target can't possess any
1526 * more permissions than the caller. KTRFAC_ROOT signifies that
1527 * root previously set the tracing status on the target process, and
1528 * so, only root may further change it.
1529 *
1530 * TODO: check groups. use caller effective gid.
1531 */
1532 int
1533 ktrcanset(lwp_t *calll, struct proc *targetp)
1534 {
1535 KASSERT(mutex_owned(&targetp->p_mutex));
1536 KASSERT(mutex_owned(&ktrace_lock));
1537
1538 if (kauth_authorize_process(calll->l_cred, KAUTH_PROCESS_CANKTRACE,
1539 targetp, NULL, NULL, NULL) == 0)
1540 return (1);
1541
1542 return (0);
1543 }
1544
1545 /*
1546 * Put user defined entry to ktrace records.
1547 */
1548 int
1549 sys_utrace(lwp_t *l, void *v, register_t *retval)
1550 {
1551 struct sys_utrace_args /* {
1552 syscallarg(const char *) label;
1553 syscallarg(void *) addr;
1554 syscallarg(size_t) len;
1555 } */ *uap = v;
1556
1557 return ktruser(SCARG(uap, label), SCARG(uap, addr),
1558 SCARG(uap, len), 1);
1559 }
1560