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