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

Annotation of src/usr.bin/pmdb/process.c, Revision 1.6

1.6     ! art         1: /*     $OpenBSD: process.c,v 1.5 2002/06/09 04:33:42 fgsch 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.4       todd       30: #include <sys/stat.h>
                     31:
1.6     ! art        32: #include <machine/reg.h>
        !            33:
1.3       fgsch      34: #include <err.h>
                     35: #include <errno.h>
                     36: #include <signal.h>
                     37: #include <stdio.h>
1.1       art        38: #include <stdlib.h>
1.3       fgsch      39: #include <string.h>
1.1       art        40: #include <unistd.h>
                     41:
                     42: #include "pmdb.h"
1.6     ! art        43: #include "core.h"
1.1       art        44: #include "symbol.h"
                     45: #include "break.h"
                     46:
                     47: int
                     48: process_load(struct pstate *ps)
                     49: {
                     50:        if (ps->ps_state == LOADED)
                     51:                return (0);
1.3       fgsch      52:
                     53:        if (access(*ps->ps_argv, R_OK|X_OK) < 0) {
                     54:                fprintf(stderr, "%s: %s.\n", *ps->ps_argv,
                     55:                    strerror(errno));
                     56:                return (0);
                     57:        }
1.1       art        58:
1.4       todd       59:        if (stat(ps->ps_argv[0], &(ps->exec_stat)) < 0)
                     60:                err(1, "stat()");
                     61:
1.6     ! art        62:        if ((ps->ps_flags & PSF_SYMBOLS) == 0) {
        !            63:                sym_init_exec(ps, ps->ps_argv[0]);
        !            64:                ps->ps_flags |= PSF_SYMBOLS;
        !            65:        }
        !            66:
        !            67:        ps->ps_state = LOADED;
        !            68:
1.4       todd       69:        if (ps->ps_pid != 0) {
                     70:                /* attach to an already running process */
                     71:                if (ptrace(PT_ATTACH, ps->ps_pid, (caddr_t) 0, 0) < 0)
                     72:                        err(1, "failed to ptrace process");
                     73:                ps->ps_state = STOPPED;
                     74:                ps->ps_flags |= PSF_ATCH;
                     75:        }
                     76:
1.6     ! art        77:        return (0);
        !            78: }
        !            79:
        !            80:
        !            81: int
        !            82: process_run(struct pstate *ps)
        !            83: {
        !            84:        int status;
        !            85:
        !            86:        if ((ps->ps_state == RUNNING) || (ps->ps_state == STOPPED)) {
        !            87:                warnx("process is already running");
        !            88:                return 0;
1.1       art        89:        }
                     90:
1.6     ! art        91:        switch (ps->ps_pid = fork()) {
        !            92:        case 0:
        !            93:                if (ptrace(PT_TRACE_ME, getpid(), NULL, 0) != 0)
        !            94:                        err(1, "ptrace(PT_TRACE_ME)");
        !            95:                execvp(*ps->ps_argv, ps->ps_argv);
        !            96:                err(1, "exec");
        !            97:                /* NOTREACHED */
        !            98:        case -1:
        !            99:                err(1, "fork");
        !           100:                /* NOTREACHED */
        !           101:        default:
        !           102:                warnx("process started with PID %d", ps->ps_pid);
        !           103:                break;
1.1       art       104:        }
                    105:
1.6     ! art       106:        ps->ps_state = LOADED;
        !           107:
1.1       art       108:        if (wait(&status) == 0)
                    109:                err(1, "wait");
                    110:
1.5       fgsch     111:        return (0);
1.1       art       112: }
                    113:
1.6     ! art       114:
1.1       art       115: int
                    116: process_kill(struct pstate *ps)
                    117: {
                    118:        switch(ps->ps_state) {
                    119:        case RUNNING:
                    120:        case STOPPED:
                    121:                if (ptrace(PT_KILL, ps->ps_pid, NULL, 0) != 0)
                    122:                        err(1, "ptrace(PT_KILL)");
1.5       fgsch     123:                return (1);
1.1       art       124:        default:
1.5       fgsch     125:                return (0);
1.1       art       126:        }
                    127: }
                    128:
                    129: int
