1 /* $NetBSD: kdump.c,v 1.47 2003/01/30 21:43:26 atatat 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95"; 45 #else 46 __RCSID("$NetBSD: kdump.c,v 1.47 2003/01/30 21:43:26 atatat Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #define _KERNEL 52 #include <sys/errno.h> 53 #undef _KERNEL 54 #include <sys/time.h> 55 #include <sys/uio.h> 56 #include <sys/ktrace.h> 57 #include <sys/ioctl.h> 58 #include <sys/ptrace.h> 59 60 #include <ctype.h> 61 #include <err.h> 62 #include <signal.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <unistd.h> 67 #include <vis.h> 68 69 #include "ktrace.h" 70 #include "setemul.h" 71 72 #include <sys/syscall.h> 73 74 int timestamp, decimal, plain, tail, maxdata = -1, numeric; 75 pid_t do_pid = -1; 76 const char *tracefile = NULL; 77 struct ktr_header ktr_header; 78 int emul_changed = 0; 79 80 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 81 82 static const char *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", 86 }; 87 88 static const char *linux_ptrace_ops[] = { 89 "PTRACE_TRACEME", 90 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER", 91 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER", 92 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP", 93 NULL, NULL, 94 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS", 95 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH", 96 "PTRACE_SYSCALL", 97 }; 98 99 int main __P((int, char **)); 100 int fread_tail __P((char *, int, int)); 101 void dumpheader __P((struct ktr_header *)); 102 void ioctldecode __P((u_long)); 103 void ktrsyscall __P((struct ktr_syscall *)); 104 void ktrsysret __P((struct ktr_sysret *)); 105 void ktrnamei __P((char *, int)); 106 void ktremul __P((char *, int, int)); 107 void ktrgenio __P((struct ktr_genio *, int)); 108 void ktrpsig __P((struct ktr_psig *)); 109 void ktrcsw __P((struct ktr_csw *)); 110 void ktruser __P((struct ktr_user *, int)); 111 void ktrmmsg __P((struct ktr_mmsg *, int)); 112 void usage __P((void)); 113 void eprint __P((int)); 114 char *ioctlname __P((long)); 115 static const char *signame __P((long, int)); 116 117 int 118 main(argc, argv) 119 int argc; 120 char *argv[]; 121 { 122 int ch, ktrlen, size; 123 void *m; 124 int trpoints = ALL_POINTS; 125 const char *emul_name = "netbsd"; 126 127 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:")) != -1) 128 switch (ch) { 129 case 'e': 130 emul_name = strdup(optarg); /* it's safer to copy it */ 131 break; 132 case 'f': 133 tracefile = optarg; 134 break; 135 case 'd': 136 decimal = 1; 137 break; 138 case 'l': 139 tail = 1; 140 break; 141 case 'p': 142 do_pid = atoi(optarg); 143 break; 144 case 'm': 145 maxdata = atoi(optarg); 146 break; 147 case 'N': 148 numeric++; 149 break; 150 case 'n': 151 plain++; 152 break; 153 case 'R': 154 timestamp = 2; /* relative timestamp */ 155 break; 156 case 'T': 157 timestamp = 1; 158 break; 159 case 't': 160 trpoints = getpoints(optarg); 161 if (trpoints < 0) 162 errx(1, "unknown trace point in %s", optarg); 163 break; 164 default: 165 usage(); 166 } 167 argv += optind; 168 argc -= optind; 169 170 if (tracefile == NULL) { 171 if (argc == 1) { 172 tracefile = argv[0]; 173 argv++; 174 argc--; 175 } 176 else 177 tracefile = DEF_TRACEFILE; 178 } 179 180 if (argc > 0) 181 usage(); 182 183 setemul(emul_name, 0, 0); 184 mach_lookup_emul(); 185 186 m = malloc(size = 1024); 187 if (m == NULL) 188 errx(1, "malloc: %s", strerror(ENOMEM)); 189 if (!freopen(tracefile, "r", stdin)) 190 err(1, "%s", tracefile); 191 while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) { 192 if (trpoints & (1<<ktr_header.ktr_type)) 193 if (do_pid == -1 || ktr_header.ktr_pid == do_pid) 194 dumpheader(&ktr_header); 195 if ((ktrlen = ktr_header.ktr_len) < 0) 196 errx(1, "bogus length 0x%x", ktrlen); 197 if (ktrlen > size) { 198 while(ktrlen > size) size *= 2; 199 m = (void *)realloc(m, size); 200 if (m == NULL) 201 errx(1, "realloc: %s", strerror(ENOMEM)); 202 } 203 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 204 errx(1, "data too short"); 205 if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 206 continue; 207 208 /* update context to match currently processed record */ 209 if (do_pid != -1 && ktr_header.ktr_pid != do_pid) 210 continue; 211 ectx_sanify(ktr_header.ktr_pid); 212 213 switch (ktr_header.ktr_type) { 214 case KTR_SYSCALL: 215 ktrsyscall((struct ktr_syscall *)m); 216 break; 217 case KTR_SYSRET: 218 ktrsysret((struct ktr_sysret *)m); 219 break; 220 case KTR_NAMEI: 221 ktrnamei(m, ktrlen); 222 break; 223 case KTR_GENIO: 224 ktrgenio((struct ktr_genio *)m, ktrlen); 225 break; 226 case KTR_PSIG: 227 ktrpsig((struct ktr_psig *)m); 228 break; 229 case KTR_CSW: 230 ktrcsw((struct ktr_csw *)m); 231 break; 232 case KTR_EMUL: 233 ktremul(m, ktrlen, size); 234 break; 235 case KTR_USER: 236 ktruser((struct ktr_user *)m, ktrlen); 237 break; 238 case KTR_MMSG: 239 ktrmmsg((struct ktr_mmsg *)m, ktrlen); 240 break; 241 } 242 if (tail) 243 (void)fflush(stdout); 244 } 245 return (0); 246 } 247 248 int 249 fread_tail(buf, size, num) 250 char *buf; 251 int num, size; 252 { 253 int i; 254 255 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 256 (void)sleep(1); 257 clearerr(stdin); 258 } 259 return (i); 260 } 261 262 void 263 dumpheader(kth) 264 struct ktr_header *kth; 265 { 266 char unknown[64], *type; 267 static struct timeval prevtime; 268 struct timeval temp; 269 270 switch (kth->ktr_type) { 271 case KTR_SYSCALL: 272 type = "CALL"; 273 break; 274 case KTR_SYSRET: 275 type = "RET "; 276 break; 277 case KTR_NAMEI: 278 type = "NAMI"; 279 break; 280 case KTR_GENIO: 281 type = "GIO "; 282 break; 283 case KTR_PSIG: 284 type = "PSIG"; 285 break; 286 case KTR_CSW: 287 type = "CSW"; 288 break; 289 case KTR_EMUL: 290 type = "EMUL"; 291 break; 292 case KTR_USER: 293 type = "USER"; 294 break; 295 case KTR_MMSG: 296 type = "MMSG"; 297 break; 298 default: 299 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 300 type = unknown; 301 } 302 303 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm); 304 if (timestamp) { 305 if (timestamp == 2) { 306 timersub(&kth->ktr_time, &prevtime, &temp); 307 prevtime = kth->ktr_time; 308 } else 309 temp = kth->ktr_time; 310 (void)printf("%ld.%06ld ", 311 (long int)temp.tv_sec, (long int)temp.tv_usec); 312 } 313 (void)printf("%s ", type); 314 } 315 316 void 317 ioctldecode(cmd) 318 u_long cmd; 319 { 320 char dirbuf[4], *dir = dirbuf; 321 322 if (cmd & IOC_IN) 323 *dir++ = 'W'; 324 if (cmd & IOC_OUT) 325 *dir++ = 'R'; 326 *dir = '\0'; 327 328 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx", 329 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff); 330 if ((cmd & IOC_VOID) == 0) 331 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff); 332 else 333 printf(")"); 334 } 335 336 void 337 ktrsyscall(ktr) 338 struct ktr_syscall *ktr; 339 { 340 int argsize = ktr->ktr_argsize; 341 const struct emulation *revelant = current; 342 register_t *ap; 343 344 if (((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0) 345 && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0)) || 346 numeric) 347 (void)printf("[%d]", ktr->ktr_code); 348 else 349 (void)printf("%s", revelant->sysnames[ktr->ktr_code]); 350 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall)); 351 if (argsize) { 352 char c = '('; 353 if (!plain) { 354 char *cp; 355 356 switch (ktr->ktr_code) { 357 case SYS_ioctl: 358 if (decimal) 359 (void)printf("(%ld", (long)*ap); 360 else 361 (void)printf("(%#lx", (long)*ap); 362 ap++; 363 argsize -= sizeof(register_t); 364 if ((cp = ioctlname(*ap)) != NULL) 365 (void)printf(",%s", cp); 366 else 367 ioctldecode(*ap); 368 c = ','; 369 ap++; 370 argsize -= sizeof(register_t); 371 break; 372 373 case SYS_ptrace: 374 if (strcmp(revelant->name, "linux") == 0) { 375 if (*ap >= 0 && *ap <= 376 sizeof(linux_ptrace_ops) / 377 sizeof(linux_ptrace_ops[0])) 378 (void)printf("(%s", 379 linux_ptrace_ops[*ap]); 380 else 381 (void)printf("(%ld", (long)*ap); 382 } else { 383 if (*ap >= 0 && *ap <= 384 sizeof(ptrace_ops) / sizeof(ptrace_ops[0])) 385 (void)printf("(%s", ptrace_ops[*ap]); 386 else 387 (void)printf("(%ld", (long)*ap); 388 } 389 c = ','; 390 ap++; 391 argsize -= sizeof(register_t); 392 break; 393 394 case SYS_kill: 395 if (decimal) 396 (void)printf("(%ld, SIG%s", 397 (long)ap[0], signame(ap[1], 1)); 398 else 399 (void)printf("(%#lx, SIG%s", 400 (long)ap[0], signame(ap[1], 1)); 401 ap += 2; 402 argsize -= 2 * sizeof(register_t); 403 break; 404 405 default: 406 /* No special handling */ 407 break; 408 } 409 } 410 while (argsize) { 411 if (decimal) 412 (void)printf("%c%ld", c, (long)*ap); 413 else 414 (void)printf("%c%#lx", c, (long)*ap); 415 c = ','; 416 ap++; 417 argsize -= sizeof(register_t); 418 } 419 (void)putchar(')'); 420 } 421 (void)putchar('\n'); 422 } 423 424 void 425 ktrsysret(ktr) 426 struct ktr_sysret *ktr; 427 { 428 const struct emulation *revelant; 429 register_t ret = ktr->ktr_retval; 430 int error = ktr->ktr_error; 431 int code = ktr->ktr_code; 432 433 if (emul_changed) 434 revelant = previous; 435 else 436 revelant = current; 437 emul_changed = 0; 438 439 if ((code >= revelant->nsysnames || code < 0 || plain > 1) 440 && (mach_traps_dispatch(&code, &revelant) == 0)) 441 (void)printf("[%d] ", code); 442 else 443 (void)printf("%s ", revelant->sysnames[code]); 444 445 switch (error) { 446 case 0: 447 if (!plain) { 448 (void)printf("%ld", (long)ret); 449 if (ret < 0 || ret > 9) 450 (void)printf("/%#lx", (long)ret); 451 } else { 452 if (decimal) 453 (void)printf("%ld", (long)ret); 454 else 455 (void)printf("%#lx", (long)ret); 456 } 457 break; 458 459 default: 460 eprint(error); 461 break; 462 } 463 (void)putchar('\n'); 464 } 465 466 /* 467 * We print the original emulation's error numerically, but we 468 * translate it to netbsd to print it symbolically. 469 */ 470 void 471 eprint(e) 472 int e; 473 { 474 int i = e; 475 476 if (current->errnomap) { 477 478 /* No remapping for ERESTART and EJUSTRETURN */ 479 /* Kludge for linux that has negative error numbers */ 480 if (current->errnomap[2] > 0 && e < 0) 481 goto normal; 482 483 for (i = 0; i < current->nerrnomap; i++) 484 if (e == current->errnomap[i]) 485 break; 486 487 if (i == current->nerrnomap) { 488 printf("-1 unknown errno %d", e); 489 return; 490 } 491 } 492 493 normal: 494 switch (i) { 495 case ERESTART: 496 (void)printf("RESTART"); 497 break; 498 499 case EJUSTRETURN: 500 (void)printf("JUSTRETURN"); 501 break; 502 503 default: 504 (void)printf("-1 errno %d", e); 505 if (!plain) 506 (void)printf(" %s", strerror(i)); 507 } 508 } 509 510 void 511 ktrnamei(cp, len) 512 char *cp; 513 int len; 514 { 515 516 (void)printf("\"%.*s\"\n", len, cp); 517 } 518 519 void 520 ktremul(name, len, bufsize) 521 char *name; 522 int len, bufsize; 523 { 524 if (len >= bufsize) 525 len = bufsize - 1; 526 527 name[len] = '\0'; 528 setemul(name, ktr_header.ktr_pid, 1); 529 emul_changed = 1; 530 531 (void)printf("\"%s\"\n", name); 532 } 533 534 void 535 ktrgenio(ktr, len) 536 struct ktr_genio *ktr; 537 int len; 538 { 539 int datalen = len - sizeof (struct ktr_genio); 540 char *dp = (char *)ktr + sizeof (struct ktr_genio); 541 char *cp; 542 int col = 0; 543 int width; 544 char visbuf[5]; 545 static int screenwidth = 0; 546 547 if (screenwidth == 0) { 548 struct winsize ws; 549 550 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 551 ws.ws_col > 8) 552 screenwidth = ws.ws_col; 553 else 554 screenwidth = 80; 555 } 556 printf("fd %d %s %d bytes\n", ktr->ktr_fd, 557 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 558 if (maxdata == 0) 559 return; 560 if (maxdata > 0 && datalen > maxdata) 561 datalen = maxdata; 562 (void)printf(" \""); 563 col = 8; 564 for (; datalen > 0; datalen--, dp++) { 565 (void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0); 566 cp = visbuf; 567 /* 568 * Keep track of printables and 569 * space chars (like fold(1)). 570 */ 571 if (col == 0) { 572 (void)putchar('\t'); 573 col = 8; 574 } 575 switch(*cp) { 576 case '\n': 577 col = 0; 578 (void)putchar('\n'); 579 continue; 580 case '\t': 581 width = 8 - (col&07); 582 break; 583 default: 584 width = strlen(cp); 585 } 586 if (col + width > (screenwidth-2)) { 587 (void)printf("\\\n\t"); 588 col = 8; 589 } 590 col += width; 591 do { 592 (void)putchar(*cp++); 593 } while (*cp); 594 } 595 if (col == 0) 596 (void)printf(" "); 597 (void)printf("\"\n"); 598 } 599 600 void 601 ktrpsig(psig) 602 struct ktr_psig *psig; 603 { 604 int signo, first; 605 606 (void)printf("SIG%s ", signame(psig->signo, 0)); 607 if (psig->action == SIG_DFL) 608 (void)printf("SIG_DFL\n"); 609 else { 610 (void)printf("caught handler=0x%lx mask=(", 611 (u_long)psig->action); 612 first = 1; 613 for (signo = 1; signo < NSIG; signo++) { 614 if (sigismember(&psig->mask, signo)) { 615 if (first) 616 first = 0; 617 else 618 (void)printf(","); 619 (void)printf("%d", signo); 620 } 621 } 622 (void)printf(") code=0x%x\n", psig->code); 623 } 624 } 625 626 void 627 ktrcsw(cs) 628 struct ktr_csw *cs; 629 { 630 631 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 632 cs->user ? "user" : "kernel"); 633 } 634 635 void 636 ktruser(usr, len) 637 struct ktr_user *usr; 638 int len; 639 { 640 int i; 641 unsigned char *dta; 642 643 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len); 644 dta = (unsigned char *)usr; 645 for(i=sizeof(struct ktr_user); i < len; i++) 646 printf("%02x", (unsigned int) dta[i]); 647 printf("\"\n"); 648 } 649 650 void 651 ktrmmsg(mmsg, len) 652 struct ktr_mmsg *mmsg; 653 int len; 654 { 655 int i,j; 656 unsigned char *data; 657 int row_len = 16; 658 int aligned_len; 659 660 printf("id %d [0x%x -> 0x%x] %d bytes, flags 0x%x", 661 mmsg->ktr_id, mmsg->ktr_local_port, 662 mmsg->ktr_remote_port, mmsg->ktr_size, mmsg->ktr_bits); 663 664 data = (unsigned char *)mmsg; 665 aligned_len = (len & ~(row_len - 1)) + row_len; 666 for (i = 0; i < aligned_len; i += row_len) { 667 printf("\n\t0x%04x ", i); 668 669 for (j = 0; j < row_len; j += sizeof(int)) 670 if ((i + j) < len) 671 printf("0x%08x ", *((int *)&data[i + j])); 672 else 673 printf(" "); 674 675 printf(" "); 676 677 for (j = 0; j < row_len; j++) { 678 if ((i + j) < len) { 679 if (isprint(data[i + j])) 680 printf("%c", data[i + j]); 681 else 682 printf("."); 683 } else { 684 printf(" "); 685 } 686 } 687 } 688 689 if (aligned_len != sizeof(struct ktr_mmsg)) 690 printf("\n"); 691 } 692 693 static const char * 694 signame(long sig, int xlat) 695 { 696 static char buf[64]; 697 if (sig == 0) 698 return " 0"; 699 else if (sig < 0 || sig >= NSIG) { 700 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig); 701 return buf; 702 } else 703 return sys_signame[(xlat && current->signalmap != NULL) ? 704 current->signalmap[sig] : sig]; 705 } 706 707 void 708 usage() 709 { 710 711 (void)fprintf(stderr, "usage: kdump [-dlNnRT] [-e emulation] " 712 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] " 713 "[file]\n"); 714 exit(1); 715 } 716