[BACK]Return to ktrstruct.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / kdump

Annotation of src/usr.bin/kdump/ktrstruct.c, Revision 1.8

1.8     ! guenther    1: /*     $OpenBSD: ktrstruct.c,v 1.7 2014/11/20 18:44:10 krw Exp $       */
1.1       guenther    2:
                      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.
                     15:  * 3. Neither the name of the University nor the names of its contributors
                     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/types.h>
                     33: #include <sys/resource.h>
                     34: #include <sys/socket.h>
                     35: #include <sys/stat.h>
                     36: #include <sys/time.h>
                     37: #include <sys/un.h>
1.6       guenther   38: #include <ufs/ufs/quota.h>
1.1       guenther   39: #include <netinet/in.h>
                     40: #include <arpa/inet.h>
                     41:
                     42: #include <ctype.h>
                     43: #include <err.h>
                     44: #include <limits.h>
                     45: #include <poll.h>
                     46: #include <signal.h>
                     47: #include <stdio.h>
                     48: #include <stdlib.h>
                     49: #include <stdint.h>
                     50: #include <string.h>
                     51: #include <grp.h>
                     52: #include <pwd.h>
                     53: #include <unistd.h>
                     54:
                     55: #include "kdump.h"
                     56: #include "kdump_subr.h"
                     57:
                     58: #define TIME_FORMAT    "%b %e %T %Y"
                     59:
                     60: static void
                     61: ktrsockaddr(struct sockaddr *sa)
                     62: {
                     63: /*
                     64:  TODO: Support additional address families
1.4       guenther   65:        #include <netmpls/mpls.h>
                     66:        struct sockaddr_mpls    *mpls;
1.1       guenther   67: */
                     68:        char addr[64];
                     69:
                     70:        /*
                     71:         * note: ktrstruct() has already verified that sa points to a
                     72:         * buffer at least sizeof(struct sockaddr) bytes long and exactly
                     73:         * sa->sa_len bytes long.
                     74:         */
                     75:        printf("struct sockaddr { ");
                     76:        sockfamilyname(sa->sa_family);
                     77:        printf(", ");
                     78:
                     79: #define check_sockaddr_len(n)                                  \
                     80:        if (sa_##n->s##n##_len < sizeof(struct sockaddr_##n)) { \
                     81:                printf("invalid");                              \
                     82:                break;                                          \
                     83:        }
                     84:
                     85:        switch(sa->sa_family) {
                     86:        case AF_INET: {
                     87:                struct sockaddr_in      *sa_in;
                     88:
                     89:                sa_in = (struct sockaddr_in *)sa;
                     90:                check_sockaddr_len(in);
                     91:                inet_ntop(AF_INET, &sa_in->sin_addr, addr, sizeof addr);
                     92:                printf("%s:%u", addr, ntohs(sa_in->sin_port));
                     93:                break;
                     94:        }
                     95:        case AF_INET6: {
                     96:                struct sockaddr_in6     *sa_in6;
                     97:
                     98:                sa_in6 = (struct sockaddr_in6 *)sa;
                     99:                check_sockaddr_len(in6);
                    100:                inet_ntop(AF_INET6, &sa_in6->sin6_addr, addr, sizeof addr);
                    101:                printf("[%s]:%u", addr, htons(sa_in6->sin6_port));
                    102:                break;
                    103:        }
                    104: #ifdef IPX
                    105:        case AF_IPX: {
                    106:                struct sockaddr_ipx     *sa_ipx;
                    107:
                    108:                sa_ipx = (struct sockaddr_ipx *)sa;
                    109:                check_sockaddr_len(ipx);
                    110:                /* XXX wish we had ipx_ntop */
                    111:                printf("%s", ipx_ntoa(sa_ipx->sipx_addr));
                    112:                break;
                    113:        }
                    114: #endif
                    115:        case AF_UNIX: {
                    116:                struct sockaddr_un *sa_un;
                    117:
                    118:                sa_un = (struct sockaddr_un *)sa;
                    119:                if (sa_un->sun_len <= sizeof(sa_un->sun_len) +
                    120:                    sizeof(sa_un->sun_family)) {
                    121:                        printf("invalid");
                    122:                        break;
                    123:                }
                    124:                printf("\"%.*s\"", (int)(sa_un->sun_len -
                    125:                    sizeof(sa_un->sun_len) - sizeof(sa_un->sun_family)),
                    126:                    sa_un->sun_path);
                    127:                break;
                    128:        }
                    129:        default:
                    130:                printf("unknown address family");
                    131:        }
                    132:        printf(" }\n");
                    133: }
                    134:
                    135: static void
