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