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