1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996 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>
36 /***************Begin MY defs*********************/
38 char registers[REGISTER_BYTES];
40 /* Index within `registers' of the first byte of the space for
44 char buf2[MAX_REGISTER_RAW_SIZE];
45 /***************End MY defs*********************/
47 #include <sys/ptrace.h>
49 #include <machine/reg.h>
52 extern char **environ;
54 extern int inferior_pid;
55 void quit (), perror_with_name ();
58 /* Start an inferior process and returns its pid.
59 ALLARGS is a vector of program-name and args.
60 ENV is the environment vector to pass. */
63 create_inferior (program, allargs)
71 perror_with_name ("fork");
75 ptrace (PTRACE_TRACEME, 0, 0, 0);
77 execv (program, allargs);
79 fprintf (stderr, "Cannot exec %s: %s.\n", program,
80 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
88 /* Kill the inferior process. Make us have no inferior. */
93 if (inferior_pid == 0)
95 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
97 /*************inferior_died ();****VK**************/
100 /* Return nonzero if the given thread is still alive. */
108 /* Wait for process, returns status */
118 if (pid != inferior_pid)
119 perror_with_name ("wait");
123 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
125 return ((unsigned char) WEXITSTATUS (w));
127 else if (!WIFSTOPPED (w))
129 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
131 return ((unsigned char) WTERMSIG (w));
134 fetch_inferior_registers (0);
137 return ((unsigned char) WSTOPSIG (w));
140 /* Resume execution of the inferior process.
141 If STEP is nonzero, single-step it.
142 If SIGNAL is nonzero, give it that signal. */
145 myresume (step, signal)
150 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
152 perror_with_name ("ptrace");
156 #if !defined (offsetof)
157 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
160 /* U_REGS_OFFSET is the offset of the registers within the u area. */
161 #if !defined (U_REGS_OFFSET)
162 #define U_REGS_OFFSET \
163 ptrace (PT_READ_U, inferior_pid, \
164 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
168 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
169 /* symbols like 'EAX' come from <sys/reg.h> */
170 static int regmap[] =
179 i386_register_u_addr (blockend, regnum)
184 /* this will be needed if fp registers are reinstated */
185 /* for now, you can look at them with 'info float'
186 * sys5 wont let you change them with ptrace anyway
188 if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM)
192 ubase = blockend + 4 * (SS + 1) - KSTKSZ;
193 fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
194 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
198 return (blockend + 4 * regmap[regnum]);
203 register_addr (regno, blockend)
209 if (regno < 0 || regno >= ARCH_NUM_REGS)
210 error ("Invalid register number %d.", regno);
212 REGISTER_U_ADDR (addr, blockend, regno);
217 /* Fetch one register. */
220 fetch_register (regno)
223 register unsigned int regaddr;
226 /* Offset of registers within the u area. */
229 offset = U_REGS_OFFSET;
231 regaddr = register_addr (regno, offset);
232 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
235 *(int *) ®isters[ regno * 4 + i] = ptrace (PTRACE_PEEKUSR, inferior_pid,
236 (PTRACE_ARG3_TYPE) regaddr, 0);
237 regaddr += sizeof (int);
240 /* Warning, not error, in case we are attached; sometimes the
241 kernel doesn't let us at the registers. */
242 char *err = strerror (errno);
243 char *msg = alloca (strlen (err) + 128);
244 sprintf (msg, "reading register %d: %s", regno, err);
252 /* Fetch all registers, or just one, from the child process. */
255 fetch_inferior_registers (regno)
258 if (regno == -1 || regno == 0)
259 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
260 fetch_register (regno);
262 fetch_register (regno);
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). */
270 store_inferior_registers (regno)
273 register unsigned int regaddr;
275 unsigned int offset = U_REGS_OFFSET;
280 if (CANNOT_STORE_REGISTER (regno))
283 regaddr = register_addr (regno, offset);
286 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
288 scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3;
289 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
293 /* Error, even if attached. Failing to write these two
294 registers is pretty serious. */
295 sprintf (buf, "writing register number %d", regno);
296 perror_with_name (buf);
301 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
304 ptrace (PTRACE_POKEUSR, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
305 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
308 /* Warning, not error, in case we are attached; sometimes the
309 kernel doesn't let us at the registers. */
310 char *err = strerror (errno);
311 char *msg = alloca (strlen (err) + 128);
312 sprintf (msg, "writing register %d: %s",
317 regaddr += sizeof(int);
321 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
322 store_inferior_registers (regno);
325 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
326 in the NEW_SUN_PTRACE case.
327 It ought to be straightforward. But it appears that writing did
328 not write the data that I specified. I cannot understand where
329 it got the data that it actually did write. */
331 /* Copy LEN bytes from inferior's memory starting at MEMADDR
332 to debugger memory starting at MYADDR. */
335 read_inferior_memory (memaddr, myaddr, len)
341 /* Round starting address down to longword boundary. */
342 register CORE_ADDR addr = memaddr & -sizeof (int);
343 /* Round ending address up; get number of longwords that makes. */
345 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
346 /* Allocate buffer of that many longwords. */
347 register int *buffer = (int *) alloca (count * sizeof (int));
349 /* Read all the longwords */
350 for (i = 0; i < count; i++, addr += sizeof (int))
352 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
355 /* Copy appropriate bytes out of the buffer. */
356 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
359 /* Copy LEN bytes of data from debugger memory at MYADDR
360 to inferior's memory at MEMADDR.
361 On failure (cannot write the inferior)
362 returns the value of errno. */
365 write_inferior_memory (memaddr, myaddr, len)
371 /* Round starting address down to longword boundary. */
372 register CORE_ADDR addr = memaddr & -sizeof (int);
373 /* Round ending address up; get number of longwords that makes. */
375 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
376 /* Allocate buffer of that many longwords. */
377 register int *buffer = (int *) alloca (count * sizeof (int));
380 /* Fill start and end extra bytes of buffer with existing memory data. */
382 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
387 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
388 addr + (count - 1) * sizeof (int), 0);
391 /* Copy data to be written over corresponding part of buffer */
393 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
395 /* Write the entire buffer. */
397 for (i = 0; i < count; i++, addr += sizeof (int))
400 ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]);
417 return inferior_pid != 0;