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