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

Annotation of src/usr.bin/ctfdump/ctfdump.c, Revision 1.15

1.15    ! mpi         1: /*     $OpenBSD: ctfdump.c,v 1.14 2017/10/27 09:35:22 mpi Exp $ */
1.2       jasper      2:
1.1       mpi         3: /*
                      4:  * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
                     21: #include <sys/mman.h>
                     22: #include <sys/ctf.h>
                     23:
1.12      mpi        24: #include <elf.h>
1.1       mpi        25: #include <err.h>
                     26: #include <fcntl.h>
                     27: #include <locale.h>
                     28: #include <stdio.h>
                     29: #include <stdint.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #ifdef ZLIB
                     35: #include <zlib.h>
                     36: #endif /* ZLIB */
                     37:
                     38: #ifndef nitems
                     39: #define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
                     40: #endif
                     41:
                     42: #define DUMP_OBJECT    (1 << 0)
                     43: #define DUMP_FUNCTION  (1 << 1)
                     44: #define DUMP_HEADER    (1 << 2)
                     45: #define DUMP_LABEL     (1 << 3)
                     46: #define DUMP_STRTAB    (1 << 4)
                     47: #define DUMP_STATISTIC (1 << 5)
                     48: #define DUMP_TYPE      (1 << 6)
                     49:
                     50: int             dump(const char *, uint8_t);
                     51: int             isctf(const char *, size_t);
                     52: __dead void     usage(void);
                     53:
                     54: int             ctf_dump(const char *, size_t, uint8_t);
1.14      mpi        55: void            ctf_dump_type(struct ctf_header *, const char *, off_t,
                     56:                     uint32_t, uint32_t *, uint32_t);
1.1       mpi        57: const char     *ctf_kind2name(uint16_t);
                     58: const char     *ctf_enc2name(uint16_t);
1.7       uwe        59: const char     *ctf_fpenc2name(uint16_t);
1.1       mpi        60: const char     *ctf_off2name(struct ctf_header *, const char *, off_t,
                     61:                     uint32_t);
                     62:
                     63: int             elf_dump(char *, size_t, uint8_t);
                     64: const char     *elf_idx2sym(size_t *, uint8_t);
                     65:
                     66: /* elf.c */
                     67: int             iself(const char *, size_t);
                     68: int             elf_getshstab(const char *, size_t, const char **, size_t *);
