kern_ktrace.c revision 1.100.6.2 1 /* $NetBSD: kern_ktrace.c,v 1.100.6.2 2006/04/22 11:39:58 simonb 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.100.6.2 2006/04/22 11:39:58 simonb 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
55 #include <sys/mount.h>
56 #include <sys/sa.h>
57 #include <sys/syscallargs.h>
58
59 #ifdef KTRACE
60
61 /*
62 * XXX:
63 * - need better error reporting?
64 * - p->p_tracep access lock. lock p_lock, lock ktd if !NULL, inc ref.
65 * - userland utility to sort ktrace.out by timestamp.
66 * - keep minimum information in ktrace_entry when rest of alloc failed.
67 * - enlarge ktrace_entry so that small entry won't require additional
68 * alloc?
69 * - per trace control of configurable parameters.
70 */
71
72 struct ktrace_entry {
73 TAILQ_ENTRY(ktrace_entry) kte_list;
74 struct ktr_header kte_kth;
75 void *kte_buf;
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 proc *ktd_proc; /* our kernel thread */
101 TAILQ_HEAD(, ktrace_entry) ktd_queue;
102 struct callout ktd_wakch; /* delayed wakeup */
103 struct simplelock ktd_slock;
104 };
105
106 static void ktrinitheader(struct ktr_header *, struct lwp *, int);
107 static void ktrwrite(struct ktr_desc *, struct ktrace_entry *);
108 static int ktrace_common(struct proc *, int, int, int, struct file *);
109 static int ktrops(struct proc *, struct proc *, int, int,
110 struct ktr_desc *);
111 static int ktrsetchildren(struct proc *, struct proc *, int, int,
112 struct ktr_desc *);
113 static int ktrcanset(struct proc *, struct proc *);
114 static int ktrsamefile(struct file *, struct file *);
115
116 static struct ktr_desc *
117 ktd_lookup(struct file *);
118 static void ktdrel(struct ktr_desc *);
119 static void ktdref(struct ktr_desc *);
120 static void ktraddentry(struct lwp *, struct ktrace_entry *, int);
121 /* Flags for ktraddentry (3rd arg) */
122 #define KTA_NOWAIT 0x0000
123 #define KTA_WAITOK 0x0001
124 #define KTA_LARGE 0x0002
125 static void ktefree(struct ktrace_entry *);
126 static void ktd_logerrl(struct ktr_desc *, int);
127 static void ktd_logerr(struct proc *, int);
128 static void ktrace_thread(void *);
129
130 /*
131 * Default vaules.
132 */
133 #define KTD_MAXENTRY 1000 /* XXX: tune */
134 #define KTD_TIMEOUT 5 /* XXX: tune */
135 #define KTD_DELAYQCNT 100 /* XXX: tune */
136 #define KTD_WAKEDELAY 5000 /* XXX: tune */
137 #define KTD_INTRWAKDL 100 /* XXX: tune */
138
139 /*
140 * Patchable variables.
141 */
142 int ktd_maxentry = KTD_MAXENTRY; /* max # of entry in the queue */
143 int ktd_timeout = KTD_TIMEOUT; /* timeout in seconds */
144 int ktd_delayqcnt = KTD_DELAYQCNT; /* # of entry allowed to delay */
145 int ktd_wakedelay = KTD_WAKEDELAY; /* delay of wakeup in *ms* */
146 int ktd_intrwakdl = KTD_INTRWAKDL; /* ditto, but when interactive */
147
148 static struct simplelock ktdq_slock = SIMPLELOCK_INITIALIZER;
149 static TAILQ_HEAD(, ktr_desc) ktdq = TAILQ_HEAD_INITIALIZER(ktdq);
150
151 MALLOC_DEFINE(M_KTRACE, "ktrace", "ktrace data buffer");
152 POOL_INIT(kte_pool, sizeof(struct ktrace_entry), 0, 0, 0,
153 "ktepl", &pool_allocator_nointr);
154
155 static inline void
156 ktd_wakeup(struct ktr_desc *ktd)
157 {
158
159 callout_stop(&ktd->ktd_wakch);
160 wakeup(ktd);
161 }
162
163 static void
164 ktd_logerrl(struct ktr_desc *ktd, int error)
165 {
166
167 ktd->ktd_error |= error;
168 ktd->ktd_errcnt++;
169 }
170
171 static void
172 ktd_logerr(struct proc *p, int error)
173 {
174 struct ktr_desc *ktd = p->p_tracep;
175
176 if (ktd == NULL)
177 return;
178
179 simple_lock(&ktd->ktd_slock);
180 ktd_logerrl(ktd, error);
181 simple_unlock(&ktd->ktd_slock);
182 }
183
184 /*
185 * Release a reference. Called with ktd_slock held.
186 */
187 void
188 ktdrel(struct ktr_desc *ktd)
189 {
190
191 KDASSERT(ktd->ktd_ref != 0);
192 KASSERT(ktd->ktd_ref > 0);
193 if (--ktd->ktd_ref <= 0) {
194 ktd->ktd_flags |= KTDF_DONE;
195 wakeup(ktd);
196 }
197 simple_unlock(&ktd->ktd_slock);
198 }
199
200 void
201 ktdref(struct ktr_desc *ktd)
202 {
203
204 simple_lock(&ktd->ktd_slock);
205 ktd->ktd_ref++;
206 simple_unlock(&ktd->ktd_slock);
207 }
208
209 struct ktr_desc *
210 ktd_lookup(struct file *fp)
211 {
212 struct ktr_desc *ktd;
213
214 simple_lock(&ktdq_slock);
215 for (ktd = TAILQ_FIRST(&ktdq); ktd != NULL;
216 ktd = TAILQ_NEXT(ktd, ktd_list)) {
217 simple_lock(&ktd->ktd_slock);
218 if (ktrsamefile(ktd->ktd_fp, fp)) {
219 ktd->ktd_ref++;
220 simple_unlock(&ktd->ktd_slock);
221 break;
222 }
223 simple_unlock(&ktd->ktd_slock);
224 }
225 simple_unlock(&ktdq_slock);
226 return (ktd);
227 }
228
229 void
230 ktraddentry(struct lwp *l, struct ktrace_entry *kte, int flags)
231 {
232 struct proc *p = l->l_proc;
233 struct ktr_desc *ktd;
234 #ifdef DEBUG
235 struct timeval t1, t2;
236 #endif
237
238 if (p->p_traceflag & KTRFAC_TRC_EMUL) {
239 /* Add emulation trace before first entry for this process */
240 p->p_traceflag &= ~KTRFAC_TRC_EMUL;
241 ktremul(l);
242 }
243
244 /*
245 * Tracing may be canceled while we were sleeping waiting for
246 * memory.
247 */
248 ktd = p->p_tracep;
249 if (ktd == NULL)
250 goto freekte;
251
252 /*
253 * Bump reference count so that the object will remain while
254 * we are here. Note that the trace is controlled by other
255 * process.
256 */
257 ktdref(ktd);
258
259 simple_lock(&ktd->ktd_slock);
260 if (ktd->ktd_flags & KTDF_DONE)
261 goto relktd;
262
263 if (ktd->ktd_qcount > ktd_maxentry) {
264 ktd_logerrl(ktd, KTDE_ENOSPC);
265 goto relktd;
266 }
267 TAILQ_INSERT_TAIL(&ktd->ktd_queue, kte, kte_list);
268 ktd->ktd_qcount++;
269 if (ktd->ktd_flags & KTDF_BLOCKING)
270 goto skip_sync;
271
272 if (flags & KTA_WAITOK &&
273 (/* flags & KTA_LARGE */0 || ktd->ktd_flags & KTDF_WAIT ||
274 ktd->ktd_qcount > ktd_maxentry >> 1))
275 /*
276 * Sync with writer thread since we're requesting rather
277 * big one or many requests are pending.
278 */
279 do {
280 ktd->ktd_flags |= KTDF_WAIT;
281 ktd_wakeup(ktd);
282 #ifdef DEBUG
283 getmicrouptime(&t1);
284 #endif
285 if (ltsleep(&ktd->ktd_flags, PWAIT, "ktrsync",
286 ktd_timeout * hz, &ktd->ktd_slock) != 0) {
287 ktd->ktd_flags |= KTDF_BLOCKING;
288 /*
289 * Maybe the writer thread is blocking
290 * completely for some reason, but
291 * don't stop target process forever.
292 */
293 log(LOG_NOTICE, "ktrace timeout\n");
294 break;
295 }
296 #ifdef DEBUG
297 getmicrouptime(&t2);
298 timersub(&t2, &t1, &t2);
299 if (t2.tv_sec > 0)
300 log(LOG_NOTICE,
301 "ktrace long wait: %ld.%06ld\n",
302 t2.tv_sec, t2.tv_usec);
303 #endif
304 } while (p->p_tracep == ktd &&
305 (ktd->ktd_flags & (KTDF_WAIT | KTDF_DONE)) == KTDF_WAIT);
306 else {
307 /* Schedule delayed wakeup */
308 if (ktd->ktd_qcount > ktd->ktd_delayqcnt)
309 ktd_wakeup(ktd); /* Wakeup now */
310 else if (!callout_pending(&ktd->ktd_wakch))
311 callout_reset(&ktd->ktd_wakch,
312 ktd->ktd_flags & KTDF_INTERACTIVE ?
313 ktd->ktd_intrwakdl : ktd->ktd_wakedelay,
314 (void (*)(void *))wakeup, ktd);
315 }
316
317 skip_sync:
318 ktdrel(ktd);
319 return;
320
321 relktd:
322 ktdrel(ktd);
323
324 freekte:
325 ktefree(kte);
326 }
327
328 void
329 ktefree(struct ktrace_entry *kte)
330 {
331
332 if (kte->kte_buf != NULL)
333 free(kte->kte_buf, M_KTRACE);
334 pool_put(&kte_pool, kte);
335 }
336
337 /*
338 * "deep" compare of two files for the purposes of clearing a trace.
339 * Returns true if they're the same open file, or if they point at the
340 * same underlying vnode/socket.
341 */
342
343 int
344 ktrsamefile(struct file *f1, struct file *f2)
345 {
346
347 return ((f1 == f2) ||
348 ((f1 != NULL) && (f2 != NULL) &&
349 (f1->f_type == f2->f_type) &&
350 (f1->f_data == f2->f_data)));
351 }
352
353 void
354 ktrderef(struct proc *p)
355 {
356 struct ktr_desc *ktd = p->p_tracep;
357
358 p->p_traceflag = 0;
359 if (ktd == NULL)
360 return;
361 p->p_tracep = NULL;
362
363 simple_lock(&ktd->ktd_slock);
364 wakeup(&ktd->ktd_flags);
365 ktdrel(ktd);
366 }
367
368 void
369 ktradref(struct proc *p)
370 {
371 struct ktr_desc *ktd = p->p_tracep;
372
373 ktdref(ktd);
374 }
375
376 void
377 ktrinitheader(struct ktr_header *kth, struct lwp *l, int type)
378 {
379 struct proc *p = l->l_proc;
380
381 (void)memset(kth, 0, sizeof(*kth));
382 kth->ktr_type = type;
383 kth->ktr_pid = p->p_pid;
384 memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
385
386 kth->ktr_version = KTRFAC_VERSION(p->p_traceflag);
387
388 switch (KTRFAC_VERSION(p->p_traceflag)) {
389 case 0:
390 /* This is the original format */
391 microtime(&kth->ktr_tv);
392 break;
393 case 1:
394 kth->ktr_lid = l->l_lid;
395 nanotime(&kth->ktr_time);
396 break;
397 default:
398 break;
399 }
400 }
401
402 void
403 ktrsyscall(struct lwp *l, register_t code, register_t realcode,
404 const struct sysent *callp, register_t args[])
405 {
406 struct proc *p = l->l_proc;
407 struct ktrace_entry *kte;
408 struct ktr_header *kth;
409 struct ktr_syscall *ktp;
410 register_t *argp;
411 int argsize;
412 size_t len;
413 u_int i;
414
415 if (callp == NULL)
416 callp = p->p_emul->e_sysent;
417
418 argsize = callp[code].sy_argsize;
419 #ifdef _LP64
420 if (p->p_flag & P_32)
421 argsize = argsize << 1;
422 #endif
423 len = sizeof(struct ktr_syscall) + argsize;
424
425 p->p_traceflag |= KTRFAC_ACTIVE;
426 kte = pool_get(&kte_pool, PR_WAITOK);
427 kth = &kte->kte_kth;
428 ktrinitheader(kth, l, KTR_SYSCALL);
429
430 ktp = malloc(len, M_KTRACE, M_WAITOK);
431 ktp->ktr_code = realcode;
432 ktp->ktr_argsize = argsize;
433 argp = (register_t *)(ktp + 1);
434 for (i = 0; i < (argsize / sizeof(*argp)); i++)
435 *argp++ = args[i];
436 kth->ktr_len = len;
437 kte->kte_buf = ktp;
438
439 ktraddentry(l, kte, KTA_WAITOK);
440 p->p_traceflag &= ~KTRFAC_ACTIVE;
441 }
442
443 void
444 ktrsysret(struct lwp *l, register_t code, int error, register_t *retval)
445 {
446 struct proc *p = l->l_proc;
447 struct ktrace_entry *kte;
448 struct ktr_header *kth;
449 struct ktr_sysret *ktp;
450
451 p->p_traceflag |= KTRFAC_ACTIVE;
452 kte = pool_get(&kte_pool, PR_WAITOK);
453 kth = &kte->kte_kth;
454 ktrinitheader(kth, l, KTR_SYSRET);
455
456 ktp = malloc(sizeof(struct ktr_sysret), M_KTRACE, M_WAITOK);
457 ktp->ktr_code = code;
458 ktp->ktr_eosys = 0; /* XXX unused */
459 ktp->ktr_error = error;
460 ktp->ktr_retval = retval ? retval[0] : 0;
461 ktp->ktr_retval_1 = retval ? retval[1] : 0;
462
463 kth->ktr_len = sizeof(struct ktr_sysret);
464 kte->kte_buf = ktp;
465
466 ktraddentry(l, kte, KTA_WAITOK);
467 p->p_traceflag &= ~KTRFAC_ACTIVE;
468 }
469
470 /*
471 * XXX: ndp->ni_pathlen should be passed.
472 */
473 void
474 ktrnamei(struct lwp *l, char *path)
475 {
476
477 ktrkmem(l, KTR_NAMEI, path, strlen(path));
478 }
479
480 void
481 ktremul(struct lwp *l)
482 {
483 const char *emul = l->l_proc->p_emul->e_name;
484
485 ktrkmem(l, KTR_EMUL, emul, strlen(emul));
486 }
487
488 void
489 ktrkmem(struct lwp *l, int type, const void *bf, size_t len)
490 {
491 struct proc *p = l->l_proc;
492 struct ktrace_entry *kte;
493 struct ktr_header *kth;
494
495 p->p_traceflag |= KTRFAC_ACTIVE;
496 kte = pool_get(&kte_pool, PR_WAITOK);
497 kth = &kte->kte_kth;
498 ktrinitheader(kth, l, type);
499
500 kth->ktr_len = len;
501 kte->kte_buf = malloc(len, M_KTRACE, M_WAITOK);
502 memcpy(kte->kte_buf, bf, len);
503
504 ktraddentry(l, kte, KTA_WAITOK);
505 p->p_traceflag &= ~KTRFAC_ACTIVE;
506 }
507
508 void
509 ktrgenio(struct lwp *l, int fd, enum uio_rw rw, struct iovec *iov,
510 int len, int error)
511 {
512 struct proc *p = l->l_proc;
513 struct ktrace_entry *kte;
514 struct ktr_header *kth;
515 struct ktr_genio *ktp;
516 int resid = len, cnt;
517 caddr_t cp;
518 int buflen;
519
520 if (error)
521 return;
522
523 p->p_traceflag |= KTRFAC_ACTIVE;
524
525 next:
526 buflen = min(PAGE_SIZE, resid + sizeof(struct ktr_genio));
527
528 kte = pool_get(&kte_pool, PR_WAITOK);
529 kth = &kte->kte_kth;
530 ktrinitheader(kth, l, KTR_GENIO);
531
532 ktp = malloc(buflen, M_KTRACE, M_WAITOK);
533 ktp->ktr_fd = fd;
534 ktp->ktr_rw = rw;
535
536 kte->kte_buf = ktp;
537
538 cp = (caddr_t)(ktp + 1);
539 buflen -= sizeof(struct ktr_genio);
540 kth->ktr_len = sizeof(struct ktr_genio);
541
542 while (buflen > 0) {
543 cnt = min(iov->iov_len, buflen);
544 if (copyin(iov->iov_base, cp, cnt) != 0)
545 goto out;
546 kth->ktr_len += cnt;
547 buflen -= cnt;
548 resid -= cnt;
549 iov->iov_len -= cnt;
550 if (iov->iov_len == 0)
551 iov++;
552 else
553 iov->iov_base = (caddr_t)iov->iov_base + cnt;
554 }
555
556 /*
557 * Don't push so many entry at once. It will cause kmem map
558 * shortage.
559 */
560 ktraddentry(l, kte, KTA_WAITOK | KTA_LARGE);
561 if (resid > 0) {
562 #if 0 /* XXX NJWLWP */
563 KDASSERT(p->p_cpu != NULL);
564 KDASSERT(p->p_cpu == curcpu());
565 #endif
566 /* XXX NJWLWP */
567 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
568 preempt(1);
569
570 goto next;
571 }
572
573 p->p_traceflag &= ~KTRFAC_ACTIVE;
574 return;
575
576 out:
577 ktefree(kte);
578 p->p_traceflag &= ~KTRFAC_ACTIVE;
579 }
580
581 void
582 ktrpsig(struct lwp *l, int sig, sig_t action, const sigset_t *mask,
583 const ksiginfo_t *ksi)
584 {
585 struct proc *p = l->l_proc;
586 struct ktrace_entry *kte;
587 struct ktr_header *kth;
588 struct {
589 struct ktr_psig kp;
590 siginfo_t si;
591 } *kbuf;
592
593 p->p_traceflag |= KTRFAC_ACTIVE;
594 kte = pool_get(&kte_pool, PR_WAITOK);
595 kth = &kte->kte_kth;
596 ktrinitheader(kth, l, KTR_PSIG);
597
598 kbuf = malloc(sizeof(*kbuf), M_KTRACE, M_WAITOK);
599 kbuf->kp.signo = (char)sig;
600 kbuf->kp.action = action;
601 kbuf->kp.mask = *mask;
602 kte->kte_buf = kbuf;
603 if (ksi) {
604 kbuf->kp.code = KSI_TRAPCODE(ksi);
605 (void)memset(&kbuf->si, 0, sizeof(kbuf->si));
606 kbuf->si._info = ksi->ksi_info;
607 kth->ktr_len = sizeof(*kbuf);
608 } else {
609 kbuf->kp.code = 0;
610 kth->ktr_len = sizeof(struct ktr_psig);
611 }
612
613 ktraddentry(l, kte, KTA_WAITOK);
614 p->p_traceflag &= ~KTRFAC_ACTIVE;
615 }
616
617 void
618 ktrcsw(struct lwp *l, int out, int user)
619 {
620 struct proc *p = l->l_proc;
621 struct ktrace_entry *kte;
622 struct ktr_header *kth;
623 struct ktr_csw *kc;
624
625 p->p_traceflag |= KTRFAC_ACTIVE;
626
627 /*
628 * We can't sleep if we're already going to sleep (if original
629 * condition is met during sleep, we hang up).
630 */
631 kte = pool_get(&kte_pool, out ? PR_NOWAIT : PR_WAITOK);
632 if (kte == NULL) {
633 ktd_logerr(p, KTDE_ENOMEM);
634 goto out;
635 }
636 kth = &kte->kte_kth;
637 ktrinitheader(kth, l, KTR_CSW);
638
639 kc = malloc(sizeof(struct ktr_csw), M_KTRACE,
640 out ? M_NOWAIT : M_WAITOK);
641 if (kc == NULL) {
642 ktd_logerr(p, KTDE_ENOMEM);
643 goto free_kte;
644 }
645 kc->out = out;
646 kc->user = user;
647 kth->ktr_len = sizeof(struct ktr_csw);
648 kte->kte_buf = kc;
649
650 ktraddentry(l, kte, out ? KTA_NOWAIT : KTA_WAITOK);
651 p->p_traceflag &= ~KTRFAC_ACTIVE;
652 return;
653
654 free_kte:
655 pool_put(&kte_pool, kte);
656 out:
657 p->p_traceflag &= ~KTRFAC_ACTIVE;
658 }
659
660 void
661 ktruser(struct lwp *l, const char *id, void *addr, size_t len, int ustr)
662 {
663 struct proc *p = l->l_proc;
664 struct ktrace_entry *kte;
665 struct ktr_header *kth;
666 struct ktr_user *ktp;
667 caddr_t user_dta;
668
669 p->p_traceflag |= KTRFAC_ACTIVE;
670 kte = pool_get(&kte_pool, PR_WAITOK);
671 kth = &kte->kte_kth;
672 ktrinitheader(kth, l, KTR_USER);
673
674 ktp = malloc(sizeof(struct ktr_user) + len, M_KTRACE, M_WAITOK);
675 if (ustr) {
676 if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
677 ktp->ktr_id[0] = '\0';
678 } else
679 strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
680 ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
681
682 user_dta = (caddr_t)(ktp + 1);
683 if (copyin(addr, (void *)user_dta, len) != 0)
684 len = 0;
685
686 kth->ktr_len = sizeof(struct ktr_user) + len;
687 kte->kte_buf = ktp;
688
689 ktraddentry(l, kte, KTA_WAITOK);
690 p->p_traceflag &= ~KTRFAC_ACTIVE;
691 }
692
693 void
694 ktrmmsg(struct lwp *l, const void *msgh, size_t size)
695 {
696 ktrkmem(l, KTR_MMSG, msgh, size);
697 }
698
699 void
700 ktrmool(struct lwp *l, const void *kaddr, size_t size, const void *uaddr)
701 {
702 struct proc *p = l->l_proc;
703 struct ktrace_entry *kte;
704 struct ktr_header *kth;
705 struct ktr_mool *kp;
706 struct ktr_mool *bf;
707
708 p->p_traceflag |= KTRFAC_ACTIVE;
709 kte = pool_get(&kte_pool, PR_WAITOK);
710 kth = &kte->kte_kth;
711 ktrinitheader(kth, l, KTR_MOOL);
712
713 kp = malloc(size + sizeof(*kp), M_KTRACE, M_WAITOK);
714 kp->uaddr = uaddr;
715 kp->size = size;
716 bf = kp + 1; /* Skip uaddr and size */
717 (void)memcpy(bf, kaddr, size);
718
719 kth->ktr_len = size + sizeof(*kp);
720 kte->kte_buf = kp;
721
722 ktraddentry(l, kte, KTA_WAITOK);
723 p->p_traceflag &= ~KTRFAC_ACTIVE;
724 }
725
726 void
727 ktrsaupcall(struct lwp *l, int type, int nevent, int nint, void *sas,
728 void *ap)
729 {
730 struct proc *p = l->l_proc;
731 struct ktrace_entry *kte;
732 struct ktr_header *kth;
733 struct ktr_saupcall *ktp;
734 size_t len;
735 struct sa_t **sapp;
736 int i;
737
738 p->p_traceflag |= KTRFAC_ACTIVE;
739 kte = pool_get(&kte_pool, PR_WAITOK);
740 kth = &kte->kte_kth;
741 ktrinitheader(kth, l, KTR_SAUPCALL);
742
743 len = sizeof(struct ktr_saupcall);
744 ktp = malloc(len + sizeof(struct sa_t) * (nevent + nint + 1), M_KTRACE,
745 M_WAITOK);
746
747 ktp->ktr_type = type;
748 ktp->ktr_nevent = nevent;
749 ktp->ktr_nint = nint;
750 ktp->ktr_sas = sas;
751 ktp->ktr_ap = ap;
752 /*
753 * Copy the sa_t's
754 */
755 sapp = (struct sa_t **) sas;
756
757 for (i = nevent + nint; i >= 0; i--) {
758 if (copyin(*sapp, (char *)ktp + len, sizeof(struct sa_t)) == 0)
759 len += sizeof(struct sa_t);
760 sapp++;
761 }
762
763 kth->ktr_len = len;
764 kte->kte_buf = ktp;
765
766 ktraddentry(l, kte, KTA_WAITOK);
767 p->p_traceflag &= ~KTRFAC_ACTIVE;
768 }
769
770 /* Interface and common routines */
771
772 int
773 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
774 {
775 struct proc *p;
776 struct pgrp *pg;
777 struct ktr_desc *ktd = NULL;
778 int ret = 0;
779 int error = 0;
780 int descend;
781
782 curp->p_traceflag |= KTRFAC_ACTIVE;
783 descend = ops & KTRFLAG_DESCEND;
784 facs = facs & ~((unsigned) KTRFAC_ROOT);
785
786 switch (KTROP(ops)) {
787
788 case KTROP_CLEARFILE:
789 /*
790 * Clear all uses of the tracefile
791 */
792
793 ktd = ktd_lookup(fp);
794 if (ktd == NULL)
795 goto done;
796
797 proclist_lock_read();
798 PROCLIST_FOREACH(p, &allproc) {
799 if (p->p_tracep == ktd) {
800 if (ktrcanset(curp, p))
801 ktrderef(p);
802 else
803 error = EPERM;
804 }
805 }
806 proclist_unlock_read();
807 goto done;
808
809 case KTROP_SET:
810 ktd = ktd_lookup(fp);
811 if (ktd == NULL) {
812 ktd = malloc(sizeof(struct ktr_desc),
813 M_KTRACE, M_WAITOK);
814 TAILQ_INIT(&ktd->ktd_queue);
815 simple_lock_init(&ktd->ktd_slock);
816 callout_init(&ktd->ktd_wakch);
817 ktd->ktd_flags = ktd->ktd_qcount =
818 ktd->ktd_error = ktd->ktd_errcnt = 0;
819 ktd->ktd_ref = 1;
820 ktd->ktd_delayqcnt = ktd_delayqcnt;
821 ktd->ktd_wakedelay = mstohz(ktd_wakedelay);
822 ktd->ktd_intrwakdl = mstohz(ktd_intrwakdl);
823 /*
824 * XXX: not correct. needs an way to detect
825 * whether ktruss or ktrace.
826 */
827 if (fp->f_type == DTYPE_PIPE)
828 ktd->ktd_flags |= KTDF_INTERACTIVE;
829
830 error = kthread_create1(ktrace_thread, ktd,
831 &ktd->ktd_proc, "ktr %p", ktd);
832 if (error != 0) {
833 free(ktd, M_KTRACE);
834 goto done;
835 }
836
837 simple_lock(&fp->f_slock);
838 fp->f_count++;
839 simple_unlock(&fp->f_slock);
840 ktd->ktd_fp = fp;
841
842 simple_lock(&ktdq_slock);
843 TAILQ_INSERT_TAIL(&ktdq, ktd, ktd_list);
844 simple_unlock(&ktdq_slock);
845 }
846 break;
847
848 case KTROP_CLEAR:
849 break;
850 }
851
852 /*
853 * need something to (un)trace (XXX - why is this here?)
854 */
855 if (!facs) {
856 error = EINVAL;
857 goto done;
858 }
859
860 /*
861 * do it
862 */
863 if (pid < 0) {
864 /*
865 * by process group
866 */
867 pg = pg_find(-pid, PFIND_UNLOCK_FAIL);
868 if (pg == NULL) {
869 error = ESRCH;
870 goto done;
871 }
872 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
873 if (descend)
874 ret |= ktrsetchildren(curp, p, ops, facs, ktd);
875 else
876 ret |= ktrops(curp, p, ops, facs, ktd);
877 }
878
879 } else {
880 /*
881 * by pid
882 */
883 p = p_find(pid, PFIND_UNLOCK_FAIL);
884 if (p == NULL) {
885 error = ESRCH;
886 goto done;
887 }
888 if (descend)
889 ret |= ktrsetchildren(curp, p, ops, facs, ktd);
890 else
891 ret |= ktrops(curp, p, ops, facs, ktd);
892 }
893 proclist_unlock_read(); /* taken by p{g}_find */
894 if (!ret)
895 error = EPERM;
896 done:
897 if (ktd != NULL) {
898 if (error != 0) {
899 /*
900 * Wakeup the thread so that it can be die if we
901 * can't trace any process.
902 */
903 ktd_wakeup(ktd);
904 }
905 if (KTROP(ops) == KTROP_SET || KTROP(ops) == KTROP_CLEARFILE) {
906 simple_lock(&ktd->ktd_slock);
907 ktdrel(ktd);
908 }
909 }
910 curp->p_traceflag &= ~KTRFAC_ACTIVE;
911 return (error);
912 }
913
914 /*
915 * fktrace system call
916 */
917 /* ARGSUSED */
918 int
919 sys_fktrace(struct lwp *l, void *v, register_t *retval)
920 {
921 struct sys_fktrace_args /* {
922 syscallarg(int) fd;
923 syscallarg(int) ops;
924 syscallarg(int) facs;
925 syscallarg(int) pid;
926 } */ *uap = v;
927 struct proc *curp;
928 struct file *fp = NULL;
929 struct filedesc *fdp = l->l_proc->p_fd;
930 int error;
931
932 curp = l->l_proc;
933 fdp = curp->p_fd;
934 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
935 return (EBADF);
936
937 FILE_USE(fp);
938
939 if ((fp->f_flag & FWRITE) == 0)
940 error = EBADF;
941 else
942 error = ktrace_common(curp, SCARG(uap, ops),
943 SCARG(uap, facs), SCARG(uap, pid), fp);
944
945 FILE_UNUSE(fp, l);
946
947 return error;
948 }
949
950 /*
951 * ktrace system call
952 */
953 /* ARGSUSED */
954 int
955 sys_ktrace(struct lwp *l, void *v, register_t *retval)
956 {
957 struct sys_ktrace_args /* {
958 syscallarg(const char *) fname;
959 syscallarg(int) ops;
960 syscallarg(int) facs;
961 syscallarg(int) pid;
962 } */ *uap = v;
963 struct proc *curp = l->l_proc;
964 struct vnode *vp = NULL;
965 struct file *fp = NULL;
966 struct nameidata nd;
967 int error = 0;
968 int fd;
969
970 curp->p_traceflag |= KTRFAC_ACTIVE;
971 if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) {
972 /*
973 * an operation which requires a file argument.
974 */
975 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
976 l);
977 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
978 curp->p_traceflag &= ~KTRFAC_ACTIVE;
979 return (error);
980 }
981 vp = nd.ni_vp;
982 VOP_UNLOCK(vp, 0);
983 if (vp->v_type != VREG) {
984 (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, l);
985 curp->p_traceflag &= ~KTRFAC_ACTIVE;
986 return (EACCES);
987 }
988 /*
989 * XXX This uses up a file descriptor slot in the
990 * tracing process for the duration of this syscall.
991 * This is not expected to be a problem. If
992 * falloc(NULL, ...) DTRT we could skip that part, but
993 * that would require changing its interface to allow
994 * the caller to pass in a ucred..
995 *
996 * This will FILE_USE the fp it returns, if any.
997 * Keep it in use until we return.
998 */
999 if ((error = falloc(curp, &fp, &fd)) != 0)
1000 goto done;
1001
1002 fp->f_flag = FWRITE;
1003 fp->f_type = DTYPE_VNODE;
1004 fp->f_ops = &vnops;
1005 fp->f_data = (caddr_t)vp;
1006 FILE_SET_MATURE(fp);
1007 vp = NULL;
1008 }
1009 error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
1010 SCARG(uap, pid), fp);
1011 done:
1012 if (vp != NULL)
1013 (void) vn_close(vp, FWRITE, curp->p_ucred, l);
1014 if (fp != NULL) {
1015 FILE_UNUSE(fp, l); /* release file */
1016 fdrelease(l, fd); /* release fd table slot */
1017 }
1018 return (error);
1019 }
1020
1021 int
1022 ktrops(struct proc *curp, struct proc *p, int ops, int facs,
1023 struct ktr_desc *ktd)
1024 {
1025
1026 int vers = ops & KTRFAC_VER_MASK;
1027
1028 if (!ktrcanset(curp, 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 (curp->p_ucred->cr_uid == 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 proc *curp, 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(curp, 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 proc *callp, struct proc *targetp)
1261 {
1262 struct pcred *caller = callp->p_cred;
1263 struct pcred *target = targetp->p_cred;
1264
1265 if ((caller->pc_ucred->cr_uid == target->p_ruid &&
1266 target->p_ruid == target->p_svuid &&
1267 caller->p_rgid == target->p_rgid && /* XXX */
1268 target->p_rgid == target->p_svgid &&
1269 (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
1270 (targetp->p_flag & P_SUGID) == 0) ||
1271 caller->pc_ucred->cr_uid == 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