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