1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1986, 1987, 1993, 1994, 1995, 1997, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 /***************************
27 #include "initialize.h"
28 ****************************/
31 #include <sys/param.h>
35 #include <sys/ioctl.h>
39 /***************Begin MY defs*********************/
40 static char my_registers
[REGISTER_BYTES
];
41 char *registers
= my_registers
;
42 /***************End MY defs*********************/
44 #include <sys/ptrace.h>
48 extern char **sys_errlist
;
51 /* Start an inferior process and returns its pid.
52 ALLARGS is a vector of program-name and args. */
55 create_inferior (char *program
, char **allargs
)
61 perror_with_name ("fork");
65 ptrace (PTRACE_TRACEME
);
67 execv (program
, allargs
);
69 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
70 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
78 /* Kill the inferior process. Make us have no inferior. */
83 if (inferior_pid
== 0)
85 ptrace (8, inferior_pid
, 0, 0);
87 /*************inferior_died ();****VK**************/
90 /* Return nonzero if the given thread is still alive. */
92 mythread_alive (int pid
)
97 /* Wait for process, returns status */
100 mywait (char *status
)
106 pid
= waitpid (inferior_pid
, &w
, 0);
108 if (pid
!= inferior_pid
)
109 perror_with_name ("wait");
113 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
115 return ((unsigned char) WEXITSTATUS (w
));
117 else if (!WIFSTOPPED (w
))
119 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
121 return ((unsigned char) WTERMSIG (w
));
124 fetch_inferior_registers (0);
127 return ((unsigned char) WSTOPSIG (w
));
130 /* Resume execution of the inferior process.
131 If STEP is nonzero, single-step it.
132 If SIGNAL is nonzero, give it that signal. */
135 myresume (int step
, int signal
)
138 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
140 perror_with_name ("ptrace");
143 /* Fetch one or more registers from the inferior. REGNO == -1 to get
144 them all. We actually fetch more than requested, when convenient,
145 marking them as valid so we won't fetch them again. */
148 fetch_inferior_registers (int ignored
)
150 struct regs inferior_registers
;
151 struct fp_status inferior_fp_registers
;
154 /* Global and Out regs are fetched directly, as well as the control
155 registers. If we're getting one of the in or local regs,
156 and the stack pointer has not yet been fetched,
157 we have to do that first, since they're found in memory relative
158 to the stack pointer. */
160 if (ptrace (PTRACE_GETREGS
, inferior_pid
,
161 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
162 perror ("ptrace_getregs");
164 registers
[REGISTER_BYTE (0)] = 0;
165 memcpy (®isters
[REGISTER_BYTE (1)], &inferior_registers
.r_g1
,
166 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
167 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
168 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
169 *(int *) ®isters
[REGISTER_BYTE (NPC_REGNUM
)] = inferior_registers
.r_npc
;
170 *(int *) ®isters
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
172 /* Floating point registers */
174 if (ptrace (PTRACE_GETFPREGS
, inferior_pid
,
175 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
,
177 perror ("ptrace_getfpregs");
178 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
179 sizeof inferior_fp_registers
.fpu_fr
);
181 /* These regs are saved on the stack by the kernel. Only read them
182 all (16 ptrace calls!) if we really need them. */
184 read_inferior_memory (*(CORE_ADDR
*) & registers
[REGISTER_BYTE (SP_REGNUM
)],
185 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
186 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
189 /* Store our register values back into the inferior.
190 If REGNO is -1, do this for all registers.
191 Otherwise, REGNO specifies which register (so we can save time). */
194 store_inferior_registers (int ignored
)
196 struct regs inferior_registers
;
197 struct fp_status inferior_fp_registers
;
198 CORE_ADDR sp
= *(CORE_ADDR
*) & registers
[REGISTER_BYTE (SP_REGNUM
)];
200 write_inferior_memory (sp
, ®isters
[REGISTER_BYTE (L0_REGNUM
)],
201 16 * REGISTER_RAW_SIZE (L0_REGNUM
));
203 memcpy (&inferior_registers
.r_g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
204 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
206 inferior_registers
.r_ps
=
207 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)];
208 inferior_registers
.r_pc
=
209 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
210 inferior_registers
.r_npc
=
211 *(int *) ®isters
[REGISTER_BYTE (NPC_REGNUM
)];
212 inferior_registers
.r_y
=
213 *(int *) ®isters
[REGISTER_BYTE (Y_REGNUM
)];
215 if (ptrace (PTRACE_SETREGS
, inferior_pid
,
216 (PTRACE_ARG3_TYPE
) & inferior_registers
, 0))
217 perror ("ptrace_setregs");
219 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
220 sizeof inferior_fp_registers
.fpu_fr
);
222 if (ptrace (PTRACE_SETFPREGS
, inferior_pid
,
223 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
, 0))
224 perror ("ptrace_setfpregs");
227 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
228 in the NEW_SUN_PTRACE case.
229 It ought to be straightforward. But it appears that writing did
230 not write the data that I specified. I cannot understand where
231 it got the data that it actually did write. */
233 /* Copy LEN bytes from inferior's memory starting at MEMADDR
234 to debugger memory starting at MYADDR. */
237 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
240 /* Round starting address down to longword boundary. */
241 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
242 /* Round ending address up; get number of longwords that makes. */
244 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
245 /* Allocate buffer of that many longwords. */
246 register int *buffer
= (int *) alloca (count
* sizeof (int));
248 /* Read all the longwords */
249 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
251 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
254 /* Copy appropriate bytes out of the buffer. */
255 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
258 /* Copy LEN bytes of data from debugger memory at MYADDR
259 to inferior's memory at MEMADDR.
260 On failure (cannot write the inferior)
261 returns the value of errno. */
264 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
267 /* Round starting address down to longword boundary. */
268 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
269 /* Round ending address up; get number of longwords that makes. */
271 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
272 /* Allocate buffer of that many longwords. */
273 register int *buffer
= (int *) alloca (count
* sizeof (int));
276 /* Fill start and end extra bytes of buffer with existing memory data. */
278 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
283 = ptrace (1, inferior_pid
,
284 addr
+ (count
- 1) * sizeof (int), 0);
287 /* Copy data to be written over corresponding part of buffer */
289 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
291 /* Write the entire buffer. */
293 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
296 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
305 initialize_low (void)