1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
9 /***************************
10 #include "initialize.h"
11 ****************************/
14 #include <sys/param.h>
18 #include <sys/ioctl.h>
22 /***************Begin MY defs*********************/
24 char registers
[REGISTER_BYTES
];
26 /* Index within `registers' of the first byte of the space for
30 char buf2
[MAX_REGISTER_RAW_SIZE
];
31 /***************End MY defs*********************/
33 #include <sys/ptrace.h>
34 #include <machine/reg.h>
36 extern char **environ
;
38 extern int inferior_pid
;
39 void error (), quit (), perror_with_name ();
41 void supply_register (), write_register ();
42 CORE_ADDR
read_register ();
44 /* Nonzero if we are debugging an attached outside process
45 rather than an inferior. */
48 /* Start an inferior process and returns its pid.
49 ALLARGS is a vector of program-name and args.
50 ENV is the environment vector to pass. */
53 create_inferior (allargs
, env
)
59 extern char *sys_errlist
[];
64 /* exec is said to fail if the executable is open. */
65 /****************close_exec_file ();*****************/
67 sprintf (execbuf
, "exec %s", allargs
);
71 perror_with_name ("vfork");
75 /* Run inferior in a separate process group. */
76 setpgrp (getpid (), getpid ());
79 ptrace (PTRACE_TRACEME
);
81 execle ("/bin/sh", "sh", "-c", execbuf
, 0, env
);
83 fprintf (stderr
, "Cannot exec /bin/sh: %s.\n",
84 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
92 /* Kill the inferior process. Make us have no inferior. */
97 if (inferior_pid
== 0)
99 ptrace (8, inferior_pid
, 0, 0);
101 /*************inferior_died ();****VK**************/
104 /* Resume execution of the inferior process.
105 If STEP is nonzero, single-step it.
106 If SIGNAL is nonzero, give it that signal. */
109 myresume (step
, signal
, status
)
118 ptrace (step
? 9 : 7, inferior_pid
, 1, signal
);
120 perror_with_name ("ptrace");
122 if (pid
!= inferior_pid
)
123 perror_with_name ("wait");
125 fetch_inferior_registers (0);
129 printf ("\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
131 return ((unsigned char) WEXITSTATUS (w
));
133 else if (!WIFSTOPPED (w
))
135 printf ("\nChild terminated with signal = %x \n", WTERMSIG (w
));
137 return ((unsigned char) WTERMSIG (w
));
141 printf ("\nChild stopped with signal = %x \n", WSTOPSIG (w
));
143 return ((unsigned char) WSTOPSIG (w
));
151 /* Fetch one or more registers from the inferior. REGNO == -1 to get
152 them all. We actually fetch more than requested, when convenient,
153 marking them as valid so we won't fetch them again. */
156 fetch_inferior_registers (ignored
)
159 struct regs inferior_registers
;
160 struct fp_status inferior_fp_registers
;
163 /* Global and Out regs are fetched directly, as well as the control
164 registers. If we're getting one of the in or local regs,
165 and the stack pointer has not yet been fetched,
166 we have to do that first, since they're found in memory relative
167 to the stack pointer. */
169 if (ptrace (PTRACE_GETREGS
, inferior_pid
,
170 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
171 perror("ptrace_getregs");
173 registers
[REGISTER_BYTE (0)] = 0;
174 memcpy (®isters
[REGISTER_BYTE (1)], &inferior_registers
.r_g1
,
175 15 * REGISTER_RAW_SIZE (G0_REGNUM
));
176 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
177 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
178 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)] = inferior_registers
.r_npc
;
179 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)] = inferior_registers
.r_y
;
181 /* Floating point registers */
183 if (ptrace (PTRACE_GETFPREGS
, inferior_pid
,
184 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
,
186 perror("ptrace_getfpregs");
187 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
188 sizeof inferior_fp_registers
.fpu_fr
);
190 /* These regs are saved on the stack by the kernel. Only read them
191 all (16 ptrace calls!) if we really need them. */
193 read_inferior_memory (*(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)],
194 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
195 16*REGISTER_RAW_SIZE (L0_REGNUM
));
198 /* Store our register values back into the inferior.
199 If REGNO is -1, do this for all registers.
200 Otherwise, REGNO specifies which register (so we can save time). */
203 store_inferior_registers (ignored
)
206 struct regs inferior_registers
;
207 struct fp_status inferior_fp_registers
;
208 CORE_ADDR sp
= *(CORE_ADDR
*)®isters
[REGISTER_BYTE (SP_REGNUM
)];
210 write_inferior_memory (sp
, ®isters
[REGISTER_BYTE (L0_REGNUM
)],
211 16*REGISTER_RAW_SIZE (L0_REGNUM
));
213 memcpy (&inferior_registers
.r_g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
214 15 * REGISTER_RAW_SIZE (G1_REGNUM
));
216 inferior_registers
.r_ps
=
217 *(int *)®isters
[REGISTER_BYTE (PS_REGNUM
)];
218 inferior_registers
.r_pc
=
219 *(int *)®isters
[REGISTER_BYTE (PC_REGNUM
)];
220 inferior_registers
.r_npc
=
221 *(int *)®isters
[REGISTER_BYTE (NPC_REGNUM
)];
222 inferior_registers
.r_y
=
223 *(int *)®isters
[REGISTER_BYTE (Y_REGNUM
)];
225 if (ptrace (PTRACE_SETREGS
, inferior_pid
,
226 (PTRACE_ARG3_TYPE
) &inferior_registers
, 0))
227 perror("ptrace_setregs");
229 memcpy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
230 sizeof inferior_fp_registers
.fpu_fr
);
232 if (ptrace (PTRACE_SETFPREGS
, inferior_pid
,
233 (PTRACE_ARG3_TYPE
) &inferior_fp_registers
, 0))
234 perror("ptrace_setfpregs");
239 fetch_inferior_registers ()
241 struct regs inferior_registers
;
242 struct fp_status inferior_fp_registers
;
243 extern char registers
[];
245 ptrace (PTRACE_GETREGS
, inferior_pid
, &inferior_registers
);
247 perror_with_name ("ptrace");
248 /**********debugging begin **********/
249 print_some_registers (&inferior_registers
);
250 /**********debugging end **********/
251 ptrace (PTRACE_GETFPREGS
, inferior_pid
, &inferior_fp_registers
);
253 perror_with_name ("ptrace");
255 bcopy (&inferior_registers
, registers
, 16 * 4);
256 bcopy (&inferior_fp_registers
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
257 sizeof inferior_fp_registers
.fpu_regs
);
258 *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)] = inferior_registers
.r_ps
;
259 *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)] = inferior_registers
.r_pc
;
260 bcopy (&inferior_fp_registers
.fpu_flags
,
261 ®isters
[REGISTER_BYTE (FPC_REGNUM
)],
262 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fpu_regs
);
265 /* Store our register values back into the inferior.
266 If REGNO is -1, do this for all registers.
267 Otherwise, REGNO specifies which register (so we can save time). */
269 store_inferior_registers (regno
)
272 struct regs inferior_registers
;
273 struct fp_status inferior_fp_registers
;
274 extern char registers
[];
276 bcopy (registers
, &inferior_registers
, 16 * 4);
277 bcopy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], &inferior_fp_registers
,
278 sizeof inferior_fp_registers
.fps_regs
);
279 inferior_registers
.r_ps
= *(int *) ®isters
[REGISTER_BYTE (PS_REGNUM
)];
280 inferior_registers
.r_pc
= *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
281 bcopy (®isters
[REGISTER_BYTE (FPC_REGNUM
)],
282 &inferior_fp_registers
.fps_control
,
283 sizeof inferior_fp_registers
- sizeof inferior_fp_registers
.fps_regs
);
285 ptrace (PTRACE_SETREGS
, inferior_pid
, &inferior_registers
);
287 perror_with_name ("ptrace");
288 ptrace (PTRACE_SETFPREGS
, inferior_pid
, &inferior_fp_registers
);
290 perror_with_name ("ptrace");
294 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
295 in the NEW_SUN_PTRACE case.
296 It ought to be straightforward. But it appears that writing did
297 not write the data that I specified. I cannot understand where
298 it got the data that it actually did write. */
300 /* Copy LEN bytes from inferior's memory starting at MEMADDR
301 to debugger memory starting at MYADDR. */
303 read_inferior_memory (memaddr
, myaddr
, len
)
309 /* Round starting address down to longword boundary. */
310 register CORE_ADDR addr
= memaddr
& -sizeof (int);
311 /* Round ending address up; get number of longwords that makes. */
313 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
314 /* Allocate buffer of that many longwords. */
315 register int *buffer
= (int *) alloca (count
* sizeof (int));
317 /* Read all the longwords */
318 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
320 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0);
323 /* Copy appropriate bytes out of the buffer. */
324 bcopy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
327 /* Copy LEN bytes of data from debugger memory at MYADDR
328 to inferior's memory at MEMADDR.
329 On failure (cannot write the inferior)
330 returns the value of errno. */
333 write_inferior_memory (memaddr
, myaddr
, len
)
339 /* Round starting address down to longword boundary. */
340 register CORE_ADDR addr
= memaddr
& -sizeof (int);
341 /* Round ending address up; get number of longwords that makes. */
343 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
344 /* Allocate buffer of that many longwords. */
345 register int *buffer
= (int *) alloca (count
* sizeof (int));
348 /* Fill start and end extra bytes of buffer with existing memory data. */
350 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0);
355 = ptrace (1, inferior_pid
,
356 addr
+ (count
- 1) * sizeof (int), 0);
359 /* Copy data to be written over corresponding part of buffer */
361 bcopy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
363 /* Write the entire buffer. */
365 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
368 ptrace (4, inferior_pid
, addr
, buffer
[i
]);
377 try_writing_regs_command ()
382 if (inferior_pid
== 0)
383 error ("There is no inferior process now.");
385 fetch_inferior_registers (0);
386 for (i
= 0; i
< 18; i
++)
390 val
= read_register (i
);
391 write_register (i
, val
);
394 printf (" Succeeded with register %d; value 0x%x (%d).\n",
398 printf (" Failed with register %d.\n", i
);
412 /* Return the contents of register REGNO,
413 regarding it as an integer. */
416 read_register (regno
)
419 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
420 return *(int *) ®isters
[REGISTER_BYTE (regno
)];
423 /* Store VALUE in the register number REGNO, regarded as an integer. */
426 write_register (regno
, val
)
429 /* This loses when REGISTER_RAW_SIZE (regno) != sizeof (int) */
430 *(int *) ®isters
[REGISTER_BYTE (regno
)] = val
;
432 if (have_inferior_p ())
433 store_inferior_registers (regno
);
440 return inferior_pid
!= 0;
443 print_some_registers (regs
)
447 for (i
= 0; i
< 18; i
++)
449 printf ("reg[%d] = %x\n", i
, regs
[i
]);