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