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

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