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