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, Boston, MA 02111-1307, USA. */
24 /***************************
25 #include "initialize.h"
26 ****************************/
29 #include <sys/param.h>
33 #include <sys/ioctl.h>
37 /***************Begin MY defs*********************/
39 char registers
[REGISTER_BYTES
];
41 /* Index within `registers' of the first byte of the space for
45 char buf2
[MAX_REGISTER_RAW_SIZE
];
46 /***************End MY defs*********************/
48 #include <sys/ptrace.h>
52 extern char **sys_errlist
;
53 extern char **environ
;
55 extern int inferior_pid
;
56 void quit (), perror_with_name ();
59 /* Start an inferior process and returns its pid.
60 ALLARGS is a vector of program-name and args.
61 ENV is the environment vector to pass. */
64 create_inferior (program
, allargs
)
72 perror_with_name ("fork");
76 ptrace (PTRACE_TRACEME
);
78 execv (program
, allargs
);
80 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
81 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
89 /* Kill the inferior process. Make us have no inferior. */
94 if (inferior_pid
== 0)
96 ptrace (8, inferior_pid
, 0, 0);
98 /*************inferior_died ();****VK**************/
101 /* Return nonzero if the given thread is still alive. */
109 /* Wait for process, returns status */
119 if (pid
!= inferior_pid
)
120 perror_with_name ("wait");
124 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
126 return ((unsigned char) WEXITSTATUS (w
));
128 else if (!WIFSTOPPED (w
))
130 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
132 return ((unsigned char) WTERMSIG (w
));
135 fetch_inferior_registers (0);
138 return ((unsigned char) WSTOPSIG (w
));
141 /* Resume execution of the inferior process.
142 If STEP is nonzero, single-step it.
143 If SIGNAL is nonzero, give it that signal. */
146 myresume (step
, signal
)
151 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
153 perror_with_name ("ptrace");
156 /* Fetch one or more registers from the inferior. REGNO == -1 to get
157 them all. We actually fetch more than requested, when convenient,
158 marking them as valid so we won't fetch them again. */
161 fetch_inferior_registers (ignored
)
164 struct regs inferior_registers
;
165 struct fp_status inferior_fp_registers
;
168 /* Global and Out regs are fetched directly, as well as the control
169 registers. If we're getting one of the in or local regs,
170 and the stack pointer has not yet been fetched,
171 we have to do that first, since they're found in memory relative
172 to the stack pointer. */
174 if (ptrace (PTRACE_GETREGS
, inferior_pid
,
175 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
176 perror("ptrace_getregs");
178 registers
[REGISTER_BYTE (0)] = 0;
179 memcpy (®isters
[REGISTER_BYTE (1)], &inferior_registers
.r_g1
,
180 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
181 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
182 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
183 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)] = inferior_registers
.r_npc
;
184 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
186 /* Floating point registers */
188 if (ptrace (PTRACE_GETFPREGS
, inferior_pid
,
189 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
,
191 perror("ptrace_getfpregs");
192 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
193 sizeof inferior_fp_registers
.fpu_fr
);
195 /* These regs are saved on the stack by the kernel. Only read them
196 all (16 ptrace calls!) if we really need them. */
198 read_inferior_memory (*(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)],
199 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
200 16*REGISTER_RAW_SIZE (L0_REGNUM
));
203 /* Store our register values back into the inferior.
204 If REGNO is -1, do this for all registers.
205 Otherwise, REGNO specifies which register (so we can save time). */
208 store_inferior_registers (ignored
)
211 struct regs inferior_registers
;
212 struct fp_status inferior_fp_registers
;
213 CORE_ADDR sp
= *(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)];
215 write_inferior_memory (sp
, ®isters
[REGISTER_BYTE (L0_REGNUM
)],
216 16*REGISTER_RAW_SIZE (L0_REGNUM
));
218 memcpy (&inferior_registers
.r_g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
219 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
221 inferior_registers
.r_ps
=
222 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
223 inferior_registers
.r_pc
=
224 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
225 inferior_registers
.r_npc
=
226 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)];
227 inferior_registers
.r_y
=
228 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)];
230 if (ptrace (PTRACE_SETREGS
, inferior_pid
,
231 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
232 perror("ptrace_setregs");
234 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
235 sizeof inferior_fp_registers
.fpu_fr
);
237 if (ptrace (PTRACE_SETFPREGS
, inferior_pid
,
238 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0))
239 perror("ptrace_setfpregs");
242 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
243 in the NEW_SUN_PTRACE case.
244 It ought to be straightforward. But it appears that writing did
245 not write the data that I specified. I cannot understand where
246 it got the data that it actually did write. */
248 /* Copy LEN bytes from inferior's memory starting at MEMADDR
249 to debugger memory starting at MYADDR. */
251 read_inferior_memory (memaddr
, myaddr
, len
)
257 /* Round starting address down to longword boundary. */
258 register CORE_ADDR addr
= memaddr
& -sizeof (int);
259 /* Round ending address up; get number of longwords that makes. */
261 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
262 /* Allocate buffer of that many longwords. */
263 register int *buffer
= (int *) alloca (count
* sizeof (int));
265 /* Read all the longwords */
266 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
268 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
271 /* Copy appropriate bytes out of the buffer. */
272 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
275 /* Copy LEN bytes of data from debugger memory at MYADDR
276 to inferior's memory at MEMADDR.
277 On failure (cannot write the inferior)
278 returns the value of errno. */
281 write_inferior_memory (memaddr
, myaddr
, len
)
287 /* Round starting address down to longword boundary. */
288 register CORE_ADDR addr
= memaddr
& -sizeof (int);
289 /* Round ending address up; get number of longwords that makes. */
291 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
292 /* Allocate buffer of that many longwords. */
293 register int *buffer
= (int *) alloca (count
* sizeof (int));
296 /* Fill start and end extra bytes of buffer with existing memory data. */
298 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
303 = ptrace (1, inferior_pid
,
304 addr
+ (count
- 1) * sizeof (int), 0);
307 /* Copy data to be written over corresponding part of buffer */
309 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
311 /* Write the entire buffer. */
313 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
316 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
333 return inferior_pid
!= 0;