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

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