Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level interface to ptrace, for the remote server for GDB. |
b6ba6518 | 2 | Copyright 1995, 1996, 1999, 2000, 2001 Free Software Foundation, Inc. |
c906108c | 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 | 20 | |
f29d9b6d | 21 | #include "server.h" |
c906108c SS |
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*********************/ | |
5c44784c JM |
36 | static char my_registers[REGISTER_BYTES]; |
37 | char *registers = my_registers; | |
c906108c SS |
38 | /***************End MY defs*********************/ |
39 | ||
40 | #include <sys/ptrace.h> | |
41 | #include <machine/reg.h> | |
42 | ||
c906108c | 43 | extern int errno; |
c906108c SS |
44 | |
45 | /* Start an inferior process and returns its pid. | |
bd2fa4f6 | 46 | ALLARGS is a vector of program-name and args. */ |
c906108c SS |
47 | |
48 | int | |
fba45db2 | 49 | create_inferior (char *program, char **allargs) |
c906108c SS |
50 | { |
51 | int pid; | |
52 | ||
53 | pid = fork (); | |
54 | if (pid < 0) | |
55 | perror_with_name ("fork"); | |
56 | ||
57 | if (pid == 0) | |
58 | { | |
59 | ptrace (PT_TRACE_ME, 0, 0, 0, 0); | |
60 | ||
61 | execv (program, allargs); | |
62 | ||
63 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
64 | errno < sys_nerr ? sys_errlist[errno] : "unknown error"); | |
65 | fflush (stderr); | |
66 | _exit (0177); | |
67 | } | |
68 | ||
69 | return pid; | |
70 | } | |
71 | ||
72 | /* Kill the inferior process. Make us have no inferior. */ | |
73 | ||
74 | void | |
fba45db2 | 75 | kill_inferior (void) |
c906108c SS |
76 | { |
77 | if (inferior_pid == 0) | |
78 | return; | |
79 | ptrace (8, inferior_pid, 0, 0, 0); | |
80 | wait (0); | |
c5aa993b | 81 | /*************inferior_died ();****VK**************/ |
c906108c SS |
82 | } |
83 | ||
84 | /* Return nonzero if the given thread is still alive. */ | |
85 | int | |
fba45db2 | 86 | mythread_alive (int pid) |
c906108c SS |
87 | { |
88 | return 1; | |
89 | } | |
90 | ||
91 | /* Wait for process, returns status */ | |
92 | ||
93 | unsigned char | |
fba45db2 | 94 | mywait (char *status) |
c906108c SS |
95 | { |
96 | int pid; | |
97 | union wait w; | |
98 | ||
99 | pid = wait (&w); | |
100 | if (pid != inferior_pid) | |
101 | perror_with_name ("wait"); | |
102 | ||
103 | if (WIFEXITED (w)) | |
104 | { | |
105 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
106 | *status = 'W'; | |
107 | return ((unsigned char) WEXITSTATUS (w)); | |
108 | } | |
109 | else if (!WIFSTOPPED (w)) | |
110 | { | |
111 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
112 | *status = 'X'; | |
113 | return ((unsigned char) WTERMSIG (w)); | |
114 | } | |
115 | ||
116 | fetch_inferior_registers (0); | |
117 | ||
118 | *status = 'T'; | |
119 | return ((unsigned char) WSTOPSIG (w)); | |
120 | } | |
121 | ||
122 | /* Resume execution of the inferior process. | |
123 | If STEP is nonzero, single-step it. | |
124 | If SIGNAL is nonzero, give it that signal. */ | |
125 | ||
126 | void | |
fba45db2 | 127 | myresume (int step, int signal) |
c906108c SS |
128 | { |
129 | errno = 0; | |
130 | ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid, 1, signal, 0); | |
131 | if (errno) | |
132 | perror_with_name ("ptrace"); | |
133 | } | |
134 | ||
135 | ||
136 | #if !defined (offsetof) | |
137 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
138 | #endif | |
139 | ||
140 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
141 | #if !defined (U_REGS_OFFSET) | |
142 | #define U_REGS_OFFSET \ | |
143 | ptrace (PT_READ_U, inferior_pid, \ | |
144 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ | |
145 | - KERNEL_U_ADDR | |
146 | #endif | |
147 | ||
148 | CORE_ADDR | |
fba45db2 | 149 | register_addr (int regno, CORE_ADDR blockend) |
c906108c SS |
150 | { |
151 | CORE_ADDR addr; | |
152 | ||
a728f042 | 153 | if (regno < 0 || regno >= NUM_REGS) |
c906108c SS |
154 | error ("Invalid register number %d.", regno); |
155 | ||
156 | REGISTER_U_ADDR (addr, blockend, regno); | |
157 | ||
158 | return addr; | |
159 | } | |
160 | ||
161 | /* Fetch one register. */ | |
162 | ||
163 | static void | |
fba45db2 | 164 | fetch_register (int regno) |
c906108c SS |
165 | { |
166 | register unsigned int regaddr; | |
167 | char buf[MAX_REGISTER_RAW_SIZE]; | |
168 | register int i; | |
169 | ||
170 | /* Offset of registers within the u area. */ | |
171 | unsigned int offset; | |
172 | ||
173 | offset = U_REGS_OFFSET; | |
174 | ||
175 | regaddr = register_addr (regno, offset); | |
176 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
177 | { | |
178 | errno = 0; | |
c5aa993b JM |
179 | *(int *) ®isters[regno * 4 + i] = ptrace (PT_RUREGS, inferior_pid, |
180 | (PTRACE_ARG3_TYPE) regaddr, 0, 0); | |
c906108c SS |
181 | regaddr += sizeof (int); |
182 | if (errno != 0) | |
183 | { | |
184 | /* Warning, not error, in case we are attached; sometimes the | |
185 | kernel doesn't let us at the registers. */ | |
186 | char *err = strerror (errno); | |
187 | char *msg = alloca (strlen (err) + 128); | |
188 | sprintf (msg, "reading register %d: %s", regno, err); | |
189 | error (msg); | |
190 | goto error_exit; | |
191 | } | |
192 | } | |
c5aa993b | 193 | error_exit:; |
c906108c SS |
194 | } |
195 | ||
196 | /* Fetch all registers, or just one, from the child process. */ | |
197 | ||
198 | void | |
fba45db2 | 199 | fetch_inferior_registers (int regno) |
c906108c SS |
200 | { |
201 | if (regno == -1 || regno == 0) | |
202 | for (regno = 0; regno < NUM_REGS; regno++) | |
203 | fetch_register (regno); | |
204 | else | |
205 | fetch_register (regno); | |
206 | } | |
207 | ||
208 | /* Store our register values back into the inferior. | |
209 | If REGNO is -1, do this for all registers. | |
210 | Otherwise, REGNO specifies which register (so we can save time). */ | |
211 | ||
212 | void | |
fba45db2 | 213 | store_inferior_registers (int regno) |
c906108c SS |
214 | { |
215 | register unsigned int regaddr; | |
216 | char buf[80]; | |
217 | extern char registers[]; | |
218 | register int i; | |
219 | unsigned int offset = U_REGS_OFFSET; | |
220 | int scratch; | |
221 | ||
222 | if (regno >= 0) | |
223 | { | |
224 | if (CANNOT_STORE_REGISTER (regno)) | |
225 | return; | |
226 | regaddr = register_addr (regno, offset); | |
227 | errno = 0; | |
228 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
c5aa993b JM |
229 | { |
230 | scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3; | |
231 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
232 | scratch, 0); | |
233 | if (errno != 0) | |
234 | { | |
c906108c | 235 | /* Error, even if attached. Failing to write these two |
c5aa993b JM |
236 | registers is pretty serious. */ |
237 | sprintf (buf, "writing register number %d", regno); | |
238 | perror_with_name (buf); | |
239 | } | |
240 | } | |
c906108c | 241 | else |
c5aa993b | 242 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) |
c906108c SS |
243 | { |
244 | errno = 0; | |
245 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
246 | *(int *) ®isters[REGISTER_BYTE (regno) + i], 0); | |
247 | if (errno != 0) | |
248 | { | |
249 | /* Warning, not error, in case we are attached; sometimes the | |
250 | kernel doesn't let us at the registers. */ | |
251 | char *err = strerror (errno); | |
252 | char *msg = alloca (strlen (err) + 128); | |
253 | sprintf (msg, "writing register %d: %s", | |
254 | regno, err); | |
255 | error (msg); | |
256 | return; | |
257 | } | |
c5aa993b | 258 | regaddr += sizeof (int); |
c906108c SS |
259 | } |
260 | } | |
261 | else | |
262 | for (regno = 0; regno < NUM_REGS; regno++) | |
263 | store_inferior_registers (regno); | |
264 | } | |
265 | ||
266 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
267 | in the NEW_SUN_PTRACE case. | |
268 | It ought to be straightforward. But it appears that writing did | |
269 | not write the data that I specified. I cannot understand where | |
270 | it got the data that it actually did write. */ | |
271 | ||
272 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
273 | to debugger memory starting at MYADDR. */ | |
274 | ||
fba45db2 | 275 | read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
276 | { |
277 | register int i; | |
278 | /* Round starting address down to longword boundary. */ | |
279 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
280 | /* Round ending address up; get number of longwords that makes. */ | |
281 | register int count | |
282 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
283 | /* Allocate buffer of that many longwords. */ | |
284 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
285 | ||
286 | /* Read all the longwords */ | |
287 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
288 | { | |
289 | buffer[i] = ptrace (1, inferior_pid, addr, 0, 0); | |
290 | } | |
291 | ||
292 | /* Copy appropriate bytes out of the buffer. */ | |
293 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
294 | } | |
295 | ||
296 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
297 | to inferior's memory at MEMADDR. | |
298 | On failure (cannot write the inferior) | |
299 | returns the value of errno. */ | |
300 | ||
301 | int | |
fba45db2 | 302 | write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
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, 0); | |
317 | ||
318 | if (count > 1) | |
319 | { | |
320 | buffer[count - 1] | |
321 | = ptrace (1, inferior_pid, | |
322 | addr + (count - 1) * sizeof (int), 0, 0); | |
323 | } | |
324 | ||
325 | /* Copy data to be written over corresponding part of buffer */ | |
326 | ||
327 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, 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], 0); | |
335 | if (errno) | |
336 | return errno; | |
337 | } | |
338 | ||
339 | return 0; | |
340 | } | |
341 | \f | |
342 | void | |
fba45db2 | 343 | initialize_low (void) |
c906108c | 344 | { |
c906108c | 345 | } |