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

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