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

1.90    ! doug        1: /*     $OpenBSD: kdump.c,v 1.89 2014/09/17 19:12:55 guenther 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>
1.62      otto       37: #include <sys/malloc.h>
                     38: #include <sys/namei.h>
1.1       deraadt    39: #include <sys/ptrace.h>
1.62      otto       40: #include <sys/sem.h>
                     41: #include <sys/shm.h>
1.51      otto       42: #include <sys/socket.h>
1.28      deraadt    43: #include <sys/sysctl.h>
1.63      deraadt    44: #include <sys/siginfo.h>
1.62      otto       45: #include <sys/vmmeter.h>
                     46: #include <sys/tty.h>
1.82      guenther   47: #include <sys/wait.h>
1.1       deraadt    48: #define _KERNEL
1.84      guenther   49: #include <errno.h>
1.1       deraadt    50: #undef _KERNEL
1.62      otto       51: #include <ddb/db_var.h>
                     52: #include <machine/cpu.h>
1.1       deraadt    53:
1.33      tedu       54: #include <ctype.h>
1.1       deraadt    55: #include <err.h>
1.61      matthew    56: #include <fcntl.h>
1.64      guenther   57: #include <limits.h>
1.77      guenther   58: #include <poll.h>
1.1       deraadt    59: #include <signal.h>
                     60: #include <stdio.h>
                     61: #include <stdlib.h>
1.55      otto       62: #include <stdint.h>
1.1       deraadt    63: #include <string.h>
                     64: #include <unistd.h>
                     65: #include <vis.h>
                     66:
                     67: #include "ktrace.h"
1.22      deraadt    68: #include "kdump.h"
1.51      otto       69: #include "kdump_subr.h"
1.12      espie      70: #include "extern.h"
1.1       deraadt    71:
1.67      mikeb      72: int timestamp, decimal, iohex, fancy = 1, maxdata = INT_MAX;
                     73: int needtid, resolv, tail;
1.1       deraadt    74: char *tracefile = DEF_TRACEFILE;
                     75: struct ktr_header ktr_header;
1.85      guenther   76: pid_t pid_opt = -1;
1.1       deraadt    77:
                     78: #define eqs(s1, s2)    (strcmp((s1), (s2)) == 0)
                     79:
                     80: #include <sys/syscall.h>
                     81:
1.25      mickey     82: #include <compat/linux/linux_syscall.h>
1.1       deraadt    83:
                     84: #define KTRACE
1.19      mickey     85: #define PTRACE
1.7       deraadt    86: #define NFSCLIENT
                     87: #define NFSSERVER
                     88: #define SYSVSEM
                     89: #define SYSVMSG
                     90: #define SYSVSHM
                     91: #define LFS
1.25      mickey     92: #include <kern/syscalls.c>
1.1       deraadt    93:
1.25      mickey     94: #include <compat/linux/linux_syscalls.c>
1.1       deraadt    95: #undef KTRACE
1.19      mickey     96: #undef PTRACE
1.7       deraadt    97: #undef NFSCLIENT
                     98: #undef NFSSERVER
                     99: #undef SYSVSEM
                    100: #undef SYSVMSG
                    101: #undef SYSVSHM
                    102: #undef LFS
1.1       deraadt   103:
                    104: struct emulation {
                    105:        char *name;             /* Emulation name */
                    106:        char **sysnames;        /* Array of system call names */
                    107:        int  nsysnames;         /* Number of */
                    108: };
                    109:
                    110: static struct emulation emulations[] = {
1.9       deraadt   111:        { "native",     syscallnames,           SYS_MAXSYSCALL },
                    112:        { "linux",      linux_syscallnames,     LINUX_SYS_MAXSYSCALL },
1.49      miod      113:        { NULL,         NULL,                   0 }
1.1       deraadt   114: };
                    115:
1.59      otto      116: static struct emulation *current;
1.60      otto      117: static struct emulation *def_emul;
1.59      otto      118:
                    119: struct pid_emul {
                    120:        struct emulation *e;
                    121:        pid_t p;
                    122: };
                    123:
                    124: static struct pid_emul *pe_table;
                    125: static size_t pe_size;
1.1       deraadt   126:
                    127:
                    128: static char *ptrace_ops[] = {
                    129:        "PT_TRACE_ME",  "PT_READ_I",    "PT_READ_D",    "PT_READ_U",
                    130:        "PT_WRITE_I",   "PT_WRITE_D",   "PT_WRITE_U",   "PT_CONTINUE",
1.15      art       131:        "PT_KILL",      "PT_ATTACH",    "PT_DETACH",    "PT_IO",
1.64      guenther  132:        "PT_SET_EVENT_MASK", "PT_GET_EVENT_MASK", "PT_GET_PROCESS_STATE",
1.71      guenther  133:        "PT_GET_THREAD_FIRST", "PT_GET_THREAD_NEXT",
1.1       deraadt   134: };
                    135:
1.52      otto      136: static int narg;
                    137: static register_t *ap;
                    138: static char sep;
                    139:
1.59      otto      140: static void mappidtoemul(pid_t, struct emulation *);
                    141: static struct emulation * findemul(pid_t);
1.37      tedu      142: static int fread_tail(void *, size_t, size_t);
1.13      millert   143: static void dumpheader(struct ktr_header *);
                    144: static void ktrcsw(struct ktr_csw *);
1.37      tedu      145: static void ktremul(char *, size_t);
                    146: static void ktrgenio(struct ktr_genio *, size_t);
                    147: static void ktrnamei(const char *, size_t);
1.13      millert   148: static void ktrpsig(struct ktr_psig *);
                    149: static void ktrsyscall(struct ktr_syscall *);
1.62      otto      150: static const char *kresolvsysctl(int, int *, int);
1.13      millert   151: static void ktrsysret(struct ktr_sysret *);
1.81      miod      152: static void ktruser(struct ktr_user *, size_t);
1.13      millert   153: static void setemul(const char *);
                    154: static void usage(void);
1.61      matthew   155: static void atfd(int);
1.77      guenther  156: static void polltimeout(int);
1.82      guenther  157: static void pgid(int);
                    158: static void wait4pid(int);
1.83      guenther  159: static void signame(int);
                    160: static void semctlname(int);
                    161: static void shmctlname(int);
                    162: static void semgetname(int);
                    163: static void flagsandmodename(int, int);
                    164: static void clockname(int);
                    165: static void sockoptlevelname(int);
