kern_ktrace.c revision 1.119.2.7 1 /* $NetBSD: kern_ktrace.c,v 1.119.2.7 2007/06/08 14:17:19 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.7 2007/06/08 14:17:19 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 void
563 ktrnamei(struct lwp *l, const char *path, size_t pathlen)
564 {
565 ktrkmem(l, KTR_NAMEI, path, pathlen);
566 }
567
568 void
569 ktrnamei2(struct lwp *l, const char *eroot, size_t erootlen,
570 const char *path, size_t pathlen)
571 {
572 struct ktrace_entry *kte;
573 void *buf;
574
575 if (ktealloc(&kte, &buf, l, KTR_NAMEI, erootlen + pathlen))
576 return;
577 memcpy(buf, eroot, erootlen);
578 buf = (char *)buf + erootlen;
579 memcpy(buf, path, pathlen);
580 ktraddentry(l, kte, KTA_WAITOK);
581 }
582
583 void
584 ktremul(struct lwp *l)
585 {
586 const char *emul = l->l_proc->p_emul->e_name;
587
588 ktrkmem(l, KTR_EMUL, emul, strlen(emul));
589 }
590
591 void
592 ktrkmem(struct lwp *l, int type, const void *bf, size_t len)
593 {
594 struct ktrace_entry *kte;
595 void *buf;
596
597 if (ktealloc(&kte, &buf, l, type, len))
598 return;
599 memcpy(buf, bf, len);
600 ktraddentry(l, kte, KTA_WAITOK);
601 }
602
603 void
604 ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov,
605 int len, int error)
606 {
607 struct ktrace_entry *kte;
608 struct ktr_genio *ktp;
609 int resid = len, cnt;
610 void *cp;
611 int buflen;
612
613 if (error)
614 return;
615
616 next:
617 buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
618
619 if (ktealloc(&kte, (void *)&ktp, l, KTR_GENIO, buflen))
620 return;
621
622 ktp->ktr_fd = fd;
623 ktp->ktr_rw = rw;
624
625 cp = (void *)(ktp + 1);
626 buflen -= sizeof(struct ktr_genio);
627 kte->kte_kth.ktr_len = sizeof(struct ktr_genio);
628
629 while (buflen > 0) {
630 cnt = min(iov->iov_len, buflen);
631 if (copyin(iov->iov_base, cp, cnt) != 0)
632 goto out;
633 kte->kte_kth.ktr_len += cnt;
634 buflen -= cnt;
635 resid -= cnt;
636 iov->iov_len -= cnt;
637 if (iov->iov_len == 0)
638 iov++;
639 else
640 iov->iov_base = (char *)iov->iov_base + cnt;
641 }
642
643 /*
644 * Don't push so many entry at once. It will cause kmem map
645 * shortage.
646 */
647 ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
648 if (resid > 0) {
649 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) {
650 (void)ktrenter(l);
651 preempt();
652 ktrexit(l);
653 }
654
655 goto next;
656 }
657
658 return;
659
660 out:
661 ktefree(kte);
662 ktrexit(l);
663 }
664
665 void
666 ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask,
667 const ksiginfo_t *ksi)
668 {
669 struct ktrace_entry *kte;
670 struct {
671 struct ktr_psig kp;
672 siginfo_t si;
673 } *kbuf;
674
675 if (ktealloc(&kte, (void *)&kbuf, l, KTR_PSIG, sizeof(*kbuf)))
676 return;
677
678 kbuf->kp.signo = (char)sig;
679 kbuf->kp.action = action;
680 kbuf->kp.mask = *mask;
681
682 if (ksi) {
683 kbuf->kp.code = KSI_TRAPCODE(ksi);
684 (void)memset(&kbuf->si, 0, sizeof(kbuf->si));
685 kbuf->si._info = ksi->ksi_info;
686 kte->kte_kth.ktr_len = sizeof(*kbuf);
687 } else {
688 kbuf->kp.code = 0;
689 kte->kte_kth.ktr_len = sizeof(struct ktr_psig);
690 }
691
692 ktraddentry(l, kte, KTA_WAITOK);
693 }
694
695 void
696 ktrcsw(struct lwp *l, int out, int user)
697 {
698 struct proc *p = l->l_proc;
699 struct ktrace_entry *kte;
700 struct ktr_csw *kc;
701
702 /*
703 * Don't record context switches resulting from blocking on
704 * locks; it's too easy to get duff results.
705 */
706 if (l->l_syncobj == &mutex_syncobj || l->l_syncobj == &rw_syncobj)
707 return;
708
709 /*
710 * We can't sleep if we're already going to sleep (if original
711 * condition is met during sleep, we hang up).
712 *
713 * XXX This is not ideal: it would be better to maintain a pool
714 * of ktes and actually push this to the kthread when context
715 * switch happens, however given the points where we are called
716 * from that is difficult to do.
717 */
718 if (out) {
719 if (ktrenter(l))
720 return;
721
722 switch (KTRFAC_VERSION(p->p_traceflag)) {
723 case 0:
724 /* This is the original format */
725 microtime(&l->l_ktrcsw.tv);
726 l->l_pflag |= LP_KTRCSW;
727 break;
728 case 1:
729 nanotime(&l->l_ktrcsw.ts);
730 l->l_pflag |= LP_KTRCSW;
731 break;
732 default:
733 break;
734 }
735
736 if (user)
737 l->l_pflag |= LP_KTRCSWUSER;
738 else
739 l->l_pflag &= ~LP_KTRCSWUSER;
740
741 ktrexit(l);
742 return;
743 }
744
745 /*
746 * On the way back in, we need to record twice: once for entry, and
747 * once for exit.
748 */
749 if ((l->l_pflag & LP_KTRCSW) != 0) {
750 l->l_pflag &= ~LP_KTRCSW;
751
752 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
753 return;
754
755 kc->out = 1;
756 kc->user = ((l->l_pflag & LP_KTRCSWUSER) != 0);
757
758 switch (KTRFAC_VERSION(p->p_traceflag)) {
759 case 0:
760 /* This is the original format */
761 memcpy(&kte->kte_kth.ktr_tv, &l->l_ktrcsw.tv,
762 sizeof(kte->kte_kth.ktr_tv));
763 break;
764 case 1:
765 memcpy(&kte->kte_kth.ktr_time, &l->l_ktrcsw.ts,
766 sizeof(kte->kte_kth.ktr_time));
767 break;
768 default:
769 break;
770 }
771
772 ktraddentry(l, kte, KTA_WAITOK);
773 }
774
775 if (ktealloc(&kte, (void *)&kc, l, KTR_CSW, sizeof(*kc)))
776 return;
777
778 kc->out = 0;
779 kc->user = user;
780
781 ktraddentry(l, kte, KTA_WAITOK);
782 }
783
784 int
785 ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr)
786 {
787 struct ktrace_entry *kte;
788 struct ktr_user *ktp;
789 void *user_dta;
790 int error;
791
792 if (len > KTR_USER_MAXLEN)
793 return ENOSPC;
794
795 error = ktealloc(&kte, (void *)&ktp, l, KTR_USER, sizeof(*ktp) + len);
796 if (error != 0)
797 return error;
798
799 if (ustr) {
800 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
801 ktp->ktr_id[0] = '\0';
802 } else
803 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
804 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
805
806 user_dta = (void *)(ktp + 1);
807 if ((error = copyin(addr, (void *)user_dta, len)) != 0)
808 len = 0;
809
810 ktraddentry(l, kte, KTA_WAITOK);
811 return error;
812 }
813
814 void
815 ktrmmsg(struct lwp *l, const void *msgh, size_t size)
816 {
817 ktrkmem(l, KTR_MMSG, msgh, size);
818 }
819
820 void
821 ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr)
822 {
823 struct ktrace_entry *kte;
824 struct ktr_mool *kp;
825 struct ktr_mool *bf;
826
827 if (ktealloc(&kte, (void *)&kp, l, KTR_MOOL, size + sizeof(*kp)))
828 return;
829
830 kp->uaddr = uaddr;
831 kp->size = size;
832 bf = kp + 1; /* Skip uaddr and size */
833 (void)memcpy(bf, kaddr, size);
834
835 ktraddentry(l, kte, KTA_WAITOK);
836 }
837
838 void
839 ktrmib(struct lwp *l, const int *name, u_int namelen)
840 {
841 struct ktrace_entry *kte;
842 int *namep;
843 size_t size;
844
845 size = namelen * sizeof(*name);
846
847 if (ktealloc(&kte, (void *)&namep, l, KTR_MIB, size))
848 return;
849
850 (void)memcpy(namep, name, namelen * sizeof(*name));
851
852 ktraddentry(l, kte, KTA_WAITOK);
853 }
854
855 /* Interface and common routines */
856
857 int
858 ktrace_common(struct lwp *curl, int ops, int facs, int pid, struct file *fp)
859 {
860 struct proc *curp;
861 struct proc *p;
862 struct pgrp *pg;
863 struct ktr_desc *ktd = NULL;
864 int ret = 0;
865 int error = 0;
866 int descend;
867
868 curp = curl->l_proc;
869 descend = ops & KTRFLAG_DESCEND;
870 facs = facs & ~((unsigned) KTRFAC_ROOT);
871
872 (void)ktrenter(curl);
873
874 switch (KTROP(ops)) {
875
876 case KTROP_CLEARFILE:
877 /*
878 * Clear all uses of the tracefile
879 */
880 mutex_enter(&ktrace_mutex);
881 ktd = ktd_lookup(fp);
882 mutex_exit(&ktrace_mutex);
883 if (ktd == NULL)
884 goto done;
885 error = ktrderefall(ktd, 1);
886 goto done;
887
888 case KTROP_SET:
889 mutex_enter(&ktrace_mutex);
890 ktd = ktd_lookup(fp);
891 mutex_exit(&ktrace_mutex);
892 if (ktd == NULL) {
893 ktd = kmem_alloc(sizeof(*ktd), KM_SLEEP);
894 TAILQ_INIT(&ktd->ktd_queue);
895 callout_init(&ktd->ktd_wakch);
896 cv_init(&ktd->ktd_cv, "ktrwait");
897 cv_init(&ktd->ktd_sync_cv, "ktrsync");
898 ktd->ktd_flags = ktd->ktd_qcount =
899 ktd->ktd_error = ktd->ktd_errcnt = 0;
900 ktd->ktd_ref = 1;
901 ktd->ktd_delayqcnt = ktd_delayqcnt;
902 ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
903 ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
904 /*
905 * XXX: not correct. needs an way to detect
906 * whether ktruss or ktrace.
907 */
908 if (fp->f_type == DTYPE_PIPE)
909 ktd->ktd_flags |= KTDF_INTERACTIVE;
910
911 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
912 ktrace_thread, ktd, &ktd->ktd_lwp, "ktrace");
913 if (error != 0) {
914 kmem_free(ktd, sizeof(*ktd));
915 goto done;
916 }
917
918 mutex_enter(&fp->f_lock);
919 fp->f_count++;
920 mutex_exit(&fp->f_lock);
921 ktd->ktd_fp = fp;
922
923 mutex_enter(&ktrace_mutex);
924 if (ktd_lookup(fp) != NULL) {
925 ktdrel(ktd);
926 ktd = NULL;
927 } else
928 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
929 if (ktd == NULL)
930 cv_wait(&lbolt, &ktrace_mutex);
931 mutex_exit(&ktrace_mutex);
932 if (ktd == NULL)
933 goto done;
934 }
935 break;
936
937 case KTROP_CLEAR:
938 break;
939 }
940
941 /*
942 * need something to (un)trace (XXX - why is this here?)
943 */
944 if (!facs) {
945 error = EINVAL;
946 goto done;
947 }
948
949 /*
950 * do it
951 */
952 mutex_enter(&proclist_lock);
953 if (pid < 0) {
954 /*
955 * by process group
956 */
957 pg = pg_find(-pid, PFIND_LOCKED);
958 if (pg == NULL)
959 error = ESRCH;
960 else {
961 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
962 if (descend)
963 ret |= ktrsetchildren(curl, p, ops,
964 facs, ktd);
965 else
966 ret |= ktrops(curl, p, ops, facs,
967 ktd);
968 }
969 }
970
971 } else {
972 /*
973 * by pid
974 */
975 p = p_find(pid, PFIND_LOCKED);
976 if (p == NULL)
977 error = ESRCH;
978 else if (descend)
979 ret |= ktrsetchildren(curl, p, ops, facs, ktd);
980 else
981 ret |= ktrops(curl, p, ops, facs, ktd);
982 }
983 mutex_exit(&proclist_lock);
984 if (error == 0 && !ret)
985 error = EPERM;
986 done:
987 if (ktd != NULL) {
988 mutex_enter(&ktrace_mutex);
989 if (error != 0) {
990 /*
991 * Wakeup the thread so that it can be die if we
992 * can't trace any process.
993 */
994 ktd_wakeup(ktd);
995 }
996 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE)
997 ktdrel(ktd);
998 mutex_exit(&ktrace_mutex);
999 }
1000 ktrexit(curl);
1001 return (error);
1002 }
1003
1004 /*
1005 * fktrace system call
1006 */
1007 /* ARGSUSED */
1008 int
1009 sys_fktrace(struct lwp *l, void *v, register_t *retval)
1010 {
1011 struct sys_fktrace_args /* {
1012 syscallarg(int) fd;
1013 syscallarg(int) ops;
1014 syscallarg(int) facs;
1015 syscallarg(int) pid;
1016 } */ *uap = v;
1017 struct file *fp = NULL;
1018 struct filedesc *fdp = l->l_proc->p_fd;
1019 int error;
1020
1021 fdp = l->l_proc->p_fd;
1022 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1023 return (EBADF);
1024
1025 FILE_USE(fp);
1026
1027 if ((fp->f_flag & FWRITE) == 0)
1028 error = EBADF;
1029 else
1030 error = ktrace_common(l, SCARG(uap, ops),
1031 SCARG(uap, facs), SCARG(uap, pid), fp);
1032
1033 FILE_UNUSE(fp, l);
1034
1035 return error;
1036 }
1037
1038 /*
1039 * ktrace system call
1040 */
1041 /* ARGSUSED */
1042 int
1043 sys_ktrace(struct lwp *l, void *v, register_t *retval)
1044 {
1045 struct sys_ktrace_args /* {
1046 syscallarg(const char *) fname;
1047 syscallarg(int) ops;
1048 syscallarg(int) facs;
1049 syscallarg(int) pid;
1050 } */ *uap = v;
1051 struct vnode *vp = NULL;
1052 struct file *fp = NULL;
1053 struct nameidata nd;
1054 int error = 0;
1055 int fd;
1056
1057 if (ktrenter(l))
1058 return EAGAIN;
1059
1060 if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) {
1061 /*
1062 * an operation which requires a file argument.
1063 */
1064 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
1065 l);
1066 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
1067 ktrexit(l);
1068 return (error);
1069 }
1070 vp = nd.ni_vp;
1071 VOP_UNLOCK(vp, 0);
1072 if (vp->v_type != VREG) {
1073 (void) vn_close(vp, FREAD|FWRITE, l->l_cred, l);
1074 ktrexit(l);
1075 return (EACCES);
1076 }
1077 /*
1078 * XXX This uses up a file descriptor slot in the
1079 * tracing process for the duration of this syscall.
1080 * This is not expected to be a problem. If
1081 * falloc(NULL, ...) DTRT we could skip that part, but
1082 * that would require changing its interface to allow
1083 * the caller to pass in a ucred..
1084 *
1085 * This will FILE_USE the fp it returns, if any.
1086 * Keep it in use until we return.
1087 */
1088 if ((error = falloc(l, &fp, &fd)) != 0)
1089 goto done;
1090
1091 fp->f_flag = FWRITE;
1092 fp->f_type = DTYPE_VNODE;
1093 fp->f_ops = &vnops;
1094 fp->f_data = (void *)vp;
1095 FILE_SET_MATURE(fp);
1096 vp = NULL;
1097 }
1098 error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
1099 SCARG(uap, pid), fp);
1100 done:
1101 if (vp != NULL)
1102 (void) vn_close(vp, FWRITE, l->l_cred, l);
1103 if (fp != NULL) {
1104 FILE_UNUSE(fp, l); /* release file */
1105 fdrelease(l, fd); /* release fd table slot */
1106 }
1107 return (error);
1108 }
1109
1110 int
1111 ktrops(struct lwp *curl, struct proc *p, int ops, int facs,
1112 struct ktr_desc *ktd)
1113 {
1114 int vers = ops & KTRFAC_VER_MASK;
1115 int error = 0;
1116
1117 mutex_enter(&p->p_mutex);
1118 mutex_enter(&ktrace_mutex);
1119
1120 if (!ktrcanset(curl, p))
1121 goto out;
1122
1123 switch (vers) {
1124 case KTRFACv0:
1125 case KTRFACv1:
1126 break;
1127 default:
1128 error = EINVAL;
1129 goto out;
1130 }
1131
1132 if (KTROP(ops) == KTROP_SET) {
1133 if (p->p_tracep != ktd) {
1134 /*
1135 * if trace file already in use, relinquish
1136 */
1137 ktrderef(p);
1138 p->p_tracep = ktd;
1139 ktradref(p);
1140 }
1141 p->p_traceflag |= facs;
1142 if (kauth_authorize_generic(curl->l_cred,
1143 KAUTH_GENERIC_ISSUSER, NULL) == 0)
1144 p->p_traceflag |= KTRFAC_ROOT;
1145 } else {
1146 /* KTROP_CLEAR */
1147 if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
1148 /* no more tracing */
1149 ktrderef(p);
1150 }
1151 }
1152
1153 if (p->p_traceflag)
1154 p->p_traceflag |= vers;
1155 /*
1156 * Emit an emulation record, every time there is a ktrace
1157 * change/attach request.
1158 */
1159 if (KTRPOINT(p, KTR_EMUL))
1160 p->p_traceflag |= KTRFAC_TRC_EMUL;
1161 #ifdef __HAVE_SYSCALL_INTERN
1162 (*p->p_emul->e_syscall_intern)(p);
1163 #endif
1164
1165 out:
1166 mutex_exit(&ktrace_mutex);
1167 mutex_exit(&p->p_mutex);
1168
1169 return (1);
1170 }
1171
1172 int
1173 ktrsetchildren(struct lwp *curl, struct proc *top, int ops, int facs,
1174 struct ktr_desc *ktd)
1175 {
1176 struct proc *p;
1177 int ret = 0;
1178
1179 KASSERT(mutex_owned(&proclist_lock));
1180
1181 p = top;
1182 for (;;) {
1183 ret |= ktrops(curl, p, ops, facs, ktd);
1184 /*
1185 * If this process has children, descend to them next,
1186 * otherwise do any siblings, and if done with this level,
1187 * follow back up the tree (but not past top).
1188 */
1189 if (LIST_FIRST(&p->p_children) != NULL) {
1190 p = LIST_FIRST(&p->p_children);
1191 continue;
1192 }
1193 for (;;) {
1194 if (p == top)
1195 return (ret);
1196 if (LIST_NEXT(p, p_sibling) != NULL) {
1197 p = LIST_NEXT(p, p_sibling);
1198 break;
1199 }
1200 p = p->p_pptr;
1201 }
1202 }
1203 /*NOTREACHED*/
1204 }
1205
1206 void
1207 ktrwrite(struct ktr_desc *ktd, struct ktrace_entry *kte)
1208 {
1209 struct uio auio;
1210 struct iovec aiov[64], *iov;
1211 struct ktrace_entry *top = kte;
1212 struct ktr_header *kth;
1213 struct file *fp = ktd->ktd_fp;
1214 int error;
1215 next:
1216 auio.uio_iov = iov = &aiov[0];
1217 auio.uio_offset = 0;
1218 auio.uio_rw = UIO_WRITE;
1219 auio.uio_resid = 0;
1220 auio.uio_iovcnt = 0;
1221 UIO_SETUP_SYSSPACE(&auio);
1222 do {
1223 kth = &kte->kte_kth;
1224
1225 if (kth->ktr_version == 0) {
1226 /*
1227 * Convert back to the old format fields
1228 */
1229 TIMESPEC_TO_TIMEVAL(&kth->ktr_tv, &kth->ktr_time);
1230 kth->ktr_unused = NULL;
1231 }
1232 iov->iov_base = (void *)kth;
1233 iov++->iov_len = sizeof(struct ktr_header);
1234 auio.uio_resid += sizeof(struct ktr_header);
1235 auio.uio_iovcnt++;
1236 if (kth->ktr_len > 0) {
1237 iov->iov_base = kte->kte_buf;
1238 iov++->iov_len = kth->ktr_len;
1239 auio.uio_resid += kth->ktr_len;
1240 auio.uio_iovcnt++;
1241 }
1242 } while ((kte = TAILQ_NEXT(kte, kte_list)) != NULL &&
1243 auio.uio_iovcnt < sizeof(aiov) / sizeof(aiov[0]) - 1);
1244
1245 again:
1246 mutex_enter(&fp->f_lock);
1247 FILE_USE(fp);
1248 error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
1249 fp->f_cred, FOF_UPDATE_OFFSET);
1250 FILE_UNUSE(fp, NULL);
1251 switch (error) {
1252
1253 case 0:
1254 if (auio.uio_resid > 0)
1255 goto again;
1256 if (kte != NULL)
1257 goto next;
1258 break;
1259
1260 case EWOULDBLOCK:
1261 kpause("ktrzzz", false, 1, NULL);
1262 goto again;
1263
1264 default:
1265 /*
1266 * If error encountered, give up tracing on this
1267 * vnode. Don't report EPIPE as this can easily
1268 * happen with fktrace()/ktruss.
1269 */
1270 #ifndef DEBUG
1271 if (error != EPIPE)
1272 #endif
1273 log(LOG_NOTICE,
1274 "ktrace write failed, errno %d, tracing stopped\n",
1275 error);
1276 (void)ktrderefall(ktd, 0);
1277 }
1278
1279 while ((kte = top) != NULL) {
1280 top = TAILQ_NEXT(top, kte_list);
1281 ktefree(kte);
1282 }
1283 }
1284
1285 void
1286 ktrace_thread(void *arg)
1287 {
1288 struct ktr_desc *ktd = arg;
1289 struct file *fp = ktd->ktd_fp;
1290 struct ktrace_entry *kte;
1291 int ktrerr, errcnt;
1292
1293 mutex_enter(&ktrace_mutex);
1294 for (;;) {
1295 kte = TAILQ_FIRST(&ktd->ktd_queue);
1296 if (kte == NULL) {
1297 if (ktd->ktd_flags & KTDF_WAIT) {
1298 ktd->ktd_flags &= ~(KTDF_WAIT | KTDF_BLOCKING);
1299 cv_broadcast(&ktd->ktd_sync_cv);
1300 }
1301 if (ktd->ktd_ref == 0)
1302 break;
1303 cv_wait(&ktd->ktd_cv, &ktrace_mutex);
1304 continue;
1305 }
1306 TAILQ_INIT(&ktd->ktd_queue);
1307 ktd->ktd_qcount = 0;
1308 ktrerr = ktd->ktd_error;
1309 errcnt = ktd->ktd_errcnt;
1310 ktd->ktd_error = ktd->ktd_errcnt = 0;
1311 mutex_exit(&ktrace_mutex);
1312
1313 if (ktrerr) {
1314 log(LOG_NOTICE,
1315 "ktrace failed, fp %p, error 0x%x, total %d\n",
1316 fp, ktrerr, errcnt);
1317 }
1318 ktrwrite(ktd, kte);
1319 mutex_enter(&ktrace_mutex);
1320 }
1321
1322 TAILQ_REMOVE(&ktdq, ktd, ktd_list);
1323 mutex_exit(&ktrace_mutex);
1324
1325 mutex_enter(&fp->f_lock);
1326 FILE_USE(fp);
1327
1328 /*
1329 * ktrace file descriptor can't be watched (are not visible to
1330 * userspace), so no kqueue stuff here
1331 * XXX: The above comment is wrong, because the fktrace file
1332 * descriptor is available in userland.
1333 */
1334 closef(fp, NULL);
1335
1336 callout_stop(&ktd->ktd_wakch);
1337 kmem_free(ktd, sizeof(*ktd));
1338
1339 kthread_exit(0);
1340 }
1341
1342 /*
1343 * Return true if caller has permission to set the ktracing state
1344 * of target. Essentially, the target can't possess any
1345 * more permissions than the caller. KTRFAC_ROOT signifies that
1346 * root previously set the tracing status on the target process, and
1347 * so, only root may further change it.
1348 *
1349 * TODO: check groups. use caller effective gid.
1350 */
1351 int
1352 ktrcanset(struct lwp *calll, struct proc *targetp)
1353 {
1354 KASSERT(mutex_owned(&targetp->p_mutex));
1355 KASSERT(mutex_owned(&ktrace_mutex));
1356
1357 if (kauth_authorize_process(calll->l_cred, KAUTH_PROCESS_CANKTRACE,
1358 targetp, NULL, NULL, NULL) == 0)
1359 return (1);
1360
1361 return (0);
1362 }
1363 #endif /* KTRACE */
1364
1365 /*
1366 * Put user defined entry to ktrace records.
1367 */
1368 int
1369 sys_utrace(struct lwp *l, void *v, register_t *retval)
1370 {
1371 #ifdef KTRACE
1372 struct sys_utrace_args /* {
1373 syscallarg(const char *) label;
1374 syscallarg(void *) addr;
1375 syscallarg(size_t) len;
1376 } */ *uap = v;
1377 struct proc *p = l->l_proc;
1378
1379 if (!KTRPOINT(p, KTR_USER))
1380 return (0);
1381
1382 return ktruser(l, SCARG(uap, label), SCARG(uap, addr),
1383 SCARG(uap, len), 1);
1384 #else /* !KTRACE */
1385 return ENOSYS;
1386 #endif /* KTRACE */
1387 }
1388