1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995 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. */
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>
48 extern char **environ;
50 extern int inferior_pid;
51 void quit (), perror_with_name ();
54 /* Start an inferior process and returns its pid.
55 ALLARGS is a vector of program-name and args.
56 ENV is the environment vector to pass. */
59 create_inferior (program, allargs)
67 perror_with_name ("fork");
71 ptrace (PT_TRACE_ME, 0, 0, 0, 0);
73 execv (program, allargs);
75 fprintf (stderr, "Cannot exec %s: %s.\n", program,
76 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
84 /* Kill the inferior process. Make us have no inferior. */
89 if (inferior_pid == 0)
91 ptrace (8, inferior_pid, 0, 0, 0);
93 /*************inferior_died ();****VK**************/
96 /* Return nonzero if the given thread is still alive. */
104 /* Wait for process, returns status */
114 if (pid != inferior_pid)
115 perror_with_name ("wait");
119 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
121 return ((unsigned char) WEXITSTATUS (w));
123 else if (!WIFSTOPPED (w))
125 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
127 return ((unsigned char) WTERMSIG (w));
130 fetch_inferior_registers (0);
133 return ((unsigned char) WSTOPSIG (w));
136 /* Resume execution of the inferior process.
137 If STEP is nonzero, single-step it.
138 If SIGNAL is nonzero, give it that signal. */
141 myresume (step, signal)
146 ptrace (step ? PT_STEP : PT_CONTINUE, inferior_pid, 1, signal, 0);
148 perror_with_name ("ptrace");
152 #if !defined (offsetof)
153 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
156 /* U_REGS_OFFSET is the offset of the registers within the u area. */
157 #if !defined (U_REGS_OFFSET)
158 #define U_REGS_OFFSET \
159 ptrace (PT_READ_U, inferior_pid, \
160 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
165 register_addr (regno, blockend)
171 if (regno < 0 || regno >= ARCH_NUM_REGS)
172 error ("Invalid register number %d.", regno);
174 REGISTER_U_ADDR (addr, blockend, regno);
179 /* Fetch one register. */
182 fetch_register (regno)
185 register unsigned int regaddr;
186 char buf[MAX_REGISTER_RAW_SIZE];
189 /* Offset of registers within the u area. */
192 offset = U_REGS_OFFSET;
194 regaddr = register_addr (regno, offset);
195 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
198 *(int *) ®isters[ regno * 4 + i] = ptrace (PT_RUREGS, inferior_pid,
199 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
200 regaddr += sizeof (int);
203 /* Warning, not error, in case we are attached; sometimes the
204 kernel doesn't let us at the registers. */
205 char *err = strerror (errno);
206 char *msg = alloca (strlen (err) + 128);
207 sprintf (msg, "reading register %d: %s", regno, err);
215 /* Fetch all registers, or just one, from the child process. */
218 fetch_inferior_registers (regno)
221 if (regno == -1 || regno == 0)
222 for (regno = 0; regno < NUM_REGS; regno++)
223 fetch_register (regno);
225 fetch_register (regno);
228 /* Store our register values back into the inferior.
229 If REGNO is -1, do this for all registers.
230 Otherwise, REGNO specifies which register (so we can save time). */
233 store_inferior_registers (regno)
236 register unsigned int regaddr;
238 extern char registers[];
240 unsigned int offset = U_REGS_OFFSET;
245 if (CANNOT_STORE_REGISTER (regno))
247 regaddr = register_addr (regno, offset);
249 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
251 scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3;
252 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
256 /* Error, even if attached. Failing to write these two
257 registers is pretty serious. */
258 sprintf (buf, "writing register number %d", regno);
259 perror_with_name (buf);
263 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
266 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
267 *(int *) ®isters[REGISTER_BYTE (regno) + i], 0);
270 /* Warning, not error, in case we are attached; sometimes the
271 kernel doesn't let us at the registers. */
272 char *err = strerror (errno);
273 char *msg = alloca (strlen (err) + 128);
274 sprintf (msg, "writing register %d: %s",
279 regaddr += sizeof(int);
283 for (regno = 0; regno < NUM_REGS; regno++)
284 store_inferior_registers (regno);
287 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
288 in the NEW_SUN_PTRACE case.
289 It ought to be straightforward. But it appears that writing did
290 not write the data that I specified. I cannot understand where
291 it got the data that it actually did write. */
293 /* Copy LEN bytes from inferior's memory starting at MEMADDR
294 to debugger memory starting at MYADDR. */
296 read_inferior_memory (memaddr, myaddr, len)
302 /* Round starting address down to longword boundary. */
303 register CORE_ADDR addr = memaddr & -sizeof (int);
304 /* Round ending address up; get number of longwords that makes. */
306 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
307 /* Allocate buffer of that many longwords. */
308 register int *buffer = (int *) alloca (count * sizeof (int));
310 /* Read all the longwords */
311 for (i = 0; i < count; i++, addr += sizeof (int))
313 buffer[i] = ptrace (1, inferior_pid, addr, 0, 0);
316 /* Copy appropriate bytes out of the buffer. */
317 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
320 /* Copy LEN bytes of data from debugger memory at MYADDR
321 to inferior's memory at MEMADDR.
322 On failure (cannot write the inferior)
323 returns the value of errno. */
326 write_inferior_memory (memaddr, myaddr, len)
332 /* Round starting address down to longword boundary. */
333 register CORE_ADDR addr = memaddr & -sizeof (int);
334 /* Round ending address up; get number of longwords that makes. */
336 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
337 /* Allocate buffer of that many longwords. */
338 register int *buffer = (int *) alloca (count * sizeof (int));
341 /* Fill start and end extra bytes of buffer with existing memory data. */
343 buffer[0] = ptrace (1, inferior_pid, addr, 0, 0);
348 = ptrace (1, inferior_pid,
349 addr + (count - 1) * sizeof (int), 0, 0);
352 /* Copy data to be written over corresponding part of buffer */
354 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
356 /* Write the entire buffer. */
358 for (i = 0; i < count; i++, addr += sizeof (int))
361 ptrace (4, inferior_pid, addr, buffer[i], 0);
378 return inferior_pid != 0;