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 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
53 extern char **environ;
55 extern int inferior_pid;
56 void quit (), perror_with_name ();
59 /* Start an inferior process and returns its pid.
60 ALLARGS is a vector of program-name and args.
61 ENV is the environment vector to pass. */
64 create_inferior (program, allargs)
72 perror_with_name ("fork");
76 ptrace (PTRACE_TRACEME, 0, 0, 0);
78 execv (program, allargs);
80 fprintf (stderr, "Cannot exec %s: %s.\n", program,
81 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
89 /* Kill the inferior process. Make us have no inferior. */
94 if (inferior_pid == 0)
96 ptrace (PTRACE_KILL, inferior_pid, 0, 0);
98 /*************inferior_died ();****VK**************/
101 /* Return nonzero if the given thread is still alive. */
109 /* Wait for process, returns status */
119 if (pid != inferior_pid)
120 perror_with_name ("wait");
124 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
126 return ((unsigned char) WEXITSTATUS (w));
128 else if (!WIFSTOPPED (w))
130 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
132 return ((unsigned char) WTERMSIG (w));
135 fetch_inferior_registers (0);
138 return ((unsigned char) WSTOPSIG (w));
141 /* Resume execution of the inferior process.
142 If STEP is nonzero, single-step it.
143 If SIGNAL is nonzero, give it that signal. */
146 myresume (step, signal)
151 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal);
153 perror_with_name ("ptrace");
157 #if !defined (offsetof)
158 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
161 /* U_REGS_OFFSET is the offset of the registers within the u area. */
162 #if !defined (U_REGS_OFFSET)
163 #define U_REGS_OFFSET \
164 ptrace (PT_READ_U, inferior_pid, \
165 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
170 /* this table must line up with REGISTER_NAMES in tm-i386v.h */
171 /* symbols like 'EAX' come from <sys/reg.h> */
172 static int regmap[] =
181 i386_register_u_addr (blockend, regnum)
186 /* this will be needed if fp registers are reinstated */
187 /* for now, you can look at them with 'info float'
188 * sys5 wont let you change them with ptrace anyway
190 if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM)
194 ubase = blockend + 4 * (SS + 1) - KSTKSZ;
195 fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
196 return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
200 return (blockend + 4 * regmap[regnum]);
203 #else /* TARGET_M68K */
204 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
205 static int regmap[] =
208 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
209 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
212 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15,
216 PT_FP0, PT_FP1, PT_FP2, PT_FP3, PT_FP4, PT_FP5, PT_FP6, PT_FP7,
217 PT_FPCR, PT_FPSR, PT_FPIAR
219 21, 24, 27, 30, 33, 36, 39, 42, 45, 46, 47
223 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
227 m68k_linux_register_u_addr (blockend, regnum)
231 return (blockend + 4 * regmap[regnum]);
236 register_addr (regno, blockend)
242 if (regno < 0 || regno >= ARCH_NUM_REGS)
243 error ("Invalid register number %d.", regno);
245 REGISTER_U_ADDR (addr, blockend, regno);
250 /* Fetch one register. */
253 fetch_register (regno)
256 register unsigned int regaddr;
259 /* Offset of registers within the u area. */
262 offset = U_REGS_OFFSET;
264 regaddr = register_addr (regno, offset);
265 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
268 *(int *) ®isters[ regno * 4 + i] = ptrace (PTRACE_PEEKUSR, inferior_pid,
269 (PTRACE_ARG3_TYPE) regaddr, 0);
270 regaddr += sizeof (int);
273 /* Warning, not error, in case we are attached; sometimes the
274 kernel doesn't let us at the registers. */
275 char *err = strerror (errno);
276 char *msg = alloca (strlen (err) + 128);
277 sprintf (msg, "reading register %d: %s", regno, err);
285 /* Fetch all registers, or just one, from the child process. */
288 fetch_inferior_registers (regno)
291 if (regno == -1 || regno == 0)
292 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
293 fetch_register (regno);
295 fetch_register (regno);
298 /* Store our register values back into the inferior.
299 If REGNO is -1, do this for all registers.
300 Otherwise, REGNO specifies which register (so we can save time). */
303 store_inferior_registers (regno)
306 register unsigned int regaddr;
308 unsigned int offset = U_REGS_OFFSET;
313 if (CANNOT_STORE_REGISTER (regno))
316 regaddr = register_addr (regno, offset);
319 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
321 scratch = *(int *) ®isters[REGISTER_BYTE (regno)] | 0x3;
322 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
326 /* Error, even if attached. Failing to write these two
327 registers is pretty serious. */
328 sprintf (buf, "writing register number %d", regno);
329 perror_with_name (buf);
334 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
337 ptrace (PTRACE_POKEUSR, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
338 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
341 /* Warning, not error, in case we are attached; sometimes the
342 kernel doesn't let us at the registers. */
343 char *err = strerror (errno);
344 char *msg = alloca (strlen (err) + 128);
345 sprintf (msg, "writing register %d: %s",
350 regaddr += sizeof(int);
354 for (regno = 0; regno < NUM_REGS-NUM_FREGS; regno++)
355 store_inferior_registers (regno);
358 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
359 in the NEW_SUN_PTRACE case.
360 It ought to be straightforward. But it appears that writing did
361 not write the data that I specified. I cannot understand where
362 it got the data that it actually did write. */
364 /* Copy LEN bytes from inferior's memory starting at MEMADDR
365 to debugger memory starting at MYADDR. */
368 read_inferior_memory (memaddr, myaddr, len)
374 /* Round starting address down to longword boundary. */
375 register CORE_ADDR addr = memaddr & -sizeof (int);
376 /* Round ending address up; get number of longwords that makes. */
378 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
379 /* Allocate buffer of that many longwords. */
380 register int *buffer = (int *) alloca (count * sizeof (int));
382 /* Read all the longwords */
383 for (i = 0; i < count; i++, addr += sizeof (int))
385 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
388 /* Copy appropriate bytes out of the buffer. */
389 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
392 /* Copy LEN bytes of data from debugger memory at MYADDR
393 to inferior's memory at MEMADDR.
394 On failure (cannot write the inferior)
395 returns the value of errno. */
398 write_inferior_memory (memaddr, myaddr, len)
404 /* Round starting address down to longword boundary. */
405 register CORE_ADDR addr = memaddr & -sizeof (int);
406 /* Round ending address up; get number of longwords that makes. */
408 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
409 /* Allocate buffer of that many longwords. */
410 register int *buffer = (int *) alloca (count * sizeof (int));
413 /* Fill start and end extra bytes of buffer with existing memory data. */
415 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, addr, 0);
420 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
421 addr + (count - 1) * sizeof (int), 0);
424 /* Copy data to be written over corresponding part of buffer */
426 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
428 /* Write the entire buffer. */
430 for (i = 0; i < count; i++, addr += sizeof (int))
433 ptrace (PTRACE_POKETEXT, inferior_pid, addr, buffer[i]);
450 return inferior_pid != 0;