* ser-unix.c [USE_{TERMIO,ALARM}_TIMEOUT]: New code to deal with
[deliverable/binutils-gdb.git] / gdb / hppab-nat.c
1 /* Machine-dependent hooks for the unix child process stratum. This
2 code is for the HP PA-RISC cpu.
3
4 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
5
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
8
9 This file is part of GDB.
10
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.
15
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.
20
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. */
24
25 #include "defs.h"
26 #include "inferior.h"
27 #include "target.h"
28 #include <sys/ptrace.h>
29
30 #ifndef PT_ATTACH
31 #define PT_ATTACH PTRACE_ATTACH
32 #endif
33 #ifndef PT_DETACH
34 #define PT_DETACH PTRACE_DETACH
35 #endif
36
37 /* This function simply calls ptrace with the given arguments.
38 It exists so that all calls to ptrace are isolated in this
39 machine-dependent file. */
40 #ifdef WANT_NATIVE_TARGET
41 int
42 call_ptrace (request, pid, addr, data)
43 int request, pid;
44 PTRACE_ARG3_TYPE addr;
45 int data;
46 {
47 return ptrace (request, pid, addr, data);
48 }
49 #endif /* WANT_NATIVE_TARGET */
50
51 #ifdef DEBUG_PTRACE
52 /* For the rest of the file, use an extra level of indirection */
53 /* This lets us breakpoint usefully on call_ptrace. */
54 #define ptrace call_ptrace
55 #endif
56
57 void
58 kill_inferior ()
59 {
60 if (inferior_pid == 0)
61 return;
62 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
63 wait ((int *)0);
64 target_mourn_inferior ();
65 }
66
67 #ifdef ATTACH_DETACH
68
69 /* Start debugging the process whose number is PID. */
70 int
71 attach (pid)
72 int pid;
73 {
74 errno = 0;
75 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
76 if (errno)
77 perror_with_name ("ptrace");
78 attach_flag = 1;
79 return pid;
80 }
81
82 /* Stop debugging the process whose number is PID
83 and continue it with signal number SIGNAL.
84 SIGNAL = 0 means just continue it. */
85
86 void
87 detach (signal)
88 int signal;
89 {
90 errno = 0;
91 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
92 if (errno)
93 perror_with_name ("ptrace");
94 attach_flag = 0;
95 }
96 #endif /* ATTACH_DETACH */
97 \f
98
99
100 #if !defined (FETCH_INFERIOR_REGISTERS)
101
102 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
103 to get the offset in the core file of the register values. */
104 #if defined (KERNEL_U_ADDR_BSD)
105 /* Get kernel_u_addr using BSD-style nlist(). */
106 CORE_ADDR kernel_u_addr;
107
108 #include <a.out.gnu.h> /* For struct nlist */
109
110 void
111 _initialize_kernel_u_addr ()
112 {
113 struct nlist names[2];
114
115 names[0].n_un.n_name = "_u";
116 names[1].n_un.n_name = NULL;
117 if (nlist ("/vmunix", names) == 0)
118 kernel_u_addr = names[0].n_value;
119 else
120 fatal ("Unable to get kernel u area address.");
121 }
122 #endif /* KERNEL_U_ADDR_BSD. */
123
124 #if defined (KERNEL_U_ADDR_HPUX)
125 /* Get kernel_u_addr using HPUX-style nlist(). */
126 CORE_ADDR kernel_u_addr;
127
128 struct hpnlist {
129 char * n_name;
130 long n_value;
131 unsigned char n_type;
132 unsigned char n_length;
133 short n_almod;
134 short n_unused;
135 };
136 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
137
138 /* read the value of the u area from the hp-ux kernel */
139 void _initialize_kernel_u_addr ()
140 {
141 struct user u;
142 nlist ("/hp-ux", &nl);
143 kernel_u_addr = nl[0].n_value;
144 }
145 #endif /* KERNEL_U_ADDR_HPUX. */
146
147 #if !defined (offsetof)
148 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
149 #endif
150
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) \
156 - KERNEL_U_ADDR
157 #endif
158
159 /* Registers we shouldn't try to fetch. */
160 #if !defined (CANNOT_FETCH_REGISTER)
161 #define CANNOT_FETCH_REGISTER(regno) 0
162 #endif
163
164 /* Fetch one register. */
165
166 static void
167 fetch_register (regno)
168 int regno;
169 {
170 register unsigned int regaddr;
171 char buf[MAX_REGISTER_RAW_SIZE];
172 char mess[128]; /* For messages */
173 register int i;
174
175 /* Offset of registers within the u area. */
176 unsigned int offset;
177
178 if (CANNOT_FETCH_REGISTER (regno))
179 {
180 bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
181 supply_register (regno, buf);
182 return;
183 }
184
185 offset = U_REGS_OFFSET;
186
187 regaddr = register_addr (regno, offset);
188 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
189 {
190 errno = 0;
191 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
192 (PTRACE_ARG3_TYPE) regaddr, 0);
193 regaddr += sizeof (int);
194 if (errno != 0)
195 {
196 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
197 perror_with_name (mess);
198 }
199 }
200 supply_register (regno, buf);
201 }
202
203 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
204 /* Fetch all registers, or just one, from the child process. */
205
206 #ifndef FETCH_INFERIOR_REGISTERS
207 void
208 fetch_inferior_registers (regno)
209 int regno;
210 {
211 if (regno == -1)
212 for (regno = 0; regno < NUM_REGS; regno++)
213 fetch_register (regno);
214 else
215 fetch_register (regno);
216 }
217
218 /* Registers we shouldn't try to store. */
219 #if !defined (CANNOT_STORE_REGISTER)
220 #define CANNOT_STORE_REGISTER(regno) 0
221 #endif
222
223 /* Store our register values back into the inferior.
224 If REGNO is -1, do this for all registers.
225 Otherwise, REGNO specifies which register (so we can save time). */
226
227 void
228 store_inferior_registers (regno)
229 int regno;
230 {
231 register unsigned int regaddr;
232 char buf[80];
233 extern char registers[];
234 register int i;
235
236 unsigned int offset = U_REGS_OFFSET;
237
238 if (regno >= 0)
239 {
240 regaddr = register_addr (regno, offset);
241 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
242 {
243 errno = 0;
244 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
245 *(int *) &registers[REGISTER_BYTE (regno) + i]);
246 if (errno != 0)
247 {
248 sprintf (buf, "writing register number %d(%d)", regno, i);
249 perror_with_name (buf);
250 }
251 regaddr += sizeof(int);
252 }
253 }
254 else
255 {
256 for (regno = 0; regno < NUM_REGS; regno++)
257 {
258 if (CANNOT_STORE_REGISTER (regno))
259 continue;
260 regaddr = register_addr (regno, offset);
261 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
262 {
263 errno = 0;
264 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
265 *(int *) &registers[REGISTER_BYTE (regno) + i]);
266 if (errno != 0)
267 {
268 sprintf (buf, "writing register number %d(%d)", regno, i);
269 perror_with_name (buf);
270 }
271 regaddr += sizeof(int);
272 }
273 }
274 }
275 return;
276 }
277 #endif /* !defined(FETCH_INFERIOR_REGISTERS) */
278
279 /* Resume execution of the inferior process.
280 If STEP is nonzero, single-step it.
281 If SIGNAL is nonzero, give it that signal. */
282
283 void
284 child_resume (step, signal)
285 int step;
286 int signal;
287 {
288 errno = 0;
289
290 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
291 it was. (If GDB wanted it to start some other way, we have already
292 written a new PC value to the child.) */
293
294 if (step)
295 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
296 else
297 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
298
299 if (errno)
300 perror_with_name ("ptrace");
301 }
302
303 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
304 in the NEW_SUN_PTRACE case.
305 It ought to be straightforward. But it appears that writing did
306 not write the data that I specified. I cannot understand where
307 it got the data that it actually did write. */
308
309 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
310 to debugger memory starting at MYADDR. Copy to inferior if
311 WRITE is nonzero.
312
313 Returns the length copied, which is either the LEN argument or zero.
314 This xfer function does not do partial moves, since child_ops
315 doesn't allow memory operations to cross below us in the target stack
316 anyway. */
317
318 int
319 child_xfer_memory (memaddr, myaddr, len, write, target)
320 CORE_ADDR memaddr;
321 char *myaddr;
322 int len;
323 int write;
324 struct target_ops *target; /* ignored */
325 {
326 register int i;
327 /* Round starting address down to longword boundary. */
328 register CORE_ADDR addr = memaddr & - sizeof (int);
329 /* Round ending address up; get number of longwords that makes. */
330 register int count
331 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
332 /* Allocate buffer of that many longwords. */
333 register int *buffer = (int *) alloca (count * sizeof (int));
334
335 if (write)
336 {
337 /* Fill start and end extra bytes of buffer with existing memory data. */
338
339 if (addr != memaddr || len < (int)sizeof (int)) {
340 /* Need part of initial word -- fetch it. */
341 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
342 0);
343 }
344
345 if (count > 1) /* FIXME, avoid if even boundary */
346 {
347 buffer[count - 1]
348 = ptrace (PT_READ_I, inferior_pid,
349 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
350 0);
351 }
352
353 /* Copy data to be written over corresponding part of buffer */
354
355 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
356
357 /* Write the entire buffer. */
358
359 for (i = 0; i < count; i++, addr += sizeof (int))
360 {
361 errno = 0;
362 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
363 buffer[i]);
364 if (errno)
365 {
366 /* Using the appropriate one (I or D) is necessary for
367 Gould NP1, at least. */
368 errno = 0;
369 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
370 buffer[i]);
371 }
372 if (errno)
373 return 0;
374 }
375 }
376 else
377 {
378 /* Read all the longwords */
379 for (i = 0; i < count; i++, addr += sizeof (int))
380 {
381 errno = 0;
382 buffer[i] = ptrace (PT_READ_I, inferior_pid,
383 (PTRACE_ARG3_TYPE) addr, 0);
384 if (errno)
385 return 0;
386 QUIT;
387 }
388
389 /* Copy appropriate bytes out of the buffer. */
390 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
391 }
392 return len;
393 }
394
This page took 0.03709 seconds and 4 git commands to generate.