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

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