* Makefile.in: added variables CHILL_FOR_TARGET, CHILLFLAGS,
[deliverable/binutils-gdb.git] / gdb / hppah-nat.c
CommitLineData
ca048722
RP
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 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
9This file is part of GDB.
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24
25
26#include "defs.h"
27#include "inferior.h"
01d1590b
SG
28#include "target.h"
29#include <sys/ptrace.h>
9f739abd
SG
30#include <sys/param.h>
31#include <sys/user.h>
32
33extern CORE_ADDR text_end;
ca048722 34
01d1590b 35static void fetch_register ();
ca048722
RP
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. */
ca048722
RP
40int
41call_ptrace (request, pid, addr, data)
42 int request, pid;
43 PTRACE_ARG3_TYPE addr;
44 int data;
45{
46 return ptrace (request, pid, addr, data, 0);
47}
ca048722
RP
48
49void
50kill_inferior ()
51{
52 if (inferior_pid == 0)
53 return;
01d1590b 54 ptrace (PT_EXIT, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
ca048722
RP
55 wait ((int *)0);
56 target_mourn_inferior ();
57}
58
ca048722
RP
59/* Start debugging the process whose number is PID. */
60int
61attach (pid)
62 int pid;
63{
64 errno = 0;
65 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
66 if (errno)
67 perror_with_name ("ptrace");
68 attach_flag = 1;
69 return pid;
70}
71
72/* Stop debugging the process whose number is PID
73 and continue it with signal number SIGNAL.
74 SIGNAL = 0 means just continue it. */
75
76void
77detach (signal)
78 int signal;
79{
80 errno = 0;
81 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
82 if (errno)
83 perror_with_name ("ptrace");
84 attach_flag = 0;
85}
ca048722
RP
86
87/* Fetch all registers, or just one, from the child process. */
88
89void
90fetch_inferior_registers (regno)
91 int regno;
92{
93 if (regno == -1)
94 for (regno = 0; regno < NUM_REGS; regno++)
95 fetch_register (regno);
96 else
97 fetch_register (regno);
98}
99
ca048722
RP
100/* Store our register values back into the inferior.
101 If REGNO is -1, do this for all registers.
102 Otherwise, REGNO specifies which register (so we can save time). */
103
104void
105store_inferior_registers (regno)
106 int regno;
107{
108 register unsigned int regaddr;
109 char buf[80];
110 extern char registers[];
111 register int i;
ca048722 112 unsigned int offset = U_REGS_OFFSET;
9f739abd 113 int scratch;
ca048722
RP
114
115 if (regno >= 0)
116 {
117 regaddr = register_addr (regno, offset);
9f739abd
SG
118 errno = 0;
119 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
120 {
121 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
122 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
123 scratch, 0);
124 if (errno != 0)
125 {
126 sprintf (buf, "writing register number %d(%d)", regno, i);
127 perror_with_name (buf);
128 }
129 }
130 else
131 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
132 {
133 errno = 0;
134 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
135 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
136 if (errno != 0)
137 {
138 sprintf (buf, "writing register number %d(%d)", regno, i);
139 perror_with_name (buf);
140 }
141 regaddr += sizeof(int);
142 }
ca048722
RP
143 }
144 else
145 {
146 for (regno = 0; regno < NUM_REGS; regno++)
147 {
9f739abd
SG
148 if (CANNOT_STORE_REGISTER (regno))
149 continue;
ca048722 150 regaddr = register_addr (regno, offset);
9f739abd
SG
151 errno = 0;
152 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
153 {
154 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
155 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
156 scratch, 0);
157 if (errno != 0)
158 {
159 sprintf (buf, "writing register number %d(%d)", regno, i);
160 perror_with_name (buf);
161 }
162 }
163 else
164 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
165 {
166 errno = 0;
167 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
168 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
169 if (errno != 0)
170 {
171 sprintf (buf, "writing register number %d(%d)", regno, i);
172 perror_with_name (buf);
173 }
174 regaddr += sizeof(int);
175 }
ca048722
RP
176 }
177 }
178 return;
179}
180
181/* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
182 to get the offset in the core file of the register values. */
ca048722 183
ca048722
RP
184/* Get kernel_u_addr using HPUX-style nlist(). */
185CORE_ADDR kernel_u_addr;
186
187struct hpnlist {
188 char * n_name;
189 long n_value;
190 unsigned char n_type;
191 unsigned char n_length;
192 short n_almod;
193 short n_unused;
194};
195static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
196
197/* read the value of the u area from the hp-ux kernel */
198void _initialize_kernel_u_addr ()
199{
200 struct user u;
201 nlist ("/hp-ux", &nl);
202 kernel_u_addr = nl[0].n_value;
203}
ca048722
RP
204
205#if !defined (offsetof)
206#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
207#endif
208
209/* U_REGS_OFFSET is the offset of the registers within the u area. */
210#if !defined (U_REGS_OFFSET)
211#define U_REGS_OFFSET \
212 ptrace (PT_READ_U, inferior_pid, \
213 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \
214 - KERNEL_U_ADDR
215#endif
216
ca048722
RP
217/* Fetch one register. */
218
219static void
220fetch_register (regno)
221 int regno;
222{
223 register unsigned int regaddr;
224 char buf[MAX_REGISTER_RAW_SIZE];
225 char mess[128]; /* For messages */
226 register int i;
227
228 /* Offset of registers within the u area. */
229 unsigned int offset;
230
ca048722
RP
231 offset = U_REGS_OFFSET;
232
233 regaddr = register_addr (regno, offset);
234 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
235 {
236 errno = 0;
237 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
238 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
239 regaddr += sizeof (int);
240 if (errno != 0)
241 {
242 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
243 perror_with_name (mess);
244 }
245 }
9f739abd
SG
246 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
247 buf[3] &= ~0x3;
ca048722
RP
248 supply_register (regno, buf);
249}
250
ca048722
RP
251/* Resume execution of the inferior process.
252 If STEP is nonzero, single-step it.
253 If SIGNAL is nonzero, give it that signal. */
254
255void
256child_resume (step, signal)
257 int step;
258 int signal;
259{
260 errno = 0;
261
262 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
263 it was. (If GDB wanted it to start some other way, we have already
264 written a new PC value to the child.) */
265
266 if (step)
267 ptrace (PT_SINGLE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
268 else
269 ptrace (PT_CONTIN, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
270
271 if (errno)
272 perror_with_name ("ptrace");
273}
274
275/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
276 in the NEW_SUN_PTRACE case.
277 It ought to be straightforward. But it appears that writing did
278 not write the data that I specified. I cannot understand where
279 it got the data that it actually did write. */
280
281/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
282 to debugger memory starting at MYADDR. Copy to inferior if
283 WRITE is nonzero.
284
285 Returns the length copied, which is either the LEN argument or zero.
286 This xfer function does not do partial moves, since child_ops
287 doesn't allow memory operations to cross below us in the target stack
288 anyway. */
289
290int
291child_xfer_memory (memaddr, myaddr, len, write, target)
292 CORE_ADDR memaddr;
293 char *myaddr;
294 int len;
295 int write;
296 struct target_ops *target; /* ignored */
297{
298 register int i;
299 /* Round starting address down to longword boundary. */
300 register CORE_ADDR addr = memaddr & - sizeof (int);
301 /* Round ending address up; get number of longwords that makes. */
302 register int count
303 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
304 /* Allocate buffer of that many longwords. */
305 register int *buffer = (int *) alloca (count * sizeof (int));
306
307 if (write)
308 {
309 /* Fill start and end extra bytes of buffer with existing memory data. */
310
311 if (addr != memaddr || len < (int)sizeof (int)) {
312 /* Need part of initial word -- fetch it. */
9f739abd
SG
313 buffer[0] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
314 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
315 }
316
317 if (count > 1) /* FIXME, avoid if even boundary */
318 {
319 buffer[count - 1]
9f739abd 320 = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid,
ca048722
RP
321 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
322 0, 0);
323 }
324
325 /* Copy data to be written over corresponding part of buffer */
326
327 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
328
329 /* Write the entire buffer. */
330
331 for (i = 0; i < count; i++, addr += sizeof (int))
332 {
ca048722
RP
333/* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
334 segment. FIXME -- does it work to write into the data segment using
335 WIUSER, or do these idiots really expect us to figure out which segment
336 the address is in, so we can use a separate system call for it??! */
337 errno = 0;
9f739abd
SG
338 ptrace (addr < text_end ? PT_WIUSER : PT_WDUSER, inferior_pid,
339 (PTRACE_ARG3_TYPE) addr,
ca048722
RP
340 buffer[i], 0);
341 if (errno)
ca048722
RP
342 return 0;
343 }
344 }
345 else
346 {
347 /* Read all the longwords */
348 for (i = 0; i < count; i++, addr += sizeof (int))
349 {
350 errno = 0;
9f739abd
SG
351 buffer[i] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
352 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
353 if (errno)
354 return 0;
355 QUIT;
356 }
357
358 /* Copy appropriate bytes out of the buffer. */
359 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
360 }
361 return len;
362}
This page took 0.044276 seconds and 4 git commands to generate.