1.8     ! guenther  136: print_time(time_t t, int relative, int have_subsec)
1.1       guenther  137: {
                    138:        char timestr[PATH_MAX + 4];
                    139:        struct tm *tm;
                    140:
1.8     ! guenther  141:        if (t < 0 && have_subsec) {
        !           142:                /* negative times with non-zero subsecs require care */
        !           143:                printf("-%jd", -(intmax_t)(t + 1));
        !           144:        } else
1.1       guenther  145:                printf("%jd", (intmax_t)t);
1.8     ! guenther  146:
        !           147:        if (!relative) {
1.1       guenther  148:                tm = localtime(&t);
                    149:                (void)strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1.8     ! guenther  150:                printf("<\"%s\">", timestr);
1.1       guenther  151:        }
                    152: }
                    153:
                    154: static void
                    155: print_timespec(const struct timespec *tsp, int relative)
                    156: {
1.2       guenther  157:        if (tsp->tv_nsec == UTIME_NOW)
                    158:                printf("UTIME_NOW");
                    159:        else if (tsp->tv_nsec == UTIME_OMIT)
                    160:                printf("UTIME_OMIT");
1.8     ! guenther  161:        else {
        !           162:                print_time(tsp->tv_sec, relative, tsp->tv_nsec);
1.2       guenther  163:                if (tsp->tv_nsec != 0)
1.8     ! guenther  164:                        printf(".%09ld", tsp->tv_sec >= 0 ? tsp->tv_nsec :
        !           165:                            1000000000 - tsp->tv_nsec);
        !           166:        }
        !           167: }
        !           168:
        !           169: void
        !           170: uidname(int uid)
        !           171: {
        !           172:        const char *name;
        !           173:
        !           174:        if (uid == -1)
        !           175:                printf("-1");
        !           176:        else {
        !           177:                printf("%u<", (unsigned)uid);
        !           178:                if (uid > UID_MAX || (name = user_from_uid(uid, 1)) == NULL)
        !           179:                        printf("unknown>");
        !           180:                else
        !           181:                        printf("\"%s\">", name);
        !           182:        }
        !           183: }
        !           184:
        !           185: void
        !           186: gidname(int gid)
        !           187: {
        !           188:        const char *name;
        !           189:
        !           190:        if (gid == -1)
        !           191:                printf("-1");
        !           192:        else {
        !           193:                printf("%u<", (unsigned)gid);
        !           194:                if (gid > GID_MAX || (name = group_from_gid(gid, 1)) == NULL)
        !           195:                        printf("unknown>");
        !           196:                else
        !           197:                        printf("\"%s\">", name);
1.2       guenther  198:        }
1.1       guenther  199: }
                    200:
                    201: static void
                    202: ktrstat(const struct stat *statp)
                    203: {
                    204:        char mode[12];
                    205:
                    206:        /*
                    207:         * note: ktrstruct() has already verified that statp points to a
                    208:         * buffer exactly sizeof(struct stat) bytes long.
                    209:         */
                    210:        printf("struct stat { ");
                    211:        strmode(statp->st_mode, mode);
1.8     ! guenther  212:        printf("dev=%d, ino=%llu, mode=%s, nlink=%u, uid=",
1.1       guenther  213:            statp->st_dev, (unsigned long long)statp->st_ino,
                    214:            mode, statp->st_nlink);
1.8     ! guenther  215:        uidname(statp->st_uid);
        !           216:        printf(", gid=");
        !           217:        gidname(statp->st_gid);
        !           218:        printf(", rdev=%d, ", statp->st_rdev);
1.1       guenther  219:        printf("atime=");
                    220:        print_timespec(&statp->st_atim, 0);
                    221:        printf(", mtime=");
                    222:        print_timespec(&statp->st_mtim, 0);
                    223:        printf(", ctime=");
                    224:        print_timespec(&statp->st_ctim, 0);
1.7       krw       225:        printf(", size=%lld, blocks=%lld, blksize=%d, flags=0x%x, gen=0x%x",
1.1       guenther  226:            statp->st_size, statp->st_blocks, statp->st_blksize,
                    227:            statp->st_flags, statp->st_gen);
                    228:        printf(" }\n");
                    229: }
                    230:
                    231: static void
                    232: ktrtimespec(const struct timespec *tsp, int relative)
                    233: {
                    234:        printf("struct timespec { ");
                    235:        print_timespec(tsp, relative);
                    236:        printf(" }\n");
                    237: }
                    238:
                    239: static void
