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