1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992 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. */
26 #include <sys/types.h>
29 #include <sys/param.h>
32 #include <sys/ioctl.h>
34 #include <sys/ptrace.h>
37 #if !defined (PT_KILL)
47 #endif /* No PT_KILL. */
50 #define PT_ATTACH PTRACE_ATTACH
53 #define PT_DETACH PTRACE_DETACH
62 #if !defined (FETCH_INFERIOR_REGISTERS)
63 #include <sys/user.h> /* Probably need to poke the user structure */
64 #if defined (KERNEL_U_ADDR_BSD)
65 #include <a.out.h> /* For struct nlist */
66 #endif /* KERNEL_U_ADDR_BSD. */
67 #endif /* !FETCH_INFERIOR_REGISTERS */
70 /* This function simply calls ptrace with the given arguments.
71 It exists so that all calls to ptrace are isolated in this
72 machine-dependent file. */
74 call_ptrace (request, pid, addr, data)
76 PTRACE_ARG3_TYPE addr;
79 return ptrace (request, pid, addr, data);
83 /* For the rest of the file, use an extra level of indirection */
84 /* This lets us breakpoint usefully on call_ptrace. */
85 #define ptrace call_ptrace
88 /* This is used when GDB is exiting. It gives less chance of error.*/
93 if (inferior_pid == 0)
95 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
102 kill_inferior_fast ();
103 target_mourn_inferior ();
106 /* Resume execution of the inferior process.
107 If STEP is nonzero, single-step it.
108 If SIGNAL is nonzero, give it that signal. */
111 child_resume (step, signal)
117 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
118 it was. (If GDB wanted it to start some other way, we have already
119 written a new PC value to the child.) */
122 #ifdef NO_SINGLE_STEP
123 single_step (signal);
125 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
128 #ifdef AIX_BUGGY_PTRACE_CONTINUE
129 AIX_BUGGY_PTRACE_CONTINUE;
131 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
135 perror_with_name ("ptrace");
139 /* Nonzero if we are debugging an attached process rather than
141 extern int attach_flag;
143 /* Start debugging the process whose number is PID. */
149 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
151 perror_with_name ("ptrace");
156 /* Stop debugging the process whose number is PID
157 and continue it with signal number SIGNAL.
158 SIGNAL = 0 means just continue it. */
165 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
167 perror_with_name ("ptrace");
170 #endif /* ATTACH_DETACH */
172 #if !defined (FETCH_INFERIOR_REGISTERS)
174 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
175 to get the offset in the core file of the register values. */
176 #if defined (KERNEL_U_ADDR_BSD)
177 /* Get kernel_u_addr using BSD-style nlist(). */
178 CORE_ADDR kernel_u_addr;
181 _initialize_kernel_u_addr ()
183 struct nlist names[2];
185 names[0].n_un.n_name = "_u";
186 names[1].n_un.n_name = NULL;
187 if (nlist ("/vmunix", names) == 0)
188 kernel_u_addr = names[0].n_value;
190 fatal ("Unable to get kernel u area address.");
192 #endif /* KERNEL_U_ADDR_BSD. */
194 #if defined (KERNEL_U_ADDR_HPUX)
195 /* Get kernel_u_addr using HPUX-style nlist(). */
196 CORE_ADDR kernel_u_addr;
201 unsigned char n_type;
202 unsigned char n_length;
206 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
208 /* read the value of the u area from the hp-ux kernel */
209 void _initialize_kernel_u_addr ()
211 nlist ("/hp-ux", &nl);
212 kernel_u_addr = nl[0].n_value;
214 #endif /* KERNEL_U_ADDR_HPUX. */
216 #if !defined (offsetof)
217 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
220 /* U_REGS_OFFSET is the offset of the registers within the u area. */
221 #if !defined (U_REGS_OFFSET)
222 #define U_REGS_OFFSET \
223 ptrace (PT_READ_U, inferior_pid, \
224 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
228 /* Registers we shouldn't try to fetch. */
229 #if !defined (CANNOT_FETCH_REGISTER)
230 #define CANNOT_FETCH_REGISTER(regno) 0
233 /* Fetch one register. */
236 fetch_register (regno)
239 register unsigned int regaddr;
240 char buf[MAX_REGISTER_RAW_SIZE];
241 char mess[128]; /* For messages */
244 /* Offset of registers within the u area. */
247 if (CANNOT_FETCH_REGISTER (regno))
249 bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
250 supply_register (regno, buf);
254 offset = U_REGS_OFFSET;
256 regaddr = register_addr (regno, offset);
257 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
260 *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
261 (PTRACE_ARG3_TYPE) regaddr, 0);
262 regaddr += sizeof (int);
265 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
266 perror_with_name (mess);
269 supply_register (regno, buf);
273 /* Fetch all registers, or just one, from the child process. */
276 fetch_inferior_registers (regno)
280 for (regno = 0; regno < NUM_REGS; regno++)
281 fetch_register (regno);
283 fetch_register (regno);
286 /* Registers we shouldn't try to store. */
287 #if !defined (CANNOT_STORE_REGISTER)
288 #define CANNOT_STORE_REGISTER(regno) 0
291 /* Store our register values back into the inferior.
292 If REGNO is -1, do this for all registers.
293 Otherwise, REGNO specifies which register (so we can save time). */
296 store_inferior_registers (regno)
299 register unsigned int regaddr;
301 extern char registers[];
304 unsigned int offset = U_REGS_OFFSET;
308 regaddr = register_addr (regno, offset);
309 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
312 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
313 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
316 sprintf (buf, "writing register number %d(%d)", regno, i);
317 perror_with_name (buf);
319 regaddr += sizeof(int);
324 for (regno = 0; regno < NUM_REGS; regno++)
326 if (CANNOT_STORE_REGISTER (regno))
328 regaddr = register_addr (regno, offset);
329 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
332 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
333 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
336 sprintf (buf, "writing register number %d(%d)", regno, i);
337 perror_with_name (buf);
339 regaddr += sizeof(int);
344 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
346 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
347 in the NEW_SUN_PTRACE case.
348 It ought to be straightforward. But it appears that writing did
349 not write the data that I specified. I cannot understand where
350 it got the data that it actually did write. */
352 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
353 to debugger memory starting at MYADDR. Copy to inferior if
356 Returns the length copied, which is either the LEN argument or zero.
357 This xfer function does not do partial moves, since child_ops
358 doesn't allow memory operations to cross below us in the target stack
362 child_xfer_memory (memaddr, myaddr, len, write, target)
367 struct target_ops *target; /* ignored */
370 /* Round starting address down to longword boundary. */
371 register CORE_ADDR addr = memaddr & - sizeof (int);
372 /* Round ending address up; get number of longwords that makes. */
374 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
375 /* Allocate buffer of that many longwords. */
376 register int *buffer = (int *) alloca (count * sizeof (int));
380 /* Fill start and end extra bytes of buffer with existing memory data. */
382 if (addr != memaddr || len < (int)sizeof (int)) {
383 /* Need part of initial word -- fetch it. */
384 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
388 if (count > 1) /* FIXME, avoid if even boundary */
391 = ptrace (PT_READ_I, inferior_pid,
392 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
396 /* Copy data to be written over corresponding part of buffer */
398 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
400 /* Write the entire buffer. */
402 for (i = 0; i < count; i++, addr += sizeof (int))
405 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
409 /* Using the appropriate one (I or D) is necessary for
410 Gould NP1, at least. */
412 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
421 /* Read all the longwords */
422 for (i = 0; i < count; i++, addr += sizeof (int))
425 buffer[i] = ptrace (PT_READ_I, inferior_pid,
426 (PTRACE_ARG3_TYPE) addr, 0);
432 /* Copy appropriate bytes out of the buffer. */
433 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);