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