1.6     ! art       130: process_read(struct pstate *ps, off_t from, void *to, size_t size)
        !           131: {
        !           132:        struct ptrace_io_desc piod;
        !           133:
        !           134:        if (((ps->ps_state == NONE) || (ps->ps_state == LOADED) ||
        !           135:            (ps->ps_state == TERMINATED)) && (ps->ps_flags & PSF_CORE)) {
        !           136:                return core_read(ps, from, to, size);
        !           137:        }
        !           138:        else {
        !           139:                piod.piod_op = PIOD_READ_D;
        !           140:                piod.piod_offs = (void *)(long)from;
        !           141:                piod.piod_addr = to;
        !           142:                piod.piod_len = size;
        !           143:
        !           144:                return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
        !           145:        }
        !           146: }
        !           147:
        !           148: int
        !           149: process_write(struct pstate *ps, off_t to, void *from, size_t size)
        !           150: {
        !           151:        struct ptrace_io_desc piod;
        !           152:
        !           153:        if ((ps->ps_state == NONE) && (ps->ps_flags & PSF_CORE))
        !           154:                return core_write(ps, to, from, size);
        !           155:        else {
        !           156:                piod.piod_op = PIOD_WRITE_D;
        !           157:                piod.piod_offs = (void *)(long)to;
        !           158:                piod.piod_addr = from;
        !           159:                piod.piod_len = size;
        !           160:
        !           161:                return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
        !           162:        }
        !           163: }
        !           164:
        !           165: int
        !           166: process_getregs(struct pstate *ps, struct reg *r)
        !           167: {
        !           168:
        !           169:        if (ps->ps_state == STOPPED) {
        !           170:                if (ptrace(PT_GETREGS, ps->ps_pid, (caddr_t)&r, 0) != 0)
        !           171:                        return (-1);
        !           172:        }
        !           173:        else if (ps->ps_flags & PSF_CORE) {
        !           174:                memcpy(r, ps->ps_core->regs, sizeof(*r));
        !           175:        }
        !           176:        else
        !           177:                return (-1);
        !           178:
        !           179:        return 0;
        !           180: }
        !           181:
        !           182: int
1.1       art       183: cmd_process_kill(int argc, char **argv, void *arg)
                    184: {
                    185:        struct pstate *ps = arg;
                    186:
                    187:        process_kill(ps);
                    188:
1.5       fgsch     189:        return (1);
1.1       art       190: }
                    191:
                    192: int
                    193: process_bkpt_main(struct pstate *ps, void *arg)
                    194: {
                    195:        sym_update(ps);
                    196:
1.5       fgsch     197:        return (BKPT_DEL_CONT);
1.1       art       198: }
                    199:
                    200: int
                    201: cmd_process_run(int argc, char **argv, void *arg)
                    202: {
                    203:        struct pstate *ps = arg;
                    204:
                    205:        if (ps->ps_state == NONE) {
                    206:                reg main_addr;
                    207:
                    208:                process_load(ps);
                    209:                if (sym_lookup(ps, "main", &main_addr))
                    210:                        warnx("no main");
                    211:                else if (bkpt_add_cb(ps, main_addr, process_bkpt_main, NULL))
                    212:                        warn("no bkpt at main 0x%lx", main_addr);
                    213:        }
                    214:
                    215:        if (ps->ps_state != LOADED) {
                    216:                fprintf(stderr, "Process already running.\n");
1.5       fgsch     217:                return (0);
1.1       art       218:        }
                    219:
1.6     ! art       220:        process_run(ps);
1.1       art       221:        /*
                    222:         * XXX - there isn't really any difference between STOPPED and
                    223:         * LOADED, we should probably get rid of one.
                    224:         */
                    225:        ps->ps_state = STOPPED;
                    226:        ps->ps_signum = 0;
                    227:
                    228:        return (cmd_process_cont(argc, argv, arg));
                    229: }
                    230:
                    231: int
                    232: cmd_process_cont(int argc, char **argv, void *arg)
                    233: {
                    234:        struct pstate *ps = arg;
                    235:        int signum;
                    236:        int req = (ps->ps_flags & PSF_STEP) ? PT_STEP : PT_CONTINUE;
                    237:
                    238:        if (ps->ps_state != STOPPED) {
                    239:                fprintf(stderr, "Process not loaded and stopped %d\n",
                    240:                    ps->ps_state);
                    241:                return (0);
                    242:        }
                    243:
                    244:        /* Catch SIGINT and SIGTRAP, pass all other signals. */
                    245:        switch (ps->ps_signum) {
                    246:        case SIGINT:
                    247:        case SIGTRAP:
                    248:                signum = 0;
                    249:                break;
                    250:        default:
                    251:                signum = ps->ps_signum;
                    252:                break;
                    253:        }
                    254:
                    255:        if (ptrace(req, ps->ps_pid, (caddr_t)ps->ps_npc, signum) != 0) {
                    256:                err(1, "ptrace(%s)", req == PT_STEP ? "PT_STEP":"PT_CONTINUE");
                    257:        }
                    258:
                    259:        ps->ps_state = RUNNING;
                    260:        ps->ps_npc = 1;
                    261:
                    262:        return (1);
                    263: }