Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc. | |
3 | ||
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
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. | |
c906108c | 10 | |
c5aa993b JM |
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. | |
c906108c | 15 | |
c5aa993b JM |
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, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "<sys/wait.h>" | |
23 | #include "frame.h" | |
24 | #include "inferior.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <sys/param.h> | |
28 | #include <sys/dir.h> | |
29 | #include <sys/user.h> | |
30 | #include <signal.h> | |
31 | #include <sys/ioctl.h> | |
32 | #include <sgtty.h> | |
33 | #include <fcntl.h> | |
34 | ||
35 | /***************Begin MY defs*********************/ | |
36 | int quit_flag = 0; | |
5c44784c JM |
37 | static char my_registers[REGISTER_BYTES]; |
38 | char *registers = my_registers; | |
c906108c SS |
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 | #include <machine/reg.h> | |
49 | ||
50 | extern int sys_nerr; | |
51 | extern char **sys_errlist; | |
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 | |
fba45db2 | 63 | create_inferior (char *program, char **allargs) |
c906108c SS |
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 | |
fba45db2 | 89 | kill_inferior (void) |
c906108c SS |
90 | { |
91 | if (inferior_pid == 0) | |
92 | return; | |
93 | ptrace (8, inferior_pid, 0, 0); | |
94 | wait (0); | |
c5aa993b | 95 | /*************inferior_died ();****VK**************/ |
c906108c SS |
96 | } |
97 | ||
98 | /* Return nonzero if the given thread is still alive. */ | |
99 | int | |
fba45db2 | 100 | mythread_alive (int pid) |
c906108c SS |
101 | { |
102 | return 1; | |
103 | } | |
104 | ||
105 | /* Wait for process, returns status */ | |
106 | ||
107 | unsigned char | |
fba45db2 | 108 | mywait (char *status) |
c906108c SS |
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 | |
fba45db2 | 141 | myresume (int step, int signal) |
c906108c SS |
142 | { |
143 | errno = 0; | |
144 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
145 | if (errno) | |
146 | perror_with_name ("ptrace"); | |
147 | } | |
148 | ||
149 | /* Fetch one or more registers from the inferior. REGNO == -1 to get | |
150 | them all. We actually fetch more than requested, when convenient, | |
151 | marking them as valid so we won't fetch them again. */ | |
152 | ||
153 | void | |
fba45db2 | 154 | fetch_inferior_registers (int ignored) |
c906108c SS |
155 | { |
156 | struct regs inferior_registers; | |
157 | struct fp_status inferior_fp_registers; | |
158 | ||
159 | ptrace (PTRACE_GETREGS, inferior_pid, | |
c5aa993b | 160 | (PTRACE_ARG3_TYPE) & inferior_registers); |
c906108c SS |
161 | #ifdef FP0_REGNUM |
162 | ptrace (PTRACE_GETFPREGS, inferior_pid, | |
c5aa993b JM |
163 | (PTRACE_ARG3_TYPE) & inferior_fp_registers); |
164 | #endif | |
165 | ||
c906108c SS |
166 | memcpy (registers, &inferior_registers, 16 * 4); |
167 | #ifdef FP0_REGNUM | |
168 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers, | |
169 | sizeof inferior_fp_registers.fps_regs); | |
c5aa993b JM |
170 | #endif |
171 | *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; | |
172 | *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc; | |
c906108c SS |
173 | #ifdef FP0_REGNUM |
174 | memcpy | |
175 | (®isters[REGISTER_BYTE (FPC_REGNUM)], | |
176 | &inferior_fp_registers.fps_control, | |
177 | sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs); | |
c5aa993b | 178 | #endif |
c906108c SS |
179 | } |
180 | ||
181 | /* Store our register values back into the inferior. | |
182 | If REGNO is -1, do this for all registers. | |
183 | Otherwise, REGNO specifies which register (so we can save time). */ | |
184 | ||
185 | void | |
fba45db2 | 186 | store_inferior_registers (int ignored) |
c906108c SS |
187 | { |
188 | struct regs inferior_registers; | |
189 | struct fp_status inferior_fp_registers; | |
190 | ||
191 | memcpy (&inferior_registers, registers, 16 * 4); | |
192 | #ifdef FP0_REGNUM | |
193 | memcpy (&inferior_fp_registers, | |
194 | ®isters[REGISTER_BYTE (FP0_REGNUM)], | |
195 | sizeof inferior_fp_registers.fps_regs); | |
196 | #endif | |
c5aa993b JM |
197 | inferior_registers.r_ps = *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)]; |
198 | inferior_registers.r_pc = *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)]; | |
c906108c SS |
199 | |
200 | #ifdef FP0_REGNUM | |
201 | memcpy (&inferior_fp_registers.fps_control, | |
202 | ®isters[REGISTER_BYTE (FPC_REGNUM)], | |
203 | (sizeof inferior_fp_registers | |
204 | - sizeof inferior_fp_registers.fps_regs)); | |
205 | #endif | |
206 | ||
207 | ptrace (PTRACE_SETREGS, inferior_pid, | |
c5aa993b | 208 | (PTRACE_ARG3_TYPE) & inferior_registers); |
c906108c SS |
209 | #if FP0_REGNUM |
210 | ptrace (PTRACE_SETFPREGS, inferior_pid, | |
c5aa993b | 211 | (PTRACE_ARG3_TYPE) & inferior_fp_registers); |
c906108c SS |
212 | #endif |
213 | } | |
214 | ||
215 | /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory | |
216 | in the NEW_SUN_PTRACE case. | |
217 | It ought to be straightforward. But it appears that writing did | |
218 | not write the data that I specified. I cannot understand where | |
219 | it got the data that it actually did write. */ | |
220 | ||
221 | /* Copy LEN bytes from inferior's memory starting at MEMADDR | |
222 | to debugger memory starting at MYADDR. */ | |
223 | ||
fba45db2 | 224 | read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
225 | { |
226 | register int i; | |
227 | /* Round starting address down to longword boundary. */ | |
228 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
229 | /* Round ending address up; get number of longwords that makes. */ | |
230 | register int count | |
231 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
232 | /* Allocate buffer of that many longwords. */ | |
233 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
234 | ||
235 | /* Read all the longwords */ | |
236 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
237 | { | |
238 | buffer[i] = ptrace (1, inferior_pid, addr, 0); | |
239 | } | |
240 | ||
241 | /* Copy appropriate bytes out of the buffer. */ | |
242 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); | |
243 | } | |
244 | ||
245 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
246 | to inferior's memory at MEMADDR. | |
247 | On failure (cannot write the inferior) | |
248 | returns the value of errno. */ | |
249 | ||
250 | int | |
fba45db2 | 251 | write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
252 | { |
253 | register int i; | |
254 | /* Round starting address down to longword boundary. */ | |
255 | register CORE_ADDR addr = memaddr & -sizeof (int); | |
256 | /* Round ending address up; get number of longwords that makes. */ | |
257 | register int count | |
258 | = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
259 | /* Allocate buffer of that many longwords. */ | |
260 | register int *buffer = (int *) alloca (count * sizeof (int)); | |
261 | extern int errno; | |
262 | ||
263 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
264 | ||
265 | buffer[0] = ptrace (1, inferior_pid, addr, 0); | |
266 | ||
267 | if (count > 1) | |
268 | { | |
269 | buffer[count - 1] | |
270 | = ptrace (1, inferior_pid, | |
271 | addr + (count - 1) * sizeof (int), 0); | |
272 | } | |
273 | ||
274 | /* Copy data to be written over corresponding part of buffer */ | |
275 | ||
276 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); | |
277 | ||
278 | /* Write the entire buffer. */ | |
279 | ||
280 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
281 | { | |
282 | errno = 0; | |
283 | ptrace (4, inferior_pid, addr, buffer[i]); | |
284 | if (errno) | |
285 | return errno; | |
286 | } | |
287 | ||
288 | return 0; | |
289 | } | |
290 | \f | |
291 | void | |
fba45db2 | 292 | initialize_low (void) |
c906108c | 293 | { |
c906108c | 294 | } |