Annotation of src/usr.bin/pmdb/process.c, Revision 1.7
1.7 ! art 1: /* $OpenBSD: process.c,v 1.6 2002/07/22 01:20:50 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));
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);
1.7 ! art 172: } else if (ps->ps_flags & PSF_CORE) {
1.6 art 173: memcpy(r, ps->ps_core->regs, sizeof(*r));
1.7 ! art 174: } else
1.6 art 175: return (-1);
176:
1.7 ! art 177: return (0);
1.6 art 178: }
179:
180: int
1.1 art 181: cmd_process_kill(int argc, char **argv, void *arg)
182: {
183: struct pstate *ps = arg;
184:
185: process_kill(ps);
186:
1.5 fgsch 187: return (1);
1.1 art 188: }
189:
190: int
191: process_bkpt_main(struct pstate *ps, void *arg)
192: {
193: sym_update(ps);
194:
1.5 fgsch 195: return (BKPT_DEL_CONT);
1.1 art 196: }
197:
198: int
199: cmd_process_run(int argc, char **argv, void *arg)
200: {
201: struct pstate *ps = arg;
202:
203: if (ps->ps_state == NONE) {
204: reg main_addr;
205:
206: process_load(ps);
207: if (sym_lookup(ps, "main", &main_addr))
208: warnx("no main");
209: else if (bkpt_add_cb(ps, main_addr, process_bkpt_main, NULL))
210: warn("no bkpt at main 0x%lx", main_addr);
211: }
212:
213: if (ps->ps_state != LOADED) {
214: fprintf(stderr, "Process already running.\n");
1.5 fgsch 215: return (0);
1.1 art 216: }
217:
1.6 art 218: process_run(ps);
1.1 art 219: /*
220: * XXX - there isn't really any difference between STOPPED and
221: * LOADED, we should probably get rid of one.
222: */
223: ps->ps_state = STOPPED;
224: ps->ps_signum = 0;
225:
226: return (cmd_process_cont(argc, argv, arg));
227: }
228:
229: int
230: cmd_process_cont(int argc, char **argv, void *arg)
231: {
232: struct pstate *ps = arg;
233: int signum;
234: int req = (ps->ps_flags & PSF_STEP) ? PT_STEP : PT_CONTINUE;
235:
236: if (ps->ps_state != STOPPED) {
237: fprintf(stderr, "Process not loaded and stopped %d\n",
238: ps->ps_state);
239: return (0);
240: }
241:
242: /* Catch SIGINT and SIGTRAP, pass all other signals. */
243: switch (ps->ps_signum) {
244: case SIGINT:
245: case SIGTRAP:
246: signum = 0;
247: break;
248: default:
249: signum = ps->ps_signum;
250: break;
251: }
252:
253: if (ptrace(req, ps->ps_pid, (caddr_t)ps->ps_npc, signum) != 0) {
254: err(1, "ptrace(%s)", req == PT_STEP ? "PT_STEP":"PT_CONTINUE");
255: }
256:
257: ps->ps_state = RUNNING;
258: ps->ps_npc = 1;
259:
260: return (1);
261: }