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 | ||
4 | This file is part of GDB. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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, Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include <sys/wait.h> | |
22 | #include "frame.h" | |
23 | #include "inferior.h" | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <sys/param.h> | |
27 | #include <sys/dir.h> | |
28 | #include <sys/user.h> | |
29 | #include <signal.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sgtty.h> | |
32 | #include <fcntl.h> | |
33 | ||
34 | /***************Begin MY defs*********************/ | |
35 | int quit_flag = 0; | |
36 | char registers[REGISTER_BYTES]; | |
37 | ||
38 | /* Index within `registers' of the first byte of the space for | |
39 | register N. */ | |
40 | ||
41 | ||
42 | char buf2[MAX_REGISTER_RAW_SIZE]; | |
43 | /***************End MY defs*********************/ | |
44 | ||
45 | #include <sys/ptrace.h> | |
46 | #include <machine/reg.h> | |
47 | ||
48 | extern char **environ; | |
49 | extern int errno; | |
50 | extern int inferior_pid; | |
51 | void quit (), perror_with_name (); | |
52 | int query (); | |
53 | ||
54 | /* Start an inferior process and returns its pid. | |
55 | ALLARGS is a vector of program-name and args. | |
56 | ENV is the environment vector to pass. */ | |
57 | ||
58 | int | |
59 | create_inferior (program, allargs) | |
60 | char *program; | |
61 | char **allargs; | |
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 | |
87 | kill_inferior () | |
88 | { | |
89 | if (inferior_pid == 0) | |
90 | return; | |
91 | ptrace (8, inferior_pid, 0, 0, 0); | |
92 | wait (0); | |
93 | /*************inferior_died ();****VK**************/ | |
94 | } | |
95 | ||
96 | /* Return nonzero if the given thread is still alive. */ | |
97 | int | |
98 | mythread_alive (pid) | |
99 | int pid; | |
100 | { | |
101 | return 1; | |
102 | } | |
103 | ||
104 | /* Wait for process, returns status */ | |
105 | ||
106 | unsigned char | |
107 | mywait (status) | |
108 | char *status; | |
109 | { | |
110 | int pid; | |
111 | union wait w; | |
112 | ||
113 | pid = wait (&w); | |
114 | if (pid != inferior_pid) | |
115 | perror_with_name ("wait"); | |
116 | ||
117 | if (WIFEXITED (w)) | |
118 | { | |
119 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
120 | *status = 'W'; | |
121 | return ((unsigned char) WEXITSTATUS (w)); | |
122 | } | |
123 | else if (!WIFSTOPPED (w)) | |
124 | { | |
125 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
126 | *status = 'X'; | |
127 | return ((unsigned char) WTERMSIG (w)); | |
128 | } | |
129 | ||
130 | fetch_inferior_registers (0); | |
131 | ||
132 | *status = 'T'; | |
133 | return ((unsigned char) WSTOPSIG (w)); | |
134 | } | |
135 | ||
136 | /* Resume execution of the inferior process. | |
137 | If STEP is nonzero, single-step it. | |
138 | If SIGNAL is nonzero, give it that signal. */ | |
139 | ||
140 | void | |
141 | myresume (step, signal) | |
142 | int step; | |
143 | int signal; | |
144 | { | |
145 | errno = 0; | |
146 | ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid, 1, signal, 0); | |
147 | if (errno) | |
148 | perror_with_name ("ptrace"); | |
149 | } | |
150 | ||
151 | ||
152 | #if !defined (offsetof) | |
153 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
154 | #endif | |
155 | ||
156 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
157 | #if !defined (U_REGS_OFFSET) | |
158 | #define U_REGS_OFFSET \ | |
159 | ptrace (PT_READ_U, inferior_pid, \ | |
160 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ | |
161 | - KERNEL_U_ADDR | |
162 | #endif | |
163 | ||
164 | CORE_ADDR | |
165 | register_addr (regno, blockend) | |
166 | int regno; | |
167 | CORE_ADDR blockend; | |
168 | { | |
169 | CORE_ADDR addr; | |
170 | ||
171 | if (regno < 0 || regno >= ARCH_NUM_REGS) | |
172 | error ("Invalid register number %d.", regno); | |
173 | ||
174 | REGISTER_U_ADDR (addr, blockend, regno); | |
175 | ||
176 | return addr; | |
177 | } | |
178 | ||
179 | /* Fetch one register. */ | |
180 | ||
181 | static void | |
182 | fetch_register (regno) | |
183 | int regno; | |
184 | { | |
185 | register unsigned int regaddr; | |
186 | char buf[MAX_REGISTER_RAW_SIZE]; | |
187 | register int i; | |
188 | ||
189 | /* Offset of registers within the u area. */ | |
190 | unsigned int offset; | |
191 | ||
192 | offset = U_REGS_OFFSET; | |
193 | ||
194 | regaddr = register_addr (regno, offset); | |
195 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
196 | { | |
197 | errno = 0; | |
198 | *(int *) ®isters[ regno * 4 + i] = ptrace (PT_RUREGS, inferior_pid, | |
199 | (PTRACE_ARG3_TYPE) regaddr, 0, 0); | |
200 | regaddr += sizeof (int); | |
201 | if (errno != 0) | |
202 | { | |
203 | /* Warning, not error, in case we are attached; sometimes the | |
204 | kernel doesn't let us at the registers. */ | |
205 | char *err = strerror (errno); | |
206 | char *msg = alloca (strlen (err) + 128); | |
207 | sprintf (msg, "reading register %d: %s", regno, err); | |
208 | error (msg); | |
209 | goto error_exit; | |
210 | } | |
211 | } | |
212 | error_exit:; | |
213 | } | |
214 | ||
215 | /* Fetch all registers, or just one, from the child process. */ | |
216 | ||
217 | void | |
218 | fetch_inferior_registers (regno) | |
219 | int regno; | |
220 | { | |
221 | if (regno == -1 || regno == 0) | |
222 | for (regno = 0; regno < NUM_REGS; regno++) | |
223 | fetch_register (regno); | |
224 | else | |
225 | fetch_register (regno); | |
226 | } | |
227 | ||
228 | /* Store our register values back into the inferior. | |
229 | If REGNO is -1, do this for all registers. | |
230 | Otherwise, REGNO specifies which register (so we can save time). */ | |
231 | ||
232 | void | |
233 | store_inferior_registers (regno) | |
234 | int regno; | |
235 | { | |
236 | register unsigned int regaddr; | |
237 | char buf[80]; | |
238 | extern char registers[]; | |
239 | register int i; | |
240 | unsigned int offset = U_REGS_OFFSET; | |
241 | int scratch; | |
242 | ||
243 | if (regno >= 0) | |
244 | { | |
245 | if (CANNOT_STORE_REGISTER (regno)) | |
246 | return; | |
247 | regaddr = register_addr (regno, offset); | |
248 | errno = 0; | |
249 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
250 | { | |
251 | scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3; | |
252 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
253 | scratch, 0); | |
254 | if (errno != 0) | |
255 | { | |
256 | /* Error, even if attached. Failing to write these two | |
257 | registers is pretty serious. */ | |
258 | sprintf (buf, "writing register number %d", regno); | |
259 | perror_with_name (buf); | |
260 | } | |
261 | } | |
262 | else | |
263 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
264 | { | |
265 | errno = 0; | |
266 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
267 | *(int *) ®isters[REGISTER_BYTE (regno) + i], 0); | |
268 | if (errno != 0) | |
269 | { | |
270 | /* Warning, not error, in case we are attached; sometimes the | |
271 | kernel doesn't let us at the registers. */ | |
272 | char *err = strerror (errno); | |
273 | char *msg = alloca (strlen (err) + 128); | |
274 | sprintf (msg, "writing register %d: %s", | |
275 | regno, err); | |
276 | error (msg); | |
277 | return; | |
278 | } | |
279 | regaddr += sizeof(int); | |
280 | } | |
281 | } | |
282 | else | |
283 | for (regno = 0; regno < NUM_REGS; regno++) | |
284 | store_inferior_registers (regno); | |
285 | } | |
286 | ||
287 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
288 | in the NEW_SUN_PTRACE case. | |
289 | It ought to be straightforward. But it appears that writing did | |
290 | not write the data that I specified. I cannot understand where | |
291 | it got the data that it actually did write. */ | |
292 | ||
293 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
294 | to debugger memory starting at MYADDR. */ | |
295 | ||
296 | read_inferior_memory (memaddr, myaddr, len) | |
297 | CORE_ADDR memaddr; | |
298 | char *myaddr; | |
299 | int len; | |
300 | { | |
301 | register int i; | |
302 | /* Round starting address down to longword boundary. */ | |
303 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
304 | /* Round ending address up; get number of longwords that makes. */ | |
305 | register int count | |
306 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
307 | /* Allocate buffer of that many longwords. */ | |
308 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
309 | ||
310 | /* Read all the longwords */ | |
311 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
312 | { | |
313 | buffer[i] = ptrace (1, inferior_pid, addr, 0, 0); | |
314 | } | |
315 | ||
316 | /* Copy appropriate bytes out of the buffer. */ | |
317 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
318 | } | |
319 | ||
320 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
321 | to inferior's memory at MEMADDR. | |
322 | On failure (cannot write the inferior) | |
323 | returns the value of errno. */ | |
324 | ||
325 | int | |
326 | write_inferior_memory (memaddr, myaddr, len) | |
327 | CORE_ADDR memaddr; | |
328 | char *myaddr; | |
329 | int len; | |
330 | { | |
331 | register int i; | |
332 | /* Round starting address down to longword boundary. */ | |
333 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
334 | /* Round ending address up; get number of longwords that makes. */ | |
335 | register int count | |
336 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
337 | /* Allocate buffer of that many longwords. */ | |
338 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
339 | extern int errno; | |
340 | ||
341 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
342 | ||
343 | buffer[0] = ptrace (1, inferior_pid, addr, 0, 0); | |
344 | ||
345 | if (count > 1) | |
346 | { | |
347 | buffer[count - 1] | |
348 | = ptrace (1, inferior_pid, | |
349 | addr + (count - 1) * sizeof (int), 0, 0); | |
350 | } | |
351 | ||
352 | /* Copy data to be written over corresponding part of buffer */ | |
353 | ||
354 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
355 | ||
356 | /* Write the entire buffer. */ | |
357 | ||
358 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
359 | { | |
360 | errno = 0; | |
361 | ptrace (4, inferior_pid, addr, buffer[i], 0); | |
362 | if (errno) | |
363 | return errno; | |
364 | } | |
365 | ||
366 | return 0; | |
367 | } | |
368 | \f | |
369 | void | |
370 | initialize () | |
371 | { | |
372 | inferior_pid = 0; | |
373 | } | |
374 | ||
375 | int | |
376 | have_inferior_p () | |
377 | { | |
378 | return inferior_pid != 0; | |
379 | } |