]>
Commit | Line | Data |
---|---|---|
7053c1a5 JK |
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 "/usr/include/sys/wait.h" | |
22 | #include "frame.h" | |
23 | #include "inferior.h" | |
7053c1a5 JK |
24 | |
25 | #include <stdio.h> | |
26 | #include <sys/param.h> | |
27 | #include <sys/dir.h> | |
28 | #include <sys/user.h> | |
29 | #include <signal.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sgtty.h> | |
32 | #include <fcntl.h> | |
33 | ||
34 | /***************Begin MY defs*********************/ | |
35 | int quit_flag = 0; | |
36 | char registers[REGISTER_BYTES]; | |
37 | ||
38 | /* Index within `registers' of the first byte of the space for | |
39 | register N. */ | |
40 | ||
41 | ||
42 | char buf2[MAX_REGISTER_RAW_SIZE]; | |
43 | /***************End MY defs*********************/ | |
44 | ||
45 | #include <sys/ptrace.h> | |
46 | #include <machine/reg.h> | |
47 | ||
48 | extern int sys_nerr; | |
49 | extern char **sys_errlist; | |
50 | extern char **environ; | |
51 | extern int errno; | |
52 | extern int inferior_pid; | |
53 | void error (), quit (), perror_with_name (); | |
54 | int query (); | |
55 | ||
56 | /* Start an inferior process and returns its pid. | |
57 | ALLARGS is a vector of program-name and args. | |
58 | ENV is the environment vector to pass. */ | |
59 | ||
60 | int | |
61 | create_inferior (program, allargs) | |
62 | char *program; | |
63 | char **allargs; | |
64 | { | |
65 | int pid; | |
66 | ||
67 | pid = fork (); | |
68 | if (pid < 0) | |
69 | perror_with_name ("fork"); | |
70 | ||
71 | if (pid == 0) | |
72 | { | |
73 | ptrace (PTRACE_TRACEME); | |
74 | ||
75 | execv (program, allargs); | |
76 | ||
77 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
78 | errno < sys_nerr ? sys_errlist[errno] : "unknown error"); | |
79 | fflush (stderr); | |
80 | _exit (0177); | |
81 | } | |
82 | ||
83 | return pid; | |
84 | } | |
85 | ||
86 | /* Kill the inferior process. Make us have no inferior. */ | |
87 | ||
88 | void | |
89 | kill_inferior () | |
90 | { | |
91 | if (inferior_pid == 0) | |
92 | return; | |
93 | ptrace (8, inferior_pid, 0, 0); | |
94 | wait (0); | |
95 | /*************inferior_died ();****VK**************/ | |
96 | } | |
97 | ||
98 | /* Wait for process, returns status */ | |
99 | ||
100 | unsigned char | |
101 | mywait (status) | |
102 | char *status; | |
103 | { | |
104 | int pid; | |
105 | union wait w; | |
106 | ||
107 | pid = wait (&w); | |
108 | if (pid != inferior_pid) | |
109 | perror_with_name ("wait"); | |
110 | ||
111 | if (WIFEXITED (w)) | |
112 | { | |
113 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
114 | *status = 'E'; | |
115 | return ((unsigned char) WEXITSTATUS (w)); | |
116 | } | |
117 | else if (!WIFSTOPPED (w)) | |
118 | { | |
119 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
120 | *status = 'T'; | |
121 | return ((unsigned char) WTERMSIG (w)); | |
122 | } | |
123 | ||
124 | fetch_inferior_registers (0); | |
125 | ||
126 | *status = 'S'; | |
127 | return ((unsigned char) WSTOPSIG (w)); | |
128 | } | |
129 | ||
130 | /* Resume execution of the inferior process. | |
131 | If STEP is nonzero, single-step it. | |
132 | If SIGNAL is nonzero, give it that signal. */ | |
133 | ||
134 | void | |
135 | myresume (step, signal) | |
136 | int step; | |
137 | int signal; | |
138 | { | |
139 | errno = 0; | |
140 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
141 | if (errno) | |
142 | perror_with_name ("ptrace"); | |
143 | } | |
144 | ||
145 | /* Fetch one or more registers from the inferior. REGNO == -1 to get | |
146 | them all. We actually fetch more than requested, when convenient, | |
147 | marking them as valid so we won't fetch them again. */ | |
148 | ||
149 | void | |
150 | fetch_inferior_registers (ignored) | |
151 | int ignored; | |
152 | { | |
153 | struct regs inferior_registers; | |
154 | struct fp_status inferior_fp_registers; | |
7053c1a5 | 155 | |
5b3c73e6 JK |
156 | ptrace (PTRACE_GETREGS, inferior_pid, |
157 | (PTRACE_ARG3_TYPE) &inferior_registers); | |
158 | #ifdef FP0_REGNUM | |
159 | ptrace (PTRACE_GETFPREGS, inferior_pid, | |
160 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); | |
161 | #endif | |
162 | ||
163 | memcpy (registers, &inferior_registers, 16 * 4); | |
164 | #ifdef FP0_REGNUM | |
7053c1a5 | 165 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, |
5b3c73e6 JK |
166 | sizeof inferior_fp_registers.fps_regs); |
167 | #endif | |
168 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; | |
169 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; | |
170 | #ifdef FP0_REGNUM | |
171 | memcpy | |
172 | (®isters[REGISTER_BYTE (FPC_REGNUM)], | |
173 | &inferior_fp_registers.fps_control, | |
174 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs); | |
175 | #endif | |
7053c1a5 JK |
176 | } |
177 | ||
178 | /* Store our register values back into the inferior. | |
179 | If REGNO is -1, do this for all registers. | |
180 | Otherwise, REGNO specifies which register (so we can save time). */ | |
181 | ||
182 | void | |
183 | store_inferior_registers (ignored) | |
184 | int ignored; | |
185 | { | |
186 | struct regs inferior_registers; | |
187 | struct fp_status inferior_fp_registers; | |
7053c1a5 | 188 | |
5b3c73e6 JK |
189 | bcopy (registers, &inferior_registers, 16 * 4); |
190 | #ifdef FP0_REGNUM | |
191 | bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, | |
192 | sizeof inferior_fp_registers.fps_regs); | |
193 | #endif | |
194 | inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)]; | |
195 | inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)]; | |
196 | ||
197 | #ifdef FP0_REGNUM | |
198 | bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)], | |
199 | &inferior_fp_registers.fps_control, | |
200 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs); | |
201 | #endif | |
202 | ||
203 | ptrace (PTRACE_SETREGS, inferior_pid, | |
204 | (PTRACE_ARG3_TYPE) &inferior_registers); | |
205 | #if FP0_REGNUM | |
206 | ptrace (PTRACE_SETFPREGS, inferior_pid, | |
207 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); | |
208 | #endif | |
7053c1a5 JK |
209 | } |
210 | ||
211 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
212 | in the NEW_SUN_PTRACE case. | |
213 | It ought to be straightforward. But it appears that writing did | |
214 | not write the data that I specified. I cannot understand where | |
215 | it got the data that it actually did write. */ | |
216 | ||
217 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
218 | to debugger memory starting at MYADDR. */ | |
219 | ||
220 | read_inferior_memory (memaddr, myaddr, len) | |
221 | CORE_ADDR memaddr; | |
222 | char *myaddr; | |
223 | int len; | |
224 | { | |
225 | register int i; | |
226 | /* Round starting address down to longword boundary. */ | |
227 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
228 | /* Round ending address up; get number of longwords that makes. */ | |
229 | register int count | |
230 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
231 | /* Allocate buffer of that many longwords. */ | |
232 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
233 | ||
234 | /* Read all the longwords */ | |
235 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
236 | { | |
237 | buffer[i] = ptrace (1, inferior_pid, addr, 0); | |
238 | } | |
239 | ||
240 | /* Copy appropriate bytes out of the buffer. */ | |
241 | bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
242 | } | |
243 | ||
244 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
245 | to inferior's memory at MEMADDR. | |
246 | On failure (cannot write the inferior) | |
247 | returns the value of errno. */ | |
248 | ||
249 | int | |
250 | write_inferior_memory (memaddr, myaddr, len) | |
251 | CORE_ADDR memaddr; | |
252 | char *myaddr; | |
253 | int len; | |
254 | { | |
255 | register int i; | |
256 | /* Round starting address down to longword boundary. */ | |
257 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
258 | /* Round ending address up; get number of longwords that makes. */ | |
259 | register int count | |
260 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
261 | /* Allocate buffer of that many longwords. */ | |
262 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
263 | extern int errno; | |
264 | ||
265 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
266 | ||
267 | buffer[0] = ptrace (1, inferior_pid, addr, 0); | |
268 | ||
269 | if (count > 1) | |
270 | { | |
271 | buffer[count - 1] | |
272 | = ptrace (1, inferior_pid, | |
273 | addr + (count - 1) * sizeof (int), 0); | |
274 | } | |
275 | ||
276 | /* Copy data to be written over corresponding part of buffer */ | |
277 | ||
278 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
279 | ||
280 | /* Write the entire buffer. */ | |
281 | ||
282 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
283 | { | |
284 | errno = 0; | |
285 | ptrace (4, inferior_pid, addr, buffer[i]); | |
286 | if (errno) | |
287 | return errno; | |
288 | } | |
289 | ||
290 | return 0; | |
291 | } | |
292 | \f | |
293 | void | |
294 | initialize () | |
295 | { | |
296 | inferior_pid = 0; | |
297 | } | |
298 | ||
299 | int | |
300 | have_inferior_p () | |
301 | { | |
302 | return inferior_pid != 0; | |
303 | } |