1 /* $NetBSD: kdump.c,v 1.104 2009/04/13 14:39:23 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.104 2009/04/13 14:39:23 christos 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") == 0) { 536 ectx_delete(); 537 538 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) { 539 (void)putchar('('); 540 output_long((long)*ap, !(decimal || small(*ap))); 541 ap++; 542 argcount--; 543 if ((cp = ioctlname(*ap)) != NULL) 544 (void)printf(",%s", cp); 545 else { 546 (void)putchar(','); 547 ioctldecode(*ap); 548 } 549 ap++; 550 argcount--; 551 c = ','; 552 553 } else if ((strstr(sys_name, "sigaction") != NULL || 554 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) { 555 (void)printf("(SIG%s", signame(ap[0], 1)); 556 ap += 1; 557 argcount -= 1; 558 c = ','; 559 560 } else if ((strcmp(sys_name, "kill") == 0 || 561 strcmp(sys_name, "killpg") == 0) && argcount >= 2) { 562 putchar('('); 563 output_long((long)ap[0], !(decimal || small(*ap))); 564 (void)printf(", SIG%s", signame(ap[1], 1)); 565 ap += 2; 566 argcount -= 2; 567 c = ','; 568 569 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) { 570 putchar('('); 571 if (strcmp(emul->name, "linux") == 0 || 572 strcmp(emul->name, "linux32") == 0) { 573 if ((long)*ap >= 0 && *ap < 574 (register_t)(sizeof(linux_ptrace_ops) / 575 sizeof(linux_ptrace_ops[0]))) 576 (void)printf("%s", 577 linux_ptrace_ops[*ap]); 578 else 579 output_long((long)*ap, 1); 580 } else { 581 if ((long)*ap >= 0 && *ap < 582 (register_t)(sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))) 583 (void)printf("%s", ptrace_ops[*ap]); 584 #ifdef PT_MACHDEP_STRINGS 585 else if (*ap >= PT_FIRSTMACH && 586 *ap - PT_FIRSTMACH < 587 (register_t)(sizeof(ptrace_machdep_ops) / 588 sizeof(ptrace_machdep_ops[0]))) 589 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]); 590 #endif 591 else 592 output_long((long)*ap, 1); 593 } 594 ap++; 595 argcount--; 596 c = ','; 597 598 } 599 while (argcount > 0) { 600 putchar(c); 601 output_long((long)*ap, !(decimal || small(*ap))); 602 ap++; 603 argcount--; 604 c = ','; 605 } 606 (void)putchar(')'); 607 } 608 (void)putchar('\n'); 609 } 610 611 static void 612 ktrsysret(struct ktr_sysret *ktr, int len) 613 { 614 const struct emulation *emul; 615 int error = ktr->ktr_error; 616 int code = ktr->ktr_code; 617 618 if (emul_changed) { 619 /* In order to get system call name right in execve return */ 620 emul = prev_emul; 621 emul_changed = 0; 622 } else 623 emul = cur_emul; 624 625 if (numeric || ((code >= emul->nsysnames || code < 0 || plain > 1) && 626 mach_traps_dispatch(&code, &emul) == 0)) 627 (void)printf("[%d] ", code); 628 else 629 (void)printf("%s ", emul->sysnames[code]); 630 631 switch (error) { 632 case 0: 633 rprint(ktr->ktr_retval); 634 if (len > (int)offsetof(struct ktr_sysret, ktr_retval_1) && 635 ktr->ktr_retval_1 != 0) { 636 (void)printf(", "); 637 rprint(ktr->ktr_retval_1); 638 } 639 break; 640 641 default: 642 eprint(error); 643 break; 644 } 645 (void)putchar('\n'); 646 } 647 648 static void 649 rprint(register_t ret) 650 { 651 652 if (!plain) { 653 (void)printf("%ld", (long)ret); 654 if (!small(ret)) 655 (void)printf("/%#lx", (long)ret); 656 } else { 657 if (decimal || small(ret)) 658 (void)printf("%ld", (long)ret); 659 else 660 (void)printf("%#lx", (long)ret); 661 } 662 } 663 664 /* 665 * We print the original emulation's error numerically, but we 666 * translate it to netbsd to print it symbolically. 667 */ 668 static void 669 eprint(int e) 670 { 671 int i = e; 672 673 if (cur_emul->errnomap) { 674 675 /* No remapping for ERESTART and EJUSTRETURN */ 676 /* Kludge for linux that has negative error numbers */ 677 if (cur_emul->errnomap[2] > 0 && e < 0) 678 goto normal; 679 680 for (i = 0; i < cur_emul->nerrnomap; i++) 681 if (e == cur_emul->errnomap[i]) 682 break; 683 684 if (i == cur_emul->nerrnomap) { 685 printf("-1 unknown errno %d", e); 686 return; 687 } 688 } 689 690 normal: 691 switch (i) { 692 case ERESTART: 693 (void)printf("RESTART"); 694 break; 695 696 case EJUSTRETURN: 697 (void)printf("JUSTRETURN"); 698 break; 699 700 default: 701 (void)printf("-1 errno %d", e); 702 if (!plain) 703 (void)printf(" %s", strerror(i)); 704 } 705 } 706 707 static void 708 ktrnamei(char *cp, int len) 709 { 710 711 (void)printf("\"%.*s\"\n", len, cp); 712 } 713 714 static void 715 ktremul(char *name, int len, int bufsize) 716 { 717 718 if (len >= bufsize) 719 len = bufsize - 1; 720 721 name[len] = '\0'; 722 setemul(name, ktr_header.ktr_pid, 1); 723 emul_changed = 1; 724 725 (void)printf("\"%s\"\n", name); 726 } 727 728 static void 729 hexdump_buf(const void *vdp, int datalen, int word_sz) 730 { 731 const char hex[] = "0123456789abcdef"; 732 char chars[16], prev[16]; 733 char bytes[16 * 3 + 4]; 734 const unsigned char *dp = vdp; 735 const unsigned char *datalim = dp + datalen; 736 const unsigned char *line_end; 737 int off, l = 0, c; 738 char *cp, *bp; 739 int divmask = word_sz - 1; /* block size in bytes */ 740 int gdelim = 3; /* gap between blocks */ 741 int bsize = 2; /* increment for each byte */ 742 int width; 743 int dupl = 0; 744 #if _BYTE_ORDER == _LITTLE_ENDIAN 745 int bswap = word_sz - 1; 746 #else 747 #define bswap 0 748 #endif 749 750 switch (word_sz) { 751 case 2: 752 gdelim = 2; 753 break; 754 case 1: 755 divmask = 7; 756 bsize = 3; 757 gdelim = 1; 758 break; 759 default: 760 break; 761 } 762 width = 16 * bsize + (16 / (divmask + 1)) * gdelim; 763 if (word_sz != 1) 764 width += 2; 765 766 for (off = 0; dp < datalim; off += l) { 767 memset(bytes, ' ', sizeof bytes); 768 line_end = dp + 16; 769 if (line_end >= datalim) { 770 line_end = datalim; 771 dupl |= 1; /* need to print */ 772 } else { 773 if (dupl == 0 || memcmp(dp, prev, sizeof chars)) 774 dupl |= 1; 775 } 776 777 if (!(dupl & 1)) { 778 /* This is a duplicate of the line above, count 'em */ 779 dupl += 2; 780 dp = line_end; 781 continue; 782 } 783 784 if (dupl > 3) { 785 /* previous line as a duplicate */ 786 if (dupl == 5) 787 /* Only one duplicate, print line */ 788 printf("\t%-5.3x%.*s%.*s\n", 789 off - l, width, bytes, l, chars); 790 else 791 printf("\t%.*s\n", 792 snprintf(NULL, 0, "%3x", off), "*****"); 793 } 794 795 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) { 796 c = *dp++; 797 prev[l] = c; 798 if ((l & divmask) == 0) 799 bp += gdelim; 800 bp[(l ^ bswap) * bsize] = hex[c >> 4]; 801 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf]; 802 *cp++ = isgraph(c) ? c : '.'; 803 } 804 805 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars); 806 dupl = 2; 807 } 808 } 809 810 static void 811 visdump_buf(const void *vdp, int datalen, int col) 812 { 813 const unsigned char *dp = vdp; 814 char *cp; 815 int width; 816 char visbuf[5]; 817 static int screenwidth = 0; 818 819 if (screenwidth == 0) { 820 struct winsize ws; 821 822 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 823 ws.ws_col > 8) 824 screenwidth = ws.ws_col; 825 else 826 screenwidth = 80; 827 } 828 829 (void)printf("\""); 830 col++; 831 for (; datalen > 0; datalen--, dp++) { 832 (void)svis(visbuf, *dp, VIS_CSTYLE, 833 datalen > 1 ? *(dp + 1) : 0, "\"\n"); 834 cp = visbuf; 835 /* 836 * Keep track of printables and 837 * space chars (like fold(1)). 838 */ 839 if (col == 0) { 840 (void)putchar('\t'); 841 col = 8; 842 } 843 switch (*cp) { 844 case '\n': 845 col = 0; 846 (void)putchar('\n'); 847 continue; 848 case '\t': 849 width = 8 - (col & 07); 850 break; 851 default: 852 width = strlen(cp); 853 } 854 if (col + width > (screenwidth - 2)) { 855 (void)printf("\\\n\t"); 856 col = 8; 857 if (*cp == '\t') 858 width = 8; 859 } 860 col += width; 861 do { 862 (void)putchar(*cp++); 863 } while (*cp); 864 } 865 if (col == 0) 866 (void)printf(" "); 867 (void)printf("\"\n"); 868 } 869 870 static void 871 ktrgenio(struct ktr_genio *ktr, int len) 872 { 873 int datalen = len - sizeof (struct ktr_genio); 874 char *dp = (char *)ktr + sizeof (struct ktr_genio); 875 876 if (ktr->ktr_fd != -1) 877 printf("fd %d ", ktr->ktr_fd); 878 printf("%s %d bytes\n", 879 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 880 if (maxdata == 0) 881 return; 882 if (maxdata > 0 && datalen > maxdata) 883 datalen = maxdata; 884 if (word_size) { 885 hexdump_buf(dp, datalen, word_size); 886 return; 887 } 888 (void)printf(" "); 889 visdump_buf(dp, datalen, 7); 890 } 891 892 static void 893 ktrpsig(void *v, int len) 894 { 895 int signo, first; 896 struct { 897 struct ktr_psig ps; 898 siginfo_t si; 899 } *psig = v; 900 siginfo_t *si = &psig->si; 901 const char *code; 902 903 (void)printf("SIG%s ", signame(psig->ps.signo, 0)); 904 if (psig->ps.action == SIG_DFL) 905 (void)printf("SIG_DFL"); 906 else { 907 (void)printf("caught handler=%p mask=(", psig->ps.action); 908 first = 1; 909 for (signo = 1; signo < NSIG; signo++) { 910 if (sigismember(&psig->ps.mask, signo)) { 911 if (first) 912 first = 0; 913 else 914 (void)printf(","); 915 (void)printf("%d", signo); 916 } 917 } 918 (void)printf(")"); 919 } 920 switch (len) { 921 case sizeof(struct ktr_psig): 922 if (psig->ps.code) 923 printf(" code=0x%x", psig->ps.code); 924 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n"); 925 return; 926 case sizeof(*psig): 927 if (si->si_code == 0) { 928 printf(": code=SI_USER sent by pid=%d, uid=%d)\n", 929 si->si_pid, si->si_uid); 930 return; 931 } 932 933 if (si->si_code < 0) { 934 switch (si->si_code) { 935 case SI_TIMER: 936 printf(": code=SI_TIMER sigval %p)\n", 937 si->si_value.sival_ptr); 938 return; 939 case SI_QUEUE: 940 code = "SI_QUEUE"; 941 break; 942 case SI_ASYNCIO: 943 code = "SI_ASYNCIO"; 944 break; 945 case SI_MESGQ: 946 code = "SI_MESGQ"; 947 break; 948 case SI_LWP: 949 code = "SI_LWP"; 950 break; 951 default: 952 code = NULL; 953 break; 954 } 955 if (code) 956 printf(": code=%s unimplemented)\n", code); 957 else 958 printf(": code=%d unimplemented)\n", 959 si->si_code); 960 return; 961 } 962 963 if (si->si_code == SI_NOINFO) { 964 printf(": code=SI_NOINFO\n"); 965 return; 966 } 967 968 code = siginfocodename(si->si_signo, si->si_code); 969 switch (si->si_signo) { 970 case SIGCHLD: 971 printf(": code=%s child pid=%d, uid=%d, " 972 " status=%u, utime=%lu, stime=%lu)\n", 973 code, si->si_pid, 974 si->si_uid, si->si_status, 975 (unsigned long) si->si_utime, 976 (unsigned long) si->si_stime); 977 return; 978 case SIGILL: 979 case SIGFPE: 980 case SIGSEGV: 981 case SIGBUS: 982 case SIGTRAP: 983 printf(": code=%s, addr=%p, trap=%d)\n", 984 code, si->si_addr, si->si_trap); 985 return; 986 case SIGIO: 987 printf(": code=%s, fd=%d, band=%lx)\n", 988 code, si->si_fd, si->si_band); 989 return; 990 default: 991 printf(": code=%s, errno=%d)\n", 992 code, si->si_errno); 993 return; 994 } 995 /*NOTREACHED*/ 996 default: 997 warnx("Unhandled size %d for ktrpsig\n", len); 998 break; 999 } 1000 } 1001 1002 static void 1003 ktrcsw(struct ktr_csw *cs) 1004 { 1005 1006 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 1007 cs->user ? "user" : "kernel"); 1008 } 1009 1010 static void 1011 ktruser(struct ktr_user *usr, int len) 1012 { 1013 int i; 1014 unsigned char *dta; 1015 1016 len -= sizeof(struct ktr_user); 1017 printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id); 1018 dta = (unsigned char *)(usr + 1); 1019 if (word_size) { 1020 printf("\n"); 1021 hexdump_buf(dta, len, word_size); 1022 return; 1023 } 1024 printf(" %d, ", len); 1025 for (i = 0; i < len; i++) 1026 printf("%02x", (unsigned int) dta[i]); 1027 printf("\n"); 1028 } 1029 1030 static void 1031 ktrmmsg(struct ktr_mmsg *mmsg, int len) 1032 { 1033 const char *service_name; 1034 const char *reply; 1035 int id; 1036 1037 id = mmsg->ktr_id; 1038 if ((id / 100) % 2) { /* Message reply */ 1039 reply = " reply"; 1040 id -= 100; 1041 } else { 1042 reply = ""; 1043 } 1044 1045 if ((service_name = mach_service_name(id)) != NULL) 1046 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id); 1047 else 1048 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id); 1049 1050 hexdump_buf(mmsg, len, word_size ? word_size : 4); 1051 } 1052 1053 static void 1054 ktrmool(struct ktr_mool *mool, int len) 1055 { 1056 size_t size = mool->size; 1057 1058 printf("%ld/0x%lx bytes at %p\n", 1059 (u_long)size, (u_long)size, mool->uaddr); 1060 mool++; 1061 hexdump_buf(mool, size, word_size ? word_size : 4); 1062 } 1063 1064 static void 1065 ktrmib(int *namep, int len) 1066 { 1067 size_t i; 1068 1069 for (i = 0; i < (len / sizeof(*namep)); i++) 1070 printf("%s%d", (i == 0) ? "" : ".", namep[i]); 1071 printf("\n"); 1072 } 1073 1074 static const char * 1075 signame(long sig, int xlat) 1076 { 1077 static char buf[64]; 1078 1079 if (sig == 0) 1080 return " 0"; 1081 else if (sig < 0 || sig >= NSIG) { 1082 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig); 1083 return buf; 1084 } else 1085 return sys_signame[(xlat && cur_emul->signalmap != NULL) ? 1086 cur_emul->signalmap[sig] : sig]; 1087 } 1088 1089 static void 1090 usage(void) 1091 { 1092 if (strcmp(getprogname(), "ioctlname") == 0) { 1093 (void)fprintf(stderr, "Usage: %s [-e emulation] <ioctl> ...\n", 1094 getprogname()); 1095 } else { 1096 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] " 1097 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] " 1098 "[-x | -X size] [file]\n", getprogname()); 1099 } 1100 exit(1); 1101 } 1102