1.3       guenther  240: print_timeval(const struct timeval *tvp, int relative)
1.1       guenther  241: {
1.8     ! guenther  242:        print_time(tvp->tv_sec, relative, tvp->tv_usec);
        !           243:        if (tvp->tv_usec != 0)
        !           244:                printf(".%06ld", tvp->tv_sec >= 0 ? tvp->tv_usec :
1.4       guenther  245:                    1000000 - tvp->tv_usec);
1.3       guenther  246: }
                    247:
                    248: static void
                    249: ktrtimeval(const struct timeval *tvp, int relative)
                    250: {
                    251:        printf("struct timeval { ");
                    252:        print_timeval(tvp, relative);
1.1       guenther  253:        printf(" }\n");
                    254: }
                    255:
                    256: static void
                    257: ktrsigaction(const struct sigaction *sa)
                    258: {
                    259:        /*
                    260:         * note: ktrstruct() has already verified that sa points to a
                    261:         * buffer exactly sizeof(struct sigaction) bytes long.
                    262:         */
                    263:        printf("struct sigaction { ");
                    264:        if (sa->sa_handler == SIG_DFL)
                    265:                printf("handler=SIG_DFL");
                    266:        else if (sa->sa_handler == SIG_IGN)
                    267:                printf("handler=SIG_IGN");
                    268:        else if (sa->sa_flags & SA_SIGINFO)
                    269:                printf("sigaction=%p", (void *)sa->sa_sigaction);
                    270:        else
                    271:                printf("handler=%p", (void *)sa->sa_handler);
                    272:        printf(", mask=");
                    273:        sigset(sa->sa_mask);
                    274:        printf(", flags=");
                    275:        sigactionflagname(sa->sa_flags);
                    276:        printf(" }\n");
                    277: }
                    278:
                    279: static void
                    280: print_rlim(rlim_t lim)
                    281: {
                    282:        if (lim == RLIM_INFINITY)
                    283:                printf("infinite");
                    284:        else
                    285:                printf("%llu", (unsigned long long)lim);
                    286: }
                    287:
                    288: static void
                    289: ktrrlimit(const struct rlimit *limp)
                    290: {
                    291:        printf("struct rlimit { ");
                    292:        printf("cur=");
                    293:        print_rlim(limp->rlim_cur);
                    294:        printf(", max=");
                    295:        print_rlim(limp->rlim_max);
                    296:        printf(" }\n");
                    297: }
                    298:
                    299: static void
                    300: ktrtfork(const struct __tfork *tf)
                    301: {
                    302:        printf("struct __tfork { tcb=%p, tid=%p, stack=%p }\n",
                    303:            tf->tf_tcb, (void *)tf->tf_tid, tf->tf_stack);
                    304: }
                    305:
                    306: static void
                    307: ktrfdset(const struct fd_set *fds, int len)
                    308: {
                    309:        int nfds, i, start = -1;
                    310:        char sep = ' ';
                    311:
                    312:        nfds = len * NBBY;
                    313:        printf("struct fd_set {");
                    314:        for (i = 0; i <= nfds; i++)
                    315:                if (i != nfds && FD_ISSET(i, fds)) {
                    316:                        if (start == -1)
                    317:                                start = i;
                    318:                } else if (start != -1) {
                    319:                        putchar(sep);
                    320:                        if (start == i - 1)
                    321:                                printf("%d", start);
                    322:                        else if (start == i - 2)
                    323:                                printf("%d,%d", start, i - 1);
                    324:                        else
                    325:                                printf("%d-%d", start, i - 1);
                    326:                        sep = ',';
                    327:                        start = -1;
                    328:                }
                    329:
                    330:        printf(" }\n");
                    331: }
                    332:
