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

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