kdump.c revision 4721
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1988, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 351590Srgrimesstatic char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1988, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 411590Srgrimesstatic char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 442215Scsgr#define KERNEL 452215Scsgrextern int errno; 462215Scsgr#include <sys/errno.h> 472215Scsgr#undef KERNEL 481590Srgrimes#include <sys/param.h> 491590Srgrimes#include <sys/errno.h> 501590Srgrimes#include <sys/time.h> 511590Srgrimes#include <sys/uio.h> 521590Srgrimes#include <sys/ktrace.h> 531590Srgrimes#include <sys/ioctl.h> 541590Srgrimes#include <sys/ptrace.h> 551590Srgrimes#include <vis.h> 561590Srgrimes#include <stdio.h> 571590Srgrimes#include <stdlib.h> 581590Srgrimes#include <string.h> 591590Srgrimes#include "ktrace.h" 601590Srgrimes 611590Srgrimesint timestamp, decimal, fancy = 1, tail, maxdata; 621590Srgrimeschar *tracefile = DEF_TRACEFILE; 631590Srgrimesstruct ktr_header ktr_header; 641590Srgrimes 651590Srgrimes#define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 661590Srgrimes 671590Srgrimesmain(argc, argv) 681590Srgrimes int argc; 691590Srgrimes char *argv[]; 701590Srgrimes{ 711590Srgrimes extern int optind; 721590Srgrimes extern char *optarg; 731590Srgrimes int ch, ktrlen, size; 741590Srgrimes register void *m; 751590Srgrimes int trpoints = ALL_POINTS; 761590Srgrimes 771590Srgrimes while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != EOF) 781590Srgrimes switch((char)ch) { 791590Srgrimes case 'f': 801590Srgrimes tracefile = optarg; 811590Srgrimes break; 821590Srgrimes case 'd': 831590Srgrimes decimal = 1; 841590Srgrimes break; 851590Srgrimes case 'l': 861590Srgrimes tail = 1; 871590Srgrimes break; 881590Srgrimes case 'm': 891590Srgrimes maxdata = atoi(optarg); 901590Srgrimes break; 911590Srgrimes case 'n': 921590Srgrimes fancy = 0; 931590Srgrimes break; 941590Srgrimes case 'R': 951590Srgrimes timestamp = 2; /* relative timestamp */ 961590Srgrimes break; 971590Srgrimes case 'T': 981590Srgrimes timestamp = 1; 991590Srgrimes break; 1001590Srgrimes case 't': 1011590Srgrimes trpoints = getpoints(optarg); 1021590Srgrimes if (trpoints < 0) { 1031590Srgrimes (void)fprintf(stderr, 1041590Srgrimes "kdump: unknown trace point in %s\n", 1051590Srgrimes optarg); 1061590Srgrimes exit(1); 1071590Srgrimes } 1081590Srgrimes break; 1091590Srgrimes default: 1101590Srgrimes usage(); 1111590Srgrimes } 1121590Srgrimes argv += optind; 1131590Srgrimes argc -= optind; 1141590Srgrimes 1151590Srgrimes if (argc > 1) 1161590Srgrimes usage(); 1171590Srgrimes 1181590Srgrimes m = (void *)malloc(size = 1025); 1191590Srgrimes if (m == NULL) { 1201590Srgrimes (void)fprintf(stderr, "kdump: %s.\n", strerror(ENOMEM)); 1211590Srgrimes exit(1); 1221590Srgrimes } 1231590Srgrimes if (!freopen(tracefile, "r", stdin)) { 1241590Srgrimes (void)fprintf(stderr, 1251590Srgrimes "kdump: %s: %s.\n", tracefile, strerror(errno)); 1261590Srgrimes exit(1); 1271590Srgrimes } 1281590Srgrimes while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 1291590Srgrimes if (trpoints & (1<<ktr_header.ktr_type)) 1301590Srgrimes dumpheader(&ktr_header); 1311590Srgrimes if ((ktrlen = ktr_header.ktr_len) < 0) { 1321590Srgrimes (void)fprintf(stderr, 1331590Srgrimes "kdump: bogus length 0x%x\n", ktrlen); 1341590Srgrimes exit(1); 1351590Srgrimes } 1361590Srgrimes if (ktrlen > size) { 1371590Srgrimes m = (void *)realloc(m, ktrlen+1); 1381590Srgrimes if (m == NULL) { 1391590Srgrimes (void)fprintf(stderr, 1401590Srgrimes "kdump: %s.\n", strerror(ENOMEM)); 1411590Srgrimes exit(1); 1421590Srgrimes } 1431590Srgrimes size = ktrlen; 1441590Srgrimes } 1451590Srgrimes if (ktrlen && fread_tail(m, ktrlen, 1) == 0) { 1461590Srgrimes (void)fprintf(stderr, "kdump: data too short.\n"); 1471590Srgrimes exit(1); 1481590Srgrimes } 1491590Srgrimes if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 1501590Srgrimes continue; 1511590Srgrimes switch (ktr_header.ktr_type) { 1521590Srgrimes case KTR_SYSCALL: 1531590Srgrimes ktrsyscall((struct ktr_syscall *)m); 1541590Srgrimes break; 1551590Srgrimes case KTR_SYSRET: 1561590Srgrimes ktrsysret((struct ktr_sysret *)m); 1571590Srgrimes break; 1581590Srgrimes case KTR_NAMEI: 1591590Srgrimes ktrnamei(m, ktrlen); 1601590Srgrimes break; 1611590Srgrimes case KTR_GENIO: 1621590Srgrimes ktrgenio((struct ktr_genio *)m, ktrlen); 1631590Srgrimes break; 1641590Srgrimes case KTR_PSIG: 1651590Srgrimes ktrpsig((struct ktr_psig *)m); 1661590Srgrimes break; 1671590Srgrimes case KTR_CSW: 1681590Srgrimes ktrcsw((struct ktr_csw *)m); 1691590Srgrimes break; 1701590Srgrimes } 1711590Srgrimes if (tail) 1721590Srgrimes (void)fflush(stdout); 1731590Srgrimes } 1741590Srgrimes} 1751590Srgrimes 1761590Srgrimesfread_tail(buf, size, num) 1771590Srgrimes char *buf; 1781590Srgrimes int num, size; 1791590Srgrimes{ 1801590Srgrimes int i; 1811590Srgrimes 1821590Srgrimes while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 1831590Srgrimes (void)sleep(1); 1841590Srgrimes clearerr(stdin); 1851590Srgrimes } 1861590Srgrimes return (i); 1871590Srgrimes} 1881590Srgrimes 1891590Srgrimesdumpheader(kth) 1901590Srgrimes struct ktr_header *kth; 1911590Srgrimes{ 1921590Srgrimes static char unknown[64]; 1931590Srgrimes static struct timeval prevtime, temp; 1941590Srgrimes char *type; 1951590Srgrimes 1961590Srgrimes switch (kth->ktr_type) { 1971590Srgrimes case KTR_SYSCALL: 1981590Srgrimes type = "CALL"; 1991590Srgrimes break; 2001590Srgrimes case KTR_SYSRET: 2011590Srgrimes type = "RET "; 2021590Srgrimes break; 2031590Srgrimes case KTR_NAMEI: 2041590Srgrimes type = "NAMI"; 2051590Srgrimes break; 2061590Srgrimes case KTR_GENIO: 2071590Srgrimes type = "GIO "; 2081590Srgrimes break; 2091590Srgrimes case KTR_PSIG: 2101590Srgrimes type = "PSIG"; 2111590Srgrimes break; 2121590Srgrimes case KTR_CSW: 2131590Srgrimes type = "CSW"; 2141590Srgrimes break; 2151590Srgrimes default: 2161590Srgrimes (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 2171590Srgrimes type = unknown; 2181590Srgrimes } 2191590Srgrimes 2201590Srgrimes (void)printf("%6d %-8s ", kth->ktr_pid, kth->ktr_comm); 2211590Srgrimes if (timestamp) { 2221590Srgrimes if (timestamp == 2) { 2231590Srgrimes temp = kth->ktr_time; 2241590Srgrimes timevalsub(&kth->ktr_time, &prevtime); 2251590Srgrimes prevtime = temp; 2261590Srgrimes } 2271590Srgrimes (void)printf("%ld.%06ld ", 2281590Srgrimes kth->ktr_time.tv_sec, kth->ktr_time.tv_usec); 2291590Srgrimes } 2301590Srgrimes (void)printf("%s ", type); 2311590Srgrimes} 2321590Srgrimes 2331590Srgrimes#include <sys/syscall.h> 2341590Srgrimes#define KTRACE 2354721Sphk#include <sys/kern/syscalls.c> 2361590Srgrimes#undef KTRACE 2371590Srgrimesint nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]); 2381590Srgrimes 2391590Srgrimesstatic char *ptrace_ops[] = { 2401590Srgrimes "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U", 2411590Srgrimes "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE", 2421590Srgrimes "PT_KILL", "PT_STEP", 2431590Srgrimes}; 2441590Srgrimes 2451590Srgrimesktrsyscall(ktr) 2461590Srgrimes register struct ktr_syscall *ktr; 2471590Srgrimes{ 2481590Srgrimes register narg = ktr->ktr_narg; 2491590Srgrimes register int *ip; 2501590Srgrimes char *ioctlname(); 2511590Srgrimes 2521590Srgrimes if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0) 2531590Srgrimes (void)printf("[%d]", ktr->ktr_code); 2541590Srgrimes else 2551590Srgrimes (void)printf("%s", syscallnames[ktr->ktr_code]); 2561590Srgrimes ip = (int *)((char *)ktr + sizeof(struct ktr_syscall)); 2571590Srgrimes if (narg) { 2581590Srgrimes char c = '('; 2591590Srgrimes if (fancy) { 2601590Srgrimes if (ktr->ktr_code == SYS_ioctl) { 2611590Srgrimes char *cp; 2621590Srgrimes if (decimal) 2631590Srgrimes (void)printf("(%d", *ip); 2641590Srgrimes else 2651590Srgrimes (void)printf("(%#x", *ip); 2661590Srgrimes ip++; 2671590Srgrimes narg--; 2681590Srgrimes if ((cp = ioctlname(*ip)) != NULL) 2691590Srgrimes (void)printf(",%s", cp); 2701590Srgrimes else { 2711590Srgrimes if (decimal) 2721590Srgrimes (void)printf(",%d", *ip); 2731590Srgrimes else 2741590Srgrimes (void)printf(",%#x ", *ip); 2751590Srgrimes } 2761590Srgrimes c = ','; 2771590Srgrimes ip++; 2781590Srgrimes narg--; 2791590Srgrimes } else if (ktr->ktr_code == SYS_ptrace) { 2801590Srgrimes if (*ip <= PT_STEP && *ip >= 0) 2811590Srgrimes (void)printf("(%s", ptrace_ops[*ip]); 2821590Srgrimes else 2831590Srgrimes (void)printf("(%d", *ip); 2841590Srgrimes c = ','; 2851590Srgrimes ip++; 2861590Srgrimes narg--; 2871590Srgrimes } 2881590Srgrimes } 2891590Srgrimes while (narg) { 2901590Srgrimes if (decimal) 2911590Srgrimes (void)printf("%c%d", c, *ip); 2921590Srgrimes else 2931590Srgrimes (void)printf("%c%#x", c, *ip); 2941590Srgrimes c = ','; 2951590Srgrimes ip++; 2961590Srgrimes narg--; 2971590Srgrimes } 2981590Srgrimes (void)putchar(')'); 2991590Srgrimes } 3001590Srgrimes (void)putchar('\n'); 3011590Srgrimes} 3021590Srgrimes 3031590Srgrimesktrsysret(ktr) 3041590Srgrimes struct ktr_sysret *ktr; 3051590Srgrimes{ 3061590Srgrimes register int ret = ktr->ktr_retval; 3071590Srgrimes register int error = ktr->ktr_error; 3081590Srgrimes register int code = ktr->ktr_code; 3091590Srgrimes 3101590Srgrimes if (code >= nsyscalls || code < 0) 3111590Srgrimes (void)printf("[%d] ", code); 3121590Srgrimes else 3131590Srgrimes (void)printf("%s ", syscallnames[code]); 3141590Srgrimes 3151590Srgrimes if (error == 0) { 3161590Srgrimes if (fancy) { 3171590Srgrimes (void)printf("%d", ret); 3181590Srgrimes if (ret < 0 || ret > 9) 3191590Srgrimes (void)printf("/%#x", ret); 3201590Srgrimes } else { 3211590Srgrimes if (decimal) 3221590Srgrimes (void)printf("%d", ret); 3231590Srgrimes else 3241590Srgrimes (void)printf("%#x", ret); 3251590Srgrimes } 3261590Srgrimes } else if (error == ERESTART) 3271590Srgrimes (void)printf("RESTART"); 3281590Srgrimes else if (error == EJUSTRETURN) 3291590Srgrimes (void)printf("JUSTRETURN"); 3301590Srgrimes else { 3311590Srgrimes (void)printf("-1 errno %d", ktr->ktr_error); 3321590Srgrimes if (fancy) 3331590Srgrimes (void)printf(" %s", strerror(ktr->ktr_error)); 3341590Srgrimes } 3351590Srgrimes (void)putchar('\n'); 3361590Srgrimes} 3371590Srgrimes 3381590Srgrimesktrnamei(cp, len) 3391590Srgrimes char *cp; 3401590Srgrimes{ 3411590Srgrimes (void)printf("\"%.*s\"\n", len, cp); 3421590Srgrimes} 3431590Srgrimes 3441590Srgrimesktrgenio(ktr, len) 3451590Srgrimes struct ktr_genio *ktr; 3461590Srgrimes{ 3471590Srgrimes register int datalen = len - sizeof (struct ktr_genio); 3481590Srgrimes register char *dp = (char *)ktr + sizeof (struct ktr_genio); 3491590Srgrimes register char *cp; 3501590Srgrimes register int col = 0; 3511590Srgrimes register width; 3521590Srgrimes char visbuf[5]; 3531590Srgrimes static screenwidth = 0; 3541590Srgrimes 3551590Srgrimes if (screenwidth == 0) { 3561590Srgrimes struct winsize ws; 3571590Srgrimes 3581590Srgrimes if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 3591590Srgrimes ws.ws_col > 8) 3601590Srgrimes screenwidth = ws.ws_col; 3611590Srgrimes else 3621590Srgrimes screenwidth = 80; 3631590Srgrimes } 3641590Srgrimes printf("fd %d %s %d bytes\n", ktr->ktr_fd, 3651590Srgrimes ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen); 3661590Srgrimes if (maxdata && datalen > maxdata) 3671590Srgrimes datalen = maxdata; 3681590Srgrimes (void)printf(" \""); 3691590Srgrimes col = 8; 3701590Srgrimes for (;datalen > 0; datalen--, dp++) { 3711590Srgrimes (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1)); 3721590Srgrimes cp = visbuf; 3731590Srgrimes /* 3741590Srgrimes * Keep track of printables and 3751590Srgrimes * space chars (like fold(1)). 3761590Srgrimes */ 3771590Srgrimes if (col == 0) { 3781590Srgrimes (void)putchar('\t'); 3791590Srgrimes col = 8; 3801590Srgrimes } 3811590Srgrimes switch(*cp) { 3821590Srgrimes case '\n': 3831590Srgrimes col = 0; 3841590Srgrimes (void)putchar('\n'); 3851590Srgrimes continue; 3861590Srgrimes case '\t': 3871590Srgrimes width = 8 - (col&07); 3881590Srgrimes break; 3891590Srgrimes default: 3901590Srgrimes width = strlen(cp); 3911590Srgrimes } 3921590Srgrimes if (col + width > (screenwidth-2)) { 3931590Srgrimes (void)printf("\\\n\t"); 3941590Srgrimes col = 8; 3951590Srgrimes } 3961590Srgrimes col += width; 3971590Srgrimes do { 3981590Srgrimes (void)putchar(*cp++); 3991590Srgrimes } while (*cp); 4001590Srgrimes } 4011590Srgrimes if (col == 0) 4021590Srgrimes (void)printf(" "); 4031590Srgrimes (void)printf("\"\n"); 4041590Srgrimes} 4051590Srgrimes 4061590Srgrimeschar *signames[] = { 4071590Srgrimes "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */ 4081590Srgrimes "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */ 4091590Srgrimes "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */ 4101590Srgrimes "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */ 4111590Srgrimes "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */ 4121590Srgrimes "USR2", NULL, /* 31 - 32 */ 4131590Srgrimes}; 4141590Srgrimes 4151590Srgrimesktrpsig(psig) 4161590Srgrimes struct ktr_psig *psig; 4171590Srgrimes{ 4181590Srgrimes (void)printf("SIG%s ", signames[psig->signo]); 4191590Srgrimes if (psig->action == SIG_DFL) 4201590Srgrimes (void)printf("SIG_DFL\n"); 4211590Srgrimes else 4221590Srgrimes (void)printf("caught handler=0x%x mask=0x%x code=0x%x\n", 4231590Srgrimes (u_int)psig->action, psig->mask, psig->code); 4241590Srgrimes} 4251590Srgrimes 4261590Srgrimesktrcsw(cs) 4271590Srgrimes struct ktr_csw *cs; 4281590Srgrimes{ 4291590Srgrimes (void)printf("%s %s\n", cs->out ? "stop" : "resume", 4301590Srgrimes cs->user ? "user" : "kernel"); 4311590Srgrimes} 4321590Srgrimes 4331590Srgrimesusage() 4341590Srgrimes{ 4351590Srgrimes (void)fprintf(stderr, 4361590Srgrimes "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n"); 4371590Srgrimes exit(1); 4381590Srgrimes} 439