* Rename remote-es1800.c to remote-es.c
[deliverable/binutils-gdb.git] / gdb / gdbserver / remote-inflow.c
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 /***************************
24 #include "initialize.h"
25 ****************************/
26
27 #include <stdio.h>
28 #include <sys/param.h>
29 #include <sys/dir.h>
30 /*#include <sys/user.h>*/
31 #define LYNXOS
32 #include <sys/mem.h>
33 #include <sys/signal.h>
34 #include <sys/file.h>
35 #include <sys/kernel.h>
36 #include <sys/itimer.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/proc.h>
40 #include <signal.h>
41 #include <sys/ioctl.h>
42 #include <sgtty.h>
43 #include <fcntl.h>
44 #include "/usr/include/wait.h"
45
46 /***************Begin MY defs*********************/
47 int quit_flag = 0;
48 char registers[REGISTER_BYTES];
49
50 /* Index within `registers' of the first byte of the space for
51 register N. */
52
53
54 char buf2[MAX_REGISTER_RAW_SIZE];
55 /***************End MY defs*********************/
56
57 #include <sys/ptrace.h>
58 /*#include <machine/reg.h>*/
59
60 extern char **environ;
61 extern int errno;
62 extern int inferior_pid;
63 void error (), quit (), perror_with_name ();
64 int query ();
65
66 /* Start an inferior process and returns its pid.
67 ALLARGS is a vector of program-name and args.
68 ENV is the environment vector to pass. */
69
70 int
71 create_inferior (program, allargs)
72 char *program;
73 char **allargs;
74 {
75 int pid;
76
77 pid = fork ();
78 if (pid < 0)
79 perror_with_name ("fork");
80
81 if (pid == 0)
82 {
83 ptrace (PTRACE_TRACEME);
84
85 execv (program, allargs);
86
87 fprintf (stderr, "Cannot exec %s: %s.\n", program,
88 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
89 fflush (stderr);
90 _exit (0177);
91 }
92
93 return pid;
94 }
95
96 /* Kill the inferior process. Make us have no inferior. */
97
98 void
99 kill_inferior ()
100 {
101 if (inferior_pid == 0)
102 return;
103 ptrace (8, inferior_pid, 0, 0);
104 wait (0);
105 /*************inferior_died ();****VK**************/
106 }
107
108 /* Wait for process, returns status */
109
110 unsigned char
111 mywait (status)
112 char *status;
113 {
114 int pid;
115 union wait w;
116
117 pid = wait (&w);
118 if (pid != PIDGET(inferior_pid))
119 perror_with_name ("wait");
120
121 inferior_pid = BUILDPID (inferior_pid, w.w_tid);
122
123 if (WIFEXITED (w))
124 {
125 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
126 *status = 'E';
127 return ((unsigned char) WEXITSTATUS (w));
128 }
129 else if (!WIFSTOPPED (w))
130 {
131 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
132 *status = 'T';
133 return ((unsigned char) WTERMSIG (w));
134 }
135
136 fetch_inferior_registers (0);
137
138 *status = 'S';
139 return ((unsigned char) WSTOPSIG (w));
140 }
141
142 /* Resume execution of the inferior process.
143 If STEP is nonzero, single-step it.
144 If SIGNAL is nonzero, give it that signal. */
145
146 void
147 myresume (step, signal)
148 int step;
149 int signal;
150 {
151 errno = 0;
152 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
153 if (errno)
154 perror_with_name ("ptrace");
155 }
156
157 #undef offsetof
158 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
159
160 static struct econtext *
161 lynx_registers_addr()
162 {
163 st_t *stblock;
164 int ecpoff = offsetof(st_t, ecp);
165 CORE_ADDR ecp;
166
167 errno = 0;
168 stblock = (st_t *) ptrace (PTRACE_THREADUSER, inferior_pid,
169 (PTRACE_ARG3_TYPE)0, 0);
170 if (errno)
171 perror_with_name ("PTRACE_THREADUSER");
172
173 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, inferior_pid,
174 (PTRACE_ARG3_TYPE)ecpoff, 0);
175 ecp -= (CORE_ADDR)stblock;
176 if (errno)
177 perror_with_name ("lynx_registers_addr(PTRACE_PEEKTHREAD)");
178
179 return (struct econtext *)ecp;
180 }
181
182 static struct econtext *ecp;
183
184 /* Mapping between GDB register #s and offsets into econtext. Must be
185 consistent with REGISTER_NAMES macro in tm-i386v.h. */
186
187 #define X(ENTRY)(offsetof(struct econtext, ENTRY) / 4)
188 static int regmap[] = {
189 X(eax),
190 X(ecx),
191 X(edx),
192 X(ebx),
193 X(esp),
194 X(ebp),
195 X(esi),
196 X(edi),
197 X(eip),
198 X(flags), /* ps */
199 X(cs),
200 X(ss),
201 X(ds),
202 X(es),
203 X(ecode), /* Lynx doesn't give us either fs or gs, so */
204 X(fault) /* we just substitute these two in the hopes
205 that they are useful. */
206 };
207
208 /* Fetch one or more registers from the inferior. REGNO == -1 to get
209 them all. We actually fetch more than requested, when convenient,
210 marking them as valid so we won't fetch them again. */
211
212 void
213 fetch_inferior_registers (ignored)
214 int ignored;
215 {
216 int regno;
217 unsigned long reg;
218 struct econtext *ecp;
219
220 ecp = lynx_registers_addr();
221
222 for (regno = 0; regno < NUM_REGS; regno++)
223 {
224 errno = 0;
225 reg = ptrace (PTRACE_PEEKTHREAD, inferior_pid,
226 (PTRACE_ARG3_TYPE) (&ecp->fault + regmap[regno]), 0);
227 if (errno)
228 perror_with_name ("fetch_inferior_registers(PTRACE_PEEKTHREAD)");
229
230 *(unsigned long *)&registers[REGISTER_BYTE (regno)] = reg;
231 }
232 }
233
234 /* Store our register values back into the inferior.
235 If REGNO is -1, do this for all registers.
236 Otherwise, REGNO specifies which register (so we can save time). */
237
238 void
239 store_inferior_registers (ignored)
240 int ignored;
241 {
242 int regno;
243 unsigned long reg;
244 struct econtext *ecp;
245
246 ecp = lynx_registers_addr();
247
248 for (regno = 0; regno < NUM_REGS; regno++)
249 {
250 reg = *(unsigned long *)&registers[REGISTER_BYTE (regno)];
251
252 errno = 0;
253 ptrace (PTRACE_POKEUSER, inferior_pid,
254 (PTRACE_ARG3_TYPE) (&ecp->fault + regmap[regno]), reg);
255 if (errno)
256 perror_with_name ("PTRACE_POKEUSER");
257 }
258 }
259
260 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
261 in the NEW_SUN_PTRACE case.
262 It ought to be straightforward. But it appears that writing did
263 not write the data that I specified. I cannot understand where
264 it got the data that it actually did write. */
265
266 /* Copy LEN bytes from inferior's memory starting at MEMADDR
267 to debugger memory starting at MYADDR. */
268
269 read_inferior_memory (memaddr, myaddr, len)
270 CORE_ADDR memaddr;
271 char *myaddr;
272 int len;
273 {
274 register int i;
275 /* Round starting address down to longword boundary. */
276 register CORE_ADDR addr = memaddr & -sizeof (int);
277 /* Round ending address up; get number of longwords that makes. */
278 register int count
279 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
280 /* Allocate buffer of that many longwords. */
281 register int *buffer = (int *) alloca (count * sizeof (int));
282
283 /* Read all the longwords */
284 for (i = 0; i < count; i++, addr += sizeof (int))
285 {
286 buffer[i] = ptrace (1, inferior_pid, addr, 0);
287 }
288
289 /* Copy appropriate bytes out of the buffer. */
290 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
291 }
292
293 /* Copy LEN bytes of data from debugger memory at MYADDR
294 to inferior's memory at MEMADDR.
295 On failure (cannot write the inferior)
296 returns the value of errno. */
297
298 int
299 write_inferior_memory (memaddr, myaddr, len)
300 CORE_ADDR memaddr;
301 char *myaddr;
302 int len;
303 {
304 register int i;
305 /* Round starting address down to longword boundary. */
306 register CORE_ADDR addr = memaddr & -sizeof (int);
307 /* Round ending address up; get number of longwords that makes. */
308 register int count
309 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
310 /* Allocate buffer of that many longwords. */
311 register int *buffer = (int *) alloca (count * sizeof (int));
312 extern int errno;
313
314 /* Fill start and end extra bytes of buffer with existing memory data. */
315
316 buffer[0] = ptrace (1, inferior_pid, addr, 0);
317
318 if (count > 1)
319 {
320 buffer[count - 1]
321 = ptrace (1, inferior_pid,
322 addr + (count - 1) * sizeof (int), 0);
323 }
324
325 /* Copy data to be written over corresponding part of buffer */
326
327 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
328
329 /* Write the entire buffer. */
330
331 for (i = 0; i < count; i++, addr += sizeof (int))
332 {
333 errno = 0;
334 ptrace (4, inferior_pid, addr, buffer[i]);
335 if (errno)
336 return errno;
337 }
338
339 return 0;
340 }
341 \f
342 void
343 initialize ()
344 {
345 inferior_pid = 0;
346 }
347
348 int
349 have_inferior_p ()
350 {
351 return inferior_pid != 0;
352 }
This page took 0.04259 seconds and 4 git commands to generate.