1.3       guenther  333: static void
                    334: ktrrusage(const struct rusage *rup)
                    335: {
                    336:        printf("struct rusage { utime=");
                    337:        print_timeval(&rup->ru_utime, 1);
                    338:        printf(", stime=");
                    339:        print_timeval(&rup->ru_stime, 1);
                    340:        printf(", maxrss=%ld, ixrss=%ld, idrss=%ld, isrss=%ld,"
                    341:            " minflt=%ld, majflt=%ld, nswap=%ld, inblock=%ld,"
                    342:            " oublock=%ld, msgsnd=%ld, msgrcv=%ld, nsignals=%ld,"
                    343:            " nvcsw=%ld, nivcsw=%ld }\n",
                    344:            rup->ru_maxrss, rup->ru_ixrss, rup->ru_idrss, rup->ru_isrss,
                    345:            rup->ru_minflt, rup->ru_majflt, rup->ru_nswap, rup->ru_inblock,
                    346:            rup->ru_oublock, rup->ru_msgsnd, rup->ru_msgrcv, rup->ru_nsignals,
                    347:            rup->ru_nvcsw, rup->ru_nivcsw);
                    348: }
                    349:
1.6       guenther  350: static void
                    351: ktrquota(const struct dqblk *quota)
                    352: {
                    353:        printf("struct dqblk { bhardlimit=%u, bsoftlimit=%u, curblocks=%u,"
                    354:            " ihardlimit=%u, isoftlimit=%u, curinodes=%u, btime=",
                    355:            quota->dqb_bhardlimit, quota->dqb_bsoftlimit,
                    356:            quota->dqb_curblocks, quota->dqb_ihardlimit,
                    357:            quota->dqb_isoftlimit, quota->dqb_curinodes);
1.8     ! guenther  358:        print_time(quota->dqb_btime, 0, 0);
1.6       guenther  359:        printf(", itime=");
1.8     ! guenther  360:        print_time(quota->dqb_itime, 0, 0);
1.6       guenther  361:        printf(" }\n");
                    362: }
                    363:
