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