1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* $OpenBSD: process.c,v 1.10 2002/08/08 18:27:57 art Exp $ */
/*
* Copyright (c) 2002 Artur Grabowski <art@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <machine/reg.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pmdb.h"
#include "core.h"
#include "symbol.h"
#include "break.h"
int
process_load(struct pstate *ps)
{
if (ps->ps_state == LOADED)
return (0);
if (access(*ps->ps_argv, R_OK|X_OK) < 0) {
fprintf(stderr, "%s: %s.\n", *ps->ps_argv,
strerror(errno));
return (0);
}
if (stat(ps->ps_argv[0], &(ps->exec_stat)) < 0)
err(1, "stat()");
if ((ps->ps_flags & PSF_SYMBOLS) == 0) {
sym_init_exec(ps, ps->ps_argv[0]);
ps->ps_flags |= PSF_SYMBOLS;
}
ps->ps_state = LOADED;
if (ps->ps_pid != 0) {
/* attach to an already running process */
if (ptrace(PT_ATTACH, ps->ps_pid, (caddr_t) 0, 0) < 0)
err(1, "failed to ptrace process");
ps->ps_state = STOPPED;
ps->ps_flags |= PSF_ATCH;
}
return (0);
}
int
process_run(struct pstate *ps)
{
int status;
if ((ps->ps_state == RUNNING) || (ps->ps_state == STOPPED)) {
warnx("process is already running");
return 0;
}
switch (ps->ps_pid = fork()) {
case 0:
if (ptrace(PT_TRACE_ME, getpid(), NULL, 0) != 0)
err(1, "ptrace(PT_TRACE_ME)");
execvp(*ps->ps_argv, ps->ps_argv);
err(1, "exec");
/* NOTREACHED */
case -1:
err(1, "fork");
/* NOTREACHED */
default:
warnx("process started with PID %d", ps->ps_pid);
break;
}
ps->ps_state = LOADED;
if (wait(&status) == 0)
err(1, "wait");
return (0);
}
int
process_kill(struct pstate *ps)
{
switch(ps->ps_state) {
case RUNNING:
case STOPPED:
if (ptrace(PT_KILL, ps->ps_pid, NULL, 0) != 0)
err(1, "ptrace(PT_KILL)");
return (1);
default:
return (0);
}
}
int
process_read(struct pstate *ps, off_t from, void *to, size_t size)
{
struct ptrace_io_desc piod;
if (((ps->ps_state == NONE) || (ps->ps_state == LOADED) ||
(ps->ps_state == TERMINATED)) && (ps->ps_flags & PSF_CORE)) {
return core_read(ps, from, to, size);
} else {
piod.piod_op = PIOD_READ_D;
piod.piod_offs = (void *)(long)from;
piod.piod_addr = to;
piod.piod_len = size;
return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
}
}
int
process_write(struct pstate *ps, off_t to, void *from, size_t size)
{
struct ptrace_io_desc piod;
if ((ps->ps_state == NONE) && (ps->ps_flags & PSF_CORE)) {
return core_write(ps, to, from, size);
} else {
piod.piod_op = PIOD_WRITE_D;
piod.piod_offs = (void *)(long)to;
piod.piod_addr = from;
piod.piod_len = size;
return (ptrace(PT_IO, ps->ps_pid, (caddr_t)&piod, 0));
}
}
int
process_getregs(struct pstate *ps, struct reg *r)
{
if (ps->ps_flags & PSF_CORE) {
memcpy(r, ps->ps_core->regs, sizeof(*r));
return (0);
}
return (ptrace(PT_GETREGS, ps->ps_pid, (caddr_t)r, 0));
}
int
cmd_process_kill(int argc, char **argv, void *arg)
{
struct pstate *ps = arg;
process_kill(ps);
return (1);
}
int
process_bkpt_main(struct pstate *ps, void *arg)
{
sym_update(ps);
return (BKPT_DEL_CONT);
}
int
cmd_process_run(int argc, char **argv, void *arg)
{
struct pstate *ps = arg;
if (ps->ps_state == NONE) {
reg main_addr;
process_load(ps);
if (sym_lookup(ps, "main", &main_addr))
warnx("no main");
else if (bkpt_add_cb(ps, main_addr, process_bkpt_main, NULL))
warn("no bkpt at main 0x%lx", main_addr);
}
if (ps->ps_state != LOADED) {
fprintf(stderr, "Process already running.\n");
return (0);
}
process_run(ps);
/*
* XXX - there isn't really any difference between STOPPED and
* LOADED, we should probably get rid of one.
*/
ps->ps_state = STOPPED;
ps->ps_signum = 0;
return (cmd_process_cont(argc, argv, arg));
}
int
cmd_process_cont(int argc, char **argv, void *arg)
{
struct pstate *ps = arg;
int signum;
int req = (ps->ps_flags & PSF_STEP) ? PT_STEP : PT_CONTINUE;
if (ps->ps_state != STOPPED) {
fprintf(stderr, "Process not loaded and stopped %d\n",
ps->ps_state);
return (0);
}
/* Catch SIGINT and SIGTRAP, pass all other signals. */
switch (ps->ps_signum) {
case SIGINT:
case SIGTRAP:
signum = 0;
break;
default:
signum = ps->ps_signum;
break;
}
if (ptrace(req, ps->ps_pid, (caddr_t)ps->ps_npc, signum) != 0) {
err(1, "ptrace(%s)", req == PT_STEP ? "PT_STEP":"PT_CONTINUE");
}
ps->ps_state = RUNNING;
ps->ps_npc = 1;
return (1);
}
int
cmd_process_setenv(int argc, char **argv, void *arg)
{
if (setenv(argv[1], argv[2], 1))
err(1, "setenv");
return (0);
}
|