1.9       jsg        69: ssize_t                 elf_getsymtab(const char *, size_t filesize, const char *,
                     70:                     size_t, const Elf_Sym **, size_t *);
                     71: ssize_t                 elf_getsection(char *, size_t, const char *, const char *,
1.1       mpi        72:                     size_t, const char **, size_t *);
                     73:
                     74: char           *decompress(const char *, size_t, off_t);
                     75:
                     76: int
                     77: main(int argc, char *argv[])
                     78: {
                     79:        const char *filename;
                     80:        uint8_t flags = 0;
                     81:        int ch, error = 0;
1.3       jasper     82:
1.4       jasper     83:        setlocale(LC_ALL, "");
                     84:
1.3       jasper     85:        if (pledge("stdio rpath", NULL) == -1)
                     86:                err(1, "pledge");
1.1       mpi        87:
                     88:        while ((ch = getopt(argc, argv, "dfhlst")) != -1) {
                     89:                switch (ch) {
                     90:                case 'd':
                     91:                        flags |= DUMP_OBJECT;
                     92:                        break;
                     93:                case 'f':
                     94:                        flags |= DUMP_FUNCTION;
                     95:                        break;
                     96:                case 'h':
                     97:                        flags |= DUMP_HEADER;
                     98:                        break;
                     99:                case 'l':
                    100:                        flags |= DUMP_LABEL;
                    101:                        break;
                    102:                case 's':
                    103:                        flags |= DUMP_STRTAB;
                    104:                        break;
                    105:                case 't':
                    106:                        flags |= DUMP_TYPE;
                    107:                        break;
                    108:                default:
                    109:                        usage();
                    110:                }
                    111:        }
                    112:
                    113:        argc -= optind;
                    114:        argv += optind;
                    115:
                    116:        if (argc <= 0)
                    117:                usage();
                    118:
                    119:        /* Dump everything by default */
                    120:        if (flags == 0)
                    121:                flags = 0xff;
                    122:
                    123:        while ((filename = *argv++) != NULL)
                    124:                error |= dump(filename, flags);
                    125:
                    126:        return error;
                    127: }
                    128:
                    129: int
                    130: dump(const char *path, uint8_t flags)
                    131: {
                    132:        struct stat              st;
                    133:        int                      fd, error = 1;
                    134:        char                    *p;
                    135:
                    136:        fd = open(path, O_RDONLY);
                    137:        if (fd == -1) {
                    138:                warn("open");
                    139:                return 1;
                    140:        }
                    141:        if (fstat(fd, &st) == -1) {
                    142:                warn("fstat");
1.6       jsg       143:                close(fd);
1.1       mpi       144:                return 1;
                    145:        }
                    146:        if ((uintmax_t)st.st_size > SIZE_MAX) {
                    147:                warnx("file too big to fit memory");
1.6       jsg       148:                close(fd);
1.1       mpi       149:                return 1;
                    150:        }
                    151:
                    152:        p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
                    153:        if (p == MAP_FAILED)
                    154:                err(1, "mmap");
                    155:
                    156:        if (iself(p, st.st_size)) {
                    157:                error = elf_dump(p, st.st_size, flags);
                    158:        } else if (isctf(p, st.st_size)) {
                    159:                error = ctf_dump(p, st.st_size, flags);
                    160:        }
                    161:
                    162:        munmap(p, st.st_size);
                    163:        close(fd);
                    164:
                    165:        return error;
                    166: }
                    167:
                    168: const char             *strtab;
                    169: const Elf_Sym          *symtab;
                    170: size_t                  strtabsz, nsymb;
                    171:
                    172: const char *
                    173: elf_idx2sym(size_t *idx, uint8_t type)
                    174: {
                    175:        const Elf_Sym   *st;
                    176:        size_t           i;
                    177:
                    178:        for (i = *idx + 1; i < nsymb; i++) {
                    179:                st = &symtab[i];
                    180:
                    181:                if (ELF_ST_TYPE(st->st_info) != type)
                    182:                        continue;
                    183:
1.10      jsg       184:                if (st->st_name >= strtabsz)
                    185:                        break;
                    186:
1.1       mpi       187:                *idx = i;
                    188:                return strtab + st->st_name;
                    189:        }
                    190:
                    191:        return NULL;
                    192: }
                    193:
                    194: int
                    195: elf_dump(char *p, size_t filesize, uint8_t flags)
                    196: {
                    197:        Elf_Ehdr                *eh = (Elf_Ehdr *)p;
                    198:        Elf_Shdr                *sh;
                    199:        const char              *shstab;
                    200:        size_t                   i, shstabsz;
                    201:
                    202:        /* Find section header string table location and size. */
                    203:        if (elf_getshstab(p, filesize, &shstab, &shstabsz))
                    204:                return 1;
                    205:
                    206:        /* Find symbol table location and number of symbols. */
1.9       jsg       207:        if (elf_getsymtab(p, filesize, shstab, shstabsz, &symtab, &nsymb) == -1)
1.1       mpi       208:                warnx("symbol table not found");
                    209:
                    210:        /* Find string table location and size. */
1.9       jsg       211:        if (elf_getsection(p, filesize, ELF_STRTAB, shstab, shstabsz, &strtab,
1.1       mpi       212:            &strtabsz) == -1)
                    213:                warnx("string table not found");
                    214:
                    215:        /* Find CTF section and dump it. */
                    216:        for (i = 0; i < eh->e_shnum; i++) {
                    217:                sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
                    218:
                    219:                if ((sh->sh_link >= eh->e_shnum) ||
                    220:                    (sh->sh_name >= shstabsz))
                    221:                        continue;
                    222:
                    223:                if (strncmp(shstab + sh->sh_name, ELF_CTF, strlen(ELF_CTF)))
1.10      jsg       224:                        continue;
                    225:
                    226:                if ((sh->sh_offset + sh->sh_size) > filesize)
1.1       mpi       227:                        continue;
                    228:
                    229:                if (!isctf(p + sh->sh_offset, sh->sh_size))
                    230:                        break;
                    231:
                    232:                return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
                    233:        }
                    234:
                    235:        warnx("%s section not found", ELF_CTF);
                    236:        return 1;
                    237: }
                    238:
                    239: int
                    240: isctf(const char *p, size_t filesize)
                    241: {
                    242:        struct ctf_header       *cth = (struct ctf_header *)p;
                    243:        off_t                    dlen;
                    244:
                    245:        if (filesize < sizeof(struct ctf_header)) {
                    246:                warnx("file too small to be CTF");
                    247:                return 0;
                    248:        }
                    249:
                    250:        if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
                    251:                return 0;
                    252:
                    253:        dlen = cth->cth_stroff + cth->cth_strlen;
                    254:        if (dlen > (off_t)filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
                    255:                warnx("bogus file size");
                    256:                return 0;
                    257:        }
                    258:
                    259:        if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
                    260:            (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
                    261:                warnx("wrongly aligned offset");
                    262:                return 0;
                    263:        }
                    264:
                    265:        if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
                    266:            (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
                    267:                warnx("truncated file");
                    268:                return 0;
                    269:        }
                    270:
                    271:        if ((cth->cth_lbloff > cth->cth_objtoff) ||
                    272:            (cth->cth_objtoff > cth->cth_funcoff) ||
                    273:            (cth->cth_funcoff > cth->cth_typeoff) ||
                    274:            (cth->cth_typeoff > cth->cth_stroff)) {
                    275:                warnx("corrupted file");
                    276:                return 0;
                    277:        }
                    278:
                    279:        return 1;
                    280: }
                    281:
                    282: int
                    283: ctf_dump(const char *p, size_t size, uint8_t flags)
                    284: {
                    285:        struct ctf_header       *cth = (struct ctf_header *)p;
                    286:        off_t                    dlen = cth->cth_stroff + cth->cth_strlen;
                    287:        char                    *data;
                    288:
                    289:        if (cth->cth_flags & CTF_F_COMPRESS) {
                    290:                data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
                    291:                if (data == NULL)
                    292:                        return 1;
                    293:        } else {
                    294:                data = (char *)p + sizeof(*cth);
                    295:        }
                    296:
                    297:        if (flags & DUMP_HEADER) {
                    298:                printf("  cth_magic    = 0x%04x\n", cth->cth_magic);
1.15    ! mpi       299:                printf("  cth_version  = %u\n", cth->cth_version);
1.1       mpi       300:                printf("  cth_flags    = 0x%02x\n", cth->cth_flags);
                    301:                printf("  cth_parlabel = %s\n",
1.11      mpi       302:                    ctf_off2name(cth, data, dlen, cth->cth_parlabel));
1.1       mpi       303:                printf("  cth_parname  = %s\n",
                    304:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
1.15    ! mpi       305:                printf("  cth_lbloff   = %u\n", cth->cth_lbloff);
        !           306:                printf("  cth_objtoff  = %u\n", cth->cth_objtoff);
        !           307:                printf("  cth_funcoff  = %u\n", cth->cth_funcoff);
        !           308:                printf("  cth_typeoff  = %u\n", cth->cth_typeoff);
        !           309:                printf("  cth_stroff   = %u\n", cth->cth_stroff);
        !           310:                printf("  cth_strlen   = %u\n", cth->cth_strlen);
1.1       mpi       311:                printf("\n");
                    312:        }
                    313:
                    314:        if (flags & DUMP_LABEL) {
                    315:                uint32_t                 lbloff = cth->cth_lbloff;
                    316:                struct ctf_lblent       *ctl;
                    317:
                    318:                while (lbloff < cth->cth_objtoff) {
                    319:                        ctl = (struct ctf_lblent *)(data + lbloff);
                    320:
                    321:                        printf("  %5u %s\n", ctl->ctl_typeidx,
                    322:                            ctf_off2name(cth, data, dlen, ctl->ctl_label));
                    323:
                    324:                        lbloff += sizeof(*ctl);
                    325:                }
                    326:                printf("\n");
                    327:        }
                    328:
                    329:        if (flags & DUMP_OBJECT) {
                    330:                uint32_t                 objtoff = cth->cth_objtoff;
                    331:                size_t                   idx = 0, i = 0;
                    332:                uint16_t                *dsp;
                    333:                const char              *s;
                    334:                int                      l;
                    335:
                    336:                while (objtoff < cth->cth_funcoff) {
                    337:                        dsp = (uint16_t *)(data + objtoff);
                    338:
                    339:                        l = printf("  [%zu] %u", i++, *dsp);
                    340:                        if ((s = elf_idx2sym(&idx, STT_OBJECT)) != NULL)
                    341:                                printf("%*s %s (%zu)\n", (14 - l), "", s, idx);
                    342:                        else
                    343:                                printf("\n");
                    344:
                    345:                        objtoff += sizeof(*dsp);
                    346:                }
                    347:                printf("\n");
                    348:        }
                    349:
                    350:        if (flags & DUMP_FUNCTION) {
                    351:                uint16_t                *fsp, kind, vlen;
1.13      mpi       352:                uint16_t                *fstart, *fend;
1.1       mpi       353:                size_t                   idx = 0, i = -1;
                    354:                const char              *s;
                    355:                int                      l;
                    356:
1.13      mpi       357:                fstart = (uint16_t *)(data + cth->cth_funcoff);
                    358:                fend = (uint16_t *)(data + cth->cth_typeoff);
                    359:
                    360:                fsp = fstart;
                    361:                while (fsp < fend) {
1.1       mpi       362:                        kind = CTF_INFO_KIND(*fsp);
                    363:                        vlen = CTF_INFO_VLEN(*fsp);
                    364:                        s = elf_idx2sym(&idx, STT_FUNC);
                    365:                        fsp++;
                    366:                        i++;
                    367:
                    368:                        if (kind == CTF_K_UNKNOWN && vlen == 0)
                    369:                                continue;
                    370:
                    371:                        l = printf("  [%zu] FUNC ", i);
                    372:                        if (s != NULL)
                    373:                                printf("(%s)", s);
                    374:                        printf(" returns: %u args: (", *fsp++);
1.13      mpi       375:                        while (vlen-- > 0 && fsp < fend)
1.1       mpi       376:                                printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
                    377:                        printf(")\n");
                    378:                }
                    379:                printf("\n");
                    380:        }
                    381:
                    382:        if (flags & DUMP_TYPE) {
                    383:                uint32_t                 idx = 1, offset = cth->cth_typeoff;
1.14      mpi       384:                uint32_t                 stroff = cth->cth_stroff;
1.1       mpi       385:
1.14      mpi       386:                while (offset < stroff) {
                    387:                        ctf_dump_type(cth, data, dlen, stroff, &offset, idx++);
1.1       mpi       388:                }
                    389:                printf("\n");
                    390:        }
                    391:
                    392:        if (flags & DUMP_STRTAB) {
                    393:                uint32_t                 offset = 0;
                    394:                const char              *str;
                    395:
                    396:                while (offset < cth->cth_strlen) {
                    397:                        str = ctf_off2name(cth, data, dlen, offset);
                    398:
                    399:                        printf("  [%u] ", offset);
                    400:                        if (strcmp(str, "(anon)"))
                    401:                                offset += printf("%s\n", str);
                    402:                        else {
                    403:                                printf("\\0\n");
                    404:                                offset++;
                    405:                        }
                    406:                }
                    407:                printf("\n");
                    408:        }
                    409:
                    410:        if (cth->cth_flags & CTF_F_COMPRESS)
                    411:                free(data);
                    412:
                    413:        return 0;
                    414: }
                    415:
1.14      mpi       416: void
1.1       mpi       417: ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
1.14      mpi       418:     uint32_t stroff, uint32_t *offset, uint32_t idx)
1.1       mpi       419: {
1.14      mpi       420:        const char              *p = data + *offset;
1.1       mpi       421:        const struct ctf_type   *ctt = (struct ctf_type *)p;
                    422:        const struct ctf_array  *cta;
                    423:        uint16_t                *argp, i, kind, vlen, root;
                    424:        uint32_t                 eob, toff;
                    425:        uint64_t                 size;
                    426:        const char              *name, *kname;
                    427:
                    428:        kind = CTF_INFO_KIND(ctt->ctt_info);
                    429:        vlen = CTF_INFO_VLEN(ctt->ctt_info);
                    430:        root = CTF_INFO_ISROOT(ctt->ctt_info);
                    431:        name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
                    432:
                    433:        if (root)
                    434:                printf("  <%u> ", idx);
                    435:        else
                    436:                printf("  [%u] ", idx);
                    437:
                    438:        if ((kname = ctf_kind2name(kind)) != NULL)
                    439:                printf("%s %s", kname, name);
                    440:
                    441:        if (ctt->ctt_size <= CTF_MAX_SIZE) {
                    442:                size = ctt->ctt_size;
                    443:                toff = sizeof(struct ctf_stype);
                    444:        } else {
                    445:                size = CTF_TYPE_LSIZE(ctt);
                    446:                toff = sizeof(struct ctf_type);
                    447:        }
                    448:
                    449:        switch (kind) {
                    450:        case CTF_K_UNKNOWN:
                    451:        case CTF_K_FORWARD:
                    452:                break;
                    453:        case CTF_K_INTEGER:
                    454:                eob = *((uint32_t *)(p + toff));
                    455:                toff += sizeof(uint32_t);
                    456:                printf(" encoding=%s offset=%u bits=%u",
                    457:                    ctf_enc2name(CTF_INT_ENCODING(eob)), CTF_INT_OFFSET(eob),
                    458:                    CTF_INT_BITS(eob));
                    459:                break;
                    460:        case CTF_K_FLOAT:
                    461:                eob = *((uint32_t *)(p + toff));
                    462:                toff += sizeof(uint32_t);
1.7       uwe       463:                printf(" encoding=%s offset=%u bits=%u",
                    464:                    ctf_fpenc2name(CTF_FP_ENCODING(eob)), CTF_FP_OFFSET(eob),
                    465:                    CTF_FP_BITS(eob));
1.1       mpi       466:                break;
                    467:        case CTF_K_ARRAY:
                    468:                cta = (struct ctf_array *)(p + toff);
                    469:                printf(" content: %u index: %u nelems: %u\n", cta->cta_contents,
                    470:                    cta->cta_index, cta->cta_nelems);
                    471:                toff += sizeof(struct ctf_array);
                    472:                break;
                    473:        case CTF_K_FUNCTION:
                    474:                argp = (uint16_t *)(p + toff);
                    475:                printf(" returns: %u args: (%u", ctt->ctt_type, *argp);
                    476:                for (i = 1; i < vlen; i++) {
                    477:                        argp++;
                    478:                        printf(", %u", *argp);
                    479:                }
                    480:                printf(")");
                    481:                toff += (vlen + (vlen & 1)) * sizeof(uint16_t);
                    482:                break;
                    483:        case CTF_K_STRUCT:
                    484:        case CTF_K_UNION:
                    485:                printf(" (%llu bytes)\n", size);
                    486:
                    487:                if (size < CTF_LSTRUCT_THRESH) {
                    488:                        for (i = 0; i < vlen; i++) {
                    489:                                struct ctf_member       *ctm;
                    490:
1.14      mpi       491:                                if (toff > (stroff - sizeof(*ctm)))
                    492:                                        break;
                    493:
1.1       mpi       494:                                ctm = (struct ctf_member *)(p + toff);
                    495:                                toff += sizeof(struct ctf_member);
                    496:
                    497:                                printf("\t%s type=%u off=%u\n",
                    498:                                    ctf_off2name(cth, data, dlen,
                    499:                                        ctm->ctm_name),
                    500:                                    ctm->ctm_type, ctm->ctm_offset);
                    501:                        }
                    502:                } else {
                    503:                        for (i = 0; i < vlen; i++) {
                    504:                                struct ctf_lmember      *ctlm;
                    505:
1.14      mpi       506:                                if (toff > (stroff - sizeof(*ctlm)))
                    507:                                        break;
                    508:
1.1       mpi       509:                                ctlm = (struct ctf_lmember *)(p + toff);
                    510:                                toff += sizeof(struct ctf_lmember);
                    511:
                    512:                                printf("\t%s type=%u off=%llu\n",
                    513:                                    ctf_off2name(cth, data, dlen,
                    514:                                        ctlm->ctlm_name),
                    515:                                    ctlm->ctlm_type, CTF_LMEM_OFFSET(ctlm));
                    516:                        }
                    517:                }
                    518:                break;
                    519:        case CTF_K_ENUM:
                    520:                printf("\n");
                    521:                for (i = 0; i < vlen; i++) {
                    522:                        struct ctf_enum *cte;
                    523:
1.14      mpi       524:                        if (toff > (stroff - sizeof(*cte)))
                    525:                                break;
                    526:
1.1       mpi       527:                        cte = (struct ctf_enum *)(p + toff);
                    528:                        toff += sizeof(struct ctf_enum);
                    529:
                    530:                        printf("\t%s = %d\n",
                    531:                            ctf_off2name(cth, data, dlen, cte->cte_name),
                    532:                            cte->cte_value);
                    533:                }
                    534:                break;
                    535:        case CTF_K_POINTER:
                    536:        case CTF_K_TYPEDEF:
                    537:        case CTF_K_VOLATILE:
                    538:        case CTF_K_CONST:
                    539:        case CTF_K_RESTRICT:
                    540:                printf(" refers to %u", ctt->ctt_type);
                    541:                break;
                    542:        default:
1.14      mpi       543:                errx(1, "incorrect type %u at offset %u", kind, *offset);
1.1       mpi       544:        }
                    545:
                    546:        printf("\n");
                    547:
1.14      mpi       548:        *offset += toff;
1.1       mpi       549: }
                    550:
                    551: const char *
                    552: ctf_kind2name(uint16_t kind)
                    553: {
                    554:        static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
                    555:           "ARRAY", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
                    556:           "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
                    557:
                    558:        if (kind >= nitems(kind_name))
                    559:                return NULL;
                    560:
                    561:        return kind_name[kind];
                    562: }
                    563:
                    564: const char *
                    565: ctf_enc2name(uint16_t enc)
                    566: {
                    567:        static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
                    568:            "BOOL", "SIGNED BOOL" };
                    569:        static char invalid[7];
                    570:
                    571:        if (enc == CTF_INT_VARARGS)
                    572:                return "VARARGS";
                    573:
