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