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