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

1.88    ! guenther    1: /*     $OpenBSD: kdump.c,v 1.87 2014/08/17 22:25:53 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:        }
                    314:        tmp = realloc(pe_table, (pe_size + 1) * sizeof(*pe_table));
                    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;
                    589:        case SYS_access:
                    590:                pn(NULL);
                    591:                pn(accessmodename);
                    592:                break;
                    593:        case SYS_chmod:
                    594:        case SYS_fchmod:
1.87      guenther  595:        case SYS_mkdir:
                    596:        case SYS_mkfifo:
                    597:        case SYS_mknod:
1.61      matthew   598:                pn(NULL);
1.87      guenther  599:                pn(modename);
                    600:                break;
                    601:        case SYS_umask:
1.52      otto      602:                pn(modename);
                    603:                break;
                    604:        case SYS_fcntl: {
                    605:                int cmd;
                    606:                int arg;
                    607:                pn(NULL);
                    608:                if (!fancy)
                    609:                        break;
                    610:                cmd = ap[0];
                    611:                arg = ap[1];
                    612:                (void)putchar(',');
                    613:                fcntlcmdname(cmd, arg);
                    614:                ap += 2;
                    615:                narg -= 2;
                    616:                break;
                    617:        }
                    618:        case SYS_flock:
                    619:                pn(NULL);
                    620:                pn(flockname);
                    621:                break;
                    622:        case SYS_getrlimit:
                    623:        case SYS_setrlimit:
                    624:                pn(rlimitname);
                    625:                break;
                    626:        case SYS_getsockopt:
                    627:        case SYS_setsockopt: {
                    628:                int level;
                    629:
                    630:                pn(NULL);
                    631:                level = *ap;
                    632:                pn(sockoptlevelname);
                    633:                if (level == SOL_SOCKET)
                    634:                        pn(sockoptname);
                    635:                break;
                    636:        }
                    637:        case SYS_kill:
1.82      guenther  638:                pn(pgid);
1.52      otto      639:                pn(signame);
                    640:                break;
                    641:        case SYS_lseek:
                    642:                pn(NULL);
                    643:                /* skip padding */
                    644:                ap++;
                    645:                narg--;
                    646:                plln();
                    647:                pn(whencename);
                    648:                break;
                    649:        case SYS_madvise:
                    650:                pn(NULL);
                    651:                pn(NULL);
                    652:                pn(madvisebehavname);
                    653:                break;
                    654:        case SYS_minherit:
                    655:                pn(NULL);
                    656:                pn(NULL);
                    657:                pn(minheritname);
                    658:                break;
                    659:        case SYS_mlockall:
                    660:                pn(mlockallname);
                    661:                break;
                    662:        case SYS_mmap:
1.88    ! guenther  663:        case SYS_mquery:
1.52      otto      664:                pn(NULL);
                    665:                pn(NULL);
                    666:                pn(mmapprotname);
                    667:                pn(mmapflagsname);
                    668:                pn(NULL);
                    669:                /* skip padding */
                    670:                ap++;
                    671:                narg--;
                    672:                plln();
                    673:                break;
                    674:        case SYS_mprotect:
                    675:                pn(NULL);
                    676:                pn(NULL);
                    677:                pn(mmapprotname);
                    678:                break;
                    679:        case SYS_msync:
                    680:                pn(NULL);
                    681:                pn(NULL);
                    682:                pn(msyncflagsname);
                    683:                break;
                    684:        case SYS_msgctl:
                    685:                pn(NULL);
                    686:                pn(shmctlname);
                    687:                break;
                    688:        case SYS_open: {
                    689:                int     flags;
                    690:                int     mode;
                    691:
                    692:                pn(NULL);
                    693:                if (!fancy)
                    694:                        break;
                    695:                flags = ap[0];
                    696:                mode = ap[1];
                    697:                (void)putchar(',');
                    698:                flagsandmodename(flags, mode);
                    699:                ap += 2;
                    700:                narg -= 2;
                    701:                break;
                    702:        }
                    703:        case SYS_pread:
                    704:        case SYS_preadv:
                    705:        case SYS_pwrite:
                    706:        case SYS_pwritev:
                    707:                pn(NULL);
                    708:                pn(NULL);
                    709:                pn(NULL);
                    710:                /* skip padding */
                    711:                ap++;
                    712:                narg--;
                    713:                plln();
                    714:                break;
                    715:        case SYS_recvmsg:
                    716:        case SYS_sendmsg:
                    717:                pn(NULL);
                    718:                pn(NULL);
                    719:                pn(sendrecvflagsname);
                    720:                break;
                    721:        case SYS_recvfrom:
                    722:        case SYS_sendto:
                    723:                pn(NULL);
                    724:                pn(NULL);
                    725:                pn(NULL);
                    726:                pn(sendrecvflagsname);
                    727:                break;
