1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
25 /***************************
26 #include "initialize.h"
27 ****************************/
30 #include <sys/param.h>
34 #include <sys/ioctl.h>
38 /***************Begin MY defs*********************/
39 static char my_registers
[REGISTER_BYTES
];
40 char *registers
= my_registers
;
41 /***************End MY defs*********************/
43 #include <sys/ptrace.h>
47 extern char **sys_errlist
;
49 extern int inferior_pid
;
50 void perror_with_name ();
52 /* Start an inferior process and returns its pid.
53 ALLARGS is a vector of program-name and args. */
56 create_inferior (char *program
, char **allargs
)
62 perror_with_name ("fork");
66 ptrace (PTRACE_TRACEME
);
68 execv (program
, allargs
);
70 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
71 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
79 /* Kill the inferior process. Make us have no inferior. */
84 if (inferior_pid
== 0)
86 ptrace (8, inferior_pid
, 0, 0);
88 /*************inferior_died ();****VK**************/
91 /* Return nonzero if the given thread is still alive. */
93 mythread_alive (int pid
)
98 /* Wait for process, returns status */
101 mywait (char *status
)
107 if (pid
!= inferior_pid
)
108 perror_with_name ("wait");
112 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
114 return ((unsigned char) WEXITSTATUS (w
));
116 else if (!WIFSTOPPED (w
))
118 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
120 return ((unsigned char) WTERMSIG (w
));
123 fetch_inferior_registers (0);
126 return ((unsigned char) WSTOPSIG (w
));
129 /* Resume execution of the inferior process.
130 If STEP is nonzero, single-step it.
131 If SIGNAL is nonzero, give it that signal. */
134 myresume (int step
, int signal
)
137 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
139 perror_with_name ("ptrace");
142 /* Fetch one or more registers from the inferior. REGNO == -1 to get
143 them all. We actually fetch more than requested, when convenient,
144 marking them as valid so we won't fetch them again. */
147 fetch_inferior_registers (int ignored
)
149 struct regs inferior_registers
;
150 struct fp_status inferior_fp_registers
;
153 /* Global and Out regs are fetched directly, as well as the control
154 registers. If we're getting one of the in or local regs,
155 and the stack pointer has not yet been fetched,
156 we have to do that first, since they're found in memory relative
157 to the stack pointer. */
159 if (ptrace (PTRACE_GETREGS
, inferior_pid
,
160 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
161 perror ("ptrace_getregs");
163 registers
[REGISTER_BYTE (0)] = 0;
164 memcpy (®isters
[REGISTER_BYTE (1)], &inferior_registers
.r_g1
,
165 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
166 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
167 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
168 *(int *) ®isters
[REGISTER_BYTE (NPC_REGNUM
)] = inferior_registers
.r_npc
;
169 *(int *) ®isters
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
171 /* Floating point registers */
173 if (ptrace (PTRACE_GETFPREGS
, inferior_pid
,
174 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
,
176 perror ("ptrace_getfpregs");
177 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
178 sizeof inferior_fp_registers
.fpu_fr
);
180 /* These regs are saved on the stack by the kernel. Only read them
181 all (16 ptrace calls!) if we really need them. */
183 read_inferior_memory (*(CORE_ADDR
*) & registers
[REGISTER_BYTE (SP_REGNUM
)],
184 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
185 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
188 /* Store our register values back into the inferior.
189 If REGNO is -1, do this for all registers.
190 Otherwise, REGNO specifies which register (so we can save time). */
193 store_inferior_registers (int ignored
)
195 struct regs inferior_registers
;
196 struct fp_status inferior_fp_registers
;
197 CORE_ADDR sp
= *(CORE_ADDR
*) & registers
[REGISTER_BYTE (SP_REGNUM
)];
199 write_inferior_memory (sp
, ®isters
[REGISTER_BYTE (L0_REGNUM
)],
200 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
202 memcpy (&inferior_registers
.r_g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
203 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
205 inferior_registers
.r_ps
=
206 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)];
207 inferior_registers
.r_pc
=
208 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
209 inferior_registers
.r_npc
=
210 *(int *) ®isters
[REGISTER_BYTE (NPC_REGNUM
)];
211 inferior_registers
.r_y
=
212 *(int *) ®isters
[REGISTER_BYTE (Y_REGNUM
)];
214 if (ptrace (PTRACE_SETREGS
, inferior_pid
,
215 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
216 perror ("ptrace_setregs");
218 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
219 sizeof inferior_fp_registers
.fpu_fr
);
221 if (ptrace (PTRACE_SETFPREGS
, inferior_pid
,
222 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0))
223 perror ("ptrace_setfpregs");
226 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
227 in the NEW_SUN_PTRACE case.
228 It ought to be straightforward. But it appears that writing did
229 not write the data that I specified. I cannot understand where
230 it got the data that it actually did write. */
232 /* Copy LEN bytes from inferior's memory starting at MEMADDR
233 to debugger memory starting at MYADDR. */
235 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
238 /* Round starting address down to longword boundary. */
239 register CORE_ADDR addr
= memaddr
& -sizeof (int);
240 /* Round ending address up; get number of longwords that makes. */
242 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
243 /* Allocate buffer of that many longwords. */
244 register int *buffer
= (int *) alloca (count
* sizeof (int));
246 /* Read all the longwords */
247 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
249 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
252 /* Copy appropriate bytes out of the buffer. */
253 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
256 /* Copy LEN bytes of data from debugger memory at MYADDR
257 to inferior's memory at MEMADDR.
258 On failure (cannot write the inferior)
259 returns the value of errno. */
262 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
265 /* Round starting address down to longword boundary. */
266 register CORE_ADDR addr
= memaddr
& -sizeof (int);
267 /* Round ending address up; get number of longwords that makes. */
269 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
270 /* Allocate buffer of that many longwords. */
271 register int *buffer
= (int *) alloca (count
* sizeof (int));
274 /* Fill start and end extra bytes of buffer with existing memory data. */
276 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
281 = ptrace (1, inferior_pid
,
282 addr
+ (count
- 1) * sizeof (int), 0);
285 /* Copy data to be written over corresponding part of buffer */
287 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
289 /* Write the entire buffer. */
291 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
294 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
303 initialize_low (void)