Commit | Line | Data |
---|---|---|
38dc5e12 SG |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright (C) 1986, 1987, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
799d5b6a SG |
19 | |
20 | #include "defs.h" | |
38dc5e12 | 21 | #include "/usr/include/sys/wait.h" |
799d5b6a SG |
22 | #include "frame.h" |
23 | #include "inferior.h" | |
24 | /*************************** | |
25 | #include "initialize.h" | |
26 | ****************************/ | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <sys/param.h> | |
30 | #include <sys/dir.h> | |
31 | #include <sys/user.h> | |
32 | #include <signal.h> | |
33 | #include <sys/ioctl.h> | |
34 | #include <sgtty.h> | |
35 | #include <fcntl.h> | |
36 | ||
37 | /***************Begin MY defs*********************/ | |
38 | int quit_flag = 0; | |
39 | char registers[REGISTER_BYTES]; | |
40 | ||
41 | /* Index within `registers' of the first byte of the space for | |
42 | register N. */ | |
43 | ||
44 | ||
45 | char buf2[MAX_REGISTER_RAW_SIZE]; | |
46 | /***************End MY defs*********************/ | |
47 | ||
48 | #include <sys/ptrace.h> | |
49 | #include <machine/reg.h> | |
50 | ||
ff1cdb40 JK |
51 | extern int sys_nerr; |
52 | extern char **sys_errlist; | |
799d5b6a SG |
53 | extern char **environ; |
54 | extern int errno; | |
55 | extern int inferior_pid; | |
56 | void error (), quit (), perror_with_name (); | |
57 | int query (); | |
799d5b6a SG |
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 | |
f450d101 SG |
64 | create_inferior (program, allargs) |
65 | char *program; | |
799d5b6a | 66 | char **allargs; |
799d5b6a SG |
67 | { |
68 | int pid; | |
799d5b6a | 69 | |
f450d101 | 70 | pid = fork (); |
799d5b6a | 71 | if (pid < 0) |
f450d101 | 72 | perror_with_name ("fork"); |
799d5b6a SG |
73 | |
74 | if (pid == 0) | |
75 | { | |
799d5b6a SG |
76 | ptrace (PTRACE_TRACEME); |
77 | ||
f450d101 | 78 | execv (program, allargs); |
799d5b6a | 79 | |
f450d101 | 80 | fprintf (stderr, "Cannot exec %s: %s.\n", program, |
799d5b6a SG |
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 (8, inferior_pid, 0, 0); | |
97 | wait (0); | |
98 | /*************inferior_died ();****VK**************/ | |
99 | } | |
100 | ||
38dc5e12 | 101 | /* Wait for process, returns status */ |
799d5b6a SG |
102 | |
103 | unsigned char | |
38dc5e12 | 104 | mywait (status) |
799d5b6a SG |
105 | char *status; |
106 | { | |
107 | int pid; | |
38dc5e12 | 108 | union wait w; |
799d5b6a | 109 | |
799d5b6a SG |
110 | pid = wait (&w); |
111 | if (pid != inferior_pid) | |
112 | perror_with_name ("wait"); | |
113 | ||
799d5b6a SG |
114 | if (WIFEXITED (w)) |
115 | { | |
38dc5e12 | 116 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); |
799d5b6a SG |
117 | *status = 'E'; |
118 | return ((unsigned char) WEXITSTATUS (w)); | |
119 | } | |
120 | else if (!WIFSTOPPED (w)) | |
121 | { | |
38dc5e12 | 122 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); |
799d5b6a SG |
123 | *status = 'T'; |
124 | return ((unsigned char) WTERMSIG (w)); | |
125 | } | |
38dc5e12 SG |
126 | |
127 | fetch_inferior_registers (0); | |
128 | ||
129 | *status = 'S'; | |
130 | return ((unsigned char) WSTOPSIG (w)); | |
799d5b6a SG |
131 | } |
132 | ||
38dc5e12 SG |
133 | /* Resume execution of the inferior process. |
134 | If STEP is nonzero, single-step it. | |
135 | If SIGNAL is nonzero, give it that signal. */ | |
136 | ||
137 | void | |
138 | myresume (step, signal) | |
139 | int step; | |
140 | int signal; | |
141 | { | |
142 | errno = 0; | |
143 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
144 | if (errno) | |
145 | perror_with_name ("ptrace"); | |
146 | } | |
799d5b6a SG |
147 | |
148 | /* Fetch one or more registers from the inferior. REGNO == -1 to get | |
149 | them all. We actually fetch more than requested, when convenient, | |
150 | marking them as valid so we won't fetch them again. */ | |
151 | ||
152 | void | |
153 | fetch_inferior_registers (ignored) | |
154 | int ignored; | |
155 | { | |
156 | struct regs inferior_registers; | |
157 | struct fp_status inferior_fp_registers; | |
158 | int i; | |
159 | ||
160 | /* Global and Out regs are fetched directly, as well as the control | |
161 | registers. If we're getting one of the in or local regs, | |
162 | and the stack pointer has not yet been fetched, | |
163 | we have to do that first, since they're found in memory relative | |
164 | to the stack pointer. */ | |
165 | ||
166 | if (ptrace (PTRACE_GETREGS, inferior_pid, | |
167 | (PTRACE_ARG3_TYPE) &inferior_registers, 0)) | |
168 | perror("ptrace_getregs"); | |
169 | ||
170 | registers[REGISTER_BYTE (0)] = 0; | |
171 | memcpy (®isters[REGISTER_BYTE (1)], &inferior_registers.r_g1, | |
172 | 15 * REGISTER_RAW_SIZE (G0_REGNUM)); | |
173 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; | |
174 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; | |
175 | *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc; | |
176 | *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y; | |
177 | ||
178 | /* Floating point registers */ | |
179 | ||
180 | if (ptrace (PTRACE_GETFPREGS, inferior_pid, | |
181 | (PTRACE_ARG3_TYPE) &inferior_fp_registers, | |
182 | 0)) | |
183 | perror("ptrace_getfpregs"); | |
184 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, | |
185 | sizeof inferior_fp_registers.fpu_fr); | |
186 | ||
187 | /* These regs are saved on the stack by the kernel. Only read them | |
188 | all (16 ptrace calls!) if we really need them. */ | |
189 | ||
190 | read_inferior_memory (*(CORE_ADDR*)®isters[REGISTER_BYTE (SP_REGNUM)], | |
191 | ®isters[REGISTER_BYTE (L0_REGNUM)], | |
192 | 16*REGISTER_RAW_SIZE (L0_REGNUM)); | |
193 | } | |
194 | ||
195 | /* Store our register values back into the inferior. | |
196 | If REGNO is -1, do this for all registers. | |
197 | Otherwise, REGNO specifies which register (so we can save time). */ | |
198 | ||
199 | void | |
200 | store_inferior_registers (ignored) | |
201 | int ignored; | |
202 | { | |
203 | struct regs inferior_registers; | |
204 | struct fp_status inferior_fp_registers; | |
205 | CORE_ADDR sp = *(CORE_ADDR *)®isters[REGISTER_BYTE (SP_REGNUM)]; | |
206 | ||
207 | write_inferior_memory (sp, ®isters[REGISTER_BYTE (L0_REGNUM)], | |
208 | 16*REGISTER_RAW_SIZE (L0_REGNUM)); | |
209 | ||
210 | memcpy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (G1_REGNUM)], | |
211 | 15 * REGISTER_RAW_SIZE (G1_REGNUM)); | |
212 | ||
213 | inferior_registers.r_ps = | |
214 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)]; | |
215 | inferior_registers.r_pc = | |
216 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)]; | |
217 | inferior_registers.r_npc = | |
218 | *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)]; | |
219 | inferior_registers.r_y = | |
220 | *(int *)®isters[REGISTER_BYTE (Y_REGNUM)]; | |
221 | ||
222 | if (ptrace (PTRACE_SETREGS, inferior_pid, | |
223 | (PTRACE_ARG3_TYPE) &inferior_registers, 0)) | |
224 | perror("ptrace_setregs"); | |
225 | ||
226 | memcpy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)], | |
227 | sizeof inferior_fp_registers.fpu_fr); | |
228 | ||
229 | if (ptrace (PTRACE_SETFPREGS, inferior_pid, | |
230 | (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0)) | |
231 | perror("ptrace_setfpregs"); | |
232 | } | |
233 | ||
799d5b6a SG |
234 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory |
235 | in the NEW_SUN_PTRACE case. | |
236 | It ought to be straightforward. But it appears that writing did | |
237 | not write the data that I specified. I cannot understand where | |
238 | it got the data that it actually did write. */ | |
239 | ||
240 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
241 | to debugger memory starting at MYADDR. */ | |
242 | ||
243 | read_inferior_memory (memaddr, myaddr, len) | |
244 | CORE_ADDR memaddr; | |
245 | char *myaddr; | |
246 | int len; | |
247 | { | |
248 | register int i; | |
249 | /* Round starting address down to longword boundary. */ | |
250 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
251 | /* Round ending address up; get number of longwords that makes. */ | |
252 | register int count | |
253 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
254 | /* Allocate buffer of that many longwords. */ | |
255 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
256 | ||
257 | /* Read all the longwords */ | |
258 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
259 | { | |
260 | buffer[i] = ptrace (1, inferior_pid, addr, 0); | |
261 | } | |
262 | ||
263 | /* Copy appropriate bytes out of the buffer. */ | |
a6e0dae9 | 264 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
799d5b6a SG |
265 | } |
266 | ||
267 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
268 | to inferior's memory at MEMADDR. | |
269 | On failure (cannot write the inferior) | |
270 | returns the value of errno. */ | |
271 | ||
272 | int | |
273 | write_inferior_memory (memaddr, myaddr, len) | |
274 | CORE_ADDR memaddr; | |
275 | char *myaddr; | |
276 | int len; | |
277 | { | |
278 | register int i; | |
279 | /* Round starting address down to longword boundary. */ | |
280 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
281 | /* Round ending address up; get number of longwords that makes. */ | |
282 | register int count | |
283 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
284 | /* Allocate buffer of that many longwords. */ | |
285 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
286 | extern int errno; | |
287 | ||
288 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
289 | ||
290 | buffer[0] = ptrace (1, inferior_pid, addr, 0); | |
291 | ||
292 | if (count > 1) | |
293 | { | |
294 | buffer[count - 1] | |
295 | = ptrace (1, inferior_pid, | |
296 | addr + (count - 1) * sizeof (int), 0); | |
297 | } | |
298 | ||
299 | /* Copy data to be written over corresponding part of buffer */ | |
300 | ||
301 | bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
302 | ||
303 | /* Write the entire buffer. */ | |
304 | ||
305 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
306 | { | |
307 | errno = 0; | |
308 | ptrace (4, inferior_pid, addr, buffer[i]); | |
309 | if (errno) | |
310 | return errno; | |
311 | } | |
312 | ||
313 | return 0; | |
314 | } | |
315 | \f | |
799d5b6a SG |
316 | void |
317 | initialize () | |
318 | { | |
799d5b6a | 319 | inferior_pid = 0; |
799d5b6a SG |
320 | } |
321 | ||
799d5b6a SG |
322 | int |
323 | have_inferior_p () | |
324 | { | |
325 | return inferior_pid != 0; | |
326 | } |