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