1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1999, 2000, 2001 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,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/param.h>
31 #include <sys/ioctl.h>
35 /***************Begin MY defs*********************/
36 static char my_registers
[REGISTER_BYTES
];
37 char *registers
= my_registers
;
38 /***************End MY defs*********************/
40 #include <sys/ptrace.h>
41 #include <machine/reg.h>
45 /* Start an inferior process and returns its pid.
46 ALLARGS is a vector of program-name and args. */
49 create_inferior (char *program
, char **allargs
)
55 perror_with_name ("fork");
59 ptrace (PT_TRACE_ME
, 0, 0, 0, 0);
61 execv (program
, allargs
);
63 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
64 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
72 /* Kill the inferior process. Make us have no inferior. */
77 if (inferior_pid
== 0)
79 ptrace (8, inferior_pid
, 0, 0, 0);
81 /*************inferior_died ();****VK**************/
84 /* Return nonzero if the given thread is still alive. */
86 mythread_alive (int pid
)
91 /* Wait for process, returns status */
100 if (pid
!= inferior_pid
)
101 perror_with_name ("wait");
105 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
107 return ((unsigned char) WEXITSTATUS (w
));
109 else if (!WIFSTOPPED (w
))
111 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
113 return ((unsigned char) WTERMSIG (w
));
116 fetch_inferior_registers (0);
119 return ((unsigned char) WSTOPSIG (w
));
122 /* Resume execution of the inferior process.
123 If STEP is nonzero, single-step it.
124 If SIGNAL is nonzero, give it that signal. */
127 myresume (int step
, int signal
)
130 ptrace (step
? PT_STEP
: PT_CONTINUE
, inferior_pid
, 1, signal
, 0);
132 perror_with_name ("ptrace");
136 #if !defined (offsetof)
137 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
140 /* U_REGS_OFFSET is the offset of the registers within the u area. */
141 #if !defined (U_REGS_OFFSET)
142 #define U_REGS_OFFSET \
143 ptrace (PT_READ_U, inferior_pid, \
144 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
149 register_addr (int regno
, CORE_ADDR blockend
)
153 if (regno
< 0 || regno
>= NUM_REGS
)
154 error ("Invalid register number %d.", regno
);
156 REGISTER_U_ADDR (addr
, blockend
, regno
);
161 /* Fetch one register. */
164 fetch_register (int regno
)
166 register unsigned int regaddr
;
167 char buf
[MAX_REGISTER_RAW_SIZE
];
170 /* Offset of registers within the u area. */
173 offset
= U_REGS_OFFSET
;
175 regaddr
= register_addr (regno
, offset
);
176 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
179 *(int *) ®isters
[regno
* 4 + i
] = ptrace (PT_RUREGS
, inferior_pid
,
180 (PTRACE_ARG3_TYPE
) regaddr
, 0, 0);
181 regaddr
+= sizeof (int);
184 /* Warning, not error, in case we are attached; sometimes the
185 kernel doesn't let us at the registers. */
186 char *err
= strerror (errno
);
187 char *msg
= alloca (strlen (err
) + 128);
188 sprintf (msg
, "reading register %d: %s", regno
, err
);
196 /* Fetch all registers, or just one, from the child process. */
199 fetch_inferior_registers (int regno
)
201 if (regno
== -1 || regno
== 0)
202 for (regno
= 0; regno
< NUM_REGS
; regno
++)
203 fetch_register (regno
);
205 fetch_register (regno
);
208 /* Store our register values back into the inferior.
209 If REGNO is -1, do this for all registers.
210 Otherwise, REGNO specifies which register (so we can save time). */
213 store_inferior_registers (int regno
)
215 register unsigned int regaddr
;
217 extern char registers
[];
219 unsigned int offset
= U_REGS_OFFSET
;
224 if (CANNOT_STORE_REGISTER (regno
))
226 regaddr
= register_addr (regno
, offset
);
228 if (regno
== PCOQ_HEAD_REGNUM
|| regno
== PCOQ_TAIL_REGNUM
)
230 scratch
= *(int *) ®isters
[REGISTER_BYTE (regno
)] | 0x3;
231 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
235 /* Error, even if attached. Failing to write these two
236 registers is pretty serious. */
237 sprintf (buf
, "writing register number %d", regno
);
238 perror_with_name (buf
);
242 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
245 ptrace (PT_WUREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
246 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
], 0);
249 /* Warning, not error, in case we are attached; sometimes the
250 kernel doesn't let us at the registers. */
251 char *err
= strerror (errno
);
252 char *msg
= alloca (strlen (err
) + 128);
253 sprintf (msg
, "writing register %d: %s",
258 regaddr
+= sizeof (int);
262 for (regno
= 0; regno
< NUM_REGS
; regno
++)
263 store_inferior_registers (regno
);
266 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
267 in the NEW_SUN_PTRACE case.
268 It ought to be straightforward. But it appears that writing did
269 not write the data that I specified. I cannot understand where
270 it got the data that it actually did write. */
272 /* Copy LEN bytes from inferior's memory starting at MEMADDR
273 to debugger memory starting at MYADDR. */
275 read_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
278 /* Round starting address down to longword boundary. */
279 register CORE_ADDR addr
= memaddr
& -sizeof (int);
280 /* Round ending address up; get number of longwords that makes. */
282 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
283 /* Allocate buffer of that many longwords. */
284 register int *buffer
= (int *) alloca (count
* sizeof (int));
286 /* Read all the longwords */
287 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
289 buffer
[i
] = ptrace (1, inferior_pid
, addr
, 0, 0);
292 /* Copy appropriate bytes out of the buffer. */
293 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (int) - 1)), len
);
296 /* Copy LEN bytes of data from debugger memory at MYADDR
297 to inferior's memory at MEMADDR.
298 On failure (cannot write the inferior)
299 returns the value of errno. */
302 write_inferior_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
305 /* Round starting address down to longword boundary. */
306 register CORE_ADDR addr
= memaddr
& -sizeof (int);
307 /* Round ending address up; get number of longwords that makes. */
309 = (((memaddr
+ len
) - addr
) + sizeof (int) - 1) / sizeof (int);
310 /* Allocate buffer of that many longwords. */
311 register int *buffer
= (int *) alloca (count
* sizeof (int));
314 /* Fill start and end extra bytes of buffer with existing memory data. */
316 buffer
[0] = ptrace (1, inferior_pid
, addr
, 0, 0);
321 = ptrace (1, inferior_pid
,
322 addr
+ (count
- 1) * sizeof (int), 0, 0);
325 /* Copy data to be written over corresponding part of buffer */
327 memcpy ((char *) buffer
+ (memaddr
& (sizeof (int) - 1)), myaddr
, len
);
329 /* Write the entire buffer. */
331 for (i
= 0; i
< count
; i
++, addr
+= sizeof (int))
334 ptrace (4, inferior_pid
, addr
, buffer
[i
], 0);
343 initialize_low (void)