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