dump.c revision 1.26 1 /* $NetBSD: dump.c,v 1.26 2006/10/22 16:20:39 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 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
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95";
41 #endif
42 __RCSID("$NetBSD: dump.c,v 1.26 2006/10/22 16:20:39 christos Exp $");
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #define _KERNEL
47 #include <sys/errno.h>
48 #undef _KERNEL
49 #include <sys/ioctl.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/ktrace.h>
53 #include <sys/ptrace.h>
54 #include <sys/queue.h>
55
56 #include <err.h>
57 #include <signal.h>
58 #include <stdarg.h>
59 #include <stddef.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <vis.h>
65
66 #include "ktrace.h"
67 #include "misc.h"
68 #include "setemul.h"
69
70 int timestamp, decimal, fancy = 1, tail, maxdata;
71
72 int width; /* Keep track of current columns. */
73
74 #include <sys/syscall.h>
75
76 static const char *const ptrace_ops[] = {
77 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
78 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
79 "PT_KILL", "PT_ATTACH", "PT_DETACH",
80 };
81
82 struct ktr_entry {
83 TAILQ_ENTRY(ktr_entry) kte_list;
84 struct ktr_header kte_kth;
85 };
86
87 TAILQ_HEAD(kteq, ktr_entry) ktependq = TAILQ_HEAD_INITIALIZER(ktependq);
88
89 void argprint(const char *, register_t **, int *);
90 void dumpheader(struct ktr_header *);
91 int dumprecord(int, FILE *);
92 void flushpendq(struct ktr_entry *);
93 int fread_tail(void *, int, int, FILE *);
94 void genioprint(struct ktr_header *);
95 struct ktr_entry *
96 getpendq(struct ktr_header *, int, struct kteq *);
97 struct ktr_entry *
98 getrecord(FILE *);
99 void indent(int);
100 void ioctldecode(u_long);
101 void ktrcsw(struct ktr_entry *);
102 void ktremul(struct ktr_entry *);
103 void ktrgenio(struct ktr_entry *);
104 void ktrnamei(struct ktr_entry *);
105 void ktrpsig(struct ktr_entry *);
106 void ktrsyscall(struct ktr_entry *);
107 void ktrsysret(struct ktr_entry *);
108 void nameiargprint(const char *, struct ktr_header *, register_t **, int *);
109 void nameiprint(struct ktr_header *);
110 void newline(void);
111 void putpendq(struct ktr_entry *);
112 void syscallnameprint(int);
113 void syscallprint(struct ktr_header *);
114 void sysretprint(struct ktr_header *);
115 int wprintf(const char *, ...);
116 void *xrealloc(void *, size_t *, size_t);
117
118 int
119 wprintf(const char *fmt, ...)
120 {
121 va_list ap;
122 int w;
123
124 va_start(ap, fmt);
125 w = vprintf(fmt, ap);
126 if (w == -1)
127 warn("vprintf");
128 else
129 width += w;
130 va_end(ap);
131 return (w);
132 }
133
134 void
135 newline(void)
136 {
137
138 if (width > 0) {
139 printf("\n");
140 width = 0;
141 }
142 }
143
144 void
145 indent(int col)
146 {
147
148 while (width < col)
149 if (wprintf(" ") < 0)
150 break;
151 }
152
153 void *
154 xrealloc(void *p, size_t *siz, size_t req)
155 {
156
157 if (*siz < req) {
158 if (*siz == 0)
159 *siz = 1;
160 while (*siz < req)
161 *siz <<= 1;
162 p = realloc(p, *siz);
163 if (p == NULL)
164 err(EXIT_FAILURE, "realloc: %lu bytes",
165 (u_long)*siz);
166 }
167 return (p);
168 }
169
170 struct ktr_entry *
171 getrecord(FILE *fp)
172 {
173 struct ktr_entry *kte;
174 struct ktr_header *kth;
175 char *cp;
176 size_t siz, len;
177
178 siz = 0;
179 kte = xrealloc(NULL, &siz, sizeof(struct ktr_entry));
180 kth = &kte->kte_kth;
181 if (fread_tail(kth, sizeof(struct ktr_header), 1, fp) == 0) {
182 free(kte);
183 return (NULL);
184 }
185
186 if (kth->ktr_len < 0)
187 errx(EXIT_FAILURE, "bogus length 0x%x", kth->ktr_len);
188 len = kth->ktr_len;
189 if (len > 0) {
190 /* + 1 to ensure room for NUL terminate */
191 kte = xrealloc(kte, &siz, sizeof(struct ktr_entry) + len + 1);
192 if (fread_tail(cp = (char *)(&kte->kte_kth + 1),
193 len, 1, fp) == 0)
194 errx(EXIT_FAILURE, "data too short");
195 cp[len] = 0;
196 }
197
198 return (kte);
199 }
200
201 /* XXX: lwp. */
202 #define KTE_TYPE(kte) ((kte)->kte_kth.ktr_type)
203 #define KTE_PID(kte) ((kte)->kte_kth.ktr_pid)
204 #define KTE_MATCH(kte, type, pid) \
205 (KTE_TYPE(kte) == (type) && KTE_PID(kte) == (pid))
206
207 void
208 putpendq(struct ktr_entry *kte)
209 {
210
211 TAILQ_INSERT_TAIL(&ktependq, kte, kte_list);
212 }
213
214 void
215 flushpendq(struct ktr_entry *us)
216 {
217 struct ktr_entry *kte, *kte_next;
218 int pid = KTE_PID(us);
219
220 for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) {
221 kte_next = TAILQ_NEXT(kte, kte_list);
222 if (KTE_PID(kte) == pid) {
223 TAILQ_REMOVE(&ktependq, kte, kte_list);
224 free(kte);
225 }
226 }
227 }
228
229 struct ktr_entry *
230 getpendq(struct ktr_header *us, int type, struct kteq *kteq)
231 {
232 struct ktr_entry *kte, *kte_next;
233 int pid = us->ktr_pid;
234
235 if (kteq != NULL)
236 TAILQ_INIT(kteq);
237 for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) {
238 kte_next = TAILQ_NEXT(kte, kte_list);
239 if (KTE_MATCH(kte, type, pid)) {
240 TAILQ_REMOVE(&ktependq, kte, kte_list);
241 if (kteq != NULL)
242 TAILQ_INSERT_TAIL(kteq, kte, kte_list);
243 else
244 break;
245 }
246 }
247
248 return (kteq ? TAILQ_FIRST(kteq) : kte);
249 }
250
251 int
252 dumprecord(int trpoints, FILE *fp)
253 {
254 struct ktr_entry *kte;
255 struct ktr_header *kth;
256
257 kte = getrecord(fp);
258 if (kte == NULL)
259 return (0);
260
261 kth = &kte->kte_kth;
262 if ((trpoints & (1 << kth->ktr_type)) == 0) {
263 free(kte);
264 goto out;
265 }
266
267 /* Update context to match currently processed record. */
268 ectx_sanify(kth->ktr_pid);
269
270 switch (kth->ktr_type) {
271 case KTR_SYSCALL:
272 ktrsyscall(kte);
273 break;
274 case KTR_SYSRET:
275 ktrsysret(kte);
276 break;
277 case KTR_NAMEI:
278 putpendq(kte);
279 break;
280 case KTR_GENIO:
281 putpendq(kte);
282 break;
283 case KTR_PSIG:
284 ktrpsig(kte);
285 break;
286 case KTR_CSW:
287 ktrcsw(kte);
288 break;
289 case KTR_EMUL:
290 ktremul(kte);
291 break;
292 default:
293 /*
294 * XXX: Other types added recently.
295 */
296 free(kte);
297 break;
298 }
299 newline();
300
301 out:
302 return (1);
303 }
304
305 void
306 dumpfile(const char *file, int fd, int trpoints)
307 {
308 FILE *fp;
309
310 if (file == NULL || *file == 0) {
311 if ((fp = fdopen(fd, "r")) == NULL)
312 err(EXIT_FAILURE, "fdopen(%d)", fd);
313 } else if (strcmp(file, "-") == 0)
314 fp = stdin;
315 else if ((fp = fopen(file, "r")) == NULL)
316 err(EXIT_FAILURE, "fopen: %s", file);
317
318 for (width = 0; dumprecord(trpoints, fp) != 0;)
319 if (tail)
320 (void)fflush(stdout);
321
322 newline();
323
324 /*
325 * XXX: Dump pending KTR_SYSCALL if any?
326 */
327 }
328
329 int
330 fread_tail(void *buf, int size, int num, FILE *fp)
331 {
332 int i;
333
334 while ((i = fread(buf, size, num, fp)) == 0 && tail) {
335 (void)sleep(1);
336 clearerr(fp);
337 }
338 return (i);
339 }
340
341 void
342 dumpheader(struct ktr_header *kth)
343 {
344 union timeholder {
345 struct timeval tv;
346 struct timespec ts;
347 };
348 static union timeholder prevtime;
349 union timeholder temp;
350
351 wprintf("%6d ", kth->ktr_pid);
352 if (kth->ktr_version > KTRFACv0)
353 wprintf("%6d ", kth->ktr_lid);
354 wprintf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
355 if (timestamp) {
356 if (timestamp == 2) {
357 if (kth->ktr_version == KTRFACv0) {
358 if (prevtime.tv.tv_sec == 0)
359 temp.tv.tv_sec = temp.tv.tv_usec = 0;
360 else
361 timersub(&kth->ktr_tv,
362 &prevtime.tv, &temp.tv);
363 prevtime.tv = kth->ktr_tv;
364 } else {
365 if (prevtime.ts.tv_sec == 0)
366 temp.ts.tv_sec = temp.ts.tv_nsec = 0;
367 else
368 timespecsub(&kth->ktr_time,
369 &prevtime.ts, &temp.ts);
370 prevtime.ts = kth->ktr_time;
371 }
372 } else {
373 if (kth->ktr_version == KTRFACv0)
374 temp.tv = kth->ktr_tv;
375 else
376 temp.ts = kth->ktr_time;
377 }
378 if (kth->ktr_version == KTRFACv0)
379 wprintf("%ld.%06ld ",
380 (long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
381 else
382 wprintf("%ld.%09ld ",
383 (long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
384 }
385 }
386
387 void
388 ioctldecode(u_long cmd)
389 {
390 char dirbuf[4], *dir = dirbuf;
391
392 if (cmd & IOC_OUT)
393 *dir++ = 'W';
394 if (cmd & IOC_IN)
395 *dir++ = 'R';
396 *dir = '\0';
397
398 wprintf(decimal ? ", _IO%s('%c',%ld" : ", _IO%s('%c',%#lx",
399 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
400 if ((cmd & IOC_VOID) == 0)
401 wprintf(decimal ? ",%ld)" : ",%#lx)",
402 (cmd >> 16) & 0xff);
403 else
404 wprintf(")");
405 }
406
407 void
408 nameiargprint(const char *prefix, struct ktr_header *kth,
409 register_t **ap, int *argsize)
410 {
411 struct ktr_entry *kte;
412
413 if (*argsize == 0)
414 errx(EXIT_FAILURE, "argument expected");
415 /*
416 * XXX: binary emulation mode.
417 */
418 kte = getpendq(kth, KTR_NAMEI, NULL);
419 if (kte == NULL)
420 argprint(prefix, ap, argsize);
421 else {
422 wprintf("%s", prefix);
423 nameiprint(&kte->kte_kth);
424 free(kte);
425 (*ap)++;
426 *argsize -= sizeof(register_t);
427 }
428 }
429
430 void
431 syscallnameprint(int code)
432 {
433
434 if (code >= cur_emul->nsysnames || code < 0)
435 wprintf("[%d]", code);
436 else
437 wprintf("%s", cur_emul->sysnames[code]);
438 }
439
440 void
441 argprint(const char *prefix, register_t **ap, int *argsize)
442 {
443
444 if (decimal)
445 wprintf("%s%ld", prefix, (long)**ap);
446 else
447 wprintf("%s%#lx", prefix, (long)**ap);
448 (*ap)++;
449 *argsize -= sizeof(register_t);
450 }
451
452 void
453 syscallprint(struct ktr_header *kth)
454 {
455 struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1);
456 register_t *ap;
457 const char *s;
458 int argsize;
459
460 syscallnameprint(ktr->ktr_code);
461
462 /*
463 * Arguments processing.
464 */
465 argsize = ktr->ktr_argsize;
466 if (argsize == 0) {
467 wprintf("(");
468 goto noargument;
469 }
470
471 ap = (register_t *)(ktr + 1);
472 if (!fancy)
473 goto print_first;
474
475 switch (ktr->ktr_code) {
476 /*
477 * All these have a path as the first param.
478 * The order is same as syscalls.master.
479 */
480 case SYS_open:
481 case SYS_link:
482 case SYS_unlink:
483 case SYS_chdir:
484 case SYS_mknod:
485 case SYS_chmod:
486 case SYS_chown:
487 case SYS_unmount:
488 case SYS_access:
489 case SYS_chflags:
490 case SYS_acct:
491 case SYS_revoke:
492 case SYS_symlink:
493 case SYS_readlink:
494 case SYS_execve:
495 case SYS_chroot:
496 case SYS_rename:
497 case SYS_mkfifo:
498 case SYS_mkdir:
499 case SYS_rmdir:
500 case SYS_utimes:
501 case SYS_quotactl:
502 case SYS_statvfs1:
503 case SYS_compat_30_getfh:
504 case SYS_pathconf:
505 case SYS_truncate:
506 case SYS_undelete:
507 case SYS___posix_rename:
508 case SYS_lchmod:
509 case SYS_lchown:
510 case SYS_lutimes:
511 case SYS___stat30:
512 case SYS___lstat30:
513 case SYS___posix_chown:
514 case SYS___posix_lchown:
515 case SYS_lchflags:
516 case SYS___getfh30:
517 nameiargprint("(", kth, &ap, &argsize);
518
519 /*
520 * 2nd argument is also pathname.
521 */
522 switch (ktr->ktr_code) {
523 case SYS_link:
524 case SYS_rename:
525 case SYS___posix_rename:
526 nameiargprint(", ", kth, &ap, &argsize);
527 break;
528 }
529 break;
530
531 case SYS_compat_16___sigaction14 :
532 wprintf("(%s", signals[(int)*ap].name);
533 ap++;
534 argsize -= sizeof(register_t);
535 break;
536
537 case SYS_ioctl :
538 argprint("(", &ap, &argsize);
539 if ((s = ioctlname(*ap)) != NULL)
540 wprintf(", %s", s);
541 else
542 ioctldecode(*ap);
543 ap++;
544 argsize -= sizeof(register_t);
545 break;
546
547 case SYS_ptrace :
548 if (*ap >= 0 &&
549 *ap < sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
550 wprintf("(%s", ptrace_ops[*ap]);
551 else
552 wprintf("(%ld", (long)*ap);
553 ap++;
554 argsize -= sizeof(register_t);
555 break;
556
557 default:
558 print_first:
559 argprint("(", &ap, &argsize);
560 break;
561 }
562
563 /* Print rest of argument. */
564 while (argsize > 0)
565 argprint(", ", &ap, &argsize);
566
567 noargument:
568 wprintf(")");
569 }
570
571 void
572 ktrsyscall(struct ktr_entry *kte)
573 {
574 struct ktr_header *kth = &kte->kte_kth;
575 struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1);
576
577 switch (ktr->ktr_code) {
578 case SYS_exit:
579 dumpheader(kth);
580 syscallprint(kth);
581 break;
582 default:
583 putpendq(kte);
584 return;
585 }
586
587 free(kte);
588 }
589
590 void
591 sysretprint(struct ktr_header *kth)
592 {
593 struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1);
594 register_t ret = ktr->ktr_retval;
595 int error = ktr->ktr_error;
596
597 indent(50);
598 if (error == EJUSTRETURN)
599 wprintf(" JUSTRETURN");
600 else if (error == ERESTART)
601 wprintf(" RESTART");
602 else if (error) {
603 wprintf(" Err#%d", error);
604 if (error < MAXERRNOS && error >= -2)
605 wprintf(" %s", errnos[error].name);
606 } else
607 switch (ktr->ktr_code) {
608 case SYS_mmap:
609 wprintf(" = %p", (long)ret);
610 break;
611 default:
612 wprintf(" = %ld", (long)ret);
613 if (kth->ktr_len > offsetof(struct ktr_sysret,
614 ktr_retval_1) && ktr->ktr_retval_1 != 0)
615 wprintf(", %ld", (long)ktr->ktr_retval_1);
616 break;
617 }
618 }
619
620 void
621 ktrsysret(struct ktr_entry *kte)
622 {
623 struct ktr_header *kth = &kte->kte_kth;
624 struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1);
625 struct ktr_entry *genio;
626 struct ktr_entry *syscall_ent;
627
628 dumpheader(kth);
629
630 /* Print syscall name and arguments. */
631 syscall_ent = getpendq(kth, KTR_SYSCALL, NULL);
632 if (syscall_ent == NULL)
633 /*
634 * Possibilly a child of fork/vfork, or tracing of
635 * process started during system call.
636 */
637 syscallnameprint(ktr->ktr_code);
638 else {
639 syscallprint(&syscall_ent->kte_kth);
640 free(syscall_ent);
641 }
642
643 /* Print return value and an error if any. */
644 sysretprint(kth);
645
646 genio = getpendq(kth, KTR_GENIO, NULL);
647 if (genio != NULL) {
648 genioprint(&genio->kte_kth);
649 free(genio);
650 }
651
652 flushpendq(kte);
653 free(kte);
654 }
655
656 void
657 nameiprint(struct ktr_header *kth)
658 {
659
660 wprintf("\"%.*s\"", kth->ktr_len, (char *)(kth + 1));
661 }
662
663 #ifdef notused
664 void
665 ktrnamei(struct ktr_entry *kte)
666 {
667 struct ktr_header *kth = &kte->kte_kth;
668
669 dumpheader(kth);
670 wprintf("namei(");
671 nameiprint(kth);
672 wprintf(")");
673
674 free(kte);
675 }
676 #endif
677
678 void
679 ktremul(struct ktr_entry *kte)
680 {
681 struct ktr_header *kth = &kte->kte_kth;
682 char *emul = (char *)(kth + 1);
683
684 dumpheader(kth);
685 wprintf("emul(%s)", emul);
686 setemul(emul, kth->ktr_pid, 1);
687
688 free(kte);
689 }
690
691 void
692 genioprint(struct ktr_header *kth)
693 {
694 struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1);
695 static int screenwidth = 0;
696 int datalen = kth->ktr_len - sizeof(struct ktr_genio);
697 /*
698 * Need to be unsigned type so that positive value is passed
699 * to vis(), which will call isgraph().
700 */
701 unsigned char *dp = (unsigned char *)(ktr + 1);
702 int w;
703 char visbuf[5];
704
705 if (screenwidth == 0) {
706 struct winsize ws;
707
708 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
709 ws.ws_col > 8)
710 screenwidth = ws.ws_col;
711 else
712 screenwidth = 80;
713 }
714
715 if (maxdata && datalen > maxdata)
716 datalen = maxdata;
717 newline();
718 wprintf(" \"");
719 for (; datalen > 0; datalen--, dp++) {
720 (void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE,
721 /* We put NUL at the end of buffer when reading */
722 *(dp + 1));
723 visbuf[4] = '\0';
724 w = strlen(visbuf);
725 if (width + w + 2 >= screenwidth)
726 break;
727 wprintf("%s", visbuf);
728 if (width + 2 >= screenwidth)
729 break;
730 }
731 wprintf("\"");
732 }
733
734 #ifdef notused
735 void
736 ktrgenio(struct ktr_entry *kte)
737 {
738 struct ktr_header *kth = &kte->kte_kth;
739 struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1);
740
741 dumpheader(kth);
742 wprintf("genio fd %d %s",
743 ktr->ktr_fd, ktr->ktr_rw ? "write" : "read");
744 genioprint(kth);
745
746 free(kte);
747 }
748 #endif
749
750 void
751 ktrpsig(struct ktr_entry *kte)
752 {
753 struct ktr_header *kth = &kte->kte_kth;
754 struct ktr_psig *psig = (struct ktr_psig *)(kth + 1);
755
756 dumpheader(kth);
757 wprintf("SIG%s ", sys_signame[psig->signo]);
758 if (psig->action == SIG_DFL)
759 wprintf("SIG_DFL");
760 else {
761 wprintf("caught handler=0x%lx mask=0x%lx code=0x%x",
762 (u_long)psig->action, (unsigned long)psig->mask.__bits[0],
763 psig->code);
764 }
765
766 free(kte);
767 }
768
769 void
770 ktrcsw(struct ktr_entry *kte)
771 {
772 struct ktr_header *kth = &kte->kte_kth;
773 struct ktr_csw *cs = (struct ktr_csw *)(kth + 1);
774
775 dumpheader(kth);
776 wprintf("%s %s", cs->out ? "stop" : "resume",
777 cs->user ? "user" : "kernel");
778
779 free(kte);
780 }
781