1.88      guenther  166: static void ktraceopname(int);
1.12      espie     167:
1.1       deraadt   168: int
1.17      deraadt   169: main(int argc, char *argv[])
1.1       deraadt   170: {
1.37      tedu      171:        int ch, silent;
                    172:        size_t ktrlen, size;
1.17      deraadt   173:        int trpoints = ALL_POINTS;
1.12      espie     174:        void *m;
1.1       deraadt   175:
1.60      otto      176:        def_emul = current = &emulations[0];    /* native */
1.1       deraadt   177:
1.67      mikeb     178:        while ((ch = getopt(argc, argv, "e:f:dHlm:nrRp:Tt:xX")) != -1)
1.1       deraadt   179:                switch (ch) {
                    180:                case 'e':
                    181:                        setemul(optarg);
1.60      otto      182:                        def_emul = current;
1.1       deraadt   183:                        break;
                    184:                case 'f':
                    185:                        tracefile = optarg;
                    186:                        break;
                    187:                case 'd':
                    188:                        decimal = 1;
                    189:                        break;
1.67      mikeb     190:                case 'H':
                    191:                        needtid = 1;
                    192:                        break;
1.1       deraadt   193:                case 'l':
                    194:                        tail = 1;
                    195:                        break;
                    196:                case 'm':
                    197:                        maxdata = atoi(optarg);
                    198:                        break;
                    199:                case 'n':
                    200:                        fancy = 0;
                    201:                        break;
1.17      deraadt   202:                case 'p':
1.85      guenther  203:                        pid_opt = atoi(optarg);
1.17      deraadt   204:                        break;
1.55      otto      205:                case 'r':
                    206:                        resolv = 1;
                    207:                        break;
1.1       deraadt   208:                case 'R':
                    209:                        timestamp = 2;  /* relative timestamp */
                    210:                        break;
                    211:                case 'T':
                    212:                        timestamp = 1;
                    213:                        break;
                    214:                case 't':
                    215:                        trpoints = getpoints(optarg);
                    216:                        if (trpoints < 0)
                    217:                                errx(1, "unknown trace point in %s", optarg);
                    218:                        break;
1.31      tedu      219:                case 'x':
                    220:                        iohex = 1;
                    221:                        break;
                    222:                case 'X':
                    223:                        iohex = 2;
                    224:                        break;
1.1       deraadt   225:                default:
                    226:                        usage();
                    227:                }
1.5       deraadt   228:        if (argc > optind)
1.1       deraadt   229:                usage();
                    230:
1.37      tedu      231:        m = malloc(size = 1025);
1.1       deraadt   232:        if (m == NULL)
1.37      tedu      233:                err(1, NULL);
1.1       deraadt   234:        if (!freopen(tracefile, "r", stdin))
                    235:                err(1, "%s", tracefile);
1.67      mikeb     236:        if (fread_tail(&ktr_header, sizeof(struct ktr_header), 1) == 0 ||
                    237:            ktr_header.ktr_type != htobe32(KTR_START))
                    238:                errx(1, "%s: not a dump", tracefile);
1.1       deraadt   239:        while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
1.17      deraadt   240:                silent = 0;
1.59      otto      241:                if (pe_size == 0)
                    242:                        mappidtoemul(ktr_header.ktr_pid, current);
1.85      guenther  243:                if (pid_opt != -1 && pid_opt != ktr_header.ktr_pid)
1.17      deraadt   244:                        silent = 1;
                    245:                if (silent == 0 && trpoints & (1<<ktr_header.ktr_type))
1.1       deraadt   246:                        dumpheader(&ktr_header);
1.37      tedu      247:                ktrlen = ktr_header.ktr_len;
1.1       deraadt   248:                if (ktrlen > size) {
1.23      tedu      249:                        void *newm;
                    250:
1.64      guenther  251:                        if (ktrlen == SIZE_MAX)
                    252:                                errx(1, "data too long");
1.23      tedu      253:                        newm = realloc(m, ktrlen+1);
                    254:                        if (newm == NULL)
1.66      deraadt   255:                                err(1, "realloc");
1.23      tedu      256:                        m = newm;
1.1       deraadt   257:                        size = ktrlen;
                    258:                }
                    259:                if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
                    260:                        errx(1, "data too short");
1.17      deraadt   261:                if (silent)
                    262:                        continue;
1.1       deraadt   263:                if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
                    264:                        continue;
1.59      otto      265:                current = findemul(ktr_header.ktr_pid);
1.1       deraadt   266:                switch (ktr_header.ktr_type) {
                    267:                case KTR_SYSCALL:
                    268:                        ktrsyscall((struct ktr_syscall *)m);
                    269:                        break;
                    270:                case KTR_SYSRET:
                    271:                        ktrsysret((struct ktr_sysret *)m);
                    272:                        break;
                    273:                case KTR_NAMEI:
                    274:                        ktrnamei(m, ktrlen);
                    275:                        break;
                    276:                case KTR_GENIO:
                    277:                        ktrgenio((struct ktr_genio *)m, ktrlen);
                    278:                        break;
                    279:                case KTR_PSIG:
                    280:                        ktrpsig((struct ktr_psig *)m);
                    281:                        break;
                    282:                case KTR_CSW:
                    283:                        ktrcsw((struct ktr_csw *)m);
                    284:                        break;
                    285:                case KTR_EMUL:
                    286:                        ktremul(m, ktrlen);
1.59      otto      287:                        mappidtoemul(ktr_header.ktr_pid, current);
1.1       deraadt   288:                        break;
1.55      otto      289:                case KTR_STRUCT:
                    290:                        ktrstruct(m, ktrlen);
                    291:                        break;
1.81      miod      292:                case KTR_USER:
                    293:                        ktruser(m, ktrlen);
                    294:                        break;
1.1       deraadt   295:                }
                    296:                if (tail)
                    297:                        (void)fflush(stdout);
                    298:        }
1.12      espie     299:        exit(0);
1.1       deraadt   300: }
                    301:
1.59      otto      302: static void
                    303: mappidtoemul(pid_t pid, struct emulation *emul)
                    304: {
                    305:        size_t i;
                    306:        struct pid_emul *tmp;
                    307:
                    308:        for (i = 0; i < pe_size; i++) {
                    309:                if (pe_table[i].p == pid) {
                    310:                        pe_table[i].e = emul;
                    311:                        return;
                    312:                }
                    313:        }
1.90    ! doug      314:        tmp = reallocarray(pe_table, pe_size + 1, sizeof(*pe_table));
1.59      otto      315:        if (tmp == NULL)
                    316:                err(1, NULL);
                    317:        pe_table = tmp;
                    318:        pe_table[pe_size].p = pid;
                    319:        pe_table[pe_size].e = emul;
                    320:        pe_size++;
                    321: }
                    322:
                    323: static struct emulation*
                    324: findemul(pid_t pid)
                    325: {
                    326:        size_t i;
                    327:
                    328:        for (i = 0; i < pe_size; i++)
                    329:                if (pe_table[i].p == pid)
                    330:                        return pe_table[i].e;
1.60      otto      331:        return def_emul;
1.59      otto      332: }
                    333:
1.12      espie     334: static int
1.37      tedu      335: fread_tail(void *buf, size_t size, size_t num)
1.1       deraadt   336: {
                    337:        int i;
                    338:
                    339:        while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
                    340:                (void)sleep(1);
                    341:                clearerr(stdin);
                    342:        }
                    343:        return (i);
                    344: }
                    345:
