kdump.c revision 1.89 1 /* $NetBSD: kdump.c,v 1.89 2007/02/09 22:08:48 ad 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 #else
42 __RCSID("$NetBSD: kdump.c,v 1.89 2007/02/09 22:08:48 ad Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #define _KERNEL
48 #include <sys/errno.h>
49 #undef _KERNEL
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/ktrace.h>
53 #include <sys/ioctl.h>
54 #include <sys/ptrace.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <signal.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 "setemul.h"
68
69 #include <sys/syscall.h>
70
71 static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
72 static int word_size = 0;
73 static pid_t do_pid = -1;
74 static const char *tracefile = NULL;
75 static struct ktr_header ktr_header;
76 static int emul_changed = 0;
77
78 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
79 #define small(v) (((long)(v) >= 0) && ((long)(v) < 10))
80
81 static const char * const ptrace_ops[] = {
82 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
83 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
84 "PT_KILL", "PT_ATTACH", "PT_DETACH", "PT_IO",
85 "PT_DUMPCORE", "PT_LWPINFO", "PT_SYSCALL",
86 };
87
88 #ifdef PT_MACHDEP_STRINGS
89 static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
90 #endif
91
92 static const char * const linux_ptrace_ops[] = {
93 "PTRACE_TRACEME",
94 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
95 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
96 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
97 NULL, NULL,
98 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
99 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
100 "PTRACE_SYSCALL",
101 };
102
103 int main(int, char **);
104 static int fread_tail(void *, size_t, size_t);
105 static int dumpheader(struct ktr_header *);
106 static void output_long(u_long, int);
107 static void ioctldecode(u_long);
108 static void ktrsyscall(struct ktr_syscall *);
109 static void ktrsysret(struct ktr_sysret *, int);
110 static void ktrnamei(char *, int);
111 static void ktremul(char *, int, int);
112 static void ktrgenio(struct ktr_genio *, int);
113 static void ktrpsig(void *, int);
114 static void ktrcsw(struct ktr_csw *);
115 static void ktruser(struct ktr_user *, int);
116 static void ktrmmsg(struct ktr_mmsg *, int);
117 static void ktrmool(struct ktr_mool *, int);
118 static void ktrmib(int *, int);
119 static void usage(void) __attribute__((__noreturn__));
120 static void eprint(int);
121 static void rprint(register_t);
122 static const char *signame(long, int);
123 static void hexdump_buf(const void *, int, int);
124 static void visdump_buf(const void *, int, int);
125
126 int
127 main(int argc, char **argv)
128 {
129 int ch, ktrlen, size;
130 void *m;
131 int trpoints = 0;
132 int trset = 0;
133 const char *emul_name = "netbsd";
134 int col;
135 char *cp;
136
137 setprogname(argv[0]);
138 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
139 switch (ch) {
140 case 'e':
141 emul_name = strdup(optarg); /* it's safer to copy it */
142 break;
143 case 'f':
144 tracefile = optarg;
145 break;
146 case 'd':
147 decimal = 1;
148 break;
149 case 'l':
150 tail = 1;
151 break;
152 case 'p':
153 do_pid = strtoul(optarg, &cp, 0);
154 if (*cp != 0)
155 errx(1,"invalid number %s", optarg);
156 break;
157 case 'm':
158 maxdata = strtoul(optarg, &cp, 0);
159 if (*cp != 0)
160 errx(1,"invalid number %s", optarg);
161 break;
162 case 'N':
163 numeric++;
164 break;
165 case 'n':
166 plain++;
167 break;
168 case 'R':
169 timestamp = 2; /* relative timestamp */
170 break;
171 case 'T':
172 timestamp = 1;
173 break;
174 case 't':
175 trset = 1;
176 trpoints = getpoints(trpoints, optarg);
177 if (trpoints < 0)
178 errx(1, "unknown trace point in %s", optarg);
179 break;
180 case 'x':
181 word_size = 1;
182 break;
183 case 'X':
184 word_size = strtoul(optarg, &cp, 0);
185 if (*cp != 0 || word_size & (word_size - 1) ||
186 word_size > 16 || word_size <= 0)
187 errx(1, "argument to -X must be "
188 "1, 2, 4, 8 or 16");
189 break;
190 default:
191 usage();
192 }
193 }
194 argv += optind;
195 argc -= optind;
196
197 if (!trset)
198 trpoints = ALL_POINTS;
199
200 if (tracefile == NULL) {
201 if (argc == 1) {
202 tracefile = argv[0];
203 argv++;
204 argc--;
205 } else
206 tracefile = DEF_TRACEFILE;
207 }
208
209 if (argc > 0)
210 usage();
211
212 setemul(emul_name, 0, 0);
213 mach_lookup_emul();
214
215 m = malloc(size = 1024);
216 if (m == NULL)
217 errx(1, "malloc: %s", strerror(ENOMEM));
218 if (!freopen(tracefile, "r", stdin))
219 err(1, "%s", tracefile);
220 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
221 if (trpoints & (1 << ktr_header.ktr_type) &&
222 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
223 col = dumpheader(&ktr_header);
224 else
225 col = -1;
226 if ((ktrlen = ktr_header.ktr_len) < 0)
227 errx(1, "bogus length 0x%x", ktrlen);
228 if (ktrlen > size) {
229 while (ktrlen > size)
230 size *= 2;
231 m = realloc(m, size);
232 if (m == NULL)
233 errx(1, "realloc: %s", strerror(ENOMEM));
234 }
235 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
236 errx(1, "data too short");
237 if (col == -1)
238 continue;
239
240 /* update context to match currently processed record */
241 ectx_sanify(ktr_header.ktr_pid);
242
243 switch (ktr_header.ktr_type) {
244 case KTR_SYSCALL:
245 ktrsyscall(m);
246 break;
247 case KTR_SYSRET:
248 ktrsysret(m, ktrlen);
249 break;
250 case KTR_NAMEI:
251 ktrnamei(m, ktrlen);
252 break;
253 case KTR_GENIO:
254 ktrgenio(m, ktrlen);
255 break;
256 case KTR_PSIG:
257 ktrpsig(m, ktrlen);
258 break;
259 case KTR_CSW:
260 ktrcsw(m);
261 break;
262 case KTR_EMUL:
263 ktremul(m, ktrlen, size);
264 break;
265 case KTR_USER:
266 ktruser(m, ktrlen);
267 break;
268 case KTR_MMSG:
269 ktrmmsg(m, ktrlen);
270 break;
271 case KTR_MOOL:
272 ktrmool(m, ktrlen);
273 break;
274 case KTR_EXEC_ARG:
275 case KTR_EXEC_ENV:
276 visdump_buf(m, ktrlen, col);
277 break;
278 case KTR_MIB:
279 ktrmib(m, ktrlen);
280 break;
281 default:
282 putchar('\n');
283 hexdump_buf(m, ktrlen, word_size ? word_size : 1);
284 }
285 if (tail)
286 (void)fflush(stdout);
287 }
288 return (0);
289 }
290
291 static int
292 fread_tail(void *buf, size_t num, size_t size)
293 {
294 int i;
295
296 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
297 (void)sleep(1);
298 clearerr(stdin);
299 }
300 return (i);
301 }
302
303 static int
304 dumpheader(struct ktr_header *kth)
305 {
306 char unknown[64];
307 const char *type;
308 union holdtime {
309 struct timeval tv;
310 struct timespec ts;
311 };
312 static union holdtime prevtime;
313 union holdtime temp;
314 int col;
315
316 switch (kth->ktr_type) {
317 case KTR_SYSCALL:
318 type = "CALL";
319 break;
320 case KTR_SYSRET:
321 type = "RET ";
322 break;
323 case KTR_NAMEI:
324 type = "NAMI";
325 break;
326 case KTR_GENIO:
327 type = "GIO ";
328 break;
329 case KTR_PSIG:
330 type = "PSIG";
331 break;
332 case KTR_CSW:
333 type = "CSW ";
334 break;
335 case KTR_EMUL:
336 type = "EMUL";
337 break;
338 case KTR_USER:
339 type = "USER";
340 break;
341 case KTR_MMSG:
342 type = "MMSG";
343 break;
344 case KTR_MOOL:
345 type = "MOOL";
346 break;
347 case KTR_EXEC_ENV:
348 type = "ENV";
349 break;
350 case KTR_EXEC_ARG:
351 type = "ARG";
352 break;
353 case KTR_SAUPCALL:
354 type = "SAU";
355 break;
356 case KTR_MIB:
357 type = "MIB";
358 break;
359 default:
360 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
361 kth->ktr_type);
362 type = unknown;
363 }
364
365 col = printf("%6d ", kth->ktr_pid);
366 if (kth->ktr_version > KTRFACv0)
367 col += printf("%6d ", kth->ktr_lid);
368 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
369 if (timestamp) {
370 if (timestamp == 2) {
371 if (kth->ktr_version == KTRFACv0) {
372 if (prevtime.tv.tv_sec == 0)
373 temp.tv.tv_sec = temp.tv.tv_usec = 0;
374 else
375 timersub(&kth->ktr_tv,
376 &prevtime.tv, &temp.tv);
377 prevtime.tv = kth->ktr_tv;
378 } else {
379 if (prevtime.ts.tv_sec == 0)
380 temp.ts.tv_sec = temp.ts.tv_nsec = 0;
381 else
382 timespecsub(&kth->ktr_time,
383 &prevtime.ts, &temp.ts);
384 prevtime.ts = kth->ktr_time;
385 }
386 } else {
387 if (kth->ktr_version == KTRFACv0)
388 temp.tv = kth->ktr_tv;
389 else
390 temp.ts = kth->ktr_time;
391 }
392 if (kth->ktr_version == KTRFACv0)
393 col += printf("%ld.%06ld ",
394 (long)temp.tv.tv_sec, (long)temp.tv.tv_usec);
395 else
396 col += printf("%ld.%09ld ",
397 (long)temp.ts.tv_sec, (long)temp.ts.tv_nsec);
398 }
399 col += printf("%-4s ", type);
400 return col;
401 }
402
403 static void
404 output_long(u_long it, int as_x)
405 {
406 if (cur_emul->flags & EMUL_FLAG_NETBSD32)
407 printf(as_x ? "%#x" : "%d", (u_int)it);
408 else
409 printf(as_x ? "%#lx" : "%ld", it);
410 }
411
412 static void
413 ioctldecode(u_long cmd)
414 {
415 char dirbuf[4], *dir = dirbuf;
416
417 if (cmd & IOC_IN)
418 *dir++ = 'W';
419 if (cmd & IOC_OUT)
420 *dir++ = 'R';
421 *dir = '\0';
422
423 printf(",_IO%s('%c',", dirbuf, (int) ((cmd >> 8) & 0xff));
424 output_long(cmd & 0xff, decimal == 0);
425 if ((cmd & IOC_VOID) == 0) {
426 putchar(',');
427 output_long(IOCPARM_LEN(cmd), decimal == 0);
428 }
429 putchar(')');
430 }
431
432 static void
433 ktrsyscall(struct ktr_syscall *ktr)
434 {
435 int argcount;
436 const struct emulation *emul = cur_emul;
437 register_t *ap;
438 char c;
439 const char *cp;
440 const char *sys_name;
441
442 argcount = ktr->ktr_argsize / sizeof (*ap);
443
444 emul_changed = 0;
445
446 if (numeric ||
447 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) &&
448 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) {
449 sys_name = "?";
450 (void)printf("[%d]", ktr->ktr_code);
451 } else {
452 sys_name = emul->sysnames[ktr->ktr_code];
453 (void)printf("%s", sys_name);
454 }
455 #ifdef _LP64
456 #define NETBSD32_ "netbsd32_"
457 if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
458 size_t len = strlen(NETBSD32_);
459 if (strncmp(sys_name, NETBSD32_, len) == 0)
460 sys_name += len;
461 }
462 #undef NETBSD32_
463 #endif
464
465 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
466 if (argcount) {
467 c = '(';
468 if (plain) {
469 ;
470
471 } else if (strcmp(sys_name, "exit") == 0) {
472 ectx_delete();
473
474 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
475 (void)putchar('(');
476 output_long((long)*ap, !(decimal || small(*ap)));
477 ap++;
478 argcount--;
479 if ((cp = ioctlname(*ap)) != NULL)
480 (void)printf(",%s", cp);
481 else
482 ioctldecode(*ap);
483 ap++;
484 argcount--;
485 c = ',';
486
487 } else if ((strstr(sys_name, "sigaction") != NULL ||
488 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
489 (void)printf("(SIG%s", signame(ap[0], 1));
490 ap += 1;
491 argcount -= 1;
492 c = ',';
493
494 } else if ((strcmp(sys_name, "kill") == 0 ||
495 strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
496 putchar('(');
497 output_long((long)ap[0], !(decimal || small(*ap)));
498 (void)printf(", SIG%s", signame(ap[1], 1));
499 ap += 2;
500 argcount -= 2;
501 c = ',';
502
503 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
504 putchar('(');
505 if (strcmp(emul->name, "linux") == 0) {
506 if ((long)*ap >= 0 && *ap <
507 sizeof(linux_ptrace_ops) /
508 sizeof(linux_ptrace_ops[0]))
509 (void)printf("%s",
510 linux_ptrace_ops[*ap]);
511 else
512 output_long((long)*ap, 1);
513 } else {
514 if ((long)*ap >= 0 && *ap <
515 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
516 (void)printf("%s", ptrace_ops[*ap]);
517 #ifdef PT_MACHDEP_STRINGS
518 else if (*ap >= PT_FIRSTMACH &&
519 *ap - PT_FIRSTMACH <
520 sizeof(ptrace_machdep_ops) /
521 sizeof(ptrace_machdep_ops[0]))
522 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
523 #endif
524 else
525 output_long((long)*ap, 1);
526 }
527 ap++;
528 argcount--;
529 c = ',';
530
531 }
532 while (argcount > 0) {
533 putchar(c);
534 output_long((long)*ap, !(decimal || small(*ap)));
535 ap++;
536 argcount--;
537 c = ',';
538 }
539 (void)putchar(')');
540 }
541 (void)putchar('\n');
542 }
543
544 static void
545 ktrsysret(struct ktr_sysret *ktr, int len)
546 {
547 const struct emulation *emul;
548 int error = ktr->ktr_error;
549 int code = ktr->ktr_code;
550
551 if (emul_changed) {
552 /* In order to get system call name right in execve return */
553 emul = prev_emul;
554 emul_changed = 0;
555 } else
556 emul = cur_emul;
557
558 if ((code >= emul->nsysnames || code < 0 || plain > 1) &&
559 (mach_traps_dispatch(&code, &emul) == 0))
560 (void)printf("[%d] ", code);
561 else
562 (void)printf("%s ", emul->sysnames[code]);
563
564 switch (error) {
565 case 0:
566 rprint(ktr->ktr_retval);
567 if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
568 ktr->ktr_retval_1 != 0) {
569 (void)printf(", ");
570 rprint(ktr->ktr_retval_1);
571 }
572 break;
573
574 default:
575 eprint(error);
576 break;
577 }
578 (void)putchar('\n');
579 }
580
581 static void
582 rprint(register_t ret)
583 {
584
585 if (!plain) {
586 (void)printf("%ld", (long)ret);
587 if (!small(ret))
588 (void)printf("/%#lx", (long)ret);
589 } else {
590 if (decimal || small(ret))
591 (void)printf("%ld", (long)ret);
592 else
593 (void)printf("%#lx", (long)ret);
594 }
595 }
596
597 /*
598 * We print the original emulation's error numerically, but we
599 * translate it to netbsd to print it symbolically.
600 */
601 static void
602 eprint(int e)
603 {
604 int i = e;
605
606 if (cur_emul->errnomap) {
607
608 /* No remapping for ERESTART and EJUSTRETURN */
609 /* Kludge for linux that has negative error numbers */
610 if (cur_emul->errnomap[2] > 0 && e < 0)
611 goto normal;
612
613 for (i = 0; i < cur_emul->nerrnomap; i++)
614 if (e == cur_emul->errnomap[i])
615 break;
616
617 if (i == cur_emul->nerrnomap) {
618 printf("-1 unknown errno %d", e);
619 return;
620 }
621 }
622
623 normal:
624 switch (i) {
625 case ERESTART:
626 (void)printf("RESTART");
627 break;
628
629 case EJUSTRETURN:
630 (void)printf("JUSTRETURN");
631 break;
632
633 default:
634 (void)printf("-1 errno %d", e);
635 if (!plain)
636 (void)printf(" %s", strerror(i));
637 }
638 }
639
640 static void
641 ktrnamei(char *cp, int len)
642 {
643
644 (void)printf("\"%.*s\"\n", len, cp);
645 }
646
647 static void
648 ktremul(char *name, int len, int bufsize)
649 {
650
651 if (len >= bufsize)
652 len = bufsize - 1;
653
654 name[len] = '\0';
655 setemul(name, ktr_header.ktr_pid, 1);
656 emul_changed = 1;
657
658 (void)printf("\"%s\"\n", name);
659 }
660
661 static void
662 hexdump_buf(const void *vdp, int datalen, int word_sz)
663 {
664 const char hex[] = "0123456789abcdef";
665 char chars[16], prev[16];
666 char bytes[16 * 3 + 4];
667 const unsigned char *dp = vdp;
668 const unsigned char *datalim = dp + datalen;
669 const unsigned char *line_end;
670 int off, l = 0, c;
671 char *cp, *bp;
672 int divmask = word_sz - 1; /* block size in bytes */
673 int gdelim = 3; /* gap between blocks */
674 int bsize = 2; /* increment for each byte */
675 int width;
676 int dupl = 0;
677 #if _BYTE_ORDER == _LITTLE_ENDIAN
678 int bswap = word_sz - 1;
679 #else
680 #define bswap 0
681 #endif
682
683 switch (word_sz) {
684 case 2:
685 gdelim = 2;
686 break;
687 case 1:
688 divmask = 7;
689 bsize = 3;
690 gdelim = 1;
691 break;
692 default:
693 break;
694 }
695 width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
696 if (word_sz != 1)
697 width += 2;
698
699 for (off = 0; dp < datalim; off += l) {
700 memset(bytes, ' ', sizeof bytes);
701 line_end = dp + 16;
702 if (line_end >= datalim) {
703 line_end = datalim;
704 dupl |= 1; /* need to print */
705 } else {
706 if (dupl == 0 || memcmp(dp, prev, sizeof chars))
707 dupl |= 1;
708 }
709
710 if (!(dupl & 1)) {
711 /* This is a duplicate of the line above, count 'em */
712 dupl += 2;
713 dp = line_end;
714 continue;
715 }
716
717 if (dupl > 3) {
718 /* previous line as a duplicate */
719 if (dupl == 5)
720 /* Only one duplicate, print line */
721 printf("\t%-5.3x%.*s%.*s\n",
722 off - l, width, bytes, l, chars);
723 else
724 printf("\t%.*s\n",
725 snprintf(NULL, 0, "%3x", off), "*****");
726 }
727
728 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
729 c = *dp++;
730 prev[l] = c;
731 if ((l & divmask) == 0)
732 bp += gdelim;
733 bp[(l ^ bswap) * bsize] = hex[c >> 4];
734 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
735 *cp++ = isgraph(c) ? c : '.';
736 }
737
738 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
739 dupl = 2;
740 }
741 }
742
743 static void
744 visdump_buf(const void *vdp, int datalen, int col)
745 {
746 const unsigned char *dp = vdp;
747 char *cp;
748 int width;
749 char visbuf[5];
750 static int screenwidth = 0;
751
752 if (screenwidth == 0) {
753 struct winsize ws;
754
755 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
756 ws.ws_col > 8)
757 screenwidth = ws.ws_col;
758 else
759 screenwidth = 80;
760 }
761
762 (void)printf("\"");
763 col++;
764 for (; datalen > 0; datalen--, dp++) {
765 (void)svis(visbuf, *dp, VIS_CSTYLE,
766 datalen > 1 ? *(dp + 1) : 0, "\"");
767 cp = visbuf;
768 /*
769 * Keep track of printables and
770 * space chars (like fold(1)).
771 */
772 if (col == 0) {
773 (void)putchar('\t');
774 col = 8;
775 }
776 switch (*cp) {
777 case '\n':
778 col = 0;
779 (void)putchar('\n');
780 continue;
781 case '\t':
782 width = 8 - (col & 07);
783 break;
784 default:
785 width = strlen(cp);
786 }
787 if (col + width > (screenwidth - 2)) {
788 (void)printf("\\\n\t");
789 col = 8;
790 if (*cp == '\t')
791 width = 8;
792 }
793 col += width;
794 do {
795 (void)putchar(*cp++);
796 } while (*cp);
797 }
798 if (col == 0)
799 (void)printf(" ");
800 (void)printf("\"\n");
801 }
802
803 static void
804 ktrgenio(struct ktr_genio *ktr, int len)
805 {
806 int datalen = len - sizeof (struct ktr_genio);
807 char *dp = (char *)ktr + sizeof (struct ktr_genio);
808
809 if (ktr->ktr_fd != -1)
810 printf("fd %d ", ktr->ktr_fd);
811 printf("%s %d bytes\n",
812 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
813 if (maxdata == 0)
814 return;
815 if (maxdata > 0 && datalen > maxdata)
816 datalen = maxdata;
817 if (word_size) {
818 hexdump_buf(dp, datalen, word_size);
819 return;
820 }
821 (void)printf(" ");
822 visdump_buf(dp, datalen, 7);
823 }
824
825 static void
826 ktrpsig(void *v, int len)
827 {
828 int signo, first;
829 struct {
830 struct ktr_psig ps;
831 siginfo_t si;
832 } *psig = v;
833 siginfo_t *si = &psig->si;
834 const char *code;
835
836 (void)printf("SIG%s ", signame(psig->ps.signo, 0));
837 if (psig->ps.action == SIG_DFL)
838 (void)printf("SIG_DFL");
839 else {
840 (void)printf("caught handler=%p mask=(", psig->ps.action);
841 first = 1;
842 for (signo = 1; signo < NSIG; signo++) {
843 if (sigismember(&psig->ps.mask, signo)) {
844 if (first)
845 first = 0;
846 else
847 (void)printf(",");
848 (void)printf("%d", signo);
849 }
850 }
851 (void)printf(")");
852 }
853 switch (len) {
854 case sizeof(struct ktr_psig):
855 if (psig->ps.code)
856 printf(" code=0x%x", psig->ps.code);
857 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
858 return;
859 case sizeof(*psig):
860 if (si->si_code == 0) {
861 printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
862 si->si_pid, si->si_uid);
863 return;
864 }
865
866 if (si->si_code < 0) {
867 switch (si->si_code) {
868 case SI_TIMER:
869 printf(": code=SI_TIMER sigval %p)\n",
870 si->si_sigval.sival_ptr);
871 return;
872 case SI_QUEUE:
873 code = "SI_QUEUE";
874 break;
875 case SI_ASYNCIO:
876 code = "SI_ASYNCIO";
877 break;
878 case SI_MESGQ:
879 code = "SI_MESGQ";
880 break;
881 default:
882 code = NULL;
883 break;
884 }
885 if (code)
886 printf(": code=%s unimplemented)\n", code);
887 else
888 printf(": code=%d unimplemented)\n",
889 si->si_code);
890 return;
891 }
892
893 code = siginfocodename(si->si_signo, si->si_code);
894 switch (si->si_signo) {
895 case SIGCHLD:
896 printf(": code=%s child pid=%d, uid=%d, "
897 " status=%u, utime=%lu, stime=%lu)\n",
898 code, si->si_pid,
899 si->si_uid, si->si_status,
900 (unsigned long) si->si_utime,
901 (unsigned long) si->si_stime);
902 return;
903 case SIGILL:
904 case SIGFPE:
905 case SIGSEGV:
906 case SIGBUS:
907 case SIGTRAP:
908 printf(": code=%s, addr=%p, trap=%d)\n",
909 code, si->si_addr, si->si_trap);
910 return;
911 case SIGIO:
912 printf(": code=%s, fd=%d, band=%lx)\n",
913 code, si->si_fd, si->si_band);
914 return;
915 default:
916 printf(": code=%s, errno=%d)\n",
917 code, si->si_errno);
918 return;
919 }
920 /*NOTREACHED*/
921 default:
922 warnx("Unhandled size %d for ktrpsig\n", len);
923 break;
924 }
925 }
926
927 static void
928 ktrcsw(struct ktr_csw *cs)
929 {
930
931 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
932 cs->user ? "user" : "kernel");
933 }
934
935 static void
936 ktruser(struct ktr_user *usr, int len)
937 {
938 int i;
939 unsigned char *dta;
940
941 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
942 dta = (unsigned char *)usr;
943 for (i = sizeof(struct ktr_user); i < len; i++)
944 printf("%02x", (unsigned int) dta[i]);
945 printf("\"\n");
946 }
947
948 static void
949 ktrmmsg(struct ktr_mmsg *mmsg, int len)
950 {
951 const char *service_name;
952 const char *reply;
953 int id;
954
955 id = mmsg->ktr_id;
956 if ((id / 100) % 2) { /* Message reply */
957 reply = " reply";
958 id -= 100;
959 } else {
960 reply = "";
961 }
962
963 if ((service_name = mach_service_name(id)) != NULL)
964 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
965 else
966 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
967
968 hexdump_buf(mmsg, len, word_size ? word_size : 4);
969 }
970
971 static void
972 ktrmool(struct ktr_mool *mool, int len)
973 {
974 size_t size = mool->size;
975
976 printf("%ld/0x%lx bytes at %p\n",
977 (u_long)size, (u_long)size, mool->uaddr);
978 mool++;
979 hexdump_buf(mool, size, word_size ? word_size : 4);
980 }
981
982 static void
983 ktrmib(int *namep, int len)
984 {
985 int i;
986
987 for (i = 0; i < (len / sizeof(*namep)); i++)
988 printf("%s%d", (i == 0) ? "" : ".", namep[i]);
989 printf("\n");
990 }
991
992 static const char *
993 signame(long sig, int xlat)
994 {
995 static char buf[64];
996
997 if (sig == 0)
998 return " 0";
999 else if (sig < 0 || sig >= NSIG) {
1000 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
1001 return buf;
1002 } else
1003 return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
1004 cur_emul->signalmap[sig] : sig];
1005 }
1006
1007 static void
1008 usage(void)
1009 {
1010
1011 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] "
1012 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
1013 "[-x | -X size] [file]\n", getprogname());
1014 exit(1);
1015 }
1016