1.77      guenther  728:        case SYS_shutdown:
                    729:                pn(NULL);
                    730:                pn(shutdownhowname);
                    731:                break;
1.52      otto      732:        case SYS___semctl:
                    733:                pn(NULL);
                    734:                pn(NULL);
                    735:                pn(semctlname);
                    736:                break;
                    737:        case SYS_semget:
                    738:                pn(NULL);
                    739:                pn(NULL);
                    740:                pn(semgetname);
                    741:                break;
                    742:        case SYS_shmat:
                    743:                pn(NULL);
                    744:                pn(NULL);
                    745:                pn(shmatname);
                    746:                break;
                    747:        case SYS_shmctl:
                    748:                pn(NULL);
                    749:                pn(shmctlname);
                    750:                break;
1.64      guenther  751:        case SYS_clock_gettime:
                    752:        case SYS_clock_settime:
                    753:        case SYS_clock_getres:
                    754:                pn(clockname);
                    755:                break;
1.77      guenther  756:        case SYS_poll:
                    757:                pn(NULL);
                    758:                pn(NULL);
                    759:                pn(polltimeout);
                    760:                break;
1.52      otto      761:        case SYS_sigaction:
                    762:                pn(signame);
                    763:                break;
                    764:        case SYS_sigprocmask:
                    765:                pn(sigprocmaskhowname);
1.64      guenther  766:                pn(sigset);
                    767:                break;
                    768:        case SYS_sigsuspend:
                    769:                pn(sigset);
1.52      otto      770:                break;
                    771:        case SYS_socket: {
                    772:                int sockdomain = *ap;
                    773:
                    774:                pn(sockdomainname);
                    775:                pn(socktypename);
                    776:                if (sockdomain == PF_INET || sockdomain == PF_INET6)
                    777:                        pn(sockipprotoname);
                    778:                break;
                    779:        }
                    780:        case SYS_socketpair:
                    781:                pn(sockdomainname);
                    782:                pn(socktypename);
                    783:                break;
                    784:        case SYS_truncate:
                    785:        case SYS_ftruncate:
                    786:                pn(NULL);
                    787:                /* skip padding */
                    788:                ap++;
                    789:                narg--;
                    790:                plln();
                    791:                break;
                    792:        case SYS_wait4:
1.82      guenther  793:                pn(wait4pid);
1.52      otto      794:                pn(NULL);
                    795:                pn(wait4optname);
                    796:                break;
1.77      guenther  797:        case SYS_getrusage:
                    798:                pn(rusagewho);
                    799:                break;
1.64      guenther  800:        case SYS___thrsleep:
                    801:                pn(NULL);
                    802:                pn(clockname);
                    803:                break;
                    804:        case SYS___thrsigdivert:
                    805:                pn(sigset);
                    806:                break;
1.61      matthew   807:        case SYS_faccessat:
                    808:                pn(atfd);
                    809:                pn(NULL);
                    810:                pn(accessmodename);
                    811:                pn(atflagsname);
                    812:                break;
                    813:        case SYS_fchmodat:
                    814:                pn(atfd);
                    815:                pn(NULL);
                    816:                pn(modename);
                    817:                pn(atflagsname);
                    818:                break;
                    819:        case SYS_fchownat:
                    820:                pn(atfd);
                    821:                pn(NULL);
                    822:                pn(NULL);
                    823:                pn(NULL);
                    824:                pn(atflagsname);
                    825:                break;
                    826:        case SYS_fstatat:
                    827:                pn(atfd);
                    828:                pn(NULL);
                    829:                pn(NULL);
                    830:                pn(atflagsname);
                    831:                break;
                    832:        case SYS_linkat:
                    833:                pn(atfd);
                    834:                pn(NULL);
                    835:                pn(atfd);
                    836:                pn(NULL);
                    837:                pn(atflagsname);
                    838:                break;
                    839:        case SYS_mkdirat:
                    840:        case SYS_mkfifoat:
                    841:        case SYS_mknodat:
                    842:                pn(atfd);
                    843:                pn(NULL);
                    844:                pn(modename);
                    845:                break;
                    846:        case SYS_openat: {
                    847:                int     flags;
                    848:                int     mode;
                    849:
                    850:                pn(atfd);
                    851:                pn(NULL);
                    852:                if (!fancy)
                    853:                        break;
                    854:                flags = ap[0];
                    855:                mode = ap[1];
                    856:                (void)putchar(',');
                    857:                flagsandmodename(flags, mode);
                    858:                ap += 2;
                    859:                narg -= 2;
                    860:                break;
                    861:        }
                    862:        case SYS_readlinkat:
                    863:                pn(atfd);
                    864:                break;
                    865:        case SYS_renameat:
                    866:                pn(atfd);
                    867:                pn(NULL);
                    868:                pn(atfd);
                    869:                break;
                    870:        case SYS_symlinkat:
                    871:                pn(NULL);
                    872:                pn(atfd);
                    873:                break;
                    874:        case SYS_unlinkat:
                    875:                pn(atfd);
                    876:                pn(NULL);
                    877:                pn(atflagsname);
                    878:                break;
                    879:        case SYS_utimensat:
                    880:                pn(atfd);
                    881:                pn(NULL);
                    882:                pn(NULL);
                    883:                pn(atflagsname);
                    884:                break;
