[BACK]Return to kdump.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / kdump

Annotation of src/usr.bin/kdump/kdump.c, Revision 1.9

1.9     ! deraadt     1: /*     $OpenBSD: kdump.c,v 1.8 1997/02/28 07:09:08 millert Exp $       */
1.4       deraadt     2:
1.1       deraadt     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: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) 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: #endif
1.9     ! deraadt    46: static char *rcsid = "$OpenBSD: kdump.c,v 1.8 1997/02/28 07:09:08 millert Exp $";
1.1       deraadt    47: #endif /* not lint */
                     48:
                     49: #include <sys/param.h>
                     50: #include <sys/time.h>
                     51: #include <sys/uio.h>
                     52: #include <sys/ktrace.h>
                     53: #include <sys/ioctl.h>
                     54: #include <sys/ptrace.h>
                     55: #define _KERNEL
                     56: #include <sys/errno.h>
                     57: #undef _KERNEL
                     58:
                     59: #include <err.h>
                     60: #include <signal.h>
                     61: #include <stdio.h>
                     62: #include <stdlib.h>
                     63: #include <string.h>
                     64: #include <unistd.h>
                     65: #include <vis.h>
                     66:
                     67: #include "ktrace.h"
                     68:
                     69: int timestamp, decimal, fancy = 1, tail, maxdata;
                     70: char *tracefile = DEF_TRACEFILE;
                     71: struct ktr_header ktr_header;
                     72:
                     73: #define eqs(s1, s2)    (strcmp((s1), (s2)) == 0)
                     74:
                     75: #include <sys/syscall.h>
                     76:
1.9     ! deraadt    77: #include "../../sys/compat/bsdos/bsdos_syscall.h"
        !            78: #include "../../sys/compat/freebsd/freebsd_syscall.h"
1.1       deraadt    79: #include "../../sys/compat/hpux/hpux_syscall.h"
                     80: #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
                     81: #include "../../sys/compat/linux/linux_syscall.h"
                     82: #include "../../sys/compat/osf1/osf1_syscall.h"
                     83: #include "../../sys/compat/sunos/sunos_syscall.h"
                     84: #include "../../sys/compat/svr4/svr4_syscall.h"
                     85: #include "../../sys/compat/ultrix/ultrix_syscall.h"
                     86:
                     87: #define KTRACE
1.7       deraadt    88: #define NFSCLIENT
                     89: #define NFSSERVER
                     90: #define SYSVSEM
                     91: #define SYSVMSG
                     92: #define SYSVSHM
                     93: #define LFS
                     94: #define NTP
1.1       deraadt    95: #include "../../sys/kern/syscalls.c"
                     96:
1.9     ! deraadt    97: #include "../../sys/compat/bsdos/bsdos_syscalls.c"
        !            98: #include "../../sys/compat/freebsd/freebsd_syscalls.c"
1.1       deraadt    99: #include "../../sys/compat/hpux/hpux_syscalls.c"
                    100: #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
                    101: #include "../../sys/compat/linux/linux_syscalls.c"
                    102: #include "../../sys/compat/osf1/osf1_syscalls.c"
                    103: #include "../../sys/compat/sunos/sunos_syscalls.c"
                    104: #include "../../sys/compat/svr4/svr4_syscalls.c"
                    105: #include "../../sys/compat/ultrix/ultrix_syscalls.c"
                    106: #undef KTRACE
1.7       deraadt   107: #undef NFSCLIENT
                    108: #undef NFSSERVER
                    109: #undef SYSVSEM
                    110: #undef SYSVMSG
                    111: #undef SYSVSHM
                    112: #undef LFS
                    113: #undef NTP
