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>
35 #ifdef PTRACE_IN_WRONG_PLACE
38 #include <sys/ptrace.h>
40 #endif /* NO_PTRACE_H */
42 #if !defined (PT_KILL)
52 #endif /* No PT_KILL. */
55 #define PT_ATTACH PTRACE_ATTACH
58 #define PT_DETACH PTRACE_DETACH
67 #if defined (FIVE_ARG_PTRACE
68 /* Deal with HPUX 8.0 braindamage. */
69 #define ptrace(a,b,c,d) ptrace(a,b,c,d,0)
72 #if !defined (FETCH_INFERIOR_REGISTERS)
73 #include <sys/user.h> /* Probably need to poke the user structure */
74 #if defined (KERNEL_U_ADDR_BSD)
75 #include <a.out.h> /* For struct nlist */
76 #endif /* KERNEL_U_ADDR_BSD. */
77 #endif /* !FETCH_INFERIOR_REGISTERS */
80 /* This function simply calls ptrace with the given arguments.
81 It exists so that all calls to ptrace are isolated in this
82 machine-dependent file. */
84 call_ptrace (request, pid, addr, data)
86 PTRACE_ARG3_TYPE addr;
89 return ptrace (request, pid, addr, data);
93 /* For the rest of the file, use an extra level of indirection */
94 /* This lets us breakpoint usefully on call_ptrace. */
95 #define ptrace call_ptrace
101 if (inferior_pid == 0)
103 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
104 a real signal first, if we can. */
105 kill (inferior_pid, SIGKILL);
106 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
108 target_mourn_inferior ();
111 /* Resume execution of the inferior process.
112 If STEP is nonzero, single-step it.
113 If SIGNAL is nonzero, give it that signal. */
116 child_resume (step, signal)
122 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
123 it was. (If GDB wanted it to start some other way, we have already
124 written a new PC value to the child.)
126 If this system does not support PT_STEP, a higher level function will
127 have called single_step() to transmute the step request into a
128 continue request (by setting breakpoints on all possible successor
129 instructions), so we don't have to worry about that here. */
132 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
134 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
137 perror_with_name ("ptrace");
141 /* Start debugging the process whose number is PID. */
147 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
149 perror_with_name ("ptrace");
154 /* Stop debugging the process whose number is PID
155 and continue it with signal number SIGNAL.
156 SIGNAL = 0 means just continue it. */
163 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
165 perror_with_name ("ptrace");
168 #endif /* ATTACH_DETACH */
170 #if !defined (FETCH_INFERIOR_REGISTERS)
172 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
173 to get the offset in the core file of the register values. */
174 #if defined (KERNEL_U_ADDR_BSD)
175 /* Get kernel_u_addr using BSD-style nlist(). */
176 CORE_ADDR kernel_u_addr;
179 _initialize_kernel_u_addr ()
181 struct nlist names[2];
183 names[0].n_un.n_name = "_u";
184 names[1].n_un.n_name = NULL;
185 if (nlist ("/vmunix", names) == 0)
186 kernel_u_addr = names[0].n_value;
188 fatal ("Unable to get kernel u area address.");
190 #endif /* KERNEL_U_ADDR_BSD. */
192 #if defined (KERNEL_U_ADDR_HPUX)
193 /* Get kernel_u_addr using HPUX-style nlist(). */
194 CORE_ADDR kernel_u_addr;
199 unsigned char n_type;
200 unsigned char n_length;
204 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
206 /* read the value of the u area from the hp-ux kernel */
207 void _initialize_kernel_u_addr ()
209 nlist ("/hp-ux", &nl);
210 kernel_u_addr = nl[0].n_value;
212 #endif /* KERNEL_U_ADDR_HPUX. */
214 #if !defined (offsetof)
215 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
218 /* U_REGS_OFFSET is the offset of the registers within the u area. */
219 #if !defined (U_REGS_OFFSET)
220 #define U_REGS_OFFSET \
221 ptrace (PT_READ_U, inferior_pid, \
222 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
226 /* Registers we shouldn't try to fetch. */
227 #if !defined (CANNOT_FETCH_REGISTER)
228 #define CANNOT_FETCH_REGISTER(regno) 0
231 /* Fetch one register. */
234 fetch_register (regno)
237 register unsigned int regaddr;
238 char buf[MAX_REGISTER_RAW_SIZE];
239 char mess[128]; /* For messages */
242 /* Offset of registers within the u area. */
245 if (CANNOT_FETCH_REGISTER (regno))
247 bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
248 supply_register (regno, buf);
252 offset = U_REGS_OFFSET;
254 regaddr = register_addr (regno, offset);
255 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
258 *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
259 (PTRACE_ARG3_TYPE) regaddr, 0);
260 regaddr += sizeof (int);
263 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
264 perror_with_name (mess);
267 supply_register (regno, buf);
271 /* Fetch all registers, or just one, from the child process. */
274 fetch_inferior_registers (regno)
278 for (regno = 0; regno < NUM_REGS; regno++)
279 fetch_register (regno);
281 fetch_register (regno);
284 /* Registers we shouldn't try to store. */
285 #if !defined (CANNOT_STORE_REGISTER)
286 #define CANNOT_STORE_REGISTER(regno) 0
289 /* Store our register values back into the inferior.
290 If REGNO is -1, do this for all registers.
291 Otherwise, REGNO specifies which register (so we can save time). */
294 store_inferior_registers (regno)
297 register unsigned int regaddr;
299 extern char registers[];
302 unsigned int offset = U_REGS_OFFSET;
306 regaddr = register_addr (regno, offset);
307 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
310 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
311 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
314 sprintf (buf, "writing register number %d(%d)", regno, i);
315 perror_with_name (buf);
317 regaddr += sizeof(int);
322 for (regno = 0; regno < NUM_REGS; regno++)
324 if (CANNOT_STORE_REGISTER (regno))
326 regaddr = register_addr (regno, offset);
327 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
330 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
331 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
334 sprintf (buf, "writing register number %d(%d)", regno, i);
335 perror_with_name (buf);
337 regaddr += sizeof(int);
342 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
344 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
345 in the NEW_SUN_PTRACE case.
346 It ought to be straightforward. But it appears that writing did
347 not write the data that I specified. I cannot understand where
348 it got the data that it actually did write. */
350 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
351 to debugger memory starting at MYADDR. Copy to inferior if
354 Returns the length copied, which is either the LEN argument or zero.
355 This xfer function does not do partial moves, since child_ops
356 doesn't allow memory operations to cross below us in the target stack
360 child_xfer_memory (memaddr, myaddr, len, write, target)
365 struct target_ops *target; /* ignored */
368 /* Round starting address down to longword boundary. */
369 register CORE_ADDR addr = memaddr & - sizeof (int);
370 /* Round ending address up; get number of longwords that makes. */
372 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
373 /* Allocate buffer of that many longwords. */
374 register int *buffer = (int *) alloca (count * sizeof (int));
378 /* Fill start and end extra bytes of buffer with existing memory data. */
380 if (addr != memaddr || len < (int)sizeof (int)) {
381 /* Need part of initial word -- fetch it. */
382 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
386 if (count > 1) /* FIXME, avoid if even boundary */
389 = ptrace (PT_READ_I, inferior_pid,
390 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
394 /* Copy data to be written over corresponding part of buffer */
396 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
398 /* Write the entire buffer. */
400 for (i = 0; i < count; i++, addr += sizeof (int))
403 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
407 /* Using the appropriate one (I or D) is necessary for
408 Gould NP1, at least. */
410 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
419 /* Read all the longwords */
420 for (i = 0; i < count; i++, addr += sizeof (int))
423 buffer[i] = ptrace (PT_READ_I, inferior_pid,
424 (PTRACE_ARG3_TYPE) addr, 0);
430 /* Copy appropriate bytes out of the buffer. */
431 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);