1.77      guenther  885:        case SYS_pathconf:
                    886:        case SYS_fpathconf:
                    887:                pn(NULL);
                    888:                pn(pathconfname);
1.86      guenther  889:                break;
                    890:        case SYS_ktrace:
                    891:                pn(NULL);
1.88    ! guenther  892:                pn(ktraceopname);
1.86      guenther  893:                pn(ktracefacname);
                    894:                pn(pgid);
                    895:                break;
                    896:        case SYS_setitimer:
                    897:        case SYS_getitimer:
                    898:                pn(itimername);
1.77      guenther  899:                break;
1.52      otto      900:        }
                    901:
1.54      otto      902: nonnative:
1.52      otto      903:        while (narg) {
                    904:                if (sep)
                    905:                        putchar(sep);
                    906:                if (decimal)
                    907:                        (void)printf("%ld", (long)*ap);
                    908:                else
                    909:                        (void)printf("%#lx", (long)*ap);
                    910:                sep = ',';
                    911:                ap++;
                    912:                narg--;
1.1       deraadt   913:        }
1.27      mickey    914:        (void)printf(")\n");
1.62      otto      915: }
                    916:
                    917: static struct ctlname topname[] = CTL_NAMES;
                    918: static struct ctlname kernname[] = CTL_KERN_NAMES;
                    919: static struct ctlname vmname[] = CTL_VM_NAMES;
                    920: static struct ctlname fsname[] = CTL_FS_NAMES;
                    921: static struct ctlname netname[] = CTL_NET_NAMES;
                    922: static struct ctlname hwname[] = CTL_HW_NAMES;
                    923: static struct ctlname debugname[CTL_DEBUG_MAXID];
                    924: static struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES;
                    925: static struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES;
                    926: static struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES;
