1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "gdb_string.h"
28 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <sys/ioctl.h>
37 #ifdef PTRACE_IN_WRONG_PLACE
40 #include <sys/ptrace.h>
42 #endif /* NO_PTRACE_H */
44 #if !defined (PT_KILL)
48 #if !defined (PT_STEP)
57 #endif /* No PT_STEP. */
60 #define PT_ATTACH PTRACE_ATTACH
63 #define PT_DETACH PTRACE_DETACH
71 /* Don't think this is used anymore. On the sequent (not sure whether it's
72 dynix or ptx or both), it is included unconditionally by sys/user.h and
73 not protected against multiple inclusion. */
77 #if !defined (FETCH_INFERIOR_REGISTERS)
78 #include <sys/user.h> /* Probably need to poke the user structure */
79 #if defined (KERNEL_U_ADDR_BSD)
80 #include <a.out.h> /* For struct nlist */
81 #endif /* KERNEL_U_ADDR_BSD. */
82 #endif /* !FETCH_INFERIOR_REGISTERS */
85 /* This function simply calls ptrace with the given arguments.
86 It exists so that all calls to ptrace are isolated in this
87 machine-dependent file. */
89 call_ptrace (request, pid, addr, data)
91 PTRACE_ARG3_TYPE addr;
94 return ptrace (request, pid, addr, data
95 #if defined (FIVE_ARG_PTRACE)
96 /* Deal with HPUX 8.0 braindamage. We never use the
97 calls which require the fifth argument. */
103 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
104 /* For the rest of the file, use an extra level of indirection */
105 /* This lets us breakpoint usefully on call_ptrace. */
106 #define ptrace call_ptrace
112 if (inferior_pid == 0)
114 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
115 a real signal first, if we can. FIXME: This is bogus. When the inferior
116 is not stopped, GDB should just be waiting for it. Either the following
117 line is unecessary, or there is some problem elsewhere in GDB which
118 causes us to get here when the inferior is not stopped. */
119 kill (inferior_pid, SIGKILL);
120 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
122 target_mourn_inferior ();
127 /* Resume execution of the inferior process.
128 If STEP is nonzero, single-step it.
129 If SIGNAL is nonzero, give it that signal. */
132 child_resume (pid, step, signal)
135 enum target_signal signal;
140 /* Resume all threads. */
141 /* I think this only gets used in the non-threaded case, where "resume
142 all threads" and "resume inferior_pid" are the same. */
145 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
146 it was. (If GDB wanted it to start some other way, we have already
147 written a new PC value to the child.)
149 If this system does not support PT_STEP, a higher level function will
150 have called single_step() to transmute the step request into a
151 continue request (by setting breakpoints on all possible successor
152 instructions), so we don't have to worry about that here. */
155 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
156 target_signal_to_host (signal));
158 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
159 target_signal_to_host (signal));
162 perror_with_name ("ptrace");
164 #endif /* CHILD_RESUME */
168 /* Start debugging the process whose number is PID. */
174 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
176 perror_with_name ("ptrace");
181 /* Stop debugging the process whose number is PID
182 and continue it with signal number SIGNAL.
183 SIGNAL = 0 means just continue it. */
190 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
192 perror_with_name ("ptrace");
195 #endif /* ATTACH_DETACH */
197 /* Default the type of the ptrace transfer to int. */
198 #ifndef PTRACE_XFER_TYPE
199 #define PTRACE_XFER_TYPE int
202 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
203 to get the offset in the core file of the register values. */
204 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
205 /* Get kernel_u_addr using BSD-style nlist(). */
206 CORE_ADDR kernel_u_addr;
207 #endif /* KERNEL_U_ADDR_BSD. */
210 _initialize_kernel_u_addr ()
212 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
213 struct nlist names[2];
215 names[0].n_un.n_name = "_u";
216 names[1].n_un.n_name = NULL;
217 if (nlist ("/vmunix", names) == 0)
218 kernel_u_addr = names[0].n_value;
220 fatal ("Unable to get kernel u area address.");
221 #endif /* KERNEL_U_ADDR_BSD. */
224 #if !defined (FETCH_INFERIOR_REGISTERS)
226 #if !defined (offsetof)
227 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
230 /* U_REGS_OFFSET is the offset of the registers within the u area. */
231 #if !defined (U_REGS_OFFSET)
232 #define U_REGS_OFFSET \
233 ptrace (PT_READ_U, inferior_pid, \
234 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
238 /* Registers we shouldn't try to fetch. */
239 #if !defined (CANNOT_FETCH_REGISTER)
240 #define CANNOT_FETCH_REGISTER(regno) 0
243 /* Fetch one register. */
246 fetch_register (regno)
249 /* This isn't really an address. But ptrace thinks of it as one. */
251 char buf[MAX_REGISTER_RAW_SIZE];
252 char mess[128]; /* For messages */
255 /* Offset of registers within the u area. */
258 if (CANNOT_FETCH_REGISTER (regno))
260 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
261 supply_register (regno, buf);
265 offset = U_REGS_OFFSET;
267 regaddr = register_addr (regno, offset);
268 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
271 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
272 (PTRACE_ARG3_TYPE) regaddr, 0);
273 regaddr += sizeof (PTRACE_XFER_TYPE);
276 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
277 perror_with_name (mess);
280 supply_register (regno, buf);
284 /* Fetch all registers, or just one, from the child process. */
287 fetch_inferior_registers (regno)
294 numregs = ARCH_NUM_REGS;
295 for (regno = 0; regno < numregs; regno++)
296 fetch_register (regno);
299 fetch_register (regno);
302 /* Registers we shouldn't try to store. */
303 #if !defined (CANNOT_STORE_REGISTER)
304 #define CANNOT_STORE_REGISTER(regno) 0
307 /* Store our register values back into the inferior.
308 If REGNO is -1, do this for all registers.
309 Otherwise, REGNO specifies which register (so we can save time). */
312 store_inferior_registers (regno)
315 /* This isn't really an address. But ptrace thinks of it as one. */
318 register int i, numregs;
320 unsigned int offset = U_REGS_OFFSET;
324 regaddr = register_addr (regno, offset);
325 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
328 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
329 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
332 sprintf (buf, "writing register number %d(%d)", regno, i);
333 perror_with_name (buf);
335 regaddr += sizeof(PTRACE_XFER_TYPE);
340 numregs = ARCH_NUM_REGS;
341 for (regno = 0; regno < numregs; regno++)
343 if (CANNOT_STORE_REGISTER (regno))
345 regaddr = register_addr (regno, offset);
346 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
349 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
350 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
353 sprintf (buf, "writing register number %d(%d)", regno, i);
354 perror_with_name (buf);
356 regaddr += sizeof(PTRACE_XFER_TYPE);
361 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
364 #if !defined (CHILD_XFER_MEMORY)
365 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
366 in the NEW_SUN_PTRACE case.
367 It ought to be straightforward. But it appears that writing did
368 not write the data that I specified. I cannot understand where
369 it got the data that it actually did write. */
371 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
372 to debugger memory starting at MYADDR. Copy to inferior if
375 Returns the length copied, which is either the LEN argument or zero.
376 This xfer function does not do partial moves, since child_ops
377 doesn't allow memory operations to cross below us in the target stack
381 child_xfer_memory (memaddr, myaddr, len, write, target)
386 struct target_ops *target; /* ignored */
389 /* Round starting address down to longword boundary. */
390 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
391 /* Round ending address up; get number of longwords that makes. */
393 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
394 / sizeof (PTRACE_XFER_TYPE);
395 /* Allocate buffer of that many longwords. */
396 register PTRACE_XFER_TYPE *buffer
397 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
401 /* Fill start and end extra bytes of buffer with existing memory data. */
403 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
404 /* Need part of initial word -- fetch it. */
405 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
409 if (count > 1) /* FIXME, avoid if even boundary */
412 = ptrace (PT_READ_I, inferior_pid,
414 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
418 /* Copy data to be written over corresponding part of buffer */
420 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
424 /* Write the entire buffer. */
426 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
429 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
433 /* Using the appropriate one (I or D) is necessary for
434 Gould NP1, at least. */
436 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
445 /* Read all the longwords */
446 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
449 buffer[i] = ptrace (PT_READ_I, inferior_pid,
450 (PTRACE_ARG3_TYPE) addr, 0);
456 /* Copy appropriate bytes out of the buffer. */
458 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
463 #endif /* !defined (CHILD_XFER_MEMORY). */