1 /* $NetBSD: kdump.c,v 1.85 2006/04/05 00:50:59 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.85 2006/04/05 00:50:59 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 #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 usage(void) __attribute__((__noreturn__)); 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_SAUPCALL: 280 ktrsaupcall(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 = "USER"; 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 default: 358 (void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)", 359 kth->ktr_type); 360 type = unknown; 361 } 362 363 col = printf("%6d ", kth->ktr_pid); 364 if (kth->ktr_version > KTRFACv0) 365 col += printf("%6d ", kth->ktr_lid); 366 col += printf("%-8.*s ", MAXCOMLEN, kth->ktr_comm); 367 if (timestamp) { 368 if (timestamp == 2) { 369 if (kth->ktr_version == KTRFACv0) { 370 if (prevtime.tv.tv_sec == 0) 371 temp.tv.tv_sec = temp.tv.tv_usec = 0; 372 else 373 timersub(&kth->ktr_tv, 374 &prevtime.tv, &temp.tv); 375 prevtime.tv = kth->ktr_tv; 376 } else { 377 if (prevtime.ts.tv_sec == 0) 378 temp.ts.tv_sec = temp.ts.tv_nsec = 0; 379 else 380 timespecsub(&kth->ktr_time, 381 &prevtime.ts, &temp.ts); 382 prevtime.ts = kth->ktr_time; 383 } 384 } else { 385 if (kth->ktr_version == KTRFACv0) 386 temp.tv = kth->ktr_tv; 387 else 388 temp.ts = kth->ktr_time; 389 } 390 if (kth->ktr_version == KTRFACv0) 391 col += printf("%ld.%06ld ", 392 (long)temp.tv.tv_sec, (long)temp.tv.tv_usec); 393 else 394 col += printf("%ld.%09ld ", 395 (long)temp.ts.tv_sec, (long)temp.ts.tv_nsec); 396 } 397 col += printf("%-4s ", type); 398 return col; 399 } 400 401 static void 402 output_long(u_long it, int as_x) 403 { 404 if (cur_emul->flags & EMUL_FLAG_NETBSD32) 405 printf(as_x ? "%#x" : "%d", (u_int)it); 406 else 407 printf(as_x ? "%#lx" : "%ld", it); 408 } 409 410 static void 411 ioctldecode(u_long cmd) 412 { 413 char dirbuf[4], *dir = dirbuf; 414 415 if (cmd & IOC_IN) 416 *dir++ = 'W'; 417 if (cmd & IOC_OUT) 418 *dir++ = 'R'; 419 *dir = '\0'; 420 421 printf(",_IO%s('%c',", dirbuf, (int) ((cmd >> 8) & 0xff)); 422 output_long(cmd & 0xff, decimal == 0); 423 if ((cmd & IOC_VOID) == 0) { 424 putchar(','); 425 output_long(IOCPARM_LEN(cmd), decimal == 0); 426 } 427 putchar(')'); 428 } 429 430 static void 431 ktrsyscall(struct ktr_syscall *ktr) 432 { 433 int argcount; 434 const struct emulation *emul = cur_emul; 435 register_t *ap; 436 char c; 437 const char *cp; 438 const char *sys_name; 439 440 argcount = ktr->ktr_argsize / sizeof (*ap); 441 442 emul_changed = 0; 443 444 if (numeric || 445 ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) && 446 mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) { 447 sys_name = "?"; 448 (void)printf("[%d]", ktr->ktr_code); 449 } else { 450 sys_name = emul->sysnames[ktr->ktr_code]; 451 (void)printf("%s", sys_name); 452 } 453 #ifdef _LP64 454 #define NETBSD32_ "netbsd32_" 455 if (cur_emul->flags & EMUL_FLAG_NETBSD32) { 456 size_t len = strlen(NETBSD32_); 457 if (strncmp(sys_name, NETBSD32_, len) == 0) 458 sys_name += len; 459 } 460 #undef NETBSD32_ 461 #endif 462 463 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall)); 464 if (argcount) { 465 c = '('; 466 if (plain) { 467 ; 468 469 } else if (strcmp(sys_name, "exit") == 0) { 470 ectx_delete(); 471 472 } else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) { 473 (void)putchar('('); 474 output_long((long)*ap, !(decimal || small(*ap))); 475 ap++; 476 argcount--; 477 if ((cp = ioctlname(*ap)) != NULL) 478 (void)printf(",%s", cp); 479 else 480 ioctldecode(*ap); 481 ap++; 482 argcount--; 483 c = ','; 484 485 } else if ((strstr(sys_name, "sigaction") != NULL || 486 strstr(sys_name, "sigvec") != NULL) && argcount >= 1) { 487 (void)printf("(SIG%s", signame(ap[0], 1)); 488 ap += 1; 489 argcount -= 1; 490 c = ','; 491 492 } else if ((strcmp(sys_name, "kill") == 0 || 493 strcmp(sys_name, "killpg") == 0) && argcount >= 2) { 494 putchar('('); 495 output_long((long)ap[0], !(decimal || small(*ap))); 496 (void)printf(", SIG%s", signame(ap[1], 1)); 497 ap += 2; 498 argcount -= 2; 499 c = ','; 500 501 } else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) { 502 putchar('('); 503 if (strcmp(emul->name, "linux") == 0) { 504 if (*ap >= 0 && *ap < 505 sizeof(linux_ptrace_ops) / 506 sizeof(linux_ptrace_ops[0])) 507 (void)printf("%s", 508 linux_ptrace_ops[*ap]); 509 else 510 output_long((long)*ap, 1); 511 } else { 512 if (*ap >= 0 && *ap < 513 sizeof(ptrace_ops) / sizeof(ptrace_ops[0])) 514 (void)printf("%s", ptrace_ops[*ap]); 515 #ifdef PT_MACHDEP_STRINGS 516 else if (*ap >= PT_FIRSTMACH && 517 *ap - PT_FIRSTMACH < 518 sizeof(ptrace_machdep_ops) / 519 sizeof(ptrace_machdep_ops[0])) 520 (void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]); 521 #endif 522 else 523 output_long((long)*ap, 1); 524 } 525 ap++; 526 argcount--; 527 c = ','; 528 529 } 530 while (argcount > 0) { 531 putchar(c); 532 output_long((long)*ap, !(decimal || small(*ap))); 533 ap++; 534 argcount--; 535 c = ','; 536 } 537 (void)putchar(')'); 538 } 539 (void)putchar('\n'); 540 } 541 542 static void 543 ktrsysret(struct ktr_sysret *ktr, int len) 544 { 545 const struct emulation *emul; 546 int error = ktr->ktr_error; 547 int code = ktr->ktr_code; 548 549 if (emul_changed) { 550 /* In order to get system call name right in execve return */ 551 emul = prev_emul; 552 emul_changed = 0; 553 } else 554 emul = cur_emul; 555 556 if ((code >= emul->nsysnames || code < 0 || plain > 1) && 557 (mach_traps_dispatch(&code, &emul) == 0)) 558 (void)printf("[%d] ", code); 559 else 560 (void)printf("%s ", emul->sysnames[code]); 561 562 switch (error) { 563 case 0: 564 rprint(ktr->ktr_retval); 565 if (len > offsetof(struct ktr_sysret, ktr_retval_1) && 566 ktr->ktr_retval_1 != 0) { 567 (void)printf(", "); 568 rprint(ktr->ktr_retval_1); 569 } 570 break; 571 572 default: 573 eprint(error); 574 break; 575 } 576 (void)putchar('\n'); 577 } 578 579 static void 580 rprint(register_t ret) 581 { 582 583 if (!plain) { 584 (void)printf("%ld", (long)ret); 585 if (!small(ret)) 586 (void)printf("/%#lx", (long)ret); 587 } else { 588 if (decimal || small(ret)) 589 (void)printf("%ld", (long)ret); 590 else 591 (void)printf("%#lx", (long)ret); 592 } 593 } 594 595 /* 596 * We print the original emulation's error numerically, but we 597 * translate it to netbsd to print it symbolically. 598 */ 599 static void 600 eprint(int e) 601 { 602 int i = e; 603 604 if (cur_emul->errnomap) { 605 606 /* No remapping for ERESTART and EJUSTRETURN */ 607 /* Kludge for linux that has negative error numbers */ 608 if (cur_emul->errnomap[2] > 0 && e < 0) 609 goto normal; 610 611 for (i = 0; i < cur_emul->nerrnomap; i++) 612 if (e == cur_emul->errnomap[i]) 613 break; 614 615 if (i == cur_emul->nerrnomap) { 616 printf("-1 unknown errno %d", e); 617 return; 618 } 619 } 620 621 normal: 622 switch (i) { 623 case ERESTART: 624 (void)printf("RESTART"); 625 break; 626 627 case EJUSTRETURN: 628 (void)printf("JUSTRETURN"); 629 break; 630 631 default: 632 (void)printf("-1 errno %d", e); 633 if (!plain) 634 (void)printf(" %s", strerror(i)); 635 } 636 } 637 638 static void 639 ktrnamei(char *cp, int len) 640 { 641 642 (void)printf("\"%.*s\"\n", len, cp); 643 } 644 645 static void 646 ktremul(char *name, int len, int bufsize) 647 { 648 649 if (len >= bufsize) 650 len = bufsize - 1; 651 652 name[len] = '\0'; 653 setemul(name, ktr_header.ktr_pid, 1); 654 emul_changed = 1; 655 656 (void)printf("\"%s\"\n", name); 657 } 658 659 static void 660 hexdump_buf(const void *vdp, int datalen, int word_sz) 661 { 662 const char hex[] = "0123456789abcdef"; 663 char chars[16], prev[16]; 664 char bytes[16 * 3 + 4]; 665 const unsigned char *dp = vdp; 666 const unsigned char *datalim = dp + datalen; 667 const unsigned char *line_end; 668 int off, l = 0, c; 669 char *cp, *bp; 670 int divmask = word_sz - 1; /* block size in bytes */ 671 int gdelim = 3; /* gap between blocks */ 672 int bsize = 2; /* increment for each byte */ 673 int width; 674 int dupl = 0; 675 #if _BYTE_ORDER == _LITTLE_ENDIAN 676 int bswap = word_sz - 1; 677 #else 678 #define bswap 0 679 #endif 680 681 switch (word_sz) { 682 case 2: 683 gdelim = 2; 684 break; 685 case 1: 686 divmask = 7; 687 bsize = 3; 688 gdelim = 1; 689 break; 690 default: 691 break; 692 } 693 width = 16 * bsize + (16 / (divmask + 1)) * gdelim; 694 if (word_sz != 1) 695 width += 2; 696 697 for (off = 0; dp < datalim; off += l) { 698 memset(bytes, ' ', sizeof bytes); 699 line_end = dp + 16; 700 if (line_end >= datalim) { 701 line_end = datalim; 702 dupl |= 1; /* need to print */ 703 } else { 704 if (dupl == 0 || memcmp(dp, prev, sizeof chars)) 705 dupl |= 1; 706 } 707 708 if (!(dupl & 1)) { 709 /* This is a duplicate of the line above, count 'em */ 710 dupl += 2; 711 dp = line_end; 712 continue; 713 } 714 715 if (dupl > 3) { 716 /* previous line as a duplicate */ 717 if (dupl == 5) 718 /* Only one duplicate, print line */ 719 printf("\t%-5.3x%.*s%.*s\n", 720 off - l, width, bytes, l, chars); 721 else 722 printf("\t%.*s\n", 723 snprintf(NULL, 0, "%3x", off), "*****"); 724 } 725 726 for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) { 727 c = *dp++; 728 prev[l] = c; 729 if ((l & divmask) == 0) 730 bp += gdelim; 731 bp[(l ^ bswap) * bsize] = hex[c >> 4]; 732 bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf]; 733 *cp++ = isgraph(c) ? c : '.'; 734 } 735 736 printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars); 737 dupl = 2; 738 } 739 } 740 741 static void 742 visdump_buf(const void *vdp, int datalen, int col) 743 { 744 const unsigned char *dp = vdp; 745 char *cp; 746 int width; 747 char visbuf[5]; 748 static int screenwidth = 0; 749 750 if (screenwidth == 0) { 751 struct winsize ws; 752 753 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 754 ws.ws_col > 8) 755 screenwidth = ws.ws_col; 756 else 757 screenwidth = 80; 758 } 759 760 (void)printf("\""); 761 col++; 762 for (; datalen > 0; datalen--, dp++) { 763 (void)svis(visbuf, *dp, VIS_CSTYLE, 764 datalen > 1 ? *(dp + 1) : 0, "\""); 765 cp = visbuf; 766 /* 767 * Keep track of printables and 768 * space chars (like fold(1)). 769 */ 770 if (col == 0) { 771 (void)putchar('\t'); 772 col = 8; 773 } 774 switch (*cp) { 775 case '\n': 776 col = 0; 777 (void)putchar('\n'); 778 continue; 779 case '\t': 780 width = 8 - (col & 07); 781 break; 782 default: 783 width = strlen(cp); 784 } 785 if (col + width > (screenwidth - 2)) { 786 (void)printf("\\\n\t"); 787 col = 8; 788 if (*cp == '\t') 789 width = 8; 790 } 791 col += width; 792 do { 793 (void)putchar(*cp++); 794 } while (*cp); 795 } 796 if (col == 0) 797 (void)printf(" "); 798 (void)printf("\"\n"); 799 } 800 801 static void 802 ktrgenio(struct ktr_genio *ktr, int len) 803 { 804 int datalen = len - sizeof (struct ktr_genio); 805 char *dp = (char *)ktr + sizeof (struct ktr_genio); 806 807 printf("fd %d %s %d bytes\n", ktr->ktr_fd, 808 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 809 if (maxdata == 0) 810 return; 811 if (maxdata > 0 && datalen > maxdata) 812 datalen = maxdata; 813 if (word_size) { 814 hexdump_buf(dp, datalen, word_size); 815 return; 816 } 817 (void)printf(" "); 818 visdump_buf(dp, datalen, 7); 819 } 820 821 static void 822 ktrpsig(void *v, int len) 823 { 824 int signo, first; 825 struct { 826 struct ktr_psig ps; 827 siginfo_t si; 828 } *psig = v; 829 siginfo_t *si = &psig->si; 830 const char *code; 831 832 (void)printf("SIG%s ", signame(psig->ps.signo, 0)); 833 if (psig->ps.action == SIG_DFL) 834 (void)printf("SIG_DFL"); 835 else { 836 (void)printf("caught handler=%p mask=(", psig->ps.action); 837 first = 1; 838 for (signo = 1; signo < NSIG; signo++) { 839 if (sigismember(&psig->ps.mask, signo)) { 840 if (first) 841 first = 0; 842 else 843 (void)printf(","); 844 (void)printf("%d", signo); 845 } 846 } 847 (void)printf(")"); 848 } 849 switch (len) { 850 case sizeof(struct ktr_psig): 851 if (psig->ps.code) 852 printf(" code=0x%x", psig->ps.code); 853 printf(psig->ps.action == SIG_DFL ? "\n" : ")\n"); 854 return; 855 case sizeof(*psig): 856 if (si->si_code == 0) { 857 printf(": code=SI_USER sent by pid=%d, uid=%d)\n", 858 si->si_pid, si->si_uid); 859 return; 860 } 861 862 if (si->si_code < 0) { 863 switch (si->si_code) { 864 case SI_TIMER: 865 printf(": code=SI_TIMER sigval %p)\n", 866 si->si_sigval.sival_ptr); 867 return; 868 case SI_QUEUE: 869 code = "SI_QUEUE"; 870 break; 871 case SI_ASYNCIO: 872 code = "SI_ASYNCIO"; 873 break; 874 case SI_MESGQ: 875 code = "SI_MESGQ"; 876 break; 877 default: 878 code = NULL; 879 break; 880 } 881 if (code) 882 printf(": code=%s unimplemented)\n", code); 883 else 884 printf(": code=%d unimplemented)\n", 885 si->si_code); 886 return; 887 } 888 889 code = siginfocodename(si->si_signo, si->si_code); 890 switch (si->si_signo) { 891 case SIGCHLD: 892 printf(": code=%s child pid=%d, uid=%d, " 893 " status=%u, utime=%lu, stime=%lu)\n", 894 code, si->si_pid, 895 si->si_uid, si->si_status, 896 (unsigned long) si->si_utime, 897 (unsigned long) si->si_stime); 898 return; 899 case SIGILL: 900 case SIGFPE: 901 case SIGSEGV: 902 case SIGBUS: 903 case SIGTRAP: 904 printf(": code=%s, addr=%p, trap=%d)\n", 905 code, si->si_addr, si->si_trap); 906 return; 907 case SIGIO: 908 printf(": code=%s, fd=%d, band=%lx)\n", 909 code, si->si_fd, si->si_band); 910 return; 911 default: 912 printf(": code=%s, errno=%d)\n", 913 code, si->si_errno); 914 return; 915 } 916 /*NOTREACHED*/ 917 default: 918 warnx("Unhandled size %d for ktrpsig\n", len); 919 break; 920 } 921 } 922 923 static void 924 ktrcsw(struct ktr_csw *cs) 925 { 926 927 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 928 cs->user ? "user" : "kernel"); 929 } 930 931 static void 932 ktruser(struct ktr_user *usr, int len) 933 { 934 int i; 935 unsigned char *dta; 936 937 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len); 938 dta = (unsigned char *)usr; 939 for (i = sizeof(struct ktr_user); i < len; i++) 940 printf("%02x", (unsigned int) dta[i]); 941 printf("\"\n"); 942 } 943 944 static void 945 ktrmmsg(struct ktr_mmsg *mmsg, int len) 946 { 947 const char *service_name; 948 const char *reply; 949 int id; 950 951 id = mmsg->ktr_id; 952 if ((id / 100) % 2) { /* Message reply */ 953 reply = " reply"; 954 id -= 100; 955 } else { 956 reply = ""; 957 } 958 959 if ((service_name = mach_service_name(id)) != NULL) 960 printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id); 961 else 962 printf("unknown service%s [%d]\n", reply, mmsg->ktr_id); 963 964 hexdump_buf(mmsg, len, word_size ? word_size : 4); 965 } 966 967 static void 968 ktr_saprint(const struct ktr_saupcall *sau, int off, int len) 969 { 970 const struct sa_t * sa = (const struct sa_t *)(const void *)&sau[1]; 971 int i; 972 for (i = off; i < off + len; i++) { 973 printf("<ctx=%p, id=%d, cpu=%d>%s", 974 sa[i].sa_context, sa[i].sa_id, sa[i].sa_cpu, 975 i == off + len - 1 ? "" : ", "); 976 } 977 } 978 979 static void 980 ktrsaupcall(const struct ktr_saupcall *sau, int len) 981 { 982 char tbuf[64]; 983 const char *type; 984 static const char *const sastrings[] = { SA_UPCALL_STRINGS }; 985 986 if (sau->ktr_type >= 0 && sau->ktr_type < SA_UPCALL_NUPCALLS) 987 type = sastrings[sau->ktr_type]; 988 else { 989 (void)snprintf(tbuf, sizeof(tbuf), "*%d*", sau->ktr_type); 990 type = tbuf; 991 } 992 printf("%s", type); 993 if (sau->ktr_nevent) { 994 printf(", event=["); 995 ktr_saprint(sau, 0, sau->ktr_nevent); 996 printf("]"); 997 } 998 if (sau->ktr_nint) { 999 printf(", intr=["); 1000 ktr_saprint(sau, sau->ktr_nevent, sau->ktr_nint); 1001 printf("]"); 1002 } 1003 printf("\n"); 1004 } 1005 1006 static void 1007 ktrmool(struct ktr_mool *mool, int len) 1008 { 1009 size_t size = mool->size; 1010 1011 printf("%ld/0x%lx bytes at %p\n", 1012 (u_long)size, (u_long)size, mool->uaddr); 1013 mool++; 1014 hexdump_buf(mool, size, word_size ? word_size : 4); 1015 } 1016 1017 static const char * 1018 signame(long sig, int xlat) 1019 { 1020 static char buf[64]; 1021 1022 if (sig == 0) 1023 return " 0"; 1024 else if (sig < 0 || sig >= NSIG) { 1025 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig); 1026 return buf; 1027 } else 1028 return sys_signame[(xlat && cur_emul->signalmap != NULL) ? 1029 cur_emul->signalmap[sig] : sig]; 1030 } 1031 1032 static void 1033 usage(void) 1034 { 1035 1036 (void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] " 1037 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] " 1038 "[-x | -X size] [file]\n", getprogname()); 1039 exit(1); 1040 } 1041