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

1.11    ! miod        1: /*     $OpenBSD: process.c,v 1.10 2002/08/08 18:27:57 art 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));
1.11    ! miod       56:                return (1);
1.3       fgsch      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);
1.8       art       137:        } else {
1.6       art       138:                piod.piod_op = PIOD_READ_D;
                    139:                piod.piod_offs = (void *)(long)from;
                    140:                piod.piod_addr = to;
                    141:                piod.piod_len = size;
                    142:
                    143:                return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
                    144:        }
                    145: }
                    146:
                    147: int
                    148: process_write(struct pstate *ps, off_t to, void *from, size_t size)
                    149: {
                    150:        struct ptrace_io_desc piod;
                    151:
1.8       art       152:        if ((ps->ps_state == NONE) && (ps->ps_flags & PSF_CORE)) {
1.6       art       153:                return core_write(ps, to, from, size);
1.8       art       154:        } else {
1.6       art       155:                piod.piod_op = PIOD_WRITE_D;
                    156:                piod.piod_offs = (void *)(long)to;
                    157:                piod.piod_addr = from;
                    158:                piod.piod_len = size;
                    159:
                    160:                return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
                    161:        }
                    162: }
                    163:
                    164: int
                    165: process_getregs(struct pstate *ps, struct reg *r)
                    166: {
                    167:
1.9       art       168:        if (ps->ps_flags & PSF_CORE) {
1.6       art       169:                memcpy(r, ps->ps_core->regs, sizeof(*r));
                    170:
1.9       art       171:                return (0);
                    172:        }
                    173:
                    174:        return (ptrace(PT_GETREGS, ps->ps_pid, (caddr_t)r, 0));
1.6       art       175: }
                    176:
                    177: int
1.1       art       178: cmd_process_kill(int argc, char **argv, void *arg)
                    179: {
                    180:        struct pstate *ps = arg;
                    181:
                    182:        process_kill(ps);
                    183:
1.5       fgsch     184:        return (1);
1.1       art       185: }
                    186:
                    187: int
                    188: process_bkpt_main(struct pstate *ps, void *arg)
                    189: {
                    190:        sym_update(ps);
                    191:
1.5       fgsch     192:        return (BKPT_DEL_CONT);
1.1       art       193: }
                    194:
                    195: int
                    196: cmd_process_run(int argc, char **argv, void *arg)
                    197: {
                    198:        struct pstate *ps = arg;
                    199:
                    200:        if (ps->ps_state == NONE) {
                    201:                reg main_addr;
                    202:
1.11    ! miod      203:                if (process_load(ps) != 0)
        !           204:                        return (0);
        !           205:
1.1       art       206:                if (sym_lookup(ps, "main", &main_addr))
                    207:                        warnx("no main");
                    208:                else if (bkpt_add_cb(ps, main_addr, process_bkpt_main, NULL))
                    209:                        warn("no bkpt at main 0x%lx", main_addr);
                    210:        }
                    211:
                    212:        if (ps->ps_state != LOADED) {
                    213:                fprintf(stderr, "Process already running.\n");
1.5       fgsch     214:                return (0);
1.1       art       215:        }
                    216:
1.6       art       217:        process_run(ps);
1.1       art       218:        /*
                    219:         * XXX - there isn't really any difference between STOPPED and
                    220:         * LOADED, we should probably get rid of one.
                    221:         */
                    222:        ps->ps_state = STOPPED;
                    223:        ps->ps_signum = 0;
                    224:
                    225:        return (cmd_process_cont(argc, argv, arg));
                    226: }
                    227:
                    228: int
                    229: cmd_process_cont(int argc, char **argv, void *arg)
                    230: {
                    231:        struct pstate *ps = arg;
                    232:        int signum;
                    233:        int req = (ps->ps_flags & PSF_STEP) ? PT_STEP : PT_CONTINUE;
                    234:
                    235:        if (ps->ps_state != STOPPED) {
                    236:                fprintf(stderr, "Process not loaded and stopped %d\n",
                    237:                    ps->ps_state);
                    238:                return (0);
                    239:        }
                    240:
                    241:        /* Catch SIGINT and SIGTRAP, pass all other signals. */
                    242:        switch (ps->ps_signum) {
                    243:        case SIGINT:
                    244:        case SIGTRAP:
                    245:                signum = 0;
                    246:                break;
                    247:        default:
                    248:                signum = ps->ps_signum;
                    249:                break;
                    250:        }
                    251:
                    252:        if (ptrace(req, ps->ps_pid, (caddr_t)ps->ps_npc, signum) != 0) {
                    253:                err(1, "ptrace(%s)", req == PT_STEP ? "PT_STEP":"PT_CONTINUE");
                    254:        }
                    255:
                    256:        ps->ps_state = RUNNING;
                    257:        ps->ps_npc = 1;
                    258:
                    259:        return (1);
1.10      art       260: }
                    261:
                    262: int
                    263: cmd_process_setenv(int argc, char **argv, void *arg)
                    264: {
                    265:        if (setenv(argv[1], argv[2], 1))
                    266:                err(1, "setenv");
                    267:
                    268:        return (0);
1.1       art       269: }