1 /* $NetBSD: dump.c,v 1.20 2004/04/21 01:05:47 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95"; 41 #endif 42 __RCSID("$NetBSD: dump.c,v 1.20 2004/04/21 01:05:47 christos Exp $"); 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #define _KERNEL 47 #include <sys/errno.h> 48 #undef _KERNEL 49 #include <sys/ioctl.h> 50 #include <sys/time.h> 51 #include <sys/uio.h> 52 #include <sys/ktrace.h> 53 #include <sys/ptrace.h> 54 #include <sys/queue.h> 55 56 #include <err.h> 57 #include <signal.h> 58 #include <stdarg.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 "misc.h" 68 #include "setemul.h" 69 70 int timestamp, decimal, fancy = 1, tail, maxdata; 71 72 int width; /* Keep track of current columns. */ 73 74 #include <sys/syscall.h> 75 76 static const char *const ptrace_ops[] = { 77 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U", 78 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE", 79 "PT_KILL", "PT_ATTACH", "PT_DETACH", 80 }; 81 82 struct ktr_entry { 83 TAILQ_ENTRY(ktr_entry) kte_list; 84 struct ktr_header kte_kth; 85 }; 86 87 TAILQ_HEAD(kteq, ktr_entry) ktependq = TAILQ_HEAD_INITIALIZER(ktependq); 88 89 void argprint(const char *, register_t **, int *); 90 void dumpheader(struct ktr_header *); 91 int dumprecord(int, FILE *); 92 void flushpendq(struct ktr_entry *); 93 int fread_tail(void *, int, int, FILE *); 94 void genioprint(struct ktr_header *); 95 struct ktr_entry * 96 getpendq(struct ktr_header *, int, struct kteq *); 97 struct ktr_entry * 98 getrecord(FILE *); 99 void indent(int); 100 void ioctldecode(u_long); 101 void ktrcsw(struct ktr_entry *); 102 void ktremul(struct ktr_entry *); 103 void ktrgenio(struct ktr_entry *); 104 void ktrnamei(struct ktr_entry *); 105 void ktrpsig(struct ktr_entry *); 106 void ktrsyscall(struct ktr_entry *); 107 void ktrsysret(struct ktr_entry *); 108 void nameiargprint(const char *, struct ktr_header *, register_t **, int *); 109 void nameiprint(struct ktr_header *); 110 void newline(void); 111 void putpendq(struct ktr_entry *); 112 void syscallnameprint(int); 113 void syscallprint(struct ktr_header *); 114 void sysretprint(struct ktr_header *); 115 int wprintf(const char *, ...); 116 void *xrealloc(void *, size_t *, size_t); 117 118 int 119 wprintf(const char *fmt, ...) 120 { 121 va_list ap; 122 int w; 123 124 va_start(ap, fmt); 125 w = vprintf(fmt, ap); 126 if (w == -1) 127 warn("vprintf"); 128 else 129 width += w; 130 va_end(ap); 131 return (w); 132 } 133 134 void 135 newline(void) 136 { 137 138 if (width > 0) { 139 printf("\n"); 140 width = 0; 141 } 142 } 143 144 void 145 indent(int col) 146 { 147 148 while (width < col) 149 if (wprintf(" ") < 0) 150 break; 151 } 152 153 void * 154 xrealloc(void *p, size_t *siz, size_t req) 155 { 156 157 if (*siz < req) { 158 if (*siz == 0) 159 *siz = 1; 160 while (*siz < req) 161 *siz <<= 1; 162 p = realloc(p, *siz); 163 if (p == NULL) 164 err(EXIT_FAILURE, "realloc: %lu bytes", 165 (u_long)*siz); 166 } 167 return (p); 168 } 169 170 struct ktr_entry * 171 getrecord(FILE *fp) 172 { 173 struct ktr_entry *kte; 174 struct ktr_header *kth; 175 char *cp; 176 size_t siz, len; 177 178 siz = 0; 179 kte = xrealloc(NULL, &siz, sizeof(struct ktr_entry)); 180 kth = &kte->kte_kth; 181 if (fread_tail(kth, sizeof(struct ktr_header), 1, fp) == 0) { 182 free(kte); 183 return (NULL); 184 } 185 186 len = kth->ktr_len; 187 if (len < 0) 188 errx(EXIT_FAILURE, "bogus length 0x%lx", (long)len); 189 if (len > 0) { 190 /* + 1 to ensure room for NUL terminate */ 191 kte = xrealloc(kte, &siz, sizeof(struct ktr_entry) + len + 1); 192 if (fread_tail(cp = (char *)(&kte->kte_kth + 1), 193 len, 1, fp) == 0) 194 errx(EXIT_FAILURE, "data too short"); 195 cp[len] = 0; 196 } 197 198 return (kte); 199 } 200 201 /* XXX: lwp. */ 202 #define KTE_TYPE(kte) ((kte)->kte_kth.ktr_type) 203 #define KTE_PID(kte) ((kte)->kte_kth.ktr_pid) 204 #define KTE_MATCH(kte, type, pid) \ 205 (KTE_TYPE(kte) == (type) && KTE_PID(kte) == (pid)) 206 207 void 208 putpendq(struct ktr_entry *kte) 209 { 210 211 TAILQ_INSERT_TAIL(&ktependq, kte, kte_list); 212 } 213 214 void 215 flushpendq(struct ktr_entry *us) 216 { 217 struct ktr_entry *kte, *kte_next; 218 int pid = KTE_PID(us); 219 220 for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) { 221 kte_next = TAILQ_NEXT(kte, kte_list); 222 if (KTE_PID(kte) == pid) { 223 TAILQ_REMOVE(&ktependq, kte, kte_list); 224 free(kte); 225 } 226 } 227 } 228 229 struct ktr_entry * 230 getpendq(struct ktr_header *us, int type, struct kteq *kteq) 231 { 232 struct ktr_entry *kte, *kte_next; 233 int pid = us->ktr_pid; 234 235 if (kteq != NULL) 236 TAILQ_INIT(kteq); 237 for (kte = TAILQ_FIRST(&ktependq); kte != NULL; kte = kte_next) { 238 kte_next = TAILQ_NEXT(kte, kte_list); 239 if (KTE_MATCH(kte, type, pid)) { 240 TAILQ_REMOVE(&ktependq, kte, kte_list); 241 if (kteq != NULL) 242 TAILQ_INSERT_TAIL(kteq, kte, kte_list); 243 else 244 break; 245 } 246 } 247 248 return (kteq ? TAILQ_FIRST(kteq) : kte); 249 } 250 251 int 252 dumprecord(int trpoints, FILE *fp) 253 { 254 struct ktr_entry *kte; 255 struct ktr_header *kth; 256 257 kte = getrecord(fp); 258 if (kte == NULL) 259 return (0); 260 261 kth = &kte->kte_kth; 262 if ((trpoints & (1 << kth->ktr_type)) == 0) { 263 free(kte); 264 goto out; 265 } 266 267 /* Update context to match currently processed record. */ 268 ectx_sanify(kth->ktr_pid); 269 270 switch (kth->ktr_type) { 271 case KTR_SYSCALL: 272 ktrsyscall(kte); 273 break; 274 case KTR_SYSRET: 275 ktrsysret(kte); 276 break; 277 case KTR_NAMEI: 278 putpendq(kte); 279 break; 280 case KTR_GENIO: 281 putpendq(kte); 282 break; 283 case KTR_PSIG: 284 ktrpsig(kte); 285 break; 286 case KTR_CSW: 287 ktrcsw(kte); 288 break; 289 case KTR_EMUL: 290 ktremul(kte); 291 break; 292 default: 293 /* 294 * XXX: Other types added recently. 295 */ 296 free(kte); 297 break; 298 } 299 newline(); 300 301 out: 302 return (1); 303 } 304 305 void 306 dumpfile(const char *file, int fd, int trpoints) 307 { 308 FILE *fp; 309 310 if (file == NULL || *file == 0) { 311 if ((fp = fdopen(fd, "r")) == NULL) 312 err(EXIT_FAILURE, "fdopen(%d)", fd); 313 } else if (strcmp(file, "-") == 0) 314 fp = stdin; 315 else if ((fp = fopen(file, "r")) == NULL) 316 err(EXIT_FAILURE, "fopen: %s", file); 317 318 for (width = 0; dumprecord(trpoints, fp) != 0;) 319 if (tail) 320 (void)fflush(stdout); 321 322 newline(); 323 324 /* 325 * XXX: Dump pending KTR_SYSCALL if any? 326 */ 327 } 328 329 int 330 fread_tail(void *buf, int size, int num, FILE *fp) 331 { 332 int i; 333 334 while ((i = fread(buf, size, num, fp)) == 0 && tail) { 335 (void)sleep(1); 336 clearerr(fp); 337 } 338 return (i); 339 } 340 341 void 342 dumpheader(struct ktr_header *kth) 343 { 344 static struct timeval prevtime; 345 struct timeval temp; 346 347 wprintf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm); 348 349 if (timestamp) { 350 if (timestamp == 2) { 351 timersub(&kth->ktr_time, &prevtime, &temp); 352 prevtime = kth->ktr_time; 353 } else 354 temp = kth->ktr_time; 355 wprintf("%ld.%06ld ", 356 (long int)temp.tv_sec, (long int)temp.tv_usec); 357 } 358 } 359 360 void 361 ioctldecode(u_long cmd) 362 { 363 char dirbuf[4], *dir = dirbuf; 364 365 if (cmd & IOC_OUT) 366 *dir++ = 'W'; 367 if (cmd & IOC_IN) 368 *dir++ = 'R'; 369 *dir = '\0'; 370 371 wprintf(decimal ? ", _IO%s('%c',%ld" : ", _IO%s('%c',%#lx", 372 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff); 373 if ((cmd & IOC_VOID) == 0) 374 wprintf(decimal ? ",%ld)" : ",%#lx)", 375 (cmd >> 16) & 0xff); 376 else 377 wprintf(")"); 378 } 379 380 void 381 nameiargprint(const char *prefix, struct ktr_header *kth, 382 register_t **ap, int *argsize) 383 { 384 struct ktr_entry *kte; 385 386 if (*argsize == 0) 387 errx(EXIT_FAILURE, "argument expected"); 388 /* 389 * XXX: binary emulation mode. 390 */ 391 kte = getpendq(kth, KTR_NAMEI, NULL); 392 if (kte == NULL) 393 argprint(prefix, ap, argsize); 394 else { 395 wprintf("%s", prefix); 396 nameiprint(&kte->kte_kth); 397 free(kte); 398 (*ap)++; 399 *argsize -= sizeof(register_t); 400 } 401 } 402 403 void 404 syscallnameprint(int code) 405 { 406 407 if (code >= cur_emul->nsysnames || code < 0) 408 wprintf("[%d]", code); 409 else 410 wprintf("%s", cur_emul->sysnames[code]); 411 } 412 413 void 414 argprint(const char *prefix, register_t **ap, int *argsize) 415 { 416 417 if (decimal) 418 wprintf("%s%ld", prefix, (long)**ap); 419 else 420 wprintf("%s%#lx", prefix, (long)**ap); 421 (*ap)++; 422 *argsize -= sizeof(register_t); 423 } 424 425 void 426 syscallprint(struct ktr_header *kth) 427 { 428 struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1); 429 register_t *ap; 430 char *s; 431 int argsize; 432 433 syscallnameprint(ktr->ktr_code); 434 435 /* 436 * Arguments processing. 437 */ 438 argsize = ktr->ktr_argsize; 439 if (argsize == 0) { 440 wprintf("("); 441 goto noargument; 442 } 443 444 ap = (register_t *)(ktr + 1); 445 if (!fancy) 446 goto print_first; 447 448 switch (ktr->ktr_code) { 449 /* 450 * All these have a path as the first param. 451 * The order is same as syscalls.master. 452 */ 453 case SYS_open: 454 case SYS_link: 455 case SYS_unlink: 456 case SYS_chdir: 457 case SYS_mknod: 458 case SYS_chmod: 459 case SYS_chown: 460 case SYS_unmount: 461 case SYS_access: 462 case SYS_chflags: 463 case SYS_acct: 464 case SYS_revoke: 465 case SYS_symlink: 466 case SYS_readlink: 467 case SYS_execve: 468 case SYS_chroot: 469 case SYS_rename: 470 case SYS_mkfifo: 471 case SYS_mkdir: 472 case SYS_rmdir: 473 case SYS_utimes: 474 case SYS_quotactl: 475 case SYS_statvfs1: 476 case SYS_getfh: 477 case SYS_pathconf: 478 case SYS_truncate: 479 case SYS_undelete: 480 case SYS___posix_rename: 481 case SYS_lchmod: 482 case SYS_lchown: 483 case SYS_lutimes: 484 case SYS___stat13: 485 case SYS___lstat13: 486 case SYS___posix_chown: 487 case SYS___posix_lchown: 488 case SYS_lchflags: 489 nameiargprint("(", kth, &ap, &argsize); 490 491 /* 492 * 2nd argument is also pathname. 493 */ 494 switch (ktr->ktr_code) { 495 case SYS_link: 496 case SYS_rename: 497 case SYS___posix_rename: 498 nameiargprint(", ", kth, &ap, &argsize); 499 break; 500 } 501 break; 502 503 case SYS_compat_16___sigaction14 : 504 wprintf("(%s", signals[(int)*ap].name); 505 ap++; 506 argsize -= sizeof(register_t); 507 break; 508 509 case SYS_ioctl : 510 argprint("(", &ap, &argsize); 511 if ((s = ioctlname(*ap)) != NULL) 512 wprintf(", %s", s); 513 else 514 ioctldecode(*ap); 515 ap++; 516 argsize -= sizeof(register_t); 517 break; 518 519 case SYS_ptrace : 520 if (*ap >= 0 && 521 *ap < sizeof(ptrace_ops) / sizeof(ptrace_ops[0])) 522 wprintf("(%s", ptrace_ops[*ap]); 523 else 524 wprintf("(%ld", (long)*ap); 525 ap++; 526 argsize -= sizeof(register_t); 527 break; 528 529 default: 530 print_first: 531 argprint("(", &ap, &argsize); 532 break; 533 } 534 535 /* Print rest of argument. */ 536 while (argsize > 0) 537 argprint(", ", &ap, &argsize); 538 539 noargument: 540 wprintf(")"); 541 } 542 543 void 544 ktrsyscall(struct ktr_entry *kte) 545 { 546 struct ktr_header *kth = &kte->kte_kth; 547 struct ktr_syscall *ktr = (struct ktr_syscall *)(kth + 1); 548 549 switch (ktr->ktr_code) { 550 case SYS_exit: 551 dumpheader(kth); 552 syscallprint(kth); 553 break; 554 default: 555 putpendq(kte); 556 return; 557 } 558 559 free(kte); 560 } 561 562 void 563 sysretprint(struct ktr_header *kth) 564 { 565 struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1); 566 register_t ret = ktr->ktr_retval; 567 int error = ktr->ktr_error; 568 569 indent(50); 570 if (error == EJUSTRETURN) 571 wprintf(" JUSTRETURN"); 572 else if (error == ERESTART) 573 wprintf(" RESTART"); 574 else if (error) { 575 wprintf(" Err#%d", error); 576 if (error < MAXERRNOS && error >= -2) 577 wprintf(" %s", errnos[error].name); 578 } else 579 switch (ktr->ktr_code) { 580 case SYS_mmap: 581 wprintf(" = %p", (long)ret); 582 break; 583 default: 584 wprintf(" = %ld", (long)ret); 585 if (kth->ktr_len > offsetof(struct ktr_sysret, 586 ktr_retval_1) && ktr->ktr_retval_1 != 0) 587 wprintf(", %ld", (long)ktr->ktr_retval_1); 588 break; 589 } 590 } 591 592 void 593 ktrsysret(struct ktr_entry *kte) 594 { 595 struct ktr_header *kth = &kte->kte_kth; 596 struct ktr_sysret *ktr = (struct ktr_sysret *)(kth + 1); 597 struct ktr_entry *genio; 598 struct ktr_entry *syscall; 599 600 dumpheader(kth); 601 602 /* Print syscall name and arguments. */ 603 syscall = getpendq(kth, KTR_SYSCALL, NULL); 604 if (syscall == NULL) 605 /* 606 * Possibilly a child of fork/vfork, or tracing of 607 * process started during system call. 608 */ 609 syscallnameprint(ktr->ktr_code); 610 else { 611 syscallprint(&syscall->kte_kth); 612 free(syscall); 613 } 614 615 /* Print return value and an error if any. */ 616 sysretprint(kth); 617 618 genio = getpendq(kth, KTR_GENIO, NULL); 619 if (genio != NULL) { 620 genioprint(&genio->kte_kth); 621 free(genio); 622 } 623 624 flushpendq(kte); 625 free(kte); 626 } 627 628 void 629 nameiprint(struct ktr_header *kth) 630 { 631 632 wprintf("\"%.*s\"", kth->ktr_len, (char *)(kth + 1)); 633 } 634 635 #ifdef notused 636 void 637 ktrnamei(struct ktr_entry *kte) 638 { 639 struct ktr_header *kth = &kte->kte_kth; 640 641 dumpheader(kth); 642 wprintf("namei("); 643 nameiprint(kth); 644 wprintf(")"); 645 646 free(kte); 647 } 648 #endif 649 650 void 651 ktremul(struct ktr_entry *kte) 652 { 653 struct ktr_header *kth = &kte->kte_kth; 654 char *emul = (char *)(kth + 1); 655 656 dumpheader(kth); 657 wprintf("emul(%s)", emul); 658 setemul(emul, kth->ktr_pid, 1); 659 660 free(kte); 661 } 662 663 void 664 genioprint(struct ktr_header *kth) 665 { 666 struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1); 667 static int screenwidth = 0; 668 int datalen = kth->ktr_len - sizeof(struct ktr_genio); 669 /* 670 * Need to be unsigned type so that positive value is passed 671 * to vis(), which will call isgraph(). 672 */ 673 unsigned char *dp = (unsigned char *)(ktr + 1); 674 int w; 675 char visbuf[5]; 676 677 if (screenwidth == 0) { 678 struct winsize ws; 679 680 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 681 ws.ws_col > 8) 682 screenwidth = ws.ws_col; 683 else 684 screenwidth = 80; 685 } 686 687 if (maxdata && datalen > maxdata) 688 datalen = maxdata; 689 newline(); 690 wprintf(" \""); 691 for (; datalen > 0; datalen--, dp++) { 692 (void) vis(visbuf, *dp, VIS_NL|VIS_TAB|VIS_CSTYLE, 693 /* We put NUL at the end of buffer when reading */ 694 *(dp + 1)); 695 visbuf[4] = '\0'; 696 w = strlen(visbuf); 697 if (width + w + 2 >= screenwidth) 698 break; 699 wprintf("%s", visbuf); 700 if (width + 2 >= screenwidth) 701 break; 702 } 703 wprintf("\""); 704 } 705 706 #ifdef notused 707 void 708 ktrgenio(struct ktr_entry *kte) 709 { 710 struct ktr_header *kth = &kte->kte_kth; 711 struct ktr_genio *ktr = (struct ktr_genio *)(kth + 1); 712 713 dumpheader(kth); 714 wprintf("genio fd %d %s", 715 ktr->ktr_fd, ktr->ktr_rw ? "write" : "read"); 716 genioprint(kth); 717 718 free(kte); 719 } 720 #endif 721 722 void 723 ktrpsig(struct ktr_entry *kte) 724 { 725 struct ktr_header *kth = &kte->kte_kth; 726 struct ktr_psig *psig = (struct ktr_psig *)(kth + 1); 727 728 dumpheader(kth); 729 wprintf("SIG%s ", sys_signame[psig->signo]); 730 if (psig->action == SIG_DFL) 731 wprintf("SIG_DFL"); 732 else { 733 wprintf("caught handler=0x%lx mask=0x%lx code=0x%x", 734 (u_long)psig->action, (unsigned long)psig->mask.__bits[0], 735 psig->code); 736 } 737 738 free(kte); 739 } 740 741 void 742 ktrcsw(struct ktr_entry *kte) 743 { 744 struct ktr_header *kth = &kte->kte_kth; 745 struct ktr_csw *cs = (struct ktr_csw *)(kth + 1); 746 747 dumpheader(kth); 748 wprintf("%s %s", cs->out ? "stop" : "resume", 749 cs->user ? "user" : "kernel"); 750 751 free(kte); 752 } 753