1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1986, 1987, 1993, 1994, 1995, 1999, 2000, 2001, 2002
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. */
23 #include "<sys/wait.h>"
28 #include <sys/param.h>
32 #include <sys/ioctl.h>
36 /***************Begin MY defs*********************/
37 static char my_registers
[REGISTER_BYTES
];
38 char *registers
= my_registers
;
39 /***************End MY defs*********************/
41 #include <sys/ptrace.h>
42 #include <machine/reg.h>
45 extern char **sys_errlist
;
48 /* Start an inferior process and returns its pid.
49 ALLARGS is a vector of program-name and args. */
52 create_inferior (char *program
, char **allargs
)
58 perror_with_name ("fork");
62 ptrace (PTRACE_TRACEME
);
64 execv (program
, allargs
);
66 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
67 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
75 /* Attaching is not supported. */
82 /* Kill the inferior process. Make us have no inferior. */
87 if (inferior_pid
== 0)
89 ptrace (8, inferior_pid
, 0, 0);
91 /*************inferior_died ();****VK**************/
94 /* Return nonzero if the given thread is still alive. */
96 mythread_alive (int pid
)
101 /* Wait for process, returns status */
104 mywait (char *status
)
110 if (pid
!= inferior_pid
)
111 perror_with_name ("wait");
115 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
117 return ((unsigned char) WEXITSTATUS (w
));
119 else if (!WIFSTOPPED (w
))
121 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
123 return ((unsigned char) WTERMSIG (w
));
126 fetch_inferior_registers (0);
129 return ((unsigned char) WSTOPSIG (w
));
132 /* Resume execution of the inferior process.
133 If STEP is nonzero, single-step it.
134 If SIGNAL is nonzero, give it that signal. */
137 myresume (int step
, int signal
)
140 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, inferior_pid
, 1, signal
);
142 perror_with_name ("ptrace");
145 /* Fetch one or more registers from the inferior. REGNO == -1 to get
146 them all. We actually fetch more than requested, when convenient,
147 marking them as valid so we won't fetch them again. */
150 fetch_inferior_registers (int ignored
)
152 struct regs inferior_registers
;
153 struct fp_status inferior_fp_registers
;
155 ptrace (PTRACE_GETREGS
, inferior_pid
,
156 (PTRACE_ARG3_TYPE
) & inferior_registers
);
158 ptrace (PTRACE_GETFPREGS
, inferior_pid
,
159 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
);
162 memcpy (registers
, &inferior_registers
, 16 * 4);
164 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
165 sizeof inferior_fp_registers
.fps_regs
);
167 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
168 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
171 (®isters
[REGISTER_BYTE (FPC_REGNUM
)],
172 &inferior_fp_registers
.fps_control
,
173 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
177 /* Store our register values back into the inferior.
178 If REGNO is -1, do this for all registers.
179 Otherwise, REGNO specifies which register (so we can save time). */
182 store_inferior_registers (int ignored
)
184 struct regs inferior_registers
;
185 struct fp_status inferior_fp_registers
;
187 memcpy (&inferior_registers
, registers
, 16 * 4);
189 memcpy (&inferior_fp_registers
,
190 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
191 sizeof inferior_fp_registers
.fps_regs
);
193 inferior_registers
.r_ps
= *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)];
194 inferior_registers
.r_pc
= *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
197 memcpy (&inferior_fp_registers
.fps_control
,
198 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
199 (sizeof inferior_fp_registers
200 - sizeof inferior_fp_registers
.fps_regs
));
203 ptrace (PTRACE_SETREGS
, inferior_pid
,
204 (PTRACE_ARG3_TYPE
) & inferior_registers
);
206 ptrace (PTRACE_SETFPREGS
, inferior_pid
,
207 (PTRACE_ARG3_TYPE
) & inferior_fp_registers
);
211 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
212 in the NEW_SUN_PTRACE case.
213 It ought to be straightforward. But it appears that writing did
214 not write the data that I specified. I cannot understand where
215 it got the data that it actually did write. */
217 /* Copy LEN bytes from inferior's memory starting at MEMADDR
218 to debugger memory starting at MYADDR. */
221 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
224 /* Round starting address down to longword boundary. */
225 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
226 /* Round ending address up; get number of longwords that makes. */
228 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
229 /* Allocate buffer of that many longwords. */
230 register int *buffer
= (int *) alloca (count
* sizeof (int));
232 /* Read all the longwords */
233 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
235 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
238 /* Copy appropriate bytes out of the buffer. */
239 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
242 /* Copy LEN bytes of data from debugger memory at MYADDR
243 to inferior's memory at MEMADDR.
244 On failure (cannot write the inferior)
245 returns the value of errno. */
248 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
251 /* Round starting address down to longword boundary. */
252 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (int);
253 /* Round ending address up; get number of longwords that makes. */
255 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
256 /* Allocate buffer of that many longwords. */
257 register int *buffer
= (int *) alloca (count
* sizeof (int));
260 /* Fill start and end extra bytes of buffer with existing memory data. */
262 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
267 = ptrace (1, inferior_pid
,
268 addr
+ (count
- 1) * sizeof (int), 0);
271 /* Copy data to be written over corresponding part of buffer */
273 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
275 /* Write the entire buffer. */
277 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
280 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
289 initialize_low (void)