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> | |
1994dc7c EE |
48 | |
49 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) | |
50 | #include <sys/reg.h> | |
0d14c7df FF |
51 | #endif |
52 | ||
53 | extern char **environ; | |
54 | extern int errno; | |
55 | extern int inferior_pid; | |
56 | void quit (), perror_with_name (); | |
57 | int query (); | |
58 | ||
59 | /* Start an inferior process and returns its pid. | |
60 | ALLARGS is a vector of program-name and args. | |
61 | ENV is the environment vector to pass. */ | |
62 | ||
63 | int | |
64 | create_inferior (program, allargs) | |
65 | char *program; | |
66 | char **allargs; | |
67 | { | |
68 | int pid; | |
69 | ||
70 | pid = fork (); | |
71 | if (pid < 0) | |
72 | perror_with_name ("fork"); | |
73 | ||
74 | if (pid == 0) | |
75 | { | |
76 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
77 | ||
78 | execv (program, allargs); | |
79 | ||
80 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
81 | errno < sys_nerr ? sys_errlist[errno] : "unknown error"); | |
82 | fflush (stderr); | |
83 | _exit (0177); | |
84 | } | |
85 | ||
86 | return pid; | |
87 | } | |
88 | ||
89 | /* Kill the inferior process. Make us have no inferior. */ | |
90 | ||
91 | void | |
92 | kill_inferior () | |
93 | { | |
94 | if (inferior_pid == 0) | |
95 | return; | |
96 | ptrace (PTRACE_KILL, inferior_pid, 0, 0); | |
97 | wait (0); | |
98 | /*************inferior_died ();****VK**************/ | |
99 | } | |
100 | ||
101 | /* Return nonzero if the given thread is still alive. */ | |
102 | int | |
103 | mythread_alive (pid) | |
104 | int pid; | |
105 | { | |
106 | return 1; | |
107 | } | |
108 | ||
109 | /* Wait for process, returns status */ | |
110 | ||
111 | unsigned char | |
112 | mywait (status) | |
113 | char *status; | |
114 | { | |
115 | int pid; | |
116 | union wait w; | |
117 | ||
118 | pid = wait (&w); | |
119 | if (pid != inferior_pid) | |
120 | perror_with_name ("wait"); | |
121 | ||
122 | if (WIFEXITED (w)) | |
123 | { | |
124 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
125 | *status = 'W'; | |
126 | return ((unsigned char) WEXITSTATUS (w)); | |
127 | } | |
128 | else if (!WIFSTOPPED (w)) | |
129 | { | |
130 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
131 | *status = 'X'; | |
132 | return ((unsigned char) WTERMSIG (w)); | |
133 | } | |
134 | ||
135 | fetch_inferior_registers (0); | |
136 | ||
137 | *status = 'T'; | |
138 | return ((unsigned char) WSTOPSIG (w)); | |
139 | } | |
140 | ||
141 | /* Resume execution of the inferior process. | |
142 | If STEP is nonzero, single-step it. | |
143 | If SIGNAL is nonzero, give it that signal. */ | |
144 | ||
145 | void | |
146 | myresume (step, signal) | |
147 | int step; | |
148 | int signal; | |
149 | { | |
150 | errno = 0; | |
151 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
152 | if (errno) | |
153 | perror_with_name ("ptrace"); | |
154 | } | |
155 | ||
156 | ||
157 | #if !defined (offsetof) | |
158 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
159 | #endif | |
160 | ||
161 | /* U_REGS_OFFSET is the offset of the registers within the u area. */ | |
162 | #if !defined (U_REGS_OFFSET) | |
163 | #define U_REGS_OFFSET \ | |
164 | ptrace (PT_READ_U, inferior_pid, \ | |
165 | (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \ | |
166 | - KERNEL_U_ADDR | |
167 | #endif | |
168 | ||
1994dc7c | 169 | #ifndef TARGET_M68K |
0d14c7df FF |
170 | /* this table must line up with REGISTER_NAMES in tm-i386v.h */ |
171 | /* symbols like 'EAX' come from <sys/reg.h> */ | |
172 | static int regmap[] = | |
173 | { | |
174 | EAX, ECX, EDX, EBX, | |
175 | UESP, EBP, ESI, EDI, | |
176 | EIP, EFL, CS, SS, | |
177 | DS, ES, FS, GS, | |
178 | }; | |
179 | ||
180 | int | |
181 | i386_register_u_addr (blockend, regnum) | |
182 | int blockend; | |
183 | int regnum; | |
184 | { | |
185 | #if 0 | |
186 | /* this will be needed if fp registers are reinstated */ | |
187 | /* for now, you can look at them with 'info float' | |
188 | * sys5 wont let you change them with ptrace anyway | |
189 | */ | |
190 | if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) | |
191 | { | |
192 | int ubase, fpstate; | |
193 | struct user u; | |
194 | ubase = blockend + 4 * (SS + 1) - KSTKSZ; | |
195 | fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u); | |
196 | return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM)); | |
197 | } | |
198 | else | |
199 | #endif | |
200 | return (blockend + 4 * regmap[regnum]); | |
201 | ||
202 | } | |
1994dc7c EE |
203 | #else /* TARGET_M68K */ |
204 | /* This table must line up with REGISTER_NAMES in tm-m68k.h */ | |
205 | static int regmap[] = | |
206 | { | |
207 | #ifdef PT_D0 | |
208 | PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7, | |
209 | PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP, | |
210 | PT_SR, PT_PC, | |
211 | #else | |
212 | 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, | |
213 | 17, 18, | |
214 | #endif | |
215 | #ifdef PT_FP0 | |
216 | PT_FP0, PT_FP1, PT_FP2, PT_FP3, PT_FP4, PT_FP5, PT_FP6, PT_FP7, | |
217 | PT_FPCR, PT_FPSR, PT_FPIAR | |
218 | #else | |
219 | 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47 | |
220 | #endif | |
221 | }; | |
222 | ||
223 | /* BLOCKEND is the value of u.u_ar0, and points to the place where GS | |
224 | is stored. */ | |
225 | ||
226 | int | |
227 | m68k_linux_register_u_addr (blockend, regnum) | |
228 | int blockend; | |
229 | int regnum; | |
230 | { | |
231 | return (blockend + 4 * regmap[regnum]); | |
232 | } | |
233 | #endif | |
0d14c7df | 234 | |
0be55272 | 235 | CORE_ADDR |
0d14c7df FF |
236 | register_addr (regno, blockend) |
237 | int regno; | |
0be55272 | 238 | CORE_ADDR blockend; |
0d14c7df | 239 | { |
0be55272 | 240 | CORE_ADDR addr; |
0d14c7df FF |
241 | |
242 | if (regno < 0 || regno >= ARCH_NUM_REGS) | |
243 | error ("Invalid register number %d.", regno); | |
244 | ||
245 | REGISTER_U_ADDR (addr, blockend, regno); | |
246 | ||
247 | return addr; | |
248 | } | |
249 | ||
250 | /* Fetch one register. */ | |
251 | ||
252 | static void | |
253 | fetch_register (regno) | |
254 | int regno; | |
255 | { | |
256 | register unsigned int regaddr; | |
0d14c7df FF |
257 | register int i; |
258 | ||
259 | /* Offset of registers within the u area. */ | |
260 | unsigned int offset; | |
261 | ||
262 | offset = U_REGS_OFFSET; | |
263 | ||
264 | regaddr = register_addr (regno, offset); | |
265 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int)) | |
266 | { | |
267 | errno = 0; | |
268 | *(int *) ®isters[ regno * 4 + i] = ptrace (PTRACE_PEEKUSR, inferior_pid, | |
269 | (PTRACE_ARG3_TYPE) regaddr, 0); | |
270 | regaddr += sizeof (int); | |
271 | if (errno != 0) | |
272 | { | |
273 | /* Warning, not error, in case we are attached; sometimes the | |
274 | kernel doesn't let us at the registers. */ | |
275 | char *err = strerror (errno); | |
276 | char *msg = alloca (strlen (err) + 128); | |
277 | sprintf (msg, "reading register %d: %s", regno, err); | |
278 | error (msg); | |
279 | goto error_exit; | |
280 | } | |
281 | } | |
282 | error_exit:; | |
283 | } | |
284 | ||
285 | /* Fetch all registers, or just one, from the child process. */ | |
286 | ||
287 | void | |
288 | fetch_inferior_registers (regno) | |
289 | int regno; | |
290 | { | |
291 | if (regno == -1 || regno == 0) | |
292 | for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++) | |
293 | fetch_register (regno); | |
294 | else | |
295 | fetch_register (regno); | |
296 | } | |
297 | ||
298 | /* Store our register values back into the inferior. | |
299 | If REGNO is -1, do this for all registers. | |
300 | Otherwise, REGNO specifies which register (so we can save time). */ | |
301 | ||
302 | void | |
303 | store_inferior_registers (regno) | |
304 | int regno; | |
305 | { | |
306 | register unsigned int regaddr; | |
0d14c7df FF |
307 | register int i; |
308 | unsigned int offset = U_REGS_OFFSET; | |
0d14c7df FF |
309 | |
310 | if (regno >= 0) | |
311 | { | |
312 | #if 0 | |
313 | if (CANNOT_STORE_REGISTER (regno)) | |
314 | return; | |
315 | #endif | |
316 | regaddr = register_addr (regno, offset); | |
317 | errno = 0; | |
318 | #if 0 | |
319 | if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM) | |
320 | { | |
321 | scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3; | |
322 | ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
323 | scratch, 0); | |
324 | if (errno != 0) | |
325 | { | |
326 | /* Error, even if attached. Failing to write these two | |
327 | registers is pretty serious. */ | |
328 | sprintf (buf, "writing register number %d", regno); | |
329 | perror_with_name (buf); | |
330 | } | |
331 | } | |
332 | else | |
333 | #endif | |
334 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int)) | |
335 | { | |
336 | errno = 0; | |
337 | ptrace (PTRACE_POKEUSR, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
338 | *(int *) ®isters[REGISTER_BYTE (regno) + i]); | |
339 | if (errno != 0) | |
340 | { | |
341 | /* Warning, not error, in case we are attached; sometimes the | |
342 | kernel doesn't let us at the registers. */ | |
343 | char *err = strerror (errno); | |
344 | char *msg = alloca (strlen (err) + 128); | |
345 | sprintf (msg, "writing register %d: %s", | |
346 | regno, err); | |
347 | error (msg); | |
348 | return; | |
349 | } | |
350 | regaddr += sizeof(int); | |
351 | } | |
352 | } | |
353 | else | |
354 | for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++) | |
355 | store_inferior_registers (regno); | |
356 | } | |
357 | ||
358 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
359 | in the NEW_SUN_PTRACE case. | |
360 | It ought to be straightforward. But it appears that writing did | |
361 | not write the data that I specified. I cannot understand where | |
362 | it got the data that it actually did write. */ | |
363 | ||
364 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
365 | to debugger memory starting at MYADDR. */ | |
366 | ||
913a7c4c | 367 | void |
0d14c7df FF |
368 | read_inferior_memory (memaddr, myaddr, len) |
369 | CORE_ADDR memaddr; | |
370 | char *myaddr; | |
371 | int len; | |
372 | { | |
373 | register int i; | |
374 | /* Round starting address down to longword boundary. */ | |
375 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
376 | /* Round ending address up; get number of longwords that makes. */ | |
377 | register int count | |
378 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
379 | /* Allocate buffer of that many longwords. */ | |
380 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
381 | ||
382 | /* Read all the longwords */ | |
383 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
384 | { | |
385 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); | |
386 | } | |
387 | ||
388 | /* Copy appropriate bytes out of the buffer. */ | |
389 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
390 | } | |
391 | ||
392 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
393 | to inferior's memory at MEMADDR. | |
394 | On failure (cannot write the inferior) | |
395 | returns the value of errno. */ | |
396 | ||
397 | int | |
398 | write_inferior_memory (memaddr, myaddr, len) | |
399 | CORE_ADDR memaddr; | |
400 | char *myaddr; | |
401 | int len; | |
402 | { | |
403 | register int i; | |
404 | /* Round starting address down to longword boundary. */ | |
405 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
406 | /* Round ending address up; get number of longwords that makes. */ | |
407 | register int count | |
408 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
409 | /* Allocate buffer of that many longwords. */ | |
410 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
411 | extern int errno; | |
412 | ||
413 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
414 | ||
415 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0); | |
416 | ||
417 | if (count > 1) | |
418 | { | |
419 | buffer[count - 1] | |
420 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
421 | addr + (count - 1) * sizeof (int), 0); | |
422 | } | |
423 | ||
424 | /* Copy data to be written over corresponding part of buffer */ | |
425 | ||
426 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
427 | ||
428 | /* Write the entire buffer. */ | |
429 | ||
430 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
431 | { | |
432 | errno = 0; | |
433 | ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]); | |
434 | if (errno) | |
435 | return errno; | |
436 | } | |
437 | ||
438 | return 0; | |
439 | } | |
440 | \f | |
441 | void | |
442 | initialize () | |
443 | { | |
444 | inferior_pid = 0; | |
445 | } | |
446 | ||
447 | int | |
448 | have_inferior_p () | |
449 | { | |
450 | return inferior_pid != 0; | |
451 | } |