Commit | Line | Data |
---|---|---|
0d14c7df FF |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright (C) 1995, 1996 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 | #if 0 | |
32 | #include <sgtty.h> | |
33 | #endif | |
34 | #include <fcntl.h> | |
35 | ||
36 | /***************Begin MY defs*********************/ | |
37 | int quit_flag = 0; | |
38 | char registers[REGISTER_BYTES]; | |
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 | #if 0 | |
49 | #include <machine/reg.h> | |
50 | #endif | |
51 | ||
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, 0, 0, 0); | |
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 (PTRACE_KILL, inferior_pid, 0, 0); | |
96 | wait (0); | |
97 | /*************inferior_died ();****VK**************/ | |
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 | ||
156 | #if !defined (offsetof) | |
157 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
158 | #endif | |
159 | ||
160 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
161 | #if !defined (U_REGS_OFFSET) | |
162 | #define U_REGS_OFFSET \ | |
163 | ptrace (PT_READ_U, inferior_pid, \ | |
164 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ | |
165 | - KERNEL_U_ADDR | |
166 | #endif | |
167 | ||
168 | /* this table must line up with REGISTER_NAMES in tm-i386v.h */ | |
169 | /* symbols like 'EAX' come from <sys/reg.h> */ | |
170 | static int regmap[] = | |
171 | { | |
172 | EAX, ECX, EDX, EBX, | |
173 | UESP, EBP, ESI, EDI, | |
174 | EIP, EFL, CS, SS, | |
175 | DS, ES, FS, GS, | |
176 | }; | |
177 | ||
178 | int | |
179 | i386_register_u_addr (blockend, regnum) | |
180 | int blockend; | |
181 | int regnum; | |
182 | { | |
183 | #if 0 | |
184 | /* this will be needed if fp registers are reinstated */ | |
185 | /* for now, you can look at them with 'info float' | |
186 | * sys5 wont let you change them with ptrace anyway | |
187 | */ | |
188 | if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) | |
189 | { | |
190 | int ubase, fpstate; | |
191 | struct user u; | |
192 | ubase = blockend + 4 * (SS + 1) - KSTKSZ; | |
193 | fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u); | |
194 | return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM)); | |
195 | } | |
196 | else | |
197 | #endif | |
198 | return (blockend + 4 * regmap[regnum]); | |
199 | ||
200 | } | |
201 | ||
0be55272 | 202 | CORE_ADDR |
0d14c7df FF |
203 | register_addr (regno, blockend) |
204 | int regno; | |
0be55272 | 205 | CORE_ADDR blockend; |
0d14c7df | 206 | { |
0be55272 | 207 | CORE_ADDR addr; |
0d14c7df FF |
208 | |
209 | if (regno < 0 || regno >= ARCH_NUM_REGS) | |
210 | error ("Invalid register number %d.", regno); | |
211 | ||
212 | REGISTER_U_ADDR (addr, blockend, regno); | |
213 | ||
214 | return addr; | |
215 | } | |
216 | ||
217 | /* Fetch one register. */ | |
218 | ||
219 | static void | |
220 | fetch_register (regno) | |
221 | int regno; | |
222 | { | |
223 | register unsigned int regaddr; | |
0d14c7df FF |
224 | register int i; |
225 | ||
226 | /* Offset of registers within the u area. */ | |
227 | unsigned int offset; | |
228 | ||
229 | offset = U_REGS_OFFSET; | |
230 | ||
231 | regaddr = register_addr (regno, offset); | |
232 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
233 | { | |
234 | errno = 0; | |
235 | *(int *) ®isters[ regno * 4 + i] = ptrace (PTRACE_PEEKUSR, inferior_pid, | |
236 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
237 | regaddr += sizeof (int); | |
238 | if (errno != 0) | |
239 | { | |
240 | /* Warning, not error, in case we are attached; sometimes the | |
241 | kernel doesn't let us at the registers. */ | |
242 | char *err = strerror (errno); | |
243 | char *msg = alloca (strlen (err) + 128); | |
244 | sprintf (msg, "reading register %d: %s", regno, err); | |
245 | error (msg); | |
246 | goto error_exit; | |
247 | } | |
248 | } | |
249 | error_exit:; | |
250 | } | |
251 | ||
252 | /* Fetch all registers, or just one, from the child process. */ | |
253 | ||
254 | void | |
255 | fetch_inferior_registers (regno) | |
256 | int regno; | |
257 | { | |
258 | if (regno == -1 || regno == 0) | |
259 | for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++) | |
260 | fetch_register (regno); | |
261 | else | |
262 | fetch_register (regno); | |
263 | } | |
264 | ||
265 | /* Store our register values back into the inferior. | |
266 | If REGNO is -1, do this for all registers. | |
267 | Otherwise, REGNO specifies which register (so we can save time). */ | |
268 | ||
269 | void | |
270 | store_inferior_registers (regno) | |
271 | int regno; | |
272 | { | |
273 | register unsigned int regaddr; | |
0d14c7df FF |
274 | register int i; |
275 | unsigned int offset = U_REGS_OFFSET; | |
0d14c7df FF |
276 | |
277 | if (regno >= 0) | |
278 | { | |
279 | #if 0 | |
280 | if (CANNOT_STORE_REGISTER (regno)) | |
281 | return; | |
282 | #endif | |
283 | regaddr = register_addr (regno, offset); | |
284 | errno = 0; | |
285 | #if 0 | |
286 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
287 | { | |
288 | scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3; | |
289 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
290 | scratch, 0); | |
291 | if (errno != 0) | |
292 | { | |
293 | /* Error, even if attached. Failing to write these two | |
294 | registers is pretty serious. */ | |
295 | sprintf (buf, "writing register number %d", regno); | |
296 | perror_with_name (buf); | |
297 | } | |
298 | } | |
299 | else | |
300 | #endif | |
301 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
302 | { | |
303 | errno = 0; | |
304 | ptrace (PTRACE_POKEUSR, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
305 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); | |
306 | if (errno != 0) | |
307 | { | |
308 | /* Warning, not error, in case we are attached; sometimes the | |
309 | kernel doesn't let us at the registers. */ | |
310 | char *err = strerror (errno); | |
311 | char *msg = alloca (strlen (err) + 128); | |
312 | sprintf (msg, "writing register %d: %s", | |
313 | regno, err); | |
314 | error (msg); | |
315 | return; | |
316 | } | |
317 | regaddr += sizeof(int); | |
318 | } | |
319 | } | |
320 | else | |
321 | for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++) | |
322 | store_inferior_registers (regno); | |
323 | } | |
324 | ||
325 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
326 | in the NEW_SUN_PTRACE case. | |
327 | It ought to be straightforward. But it appears that writing did | |
328 | not write the data that I specified. I cannot understand where | |
329 | it got the data that it actually did write. */ | |
330 | ||
331 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
332 | to debugger memory starting at MYADDR. */ | |
333 | ||
913a7c4c | 334 | void |
0d14c7df FF |
335 | read_inferior_memory (memaddr, myaddr, len) |
336 | CORE_ADDR memaddr; | |
337 | char *myaddr; | |
338 | int len; | |
339 | { | |
340 | register int i; | |
341 | /* Round starting address down to longword boundary. */ | |
342 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
343 | /* Round ending address up; get number of longwords that makes. */ | |
344 | register int count | |
345 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
346 | /* Allocate buffer of that many longwords. */ | |
347 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
348 | ||
349 | /* Read all the longwords */ | |
350 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
351 | { | |
352 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); | |
353 | } | |
354 | ||
355 | /* Copy appropriate bytes out of the buffer. */ | |
356 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
357 | } | |
358 | ||
359 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
360 | to inferior's memory at MEMADDR. | |
361 | On failure (cannot write the inferior) | |
362 | returns the value of errno. */ | |
363 | ||
364 | int | |
365 | write_inferior_memory (memaddr, myaddr, len) | |
366 | CORE_ADDR memaddr; | |
367 | char *myaddr; | |
368 | int len; | |
369 | { | |
370 | register int i; | |
371 | /* Round starting address down to longword boundary. */ | |
372 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
373 | /* Round ending address up; get number of longwords that makes. */ | |
374 | register int count | |
375 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
376 | /* Allocate buffer of that many longwords. */ | |
377 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
378 | extern int errno; | |
379 | ||
380 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
381 | ||
382 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); | |
383 | ||
384 | if (count > 1) | |
385 | { | |
386 | buffer[count - 1] | |
387 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
388 | addr + (count - 1) * sizeof (int), 0); | |
389 | } | |
390 | ||
391 | /* Copy data to be written over corresponding part of buffer */ | |
392 | ||
393 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
394 | ||
395 | /* Write the entire buffer. */ | |
396 | ||
397 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
398 | { | |
399 | errno = 0; | |
400 | ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]); | |
401 | if (errno) | |
402 | return errno; | |
403 | } | |
404 | ||
405 | return 0; | |
406 | } | |
407 | \f | |
408 | void | |
409 | initialize () | |
410 | { | |
411 | inferior_pid = 0; | |
412 | } | |
413 | ||
414 | int | |
415 | have_inferior_p () | |
416 | { | |
417 | return inferior_pid != 0; | |
418 | } |