1.1       guenther  364: void
                    365: ktrstruct(char *buf, size_t buflen)
                    366: {
                    367:        char *name, *data;
                    368:        size_t namelen, datalen;
                    369:        int i;
                    370:
                    371:        for (name = buf, namelen = 0; namelen < buflen && name[namelen] != '\0';
                    372:             ++namelen)
                    373:                /* nothing */;
                    374:        if (namelen == buflen)
                    375:                goto invalid;
                    376:        if (name[namelen] != '\0')
                    377:                goto invalid;
                    378:        data = buf + namelen + 1;
                    379:        datalen = buflen - namelen - 1;
                    380:        if (datalen == 0)
                    381:                goto invalid;
                    382:        /* sanity check */
                    383:        for (i = 0; i < namelen; ++i)
                    384:                if (!isalpha((unsigned char)name[i]))
                    385:                        goto invalid;
                    386:        if (strcmp(name, "stat") == 0) {
                    387:                struct stat sb;
                    388:
                    389:                if (datalen != sizeof(struct stat))
                    390:                        goto invalid;
                    391:                memcpy(&sb, data, datalen);
                    392:                ktrstat(&sb);
                    393:        } else if (strcmp(name, "sockaddr") == 0) {
                    394:                struct sockaddr_storage ss;
                    395:
                    396:                if (datalen > sizeof(ss))
                    397:                        goto invalid;
                    398:                memcpy(&ss, data, datalen);
                    399:                if ((ss.ss_family != AF_UNIX &&
                    400:                    datalen < sizeof(struct sockaddr)) || datalen != ss.ss_len)
                    401:                        goto invalid;
                    402:                ktrsockaddr((struct sockaddr *)&ss);
                    403:        } else if (strcmp(name, "abstimespec") == 0 ||
                    404:            strcmp(name, "reltimespec") == 0) {
                    405:                struct timespec ts;
                    406:
                    407:                if (datalen != sizeof(ts))
                    408:                        goto invalid;
                    409:                memcpy(&ts, data, datalen);
                    410:                ktrtimespec(&ts, name[0] == 'r');
                    411:        } else if (strcmp(name, "abstimeval") == 0 ||
                    412:            strcmp(name, "reltimeval") == 0) {
                    413:                struct timeval tv;
                    414:
                    415:                if (datalen != sizeof(tv))
                    416:                        goto invalid;
                    417:                memcpy(&tv, data, datalen);
                    418:                ktrtimeval(&tv, name[0] == 'r');
                    419:        } else if (strcmp(name, "sigaction") == 0) {
                    420:                struct sigaction sa;
                    421:
                    422:                if (datalen != sizeof(sa))
                    423:                        goto invalid;
                    424:                memcpy(&sa, data, datalen);
                    425:                ktrsigaction(&sa);
                    426:        } else if (strcmp(name, "rlimit") == 0) {
                    427:                struct rlimit lim;
                    428:
                    429:                if (datalen != sizeof(lim))
                    430:                        goto invalid;
                    431:                memcpy(&lim, data, datalen);
                    432:                ktrrlimit(&lim);
1.3       guenther  433:        } else if (strcmp(name, "rusage") == 0) {
                    434:                struct rusage ru;
                    435:
                    436:                if (datalen != sizeof(ru))
                    437:                        goto invalid;
                    438:                memcpy(&ru, data, datalen);
                    439:                ktrrusage(&ru);
1.1       guenther  440:        } else if (strcmp(name, "tfork") == 0) {
                    441:                struct __tfork tf;
                    442:
                    443:                if (datalen != sizeof(tf))
                    444:                        goto invalid;
                    445:                memcpy(&tf, data, datalen);
                    446:                ktrtfork(&tf);
                    447:        } else if (strcmp(name, "fdset") == 0) {
                    448:                struct fd_set *fds;
                    449:                if ((fds = malloc(datalen)) == NULL)
                    450:                        err(1, "malloc");
                    451:                memcpy(fds, data, datalen);
                    452:                ktrfdset(fds, datalen);
                    453:                free(fds);
1.6       guenther  454:        } else if (strcmp(name, "quota") == 0) {
                    455:                struct dqblk quota;
                    456:
                    457:                if (datalen != sizeof(quota))
                    458:                        goto invalid;
                    459:                memcpy(&quota, data, datalen);
                    460:                ktrquota(&quota);
1.1       guenther  461:        } else {
                    462:                printf("unknown structure %s\n", name);
                    463:        }
                    464:        return;
                    465: invalid:
                    466:        printf("invalid record\n");
                    467: }