Commit | Line | Data |
---|---|---|
7053c1a5 JK |
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. */ | |
19 | ||
20 | #include "defs.h" | |
b292fca8 | 21 | #include "<sys/wait.h>" |
7053c1a5 JK |
22 | #include "frame.h" |
23 | #include "inferior.h" | |
7053c1a5 JK |
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 int sys_nerr; | |
49 | extern char **sys_errlist; | |
50 | extern char **environ; | |
51 | extern int errno; | |
52 | extern int inferior_pid; | |
53 | void error (), 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 | |
61 | create_inferior (program, allargs) | |
62 | char *program; | |
63 | char **allargs; | |
64 | { | |
65 | int pid; | |
66 | ||
67 | pid = fork (); | |
68 | if (pid < 0) | |
69 | perror_with_name ("fork"); | |
70 | ||
71 | if (pid == 0) | |
72 | { | |
73 | ptrace (PTRACE_TRACEME); | |
74 | ||
75 | execv (program, allargs); | |
76 | ||
77 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
78 | errno < sys_nerr ? sys_errlist[errno] : "unknown error"); | |
79 | fflush (stderr); | |
80 | _exit (0177); | |
81 | } | |
82 | ||
83 | return pid; | |
84 | } | |
85 | ||
86 | /* Kill the inferior process. Make us have no inferior. */ | |
87 | ||
88 | void | |
89 | kill_inferior () | |
90 | { | |
91 | if (inferior_pid == 0) | |
92 | return; | |
93 | ptrace (8, inferior_pid, 0, 0); | |
94 | wait (0); | |
95 | /*************inferior_died ();****VK**************/ | |
96 | } | |
97 | ||
98 | /* Wait for process, returns status */ | |
99 | ||
100 | unsigned char | |
101 | mywait (status) | |
102 | char *status; | |
103 | { | |
104 | int pid; | |
105 | union wait w; | |
106 | ||
107 | pid = wait (&w); | |
108 | if (pid != inferior_pid) | |
109 | perror_with_name ("wait"); | |
110 | ||
111 | if (WIFEXITED (w)) | |
112 | { | |
113 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
4cc1b3f7 | 114 | *status = 'W'; |
7053c1a5 JK |
115 | return ((unsigned char) WEXITSTATUS (w)); |
116 | } | |
117 | else if (!WIFSTOPPED (w)) | |
118 | { | |
119 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
4cc1b3f7 | 120 | *status = 'X'; |
7053c1a5 JK |
121 | return ((unsigned char) WTERMSIG (w)); |
122 | } | |
123 | ||
124 | fetch_inferior_registers (0); | |
125 | ||
4cc1b3f7 | 126 | *status = 'T'; |
7053c1a5 JK |
127 | return ((unsigned char) WSTOPSIG (w)); |
128 | } | |
129 | ||
130 | /* Resume execution of the inferior process. | |
131 | If STEP is nonzero, single-step it. | |
132 | If SIGNAL is nonzero, give it that signal. */ | |
133 | ||
134 | void | |
135 | myresume (step, signal) | |
136 | int step; | |
137 | int signal; | |
138 | { | |
139 | errno = 0; | |
140 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
141 | if (errno) | |
142 | perror_with_name ("ptrace"); | |
143 | } | |
144 | ||
145 | /* Fetch one or more registers from the inferior. REGNO == -1 to get | |
146 | them all. We actually fetch more than requested, when convenient, | |
147 | marking them as valid so we won't fetch them again. */ | |
148 | ||
149 | void | |
150 | fetch_inferior_registers (ignored) | |
151 | int ignored; | |
152 | { | |
153 | struct regs inferior_registers; | |
154 | struct fp_status inferior_fp_registers; | |
7053c1a5 | 155 | |
5b3c73e6 JK |
156 | ptrace (PTRACE_GETREGS, inferior_pid, |
157 | (PTRACE_ARG3_TYPE) &inferior_registers); | |
158 | #ifdef FP0_REGNUM | |
159 | ptrace (PTRACE_GETFPREGS, inferior_pid, | |
160 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); | |
161 | #endif | |
162 | ||
163 | memcpy (registers, &inferior_registers, 16 * 4); | |
164 | #ifdef FP0_REGNUM | |
7053c1a5 | 165 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, |
5b3c73e6 JK |
166 | sizeof inferior_fp_registers.fps_regs); |
167 | #endif | |
168 | *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; | |
169 | *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; | |
170 | #ifdef FP0_REGNUM | |
171 | memcpy | |
172 | (®isters[REGISTER_BYTE (FPC_REGNUM)], | |
173 | &inferior_fp_registers.fps_control, | |
174 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs); | |
175 | #endif | |
7053c1a5 JK |
176 | } |
177 | ||
178 | /* Store our register values back into the inferior. | |
179 | If REGNO is -1, do this for all registers. | |
180 | Otherwise, REGNO specifies which register (so we can save time). */ | |
181 | ||
182 | void | |
183 | store_inferior_registers (ignored) | |
184 | int ignored; | |
185 | { | |
186 | struct regs inferior_registers; | |
187 | struct fp_status inferior_fp_registers; | |
7053c1a5 | 188 | |
a6e0dae9 | 189 | memcpy (&inferior_registers, registers, 16 * 4); |
5b3c73e6 | 190 | #ifdef FP0_REGNUM |
a6e0dae9 JK |
191 | memcpy (&inferior_fp_registers, |
192 | ®isters[REGISTER_BYTE (FP0_REGNUM)], | |
193 | sizeof inferior_fp_registers.fps_regs); | |
5b3c73e6 JK |
194 | #endif |
195 | inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)]; | |
196 | inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)]; | |
197 | ||
198 | #ifdef FP0_REGNUM | |
a6e0dae9 JK |
199 | memcpy (&inferior_fp_registers.fps_control, |
200 | ®isters[REGISTER_BYTE (FPC_REGNUM)], | |
201 | (sizeof inferior_fp_registers | |
202 | - sizeof inferior_fp_registers.fps_regs)); | |
5b3c73e6 JK |
203 | #endif |
204 | ||
205 | ptrace (PTRACE_SETREGS, inferior_pid, | |
206 | (PTRACE_ARG3_TYPE) &inferior_registers); | |
207 | #if FP0_REGNUM | |
208 | ptrace (PTRACE_SETFPREGS, inferior_pid, | |
209 | (PTRACE_ARG3_TYPE) &inferior_fp_registers); | |
210 | #endif | |
7053c1a5 JK |
211 | } |
212 | ||
213 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
214 | in the NEW_SUN_PTRACE case. | |
215 | It ought to be straightforward. But it appears that writing did | |
216 | not write the data that I specified. I cannot understand where | |
217 | it got the data that it actually did write. */ | |
218 | ||
219 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
220 | to debugger memory starting at MYADDR. */ | |
221 | ||
222 | read_inferior_memory (memaddr, myaddr, len) | |
223 | CORE_ADDR memaddr; | |
224 | char *myaddr; | |
225 | int len; | |
226 | { | |
227 | register int i; | |
228 | /* Round starting address down to longword boundary. */ | |
229 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
230 | /* Round ending address up; get number of longwords that makes. */ | |
231 | register int count | |
232 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
233 | /* Allocate buffer of that many longwords. */ | |
234 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
235 | ||
236 | /* Read all the longwords */ | |
237 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
238 | { | |
239 | buffer[i] = ptrace (1, inferior_pid, addr, 0); | |
240 | } | |
241 | ||
242 | /* Copy appropriate bytes out of the buffer. */ | |
a6e0dae9 | 243 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
7053c1a5 JK |
244 | } |
245 | ||
246 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
247 | to inferior's memory at MEMADDR. | |
248 | On failure (cannot write the inferior) | |
249 | returns the value of errno. */ | |
250 | ||
251 | int | |
252 | write_inferior_memory (memaddr, myaddr, len) | |
253 | CORE_ADDR memaddr; | |
254 | char *myaddr; | |
255 | int len; | |
256 | { | |
257 | register int i; | |
258 | /* Round starting address down to longword boundary. */ | |
259 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
260 | /* Round ending address up; get number of longwords that makes. */ | |
261 | register int count | |
262 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
263 | /* Allocate buffer of that many longwords. */ | |
264 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
265 | extern int errno; | |
266 | ||
267 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
268 | ||
269 | buffer[0] = ptrace (1, inferior_pid, addr, 0); | |
270 | ||
271 | if (count > 1) | |
272 | { | |
273 | buffer[count - 1] | |
274 | = ptrace (1, inferior_pid, | |
275 | addr + (count - 1) * sizeof (int), 0); | |
276 | } | |
277 | ||
278 | /* Copy data to be written over corresponding part of buffer */ | |
279 | ||
a6e0dae9 | 280 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
7053c1a5 JK |
281 | |
282 | /* Write the entire buffer. */ | |
283 | ||
284 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
285 | { | |
286 | errno = 0; | |
287 | ptrace (4, inferior_pid, addr, buffer[i]); | |
288 | if (errno) | |
289 | return errno; | |
290 | } | |
291 | ||
292 | return 0; | |
293 | } | |
294 | \f | |
295 | void | |
296 | initialize () | |
297 | { | |
298 | inferior_pid = 0; | |
299 | } | |
300 | ||
301 | int | |
302 | have_inferior_p () | |
303 | { | |
304 | return inferior_pid != 0; | |
305 | } |