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

Annotation of src/usr.bin/pmdb/pmdb.c, Revision 1.17

1.17    ! deraadt     1: /*     $OpenBSD: pmdb.c,v 1.16 2003/05/15 00:11:03 jfb Exp $   */
1.1       art         2: /*
                      3:  * Copyright (c) 2002 Artur Grabowski <art@openbsd.org>
                      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/types.h>
                     28: #include <sys/ptrace.h>
                     29: #include <sys/wait.h>
1.7       fgsch      30: #include <sys/endian.h>
1.1       art        31:
                     32: #include <stdlib.h>
                     33: #include <stdio.h>
                     34: #include <unistd.h>
                     35: #include <signal.h>
                     36: #include <err.h>
                     37: #include <errno.h>
                     38: #include <string.h>
                     39:
                     40: #include "pmdb.h"
                     41: #include "symbol.h"
                     42: #include "clit.h"
                     43: #include "break.h"
1.7       fgsch      44: #include "core.h"
1.1       art        45:
                     46: static int cmd_show_registers(int, char **, void *);
                     47: static int cmd_show_backtrace(int, char **, void *);
1.14      mickey     48: static int cmd_examine(int, char **, void *);
1.1       art        49: static int cmd_quit(int, char **, void *);
                     50:
                     51: struct clit cmds[] = {
                     52:        /* debugging info commands. */
                     53:        { "regs", "show registers", 0, 0, cmd_show_registers, (void *)-1 },
                     54:        { "trace", "show backtrace", 0, 0, cmd_show_backtrace, (void *)-1 },
1.14      mickey     55:        { "x", "examine memory", 1, 16, cmd_examine, (void *)-1 },
1.1       art        56:
                     57:        /* Process handling commands. */
                     58:        { "run", "run process", 0, 0, cmd_process_run, (void *)-1 },
                     59:        { "continue", "continue process", 0, 0, cmd_process_cont, (void *)-1 },
                     60:        { "kill", "kill process", 0, 0, cmd_process_kill, (void *)-1 },
1.12      art        61:        { "setenv", "set env variables", 2, 2, cmd_process_setenv, (void *)-1 },
1.1       art        62:
                     63:        /* signal handling commands. */
                     64:        { "signal", "ignore signal", 2, 2, cmd_signal_ignore, (void *)-1 },
                     65:        { "sigstate", "show signal state", 0, 0, cmd_signal_show, (void *)-1 },
                     66:
                     67:        /* breakpoints */
                     68:        { "break", "set breakpoint", 1, 1, cmd_bkpt_add, (void *)-1 },
                     69:        { "step", "single step one insn", 0, 0, cmd_sstep, (void *)-1 },
1.3       art        70:
                     71:        /* symbols */
                     72:        { "sym_load", "load symbol table", 2, 2, cmd_sym_load, (void *)-1 },
1.1       art        73:
                     74:        /* misc commands. */
                     75:        { "help", "print help", 0, 1, cmd_help, NULL },
                     76:        { "quit", "quit", 0, 0, cmd_quit, (void *)-1 },
                     77:        { "exit", "quit", 0, 0, cmd_quit, (void *)-1 },
                     78: };
                     79:
1.6       fgsch      80: #define NCMDS  sizeof(cmds)/sizeof(cmds[0])
                     81:
1.7       fgsch      82: void
1.17    ! deraadt    83: usage(void)
1.7       fgsch      84: {
                     85:        extern char *__progname;
                     86:
1.9       fgsch      87:        fprintf(stderr, "Usage: %s [-c core] [-p pid] <program> args\n",
                     88:            __progname);
1.7       fgsch      89:        exit(1);
                     90: }
                     91:
1.1       art        92: int
                     93: main(int argc, char **argv)
                     94: {
                     95:        struct pstate ps;
1.7       fgsch      96:        int i, c;
1.1       art        97:        int status;
                     98:        void *cm;
1.8       todd       99:        char *pmenv, *core, *perr;
1.1       art       100:        int level;
1.8       todd      101:        pid_t pid;
1.1       art       102:
1.7       fgsch     103:        core = NULL;
1.8       todd      104:        pid = 0;
1.7       fgsch     105:
1.9       fgsch     106:        while ((c = getopt(argc, argv, "c:p:")) != -1) {
1.7       fgsch     107:                switch(c) {
                    108:                        case 'c':
                    109:                                core = optarg;
                    110:                                break;
1.8       todd      111:                        case 'p':
                    112:                                pid = (pid_t) strtol(optarg, &perr, 10);
1.9       fgsch     113:                                if (*perr != '\0')
1.8       todd      114:                                        errx(1, "invalid PID");
                    115:                                break;
1.7       fgsch     116:                        case '?':
                    117:                        default:
                    118:                                usage();
                    119:                                /* NOTREACHED */
                    120:                }
                    121:        }
                    122:        argc -= optind;
                    123:        argv += optind;
1.11      fgsch     124:
                    125:        if (argc == 0)
                    126:                usage();
1.7       fgsch     127:
1.1       art       128:        if ((pmenv = getenv("IN_PMDB")) != NULL) {
                    129:                level = atoi(pmenv);
                    130:                level++;
                    131:        } else
                    132:                level = 0;
                    133:
                    134:        if (level > 0)
                    135:                asprintf(&prompt_add, "(%d)", level);
                    136:        asprintf(&pmenv, "%d", level);
                    137:        setenv("IN_PMDB", pmenv, 1);
1.15      pvalchev  138:        if (pmenv)
                    139:                free(pmenv);
1.1       art       140:
1.8       todd      141:        ps.ps_pid = pid;
1.1       art       142:        ps.ps_state = NONE;
1.7       fgsch     143:        ps.ps_argc = argc;
                    144:        ps.ps_argv = argv;
1.1       art       145:        ps.ps_flags = 0;
                    146:        ps.ps_signum = 0;
                    147:        ps.ps_npc = 1;
                    148:        TAILQ_INIT(&ps.ps_bkpts);
                    149:        TAILQ_INIT(&ps.ps_sstep_cbs);
                    150:
                    151:        signal(SIGINT, SIG_IGN);
                    152:
1.6       fgsch     153:        for (i = 0; i < NCMDS; i++)
1.1       art       154:                if (cmds[i].arg == (void *)-1)
                    155:                        cmds[i].arg = &ps;
                    156:
                    157:        md_def_init();
                    158:        init_sigstate(&ps);
                    159:
1.10      art       160:        if ((core != NULL) && (read_core(core, &ps) < 0))
                    161:                warnx("failed to load core file");
1.1       art       162:
1.10      art       163:        if (process_load(&ps) < 0)
                    164:                errx(1, "failed to load process");
1.7       fgsch     165:
1.6       fgsch     166:        cm = cmdinit(cmds, NCMDS);
1.1       art       167:        while (ps.ps_state != TERMINATED) {
                    168:                int signum;
                    169:                int stopped;
                    170:                int cont;
                    171:
                    172:                if (ps.ps_state == STOPPED) {
                    173:                        sym_update(&ps);
                    174:                }
                    175:
                    176:                if (ps.ps_state != RUNNING && cmdloop(cm) == 0) {
                    177:                        cmd_quit(0, NULL, &ps);
                    178:                }
                    179:
                    180:                if (ps.ps_state == TERMINATED)
                    181:                        break;
                    182:
                    183:                if (wait(&status) == 0)
                    184:                        err(1, "wait");
                    185:                if (WIFEXITED(status)) {
                    186:                        if ((ps.ps_flags & PSF_KILL) == 0) {
                    187:                                ps.ps_state = NONE;
                    188:                        } else {
                    189:                                ps.ps_state = TERMINATED;
                    190:                        }
                    191:                        fprintf(stderr, "process exited with status %d\n",
                    192:                            WEXITSTATUS(status));
                    193:                        continue;
                    194:                }
                    195:                if (WIFSIGNALED(status)) {
                    196:                        signum = WTERMSIG(status);
                    197:                        stopped = 0;
                    198:                } else {
                    199:                        signum = WSTOPSIG(status);
                    200:                        stopped = 1;
                    201:                }
                    202:                cont = 0;
                    203:                if (stopped)
                    204:                        cont = bkpt_check(&ps);
                    205:                process_signal(&ps, signum, stopped, cont);
                    206:        }
                    207:
                    208:        cmdend(cm);
                    209:
                    210:        sym_destroy(&ps);
                    211:
                    212:        return (0);
                    213: }
                    214:
                    215:
                    216: static int
                    217: cmd_show_registers(int argc, char **argv, void *arg)
                    218: {
                    219:        struct pstate *ps = arg;
                    220:        char buf[256];
                    221:        int i;
                    222:        reg *rg;
                    223:
                    224:        if (ps->ps_state != STOPPED) {
1.7       fgsch     225:                if (ps->ps_flags & PSF_CORE) {
                    226:                        /* dump registers from core */
1.13      mickey    227:                        core_printregs(ps);
1.7       fgsch     228:                        return (0);
                    229:                }
1.1       art       230:                fprintf(stderr, "process not stopped\n");
1.7       fgsch     231:                return (0);
1.1       art       232:        }
                    233:
                    234:        rg = alloca(sizeof(*rg) * md_def.nregs);
                    235:
                    236:        if (md_getregs(ps, rg))
                    237:                err(1, "can't get registers");
                    238:        for (i = 0; i < md_def.nregs; i++)
                    239:                printf("%s:\t0x%.*lx\t%s\n", md_def.md_reg_names[i],
                    240:                    (int)(sizeof(reg) * 2), (long)rg[i],
                    241:                    sym_print(ps, rg[i], buf, sizeof(buf)));
1.7       fgsch     242:        return (0);
1.1       art       243: }
                    244:
                    245: static int
                    246: cmd_show_backtrace(int argc, char **argv, void *arg)
                    247: {
                    248:        struct pstate *ps = arg;
                    249:        int i;
                    250:
1.10      art       251:        if (ps->ps_state != STOPPED && !(ps->ps_flags & PSF_CORE)) {
1.1       art       252:                fprintf(stderr, "process not stopped\n");
1.7       fgsch     253:                return (0);
1.1       art       254:        }
                    255:
                    256:        /* no more than 100 frames */
                    257:        for (i = 0; i < 100; i++) {
                    258:                struct md_frame mfr;
                    259:                char namebuf[1024], *name;
                    260:                reg offs;
                    261:                int j;
                    262:
                    263:                mfr.nargs = -1;
                    264:
                    265:                if (md_getframe(ps, i, &mfr))
                    266:                        break;
                    267:
                    268:                name = sym_name_and_offset(ps, mfr.pc, namebuf,
                    269:                    sizeof(namebuf), &offs);
                    270:                if (name == NULL) {
                    271:                        snprintf(namebuf, sizeof(namebuf), "0x%lx", mfr.pc);
                    272:                        name = namebuf;
1.5       drahn     273:                        offs = 0;
1.1       art       274:                }
                    275:
                    276:                printf("%s(", name);
                    277:                for (j = 0; j < mfr.nargs; j++) {
                    278:                        printf("0x%lx", mfr.args[j]);
                    279:                        if (j < mfr.nargs - 1)
                    280:                                printf(", ");
                    281:                }
1.5       drahn     282:                if (offs == 0) {
1.6       fgsch     283:                        printf(")\n");
1.5       drahn     284:                } else {
                    285:                        printf(")+0x%lx\n", offs);
                    286:                }
1.1       art       287:        }
1.7       fgsch     288:
                    289:        return (0);
1.1       art       290: }
                    291:
                    292: static int
                    293: cmd_quit(int argc, char **argv, void *arg)
                    294: {
                    295:        struct pstate *ps = arg;
                    296:
1.8       todd      297:        if ((ps->ps_flags & PSF_ATCH)) {
                    298:                if ((ps->ps_flags & PSF_ATCH) &&
                    299:                    ptrace(PT_DETACH, ps->ps_pid, NULL, 0) < 0)
                    300:                        err(1, "ptrace(PT_DETACH)");
                    301:        } else {
                    302:                ps->ps_flags |= PSF_KILL;
1.1       art       303:
1.8       todd      304:                if (process_kill(ps))
1.9       fgsch     305:                        return (1);
1.8       todd      306:        }
1.1       art       307:
                    308:        ps->ps_state = TERMINATED;
1.9       fgsch     309:        return (1);
1.14      mickey    310: }
                    311:
                    312: static int
                    313: cmd_examine(int argc, char **argv, void *arg)
                    314: {
                    315:        struct pstate *ps = arg;
                    316:        char buf[256];
                    317:        reg addr, val;
                    318:        int i;
                    319:
                    320:        for (i = 1; argv[i]; i++) {
                    321:
                    322:                addr = strtoul(argv[i], NULL, 0);
                    323:                if (!addr) {    /* assume it's a symbol */
                    324:                        if (sym_lookup(ps, argv[i], &addr)) {
                    325:                                warn( "Can't find: %s", argv[i]);
                    326:                                return (0);
                    327:                        }
                    328:                }
                    329:
                    330:                if (process_read(ps, addr, &val, sizeof(val))) {
                    331:                        warn("Can't read process contents at 0x%lx", addr);
                    332:                        return (0);
                    333:                }
                    334:
                    335:                printf("%s:\t%s\n", argv[i],
                    336:                    sym_print(ps, val, buf, sizeof(buf)));
                    337:        }
                    338:
                    339:        return (0);
1.1       art       340: }
                    341:
                    342: /*
                    343:  * Perform command completion.
                    344:  * Pretty simple. if there are spaces in "buf", the last string is a symbol
                    345:  * otherwise it's a command.
                    346:  */
                    347: int
                    348: cmd_complt(char *buf, size_t buflen)
                    349: {
                    350:        struct clit *match;
                    351:        char *start;
                    352:        int command;
                    353:        int i, j, len;
                    354:        int onlymatch;
                    355:
                    356:        command = (strchr(buf, ' ') == NULL);
                    357:
                    358:        if (!command) {
                    359:                /* XXX - can't handle symbols yet. */
1.9       fgsch     360:                return (-1);
1.1       art       361:        }
                    362:
                    363:        start = buf;
                    364:        len = strlen(buf);
                    365:
                    366:        match = NULL;
                    367:        for (i = 0; i < sizeof(cmds) / sizeof(cmds[i]); i++) {
                    368:                if (strncmp(start, cmds[i].cmd, len) == 0) {
                    369:                        struct clit *cmdp;
                    370:
                    371:                        cmdp = &cmds[i];
                    372:                        if (match == NULL) {
                    373:                                onlymatch = 1;
                    374:                                match = cmdp;
                    375:                                strlcpy(buf, match->cmd, buflen);
                    376:                                continue;
                    377:                        }
                    378:                        onlymatch = 0;
                    379:                        for (j = len; j < buflen; j++) {
                    380:                                if (buf[j] != cmdp->cmd[j]) {
                    381:                                        buf[j] = '\0';
                    382:                                        break;
                    383:                                }
                    384:                                if (cmdp->cmd[j] == '\0')
                    385:                                        break;
                    386:                        }
                    387:                }
                    388:        }
                    389:
                    390:        /*
                    391:         * Be nice. If there could be arguments for this command and it's
                    392:         * the only match append a space.
                    393:         */
                    394:        if (match && onlymatch /*&& match->maxargc > 0*/)
                    395:                strlcat(buf, " ", buflen);
                    396:
                    397:        return (match && onlymatch) ? 0 : -1;
                    398: }
                    399:
                    400: /*
1.7       fgsch     401:  * The "standard" wrapper
1.1       art       402:  */
                    403: void *
                    404: emalloc(size_t sz)
                    405: {
                    406:        void *ret;
                    407:        if ((ret = malloc(sz)) == NULL)
                    408:                err(1, "malloc");
                    409:        return (ret);
                    410: }