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