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