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