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 "<sys/wait.h>"
26 #include <sys/param.h>
30 #include <sys/ioctl.h>
34 /***************Begin MY defs*********************/
36 char registers
[REGISTER_BYTES
];
38 /* Index within `registers' of the first byte of the space for
42 char buf2
[MAX_REGISTER_RAW_SIZE
];
43 /***************End MY defs*********************/
45 #include <sys/ptrace.h>
46 #include <machine/reg.h>
49 extern char **sys_errlist
;
50 extern char **environ
;
52 extern int inferior_pid
;
53 void error (), quit (), perror_with_name ();
56 /* Start an inferior process and returns its pid.
57 ALLARGS is a vector of program-name and args.
58 ENV is the environment vector to pass. */
61 create_inferior (program
, allargs
)
69 perror_with_name ("fork");
73 ptrace (PTRACE_TRACEME
);
75 execv (program
, allargs
);
77 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
78 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
86 /* Kill the inferior process. Make us have no inferior. */
91 if (inferior_pid
== 0)
93 ptrace (8, inferior_pid
, 0, 0);
95 /*************inferior_died ();****VK**************/
98 /* Wait for process, returns status */
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 (step
, 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 (ignored
)
153 struct regs inferior_registers
;
154 struct fp_status inferior_fp_registers
;
156 ptrace (PTRACE_GETREGS
, inferior_pid
,
157 (PTRACE_ARG3_TYPE
) &inferior_registers
);
159 ptrace (PTRACE_GETFPREGS
, inferior_pid
,
160 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
);
163 memcpy (registers
, &inferior_registers
, 16 * 4);
165 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
166 sizeof inferior_fp_registers
.fps_regs
);
168 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
169 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
172 (®isters
[REGISTER_BYTE (FPC_REGNUM
)],
173 &inferior_fp_registers
.fps_control
,
174 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
178 /* Store our register values back into the inferior.
179 If REGNO is -1, do this for all registers.
180 Otherwise, REGNO specifies which register (so we can save time). */
183 store_inferior_registers (ignored
)
186 struct regs inferior_registers
;
187 struct fp_status inferior_fp_registers
;
189 memcpy (&inferior_registers
, registers
, 16 * 4);
191 memcpy (&inferior_fp_registers
,
192 ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
193 sizeof inferior_fp_registers
.fps_regs
);
195 inferior_registers
.r_ps
= *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
196 inferior_registers
.r_pc
= *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
199 memcpy (&inferior_fp_registers
.fps_control
,
200 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
201 (sizeof inferior_fp_registers
202 - sizeof inferior_fp_registers
.fps_regs
));
205 ptrace (PTRACE_SETREGS
, inferior_pid
,
206 (PTRACE_ARG3_TYPE
) &inferior_registers
);
208 ptrace (PTRACE_SETFPREGS
, inferior_pid
,
209 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
);
213 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
214 in the NEW_SUN_PTRACE case.
215 It ought to be straightforward. But it appears that writing did
216 not write the data that I specified. I cannot understand where
217 it got the data that it actually did write. */
219 /* Copy LEN bytes from inferior's memory starting at MEMADDR
220 to debugger memory starting at MYADDR. */
222 read_inferior_memory (memaddr
, myaddr
, len
)
228 /* Round starting address down to longword boundary. */
229 register CORE_ADDR addr
= memaddr
& -sizeof (int);
230 /* Round ending address up; get number of longwords that makes. */
232 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
233 /* Allocate buffer of that many longwords. */
234 register int *buffer
= (int *) alloca (count
* sizeof (int));
236 /* Read all the longwords */
237 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
239 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
242 /* Copy appropriate bytes out of the buffer. */
243 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
246 /* Copy LEN bytes of data from debugger memory at MYADDR
247 to inferior's memory at MEMADDR.
248 On failure (cannot write the inferior)
249 returns the value of errno. */
252 write_inferior_memory (memaddr
, myaddr
, len
)
258 /* Round starting address down to longword boundary. */
259 register CORE_ADDR addr
= memaddr
& -sizeof (int);
260 /* Round ending address up; get number of longwords that makes. */
262 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
263 /* Allocate buffer of that many longwords. */
264 register int *buffer
= (int *) alloca (count
* sizeof (int));
267 /* Fill start and end extra bytes of buffer with existing memory data. */
269 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
274 = ptrace (1, inferior_pid
,
275 addr
+ (count
- 1) * sizeof (int), 0);
278 /* Copy data to be written over corresponding part of buffer */
280 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
282 /* Write the entire buffer. */
284 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
287 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
304 return inferior_pid
!= 0;