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