1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1986, 1987, 1993, 2000 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. */
21 #include <sys/types.h>
29 /***************Begin MY defs*********************/
31 static char my_registers
[REGISTER_BYTES
];
32 char *registers
= my_registers
;
34 /* Index within `registers' of the first byte of the space for
37 char buf2
[MAX_REGISTER_RAW_SIZE
];
38 /***************End MY defs*********************/
40 #include <sys/ptrace.h>
41 #include <machine/reg.h>
44 // extern char **sys_errlist;
45 extern char **environ
;
46 extern int inferior_pid
;
47 void quit (), perror_with_name ();
50 /* i386_register_raw_size[i] is the number of bytes of storage in the
51 actual machine representation for register i. */
52 int i386_register_raw_size
[MAX_NUM_REGS
] = {
66 int i386_register_byte
[MAX_NUM_REGS
];
71 /* Initialize the table saying where each register starts in the
77 for (i
= 0; i
< MAX_NUM_REGS
; i
++)
79 i386_register_byte
[i
] = offset
;
80 offset
+= i386_register_raw_size
[i
];
88 /* Start an inferior process and returns its pid.
89 ALLARGS is a vector of program-name and args.
90 ENV is the environment vector to pass. */
93 create_inferior (program
, allargs
)
101 perror_with_name ("fork");
105 ptrace (PT_TRACE_ME
, 0, 0, 0);
107 execv (program
, allargs
);
109 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
110 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
118 /* Kill the inferior process. Make us have no inferior. */
123 if (inferior_pid
== 0)
125 ptrace (PT_KILL
, inferior_pid
, 0, 0);
127 /*************inferior_died ();****VK**************/
130 /* Return nonzero if the given thread is still alive. */
138 /* Wait for process, returns status */
148 if (pid
!= inferior_pid
)
149 perror_with_name ("wait");
153 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
155 return ((unsigned char) WEXITSTATUS (w
));
157 else if (!WIFSTOPPED (w
))
159 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
161 return ((unsigned char) WTERMSIG (w
));
164 fetch_inferior_registers (0);
167 return ((unsigned char) WSTOPSIG (w
));
170 /* Resume execution of the inferior process.
171 If STEP is nonzero, single-step it.
172 If SIGNAL is nonzero, give it that signal. */
175 myresume (step
, signal
)
180 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
,
181 (PTRACE_ARG3_TYPE
) 1, signal
);
183 perror_with_name ("ptrace");
186 /* Fetch one or more registers from the inferior. REGNO == -1 to get
187 them all. We actually fetch more than requested, when convenient,
188 marking them as valid so we won't fetch them again. */
191 fetch_inferior_registers (ignored
)
194 struct reg inferior_registers
;
195 struct fpreg inferior_fp_registers
;
197 ptrace (PT_GETREGS
, inferior_pid
,
198 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
199 memcpy (®isters
[REGISTER_BYTE(0)], &inferior_registers
,
200 sizeof(inferior_registers
));
202 #if 0 /* def FP0_REGNUM */
203 ptrace (PT_GETFPREGS
, inferior_pid
,
204 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
205 memcpy (®isters
[REGISTER_BYTE(FP0_REGNUM
)], &inferior_fp_registers
,
206 sizeof(inferior_fp_registers
));
210 /* Store our register values back into the inferior.
211 If REGNO is -1, do this for all registers.
212 Otherwise, REGNO specifies which register (so we can save time). */
215 store_inferior_registers (ignored
)
218 struct reg inferior_registers
;
219 struct fpreg inferior_fp_registers
;
221 memcpy (&inferior_registers
, ®isters
[REGISTER_BYTE(0)],
222 sizeof(inferior_registers
));
223 ptrace (PT_SETREGS
, inferior_pid
,
224 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0);
226 #if 0 /* def FP0_REGNUM */
227 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
228 sizeof (inferior_fp_registers
));
229 ptrace (PT_SETFPREGS
, inferior_pid
,
230 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0);
234 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
235 in the NEW_SUN_PTRACE case.
236 It ought to be straightforward. But it appears that writing did
237 not write the data that I specified. I cannot understand where
238 it got the data that it actually did write. */
240 /* Copy LEN bytes from inferior's memory starting at MEMADDR
241 to debugger memory starting at MYADDR. */
243 read_inferior_memory (memaddr
, myaddr
, len
)
249 /* Round starting address down to longword boundary. */
250 register CORE_ADDR addr
= memaddr
& -sizeof (int);
251 /* Round ending address up; get number of longwords that makes. */
253 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
254 /* Allocate buffer of that many longwords. */
255 register int *buffer
= (int *) alloca (count
* sizeof (int));
257 /* Read all the longwords */
258 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
260 buffer
[i
] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
263 /* Copy appropriate bytes out of the buffer. */
264 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
267 /* Copy LEN bytes of data from debugger memory at MYADDR
268 to inferior's memory at MEMADDR.
269 On failure (cannot write the inferior)
270 returns the value of errno. */
273 write_inferior_memory (memaddr
, myaddr
, len
)
279 /* Round starting address down to longword boundary. */
280 register CORE_ADDR addr
= memaddr
& -sizeof (int);
281 /* Round ending address up; get number of longwords that makes. */
283 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
284 /* Allocate buffer of that many longwords. */
285 register int *buffer
= (int *) alloca (count
* sizeof (int));
288 /* Fill start and end extra bytes of buffer with existing memory data. */
290 buffer
[0] = ptrace (PT_READ_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
295 = ptrace (PT_READ_D
, inferior_pid
,
296 (PTRACE_ARG3_TYPE
) addr
+ (count
- 1) * sizeof (int), 0);
299 /* Copy data to be written over corresponding part of buffer */
301 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
303 /* Write the entire buffer. */
305 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
308 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);