1 /*- 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1988, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 #endif /* not lint */ 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #define _KERNEL 49 extern int errno; 50 #include <sys/errno.h> 51 #undef _KERNEL 52 #include <sys/param.h> 53 #include <sys/errno.h> 54 #define _KERNEL 55 #include <sys/time.h> 56 #undef _KERNEL 57 #include <sys/uio.h> 58 #include <sys/ktrace.h> 59 #include <sys/ioctl.h> 60 #include <sys/socket.h> 61 #include <dlfcn.h> 62 #include <err.h> 63 #include <locale.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #include <vis.h> 69 #include "ktrace.h" 70 #include "kdump_subr.h" 71 72 int fread_tail(void *, int, int); 73 void dumpheader(struct ktr_header *); 74 void ktrsyscall(struct ktr_syscall *); 75 void ktrsysret(struct ktr_sysret *); 76 void ktrnamei(char *, int); 77 void hexdump(char *, int, int); 78 void visdump(char *, int, int); 79 void ktrgenio(struct ktr_genio *, int); 80 void ktrpsig(struct ktr_psig *); 81 void ktrcsw(struct ktr_csw *); 82 void ktruser(int, unsigned char *); 83 void usage(void); 84 const char *ioctlname(u_long); 85 86 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata; 87 const char *tracefile = DEF_TRACEFILE; 88 struct ktr_header ktr_header; 89 90 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 91 92 int 93 main(int argc, char *argv[]) 94 { 95 int ch, ktrlen, size; 96 void *m; 97 int trpoints = ALL_POINTS; 98 int drop_logged; 99 pid_t pid = 0; 100 101 (void) setlocale(LC_CTYPE, ""); 102 103 while ((ch = getopt(argc,argv,"f:dElm:np:HRsTt:")) != -1) 104 switch((char)ch) { 105 case 'f': 106 tracefile = optarg; 107 break; 108 case 'd': 109 decimal = 1; 110 break; 111 case 'l': 112 tail = 1; 113 break; 114 case 'm': 115 maxdata = atoi(optarg); 116 break; 117 case 'n': 118 fancy = 0; 119 break; 120 case 'p': 121 pid = atoi(optarg); 122 break; 123 case 's': 124 suppressdata = 1; 125 break; 126 case 'E': 127 timestamp = 3; /* elapsed timestamp */ 128 break; 129 case 'H': 130 threads = 1; 131 break; 132 case 'R': 133 timestamp = 2; /* relative timestamp */ 134 break; 135 case 'T': 136 timestamp = 1; 137 break; 138 case 't': 139 trpoints = getpoints(optarg); 140 if (trpoints < 0) 141 errx(1, "unknown trace point in %s", optarg); 142 break; 143 default: 144 usage(); 145 } 146 147 if (argc > optind) 148 usage(); 149 150 m = (void *)malloc(size = 1025); 151 if (m == NULL) 152 errx(1, "%s", strerror(ENOMEM)); 153 if (!freopen(tracefile, "r", stdin)) 154 err(1, "%s", tracefile); 155 drop_logged = 0; 156 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 157 if (ktr_header.ktr_type & KTR_DROP) { 158 ktr_header.ktr_type &= ~KTR_DROP; 159 if (!drop_logged && threads) { 160 (void)printf("%6d %6d %-8.*s Events dropped.\n", 161 ktr_header.ktr_pid, ktr_header.ktr_tid > 162 0 ? ktr_header.ktr_tid : 0, MAXCOMLEN, 163 ktr_header.ktr_comm); 164 drop_logged = 1; 165 } else if (!drop_logged) { 166 (void)printf("%6d %-8.*s Events dropped.\n", 167 ktr_header.ktr_pid, MAXCOMLEN, 168 ktr_header.ktr_comm); 169 drop_logged = 1; 170 } 171 } 172 if (trpoints & (1<<ktr_header.ktr_type)) 173 if (pid == 0 || ktr_header.ktr_pid == pid) 174 dumpheader(&ktr_header); 175 if ((ktrlen = ktr_header.ktr_len) < 0) 176 errx(1, "bogus length 0x%x", ktrlen); 177 if (ktrlen > size) { 178 m = (void *)realloc(m, ktrlen+1); 179 if (m == NULL) 180 errx(1, "%s", strerror(ENOMEM)); 181 size = ktrlen; 182 } 183 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 184 errx(1, "data too short"); 185 if (pid && ktr_header.ktr_pid != pid) 186 continue; 187 if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 188 continue; 189 drop_logged = 0; 190 switch (ktr_header.ktr_type) { 191 case KTR_SYSCALL: 192 ktrsyscall((struct ktr_syscall *)m); 193 break; 194 case KTR_SYSRET: 195 ktrsysret((struct ktr_sysret *)m); 196 break; 197 case KTR_NAMEI: 198 ktrnamei(m, ktrlen); 199 break; 200 case KTR_GENIO: 201 ktrgenio((struct ktr_genio *)m, ktrlen); 202 break; 203 case KTR_PSIG: 204 ktrpsig((struct ktr_psig *)m); 205 break; 206 case KTR_CSW: 207 ktrcsw((struct ktr_csw *)m); 208 break; 209 case KTR_USER: 210 ktruser(ktrlen, m); 211 break; 212 default: 213 printf("\n"); 214 break; 215 } 216 if (tail) 217 (void)fflush(stdout); 218 } 219 return 0; 220 } 221 222 int 223 fread_tail(void *buf, int size, int num) 224 { 225 int i; 226 227 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 228 (void)sleep(1); 229 clearerr(stdin); 230 } 231 return (i); 232 } 233 234 void 235 dumpheader(struct ktr_header *kth) 236 { 237 static char unknown[64]; 238 static struct timeval prevtime, temp; 239 const char *type; 240 241 switch (kth->ktr_type) { 242 case KTR_SYSCALL: 243 type = "CALL"; 244 break; 245 case KTR_SYSRET: 246 type = "RET "; 247 break; 248 case KTR_NAMEI: 249 type = "NAMI"; 250 break; 251 case KTR_GENIO: 252 type = "GIO "; 253 break; 254 case KTR_PSIG: 255 type = "PSIG"; 256 break; 257 case KTR_CSW: 258 type = "CSW"; 259 break; 260 case KTR_USER: 261 type = "USER"; 262 break; 263 default: 264 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 265 type = unknown; 266 } 267 268 /* 269 * The ktr_tid field was previously the ktr_buffer field, which held 270 * the kernel pointer value for the buffer associated with data 271 * following the record header. It now holds a threadid, but only 272 * for trace files after the change. Older trace files still contain 273 * kernel pointers. Detect this and suppress the results by printing 274 * negative tid's as 0. 275 */ 276 if (threads) 277 (void)printf("%6d %6d %-8.*s ", kth->ktr_pid, kth->ktr_tid > 278 0 ? kth->ktr_tid : 0, MAXCOMLEN, kth->ktr_comm); 279 else 280 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, 281 kth->ktr_comm); 282 if (timestamp) { 283 if (timestamp == 3) { 284 if (prevtime.tv_sec == 0) 285 prevtime = kth->ktr_time; 286 timevalsub(&kth->ktr_time, &prevtime); 287 } 288 if (timestamp == 2) { 289 temp = kth->ktr_time; 290 timevalsub(&kth->ktr_time, &prevtime); 291 prevtime = temp; 292 } 293 (void)printf("%ld.%06ld ", 294 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec); 295 } 296 (void)printf("%s ", type); 297 } 298 299 #include <sys/syscall.h> 300 #define KTRACE 301 #include <sys/kern/syscalls.c> 302 #undef KTRACE 303 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]); 304 305 void 306 ktrsyscall(struct ktr_syscall *ktr) 307 { 308 int narg = ktr->ktr_narg; 309 register_t *ip; 310 311 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0) 312 (void)printf("[%d]", ktr->ktr_code); 313 else 314 (void)printf("%s", syscallnames[ktr->ktr_code]); 315 ip = &ktr->ktr_args[0]; 316 if (narg) { 317 char c = '('; 318 if (fancy) { 319 320 #define print_number(i,n,c) do { \ 321 if (decimal) \ 322 (void)printf("%c%ld", c, (long)*i); \ 323 else \ 324 (void)printf("%c%#lx", c, (long)*i); \ 325 i++; \ 326 n--; \ 327 c = ','; \ 328 } while (0); 329 330 if (ktr->ktr_code == SYS_ioctl) { 331 const char *cp; 332 print_number(ip,narg,c); 333 if ((cp = ioctlname(*ip)) != NULL) 334 (void)printf(",%s", cp); 335 else { 336 if (decimal) 337 (void)printf(",%ld", (long)*ip); 338 else 339 (void)printf(",%#lx ", (long)*ip); 340 } 341 c = ','; 342 ip++; 343 narg--; 344 } else if (ktr->ktr_code == SYS_ptrace) { 345 (void)putchar('('); 346 ptraceopname ((int)*ip); 347 c = ','; 348 ip++; 349 narg--; 350 } else if (ktr->ktr_code == SYS_access || 351 ktr->ktr_code == SYS_eaccess) { 352 print_number(ip,narg,c); 353 (void)putchar(','); 354 accessmodename ((int)*ip); 355 ip++; 356 narg--; 357 } else if (ktr->ktr_code == SYS_open) { 358 int flags; 359 int mode; 360 print_number(ip,narg,c); 361 flags = *ip; 362 mode = *++ip; 363 (void)putchar(','); 364 flagsandmodename (flags, mode, decimal); 365 ip++; 366 narg-=2; 367 } else if (ktr->ktr_code == SYS_wait4) { 368 print_number(ip,narg,c); 369 print_number(ip,narg,c); 370 (void)putchar(','); 371 wait4optname ((int)*ip); 372 ip++; 373 narg--; 374 } else if (ktr->ktr_code == SYS_chmod || 375 ktr->ktr_code == SYS_fchmod || 376 ktr->ktr_code == SYS_lchmod) { 377 print_number(ip,narg,c); 378 (void)putchar(','); 379 modename ((int)*ip); 380 ip++; 381 narg--; 382 } else if (ktr->ktr_code == SYS_mknod) { 383 print_number(ip,narg,c); 384 (void)putchar(','); 385 modename ((int)*ip); 386 ip++; 387 narg--; 388 } else if (ktr->ktr_code == SYS_getfsstat) { 389 print_number(ip,narg,c); 390 print_number(ip,narg,c); 391 (void)putchar(','); 392 getfsstatflagsname ((int)*ip); 393 ip++; 394 narg--; 395 } else if (ktr->ktr_code == SYS_mount) { 396 print_number(ip,narg,c); 397 print_number(ip,narg,c); 398 (void)putchar(','); 399 mountflagsname ((int)*ip); 400 ip++; 401 narg--; 402 } else if (ktr->ktr_code == SYS_unmount) { 403 print_number(ip,narg,c); 404 (void)putchar(','); 405 mountflagsname ((int)*ip); 406 ip++; 407 narg--; 408 } else if (ktr->ktr_code == SYS_recvmsg || 409 ktr->ktr_code == SYS_sendmsg) { 410 print_number(ip,narg,c); 411 print_number(ip,narg,c); 412 (void)putchar(','); 413 sendrecvflagsname ((int)*ip); 414 ip++; 415 narg--; 416 } else if (ktr->ktr_code == SYS_recvfrom || 417 ktr->ktr_code == SYS_sendto) { 418 print_number(ip,narg,c); 419 print_number(ip,narg,c); 420 print_number(ip,narg,c); 421 (void)putchar(','); 422 sendrecvflagsname ((int)*ip); 423 ip++; 424 narg--; 425 } else if (ktr->ktr_code == SYS_chflags || 426 ktr->ktr_code == SYS_fchflags || 427 ktr->ktr_code == SYS_lchflags) { 428 print_number(ip,narg,c); 429 (void)putchar(','); 430 modename((int)*ip); 431 ip++; 432 narg--; 433 } else if (ktr->ktr_code == SYS_kill) { 434 print_number(ip,narg,c); 435 (void)putchar(','); 436 signame((int)*ip); 437 ip++; 438 narg--; 439 } else if (ktr->ktr_code == SYS_reboot) { 440 (void)putchar('('); 441 rebootoptname((int)*ip); 442 ip++; 443 narg--; 444 } else if (ktr->ktr_code == SYS_umask) { 445 (void)putchar('('); 446 modename((int)*ip); 447 ip++; 448 narg--; 449 } else if (ktr->ktr_code == SYS_msync) { 450 print_number(ip,narg,c); 451 print_number(ip,narg,c); 452 (void)putchar(','); 453 msyncflagsname((int)*ip); 454 ip++; 455 narg--; 456 } else if (ktr->ktr_code == SYS_mmap) { 457 print_number(ip,narg,c); 458 print_number(ip,narg,c); 459 (void)putchar(','); 460 mmapprotname ((int)*ip); 461 (void)putchar(','); 462 ip++; 463 narg--; 464 mmapflagsname ((int)*ip); 465 ip++; 466 narg--; 467 } else if (ktr->ktr_code == SYS_mprotect) { 468 print_number(ip,narg,c); 469 print_number(ip,narg,c); 470 (void)putchar(','); 471 mmapprotname ((int)*ip); 472 ip++; 473 narg--; 474 } else if (ktr->ktr_code == SYS_madvise) { 475 print_number(ip,narg,c); 476 print_number(ip,narg,c); 477 (void)putchar(','); 478 madvisebehavname((int)*ip); 479 ip++; 480 narg--; 481 } else if (ktr->ktr_code == SYS_setpriority) { 482 print_number(ip,narg,c); 483 print_number(ip,narg,c); 484 (void)putchar(','); 485 prioname((int)*ip); 486 ip++; 487 narg--; 488 } else if (ktr->ktr_code == SYS_fcntl) { 489 int cmd; 490 int arg; 491 print_number(ip,narg,c); 492 cmd = *ip; 493 arg = *++ip; 494 (void)putchar(','); 495 fcntlcmdname(cmd, arg, decimal); 496 ip++; 497 narg-=2; 498 } else if (ktr->ktr_code == SYS_socket) { 499 int sockdomain; 500 (void)putchar('('); 501 sockdomain=(int)*ip; 502 sockdomainname(sockdomain); 503 ip++; 504 narg--; 505 (void)putchar(','); 506 socktypename((int)*ip); 507 ip++; 508 narg--; 509 if (sockdomain == PF_INET || 510 sockdomain == PF_INET6) { 511 (void)putchar(','); 512 sockipprotoname((int)*ip); 513 ip++; 514 narg--; 515 } 516 c = ','; 517 } else if (ktr->ktr_code == SYS_setsockopt || 518 ktr->ktr_code == SYS_getsockopt) { 519 print_number(ip,narg,c); 520 (void)putchar(','); 521 sockoptlevelname((int)*ip, decimal); 522 ip++; 523 narg--; 524 (void)putchar(','); 525 sockoptname((int)*ip); 526 ip++; 527 narg--; 528 } else if (ktr->ktr_code == SYS_lseek) { 529 print_number(ip,narg,c); 530 /* Hidden 'pad' argument, not in lseek(2) */ 531 print_number(ip,narg,c); 532 print_number(ip,narg,c); 533 (void)putchar(','); 534 whencename ((int)*ip); 535 ip++; 536 narg--; 537 } else if (ktr->ktr_code == SYS_flock) { 538 print_number(ip,narg,c); 539 (void)putchar(','); 540 flockname((int)*ip); 541 ip++; 542 narg--; 543 } else if (ktr->ktr_code == SYS_mkfifo || 544 ktr->ktr_code == SYS_mkdir) { 545 print_number(ip,narg,c); 546 (void)putchar(','); 547 modename((int)*ip); 548 ip++; 549 narg--; 550 } else if (ktr->ktr_code == SYS_shutdown) { 551 print_number(ip,narg,c); 552 (void)putchar(','); 553 shutdownhowname((int)*ip); 554 ip++; 555 narg--; 556 } else if (ktr->ktr_code == SYS_socketpair) { 557 (void)putchar('('); 558 sockdomainname((int)*ip); 559 ip++; 560 narg--; 561 (void)putchar(','); 562 socktypename((int)*ip); 563 ip++; 564 narg--; 565 c = ','; 566 } else if (ktr->ktr_code == SYS_getrlimit || 567 ktr->ktr_code == SYS_setrlimit) { 568 (void)putchar('('); 569 rlimitname((int)*ip); 570 ip++; 571 narg--; 572 c = ','; 573 } else if (ktr->ktr_code == SYS_quotactl) { 574 print_number(ip,narg,c); 575 quotactlname((int)*ip); 576 ip++; 577 narg--; 578 c = ','; 579 } else if (ktr->ktr_code == SYS_nfssvc) { 580 (void)putchar('('); 581 nfssvcname((int)*ip); 582 ip++; 583 narg--; 584 c = ','; 585 } else if (ktr->ktr_code == SYS_rtprio) { 586 (void)putchar('('); 587 rtprioname((int)*ip); 588 ip++; 589 narg--; 590 c = ','; 591 } else if (ktr->ktr_code == SYS___semctl) { 592 print_number(ip,narg,c); 593 print_number(ip,narg,c); 594 semctlname((int)*ip); 595 ip++; 596 narg--; 597 } else if (ktr->ktr_code == SYS_semget) { 598 print_number(ip,narg,c); 599 print_number(ip,narg,c); 600 semgetname((int)*ip); 601 ip++; 602 narg--; 603 } else if (ktr->ktr_code == SYS_msgctl) { 604 print_number(ip,narg,c); 605 shmctlname((int)*ip); 606 ip++; 607 narg--; 608 } else if (ktr->ktr_code == SYS_shmat) { 609 print_number(ip,narg,c); 610 print_number(ip,narg,c); 611 shmatname((int)*ip); 612 ip++; 613 narg--; 614 } else if (ktr->ktr_code == SYS_shmctl) { 615 print_number(ip,narg,c); 616 shmctlname((int)*ip); 617 ip++; 618 narg--; 619 } else if (ktr->ktr_code == SYS_minherit) { 620 print_number(ip,narg,c); 621 print_number(ip,narg,c); 622 minheritname((int)*ip); 623 ip++; 624 narg--; 625 } else if (ktr->ktr_code == SYS_rfork) { 626 (void)putchar('('); 627 rforkname((int)*ip); 628 ip++; 629 narg--; 630 c = ','; 631 } else if (ktr->ktr_code == SYS_lio_listio) { 632 (void)putchar('('); 633 lio_listioname((int)*ip); 634 ip++; 635 narg--; 636 c = ','; 637 } else if (ktr->ktr_code == SYS_mlockall) { 638 (void)putchar('('); 639 mlockallname((int)*ip); 640 ip++; 641 narg--; 642 } else if (ktr->ktr_code == SYS_sched_setscheduler) { 643 print_number(ip,narg,c); 644 schedpolicyname((int)*ip); 645 ip++; 646 narg--; 647 } else if (ktr->ktr_code == SYS_sched_get_priority_max || 648 ktr->ktr_code == SYS_sched_get_priority_min) { 649 (void)putchar('('); 650 schedpolicyname((int)*ip); 651 ip++; 652 narg--; 653 } else if (ktr->ktr_code == SYS_sendfile) { 654 print_number(ip,narg,c); 655 print_number(ip,narg,c); 656 print_number(ip,narg,c); 657 print_number(ip,narg,c); 658 print_number(ip,narg,c); 659 print_number(ip,narg,c); 660 sendfileflagsname((int)*ip); 661 ip++; 662 narg--; 663 } else if (ktr->ktr_code == SYS_kldsym) { 664 print_number(ip,narg,c); 665 kldsymcmdname((int)*ip); 666 ip++; 667 narg--; 668 } else if (ktr->ktr_code == SYS_sigprocmask) { 669 (void)putchar('('); 670 sigprocmaskhowname((int)*ip); 671 ip++; 672 narg--; 673 c = ','; 674 } else if (ktr->ktr_code == SYS___acl_get_file || 675 ktr->ktr_code == SYS___acl_set_file || 676 ktr->ktr_code == SYS___acl_get_fd || 677 ktr->ktr_code == SYS___acl_set_fd || 678 ktr->ktr_code == SYS___acl_delete_file || 679 ktr->ktr_code == SYS___acl_delete_fd || 680 ktr->ktr_code == SYS___acl_aclcheck_file || 681 ktr->ktr_code == SYS___acl_aclcheck_fd || 682 ktr->ktr_code == SYS___acl_get_link || 683 ktr->ktr_code == SYS___acl_set_link || 684 ktr->ktr_code == SYS___acl_delete_link || 685 ktr->ktr_code == SYS___acl_aclcheck_link) { 686 print_number(ip,narg,c); 687 acltypename((int)*ip); 688 ip++; 689 narg--; 690 } else if (ktr->ktr_code == SYS_sigaction) { 691 (void)putchar('('); 692 signame((int)*ip); 693 ip++; 694 narg--; 695 c = ','; 696 } else if (ktr->ktr_code == SYS_extattrctl) { 697 print_number(ip,narg,c); 698 extattrctlname((int)*ip); 699 ip++; 700 narg--; 701 } else if (ktr->ktr_code == SYS_nmount) { 702 print_number(ip,narg,c); 703 print_number(ip,narg,c); 704 (void)putchar(','); 705 mountflagsname ((int)*ip); 706 ip++; 707 narg--; 708 } else if (ktr->ktr_code == SYS_kse_thr_interrupt) { 709 print_number(ip,narg,c); 710 (void)putchar(','); 711 ksethrcmdname ((int)*ip); 712 ip++; 713 narg--; 714 } else if (ktr->ktr_code == SYS_thr_create) { 715 print_number(ip,narg,c); 716 print_number(ip,narg,c); 717 (void)putchar(','); 718 thrcreateflagsname ((int)*ip); 719 ip++; 720 narg--; 721 } else if (ktr->ktr_code == SYS_thr_kill) { 722 print_number(ip,narg,c); 723 (void)putchar(','); 724 signame ((int)*ip); 725 ip++; 726 narg--; 727 } else if (ktr->ktr_code == SYS_kldunloadf) { 728 print_number(ip,narg,c); 729 (void)putchar(','); 730 kldunloadfflagsname ((int)*ip); 731 ip++; 732 narg--; 733 } 734 } 735 while (narg) { 736 print_number(ip,narg,c); 737 } 738 (void)putchar(')'); 739 } 740 (void)putchar('\n'); 741 } 742 743 void 744 ktrsysret(struct ktr_sysret *ktr) 745 { 746 register_t ret = ktr->ktr_retval; 747 int error = ktr->ktr_error; 748 int code = ktr->ktr_code; 749 750 if (code >= nsyscalls || code < 0) 751 (void)printf("[%d] ", code); 752 else 753 (void)printf("%s ", syscallnames[code]); 754 755 if (error == 0) { 756 if (fancy) { 757 (void)printf("%d", ret); 758 if (ret < 0 || ret > 9) 759 (void)printf("/%#lx", (long)ret); 760 } else { 761 if (decimal) 762 (void)printf("%ld", (long)ret); 763 else 764 (void)printf("%#lx", (long)ret); 765 } 766 } else if (error == ERESTART) 767 (void)printf("RESTART"); 768 else if (error == EJUSTRETURN) 769 (void)printf("JUSTRETURN"); 770 else { 771 (void)printf("-1 errno %d", ktr->ktr_error); 772 if (fancy) 773 (void)printf(" %s", strerror(ktr->ktr_error)); 774 } 775 (void)putchar('\n'); 776 } 777 778 void 779 ktrnamei(char *cp, int len) 780 { 781 (void)printf("\"%.*s\"\n", len, cp); 782 } 783 784 void 785 hexdump(char *p, int len, int screenwidth) 786 { 787 int n, i; 788 int width; 789 790 width = 0; 791 do { 792 width += 2; 793 i = 13; /* base offset */ 794 i += (width / 2) + 1; /* spaces every second byte */ 795 i += (width * 2); /* width of bytes */ 796 i += 3; /* " |" */ 797 i += width; /* each byte */ 798 i += 1; /* "|" */ 799 } while (i < screenwidth); 800 width -= 2; 801 802 for (n = 0; n < len; n += width) { 803 for (i = n; i < n + width; i++) { 804 if ((i % width) == 0) { /* beginning of line */ 805 printf(" 0x%04x", i); 806 } 807 if ((i % 2) == 0) { 808 printf(" "); 809 } 810 if (i < len) 811 printf("%02x", p[i] & 0xff); 812 else 813 printf(" "); 814 } 815 printf(" |"); 816 for (i = n; i < n + width; i++) { 817 if (i >= len) 818 break; 819 if (p[i] >= ' ' && p[i] <= '~') 820 printf("%c", p[i]); 821 else 822 printf("."); 823 } 824 printf("|\n"); 825 } 826 if ((i % width) != 0) 827 printf("\n"); 828 } 829 830 void 831 visdump(char *dp, int datalen, int screenwidth) 832 { 833 int col = 0; 834 char *cp; 835 int width; 836 char visbuf[5]; 837 838 (void)printf(" \""); 839 col = 8; 840 for (;datalen > 0; datalen--, dp++) { 841 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1)); 842 cp = visbuf; 843 /* 844 * Keep track of printables and 845 * space chars (like fold(1)). 846 */ 847 if (col == 0) { 848 (void)putchar('\t'); 849 col = 8; 850 } 851 switch(*cp) { 852 case '\n': 853 col = 0; 854 (void)putchar('\n'); 855 continue; 856 case '\t': 857 width = 8 - (col&07); 858 break; 859 default: 860 width = strlen(cp); 861 } 862 if (col + width > (screenwidth-2)) { 863 (void)printf("\\\n\t"); 864 col = 8; 865 } 866 col += width; 867 do { 868 (void)putchar(*cp++); 869 } while (*cp); 870 } 871 if (col == 0) 872 (void)printf(" "); 873 (void)printf("\"\n"); 874 } 875 876 void 877 ktrgenio(struct ktr_genio *ktr, int len) 878 { 879 int datalen = len - sizeof (struct ktr_genio); 880 char *dp = (char *)ktr + sizeof (struct ktr_genio); 881 static int screenwidth = 0; 882 int i, binary; 883 884 if (screenwidth == 0) { 885 struct winsize ws; 886 887 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 888 ws.ws_col > 8) 889 screenwidth = ws.ws_col; 890 else 891 screenwidth = 80; 892 } 893 printf("fd %d %s %d byte%s\n", ktr->ktr_fd, 894 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen, 895 datalen == 1 ? "" : "s"); 896 if (suppressdata) 897 return; 898 if (maxdata && datalen > maxdata) 899 datalen = maxdata; 900 901 for (i = 0, binary = 0; i < datalen && binary == 0; i++) { 902 if (dp[i] >= 32 && dp[i] < 127) 903 continue; 904 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9) 905 continue; 906 binary = 1; 907 } 908 if (binary) 909 hexdump(dp, datalen, screenwidth); 910 else 911 visdump(dp, datalen, screenwidth); 912 } 913 914 const char *signames[] = { 915 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */ 916 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */ 917 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */ 918 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */ 919 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */ 920 "USR2", NULL, /* 31 - 32 */ 921 }; 922 923 void 924 ktrpsig(struct ktr_psig *psig) 925 { 926 if (psig->signo > 0 && psig->signo < NSIG) 927 (void)printf("SIG%s ", signames[psig->signo]); 928 else 929 (void)printf("SIG %d ", psig->signo); 930 if (psig->action == SIG_DFL) 931 (void)printf("SIG_DFL\n"); 932 else { 933 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n", 934 (u_long)psig->action, psig->mask.__bits[0], psig->code); 935 } 936 } 937 938 void 939 ktrcsw(struct ktr_csw *cs) 940 { 941 (void)printf("%s %s\n", cs->out ? "stop" : "resume", 942 cs->user ? "user" : "kernel"); 943 } 944 945 #define UTRACE_DLOPEN_START 1 946 #define UTRACE_DLOPEN_STOP 2 947 #define UTRACE_DLCLOSE_START 3 948 #define UTRACE_DLCLOSE_STOP 4 949 #define UTRACE_LOAD_OBJECT 5 950 #define UTRACE_UNLOAD_OBJECT 6 951 #define UTRACE_ADD_RUNDEP 7 952 #define UTRACE_PRELOAD_FINISHED 8 953 #define UTRACE_INIT_CALL 9 954 #define UTRACE_FINI_CALL 10 955 956 struct utrace_rtld { 957 char sig[4]; /* 'RTLD' */ 958 int event; 959 void *handle; 960 void *mapbase; 961 size_t mapsize; 962 int refcnt; 963 char name[MAXPATHLEN]; 964 }; 965 966 void 967 ktruser_rtld(int len, unsigned char *p) 968 { 969 struct utrace_rtld *ut = (struct utrace_rtld *)p; 970 void *parent; 971 int mode; 972 973 switch (ut->event) { 974 case UTRACE_DLOPEN_START: 975 mode = ut->refcnt; 976 printf("dlopen(%s, ", ut->name); 977 switch (mode & RTLD_MODEMASK) { 978 case RTLD_NOW: 979 printf("RTLD_NOW"); 980 break; 981 case RTLD_LAZY: 982 printf("RTLD_LAZY"); 983 break; 984 default: 985 printf("%#x", mode & RTLD_MODEMASK); 986 } 987 if (mode & RTLD_GLOBAL) 988 printf(" | RTLD_GLOBAL"); 989 if (mode & RTLD_TRACE) 990 printf(" | RTLD_TRACE"); 991 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)) 992 printf(" | %#x", mode & 993 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE)); 994 printf(")\n"); 995 break; 996 case UTRACE_DLOPEN_STOP: 997 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name, 998 ut->refcnt); 999 break; 1000 case UTRACE_DLCLOSE_START: 1001 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name, 1002 ut->refcnt); 1003 break; 1004 case UTRACE_DLCLOSE_STOP: 1005 printf("dlclose(%p) finished\n", ut->handle); 1006 break; 1007 case UTRACE_LOAD_OBJECT: 1008 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle, 1009 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 1010 ut->name); 1011 break; 1012 case UTRACE_UNLOAD_OBJECT: 1013 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle, 1014 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1, 1015 ut->name); 1016 break; 1017 case UTRACE_ADD_RUNDEP: 1018 parent = ut->mapbase; 1019 printf("RTLD: %p now depends on %p (%s, %d)\n", parent, 1020 ut->handle, ut->name, ut->refcnt); 1021 break; 1022 case UTRACE_PRELOAD_FINISHED: 1023 printf("RTLD: LD_PRELOAD finished\n"); 1024 break; 1025 case UTRACE_INIT_CALL: 1026 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle, 1027 ut->name); 1028 break; 1029 case UTRACE_FINI_CALL: 1030 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle, 1031 ut->name); 1032 break; 1033 default: 1034 p += 4; 1035 len -= 4; 1036 printf("RTLD: %d ", len); 1037 while (len--) 1038 if (decimal) 1039 printf(" %d", *p++); 1040 else 1041 printf(" %02x", *p++); 1042 printf("\n"); 1043 } 1044 } 1045 1046 struct utrace_malloc { 1047 void *p; 1048 size_t s; 1049 void *r; 1050 }; 1051 1052 void 1053 ktruser_malloc(int len, unsigned char *p) 1054 { 1055 struct utrace_malloc *ut = (struct utrace_malloc *)p; 1056 1057 if (ut->p == NULL) { 1058 if (ut->s == 0 && ut->r == NULL) 1059 printf("malloc_init()\n"); 1060 else 1061 printf("%p = malloc(%zu)\n", ut->r, ut->s); 1062 } else { 1063 if (ut->s == 0) 1064 printf("free(%p)\n", ut->p); 1065 else 1066 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s); 1067 } 1068 } 1069 1070 void 1071 ktruser(int len, unsigned char *p) 1072 { 1073 1074 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) { 1075 ktruser_rtld(len, p); 1076 return; 1077 } 1078 1079 if (len == sizeof(struct utrace_malloc)) { 1080 ktruser_malloc(len, p); 1081 return; 1082 } 1083 1084 (void)printf("%d ", len); 1085 while (len--) 1086 if (decimal) 1087 (void)printf(" %d", *p++); 1088 else 1089 (void)printf(" %02x", *p++); 1090 (void)printf("\n"); 1091 } 1092 1093 void 1094 usage(void) 1095 { 1096 (void)fprintf(stderr, 1097 "usage: kdump [-dEnlHRsT] [-f trfile] [-m maxdata] [-p pid] [-t [cnisuw]]\n"); 1098 exit(1); 1099 } 1100