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