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