[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.34

1.34    ! tedu        1: /*     $OpenBSD: kdump.c,v 1.33 2006/05/11 07:13:17 tedu 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.
1.21      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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: #ifndef lint
1.32      tedu       33: static const char copyright[] =
1.1       deraadt    34: "@(#) 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
1.34    ! tedu       42: static const char rcsid[] = "$OpenBSD: kdump.c,v 1.33 2006/05/11 07:13:17 tedu Exp $";
1.1       deraadt    43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/time.h>
                     47: #include <sys/uio.h>
                     48: #include <sys/ktrace.h>
                     49: #include <sys/ioctl.h>
                     50: #include <sys/ptrace.h>
1.28      deraadt    51: #include <sys/sysctl.h>
1.1       deraadt    52: #define _KERNEL
                     53: #include <sys/errno.h>
                     54: #undef _KERNEL
                     55:
1.33      tedu       56: #include <ctype.h>
1.1       deraadt    57: #include <err.h>
                     58: #include <signal.h>
                     59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
                     62: #include <unistd.h>
                     63: #include <vis.h>
                     64:
                     65: #include "ktrace.h"
1.22      deraadt    66: #include "kdump.h"
1.12      espie      67: #include "extern.h"
1.1       deraadt    68:
1.31      tedu       69: int timestamp, decimal, iohex, fancy = 1, tail, maxdata;
1.1       deraadt    70: char *tracefile = DEF_TRACEFILE;
                     71: struct ktr_header ktr_header;
1.17      deraadt    72: pid_t pid = -1;
1.1       deraadt    73:
                     74: #define eqs(s1, s2)    (strcmp((s1), (s2)) == 0)
                     75:
                     76: #include <sys/syscall.h>
                     77:
1.25      mickey     78: #include <compat/bsdos/bsdos_syscall.h>
                     79: #include <compat/freebsd/freebsd_syscall.h>
                     80: #include <compat/netbsd/netbsd_syscall.h>
1.26      deraadt    81: #if defined(__hppa__) || defined(__m68k__)
1.25      mickey     82: #include <compat/hpux/hpux_syscall.h>
1.26      deraadt    83: #endif
1.25      mickey     84: #include <compat/ibcs2/ibcs2_syscall.h>
                     85: #include <compat/linux/linux_syscall.h>
                     86: #include <compat/osf1/osf1_syscall.h>
                     87: #include <compat/sunos/sunos_syscall.h>
                     88: #include <compat/svr4/svr4_syscall.h>
                     89: #include <compat/ultrix/ultrix_syscall.h>
1.1       deraadt    90:
                     91: #define KTRACE
1.19      mickey     92: #define PTRACE
1.7       deraadt    93: #define NFSCLIENT
                     94: #define NFSSERVER
                     95: #define SYSVSEM
                     96: #define SYSVMSG
                     97: #define SYSVSHM
                     98: #define LFS
1.30      miod       99: #define RTHREADS
1.25      mickey    100: #include <kern/syscalls.c>
1.1       deraadt   101:
1.25      mickey    102: #include <compat/bsdos/bsdos_syscalls.c>
                    103: #include <compat/freebsd/freebsd_syscalls.c>
                    104: #include <compat/netbsd/netbsd_syscalls.c>
1.26      deraadt   105: #if defined(__hppa__) || defined(__m68k__)
1.25      mickey    106: #include <compat/hpux/hpux_syscalls.c>
1.26      deraadt   107: #endif
1.25      mickey    108: #include <compat/ibcs2/ibcs2_syscalls.c>
                    109: #include <compat/linux/linux_syscalls.c>
                    110: #include <compat/osf1/osf1_syscalls.c>
                    111: #include <compat/sunos/sunos_syscalls.c>
                    112: #include <compat/svr4/svr4_syscalls.c>
                    113: #include <compat/ultrix/ultrix_syscalls.c>
1.1       deraadt   114: #undef KTRACE
1.19      mickey    115: #undef PTRACE
1.7       deraadt   116: #undef NFSCLIENT
                    117: #undef NFSSERVER
                    118: #undef SYSVSEM
                    119: #undef SYSVMSG
                    120: #undef SYSVSHM
                    121: #undef LFS
1.30      miod      122: #undef RTHREADS
1.1       deraadt   123:
                    124: struct emulation {
                    125:        char *name;             /* Emulation name */
                    126:        char **sysnames;        /* Array of system call names */
                    127:        int  nsysnames;         /* Number of */
                    128: };
                    129:
                    130: static struct emulation emulations[] = {
1.9       deraadt   131:        { "native",     syscallnames,           SYS_MAXSYSCALL },
1.26      deraadt   132: #if defined(__hppa__) || defined(__m68k__)
1.9       deraadt   133:        { "hpux",       hpux_syscallnames,      HPUX_SYS_MAXSYSCALL },
1.26      deraadt   134: #endif
1.9       deraadt   135:        { "ibcs2",      ibcs2_syscallnames,     IBCS2_SYS_MAXSYSCALL },
                    136:        { "linux",      linux_syscallnames,     LINUX_SYS_MAXSYSCALL },
                    137:        { "osf1",       osf1_syscallnames,      OSF1_SYS_MAXSYSCALL },
                    138:        { "sunos",      sunos_syscallnames,     SUNOS_SYS_MAXSYSCALL },
                    139:        { "svr4",       svr4_syscallnames,      SVR4_SYS_MAXSYSCALL },
                    140:        { "ultrix",     ultrix_syscallnames,    ULTRIX_SYS_MAXSYSCALL },
                    141:        { "bsdos",      bsdos_syscallnames,     BSDOS_SYS_MAXSYSCALL },
                    142:        { "freebsd",    freebsd_syscallnames,   FREEBSD_SYS_MAXSYSCALL },
1.10      kstailey  143:        { "netbsd",     netbsd_syscallnames,    NETBSD_SYS_MAXSYSCALL },
1.9       deraadt   144:        { NULL,         NULL,                   NULL }
1.1       deraadt   145: };
                    146:
                    147: struct emulation *current;
                    148:
                    149:
                    150: static char *ptrace_ops[] = {
                    151:        "PT_TRACE_ME",  "PT_READ_I",    "PT_READ_D",    "PT_READ_U",
                    152:        "PT_WRITE_I",   "PT_WRITE_D",   "PT_WRITE_U",   "PT_CONTINUE",
1.15      art       153:        "PT_KILL",      "PT_ATTACH",    "PT_DETACH",    "PT_IO",
1.1       deraadt   154: };
                    155:
1.13      millert   156: static int fread_tail(void *, int, int);
                    157: static void dumpheader(struct ktr_header *);
                    158: static void ktrcsw(struct ktr_csw *);
                    159: static void ktremul(char *, int);
                    160: static void ktrgenio(struct ktr_genio *, int);
                    161: static void ktrnamei(const char *, int);
                    162: static void ktrpsig(struct ktr_psig *);
                    163: static void ktrsyscall(struct ktr_syscall *);
                    164: static void ktrsysret(struct ktr_sysret *);
                    165: static void setemul(const char *);
                    166: static void usage(void);
1.12      espie     167:
1.1       deraadt   168: int
1.17      deraadt   169: main(int argc, char *argv[])
1.1       deraadt   170: {
1.17      deraadt   171:        int ch, ktrlen, size, silent;
                    172:        int trpoints = ALL_POINTS;
1.12      espie     173:        void *m;
1.1       deraadt   174:
1.3       deraadt   175:        current = &emulations[0];       /* native */
1.1       deraadt   176:
1.31      tedu      177:        while ((ch = getopt(argc, argv, "e:f:dlm:nRp:Tt:xX")) != -1)
1.1       deraadt   178:                switch (ch) {
                    179:                case 'e':
                    180:                        setemul(optarg);
                    181:                        break;
                    182:                case 'f':
                    183:                        tracefile = optarg;
                    184:                        break;
                    185:                case 'd':
                    186:                        decimal = 1;
                    187:                        break;
                    188:                case 'l':
                    189:                        tail = 1;
                    190:                        break;
                    191:                case 'm':
                    192:                        maxdata = atoi(optarg);
                    193:                        break;
                    194:                case 'n':
                    195:                        fancy = 0;
                    196:                        break;
1.17      deraadt   197:                case 'p':
                    198:                        pid = atoi(optarg);
                    199:                        break;
1.1       deraadt   200:                case 'R':
                    201:                        timestamp = 2;  /* relative timestamp */
                    202:                        break;
                    203:                case 'T':
                    204:                        timestamp = 1;
                    205:                        break;
                    206:                case 't':
                    207:                        trpoints = getpoints(optarg);
                    208:                        if (trpoints < 0)
                    209:                                errx(1, "unknown trace point in %s", optarg);
                    210:                        break;
1.31      tedu      211:                case 'x':
                    212:                        iohex = 1;
                    213:                        break;
                    214:                case 'X':
                    215:                        iohex = 2;
                    216:                        break;
1.1       deraadt   217:                default:
                    218:                        usage();
                    219:                }
1.5       deraadt   220:        if (argc > optind)
1.1       deraadt   221:                usage();
                    222:
                    223:        m = (void *)malloc(size = 1025);
                    224:        if (m == NULL)
                    225:                errx(1, "%s", strerror(ENOMEM));
                    226:        if (!freopen(tracefile, "r", stdin))
                    227:                err(1, "%s", tracefile);
                    228:        while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
1.17      deraadt   229:                silent = 0;
                    230:                if (pid != -1 && pid != ktr_header.ktr_pid)
                    231:                        silent = 1;
                    232:                if (silent == 0 && trpoints & (1<<ktr_header.ktr_type))
1.1       deraadt   233:                        dumpheader(&ktr_header);
                    234:                if ((ktrlen = ktr_header.ktr_len) < 0)
                    235:                        errx(1, "bogus length 0x%x", ktrlen);
                    236:                if (ktrlen > size) {
1.23      tedu      237:                        void *newm;
                    238:
                    239:                        newm = realloc(m, ktrlen+1);
                    240:                        if (newm == NULL)
1.1       deraadt   241:                                errx(1, "%s", strerror(ENOMEM));
1.23      tedu      242:                        m = newm;
1.1       deraadt   243:                        size = ktrlen;
                    244:                }
                    245:                if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
                    246:                        errx(1, "data too short");
1.17      deraadt   247:                if (silent)
                    248:                        continue;
1.1       deraadt   249:                if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
                    250:                        continue;
                    251:                switch (ktr_header.ktr_type) {
                    252:                case KTR_SYSCALL:
                    253:                        ktrsyscall((struct ktr_syscall *)m);
                    254:                        break;
                    255:                case KTR_SYSRET:
                    256:                        ktrsysret((struct ktr_sysret *)m);
                    257:                        break;
                    258:                case KTR_NAMEI:
                    259:                        ktrnamei(m, ktrlen);
                    260:                        break;
                    261:                case KTR_GENIO:
                    262:                        ktrgenio((struct ktr_genio *)m, ktrlen);
                    263:                        break;
                    264:                case KTR_PSIG:
                    265:                        ktrpsig((struct ktr_psig *)m);
                    266:                        break;
                    267:                case KTR_CSW:
                    268:                        ktrcsw((struct ktr_csw *)m);
                    269:                        break;
                    270:                case KTR_EMUL:
                    271:                        ktremul(m, ktrlen);
                    272:                        break;
                    273:                }
                    274:                if (tail)
                    275:                        (void)fflush(stdout);
                    276:        }
