kdump.c revision 1.130 1 /* $NetBSD: kdump.c,v 1.130 2018/04/29 18:00:31 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\
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.130 2018/04/29 18:00:31 christos Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #define _KMEMUSER /* To get the pseudo errors defined */
49 #include <sys/errno.h>
50 #undef _KMEMUSER
51 #include <sys/mman.h>
52 #include <sys/time.h>
53 #include <sys/uio.h>
54 #include <sys/ktrace.h>
55 #include <sys/ioctl.h>
56 #include <sys/ptrace.h>
57 #include <sys/socket.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <signal.h>
62 #include <stddef.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <vis.h>
68 #include <util.h>
69
70 #include "ktrace.h"
71 #include "setemul.h"
72
73 #include <sys/syscall.h>
74
75 #define TIMESTAMP_NONE 0x0
76 #define TIMESTAMP_ABSOLUTE 0x1
77 #define TIMESTAMP_ELAPSED 0x2
78 #define TIMESTAMP_RELATIVE 0x4
79
80 static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
81 static int word_size = 0;
82 static pid_t do_pid = -1;
83 static const char *tracefile = NULL;
84 static struct ktr_header ktr_header;
85 static int emul_changed = 0;
86
87 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
88 #define small(v) (((long)(v) >= 0) && ((long)(v) < 10))
89
90 static const char * const ptrace_ops[] = {
91 PT_STRINGS
92 };
93
94 #ifdef PT_MACHDEP_STRINGS
95 static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
96 #endif
97
98 static const char * const linux_ptrace_ops[] = {
99 "PTRACE_TRACEME",
100 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
101 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
102 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
103 NULL, NULL,
104 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
105 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
106 NULL, NULL, NULL, NULL, NULL, NULL,
107 "PTRACE_SYSCALL",
108 };
109
110 static int fread_tail(void *, size_t, size_t);
111 static int dumpheader(struct ktr_header *);
112 static int output_ts(const struct timespec *);
113 static void output_long(u_long, int);
114 static void ioctldecode(u_long);
115 static void ktrsyscall(struct ktr_syscall *);
116 static void ktrsysret(struct ktr_sysret *, int);
117 static void ktrnamei(char *, int);
118 static void ktremul(char *, size_t, size_t);
119 static void ktrgenio(struct ktr_genio *, int);
120 static void ktrpsig(void *, int);
121 static void ktrcsw(struct ktr_csw *);
122 static void ktruser(struct ktr_user *, int);
123 static void ktrmib(int *, int);
124 static void ktrexecfd(struct ktr_execfd *);
125 static void usage(void) __dead;
126 static void eprint(int);
127 static void rprint(register_t);
128 static const char *signame(long, int);
129 static void hexdump_buf(const void *, int, int);
130 static void visdump_buf(const void *, int, int);
131
132 int
133 main(int argc, char **argv)
134 {
135 unsigned int ktrlen, size;
136 int ch;
137 void *m;
138 int trpoints = 0;
139 int trset = 0;
140 const char *emul_name = "netbsd";
141 int col;
142 char *cp;
143
144 setprogname(argv[0]);
145
146 if (strcmp(getprogname(), "ioctlname") == 0) {
147 int i;
148
149 while ((ch = getopt(argc, argv, "e:")) != -1)
150 switch (ch) {
151 case 'e':
152 emul_name = optarg;
153 break;
154 default:
155 usage();
156 break;
157 }
158 setemul(emul_name, 0, 0);
159 argv += optind;
160 argc -= optind;
161
162 if (argc < 1)
163 usage();
164
165 for (i = 0; i < argc; i++) {
166 ioctldecode(strtoul(argv[i], NULL, 0));
167 (void)putchar('\n');
168 }
169 return 0;
170 }
171
172 timestamp = TIMESTAMP_NONE;
173
174 while ((ch = getopt(argc, argv, "Ee:f:dlm:Nnp:RTt:xX:")) != -1) {
175 switch (ch) {
176 case 'E':
177 timestamp |= TIMESTAMP_ELAPSED;
178 break;
179 case 'e':
180 emul_name = strdup(optarg); /* it's safer to copy it */
181 break;
182 case 'f':
183 tracefile = optarg;
184 break;
185 case 'd':
186 decimal = 1;
187 break;
188 case 'l':
189 tail = 1;
190 break;
191 case 'p':
192 do_pid = strtoul(optarg, &cp, 0);
193 if (*cp != 0)
194 errx(1,"invalid number %s", optarg);
195 break;
196 case 'm':
197 maxdata = strtoul(optarg, &cp, 0);
198 if (*cp != 0)
199 errx(1,"invalid number %s", optarg);
200 break;
201 case 'N':
202 numeric++;
203 break;
204 case 'n':
205 plain++;
206 break;
207 case 'R':
208 timestamp |= TIMESTAMP_RELATIVE;
209 break;
210 case 'T':
211 timestamp |= TIMESTAMP_ABSOLUTE;
212 break;
213 case 't':
214 trset = 1;
215 trpoints = getpoints(trpoints, optarg);
216 if (trpoints < 0)
217 errx(1, "unknown trace point in %s", optarg);
218 break;
219 case 'x':
220 word_size = 1;
221 break;
222 case 'X':
223 word_size = strtoul(optarg, &cp, 0);
224 if (*cp != 0 || word_size & (word_size - 1) ||
225 word_size > 16 || word_size <= 0)
226 errx(1, "argument to -X must be "
227 "1, 2, 4, 8 or 16");
228 break;
229 default:
230 usage();
231 }
232 }
233 argv += optind;
234 argc -= optind;
235
236 if (!trset)
237 trpoints = ALL_POINTS;
238
239 if (tracefile == NULL) {
240 if (argc == 1) {
241 tracefile = argv[0];
242 argv++;
243 argc--;
244 } else
245 tracefile = DEF_TRACEFILE;
246 }
247
248 if (argc > 0)
249 usage();
250
251 setemul(emul_name, 0, 0);
252
253 m = malloc(size = 1024);
254 if (m == NULL)
255 errx(1, "malloc: %s", strerror(ENOMEM));
256 if (!freopen(tracefile, "r", stdin))
257 err(1, "%s", tracefile);
258 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
259 if (trpoints & (1 << ktr_header.ktr_type) &&
260 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
261 col = dumpheader(&ktr_header);
262 else
263 col = -1;
264 if ((ktrlen = ktr_header.ktr_len) > INT_MAX)
265 errx(1, "bogus length 0x%x", ktrlen);
266 if (ktrlen > size) {
267 while (ktrlen > size)
268 size *= 2;
269 m = realloc(m, size);
270 if (m == NULL)
271 errx(1, "realloc: %s", strerror(ENOMEM));
272 }
273 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
274 errx(1, "data too short");
275 if (col == -1)
276 continue;
277
278 /* update context to match currently processed record */
279 ectx_sanify(ktr_header.ktr_pid);
280
281 switch (ktr_header.ktr_type) {
282 case KTR_SYSCALL:
283 ktrsyscall(m);
284 break;
285 case KTR_SYSRET:
286 ktrsysret(m, ktrlen);
287 break;
288 case KTR_NAMEI:
289 ktrnamei(m, ktrlen);
290 break;
291 case KTR_GENIO:
292 ktrgenio(m, ktrlen);
293 break;
294 case KTR_PSIG:
295 ktrpsig(m, ktrlen);
296 break;
297 case KTR_CSW:
298 ktrcsw(m);
299 break;
300 case KTR_EMUL:
301 ktremul(m, ktrlen, size);
302 break;
303 case KTR_USER:
304 ktruser(m, ktrlen);
305 break;
306 case KTR_EXEC_ARG:
307 case KTR_EXEC_ENV:
308 visdump_buf(m, ktrlen, col);
309 break;
310 case KTR_EXEC_FD:
311 ktrexecfd(m);
312 break;
313 case KTR_MIB:
314 ktrmib(m, ktrlen);
315 break;
316 default:
317 putchar('\n');
318 hexdump_buf(m, ktrlen, word_size ? word_size : 1);
319 }
320 if (tail)
321 (void)fflush(stdout);
322 }
323 return (0);
324 }
325
326 static int
327 fread_tail(void *buf, size_t num, size_t size)
328 {
329 int i;
330
331 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
332 (void)sleep(1);
333 clearerr(stdin);
334 }
335 return (i);
336 }
337
338 static int
339 dumpheader(struct ktr_header *kth)
340 {
341 char unknown[64];
342 const char *type;
343 static struct timespec starttime, prevtime;
344 struct timespec temp;
345 int col;
346
347 if (__predict_false(kth->ktr_version != KTRFAC_VERSION(KTRFACv2)))
348 errx(EXIT_FAILURE, "Unsupported ktrace version %x",
349 kth->ktr_version);
350
351 switch (kth->ktr_type) {
352 case KTR_SYSCALL:
353 type = "CALL";
354 break;
355 case KTR_SYSRET:
356 type = "RET ";
357 break;
358 case KTR_NAMEI:
359 type = "NAMI";
360 break;
361 case KTR_GENIO:
362 type = "GIO ";
363 break;
364 case KTR_PSIG:
365 type = "PSIG";
366 break;
367 case KTR_CSW:
368 type = "CSW ";
369 break;
370 case KTR_EMUL:
371 type = "EMUL";
372 break;
373 case KTR_USER:
374 type = "MISC";
375 break;
376 case KTR_EXEC_ENV:
377 type = "ENV";
378 break;
379 case KTR_EXEC_ARG:
380 type = "ARG";
381 break;
382 case KTR_EXEC_FD:
383 type = "FD";
384 break;
385 case KTR_SAUPCALL:
386 type = "SAU";
387 break;
388 case KTR_MIB:
389 type = "MIB";
390 break;
391 default:
392 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
393 kth->ktr_type);
394 type = unknown;
395 }
396
397 col = printf("%6d %6d ", kth->ktr_pid, kth->ktr_lid);
398 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm);
399 if (timestamp) {
400 if (timestamp & TIMESTAMP_ABSOLUTE) {
401 temp.tv_sec = kth->ktr_ts.tv_sec;
402 temp.tv_nsec = kth->ktr_ts.tv_nsec;
403 col += output_ts(&temp);
404 }
405
406 if (timestamp & TIMESTAMP_ELAPSED) {
407 if (starttime.tv_sec == 0) {
408 starttime.tv_sec = kth->ktr_ts.tv_sec;
409 starttime.tv_nsec = kth->ktr_ts.tv_nsec;
410 temp.tv_sec = temp.tv_nsec = 0;
411 } else
412 timespecsub(&kth->ktr_ts, &starttime, &temp);
413 col += output_ts(&temp);
414 }
415
416 if (timestamp & TIMESTAMP_RELATIVE) {
417 if (prevtime.tv_sec == 0)
418 temp.tv_sec = temp.tv_nsec = 0;
419 else
420 timespecsub(&kth->ktr_ts, &prevtime, &temp);
421 prevtime.tv_sec = kth->ktr_ts.tv_sec;
422 prevtime.tv_nsec = kth->ktr_ts.tv_nsec;
423 col += output_ts(&temp);
424 }
425 }
426 col += printf("%-4s ", type);
427 return col;
428 }
429
430 static int
431 output_ts(const struct timespec *ts)
432 {
433 int col;
434
435 if (__predict_true(ts->tv_sec >= 0))
436 col = printf("%lld.%09ld ",
437 (long long)ts->tv_sec, (long)ts->tv_nsec);
438 else {
439 /*
440 * The time represented by a timespec object ts is always
441 *
442 * ts.tv_sec + ts.tv_nsec * 1e-9
443 *
444 * where ts.tv_sec may be negative but ts.tv_nsec is
445 * always in [0, 1e9). So, for example, -1/4 second is
446 * represented by the struct timespec object
447 *
448 * { .tv_sec = -1, .tv_nsec = 750000000 }
449 */
450 const struct timespec zero_ts = { 0, 0 };
451 struct timespec abs_ts;
452 timespecsub(&zero_ts, ts, &abs_ts);
453 col = printf("-%lld.%09ld ",
454 (long long)abs_ts.tv_sec, (long)abs_ts.tv_nsec);
455 }
456 return col;
457 }
458
459 static void
460 output_long(u_long it, int as_x)
461 {
462 if (cur_emul->flags & EMUL_FLAG_NETBSD32)
463 printf(as_x ? "%#x" : "%d", (u_int)it);
464 else
465 printf(as_x ? "%#lx" : "%ld", it);
466 }
467
468 static const char *
469 fcntlname(u_long cmd)
470 {
471 #define FCNTLCASE(a) case a: return # a
472 switch (cmd) {
473 FCNTLCASE(F_DUPFD);
474 FCNTLCASE(F_GETFD);
475 FCNTLCASE(F_SETFD);
476 FCNTLCASE(F_GETFL);
477 FCNTLCASE(F_SETFL);
478 FCNTLCASE(F_GETOWN);
479 FCNTLCASE(F_SETOWN);
480 FCNTLCASE(F_GETLK);
481 FCNTLCASE(F_SETLK);
482 FCNTLCASE(F_SETLKW);
483 FCNTLCASE(F_CLOSEM);
484 FCNTLCASE(F_MAXFD);
485 FCNTLCASE(F_DUPFD_CLOEXEC);
486 FCNTLCASE(F_GETNOSIGPIPE);
487 FCNTLCASE(F_SETNOSIGPIPE);
488 default:
489 return NULL;
490 }
491 }
492
493 static void
494 ioctldecode(u_long cmd)
495 {
496 char dirbuf[4], *dir = dirbuf;
497 int c;
498
499 if (cmd & IOC_IN)
500 *dir++ = 'W';
501 if (cmd & IOC_OUT)
502 *dir++ = 'R';
503 *dir = '\0';
504
505 c = (cmd >> 8) & 0xff;
506 if (isprint(c))
507 printf("_IO%s('%c',", dirbuf, c);
508 else
509 printf("_IO%s(0x%02x,", dirbuf, c);
510 output_long(cmd & 0xff, decimal == 0);
511 if ((cmd & IOC_VOID) == 0) {
512 putchar(',');
513 output_long(IOCPARM_LEN(cmd), decimal == 0);
514 }
515 putchar(')');
516 }
517
518 static void
519 putprot(int pr)
520 {
521 const char *s = "";
522
523 if (pr == PROT_NONE) {
524 fputs("PROT_NONE", stdout);
525 return;
526 }
527
528 if (pr & PROT_READ) {
529 fputs("PROT_READ", stdout);
530 s = "|";
531 pr &= ~PROT_READ;
532 }
533
534 if (pr & PROT_WRITE) {
535 printf("%sPROT_WRITE", s);
536 pr &= ~PROT_WRITE;
537 s = "|";
538 }
539 if (pr & PROT_EXEC) {
540 printf("%sPROT_EXEC", s);
541 pr &= ~PROT_EXEC;
542 s = "|";
543 }
544 if (pr) {
545 printf("%s%#lx", s, (long)pr);
546 }
547 }
548
549 static void
550 ktrsyscall(struct ktr_syscall *ktr)
551 {
552 int argcount;
553 const struct emulation *emul = cur_emul;
554 register_t *ap;
555 char c;
556 const char *cp;
557 const char *sys_name;
558
559 argcount = ktr->ktr_argsize / sizeof (*ap);
560
561 emul_changed = 0;
562
563 if (numeric ||
564 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0))) {
565 sys_name = "?";
566 (void)printf("[%d]", ktr->ktr_code);
567 } else {
568 sys_name = emul->sysnames[ktr->ktr_code];
569 (void)printf("%s", sys_name);
570 }
571 #ifdef _LP64
572 #define NETBSD32_ "netbsd32_"
573 if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
574 size_t len = strlen(NETBSD32_);
575 if (strncmp(sys_name, NETBSD32_, len) == 0)
576 sys_name += len;
577 }
578 #undef NETBSD32_
579 #endif
580
581 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
582 if (argcount) {
583 c = '(';
584 if (plain) {
585 ;
586
587 } else if (strcmp(sys_name, "exit_group") == 0 ||
588 (strcmp(emul->name, "linux") != 0 &&
589 strcmp(emul->name, "linux32") != 0 &&
590 strcmp(sys_name, "exit") == 0)) {
591 ectx_delete();
592
593 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
594 (void)putchar('(');
595 output_long((long)*ap, !(decimal || small(*ap)));
596 ap++;
597 argcount--;
598 if ((cp = ioctlname(*ap)) != NULL)
599 (void)printf(",%s", cp);
600 else {
601 (void)putchar(',');
602 ioctldecode(*ap);
603 }
604 ap++;
605 argcount--;
606 c = ',';
607
608 } else if (strcmp(sys_name, "fcntl") == 0 && argcount >= 2) {
609 (void)putchar('(');
610 output_long((long)*ap, !(decimal || small(*ap)));
611 ap++;
612 argcount--;
613 if ((cp = fcntlname(*ap)) != NULL)
614 (void)printf(",%s", cp);
615 else {
616 (void)printf(",%#lx", (unsigned long)*ap);
617 }
618 ap++;
619 argcount--;
620 c = ',';
621 } else if ((strstr(sys_name, "sigaction") != NULL ||
622 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
623 (void)printf("(SIG%s", signame(ap[0], 1));
624 ap += 1;
625 argcount -= 1;
626 c = ',';
627
628 } else if ((strcmp(sys_name, "kill") == 0 ||
629 strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
630 putchar('(');
631 output_long((long)ap[0], !(decimal || small(*ap)));
632 (void)printf(", SIG%s", signame(ap[1], 1));
633 ap += 2;
634 argcount -= 2;
635 c = ',';
636 } else if (strcmp(sys_name, "mprotect") == 0 && argcount >= 3) {
637 putchar('(');
638 output_long((long)ap[0], !(decimal || small(ap[0])));
639 c = ',';
640 putchar(c);
641 output_long((long)ap[1], !(decimal || small(ap[1])));
642 putchar(c);
643 putprot(ap[2]);
644 ap += 3;
645 argcount -= 3;
646 c = ',';
647 } else if (strcmp(sys_name, "mmap") == 0 && argcount >= 6) {
648 char buf[1024];
649 putchar('(');
650 output_long((long)ap[0], !(decimal || small(ap[0])));
651 c = ',';
652 putchar(c);
653 output_long((long)ap[1], !(decimal || small(ap[1])));
654 putchar(c);
655 putprot(ap[2]);
656 snprintb(buf, sizeof(buf), MAP_FMT, ap[3]);
657 printf(",%s", buf);
658 ap += 4;
659 argcount -= 4;
660 c = ',';
661 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
662 putchar('(');
663 if (strcmp(emul->name, "linux") == 0 ||
664 strcmp(emul->name, "linux32") == 0) {
665 if ((long)*ap >= 0 && *ap <
666 (register_t)(sizeof(linux_ptrace_ops) /
667 sizeof(linux_ptrace_ops[0])))
668 (void)printf("%s",
669 linux_ptrace_ops[*ap]);
670 else
671 output_long((long)*ap, 1);
672 } else {
673 if ((long)*ap >= 0 && *ap < (register_t)
674 __arraycount(ptrace_ops))
675 (void)printf("%s", ptrace_ops[*ap]);
676 #ifdef PT_MACHDEP_STRINGS
677 else if (*ap >= PT_FIRSTMACH &&
678 *ap - PT_FIRSTMACH < (register_t)
679 __arraycount(ptrace_machdep_ops))
680 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
681 #endif
682 else
683 output_long((long)*ap, 1);
684 }
685 ap++;
686 argcount--;
687 c = ',';
688
689 }
690 while (argcount > 0) {
691 putchar(c);
692 output_long((long)*ap, !(decimal || small(*ap)));
693 ap++;
694 argcount--;
695 c = ',';
696 }
697 (void)putchar(')');
698 }
699 (void)putchar('\n');
700 }
701
702 static void
703 ktrsysret(struct ktr_sysret *ktr, int len)
704 {
705 const struct emulation *emul;
706 int error = ktr->ktr_error;
707 int code = ktr->ktr_code;
708
709 if (emul_changed) {
710 /* In order to get system call name right in execve return */
711 emul = prev_emul;
712 emul_changed = 0;
713 } else
714 emul = cur_emul;
715
716 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1)))
717 (void)printf("[%d] ", code);
718 else
719 (void)printf("%s ", emul->sysnames[code]);
720
721 switch (error) {
722 case 0:
723 rprint(ktr->ktr_retval);
724 if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) &&
725 ktr->ktr_retval_1 != 0) {
726 (void)printf(", ");
727 rprint(ktr->ktr_retval_1);
728 }
729 break;
730
731 default:
732 eprint(error);
733 break;
734 }
735 (void)putchar('\n');
736 }
737
738 static void
739 ktrexecfd(struct ktr_execfd *ktr)
740 {
741 static const char *dnames[] = { DTYPE_NAMES };
742 if (ktr->ktr_dtype < __arraycount(dnames))
743 printf("%s %d\n", dnames[ktr->ktr_dtype], ktr->ktr_fd);
744 else
745 printf("UNKNOWN(%u) %d\n", ktr->ktr_dtype, ktr->ktr_fd);
746 }
747
748 static void
749 rprint(register_t ret)
750 {
751
752 if (!plain) {
753 output_long(ret, 0);
754 if (!small(ret)) {
755 putchar('/');
756 output_long(ret, 1);
757 }
758 } else {
759 output_long(ret, !(decimal || small(ret)));
760 }
761 }
762
763 /*
764 * We print the original emulation's error numerically, but we
765 * translate it to netbsd to print it symbolically.
766 */
767 static void
768 eprint(int e)
769 {
770 int i = e;
771
772 if (cur_emul->errnomap) {
773
774 /* No remapping for ERESTART and EJUSTRETURN */
775 /* Kludge for linux that has negative error numbers */
776 if (cur_emul->errnomap[2] > 0 && e < 0)
777 goto normal;
778
779 for (i = 0; i < cur_emul->nerrnomap; i++)
780 if (e == cur_emul->errnomap[i])
781 break;
782
783 if (i == cur_emul->nerrnomap) {
784 printf("-1 unknown errno %d", e);
785 return;
786 }
787 }
788
789 normal:
790 switch (i) {
791 case ERESTART:
792 (void)printf("RESTART");
793 break;
794
795 case EJUSTRETURN:
796 (void)printf("JUSTRETURN");
797 break;
798
799 default:
800 (void)printf("-1 errno %d", e);
801 if (!plain)
802 (void)printf(" %s", strerror(i));
803 }
804 }
805
806 static void
807 ktrnamei(char *cp, int len)
808 {
809
810 (void)printf("\"%.*s\"\n", len, cp);
811 }
812
813 static void
814 ktremul(char *name, size_t len, size_t bufsize)
815 {
816
817 if (len >= bufsize)
818 len = bufsize - 1;
819
820 name[len] = '\0';
821 setemul(name, ktr_header.ktr_pid, 1);
822 emul_changed = 1;
823
824 (void)printf("\"%s\"\n", name);
825 }
826
827 static void
828 hexdump_buf(const void *vdp, int datalen, int word_sz)
829 {
830 const char hex[] = "0123456789abcdef";
831 char chars[16], prev[16];
832 char bytes[16 * 3 + 4];
833 const unsigned char *dp = vdp;
834 const unsigned char *datalim = dp + datalen;
835 const unsigned char *line_end;
836 int off, l = 0, c;
837 char *cp, *bp;
838 int divmask = word_sz - 1; /* block size in bytes */
839 int gdelim = 3; /* gap between blocks */
840 int bsize = 2; /* increment for each byte */
841 int width;
842 int dupl = 0;
843 #if _BYTE_ORDER == _LITTLE_ENDIAN
844 int bswap = word_sz - 1;
845 #else
846 #define bswap 0
847 #endif
848
849 switch (word_sz) {
850 case 2:
851 gdelim = 2;
852 break;
853 case 1:
854 divmask = 7;
855 bsize = 3;
856 gdelim = 1;
857 break;
858 default:
859 break;
860 }
861 width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
862 if (word_sz != 1)
863 width += 2;
864
865 for (off = 0; dp < datalim; off += l) {
866 memset(bytes, ' ', sizeof bytes);
867 line_end = dp + 16;
868 if (line_end >= datalim) {
869 line_end = datalim;
870 dupl |= 1; /* need to print */
871 } else {
872 if (dupl == 0 || memcmp(dp, prev, sizeof chars))
873 dupl |= 1;
874 }
875
876 if (!(dupl & 1)) {
877 /* This is a duplicate of the line above, count 'em */
878 dupl += 2;
879 dp = line_end;
880 continue;
881 }
882
883 if (dupl > 3) {
884 /* previous line as a duplicate */
885 if (dupl == 5)
886 /* Only one duplicate, print line */
887 printf("\t%-5.3x%.*s%.*s\n",
888 off - l, width, bytes, l, chars);
889 else
890 printf("\t%.*s\n",
891 snprintf(NULL, 0, "%3x", off), "*****");
892 }
893
894 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
895 c = *dp++;
896 prev[l] = c;
897 if ((l & divmask) == 0)
898 bp += gdelim;
899 bp[(l ^ bswap) * bsize] = hex[c >> 4];
900 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
901 *cp++ = isgraph(c) ? c : '.';
902 }
903
904 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
905 dupl = 2;
906 }
907 }
908
909 static void
910 visdump_buf(const void *vdp, int datalen, int col)
911 {
912 const unsigned char *dp = vdp;
913 char *cp;
914 int width;
915 char visbuf[5];
916 static int screenwidth = 0;
917
918 if (screenwidth == 0) {
919 struct winsize ws;
920
921 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
922 ws.ws_col > 8)
923 screenwidth = ws.ws_col;
924 else
925 screenwidth = 80;
926 }
927
928 (void)printf("\"");
929 col++;
930 for (; datalen > 0; datalen--, dp++) {
931 (void)svis(visbuf, *dp, VIS_CSTYLE,
932 datalen > 1 ? *(dp + 1) : 0, "\"\n");
933 cp = visbuf;
934 /*
935 * Keep track of printables and
936 * space chars (like fold(1)).
937 */
938 if (col == 0) {
939 (void)putchar('\t');
940 col = 8;
941 }
942 switch (*cp) {
943 case '\n':
944 col = 0;
945 (void)putchar('\n');
946 continue;
947 case '\t':
948 width = 8 - (col & 07);
949 break;
950 default:
951 width = strlen(cp);
952 }
953 if (col + width > (screenwidth - 2)) {
954 (void)printf("\\\n\t");
955 col = 8;
956 if (*cp == '\t')
957 width = 8;
958 }
959 col += width;
960 do {
961 (void)putchar(*cp++);
962 } while (*cp);
963 }
964 if (col == 0)
965 (void)printf(" ");
966 (void)printf("\"\n");
967 }
968
969 static void
970 ktrgenio(struct ktr_genio *ktr, int len)
971 {
972 int datalen = len - sizeof (struct ktr_genio);
973 char *dp = (char *)ktr + sizeof (struct ktr_genio);
974
975 if (ktr->ktr_fd != -1)
976 printf("fd %d ", ktr->ktr_fd);
977 printf("%s %d bytes\n",
978 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
979 if (maxdata == 0)
980 return;
981 if (maxdata > 0 && datalen > maxdata)
982 datalen = maxdata;
983 if (word_size) {
984 hexdump_buf(dp, datalen, word_size);
985 return;
986 }
987 (void)printf(" ");
988 visdump_buf(dp, datalen, 7);
989 }
990
991 static void
992 ktrpsig(void *v, int len)
993 {
994 int signo, first;
995 struct {
996 struct ktr_psig ps;
997 siginfo_t si;
998 } *psig = v;
999 siginfo_t *si = &psig->si;
1000 const char *code;
1001
1002 (void)printf("SIG%s ", signame(psig->ps.signo, 0));
1003 if (psig->ps.action == SIG_DFL)
1004 (void)printf("SIG_DFL");
1005 else {
1006 (void)printf("caught handler=%p mask=(", psig->ps.action);
1007 first = 1;
1008 for (signo = 1; signo < NSIG; signo++) {
1009 if (sigismember(&psig->ps.mask, signo)) {
1010 if (first)
1011 first = 0;
1012 else
1013 (void)printf(",");
1014 (void)printf("%d", signo);
1015 }
1016 }
1017 (void)printf(")");
1018 }
1019 switch (len) {
1020 case sizeof(struct ktr_psig):
1021 if (psig->ps.code)
1022 printf(" code=0x%x", psig->ps.code);
1023 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
1024 return;
1025 case sizeof(*psig):
1026 if (si->si_code == 0) {
1027 printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
1028 si->si_pid, si->si_uid);
1029 return;
1030 }
1031
1032 if (si->si_code < 0) {
1033 switch (si->si_code) {
1034 case SI_TIMER:
1035 case SI_QUEUE:
1036 printf(": code=%s sent by pid=%d, uid=%d with "
1037 "sigval %p)\n", si->si_code == SI_TIMER ?
1038 "SI_TIMER" : "SI_QUEUE", si->si_pid,
1039 si->si_uid, si->si_value.sival_ptr);
1040 return;
1041 case SI_ASYNCIO:
1042 case SI_MESGQ:
1043 printf(": code=%s with sigval %p)\n",
1044 si->si_code == SI_ASYNCIO ?
1045 "SI_ASYNCIO" : "SI_MESGQ",
1046 si->si_value.sival_ptr);
1047 return;
1048 case SI_LWP:
1049 printf(": code=SI_LWP sent by pid=%d, "
1050 "uid=%d)\n", si->si_pid, si->si_uid);
1051 return;
1052 default:
1053 code = NULL;
1054 break;
1055 }
1056 if (code)
1057 printf(": code=%s unimplemented)\n", code);
1058 else
1059 printf(": code=%d unimplemented)\n",
1060 si->si_code);
1061 return;
1062 }
1063
1064 if (si->si_code == SI_NOINFO) {
1065 printf(": code=SI_NOINFO\n");
1066 return;
1067 }
1068
1069 code = siginfocodename(si->si_signo, si->si_code);
1070 switch (si->si_signo) {
1071 case SIGCHLD:
1072 printf(": code=%s child pid=%d, uid=%d, "
1073 " status=%u, utime=%lu, stime=%lu)\n",
1074 code, si->si_pid,
1075 si->si_uid, si->si_status,
1076 (unsigned long) si->si_utime,
1077 (unsigned long) si->si_stime);
1078 return;
1079 case SIGILL:
1080 case SIGFPE:
1081 case SIGSEGV:
1082 case SIGBUS:
1083 case SIGTRAP:
1084 printf(": code=%s, addr=%p, trap=%d)\n",
1085 code, si->si_addr, si->si_trap);
1086 return;
1087 case SIGIO:
1088 printf(": code=%s, fd=%d, band=%lx)\n",
1089 code, si->si_fd, si->si_band);
1090 return;
1091 default:
1092 printf(": code=%s, errno=%d)\n",
1093 code, si->si_errno);
1094 return;
1095 }
1096 /*NOTREACHED*/
1097 default:
1098 warnx("Unhandled size %d for ktrpsig", len);
1099 break;
1100 }
1101 }
1102
1103 static void
1104 ktrcsw(struct ktr_csw *cs)
1105 {
1106
1107 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
1108 cs->user ? "user" : "kernel");
1109 }
1110
1111 static void
1112 ktruser_msghdr(const char *name, const void *buf, size_t len)
1113 {
1114 struct msghdr m;
1115
1116 if (len != sizeof(m))
1117 warnx("%.*s: len %zu != %zu", KTR_USER_MAXIDLEN, name, len,
1118 sizeof(m));
1119 memcpy(&m, buf, len);
1120 printf("%.*s: [name=%p, namelen=%zu, iov=%p, iovlen=%zu, control=%p, "
1121 "controllen=%zu, flags=%x]\n", KTR_USER_MAXIDLEN, name,
1122 m.msg_name, (size_t)m.msg_namelen, m.msg_iov, (size_t)m.msg_iovlen,
1123 m.msg_control, (size_t)m.msg_controllen, m.msg_flags);
1124 }
1125
1126 static void
1127 ktruser_soname(const char *name, const void *buf, size_t len)
1128 {
1129 char fmt[512];
1130 sockaddr_snprintf(fmt, sizeof(fmt), "%a", buf);
1131 printf("%.*s: [%s]\n", KTR_USER_MAXIDLEN, name, fmt);
1132 }
1133
1134 static void
1135 ktruser_control(const char *name, const void *buf, size_t len)
1136 {
1137 struct cmsghdr m;
1138
1139 if (len < sizeof(m))
1140 warnx("%.*s: len %zu < %zu", KTR_USER_MAXIDLEN, name, len,
1141 sizeof(m));
1142 memcpy(&m, buf, sizeof(m));
1143 printf("%.*s: [len=%zu, level=%d, type=%d]\n", KTR_USER_MAXIDLEN, name,
1144 (size_t)m.cmsg_len, m.cmsg_level, m.cmsg_type);
1145 }
1146
1147 static void
1148 ktruser_misc(const char *name, const void *buf, size_t len)
1149 {
1150 size_t i;
1151 const char *dta = buf;
1152
1153 printf("%.*s: %zu, ", KTR_USER_MAXIDLEN, name, len);
1154 for (i = 0; i < len; i++)
1155 printf("%02x", (unsigned char)dta[i]);
1156 printf("\n");
1157 }
1158
1159 static struct {
1160 const char *name;
1161 void (*func)(const char *, const void *, size_t);
1162 } nv[] = {
1163 { "msghdr", ktruser_msghdr },
1164 { "mbsoname", ktruser_soname },
1165 { "mbcontrol", ktruser_control },
1166 { NULL, ktruser_misc },
1167 };
1168
1169 static void
1170 ktruser(struct ktr_user *usr, int len)
1171 {
1172 unsigned char *dta;
1173
1174 len -= sizeof(struct ktr_user);
1175 dta = (unsigned char *)(usr + 1);
1176 if (word_size) {
1177 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
1178 printf("\n");
1179 hexdump_buf(dta, len, word_size);
1180 return;
1181 }
1182 for (size_t j = 0; j < __arraycount(nv); j++)
1183 if (nv[j].name == NULL ||
1184 strncmp(nv[j].name, usr->ktr_id, KTR_USER_MAXIDLEN) == 0) {
1185 (*nv[j].func)(usr->ktr_id, dta, len);
1186 break;
1187 }
1188 }
1189
1190 static void
1191 ktrmib(int *namep, int len)
1192 {
1193 size_t i;
1194
1195 for (i = 0; i < (len / sizeof(*namep)); i++)
1196 printf("%s%d", (i == 0) ? "" : ".", namep[i]);
1197 printf("\n");
1198 }
1199
1200 static const char *
1201 signame(long sig, int xlat)
1202 {
1203 static char buf[64];
1204
1205 if (sig == 0)
1206 return " 0";
1207 else if (sig < 0 || sig >= NSIG) {
1208 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
1209 return buf;
1210 } else
1211 return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
1212 cur_emul->signalmap[sig] : sig];
1213 }
1214
1215 static void
1216 usage(void)
1217 {
1218 if (strcmp(getprogname(), "ioctlname") == 0) {
1219 (void)fprintf(stderr, "Usage: %s [-e emulation] <ioctl> ...\n",
1220 getprogname());
1221 } else {
1222 (void)fprintf(stderr, "Usage: %s [-dElNnRT] [-e emulation] "
1223 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
1224 "[-x | -X size] [file]\n", getprogname());
1225 }
1226 exit(1);
1227 }
1228