1.88    ! guenther  927: static struct ctlname kernprocname[] = {
1.64      guenther  928:        { NULL },
                    929:        { "all" },
                    930:        { "pid" },
                    931:        { "pgrp" },
                    932:        { "session" },
                    933:        { "tty" },
                    934:        { "uid" },
                    935:        { "ruid" },
1.77      guenther  936:        { "kthread" },
1.64      guenther  937: };
1.62      otto      938: static struct ctlname ttysname[] = CTL_KERN_TTY_NAMES;
                    939: static struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES;
                    940: static struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES;
                    941: static struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES;
                    942: static struct ctlname tcname[] = CTL_KERN_TIMECOUNTER_NAMES;
                    943: #ifdef CTL_MACHDEP_NAMES
                    944: static struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
                    945: #endif
                    946: static struct ctlname ddbname[] = CTL_DDB_NAMES;
                    947:
                    948: #ifndef nitems
                    949: #define nitems(_a)    (sizeof((_a)) / sizeof((_a)[0]))
                    950: #endif
                    951:
                    952: #define SETNAME(name) do { names = (name); limit = nitems(name); } while (0)
                    953:
                    954: static const char *
                    955: kresolvsysctl(int depth, int *top, int idx)
                    956: {
                    957:        struct ctlname *names;
                    958:        size_t          limit;
                    959:
                    960:        names = NULL;
                    961:
                    962:        switch (depth) {
                    963:        case 0:
                    964:                SETNAME(topname);
                    965:                break;
                    966:        case 1:
                    967:                switch (top[0]) {
                    968:                case CTL_KERN:
                    969:                        SETNAME(kernname);
                    970:                        break;
                    971:                case CTL_VM:
                    972:                        SETNAME(vmname);
                    973:                        break;
                    974:                case CTL_FS:
                    975:                        SETNAME(fsname);
                    976:                        break;
                    977:                case CTL_NET:
                    978:                        SETNAME(netname);
                    979:                        break;
                    980:                case CTL_DEBUG:
                    981:                        SETNAME(debugname);
                    982:                        break;
                    983:                case CTL_HW:
                    984:                        SETNAME(hwname);
                    985:                        break;
                    986: #ifdef CTL_MACHDEP_NAMES
                    987:                case CTL_MACHDEP:
                    988:                        SETNAME(machdepname);
                    989:                        break;
                    990: #endif
                    991:                case CTL_DDB:
                    992:                        SETNAME(ddbname);
                    993:                        break;
                    994:                }
                    995:                break;
                    996:        case 2:
                    997:                switch (top[0]) {
                    998:                case CTL_KERN:
                    999:                        switch (top[1]) {
                   1000:                        case KERN_MALLOCSTATS:
                   1001:                                SETNAME(kernmallocname);
                   1002:                                break;
                   1003:                        case KERN_FORKSTAT:
                   1004:                                SETNAME(forkstatname);
                   1005:                                break;
                   1006:                        case KERN_NCHSTATS:
                   1007:                                SETNAME(nchstatsname);
                   1008:                                break;
                   1009:                        case KERN_TTY:
                   1010:                                SETNAME(ttysname);
                   1011:                                break;
                   1012:                        case KERN_SEMINFO:
                   1013:                                SETNAME(semname);
                   1014:                                break;
                   1015:                        case KERN_SHMINFO:
                   1016:                                SETNAME(shmname);
                   1017:                                break;
                   1018:                        case KERN_WATCHDOG:
                   1019:                                SETNAME(watchdogname);
                   1020:                                break;
1.64      guenther 1021:                        case KERN_PROC:
                   1022:                                idx++;  /* zero is valid at this level */
                   1023:                                SETNAME(kernprocname);
                   1024:                                break;
1.62      otto     1025:                        case KERN_TIMECOUNTER:
                   1026:                                SETNAME(tcname);
                   1027:                                break;
                   1028:                        }
                   1029:                }
                   1030:                break;
                   1031:        }
                   1032:        if (names != NULL && idx > 0 && idx < limit)
                   1033:                return (names[idx].ctl_name);
                   1034:        return (NULL);
1.1       deraadt  1035: }
                   1036:
1.12      espie    1037: static void
1.17      deraadt  1038: ktrsysret(struct ktr_sysret *ktr)
1.1       deraadt  1039: {
1.50      deraadt  1040:        register_t ret = ktr->ktr_retval;
1.12      espie    1041:        int error = ktr->ktr_error;
                   1042:        int code = ktr->ktr_code;
1.1       deraadt  1043:
                   1044:        if (code >= current->nsysnames || code < 0)
                   1045:                (void)printf("[%d] ", code);
1.59      otto     1046:        else {
1.1       deraadt  1047:                (void)printf("%s ", current->sysnames[code]);
1.59      otto     1048:                if (ret > 0 && (strcmp(current->sysnames[code], "fork") == 0 ||
                   1049:                    strcmp(current->sysnames[code], "vfork") == 0 ||
1.64      guenther 1050:                    strcmp(current->sysnames[code], "__tfork") == 0 ||
1.59      otto     1051:                    strcmp(current->sysnames[code], "clone") == 0))
                   1052:                        mappidtoemul(ret, current);
                   1053:        }
1.1       deraadt  1054:
                   1055:        if (error == 0) {
                   1056:                if (fancy) {
1.64      guenther 1057:                        switch (current == &emulations[0] ? code : -1) {
                   1058:                        case SYS_sigprocmask:
1.73      guenther 1059:                        case SYS_sigpending:
1.64      guenther 1060:                                sigset(ret);
                   1061:                                break;
                   1062:                        case SYS___thrsigdivert:
                   1063:                                signame(ret);
                   1064:                                break;
                   1065:                        case -1:        /* non-default emulation */
                   1066:                        default:
                   1067:                                (void)printf("%ld", (long)ret);
                   1068:                                if (ret < 0 || ret > 9)
                   1069:                                        (void)printf("/%#lx", (long)ret);
                   1070:                        }
1.1       deraadt  1071:                } else {
                   1072:                        if (decimal)
1.50      deraadt  1073:                                (void)printf("%ld", (long)ret);
1.1       deraadt  1074:                        else
1.50      deraadt  1075:                                (void)printf("%#lx", (long)ret);
1.1       deraadt  1076:                }
                   1077:        } else if (error == ERESTART)
                   1078:                (void)printf("RESTART");
                   1079:        else if (error == EJUSTRETURN)
                   1080:                (void)printf("JUSTRETURN");
                   1081:        else {
                   1082:                (void)printf("-1 errno %d", ktr->ktr_error);
                   1083:                if (fancy)
                   1084:                        (void)printf(" %s", strerror(ktr->ktr_error));
                   1085:        }
                   1086:        (void)putchar('\n');
                   1087: }
                   1088:
1.12      espie    1089: static void
1.37      tedu     1090: ktrnamei(const char *cp, size_t len)
1.1       deraadt  1091: {
1.37      tedu     1092:        (void)printf("\"%.*s\"\n", (int)len, cp);
1.1       deraadt  1093: }
                   1094:
1.12      espie    1095: static void
1.37      tedu     1096: ktremul(char *cp, size_t len)
1.1       deraadt  1097: {
                   1098:        char name[1024];
                   1099:
                   1100:        if (len >= sizeof(name))
                   1101:                errx(1, "Emulation name too long");
                   1102:
                   1103:        strncpy(name, cp, len);
                   1104:        name[len] = '\0';
                   1105:        (void)printf("\"%s\"\n", name);
                   1106:
                   1107:        setemul(name);
                   1108: }
                   1109:
1.12      espie    1110: static void
1.81      miod     1111: showbuf(unsigned char *dp, size_t datalen)
1.1       deraadt  1112: {
1.37      tedu     1113:        int i, j;
1.81      miod     1114:        static int screenwidth;
1.31      tedu     1115:        int col = 0, width, bpl;
1.32      tedu     1116:        unsigned char visbuf[5], *cp, c;
1.1       deraadt  1117:
                   1118:        if (screenwidth == 0) {
                   1119:                struct winsize ws;
                   1120:
                   1121:                if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
                   1122:                    ws.ws_col > 8)
                   1123:                        screenwidth = ws.ws_col;
                   1124:                else
                   1125:                        screenwidth = 80;
                   1126:        }
1.31      tedu     1127:        if (iohex == 1) {
                   1128:                putchar('\t');
                   1129:                col = 8;
                   1130:                for (i = 0; i < datalen; i++) {
1.35      tedu     1131:                        printf("%02x", dp[i]);
1.31      tedu     1132:                        col += 3;
                   1133:                        if (i < datalen - 1) {
                   1134:                                if (col + 3 > screenwidth) {
                   1135:                                        printf("\n\t");
                   1136:                                        col = 8;
                   1137:                                } else
                   1138:                                        putchar(' ');
                   1139:                        }
                   1140:                }
                   1141:                putchar('\n');
                   1142:                return;
                   1143:        }
                   1144:        if (iohex == 2) {
                   1145:                bpl = (screenwidth - 13)/4;
                   1146:                if (bpl <= 0)
                   1147:                        bpl = 1;
                   1148:                for (i = 0; i < datalen; i += bpl) {
                   1149:                        printf("   %04x:  ", i);
                   1150:                        for (j = 0; j < bpl; j++) {
                   1151:                                if (i+j >= datalen)
                   1152:                                        printf("   ");
                   1153:                                else
1.35      tedu     1154:                                        printf("%02x ", dp[i+j]);
1.31      tedu     1155:                        }
                   1156:                        putchar(' ');
                   1157:                        for (j = 0; j < bpl; j++) {
                   1158:                                if (i+j >= datalen)
                   1159:                                        break;
                   1160:                                c = dp[i+j];
                   1161:                                if (!isprint(c))
                   1162:                                        c = '.';
                   1163:                                putchar(c);
                   1164:                        }
                   1165:                        putchar('\n');
                   1166:                }
                   1167:                return;
                   1168:        }
1.1       deraadt  1169:        (void)printf("       \"");
                   1170:        col = 8;
                   1171:        for (; datalen > 0; datalen--, dp++) {
1.31      tedu     1172:                (void)vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1.1       deraadt  1173:                cp = visbuf;
1.17      deraadt  1174:
1.1       deraadt  1175:                /*
                   1176:                 * Keep track of printables and
                   1177:                 * space chars (like fold(1)).
                   1178:                 */
                   1179:                if (col == 0) {
                   1180:                        (void)putchar('\t');
                   1181:                        col = 8;
                   1182:                }
1.17      deraadt  1183:                switch (*cp) {
1.1       deraadt  1184:                case '\n':
                   1185:                        col = 0;
                   1186:                        (void)putchar('\n');
                   1187:                        continue;
                   1188:                case '\t':
                   1189:                        width = 8 - (col&07);
                   1190:                        break;
                   1191:                default:
                   1192:                        width = strlen(cp);
                   1193:                }
                   1194:                if (col + width > (screenwidth-2)) {
                   1195:                        (void)printf("\\\n\t");
                   1196:                        col = 8;
                   1197:                }
                   1198:                col += width;
                   1199:                do {
                   1200:                        (void)putchar(*cp++);
                   1201:                } while (*cp);
                   1202:        }
                   1203:        if (col == 0)
                   1204:                (void)printf("       ");
                   1205:        (void)printf("\"\n");
                   1206: }
                   1207:
1.12      espie    1208: static void
1.81      miod     1209: ktrgenio(struct ktr_genio *ktr, size_t len)
                   1210: {
                   1211:        unsigned char *dp = (unsigned char *)ktr + sizeof(struct ktr_genio);
                   1212:        size_t datalen = len - sizeof(struct ktr_genio);
                   1213:
                   1214:        printf("fd %d %s %zu bytes\n", ktr->ktr_fd,
                   1215:                ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
                   1216:        if (maxdata == 0)
                   1217:                return;
                   1218:        if (datalen > maxdata)
                   1219:                datalen = maxdata;
                   1220:        if (iohex && !datalen)
                   1221:                return;
                   1222:        showbuf(dp, datalen);
                   1223: }
                   1224:
                   1225: static void
1.17      deraadt  1226: ktrpsig(struct ktr_psig *psig)
1.1       deraadt  1227: {
                   1228:        (void)printf("SIG%s ", sys_signame[psig->signo]);
                   1229:        if (psig->action == SIG_DFL)
1.63      deraadt  1230:                (void)printf("SIG_DFL");
1.64      guenther 1231:        else {
                   1232:                (void)printf("caught handler=0x%lx mask=",
                   1233:                    (u_long)psig->action);
                   1234:                sigset(psig->mask);
                   1235:        }
1.63      deraadt  1236:        if (psig->code) {
                   1237:                printf(" code ");
                   1238:                if (fancy) {
                   1239:                        switch (psig->signo) {
                   1240:                        case SIGILL:
                   1241:                                sigill_name(psig->code);
                   1242:                                break;
                   1243:                        case SIGTRAP:
                   1244:                                sigtrap_name(psig->code);
                   1245:                                break;
                   1246:                        case SIGEMT:
                   1247:                                sigemt_name(psig->code);
                   1248:                                break;
                   1249:                        case SIGFPE:
                   1250:                                sigfpe_name(psig->code);
                   1251:                                break;
                   1252:                        case SIGBUS:
                   1253:                                sigbus_name(psig->code);
                   1254:                                break;
                   1255:                        case SIGSEGV:
                   1256:                                sigsegv_name(psig->code);
                   1257:                                break;
                   1258:                        case SIGCHLD:
                   1259:                                sigchld_name(psig->code);
                   1260:                                break;
                   1261:                        }
                   1262:                }
                   1263:                printf("<%d>", psig->code);
                   1264:        }
                   1265:
1.14      deraadt  1266:        switch (psig->signo) {
                   1267:        case SIGSEGV:
                   1268:        case SIGILL:
                   1269:        case SIGBUS:
                   1270:        case SIGFPE:
                   1271:                printf(" addr=%p trapno=%d", psig->si.si_addr,
                   1272:                    psig->si.si_trapno);
                   1273:                break;
                   1274:        default:
                   1275:                break;
                   1276:        }
                   1277:        printf("\n");
1.1       deraadt  1278: }
                   1279:
1.12      espie    1280: static void
1.17      deraadt  1281: ktrcsw(struct ktr_csw *cs)
1.1       deraadt  1282: {
                   1283:        (void)printf("%s %s\n", cs->out ? "stop" : "resume",
                   1284:            cs->user ? "user" : "kernel");
                   1285: }
                   1286:
1.64      guenther 1287: static void
1.81      miod     1288: ktruser(struct ktr_user *usr, size_t len)
                   1289: {
                   1290:        len -= sizeof(struct ktr_user);
                   1291:        printf("%.*s:", KTR_USER_MAXIDLEN, usr->ktr_id);
                   1292:        printf(" %zu bytes\n", len);
                   1293:        showbuf((unsigned char *)(usr + 1), len);
                   1294: }
                   1295:
                   1296: static void
1.17      deraadt  1297: usage(void)
1.1       deraadt  1298: {
                   1299:
1.19      mickey   1300:        extern char *__progname;
                   1301:        fprintf(stderr, "usage: %s "
1.68      jmc      1302:            "[-dHlnRrTXx] [-e emulation] [-f file] [-m maxdata] [-p pid]\n"
1.81      miod     1303:            "%*s[-t [ceinstuw]]\n",
1.51      otto     1304:            __progname, (int)(sizeof("usage: ") + strlen(__progname)), "");
1.1       deraadt  1305:        exit(1);
                   1306: }
                   1307:
1.12      espie    1308: static void
1.17      deraadt  1309: setemul(const char *name)
1.1       deraadt  1310: {
                   1311:        int i;
1.17      deraadt  1312:
1.1       deraadt  1313:        for (i = 0; emulations[i].name != NULL; i++)
                   1314:                if (strcmp(emulations[i].name, name) == 0) {
                   1315:                        current = &emulations[i];
                   1316:                        return;
                   1317:                }
                   1318:        warnx("Emulation `%s' unknown", name);
1.61      matthew  1319: }
                   1320:
                   1321: static void
                   1322: atfd(int fd)
                   1323: {
                   1324:        if (fd == AT_FDCWD)
                   1325:                (void)printf("AT_FDCWD");
                   1326:        else if (decimal)
                   1327:                (void)printf("%d", fd);
                   1328:        else
                   1329:                (void)printf("%#x", fd);
1.77      guenther 1330: }
                   1331:
                   1332: static void
                   1333: polltimeout(int timeout)
                   1334: {
                   1335:        if (timeout == INFTIM)
                   1336:                (void)printf("INFTIM");
                   1337:        else if (decimal)
                   1338:                (void)printf("%d", timeout);
                   1339:        else
                   1340:                (void)printf("%#x", timeout);
1.82      guenther 1341: }
                   1342:
                   1343: static void
                   1344: pgid(int pid)
                   1345: {
                   1346:        (void)printf("%d", pid);
                   1347: }
                   1348:
                   1349: static void
                   1350: wait4pid(int pid)
                   1351: {
                   1352:        if (pid == WAIT_ANY)
                   1353:                (void)printf("WAIT_ANY");
                   1354:        else if (pid == WAIT_MYPGRP)
                   1355:                (void)printf("WAIT_MYPGRP");
                   1356:        else
                   1357:                pgid(pid);
1.1       deraadt  1358: }
1.83      guenther 1359:
                   1360: static void
                   1361: signame(int sig)
                   1362: {
                   1363:        if (sig > 0 && sig < NSIG)
                   1364:                (void)printf("SIG%s", sys_signame[sig]);
                   1365:        else
                   1366:                (void)printf("SIG %d", sig);
                   1367: }
                   1368:
1.84      guenther 1369: void
1.83      guenther 1370: sigset(int ss)
                   1371: {
                   1372:        int     or = 0;
                   1373:        int     cnt = 0;
                   1374:        int     i;
                   1375:
                   1376:        for (i = 1; i < NSIG; i++)
                   1377:                if (sigismember(&ss, i))
                   1378:                        cnt++;
                   1379:        if (cnt > (NSIG-1)/2) {
                   1380:                ss = ~ss;
                   1381:                putchar('~');
                   1382:        }
                   1383:
                   1384:        if (ss == 0) {
                   1385:                (void)printf("0<>");
                   1386:                return;
                   1387:        }
                   1388:
                   1389:        printf("%#x<", ss);
                   1390:        for (i = 1; i < NSIG; i++)
                   1391:                if (sigismember(&ss, i)) {
                   1392:                        if (or) putchar('|'); else or=1;
                   1393:                        signame(i);
                   1394:                }
                   1395:        printf(">");
                   1396: }
                   1397:
                   1398: static void
                   1399: semctlname(int cmd)
                   1400: {
                   1401:        switch (cmd) {
                   1402:        case GETNCNT:
                   1403:                (void)printf("GETNCNT");
                   1404:                break;
                   1405:        case GETPID:
                   1406:                (void)printf("GETPID");
                   1407:                break;
                   1408:        case GETVAL:
                   1409:                (void)printf("GETVAL");
                   1410:                break;
                   1411:        case GETALL:
                   1412:                (void)printf("GETALL");
                   1413:                break;
                   1414:        case GETZCNT:
                   1415:                (void)printf("GETZCNT");
                   1416:                break;
                   1417:        case SETVAL:
                   1418:                (void)printf("SETVAL");
                   1419:                break;
                   1420:        case SETALL:
                   1421:                (void)printf("SETALL");
                   1422:                break;
                   1423:        case IPC_RMID:
                   1424:                (void)printf("IPC_RMID");
                   1425:                break;
                   1426:        case IPC_SET:
                   1427:                (void)printf("IPC_SET");
                   1428:                break;
                   1429:        case IPC_STAT:
                   1430:                (void)printf("IPC_STAT");
                   1431:                break;
                   1432:        default: /* Should not reach */
                   1433:                (void)printf("<invalid=%ld>", (long)cmd);
                   1434:        }
                   1435: }
                   1436:
                   1437: static void
1.88    ! guenther 1438: shmctlname(int cmd)
        !          1439: {
1.83      guenther 1440:        switch (cmd) {
                   1441:        case IPC_RMID:
                   1442:                (void)printf("IPC_RMID");
                   1443:                break;
                   1444:        case IPC_SET:
                   1445:                (void)printf("IPC_SET");
                   1446:                break;
                   1447:        case IPC_STAT:
                   1448:                (void)printf("IPC_STAT");
                   1449:                break;
                   1450:        default: /* Should not reach */
                   1451:                (void)printf("<invalid=%ld>", (long)cmd);
                   1452:        }
                   1453: }
                   1454:
                   1455:
                   1456: static void
1.88    ! guenther 1457: semgetname(int flag)
        !          1458: {
1.83      guenther 1459:        int     or = 0;
                   1460:        if_print_or(flag, IPC_CREAT, or);
                   1461:        if_print_or(flag, IPC_EXCL, or);
                   1462:        if_print_or(flag, SEM_R, or);
                   1463:        if_print_or(flag, SEM_A, or);
                   1464:        if_print_or(flag, (SEM_R>>3), or);
                   1465:        if_print_or(flag, (SEM_A>>3), or);
                   1466:        if_print_or(flag, (SEM_R>>6), or);
                   1467:        if_print_or(flag, (SEM_A>>6), or);
                   1468: }
                   1469:
                   1470:
                   1471: /*
                   1472:  * Only used by SYS_open. Unless O_CREAT is set in flags, the
                   1473:  * mode argument is unused (and often bogus and misleading).
                   1474:  */
                   1475: static void
1.88    ! guenther 1476: flagsandmodename(int flags, int mode)
        !          1477: {
1.83      guenther 1478:        flagsname (flags);
                   1479:        if ((flags & O_CREAT) == O_CREAT) {
                   1480:                (void)putchar(',');
                   1481:                modename (mode);
                   1482:        } else if (!fancy) {
                   1483:                (void)putchar(',');
                   1484:                if (decimal) {
                   1485:                        (void)printf("<unused>%ld", (long)mode);
                   1486:                } else {
                   1487:                        (void)printf("<unused>%#lx", (long)mode);
                   1488:                }
                   1489:        }
                   1490: }
                   1491:
                   1492: static void
                   1493: clockname(int clockid)
                   1494: {
                   1495:        clocktypename(__CLOCK_TYPE(clockid));
                   1496:        if (__CLOCK_PTID(clockid) != 0)
                   1497:                printf("(%d)", __CLOCK_PTID(clockid));
                   1498: }
                   1499:
                   1500: /*
                   1501:  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
                   1502:  * referring to a line in /etc/protocols . It might be appropriate
                   1503:  * to use getprotoent(3) here.
                   1504:  */
                   1505: static void
                   1506: sockoptlevelname(int level)
                   1507: {
                   1508:        if (level == SOL_SOCKET) {
                   1509:                (void)printf("SOL_SOCKET");
                   1510:        } else {
                   1511:                if (decimal) {
                   1512:                        (void)printf("%ld", (long)level);
                   1513:                } else {
                   1514:                        (void)printf("%#lx", (long)level);
                   1515:                }
                   1516:        }
                   1517: }
                   1518:
1.88    ! guenther 1519: static void
        !          1520: ktraceopname(int ops)
        !          1521: {
        !          1522:        int invalid = 0;
        !          1523:
        !          1524:        printf("%#x<", ops);
        !          1525:        switch (KTROP(ops)) {
        !          1526:        case KTROP_SET:
        !          1527:                printf("KTROP_SET");
        !          1528:                break;
        !          1529:        case KTROP_CLEAR:
        !          1530:                printf("KTROP_CLEAR");
        !          1531:                break;
        !          1532:        case KTROP_CLEARFILE:
        !          1533:                printf("KTROP_CLEARFILE");
        !          1534:                break;
        !          1535:        default:
        !          1536:                printf("KTROP(%d)", KTROP(ops));
        !          1537:                invalid = 1;
        !          1538:                break;
        !          1539:        }
        !          1540:        if (ops & KTRFLAG_DESCEND) printf ("|%s", "KTRFLAG_DESCEND");
        !          1541:        printf(">");
        !          1542:        if (invalid || (ops & ~(KTROP((unsigned)-1) | KTRFLAG_DESCEND)))
        !          1543:                (void)printf("<invalid>%ld", (long)ops);
        !          1544: }