1.8       uwe       574:        if (enc > 0 && enc <= nitems(enc_name))
1.7       uwe       575:                return enc_name[enc - 1];
                    576:
                    577:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    578:        return invalid;
                    579: }
                    580:
                    581: const char *
                    582: ctf_fpenc2name(uint16_t enc)
                    583: {
                    584:        static const char *enc_name[] = { "SINGLE", "DOUBLE", NULL, NULL,
                    585:            NULL, "LDOUBLE" };
                    586:        static char invalid[7];
                    587:
                    588:        if (enc > 0 && enc <= nitems(enc_name) && enc_name[enc - 1] != NULL)
1.1       mpi       589:                return enc_name[enc - 1];
                    590:
                    591:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    592:        return invalid;
                    593: }
                    594:
                    595: const char *
                    596: ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
                    597:     uint32_t offset)
                    598: {
                    599:        const char              *name;
                    600:
                    601:        if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
                    602:                return "external";
                    603:
                    604:        if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
                    605:                return "exceeds strlab";
                    606:
                    607:        if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
                    608:                return "invalid";
                    609:
                    610:        name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
                    611:        if (*name == '\0')
                    612:                return "(anon)";
                    613:
                    614:        return name;
                    615: }
                    616:
                    617: char *
                    618: decompress(const char *buf, size_t size, off_t len)
                    619: {
                    620: #ifdef ZLIB
                    621:        z_stream                 stream;
                    622:        char                    *data;
                    623:        int                      error;
                    624:
                    625:        data = malloc(len);
                    626:        if (data == NULL) {
                    627:                warn(NULL);
                    628:                return NULL;
                    629:        }
                    630:
                    631:        memset(&stream, 0, sizeof(stream));
                    632:        stream.next_in = (void *)buf;
                    633:        stream.avail_in = size;
                    634:        stream.next_out = (uint8_t *)data;
                    635:        stream.avail_out = len;
                    636:
                    637:        if ((error = inflateInit(&stream)) != Z_OK) {
                    638:                warnx("zlib inflateInit failed: %s", zError(error));
                    639:                goto exit;
                    640:        }
                    641:
                    642:        if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
                    643:                warnx("zlib inflate failed: %s", zError(error));
                    644:                inflateEnd(&stream);
                    645:                goto exit;
                    646:        }
                    647:
                    648:        if ((error = inflateEnd(&stream)) != Z_OK) {
                    649:                warnx("zlib inflateEnd failed: %s", zError(error));
                    650:                goto exit;
                    651:        }
                    652:
                    653:        if (stream.total_out != len) {
                    654:                warnx("decompression failed: %llu != %llu",
                    655:                    stream.total_out, len);
                    656:                goto exit;
                    657:        }
                    658:
                    659:        return data;
                    660:
                    661: exit:
                    662:        free(data);
                    663: #endif /* ZLIB */
                    664:        return NULL;
                    665: }
                    666:
                    667: __dead void
                    668: usage(void)
                    669: {
                    670:        fprintf(stderr, "usage: %s [-dfhlst] file ...\n",
                    671:            getprogname());
                    672:        exit(1);
                    673: }