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