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

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