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