1.12      espie     346: static void
1.17      deraadt   347: dumpheader(struct ktr_header *kth)
1.1       deraadt   348: {
1.67      mikeb     349:        static struct timespec prevtime;
1.1       deraadt   350:        char unknown[64], *type;
1.67      mikeb     351:        struct timespec temp;
1.1       deraadt   352:
                    353:        switch (kth->ktr_type) {
                    354:        case KTR_SYSCALL:
                    355:                type = "CALL";
                    356:                break;
                    357:        case KTR_SYSRET:
                    358:                type = "RET ";
                    359:                break;
                    360:        case KTR_NAMEI:
                    361:                type = "NAMI";
                    362:                break;
                    363:        case KTR_GENIO:
                    364:                type = "GIO ";
                    365:                break;
                    366:        case KTR_PSIG:
                    367:                type = "PSIG";
                    368:                break;
                    369:        case KTR_CSW:
                    370:                type = "CSW";
                    371:                break;
                    372:        case KTR_EMUL:
                    373:                type = "EMUL";
                    374:                break;
1.55      otto      375:        case KTR_STRUCT:
                    376:                type = "STRU";
                    377:                break;
1.81      miod      378:        case KTR_USER:
                    379:                type = "USER";
                    380:                break;
1.1       deraadt   381:        default:
1.17      deraadt   382:                (void)snprintf(unknown, sizeof unknown, "UNKNOWN(%d)",
                    383:                    kth->ktr_type);
1.1       deraadt   384:                type = unknown;
                    385:        }
                    386:
1.67      mikeb     387:        (void)printf("%6ld", (long)kth->ktr_pid);
                    388:        if (needtid)
1.69      mikeb     389:                (void)printf("/%-7ld", (long)kth->ktr_tid);
1.67      mikeb     390:        (void)printf(" %-8.*s ", MAXCOMLEN, kth->ktr_comm);
1.1       deraadt   391:        if (timestamp) {
                    392:                if (timestamp == 2) {
1.67      mikeb     393:                        timespecsub(&kth->ktr_time, &prevtime, &temp);
1.1       deraadt   394:                        prevtime = kth->ktr_time;
                    395:                } else
                    396:                        temp = kth->ktr_time;
1.79      deraadt   397:                printf("%lld.%06ld ", (long long)temp.tv_sec,
                    398:                    temp.tv_nsec / 1000);
1.1       deraadt   399:        }
                    400:        (void)printf("%s  ", type);
                    401: }
                    402:
1.12      espie     403: static void
1.17      deraadt   404: ioctldecode(u_long cmd)
1.2       deraadt   405: {
                    406:        char dirbuf[4], *dir = dirbuf;
                    407:
1.6       deraadt   408:        if (cmd & IOC_IN)
                    409:                *dir++ = 'W';
1.2       deraadt   410:        if (cmd & IOC_OUT)
                    411:                *dir++ = 'R';
                    412:        *dir = '\0';
                    413:
1.33      tedu      414:        printf(decimal ? ",_IO%s('%c',%lu" : ",_IO%s('%c',%#lx",
                    415:            dirbuf, (int)((cmd >> 8) & 0xff), cmd & 0xff);
1.2       deraadt   416:        if ((cmd & IOC_VOID) == 0)
1.34      tedu      417:                printf(decimal ? ",%lu)" : ",%#lx)", (cmd >> 16) & 0xff);
1.2       deraadt   418:        else
                    419:                printf(")");
                    420: }
