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

1.1       art         1: /*     $PMDB: process.c,v 1.19 2002/03/11 23:39:49 art Exp $   */
                      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>
                     30: #include <stdlib.h>
                     31: #include <stdio.h>
                     32: #include <signal.h>
                     33: #include <unistd.h>
                     34: #include <err.h>
                     35:
                     36: #include "pmdb.h"
                     37: #include "symbol.h"
                     38: #include "break.h"
                     39:
                     40: int
                     41: process_load(struct pstate *ps)
                     42: {
                     43:        int status;
                     44:
                     45:        if (ps->ps_state == LOADED)
                     46:                return (0);
                     47:
                     48:        switch (ps->ps_pid = fork()) {
                     49:        case 0:
                     50:                if (ptrace(PT_TRACE_ME, getpid(), NULL, 0) != 0)
                     51:                        err(1, "ptrace(PT_TRACE_ME)");
                     52:                execvp(*ps->ps_argv, ps->ps_argv);
                     53:                err(1, "exec");
                     54:                /* NOTREACHED */
                     55:        case -1:
                     56:                err(1, "fork");
                     57:                /* NOTREACHED */
                     58:        default:
                     59:                break;
                     60:        }
                     61:
                     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:        if (wait(&status) == 0)
                     68:                err(1, "wait");
                     69:
                     70:        ps->ps_state = LOADED;
                     71:        return 0;
                     72: }
                     73:
                     74: int
                     75: process_kill(struct pstate *ps)
                     76: {
                     77:        switch(ps->ps_state) {
                     78:        case LOADED:
                     79:        case RUNNING:
                     80:        case STOPPED:
                     81:                if (ptrace(PT_KILL, ps->ps_pid, NULL, 0) != 0)
                     82:                        err(1, "ptrace(PT_KILL)");
                     83:                return 1;
                     84:        default:
                     85:                return 0;
                     86:        }
                     87: }
                     88:
                     89: int
                     90: cmd_process_kill(int argc, char **argv, void *arg)
                     91: {
                     92:        struct pstate *ps = arg;
                     93:
                     94:        process_kill(ps);
                     95:
                     96:        return 1;
                     97: }
                     98:
                     99: int
                    100: process_bkpt_main(struct pstate *ps, void *arg)
                    101: {
                    102:        sym_update(ps);
                    103:
                    104:        return BKPT_DEL_CONT;
                    105: }
                    106:
                    107: int
                    108: cmd_process_run(int argc, char **argv, void *arg)
                    109: {
                    110:        struct pstate *ps = arg;
                    111:
                    112:        if (ps->ps_state == NONE) {
                    113:                reg main_addr;
                    114:
                    115:                process_load(ps);
                    116:                if (sym_lookup(ps, "main", &main_addr))
                    117:                        warnx("no main");
                    118:                else if (bkpt_add_cb(ps, main_addr, process_bkpt_main, NULL))
                    119:                        warn("no bkpt at main 0x%lx", main_addr);
                    120:        }
                    121:
                    122:        if (ps->ps_state != LOADED) {
                    123:                fprintf(stderr, "Process already running.\n");
                    124:                return 0;
                    125:        }
                    126:
                    127:        /*
                    128:         * XXX - there isn't really any difference between STOPPED and
                    129:         * LOADED, we should probably get rid of one.
                    130:         */
                    131:        ps->ps_state = STOPPED;
                    132:        ps->ps_signum = 0;
                    133:
                    134:        return (cmd_process_cont(argc, argv, arg));
                    135: }
                    136:
                    137: int
                    138: cmd_process_cont(int argc, char **argv, void *arg)
                    139: {
                    140:        struct pstate *ps = arg;
                    141:        int signum;
                    142:        int req = (ps->ps_flags & PSF_STEP) ? PT_STEP : PT_CONTINUE;
                    143:
                    144:        if (ps->ps_state != STOPPED) {
                    145:                fprintf(stderr, "Process not loaded and stopped %d\n",
                    146:                    ps->ps_state);
                    147:                return (0);
                    148:        }
                    149:
                    150:        /* Catch SIGINT and SIGTRAP, pass all other signals. */
                    151:        switch (ps->ps_signum) {
                    152:        case SIGINT:
                    153:        case SIGTRAP:
                    154:                signum = 0;
                    155:                break;
                    156:        default:
                    157:                signum = ps->ps_signum;
                    158:                break;
                    159:        }
                    160:
                    161:        if (ptrace(req, ps->ps_pid, (caddr_t)ps->ps_npc, signum) != 0) {
                    162:                err(1, "ptrace(%s)", req == PT_STEP ? "PT_STEP":"PT_CONTINUE");
                    163:        }
                    164:
                    165:        ps->ps_state = RUNNING;
                    166:        ps->ps_npc = 1;
                    167:
                    168:        return (1);
                    169: }