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

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