1.1       deraadt   421:
1.52      otto      422: static void
                    423: ptracedecode(void)
                    424: {
                    425:        if (*ap >= 0 && *ap <
                    426:            sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
                    427:                (void)printf("%s", ptrace_ops[*ap]);
                    428:        else switch(*ap) {
                    429: #ifdef PT_GETFPREGS
                    430:        case PT_GETFPREGS:
                    431:                (void)printf("PT_GETFPREGS");
                    432:                break;
                    433: #endif
                    434:        case PT_GETREGS:
                    435:                (void)printf("PT_GETREGS");
                    436:                break;
1.75      guenther  437: #ifdef PT_GETXMMREGS
                    438:        case PT_GETXMMREGS:
                    439:                (void)printf("PT_GETXMMREGS");
                    440:                break;
                    441: #endif
1.52      otto      442: #ifdef PT_SETFPREGS
                    443:        case PT_SETFPREGS:
                    444:                (void)printf("PT_SETFPREGS");
                    445:                break;
                    446: #endif
                    447:        case PT_SETREGS:
                    448:                (void)printf("PT_SETREGS");
                    449:                break;
1.75      guenther  450: #ifdef PT_SETXMMREGS
                    451:        case PT_SETXMMREGS:
                    452:                (void)printf("PT_SETXMMREGS");
                    453:                break;
                    454: #endif
1.52      otto      455: #ifdef PT_STEP
                    456:        case PT_STEP:
                    457:                (void)printf("PT_STEP");
                    458:                break;
                    459: #endif
                    460: #ifdef PT_WCOOKIE
                    461:        case PT_WCOOKIE:
                    462:                (void)printf("PT_WCOOKIE");
                    463:                break;
                    464: #endif
                    465:        default:
                    466:                (void)printf("%ld", (long)*ap);
                    467:                break;
                    468:        }
                    469:        sep = ',';
                    470:        ap++;
                    471:        narg--;
                    472: }
                    473:
                    474: static void
                    475: pn(void (*f)(int))
                    476: {
                    477:        if (sep)
                    478:                (void)putchar(sep);
                    479:        if (fancy && f != NULL)
                    480:                f((int)*ap);
                    481:        else if (decimal)
                    482:                (void)printf("%ld", (long)*ap);
                    483:        else
                    484:                (void)printf("%#lx", (long)*ap);
                    485:        ap++;
                    486:        narg--;
                    487:        sep = ',';
                    488: }
                    489:
                    490: #ifdef __LP64__
                    491: #define plln() pn(NULL)
                    492: #elif _BYTE_ORDER == _LITTLE_ENDIAN
                    493: static void
                    494: plln(void)
                    495: {
                    496:        long long val = ((long long)*ap) & 0xffffffff;
                    497:        ap++;
                    498:        val |= ((long long)*ap) << 32;
                    499:        ap++;
                    500:        narg -= 2;
                    501:        if (sep)
                    502:                (void)putchar(sep);
                    503:        if (decimal)
                    504:                (void)printf("%lld", val);
                    505:        else
                    506:                (void)printf("%#llx", val);
                    507:        sep = ',';
                    508: }
                    509: #else
                    510: static void
                    511: plln(void)
                    512: {
                    513:        long long val = ((long long)*ap) << 32;
                    514:        ap++;
                    515:        val |= ((long long)*ap) & 0xffffffff;
                    516:        ap++;
                    517:        narg -= 2;
                    518:        if (sep)
                    519:                (void)putchar(sep);
                    520:        if (decimal)
                    521:                (void)printf("%lld", val);
                    522:        else
                    523:                (void)printf("%#llx", val);
                    524:        sep = ',';
                    525: }
                    526: #endif
1.51      otto      527:
1.12      espie     528: static void
1.17      deraadt   529: ktrsyscall(struct ktr_syscall *ktr)
1.1       deraadt   530: {
1.52      otto      531:        narg = ktr->ktr_argsize / sizeof(register_t);
                    532:        sep = '\0';
1.1       deraadt   533:
                    534:        if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
                    535:                (void)printf("[%d]", ktr->ktr_code);
                    536:        else
                    537:                (void)printf("%s", current->sysnames[ktr->ktr_code]);
                    538:        ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
1.27      mickey    539:        (void)putchar('(');
1.52      otto      540:
1.54      otto      541:        if (current != &emulations[0])
                    542:                goto nonnative;
                    543:
1.52      otto      544:        switch (ktr->ktr_code) {
                    545:        case SYS_ioctl: {
                    546:                const char *cp;
                    547:
                    548:                pn(NULL);
                    549:                if (!fancy)
                    550:                        break;
                    551:                if ((cp = ioctlname(*ap)) != NULL)
                    552:                        (void)printf(",%s", cp);
                    553:                else
                    554:                        ioctldecode(*ap);
                    555:                ap++;
                    556:                narg--;
                    557:                break;
                    558:        }
                    559:        case SYS___sysctl: {
1.62      otto      560:                const char *s;
                    561:                int *np, n, i, *top;
1.52      otto      562:
                    563:                if (!fancy)
                    564:                        break;
                    565:                n = ap[1];
                    566:                if (n > CTL_MAXNAME)
                    567:                        n = CTL_MAXNAME;
1.62      otto      568:                np = top = (int *)(ap + 6);
                    569:                for (i = 0; n--; np++, i++) {
1.52      otto      570:                        if (sep)
                    571:                                putchar(sep);
1.62      otto      572:                        if (resolv && (s = kresolvsysctl(i, top, *np)) != NULL)
                    573:                                printf("%s", s);
                    574:                        else
                    575:                                printf("%d", *np);
1.52      otto      576:                        sep = '.';
1.1       deraadt   577:                }
1.52      otto      578:
                    579:                sep = ',';
                    580:                ap += 2;
                    581:                narg -= 2;
                    582:                break;
                    583:        }
                    584:        case SYS_ptrace:
                    585:                if (!fancy)
                    586:                        break;
                    587:                ptracedecode();
                    588:                break;
1.89      guenther  589:        case SYS_accept4:
                    590:                pn(NULL);
                    591:                pn(NULL);
                    592:                pn(NULL);
                    593:                pn(sockflagsname);
                    594:                break;
1.52      otto      595:        case SYS_access:
                    596:                pn(NULL);
                    597:                pn(accessmodename);
                    598:                break;
                    599:        case SYS_chmod:
                    600:        case SYS_fchmod:
1.87      guenther  601:        case SYS_mkdir:
                    602:        case SYS_mkfifo:
                    603:        case SYS_mknod:
1.61      matthew   604:                pn(NULL);
1.87      guenther  605:                pn(modename);
                    606:                break;
                    607:        case SYS_umask:
1.52      otto      608:                pn(modename);
                    609:                break;
1.89      guenther  610:        case SYS_dup3:
                    611:                pn(NULL);
                    612:                pn(NULL);
                    613:                pn(flagsname);
                    614:                break;
1.52      otto      615:        case SYS_fcntl: {
                    616:                int cmd;
                    617:                int arg;
                    618:                pn(NULL);
                    619:                if (!fancy)
                    620:                        break;
                    621:                cmd = ap[0];
                    622:                arg = ap[1];
                    623:                (void)putchar(',');
                    624:                fcntlcmdname(cmd, arg);
                    625:                ap += 2;
                    626:                narg -= 2;
                    627:                break;
                    628:        }
                    629:        case SYS_flock:
                    630:                pn(NULL);
                    631:                pn(flockname);
                    632:                break;
                    633:        case SYS_getrlimit:
                    634:        case SYS_setrlimit:
                    635:                pn(rlimitname);
                    636:                break;
                    637:        case SYS_getsockopt:
                    638:        case SYS_setsockopt: {
                    639:                int level;
                    640:
                    641:                pn(NULL);
                    642:                level = *ap;
                    643:                pn(sockoptlevelname);
                    644:                if (level == SOL_SOCKET)
                    645:                        pn(sockoptname);
                    646:                break;
                    647:        }
                    648:        case SYS_kill:
1.82      guenther  649:                pn(pgid);
1.52      otto      650:                pn(signame);
                    651:                break;
                    652:        case SYS_lseek:
                    653:                pn(NULL);
                    654:                /* skip padding */
                    655:                ap++;
                    656:                narg--;
                    657:                plln();
                    658:                pn(whencename);
                    659:                break;
                    660:        case SYS_madvise:
                    661:                pn(NULL);
                    662:                pn(NULL);
                    663:                pn(madvisebehavname);
                    664:                break;
                    665:        case SYS_minherit:
                    666:                pn(NULL);
                    667:                pn(NULL);
                    668:                pn(minheritname);
                    669:                break;
                    670:        case SYS_mlockall:
                    671:                pn(mlockallname);
                    672:                break;
                    673:        case SYS_mmap:
1.88      guenther  674:        case SYS_mquery:
1.52      otto      675:                pn(NULL);
                    676:                pn(NULL);
                    677:                pn(mmapprotname);
                    678:                pn(mmapflagsname);
                    679:                pn(NULL);
                    680:                /* skip padding */
                    681:                ap++;
                    682:                narg--;
                    683:                plln();
                    684:                break;
                    685:        case SYS_mprotect:
                    686:                pn(NULL);
                    687:                pn(NULL);
                    688:                pn(mmapprotname);
                    689:                break;
                    690:        case SYS_msync:
                    691:                pn(NULL);
                    692:                pn(NULL);
                    693:                pn(msyncflagsname);
                    694:                break;
                    695:        case SYS_msgctl:
                    696:                pn(NULL);
                    697:                pn(shmctlname);
                    698:                break;
                    699:        case SYS_open: {
                    700:                int     flags;
                    701:                int     mode;
                    702:
                    703:                pn(NULL);
                    704:                if (!fancy)
                    705:                        break;
                    706:                flags = ap[0];
                    707:                mode = ap[1];
                    708:                (void)putchar(',');
                    709:                flagsandmodename(flags, mode);
                    710:                ap += 2;
                    711:                narg -= 2;
                    712:                break;
                    713:        }
1.89      guenther  714:        case SYS_pipe2:
                    715:                pn(NULL);
                    716:                pn(flagsname);
                    717:                break;
1.52      otto      718:        case SYS_pread:
                    719:        case SYS_preadv:
                    720:        case SYS_pwrite:
                    721:        case SYS_pwritev:
                    722:                pn(NULL);
                    723:                pn(NULL);
                    724:                pn(NULL);
                    725:                /* skip padding */
                    726:                ap++;
                    727:                narg--;
                    728:                plln();
                    729:                break;
                    730:        case SYS_recvmsg:
                    731:        case SYS_sendmsg:
                    732:                pn(NULL);
                    733:                pn(NULL);
                    734:                pn(sendrecvflagsname);
                    735:                break;
                    736:        case SYS_recvfrom:
                    737:        case SYS_sendto:
                    738:                pn(NULL);
                    739:                pn(NULL);
                    740:                pn(NULL);
                    741:                pn(sendrecvflagsname);
                    742:                break;
1.77      guenther  743:        case SYS_shutdown:
                    744:                pn(NULL);
                    745:                pn(shutdownhowname);
                    746:                break;
1.52      otto      747:        case SYS___semctl:
                    748:                pn(NULL);
                    749:                pn(NULL);
                    750:                pn(semctlname);
                    751:                break;
                    752:        case SYS_semget:
                    753:                pn(NULL);
                    754:                pn(NULL);
                    755:                pn(semgetname);
                    756:                break;
                    757:        case SYS_shmat:
                    758:                pn(NULL);
                    759:                pn(NULL);
                    760:                pn(shmatname);
                    761:                break;
                    762:        case SYS_shmctl:
                    763:                pn(NULL);
                    764:                pn(shmctlname);
                    765:                break;
1.64      guenther  766:        case SYS_clock_gettime:
                    767:        case SYS_clock_settime:
                    768:        case SYS_clock_getres:
                    769:                pn(clockname);
                    770:                break;
1.77      guenther  771:        case SYS_poll:
                    772:                pn(NULL);
                    773:                pn(NULL);
                    774:                pn(polltimeout);
                    775:                break;
1.52      otto      776:        case SYS_sigaction:
                    777:                pn(signame);
                    778:                break;
                    779:        case SYS_sigprocmask:
                    780:                pn(sigprocmaskhowname);
1.64      guenther  781:                pn(sigset);
                    782:                break;
                    783:        case SYS_sigsuspend:
                    784:                pn(sigset);
1.52      otto      785:                break;
                    786:        case SYS_socket: {
                    787:                int sockdomain = *ap;
                    788:
                    789:                pn(sockdomainname);
                    790:                pn(socktypename);
                    791:                if (sockdomain == PF_INET || sockdomain == PF_INET6)
                    792:                        pn(sockipprotoname);
                    793:                break;
                    794:        }
                    795:        case SYS_socketpair:
                    796:                pn(sockdomainname);
                    797:                pn(socktypename);
                    798:                break;
                    799:        case SYS_truncate:
                    800:        case SYS_ftruncate:
                    801:                pn(NULL);
                    802:                /* skip padding */
                    803:                ap++;
                    804:                narg--;
                    805:                plln();
                    806:                break;
                    807:        case SYS_wait4:
1.82      guenther  808:                pn(wait4pid);
1.52      otto      809:                pn(NULL);
                    810:                pn(wait4optname);
                    811:                break;
1.77      guenther  812:        case SYS_getrusage:
                    813:                pn(rusagewho);
                    814:                break;
1.64      guenther  815:        case SYS___thrsleep:
                    816:                pn(NULL);
                    817:                pn(clockname);
                    818:                break;
                    819:        case SYS___thrsigdivert:
                    820:                pn(sigset);
                    821:                break;
1.61      matthew   822:        case SYS_faccessat:
                    823:                pn(atfd);
                    824:                pn(NULL);
                    825:                pn(accessmodename);
                    826:                pn(atflagsname);
                    827:                break;
                    828:        case SYS_fchmodat:
                    829:                pn(atfd);
                    830:                pn(NULL);
                    831:                pn(modename);
                    832:                pn(atflagsname);
                    833:                break;
                    834:        case SYS_fchownat:
                    835:                pn(atfd);
                    836:                pn(NULL);
                    837:                pn(NULL);
                    838:                pn(NULL);
                    839:                pn(atflagsname);
                    840:                break;
                    841:        case SYS_fstatat:
                    842:                pn(atfd);
                    843:                pn(NULL);
                    844:                pn(NULL);
                    845:                pn(atflagsname);
                    846:                break;
                    847:        case SYS_linkat:
                    848:                pn(atfd);
                    849:                pn(NULL);
                    850:                pn(atfd);
                    851:                pn(NULL);
                    852:                pn(atflagsname);
                    853:                break;
                    854:        case SYS_mkdirat:
                    855:        case SYS_mkfifoat:
                    856:        case SYS_mknodat:
                    857:                pn(atfd);
                    858:                pn(NULL);
                    859:                pn(modename);
                    860:                break;
                    861:        case SYS_openat: {
                    862:                int     flags;
                    863:                int     mode;
                    864:
                    865:                pn(atfd);
                    866:                pn(NULL);
                    867:                if (!fancy)
                    868:                        break;
                    869:                flags = ap[0];
                    870:                mode = ap[1];
                    871:                (void)putchar(',');
                    872:                flagsandmodename(flags, mode);
                    873:                ap += 2;
                    874:                narg -= 2;
                    875:                break;
                    876:        }
                    877:        case SYS_readlinkat:
                    878:                pn(atfd);
                    879:                break;
                    880:        case SYS_renameat:
                    881:                pn(atfd);
                    882:                pn(NULL);
                    883:                pn(atfd);
                    884:                break;
                    885:        case SYS_symlinkat:
                    886:                pn(NULL);
                    887:                pn(atfd);
                    888:                break;
                    889:        case SYS_unlinkat:
                    890:                pn(atfd);
                    891:                pn(NULL);
                    892:                pn(atflagsname);
                    893:                break;
                    894:        case SYS_utimensat:
                    895:                pn(atfd);
                    896:                pn(NULL);
                    897:                pn(NULL);
                    898:                pn(atflagsname);
                    899:                break;
1.77      guenther  900:        case SYS_pathconf:
                    901:        case SYS_fpathconf:
                    902:                pn(NULL);
                    903:                pn(pathconfname);
1.86      guenther  904:                break;
                    905:        case SYS_ktrace:
                    906:                pn(NULL);
1.88      guenther  907:                pn(ktraceopname);
1.86      guenther  908:                pn(ktracefacname);
                    909:                pn(pgid);
                    910:                break;
                    911:        case SYS_setitimer:
                    912:        case SYS_getitimer:
                    913:                pn(itimername);
1.77      guenther  914:                break;
1.52      otto      915:        }
                    916:
1.54      otto      917: nonnative:
1.52      otto      918:        while (narg) {
                    919:                if (sep)
                    920:                        putchar(sep);
                    921:                if (decimal)
                    922:                        (void)printf("%ld", (long)*ap);
                    923:                else
                    924:                        (void)printf("%#lx", (long)*ap);
                    925:                sep = ',';
                    926:                ap++;
                    927:                narg--;
1.1       deraadt   928:        }
1.27      mickey    929:        (void)printf(")\n");
1.62      otto      930: }
                    931:
                    932: static struct ctlname topname[] = CTL_NAMES;
                    933: static struct ctlname kernname[] = CTL_KERN_NAMES;
                    934: static struct ctlname vmname[] = CTL_VM_NAMES;
                    935: static struct ctlname fsname[] = CTL_FS_NAMES;
                    936: static struct ctlname netname[] = CTL_NET_NAMES;
                    937: static struct ctlname hwname[] = CTL_HW_NAMES;
                    938: static struct ctlname debugname[CTL_DEBUG_MAXID];
                    939: static struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES;
                    940: static struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES;
                    941: static struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES;
1.88      guenther  942: static struct ctlname kernprocname[] = {
1.64      guenther  943:        { NULL },
                    944:        { "all" },
                    945:        { "pid" },
                    946:        { "pgrp" },
                    947:        { "session" },
                    948:        { "tty" },
                    949:        { "uid" },
                    950:        { "ruid" },
1.77      guenther  951:        { "kthread" },
1.64      guenther  952: };
1.62      otto      953: static struct ctlname ttysname[] = CTL_KERN_TTY_NAMES;
                    954: static struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES;
                    955: static struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES;
                    956: static struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES;
                    957: static struct ctlname tcname[] = CTL_KERN_TIMECOUNTER_NAMES;
                    958: #ifdef CTL_MACHDEP_NAMES
                    959: static struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
                    960: #endif
                    961: static struct ctlname ddbname[] = CTL_DDB_NAMES;
                    962:
                    963: #ifndef nitems
                    964: #define nitems(_a)    (sizeof((_a)) / sizeof((_a)[0]))
                    965: #endif
                    966:
                    967: #define SETNAME(name) do { names = (name); limit = nitems(name); } while (0)
                    968:
                    969: static const char *
                    970: kresolvsysctl(int depth, int *top, int idx)
                    971: {
                    972:        struct ctlname *names;
                    973:        size_t          limit;
                    974:
                    975:        names = NULL;
                    976:
                    977:        switch (depth) {
                    978:        case 0:
                    979:                SETNAME(topname);
                    980:                break;
                    981:        case 1:
                    982:                switch (top[0]) {
                    983:                case CTL_KERN:
                    984:                        SETNAME(kernname);
                    985:                        break;
                    986:                case CTL_VM:
                    987:                        SETNAME(vmname);
                    988:                        break;
                    989:                case CTL_FS:
                    990:                        SETNAME(fsname);
                    991:                        break;
                    992:                case CTL_NET:
                    993:                        SETNAME(netname);
                    994:                        break;
                    995:                case CTL_DEBUG:
                    996:                        SETNAME(debugname);
                    997:                        break;
                    998:                case CTL_HW:
                    999:                        SETNAME(hwname);
                   1000:                        break;
                   1001: #ifdef CTL_MACHDEP_NAMES
                   1002:                case CTL_MACHDEP:
                   1003:                        SETNAME(machdepname);
                   1004:                        break;
                   1005: #endif
                   1006:                case CTL_DDB:
                   1007:                        SETNAME(ddbname);
                   1008:                        break;
                   1009:                }
                   1010:                break;
                   1011:        case 2:
                   1012:                switch (top[0]) {
                   1013:                case CTL_KERN:
                   1014:                        switch (top[1]) {
                   1015:                        case KERN_MALLOCSTATS:
                   1016:                                SETNAME(kernmallocname);
                   1017:                                break;
                   1018:                        case KERN_FORKSTAT:
                   1019:                                SETNAME(forkstatname);
                   1020:                                break;
                   1021:                        case KERN_NCHSTATS:
                   1022:                                SETNAME(nchstatsname);
                   1023:                                break;
                   1024:                        case KERN_TTY:
                   1025:                                SETNAME(ttysname);
                   1026:                                break;
                   1027:                        case KERN_SEMINFO:
                   1028:                                SETNAME(semname);
                   1029:                                break;
                   1030:                        case KERN_SHMINFO:
                   1031:                                SETNAME(shmname);
                   1032:                                break;
                   1033:                        case KERN_WATCHDOG:
                   1034:                                SETNAME(watchdogname);
                   1035:                                break;
1.64      guenther 1036:                        case KERN_PROC:
                   1037:                                idx++;  /* zero is valid at this level */
                   1038:                                SETNAME(kernprocname);
                   1039:                                break;
1.62      otto     1040:                        case KERN_TIMECOUNTER:
                   1041:                                SETNAME(tcname);
                   1042:                                break;
                   1043:                        }
                   1044:                }
                   1045:                break;
                   1046:        }
                   1047:        if (names != NULL && idx > 0 && idx < limit)
                   1048:                return (names[idx].ctl_name);
                   1049:        return (NULL);
1.1       deraadt  1050: }
                   1051:
1.12      espie    1052: static void
1.17      deraadt  1053: ktrsysret(struct ktr_sysret *ktr)
1.1       deraadt  1054: {
1.50      deraadt  1055:        register_t ret = ktr->ktr_retval;
1.12      espie    1056:        int error = ktr->ktr_error;
                   1057:        int code = ktr->ktr_code;
1.1       deraadt  1058:
                   1059:        if (code >= current->nsysnames || code < 0)
                   1060:                (void)printf("[%d] ", code);
1.59      otto     1061:        else {
1.1       deraadt  1062:                (void)printf("%s ", current->sysnames[code]);
1.59      otto     1063:                if (ret > 0 && (strcmp(current->sysnames[code], "fork") == 0 ||
                   1064:                    strcmp(current->sysnames[code], "vfork") == 0 ||
1.64      guenther 1065:                    strcmp(current->sysnames[code], "__tfork") == 0 ||
1.59      otto     1066:                    strcmp(current->sysnames[code], "clone") == 0))
                   1067:                        mappidtoemul(ret, current);
                   1068:        }
1.1       deraadt  1069:
                   1070:        if (error == 0) {
                   1071:                if (fancy) {
1.64      guenther 1072:                        switch (current == &emulations[0] ? code : -1) {
                   1073:                        case SYS_sigprocmask:
1.73      guenther 1074:                        case SYS_sigpending:
1.64      guenther 1075:                                sigset(ret);
                   1076:                                break;
                   1077:                        case SYS___thrsigdivert:
                   1078:                                signame(ret);
                   1079:                                break;
                   1080:                        case -1:        /* non-default emulation */
                   1081:                        default:
                   1082:                                (void)printf("%ld", (long)ret);
                   1083:                                if (ret < 0 || ret > 9)
                   1084:                                        (void)printf("/%#lx", (long)ret);
                   1085:                        }
1.1       deraadt  1086:                } else {
                   1087:                        if (decimal)
1.50      deraadt  1088:                                (void)printf("%ld", (long)ret);
1.1       deraadt  1089:                        else
1.50      deraadt  1090:                                (void)printf("%#lx", (long)ret);
1.1       deraadt  1091:                }
                   1092:        } else if (error == ERESTART)
                   1093:                (void)printf("RESTART");
                   1094:        else if (error == EJUSTRETURN)
                   1095:                (void)printf("JUSTRETURN");
                   1096:        else {
                   1097:                (void)printf("-1 errno %d", ktr->ktr_error);
                   1098:                if (fancy)
                   1099:                        (void)printf(" %s", strerror(ktr->ktr_error));
                   1100:        }
                   1101:        (void)putchar('\n');
                   1102: }
                   1103:
1.12      espie    1104: static void
1.37      tedu     1105: ktrnamei(const char *cp, size_t len)
1.1       deraadt  1106: {
1.37      tedu     1107:        (void)printf("\"%.*s\"\n", (int)len, cp);
1.1       deraadt  1108: }
                   1109:
1.12      espie    1110: static void
1.37      tedu     1111: ktremul(char *cp, size_t len)
1.1       deraadt  1112: {
                   1113:        char name[1024];
                   1114:
                   1115:        if (len >= sizeof(name))
                   1116:                errx(1, "Emulation name too long");
                   1117:
                   1118:        strncpy(name, cp, len);
                   1119:        name[len] = '\0';
                   1120:        (void)printf("\"%s\"\n", name);
                   1121:
                   1122:        setemul(name);
                   1123: }
                   1124:
1.12      espie    1125: static void
1.81      miod     1126: showbuf(unsigned char *dp, size_t datalen)
1.1       deraadt  1127: {
1.37      tedu     1128:        int i, j;
1.81      miod     1129:        static int screenwidth;
1.31      tedu     1130:        int col = 0, width, bpl;
1.32      tedu     1131:        unsigned char visbuf[5], *cp, c;
1.1       deraadt  1132:
                   1133:        if (screenwidth == 0) {
                   1134:                struct winsize ws;
                   1135:
                   1136:                if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
                   1137:                    ws.ws_col > 8)
                   1138:                        screenwidth = ws.ws_col;
                   1139:                else
                   1140:                        screenwidth = 80;
                   1141:        }
1.31      tedu     1142:        if (iohex == 1) {
                   1143:                putchar('\t');
                   1144:                col = 8;
                   1145:                for (i = 0; i < datalen; i++) {
1.35      tedu     1146:                        printf("%02x", dp[i]);
1.31      tedu     1147:                        col += 3;
                   1148:                        if (i < datalen - 1) {
                   1149:                                if (col + 3 > screenwidth) {
                   1150:                                        printf("\n\t");
                   1151:                                        col = 8;
                   1152:                                } else
                   1153:                                        putchar(' ');
                   1154:                        }
                   1155:                }
                   1156:                putchar('\n');
                   1157:                return;
                   1158:        }
                   1159:        if (iohex == 2) {
                   1160:                bpl = (screenwidth - 13)/4;
                   1161:                if (bpl <= 0)
                   1162:                        bpl = 1;
                   1163:                for (i = 0; i < datalen; i += bpl) {
                   1164:                        printf("   %04x:  ", i);
                   1165:                        for (j = 0; j < bpl; j++) {
                   1166:                                if (i+j >= datalen)
                   1167:                                        printf("   ");
                   1168:                                else
1.35      tedu     1169:                                        printf("%02x ", dp[i+j]);
1.31      tedu     1170:                        }
                   1171:                        putchar(' ');
                   1172:                        for (j = 0; j < bpl; j++) {
                   1173:                                if (i+j >= datalen)
                   1174:                                        break;
                   1175:                                c = dp[i+j];
                   1176:                                if (!isprint(c))
                   1177:                                        c = '.';
                   1178:                                putchar(c);
                   1179:                        }
                   1180:                        putchar('\n');
                   1181:                }
                   1182:                return;
                   1183:        }
1.1       deraadt  1184:        (void)printf("       \"");
                   1185:        col = 8;
                   1186:        for (; datalen > 0; datalen--, dp++) {
1.31      tedu     1187:                (void)vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1.1       deraadt  1188:                cp = visbuf;
1.17      deraadt  1189:
1.1       deraadt  1190:                /*
                   1191:                 * Keep track of printables and
                   1192:                 * space chars (like fold(1)).
                   1193:                 */
                   1194:                if (col == 0) {
                   1195:                        (void)putchar('\t');
                   1196:                        col = 8;
                   1197:                }
1.17      deraadt  1198:                switch (*cp) {
1.1       deraadt  1199:                case '\n':
                   1200:                        col = 0;
                   1201:                        (void)putchar('\n');
                   1202:                        continue;
                   1203:                case '\t':
                   1204:                        width = 8 - (col&07);
                   1205:                        break;
                   1206:                default:
                   1207:                        width = strlen(cp);
                   1208:                }
                   1209:                if (col + width > (screenwidth-2)) {
                   1210:                        (void)printf("\\\n\t");
                   1211:                        col = 8;
                   1212:                }
                   1213:                col += width;
                   1214:                do {
                   1215:                        (void)putchar(*cp++);
                   1216:                } while (*cp);
                   1217:        }
                   1218:        if (col == 0)
                   1219:                (void)printf("       ");
                   1220:        (void)printf("\"\n");
                   1221: }
                   1222:
1.12      espie    1223: static void
1.81      miod     1224: ktrgenio(struct ktr_genio *ktr, size_t len)
                   1225: {
                   1226:        unsigned char *dp = (unsigned char *)ktr + sizeof(struct ktr_genio);
                   1227:        size_t datalen = len - sizeof(struct ktr_genio);
                   1228:
                   1229:        printf("fd %d %s %zu bytes\n", ktr->ktr_fd,
                   1230:                ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
                   1231:        if (maxdata == 0)
                   1232:                return;
                   1233:        if (datalen > maxdata)
                   1234:                datalen = maxdata;
                   1235:        if (iohex && !datalen)
                   1236:                return;
                   1237:        showbuf(dp, datalen);
                   1238: }
                   1239:
                   1240: static void
1.17      deraadt  1241: ktrpsig(struct ktr_psig *psig)
1.1       deraadt  1242: {
                   1243:        (void)printf("SIG%s ", sys_signame[psig->signo]);
                   1244:        if (psig->action == SIG_DFL)
1.63      deraadt  1245:                (void)printf("SIG_DFL");
1.64      guenther 1246:        else {
                   1247:                (void)printf("caught handler=0x%lx mask=",
                   1248:                    (u_long)psig->action);
                   1249:                sigset(psig->mask);
                   1250:        }
1.63      deraadt  1251:        if (psig->code) {
                   1252:                printf(" code ");
                   1253:                if (fancy) {
                   1254:                        switch (psig->signo) {
                   1255:                        case SIGILL:
                   1256:                                sigill_name(psig->code);
                   1257:                                break;
                   1258:                        case SIGTRAP:
                   1259:                                sigtrap_name(psig->code);
                   1260:                                break;
                   1261:                        case SIGEMT:
                   1262:                                sigemt_name(psig->code);
                   1263:                                break;
                   1264:                        case SIGFPE:
                   1265:                                sigfpe_name(psig->code);
                   1266:                                break;
                   1267:                        case SIGBUS:
                   1268:                                sigbus_name(psig->code);
                   1269:                                break;
                   1270:                        case SIGSEGV:
                   1271:                                sigsegv_name(psig->code);
                   1272:                                break;
                   1273:                        case SIGCHLD:
                   1274:                                sigchld_name(psig->code);
                   1275:                                break;
                   1276:                        }
                   1277:                }
                   1278:                printf("<%d>", psig->code);
                   1279:        }
                   1280:
1.14      deraadt  1281:        switch (psig->signo) {
                   1282:        case SIGSEGV:
                   1283:        case SIGILL:
                   1284:        case SIGBUS:
                   1285:        case SIGFPE:
                   1286:                printf(" addr=%p trapno=%d", psig->si.si_addr,
                   1287:                    psig->si.si_trapno);
                   1288:                break;
                   1289:        default:
                   1290:                break;
                   1291:        }
                   1292:        printf("\n");
1.1       deraadt  1293: }
                   1294:
1.12      espie    1295: static void
1.17      deraadt  1296: ktrcsw(struct ktr_csw *cs)
1.1       deraadt  1297: {
                   1298:        (void)printf("%s %s\n", cs->out ? "stop" : "resume",
                   1299:            cs->user ? "user" : "kernel");
                   1300: }
                   1301:
1.64      guenther 1302: static void
1.81      miod     1303: ktruser(struct ktr_user *usr, size_t len)
                   1304: {
                   1305:        len -= sizeof(struct ktr_user);
                   1306:        printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
                   1307:        printf(" %zu bytes\n", len);
                   1308:        showbuf((unsigned char *)(usr + 1), len);
                   1309: }
                   1310:
                   1311: static void
1.17      deraadt  1312: usage(void)
1.1       deraadt  1313: {
                   1314:
1.19      mickey   1315:        extern char *__progname;
                   1316:        fprintf(stderr, "usage: %s "
1.68      jmc      1317:            "[-dHlnRrTXx] [-e emulation] [-f file] [-m maxdata] [-p pid]\n"
1.81      miod     1318:            "%*s[-t [ceinstuw]]\n",
1.51      otto     1319:            __progname, (int)(sizeof("usage: ") + strlen(__progname)), "");
1.1       deraadt  1320:        exit(1);
                   1321: }
                   1322:
1.12      espie    1323: static void
1.17      deraadt  1324: setemul(const char *name)
1.1       deraadt  1325: {
                   1326:        int i;
1.17      deraadt  1327:
1.1       deraadt  1328:        for (i = 0; emulations[i].name != NULL; i++)
                   1329:                if (strcmp(emulations[i].name, name) == 0) {
                   1330:                        current = &emulations[i];
                   1331:                        return;
                   1332:                }
                   1333:        warnx("Emulation `%s' unknown", name);
1.61      matthew  1334: }
                   1335:
                   1336: static void
                   1337: atfd(int fd)
                   1338: {
                   1339:        if (fd == AT_FDCWD)
                   1340:                (void)printf("AT_FDCWD");
                   1341:        else if (decimal)
                   1342:                (void)printf("%d", fd);
                   1343:        else
                   1344:                (void)printf("%#x", fd);
1.77      guenther 1345: }
                   1346:
                   1347: static void
                   1348: polltimeout(int timeout)
                   1349: {
                   1350:        if (timeout == INFTIM)
                   1351:                (void)printf("INFTIM");
                   1352:        else if (decimal)
                   1353:                (void)printf("%d", timeout);
                   1354:        else
                   1355:                (void)printf("%#x", timeout);
1.82      guenther 1356: }
                   1357:
                   1358: static void
                   1359: pgid(int pid)
                   1360: {
                   1361:        (void)printf("%d", pid);
                   1362: }
                   1363:
                   1364: static void
                   1365: wait4pid(int pid)
                   1366: {
                   1367:        if (pid == WAIT_ANY)
                   1368:                (void)printf("WAIT_ANY");
                   1369:        else if (pid == WAIT_MYPGRP)
                   1370:                (void)printf("WAIT_MYPGRP");
                   1371:        else
                   1372:                pgid(pid);
1.1       deraadt  1373: }
1.83      guenther 1374:
                   1375: static void
                   1376: signame(int sig)
                   1377: {
                   1378:        if (sig > 0 && sig < NSIG)
                   1379:                (void)printf("SIG%s", sys_signame[sig]);
                   1380:        else
                   1381:                (void)printf("SIG %d", sig);
                   1382: }
                   1383:
1.84      guenther 1384: void
1.83      guenther 1385: sigset(int ss)
                   1386: {
                   1387:        int     or = 0;
                   1388:        int     cnt = 0;
                   1389:        int     i;
                   1390:
                   1391:        for (i = 1; i < NSIG; i++)
                   1392:                if (sigismember(&ss, i))
                   1393:                        cnt++;
                   1394:        if (cnt > (NSIG-1)/2) {
                   1395:                ss = ~ss;
                   1396:                putchar('~');
                   1397:        }
                   1398:
                   1399:        if (ss == 0) {
                   1400:                (void)printf("0<>");
                   1401:                return;
                   1402:        }
                   1403:
                   1404:        printf("%#x<", ss);
                   1405:        for (i = 1; i < NSIG; i++)
                   1406:                if (sigismember(&ss, i)) {
                   1407:                        if (or) putchar('|'); else or=1;
                   1408:                        signame(i);
                   1409:                }
                   1410:        printf(">");
                   1411: }
                   1412:
                   1413: static void
                   1414: semctlname(int cmd)
                   1415: {
                   1416:        switch (cmd) {
                   1417:        case GETNCNT:
                   1418:                (void)printf("GETNCNT");
                   1419:                break;
                   1420:        case GETPID:
                   1421:                (void)printf("GETPID");
                   1422:                break;
                   1423:        case GETVAL:
                   1424:                (void)printf("GETVAL");
                   1425:                break;
                   1426:        case GETALL:
                   1427:                (void)printf("GETALL");
                   1428:                break;
                   1429:        case GETZCNT:
                   1430:                (void)printf("GETZCNT");
                   1431:                break;
                   1432:        case SETVAL:
                   1433:                (void)printf("SETVAL");
                   1434:                break;
                   1435:        case SETALL:
                   1436:                (void)printf("SETALL");
                   1437:                break;
                   1438:        case IPC_RMID:
                   1439:                (void)printf("IPC_RMID");
                   1440:                break;
                   1441:        case IPC_SET:
                   1442:                (void)printf("IPC_SET");
                   1443:                break;
                   1444:        case IPC_STAT:
                   1445:                (void)printf("IPC_STAT");
                   1446:                break;
                   1447:        default: /* Should not reach */
                   1448:                (void)printf("<invalid=%ld>", (long)cmd);
                   1449:        }
                   1450: }
                   1451:
                   1452: static void
1.88      guenther 1453: shmctlname(int cmd)
                   1454: {
1.83      guenther 1455:        switch (cmd) {
                   1456:        case IPC_RMID:
                   1457:                (void)printf("IPC_RMID");
                   1458:                break;
                   1459:        case IPC_SET:
                   1460:                (void)printf("IPC_SET");
                   1461:                break;
                   1462:        case IPC_STAT:
                   1463:                (void)printf("IPC_STAT");
                   1464:                break;
                   1465:        default: /* Should not reach */
                   1466:                (void)printf("<invalid=%ld>", (long)cmd);
                   1467:        }
                   1468: }
                   1469:
                   1470:
                   1471: static void
1.88      guenther 1472: semgetname(int flag)
                   1473: {
1.83      guenther 1474:        int     or = 0;
                   1475:        if_print_or(flag, IPC_CREAT, or);
                   1476:        if_print_or(flag, IPC_EXCL, or);
                   1477:        if_print_or(flag, SEM_R, or);
                   1478:        if_print_or(flag, SEM_A, or);
                   1479:        if_print_or(flag, (SEM_R>>3), or);
                   1480:        if_print_or(flag, (SEM_A>>3), or);
                   1481:        if_print_or(flag, (SEM_R>>6), or);
                   1482:        if_print_or(flag, (SEM_A>>6), or);
                   1483: }
                   1484:
                   1485:
                   1486: /*
                   1487:  * Only used by SYS_open. Unless O_CREAT is set in flags, the
                   1488:  * mode argument is unused (and often bogus and misleading).
                   1489:  */
                   1490: static void
1.88      guenther 1491: flagsandmodename(int flags, int mode)
                   1492: {
1.89      guenther 1493:        doflagsname(flags, 1);
1.83      guenther 1494:        if ((flags & O_CREAT) == O_CREAT) {
                   1495:                (void)putchar(',');
                   1496:                modename (mode);
                   1497:        } else if (!fancy) {
                   1498:                (void)putchar(',');
                   1499:                if (decimal) {
                   1500:                        (void)printf("<unused>%ld", (long)mode);
                   1501:                } else {
                   1502:                        (void)printf("<unused>%#lx", (long)mode);
                   1503:                }
                   1504:        }
                   1505: }
                   1506:
                   1507: static void
                   1508: clockname(int clockid)
                   1509: {
                   1510:        clocktypename(__CLOCK_TYPE(clockid));
                   1511:        if (__CLOCK_PTID(clockid) != 0)
                   1512:                printf("(%d)", __CLOCK_PTID(clockid));
                   1513: }
                   1514:
                   1515: /*
                   1516:  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
                   1517:  * referring to a line in /etc/protocols . It might be appropriate
                   1518:  * to use getprotoent(3) here.
                   1519:  */
                   1520: static void
                   1521: sockoptlevelname(int level)
                   1522: {
                   1523:        if (level == SOL_SOCKET) {
                   1524:                (void)printf("SOL_SOCKET");
                   1525:        } else {
                   1526:                if (decimal) {
                   1527:                        (void)printf("%ld", (long)level);
                   1528:                } else {
                   1529:                        (void)printf("%#lx", (long)level);
                   1530:                }
                   1531:        }
                   1532: }
                   1533:
1.88      guenther 1534: static void
                   1535: ktraceopname(int ops)
                   1536: {
                   1537:        int invalid = 0;
                   1538:
                   1539:        printf("%#x<", ops);
                   1540:        switch (KTROP(ops)) {
                   1541:        case KTROP_SET:
                   1542:                printf("KTROP_SET");
                   1543:                break;
                   1544:        case KTROP_CLEAR:
                   1545:                printf("KTROP_CLEAR");
                   1546:                break;
                   1547:        case KTROP_CLEARFILE:
                   1548:                printf("KTROP_CLEARFILE");
                   1549:                break;
                   1550:        default:
                   1551:                printf("KTROP(%d)", KTROP(ops));
                   1552:                invalid = 1;
                   1553:                break;
                   1554:        }
                   1555:        if (ops & KTRFLAG_DESCEND) printf ("|%s", "KTRFLAG_DESCEND");
                   1556:        printf(">");
                   1557:        if (invalid || (ops & ~(KTROP((unsigned)-1) | KTRFLAG_DESCEND)))
                   1558:                (void)printf("<invalid>%ld", (long)ops);
                   1559: }