1.1       deraadt   114:
                    115: struct emulation {
                    116:        char *name;             /* Emulation name */
                    117:        char **sysnames;        /* Array of system call names */
                    118:        int  nsysnames;         /* Number of */
                    119: };
                    120:
                    121: static struct emulation emulations[] = {
1.9     ! deraadt   122:        { "native",     syscallnames,           SYS_MAXSYSCALL },
        !           123:        { "hpux",       hpux_syscallnames,      HPUX_SYS_MAXSYSCALL },
        !           124:        { "ibcs2",      ibcs2_syscallnames,     IBCS2_SYS_MAXSYSCALL },
        !           125:        { "linux",      linux_syscallnames,     LINUX_SYS_MAXSYSCALL },
        !           126:        { "osf1",       osf1_syscallnames,      OSF1_SYS_MAXSYSCALL },
        !           127:        { "sunos",      sunos_syscallnames,     SUNOS_SYS_MAXSYSCALL },
        !           128:        { "svr4",       svr4_syscallnames,      SVR4_SYS_MAXSYSCALL },
        !           129:        { "ultrix",     ultrix_syscallnames,    ULTRIX_SYS_MAXSYSCALL },
        !           130:        { "bsdos",      bsdos_syscallnames,     BSDOS_SYS_MAXSYSCALL },
        !           131:        { "freebsd",    freebsd_syscallnames,   FREEBSD_SYS_MAXSYSCALL },
        !           132:        { NULL,         NULL,                   NULL }
1.1       deraadt   133: };
                    134:
                    135: struct emulation *current;
                    136:
                    137:
                    138: static char *ptrace_ops[] = {
                    139:        "PT_TRACE_ME",  "PT_READ_I",    "PT_READ_D",    "PT_READ_U",
                    140:        "PT_WRITE_I",   "PT_WRITE_D",   "PT_WRITE_U",   "PT_CONTINUE",
                    141:        "PT_KILL",      "PT_ATTACH",    "PT_DETACH",
                    142: };
                    143:
                    144: int
                    145: main(argc, argv)
                    146:        int argc;
                    147:        char *argv[];
                    148: {
                    149:        int ch, ktrlen, size;
                    150:        register void *m;
                    151:        int trpoints = ALL_POINTS;
                    152:
1.3       deraadt   153:        current = &emulations[0];       /* native */
1.1       deraadt   154:
                    155:        while ((ch = getopt(argc, argv, "e:f:dlm:nRTt:")) != -1)
                    156:                switch (ch) {
                    157:                case 'e':
                    158:                        setemul(optarg);
                    159:                        break;
                    160:                case 'f':
                    161:                        tracefile = optarg;
                    162:                        break;
                    163:                case 'd':
                    164:                        decimal = 1;
                    165:                        break;
                    166:                case 'l':
                    167:                        tail = 1;
                    168:                        break;
                    169:                case 'm':
                    170:                        maxdata = atoi(optarg);
                    171:                        break;
                    172:                case 'n':
                    173:                        fancy = 0;
                    174:                        break;
                    175:                case 'R':
                    176:                        timestamp = 2;  /* relative timestamp */
                    177:                        break;
                    178:                case 'T':
                    179:                        timestamp = 1;
                    180:                        break;
                    181:                case 't':
                    182:                        trpoints = getpoints(optarg);
                    183:                        if (trpoints < 0)
                    184:                                errx(1, "unknown trace point in %s", optarg);
                    185:                        break;
                    186:                default:
                    187:                        usage();
                    188:                }
1.5       deraadt   189:        if (argc > optind)
1.1       deraadt   190:                usage();
                    191:
                    192:        m = (void *)malloc(size = 1025);
                    193:        if (m == NULL)
                    194:                errx(1, "%s", strerror(ENOMEM));
                    195:        if (!freopen(tracefile, "r", stdin))
                    196:                err(1, "%s", tracefile);
                    197:        while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
                    198:                if (trpoints & (1<<ktr_header.ktr_type))
                    199:                        dumpheader(&ktr_header);
                    200:                if ((ktrlen = ktr_header.ktr_len) < 0)
                    201:                        errx(1, "bogus length 0x%x", ktrlen);
                    202:                if (ktrlen > size) {
                    203:                        m = (void *)realloc(m, ktrlen+1);
                    204:                        if (m == NULL)
                    205:                                errx(1, "%s", strerror(ENOMEM));
                    206:                        size = ktrlen;
                    207:                }
                    208:                if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
                    209:                        errx(1, "data too short");
                    210:                if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
                    211:                        continue;
                    212:                switch (ktr_header.ktr_type) {
                    213:                case KTR_SYSCALL:
                    214:                        ktrsyscall((struct ktr_syscall *)m);
                    215:                        break;
                    216:                case KTR_SYSRET:
                    217:                        ktrsysret((struct ktr_sysret *)m);
                    218:                        break;
                    219:                case KTR_NAMEI:
                    220:                        ktrnamei(m, ktrlen);
                    221:                        break;
                    222:                case KTR_GENIO:
                    223:                        ktrgenio((struct ktr_genio *)m, ktrlen);
                    224:                        break;
                    225:                case KTR_PSIG:
                    226:                        ktrpsig((struct ktr_psig *)m);
                    227:                        break;
                    228:                case KTR_CSW:
                    229:                        ktrcsw((struct ktr_csw *)m);
                    230:                        break;
                    231:                case KTR_EMUL:
                    232:                        ktremul(m, ktrlen);
                    233:                        break;
                    234:                }
                    235:                if (tail)
                    236:                        (void)fflush(stdout);
                    237:        }
                    238: }
                    239:
                    240: fread_tail(buf, size, num)
                    241:        char *buf;
                    242:        int num, size;
                    243: {
                    244:        int i;
                    245:
                    246:        while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
                    247:                (void)sleep(1);
                    248:                clearerr(stdin);
                    249:        }
                    250:        return (i);
                    251: }
                    252:
                    253: dumpheader(kth)
                    254:        struct ktr_header *kth;
                    255: {
                    256:        char unknown[64], *type;
                    257:        static struct timeval prevtime;
                    258:        struct timeval temp;
                    259:
                    260:        switch (kth->ktr_type) {
                    261:        case KTR_SYSCALL:
                    262:                type = "CALL";
                    263:                break;
                    264:        case KTR_SYSRET:
                    265:                type = "RET ";
                    266:                break;
                    267:        case KTR_NAMEI:
                    268:                type = "NAMI";
                    269:                break;
                    270:        case KTR_GENIO:
                    271:                type = "GIO ";
                    272:                break;
                    273:        case KTR_PSIG:
                    274:                type = "PSIG";
                    275:                break;
                    276:        case KTR_CSW:
                    277:                type = "CSW";
                    278:                break;
                    279:        case KTR_EMUL:
                    280:                type = "EMUL";
                    281:                break;
                    282:        default:
                    283:                (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
                    284:                type = unknown;
                    285:        }
                    286:
                    287:        (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
                    288:        if (timestamp) {
                    289:                if (timestamp == 2) {
                    290:                        timersub(&kth->ktr_time, &prevtime, &temp);
                    291:                        prevtime = kth->ktr_time;
                    292:                } else
                    293:                        temp = kth->ktr_time;
                    294:                (void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
                    295:        }
                    296:        (void)printf("%s  ", type);
                    297: }
                    298:
1.2       deraadt   299: void
                    300: ioctldecode(cmd)
                    301:        u_long cmd;
                    302: {
                    303:        char dirbuf[4], *dir = dirbuf;
                    304:
1.6       deraadt   305:        if (cmd & IOC_IN)
                    306:                *dir++ = 'W';
1.2       deraadt   307:        if (cmd & IOC_OUT)
                    308:                *dir++ = 'R';
                    309:        *dir = '\0';
                    310:
                    311:        printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
                    312:            dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
                    313:        if ((cmd & IOC_VOID) == 0)
                    314:                printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
                    315:        else
                    316:                printf(")");
                    317: }
1.1       deraadt   318:
                    319: ktrsyscall(ktr)
                    320:        register struct ktr_syscall *ktr;
                    321: {
                    322:        register argsize = ktr->ktr_argsize;
                    323:        register register_t *ap;
                    324:        char *ioctlname();
                    325:
                    326:        if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
                    327:                (void)printf("[%d]", ktr->ktr_code);
                    328:        else
                    329:                (void)printf("%s", current->sysnames[ktr->ktr_code]);
                    330:        ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
                    331:        if (argsize) {
                    332:                char c = '(';
                    333:                if (fancy) {
                    334:                        if (ktr->ktr_code == SYS_ioctl) {
                    335:                                char *cp;
                    336:                                if (decimal)
                    337:                                        (void)printf("(%ld", (long)*ap);
                    338:                                else
                    339:                                        (void)printf("(%#lx", (long)*ap);
                    340:                                ap++;
                    341:                                argsize -= sizeof(register_t);
                    342:                                if ((cp = ioctlname(*ap)) != NULL)
                    343:                                        (void)printf(",%s", cp);
1.2       deraadt   344:                                else
                    345:                                        ioctldecode(*ap);
1.1       deraadt   346:                                c = ',';
                    347:                                ap++;
                    348:                                argsize -= sizeof(register_t);
                    349:                        } else if (ktr->ktr_code == SYS_ptrace) {
                    350:                                if (*ap >= 0 && *ap <=
                    351:                                    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
                    352:                                        (void)printf("(%s", ptrace_ops[*ap]);
                    353:                                else
                    354:                                        (void)printf("(%ld", (long)*ap);
                    355:                                c = ',';
                    356:                                ap++;
                    357:                                argsize -= sizeof(register_t);
                    358:                        }
                    359:                }
                    360:                while (argsize) {
                    361:                        if (decimal)
                    362:                                (void)printf("%c%ld", c, (long)*ap);
                    363:                        else
                    364:                                (void)printf("%c%#lx", c, (long)*ap);
                    365:                        c = ',';
                    366:                        ap++;
                    367:                        argsize -= sizeof(register_t);
                    368:                }
                    369:                (void)putchar(')');
                    370:        }
                    371:        (void)putchar('\n');
                    372: }
                    373:
                    374: ktrsysret(ktr)
                    375:        struct ktr_sysret *ktr;
                    376: {
                    377:        register int ret = ktr->ktr_retval;
                    378:        register int error = ktr->ktr_error;
                    379:        register int code = ktr->ktr_code;
                    380:
                    381:        if (code >= current->nsysnames || code < 0)
                    382:                (void)printf("[%d] ", code);
                    383:        else
                    384:                (void)printf("%s ", current->sysnames[code]);
                    385:
                    386:        if (error == 0) {
                    387:                if (fancy) {
                    388:                        (void)printf("%d", ret);
                    389:                        if (ret < 0 || ret > 9)
                    390:                                (void)printf("/%#x", ret);
                    391:                } else {
                    392:                        if (decimal)
                    393:                                (void)printf("%d", ret);
                    394:                        else
                    395:                                (void)printf("%#x", ret);
                    396:                }
                    397:        } else if (error == ERESTART)
                    398:                (void)printf("RESTART");
                    399:        else if (error == EJUSTRETURN)
                    400:                (void)printf("JUSTRETURN");
                    401:        else {
                    402:                (void)printf("-1 errno %d", ktr->ktr_error);
                    403:                if (fancy)
                    404:                        (void)printf(" %s", strerror(ktr->ktr_error));
                    405:        }
                    406:        (void)putchar('\n');
                    407: }
                    408:
                    409: ktrnamei(cp, len)
                    410:        char *cp;
                    411: {
                    412:        (void)printf("\"%.*s\"\n", len, cp);
                    413: }
                    414:
                    415: ktremul(cp, len)
                    416:        char *cp;
                    417: {
                    418:        char name[1024];
                    419:
                    420:        if (len >= sizeof(name))
                    421:                errx(1, "Emulation name too long");
                    422:
                    423:        strncpy(name, cp, len);
                    424:        name[len] = '\0';
                    425:        (void)printf("\"%s\"\n", name);
                    426:
                    427:        setemul(name);
                    428: }
                    429:
                    430: ktrgenio(ktr, len)
                    431:        struct ktr_genio *ktr;
                    432: {
                    433:        register int datalen = len - sizeof (struct ktr_genio);
                    434:        register char *dp = (char *)ktr + sizeof (struct ktr_genio);
                    435:        register char *cp;
                    436:        register int col = 0;
                    437:        register width;
                    438:        char visbuf[5];
                    439:        static screenwidth = 0;
                    440:
                    441:        if (screenwidth == 0) {
                    442:                struct winsize ws;
                    443:
                    444:                if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
                    445:                    ws.ws_col > 8)
                    446:                        screenwidth = ws.ws_col;
                    447:                else
                    448:                        screenwidth = 80;
                    449:        }
                    450:        printf("fd %d %s %d bytes\n", ktr->ktr_fd,
                    451:                ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
                    452:        if (maxdata && datalen > maxdata)
                    453:                datalen = maxdata;
                    454:        (void)printf("       \"");
                    455:        col = 8;
                    456:        for (; datalen > 0; datalen--, dp++) {
                    457:                (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
                    458:                cp = visbuf;
                    459:                /*
                    460:                 * Keep track of printables and
                    461:                 * space chars (like fold(1)).
                    462:                 */
                    463:                if (col == 0) {
                    464:                        (void)putchar('\t');
                    465:                        col = 8;
                    466:                }
                    467:                switch(*cp) {
                    468:                case '\n':
                    469:                        col = 0;
                    470:                        (void)putchar('\n');
                    471:                        continue;
                    472:                case '\t':
                    473:                        width = 8 - (col&07);
                    474:                        break;
                    475:                default:
                    476:                        width = strlen(cp);
                    477:                }
                    478:                if (col + width > (screenwidth-2)) {
                    479:                        (void)printf("\\\n\t");
                    480:                        col = 8;
                    481:                }
                    482:                col += width;
                    483:                do {
                    484:                        (void)putchar(*cp++);
                    485:                } while (*cp);
                    486:        }
                    487:        if (col == 0)
                    488:                (void)printf("       ");
                    489:        (void)printf("\"\n");
                    490: }
                    491:
                    492: ktrpsig(psig)
                    493:        struct ktr_psig *psig;
                    494: {
                    495:        (void)printf("SIG%s ", sys_signame[psig->signo]);
                    496:        if (psig->action == SIG_DFL)
                    497:                (void)printf("SIG_DFL\n");
                    498:        else
                    499:                (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
                    500:                    (u_long)psig->action, psig->mask, psig->code);
                    501: }
                    502:
                    503: ktrcsw(cs)
                    504:        struct ktr_csw *cs;
                    505: {
                    506:        (void)printf("%s %s\n", cs->out ? "stop" : "resume",
                    507:            cs->user ? "user" : "kernel");
                    508: }
                    509:
                    510: usage()
                    511: {
                    512:
                    513:        (void)fprintf(stderr,
                    514: "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-t [cnis]]\n");
                    515:        exit(1);
                    516: }
                    517:
                    518: setemul(name)
                    519:        char *name;
                    520: {
                    521:        int i;
                    522:        for (i = 0; emulations[i].name != NULL; i++)
                    523:                if (strcmp(emulations[i].name, name) == 0) {
                    524:                        current = &emulations[i];
                    525:                        return;
                    526:                }
                    527:        warnx("Emulation `%s' unknown", name);
                    528: }