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

Annotation of src/usr.bin/pmdb/core.c, Revision 1.4

1.4     ! mickey      1: /*     $OpenBSD: core.c,v 1.3 2002/07/22 02:55:54 art Exp $    */
1.1       fgsch       2: /*
                      3:  * Copyright (c) 2002 Jean-Francois Brousseau <krapht@secureops.com>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29: #include <sys/mman.h>
                     30:
                     31: #include <err.h>
                     32: #include <stdio.h>
                     33: #include <fcntl.h>
                     34: #include <string.h>
                     35: #include <stdlib.h>
                     36: #include <signal.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "core.h"
                     40: #include "pmdb.h"
1.4     ! mickey     41: #include "symbol.h"
1.1       fgsch      42:
                     43: int
                     44: read_core(const char *path, struct pstate *ps)
                     45: {
                     46:        struct corefile *cf;
                     47:        void *core_map;
                     48:        off_t c_off;
                     49:        int i, cfd;
                     50:
                     51:        cf = (struct corefile *)malloc(sizeof(*cf));
                     52:        if (cf == NULL)
                     53:                err(1, "malloc");
                     54:
                     55:        cfd = open(path, O_RDONLY, 0600);
                     56:        if (cfd < 0)
                     57:                err(1, "open() failed on core file");
                     58:
                     59:        if (fstat(cfd, &(cf->cfstat)) < 0)
                     60:                err(1, "fstat() failed on core");
                     61:
                     62:        if (cf->cfstat.st_mtimespec.tv_sec < ps->exec_stat.st_mtimespec.tv_sec)
                     63:                warnx("executable is more recent than core file!");
                     64:
1.3       art        65:        core_map = mmap(NULL, cf->cfstat.st_size, PROT_READ, MAP_PRIVATE,
1.1       fgsch      66:            cfd, 0);
                     67:        if (core_map == MAP_FAILED)
                     68:                err(1, "mmap() failed on core");
                     69:
                     70:        cf->chdr = (struct core *)core_map;
                     71:        c_off = cf->chdr->c_hdrsize;
                     72:        if (CORE_GETMAGIC(*(cf->chdr)) != COREMAGIC)
                     73:                errx(1, "hey, that's not a core file");
                     74:
                     75:        printf("Core file generated from '%s' by signal %d (SIG%s)\n",
                     76:            cf->chdr->c_name, cf->chdr->c_signo,
                     77:            sys_signame[cf->chdr->c_signo]);
                     78:
                     79: #ifdef DEBUG
                     80:        printf("Core: text=0x%lx, data=0x%lx, stack=0x%lx\n",
                     81:            cf->chdr->c_tsize, cf->chdr->c_dsize, cf->chdr->c_ssize);
                     82: #endif
                     83:
                     84:        cf->segs = (struct coreseg **)calloc(cf->chdr->c_nseg,
                     85:            sizeof(cf->segs));
                     86:        if (cf->segs == NULL)
                     87:                err(1, "calloc");
                     88:
                     89:        for (i = 0; i < cf->chdr->c_nseg; i++) {
                     90:                cf->segs[i] = (struct coreseg *)(core_map + c_off);
                     91:                if (CORE_GETMAGIC(*(cf->segs[i])) != CORESEGMAGIC)
                     92:                        errx(1, "invalid segment hdr for segment %d", i);
                     93:
                     94:                if (CORE_GETFLAG(*(cf->segs[i])) & CORE_CPU) {
                     95:                        cf->regs = (struct reg *)
                     96:                            ((long) cf->segs[i] + cf->chdr->c_seghdrsize);
                     97:                }
                     98:
                     99:                if (CORE_GETFLAG(*(cf->segs[i])) & CORE_STACK)
                    100:                        cf->c_stack = cf->segs[i] + cf->chdr->c_seghdrsize;
                    101:
                    102:                c_off += cf->chdr->c_seghdrsize + cf->segs[i]->c_size;
                    103:
                    104: #ifdef DEBUG
                    105:                (void)printf("seg[%d]: midmag=0x%lx  addr=0x%lx  size=0x%lx\n",
                    106:                    i, cf->segs[i]->c_midmag, cf->segs[i]->c_addr,
                    107:                    cf->segs[i]->c_size);
                    108: #endif
                    109:        }
                    110:
                    111:        cf->path = (char *)path;
                    112:        ps->ps_flags |= PSF_CORE;
                    113:        ps->ps_core = cf;
                    114:
                    115:        return (0);
                    116: }
                    117:
                    118: void
                    119: free_core(struct pstate *ps)
                    120: {
                    121:        struct corefile *cf = ps->ps_core;
                    122:
                    123:        if (cf == NULL)
                    124:                return;
                    125:
                    126:        if (cf->segs != NULL) {
                    127:                free(cf->segs);
                    128:                cf->segs = NULL;
                    129:        }
                    130: }
                    131:
                    132: void
1.4     ! mickey    133: core_printregs(struct pstate *ps)
1.1       fgsch     134: {
1.4     ! mickey    135:        struct corefile *cf = ps->ps_core;
1.1       fgsch     136:        reg *rg;
1.4     ! mickey    137:        char buf[256];
1.1       fgsch     138:        int i;
                    139:
                    140:        rg = (reg *)cf->regs;
                    141:        for (i = 0; i < md_def.nregs; i++)
1.4     ! mickey    142:                printf("%s:\t0x%.*lx\t%s\n", md_def.md_reg_names[i],
        !           143:                    (int)(sizeof(reg) * 2), (long) rg[i],
        !           144:                    sym_print(ps, rg[i], buf, sizeof(buf)));
1.2       art       145: }
                    146:
                    147:
                    148: ssize_t
                    149: core_read(struct pstate *ps, off_t from, void *to, size_t size)
                    150: {
1.3       art       151:        struct coreseg *cs;
1.2       art       152:        size_t read;
                    153:        void *fp;
1.3       art       154:        int i;
1.2       art       155:
                    156:        for (i = 0; i < ps->ps_core->chdr->c_nseg; i++) {
                    157:                cs = ps->ps_core->segs[i];
                    158:                if ((from >= cs->c_addr) && (from < (cs->c_addr + cs->c_size))) {
                    159:                        read = size;
1.3       art       160:                        fp = cs + sizeof(*cs) + ((u_long)from - cs->c_addr);
1.2       art       161:                        memcpy(to, fp, read);
1.3       art       162:                        return (read);
1.2       art       163:                }
                    164:        }
                    165:
                    166:        return (-1);
                    167: }
                    168:
                    169:
                    170: ssize_t
                    171: core_write(struct pstate *ps, off_t to, void *from, size_t size)
                    172: {
1.3       art       173:        struct coreseg *cs;
1.2       art       174:        size_t written;
                    175:        void *fp;
1.3       art       176:        int i;
1.2       art       177:
                    178:        for (i = 0; i < ps->ps_core->chdr->c_nseg; i++) {
                    179:                cs = ps->ps_core->segs[i];
                    180:                if ((to > cs->c_addr) && (to < (cs->c_addr + cs->c_size))) {
                    181:                        written = size;
                    182:                        fp = cs + sizeof(*cs) + (to - cs->c_addr);
                    183:                        memcpy(fp, from, written);
1.3       art       184:                        return (written);
1.2       art       185:                }
                    186:        }
                    187:
                    188:        return (-1);
1.1       fgsch     189: }