1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988, 1989, 1990, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
36 #include <sys/ptrace.h>
39 #if !defined (PT_KILL)
49 #endif /* No PT_KILL. */
52 #define PT_ATTACH PTRACE_ATTACH
55 #define PT_DETACH PTRACE_DETACH
59 #include <sys/user.h> /* After a.out.h */
63 /* This function simply calls ptrace with the given arguments.
64 It exists so that all calls to ptrace are isolated in this
65 machine-dependent file. */
67 call_ptrace (request, pid, addr, data)
68 int request, pid, *addr, data;
70 return ptrace (request, pid, addr, data);
74 /* For the rest of the file, use an extra level of indirection */
75 /* This lets us breakpoint usefully on call_ptrace. */
76 #define ptrace call_ptrace
79 /* This is used when GDB is exiting. It gives less chance of error.*/
84 if (inferior_pid == 0)
86 ptrace (PT_KILL, inferior_pid, 0, 0);
91 kill_inferior (args, from_tty)
95 kill_inferior_fast ();
96 target_mourn_inferior ();
99 /* Resume execution of the inferior process.
100 If STEP is nonzero, single-step it.
101 If SIGNAL is nonzero, give it that signal. */
104 child_resume (step, signal)
110 /* An address of (int *)1 tells ptrace to continue from where it was.
111 (If GDB wanted it to start some other way, we have already written
112 a new PC value to the child.) */
115 ptrace (PT_STEP, inferior_pid, (int *)1, signal);
117 ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
120 perror_with_name ("ptrace");
124 /* Nonzero if we are debugging an attached process rather than
126 extern int attach_flag;
128 /* Start debugging the process whose number is PID. */
134 ptrace (PT_ATTACH, pid, 0, 0);
136 perror_with_name ("ptrace");
141 /* Stop debugging the process whose number is PID
142 and continue it with signal number SIGNAL.
143 SIGNAL = 0 means just continue it. */
150 ptrace (PT_DETACH, inferior_pid, 1, signal);
152 perror_with_name ("ptrace");
155 #endif /* ATTACH_DETACH */
157 #if !defined (FETCH_INFERIOR_REGISTERS)
159 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
160 to get the offset in the core file of the register values. */
161 #if defined (KERNEL_U_ADDR_BSD)
162 /* Get kernel_u_addr using BSD-style nlist(). */
163 CORE_ADDR kernel_u_addr;
166 _initialize_kernel_u_addr ()
168 struct nlist names[2];
170 names[0].n_un.n_name = "_u";
171 names[1].n_un.n_name = NULL;
172 if (nlist ("/vmunix", names) == 0)
173 kernel_u_addr = names[0].n_value;
175 fatal ("Unable to get kernel u area address.");
177 #endif /* KERNEL_U_ADDR_BSD. */
179 #if defined (KERNEL_U_ADDR_HPUX)
180 /* Get kernel_u_addr using HPUX-style nlist(). */
181 CORE_ADDR kernel_u_addr;
186 unsigned char n_type;
187 unsigned char n_length;
191 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
193 /* read the value of the u area from the hp-ux kernel */
194 void _initialize_kernel_u_addr ()
197 nlist ("/hp-ux", &nl);
198 kernel_u_addr = nl[0].n_value;
200 #endif /* KERNEL_U_ADDR_HPUX. */
202 #if !defined (offsetof)
203 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
206 /* U_REGS_OFFSET is the offset of the registers within the u area. */
207 #if !defined (U_REGS_OFFSET)
208 #define U_REGS_OFFSET \
209 ptrace (PT_READ_U, inferior_pid, \
210 (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
213 /* Fetch one register. */
215 fetch_register (regno)
218 register unsigned int regaddr;
219 char buf[MAX_REGISTER_RAW_SIZE];
222 /* Offset of registers within the u area. */
223 unsigned int offset = U_REGS_OFFSET;
225 regaddr = register_addr (regno, offset);
226 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
228 *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0);
229 regaddr += sizeof (int);
231 supply_register (regno, buf);
234 /* Fetch all registers, or just one, from the child process. */
237 fetch_inferior_registers (regno)
241 for (regno = 0; regno < NUM_REGS; regno++)
242 fetch_register (regno);
244 fetch_register (regno);
247 /* Registers we shouldn't try to store. */
248 #if !defined (CANNOT_STORE_REGISTER)
249 #define CANNOT_STORE_REGISTER(regno) 0
252 /* Store our register values back into the inferior.
253 If REGNO is -1, do this for all registers.
254 Otherwise, REGNO specifies which register (so we can save time). */
257 store_inferior_registers (regno)
260 register unsigned int regaddr;
262 extern char registers[];
266 unsigned int offset = U_REGS_OFFSET;
270 regaddr = register_addr (regno, offset);
271 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
274 ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
275 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
278 sprintf (buf, "writing register number %d(%d)", regno, i);
279 perror_with_name (buf);
282 regaddr += sizeof(int);
287 for (regno = 0; regno < NUM_REGS; regno++)
289 if (CANNOT_STORE_REGISTER (regno))
291 regaddr = register_addr (regno, offset);
292 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
295 ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
296 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
299 sprintf (buf, "writing register number %d(%d)", regno, i);
300 perror_with_name (buf);
303 regaddr += sizeof(int);
309 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
311 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
312 in the NEW_SUN_PTRACE case.
313 It ought to be straightforward. But it appears that writing did
314 not write the data that I specified. I cannot understand where
315 it got the data that it actually did write. */
317 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
318 to debugger memory starting at MYADDR. Copy to inferior if
321 Returns the length copied, which is either the LEN argument or zero.
322 This xfer function does not do partial moves, since child_ops
323 doesn't allow memory operations to cross below us in the target stack
327 child_xfer_memory (memaddr, myaddr, len, write, target)
332 struct target_ops target; /* ignored */
335 /* Round starting address down to longword boundary. */
336 register CORE_ADDR addr = memaddr & - sizeof (int);
337 /* Round ending address up; get number of longwords that makes. */
339 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
340 /* Allocate buffer of that many longwords. */
341 register int *buffer = (int *) alloca (count * sizeof (int));
345 /* Fill start and end extra bytes of buffer with existing memory data. */
347 if (addr != memaddr || len < (int)sizeof (int)) {
348 /* Need part of initial word -- fetch it. */
349 buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
352 if (count > 1) /* FIXME, avoid if even boundary */
355 = ptrace (PT_READ_I, inferior_pid,
356 (int *)(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 (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
371 /* Using the appropriate one (I or D) is necessary for
372 Gould NP1, at least. */
374 ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
382 /* Read all the longwords */
383 for (i = 0; i < count; i++, addr += sizeof (int))
386 buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
392 /* Copy appropriate bytes out of the buffer. */
393 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);