1.12      espie     277:        exit(0);
1.1       deraadt   278: }
                    279:
1.12      espie     280: static int
1.17      deraadt   281: fread_tail(void *buf, int size, int num)
1.1       deraadt   282: {
                    283:        int i;
                    284:
                    285:        while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
                    286:                (void)sleep(1);
                    287:                clearerr(stdin);
                    288:        }
                    289:        return (i);
                    290: }
                    291:
1.12      espie     292: static void
1.17      deraadt   293: dumpheader(struct ktr_header *kth)
1.1       deraadt   294: {
1.17      deraadt   295:        static struct timeval prevtime;
1.1       deraadt   296:        char unknown[64], *type;
                    297:        struct timeval temp;
                    298:
                    299:        switch (kth->ktr_type) {
                    300:        case KTR_SYSCALL:
                    301:                type = "CALL";
                    302:                break;
                    303:        case KTR_SYSRET:
                    304:                type = "RET ";
                    305:                break;
                    306:        case KTR_NAMEI:
                    307:                type = "NAMI";
                    308:                break;
                    309:        case KTR_GENIO:
                    310:                type = "GIO ";
                    311:                break;
                    312:        case KTR_PSIG:
                    313:                type = "PSIG";
                    314:                break;
                    315:        case KTR_CSW:
                    316:                type = "CSW";
                    317:                break;
                    318:        case KTR_EMUL:
                    319:                type = "EMUL";
                    320:                break;
                    321:        default:
1.17      deraadt   322:                (void)snprintf(unknown, sizeof unknown, "UNKNOWN(%d)",
                    323:                    kth->ktr_type);
1.1       deraadt   324:                type = unknown;
                    325:        }
                    326:
1.16      mpech     327:        (void)printf("%6ld %-8.*s ", (long)kth->ktr_pid, MAXCOMLEN,
                    328:            kth->ktr_comm);
1.1       deraadt   329:        if (timestamp) {
                    330:                if (timestamp == 2) {
                    331:                        timersub(&kth->ktr_time, &prevtime, &temp);
                    332:                        prevtime = kth->ktr_time;
                    333:                } else
                    334:                        temp = kth->ktr_time;
                    335:                (void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
                    336:        }
                    337:        (void)printf("%s  ", type);
                    338: }
                    339:
1.12      espie     340: static void
1.17      deraadt   341: ioctldecode(u_long cmd)
1.2       deraadt   342: {
                    343:        char dirbuf[4], *dir = dirbuf;
                    344:
1.6       deraadt   345:        if (cmd & IOC_IN)
                    346:                *dir++ = 'W';
1.2       deraadt   347:        if (cmd & IOC_OUT)
                    348:                *dir++ = 'R';
                    349:        *dir = '\0';
                    350:
1.33      tedu      351:        printf(decimal ? ",_IO%s('%c',%lu" : ",_IO%s('%c',%#lx",
                    352:            dirbuf, (int)((cmd >> 8) & 0xff), cmd & 0xff);
1.2       deraadt   353:        if ((cmd & IOC_VOID) == 0)
1.34    ! tedu      354:                printf(decimal ? ",%lu)" : ",%#lx)", (cmd >> 16) & 0xff);
1.2       deraadt   355:        else
                    356:                printf(")");
                    357: }
1.1       deraadt   358:
1.12      espie     359: static void
1.17      deraadt   360: ktrsyscall(struct ktr_syscall *ktr)
1.1       deraadt   361: {
1.12      espie     362:        int argsize = ktr->ktr_argsize;
                    363:        register_t *ap;
1.1       deraadt   364:
                    365:        if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
                    366:                (void)printf("[%d]", ktr->ktr_code);
                    367:        else
                    368:                (void)printf("%s", current->sysnames[ktr->ktr_code]);
                    369:        ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
1.27      mickey    370:        (void)putchar('(');
1.1       deraadt   371:        if (argsize) {
1.27      mickey    372:                char c = '\0';
1.1       deraadt   373:                if (fancy) {
                    374:                        if (ktr->ktr_code == SYS_ioctl) {
1.12      espie     375:                                const char *cp;
1.17      deraadt   376:
1.1       deraadt   377:                                if (decimal)
1.27      mickey    378:                                        (void)printf("%ld", (long)*ap);
1.1       deraadt   379:                                else
1.27      mickey    380:                                        (void)printf("%#lx", (long)*ap);
1.1       deraadt   381:                                ap++;
                    382:                                argsize -= sizeof(register_t);
                    383:                                if ((cp = ioctlname(*ap)) != NULL)
                    384:                                        (void)printf(",%s", cp);
1.2       deraadt   385:                                else
                    386:                                        ioctldecode(*ap);
1.1       deraadt   387:                                c = ',';
                    388:                                ap++;
                    389:                                argsize -= sizeof(register_t);
1.27      mickey    390:                        } else if (ktr->ktr_code == SYS___sysctl) {
                    391:                                int *np, n;
                    392:
                    393:                                n = ap[1];
1.28      deraadt   394:                                if (n > CTL_MAXNAME)
                    395:                                        n = CTL_MAXNAME;
1.27      mickey    396:                                np = (int *)(ap + 6);
                    397:                                for (; n--; np++) {
                    398:                                        if (c)
                    399:                                                putchar(c);
                    400:                                        printf("%d", *np);
                    401:                                        c = '.';
                    402:                                }
                    403:
                    404:                                c = ',';
                    405:                                ap += 2;
                    406:                                argsize -= 2 * sizeof(register_t);
1.1       deraadt   407:                        } else if (ktr->ktr_code == SYS_ptrace) {
1.24      miod      408:                                if (*ap >= 0 && *ap <
1.1       deraadt   409:                                    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
1.27      mickey    410:                                        (void)printf("%s", ptrace_ops[*ap]);
1.24      miod      411:                                else switch(*ap) {
                    412: #ifdef PT_GETFPREGS
                    413:                                case PT_GETFPREGS:
1.27      mickey    414:                                        (void)printf("PT_GETFPREGS");
1.24      miod      415:                                        break;
                    416: #endif
                    417:                                case PT_GETREGS:
1.27      mickey    418:                                        (void)printf("PT_GETREGS");
1.24      miod      419:                                        break;
                    420: #ifdef PT_SETFPREGS
                    421:                                case PT_SETFPREGS:
1.27      mickey    422:                                        (void)printf("PT_SETFPREGS");
1.24      miod      423:                                        break;
                    424: #endif
                    425:                                case PT_SETREGS:
1.27      mickey    426:                                        (void)printf("PT_SETREGS");
1.24      miod      427:                                        break;
                    428: #ifdef PT_STEP
                    429:                                case PT_STEP:
1.27      mickey    430:                                        (void)printf("PT_STEP");
1.24      miod      431:                                        break;
                    432: #endif
                    433: #ifdef PT_WCOOKIE
                    434:                                case PT_WCOOKIE:
1.27      mickey    435:                                        (void)printf("PT_WCOOKIE");
1.24      miod      436:                                        break;
                    437: #endif
                    438:                                default:
1.27      mickey    439:                                        (void)printf("%ld", (long)*ap);
1.24      miod      440:                                        break;
                    441:                                }
1.1       deraadt   442:                                c = ',';
                    443:                                ap++;
                    444:                                argsize -= sizeof(register_t);
                    445:                        }
                    446:                }
                    447:                while (argsize) {
1.27      mickey    448:                        if (c)
                    449:                                putchar(c);
1.1       deraadt   450:                        if (decimal)
1.27      mickey    451:                                (void)printf("%ld", (long)*ap);
1.1       deraadt   452:                        else
1.27      mickey    453:                                (void)printf("%#lx", (long)*ap);
1.1       deraadt   454:                        c = ',';
                    455:                        ap++;
                    456:                        argsize -= sizeof(register_t);
                    457:                }
                    458:        }
1.27      mickey    459:        (void)printf(")\n");
1.1       deraadt   460: }
                    461:
1.12      espie     462: static void
1.17      deraadt   463: ktrsysret(struct ktr_sysret *ktr)
1.1       deraadt   464: {
1.12      espie     465:        int ret = ktr->ktr_retval;
                    466:        int error = ktr->ktr_error;
                    467:        int code = ktr->ktr_code;
1.1       deraadt   468:
                    469:        if (code >= current->nsysnames || code < 0)
                    470:                (void)printf("[%d] ", code);
                    471:        else
                    472:                (void)printf("%s ", current->sysnames[code]);
                    473:
                    474:        if (error == 0) {
                    475:                if (fancy) {
                    476:                        (void)printf("%d", ret);
                    477:                        if (ret < 0 || ret > 9)
                    478:                                (void)printf("/%#x", ret);
                    479:                } else {
                    480:                        if (decimal)
                    481:                                (void)printf("%d", ret);
                    482:                        else
                    483:                                (void)printf("%#x", ret);
                    484:                }
                    485:        } else if (error == ERESTART)
                    486:                (void)printf("RESTART");
                    487:        else if (error == EJUSTRETURN)
                    488:                (void)printf("JUSTRETURN");
                    489:        else {
                    490:                (void)printf("-1 errno %d", ktr->ktr_error);
                    491:                if (fancy)
                    492:                        (void)printf(" %s", strerror(ktr->ktr_error));
                    493:        }
                    494:        (void)putchar('\n');
                    495: }
                    496:
1.12      espie     497: static void
1.17      deraadt   498: ktrnamei(const char *cp, int len)
1.1       deraadt   499: {
                    500:        (void)printf("\"%.*s\"\n", len, cp);
                    501: }
                    502:
1.12      espie     503: static void
1.17      deraadt   504: ktremul(char *cp, int len)
1.1       deraadt   505: {
                    506:        char name[1024];
                    507:
                    508:        if (len >= sizeof(name))
                    509:                errx(1, "Emulation name too long");
                    510:
                    511:        strncpy(name, cp, len);
                    512:        name[len] = '\0';
                    513:        (void)printf("\"%s\"\n", name);
                    514:
                    515:        setemul(name);
                    516: }
                    517:
1.12      espie     518: static void
1.17      deraadt   519: ktrgenio(struct ktr_genio *ktr, int len)
1.1       deraadt   520: {
1.32      tedu      521:        unsigned char *dp = (unsigned char *)ktr + sizeof (struct ktr_genio);
1.31      tedu      522:        int i, j, datalen = len - sizeof (struct ktr_genio);
1.12      espie     523:        static int screenwidth = 0;
1.31      tedu      524:        int col = 0, width, bpl;
1.32      tedu      525:        unsigned char visbuf[5], *cp, c;
1.1       deraadt   526:
                    527:        if (screenwidth == 0) {
                    528:                struct winsize ws;
                    529:
                    530:                if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
                    531:                    ws.ws_col > 8)
                    532:                        screenwidth = ws.ws_col;
                    533:                else
                    534:                        screenwidth = 80;
                    535:        }
                    536:        printf("fd %d %s %d bytes\n", ktr->ktr_fd,
                    537:                ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
                    538:        if (maxdata && datalen > maxdata)
                    539:                datalen = maxdata;
1.31      tedu      540:        if (iohex && !datalen)
                    541:                return;
                    542:        if (iohex == 1) {
                    543:                putchar('\t');
                    544:                col = 8;
                    545:                for (i = 0; i < datalen; i++) {
                    546:                        printf("%02x", dp[i] & 0xff);
                    547:                        col += 3;
                    548:                        if (i < datalen - 1) {
                    549:                                if (col + 3 > screenwidth) {
                    550:                                        printf("\n\t");
                    551:                                        col = 8;
                    552:                                } else
                    553:                                        putchar(' ');
                    554:                        }
                    555:                }
                    556:                putchar('\n');
                    557:                return;
                    558:        }
                    559:        if (iohex == 2) {
                    560:                bpl = (screenwidth - 13)/4;
                    561:                if (bpl <= 0)
                    562:                        bpl = 1;
                    563:                for (i = 0; i < datalen; i += bpl) {
                    564:                        printf("   %04x:  ", i);
                    565:                        for (j = 0; j < bpl; j++) {
                    566:                                if (i+j >= datalen)
                    567:                                        printf("   ");
                    568:                                else
                    569:                                        printf("%02x ", dp[i+j] & 0xff);
                    570:                        }
                    571:                        putchar(' ');
                    572:                        for (j = 0; j < bpl; j++) {
                    573:                                if (i+j >= datalen)
                    574:                                        break;
                    575:                                c = dp[i+j];
                    576:                                if (!isprint(c))
                    577:                                        c = '.';
                    578:                                putchar(c);
                    579:                        }
                    580:                        putchar('\n');
                    581:                }
                    582:                return;
                    583:        }
1.1       deraadt   584:        (void)printf("       \"");
                    585:        col = 8;
                    586:        for (; datalen > 0; datalen--, dp++) {
1.31      tedu      587:                (void)vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1.1       deraadt   588:                cp = visbuf;
1.17      deraadt   589:
1.1       deraadt   590:                /*
                    591:                 * Keep track of printables and
                    592:                 * space chars (like fold(1)).
                    593:                 */
                    594:                if (col == 0) {
                    595:                        (void)putchar('\t');
                    596:                        col = 8;
                    597:                }
1.17      deraadt   598:                switch (*cp) {
1.1       deraadt   599:                case '\n':
                    600:                        col = 0;
                    601:                        (void)putchar('\n');
                    602:                        continue;
                    603:                case '\t':
                    604:                        width = 8 - (col&07);
                    605:                        break;
                    606:                default:
                    607:                        width = strlen(cp);
                    608:                }
                    609:                if (col + width > (screenwidth-2)) {
                    610:                        (void)printf("\\\n\t");
                    611:                        col = 8;
                    612:                }
                    613:                col += width;
                    614:                do {
                    615:                        (void)putchar(*cp++);
                    616:                } while (*cp);
                    617:        }
                    618:        if (col == 0)
                    619:                (void)printf("       ");
                    620:        (void)printf("\"\n");
                    621: }
                    622:
1.12      espie     623: static void
1.17      deraadt   624: ktrpsig(struct ktr_psig *psig)
1.1       deraadt   625: {
                    626:        (void)printf("SIG%s ", sys_signame[psig->signo]);
                    627:        if (psig->action == SIG_DFL)
1.14      deraadt   628:                (void)printf("SIG_DFL code %d", psig->code);
1.1       deraadt   629:        else
1.14      deraadt   630:                (void)printf("caught handler=0x%lx mask=0x%x",
                    631:                    (u_long)psig->action, psig->mask);
                    632:        switch (psig->signo) {
                    633:        case SIGSEGV:
                    634:        case SIGILL:
                    635:        case SIGBUS:
                    636:        case SIGFPE:
                    637:                printf(" addr=%p trapno=%d", psig->si.si_addr,
                    638:                    psig->si.si_trapno);
                    639:                break;
                    640:        default:
                    641:                break;
                    642:        }
                    643:        printf("\n");
1.1       deraadt   644: }
                    645:
1.12      espie     646: static void
1.17      deraadt   647: ktrcsw(struct ktr_csw *cs)
1.1       deraadt   648: {
                    649:        (void)printf("%s %s\n", cs->out ? "stop" : "resume",
                    650:            cs->user ? "user" : "kernel");
                    651: }
                    652:
1.12      espie     653: static void
1.17      deraadt   654: usage(void)
1.1       deraadt   655: {
                    656:
1.19      mickey    657:        extern char *__progname;
                    658:        fprintf(stderr, "usage: %s "
1.31      tedu      659:            "[-dnlRTxX] [-e emulation] [-p pid] [-f trfile] [-m maxdata] "
1.20      deraadt   660:            "[-t [ceinsw]]\n", __progname);
1.1       deraadt   661:        exit(1);
                    662: }
                    663:
1.12      espie     664: static void
1.17      deraadt   665: setemul(const char *name)
1.1       deraadt   666: {
                    667:        int i;
1.17      deraadt   668:
1.1       deraadt   669:        for (i = 0; emulations[i].name != NULL; i++)
                    670:                if (strcmp(emulations[i].name, name) == 0) {
                    671:                        current = &emulations[i];
                    672:                        return;
                    673:                }
                    674:        warnx("Emulation `%s' unknown", name);
                    675: }