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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "/usr/include/sys/wait.h"
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>
49 #include <machine/reg.h>
51 extern char **environ
;
53 extern int inferior_pid
;
54 void error (), quit (), perror_with_name ();
57 /* Start an inferior process and returns its pid.
58 ALLARGS is a vector of program-name and args.
59 ENV is the environment vector to pass. */
62 create_inferior (allargs
, env
)
68 extern char *sys_errlist
[];
73 /* exec is said to fail if the executable is open. */
74 /****************close_exec_file ();*****************/
76 sprintf (execbuf
, "exec %s", allargs
);
80 perror_with_name ("vfork");
84 /* Run inferior in a separate process group. */
85 setpgrp (getpid (), getpid ());
88 ptrace (PTRACE_TRACEME
);
90 execle ("/bin/sh", "sh", "-c", execbuf
, 0, env
);
92 fprintf (stderr
, "Cannot exec /bin/sh: %s.\n",
93 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
101 /* Kill the inferior process. Make us have no inferior. */
106 if (inferior_pid
== 0)
108 ptrace (8, inferior_pid
, 0, 0);
110 /*************inferior_died ();****VK**************/
113 /* Wait for process, returns status */
123 if (pid
!= inferior_pid
)
124 perror_with_name ("wait");
128 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
130 return ((unsigned char) WEXITSTATUS (w
));
132 else if (!WIFSTOPPED (w
))
134 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
136 return ((unsigned char) WTERMSIG (w
));
139 fetch_inferior_registers (0);
142 return ((unsigned char) WSTOPSIG (w
));
145 /* Resume execution of the inferior process.
146 If STEP is nonzero, single-step it.
147 If SIGNAL is nonzero, give it that signal. */
150 myresume (step
, signal
)
155 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
157 perror_with_name ("ptrace");
160 /* Fetch one or more registers from the inferior. REGNO == -1 to get
161 them all. We actually fetch more than requested, when convenient,
162 marking them as valid so we won't fetch them again. */
165 fetch_inferior_registers (ignored
)
168 struct regs inferior_registers
;
169 struct fp_status inferior_fp_registers
;
172 /* Global and Out regs are fetched directly, as well as the control
173 registers. If we're getting one of the in or local regs,
174 and the stack pointer has not yet been fetched,
175 we have to do that first, since they're found in memory relative
176 to the stack pointer. */
178 if (ptrace (PTRACE_GETREGS
, inferior_pid
,
179 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
180 perror("ptrace_getregs");
182 registers
[REGISTER_BYTE (0)] = 0;
183 memcpy (®isters
[REGISTER_BYTE (1)], &inferior_registers
.r_g1
,
184 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
185 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
186 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
187 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)] = inferior_registers
.r_npc
;
188 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
190 /* Floating point registers */
192 if (ptrace (PTRACE_GETFPREGS
, inferior_pid
,
193 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
,
195 perror("ptrace_getfpregs");
196 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
197 sizeof inferior_fp_registers
.fpu_fr
);
199 /* These regs are saved on the stack by the kernel. Only read them
200 all (16 ptrace calls!) if we really need them. */
202 read_inferior_memory (*(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)],
203 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
204 16*REGISTER_RAW_SIZE (L0_REGNUM
));
207 /* Store our register values back into the inferior.
208 If REGNO is -1, do this for all registers.
209 Otherwise, REGNO specifies which register (so we can save time). */
212 store_inferior_registers (ignored
)
215 struct regs inferior_registers
;
216 struct fp_status inferior_fp_registers
;
217 CORE_ADDR sp
= *(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)];
219 write_inferior_memory (sp
, ®isters
[REGISTER_BYTE (L0_REGNUM
)],
220 16*REGISTER_RAW_SIZE (L0_REGNUM
));
222 memcpy (&inferior_registers
.r_g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
223 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
225 inferior_registers
.r_ps
=
226 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
227 inferior_registers
.r_pc
=
228 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
229 inferior_registers
.r_npc
=
230 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)];
231 inferior_registers
.r_y
=
232 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)];
234 if (ptrace (PTRACE_SETREGS
, inferior_pid
,
235 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
236 perror("ptrace_setregs");
238 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
239 sizeof inferior_fp_registers
.fpu_fr
);
241 if (ptrace (PTRACE_SETFPREGS
, inferior_pid
,
242 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0))
243 perror("ptrace_setfpregs");
246 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
247 in the NEW_SUN_PTRACE case.
248 It ought to be straightforward. But it appears that writing did
249 not write the data that I specified. I cannot understand where
250 it got the data that it actually did write. */
252 /* Copy LEN bytes from inferior's memory starting at MEMADDR
253 to debugger memory starting at MYADDR. */
255 read_inferior_memory (memaddr
, myaddr
, len
)
261 /* Round starting address down to longword boundary. */
262 register CORE_ADDR addr
= memaddr
& -sizeof (int);
263 /* Round ending address up; get number of longwords that makes. */
265 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
266 /* Allocate buffer of that many longwords. */
267 register int *buffer
= (int *) alloca (count
* sizeof (int));
269 /* Read all the longwords */
270 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
272 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
275 /* Copy appropriate bytes out of the buffer. */
276 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
279 /* Copy LEN bytes of data from debugger memory at MYADDR
280 to inferior's memory at MEMADDR.
281 On failure (cannot write the inferior)
282 returns the value of errno. */
285 write_inferior_memory (memaddr
, myaddr
, len
)
291 /* Round starting address down to longword boundary. */
292 register CORE_ADDR addr
= memaddr
& -sizeof (int);
293 /* Round ending address up; get number of longwords that makes. */
295 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
296 /* Allocate buffer of that many longwords. */
297 register int *buffer
= (int *) alloca (count
* sizeof (int));
300 /* Fill start and end extra bytes of buffer with existing memory data. */
302 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
307 = ptrace (1, inferior_pid
,
308 addr
+ (count
- 1) * sizeof (int), 0);
311 /* Copy data to be written over corresponding part of buffer */
313 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
315 /* Write the entire buffer. */
317 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
320 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
337 return inferior_pid
!= 0;