1 /* Machine-dependent hooks for the unix child process stratum. This
2 code is for the HP PA-RISC cpu.
4 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
6 Contributed by the Center for Software Science at the
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/ptrace.h>
31 #define PT_ATTACH PTRACE_ATTACH
35 #define PT_DETACH PTRACE_DETACH
38 /* This function simply calls ptrace with the given arguments.
39 It exists so that all calls to ptrace are isolated in this
40 machine-dependent file. */
43 call_ptrace (request, pid, addr, data)
45 PTRACE_ARG3_TYPE addr;
48 return ptrace (request, pid, addr, data, 0);
51 /* Use an extra level of indirection for ptrace calls.
52 This lets us breakpoint usefully on call_ptrace. It also
53 allows us to pass an extra argument to ptrace without
54 using an ANSI-C specific macro. */
56 #define ptrace call_ptrace
61 if (inferior_pid == 0)
63 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
65 target_mourn_inferior ();
70 /* Start debugging the process whose number is PID. */
76 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
78 perror_with_name ("ptrace");
83 /* Stop debugging the process whose number is PID
84 and continue it with signal number SIGNAL.
85 SIGNAL = 0 means just continue it. */
92 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
94 perror_with_name ("ptrace");
97 #endif /* ATTACH_DETACH */
101 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
102 to get the offset in the core file of the register values. */
103 #if defined (KERNEL_U_ADDR_BSD)
104 /* Get kernel_u_addr using BSD-style nlist(). */
105 CORE_ADDR kernel_u_addr;
107 #include <a.out.gnu.h> /* For struct nlist */
110 _initialize_kernel_u_addr ()
112 struct nlist names[2];
114 names[0].n_un.n_name = "_u";
115 names[1].n_un.n_name = NULL;
116 if (nlist ("/vmunix", names) == 0)
117 kernel_u_addr = names[0].n_value;
119 fatal ("Unable to get kernel u area address.");
121 #endif /* KERNEL_U_ADDR_BSD. */
123 #if defined (KERNEL_U_ADDR_HPUX)
124 /* Get kernel_u_addr using HPUX-style nlist(). */
125 CORE_ADDR kernel_u_addr;
130 unsigned char n_type;
131 unsigned char n_length;
135 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
137 /* read the value of the u area from the hp-ux kernel */
139 _initialize_kernel_u_addr ()
142 nlist ("/hp-ux", &nl);
143 kernel_u_addr = nl[0].n_value;
145 #endif /* KERNEL_U_ADDR_HPUX. */
147 #if !defined (offsetof)
148 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
151 /* U_REGS_OFFSET is the offset of the registers within the u area. */
152 #if !defined (U_REGS_OFFSET)
153 #define U_REGS_OFFSET \
154 ptrace (PT_READ_U, inferior_pid, \
155 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
159 /* Fetch one register. */
162 fetch_register (regno)
165 register unsigned int regaddr;
166 char buf[MAX_REGISTER_RAW_SIZE];
169 /* Offset of registers within the u area. */
172 offset = U_REGS_OFFSET;
174 regaddr = register_addr (regno, offset);
175 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
178 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
179 (PTRACE_ARG3_TYPE) regaddr, 0);
180 regaddr += sizeof (int);
183 /* Warning, not error, in case we are attached; sometimes the
184 kernel doesn't let us at the registers. */
185 char *err = safe_strerror (errno);
186 char *msg = alloca (strlen (err) + 128);
187 sprintf (msg, "reading register %s: %s", reg_names[regno], err);
192 supply_register (regno, buf);
196 /* Fetch all registers, or just one, from the child process. */
199 fetch_inferior_registers (regno)
203 for (regno = 0; regno < NUM_REGS; regno++)
204 fetch_register (regno);
206 fetch_register (regno);
209 /* Store our register values back into the inferior.
210 If REGNO is -1, do this for all registers.
211 Otherwise, REGNO specifies which register (so we can save time). */
214 store_inferior_registers (regno)
217 register unsigned int regaddr;
218 extern char registers[];
221 unsigned int offset = U_REGS_OFFSET;
225 regaddr = register_addr (regno, offset);
226 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
229 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
230 *(int *) ®isters[REGISTER_BYTE (regno) + i]);
233 char *err = safe_strerror (errno);
234 char *msg = alloca (strlen (err) + 128);
235 sprintf (msg, "writing register %s: %s", reg_names[regno], err);
238 regaddr += sizeof(int);
243 for (regno = 0; regno < NUM_REGS; regno++)
245 if (CANNOT_STORE_REGISTER (regno))
247 store_inferior_registers (regno);
253 /* Resume execution of process PID.
254 If STEP is nonzero, single-step it.
255 If SIGNAL is nonzero, give it that signal. */
258 child_resume (pid, step, signal)
268 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
269 it was. (If GDB wanted it to start some other way, we have already
270 written a new PC value to the child.) */
273 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1, signal);
275 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1, signal);
278 perror_with_name ("ptrace");
281 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
282 in the NEW_SUN_PTRACE case.
283 It ought to be straightforward. But it appears that writing did
284 not write the data that I specified. I cannot understand where
285 it got the data that it actually did write. */
287 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
288 to debugger memory starting at MYADDR. Copy to inferior if
291 Returns the length copied, which is either the LEN argument or zero.
292 This xfer function does not do partial moves, since child_ops
293 doesn't allow memory operations to cross below us in the target stack
297 child_xfer_memory (memaddr, myaddr, len, write, target)
302 struct target_ops *target; /* ignored */
305 /* Round starting address down to longword boundary. */
306 register CORE_ADDR addr = memaddr & - sizeof (int);
307 /* Round ending address up; get number of longwords that makes. */
309 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
310 /* Allocate buffer of that many longwords. */
311 register int *buffer = (int *) alloca (count * sizeof (int));
315 /* Fill start and end extra bytes of buffer with existing memory data. */
317 if (addr != memaddr || len < (int)sizeof (int)) {
318 /* Need part of initial word -- fetch it. */
319 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
323 if (count > 1) /* FIXME, avoid if even boundary */
326 = ptrace (PT_READ_I, inferior_pid,
327 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
331 /* Copy data to be written over corresponding part of buffer */
333 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
335 /* Write the entire buffer. */
337 for (i = 0; i < count; i++, addr += sizeof (int))
340 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
344 /* Using the appropriate one (I or D) is necessary for
345 Gould NP1, at least. */
347 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
356 /* Read all the longwords */
357 for (i = 0; i < count; i++, addr += sizeof (int))
360 buffer[i] = ptrace (PT_READ_I, inferior_pid,
361 (PTRACE_ARG3_TYPE) addr, 0);
367 /* Copy appropriate bytes out of the buffer. */
368 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);