Keep Lynx files
[deliverable/binutils-gdb.git] / gdb / hppah-nat.c
CommitLineData
ec68a93f 1/* Machine-dependent hooks for the unix child process stratum, for HPUX PA-RISC.
ca048722 2
ec68a93f
JG
3 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993
4 Free Software Foundation, Inc.
ca048722
RP
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
31extern CORE_ADDR text_end;
ca048722 32
01d1590b 33static void fetch_register ();
ca048722
RP
34
35/* This function simply calls ptrace with the given arguments.
36 It exists so that all calls to ptrace are isolated in this
37 machine-dependent file. */
ca048722
RP
38int
39call_ptrace (request, pid, addr, data)
40 int request, pid;
41 PTRACE_ARG3_TYPE addr;
42 int data;
43{
44 return ptrace (request, pid, addr, data, 0);
45}
ca048722
RP
46
47void
48kill_inferior ()
49{
50 if (inferior_pid == 0)
51 return;
01d1590b 52 ptrace (PT_EXIT, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
ca048722
RP
53 wait ((int *)0);
54 target_mourn_inferior ();
55}
56
ca048722
RP
57/* Start debugging the process whose number is PID. */
58int
59attach (pid)
60 int pid;
61{
62 errno = 0;
63 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
64 if (errno)
65 perror_with_name ("ptrace");
66 attach_flag = 1;
67 return pid;
68}
69
70/* Stop debugging the process whose number is PID
71 and continue it with signal number SIGNAL.
72 SIGNAL = 0 means just continue it. */
73
74void
75detach (signal)
76 int signal;
77{
78 errno = 0;
79 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
80 if (errno)
81 perror_with_name ("ptrace");
82 attach_flag = 0;
83}
ca048722
RP
84
85/* Fetch all registers, or just one, from the child process. */
86
87void
88fetch_inferior_registers (regno)
89 int regno;
90{
91 if (regno == -1)
92 for (regno = 0; regno < NUM_REGS; regno++)
93 fetch_register (regno);
94 else
95 fetch_register (regno);
96}
97
ca048722
RP
98/* Store our register values back into the inferior.
99 If REGNO is -1, do this for all registers.
100 Otherwise, REGNO specifies which register (so we can save time). */
101
102void
103store_inferior_registers (regno)
104 int regno;
105{
106 register unsigned int regaddr;
107 char buf[80];
108 extern char registers[];
109 register int i;
ca048722 110 unsigned int offset = U_REGS_OFFSET;
9f739abd 111 int scratch;
ca048722
RP
112
113 if (regno >= 0)
114 {
115 regaddr = register_addr (regno, offset);
9f739abd
SG
116 errno = 0;
117 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
118 {
119 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
120 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
121 scratch, 0);
122 if (errno != 0)
123 {
72943ad0
JK
124 /* Error, even if attached. Failing to write these two
125 registers is pretty serious. */
bf5b632d 126 sprintf (buf, "writing register number %d", regno);
9f739abd
SG
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 {
72943ad0
JK
138 /* Warning, not error, in case we are attached; sometimes the
139 kernel doesn't let us at the registers. */
140 char *err = safe_strerror (errno);
141 char *msg = alloca (strlen (err) + 128);
142 sprintf (msg, "writing register %s: %s",
143 reg_names[regno], err);
144 warning (msg);
145 goto error_exit;
9f739abd
SG
146 }
147 regaddr += sizeof(int);
148 }
ca048722
RP
149 }
150 else
151 {
152 for (regno = 0; regno < NUM_REGS; regno++)
153 {
9f739abd
SG
154 if (CANNOT_STORE_REGISTER (regno))
155 continue;
72943ad0 156 store_inferior_registers (regno);
ca048722
RP
157 }
158 }
72943ad0 159 error_exit:
ca048722
RP
160 return;
161}
162
ca048722
RP
163/* Fetch one register. */
164
165static void
166fetch_register (regno)
167 int regno;
168{
169 register unsigned int regaddr;
170 char buf[MAX_REGISTER_RAW_SIZE];
171 char mess[128]; /* For messages */
172 register int i;
173
174 /* Offset of registers within the u area. */
175 unsigned int offset;
176
ca048722
RP
177 offset = U_REGS_OFFSET;
178
179 regaddr = register_addr (regno, offset);
180 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
181 {
182 errno = 0;
183 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
184 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
185 regaddr += sizeof (int);
186 if (errno != 0)
187 {
72943ad0
JK
188 /* Warning, not error, in case we are attached; sometimes the
189 kernel doesn't let us at the registers. */
190 char *err = safe_strerror (errno);
191 char *msg = alloca (strlen (err) + 128);
192 sprintf (msg, "reading register %s: %s", reg_names[regno], err);
193 warning (msg);
194 goto error_exit;
ca048722
RP
195 }
196 }
9f739abd
SG
197 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
198 buf[3] &= ~0x3;
ca048722 199 supply_register (regno, buf);
72943ad0 200 error_exit:;
ca048722
RP
201}
202
25286543 203/* Resume execution of process PID.
ca048722
RP
204 If STEP is nonzero, single-step it.
205 If SIGNAL is nonzero, give it that signal. */
206
207void
25286543
SG
208child_resume (pid, step, signal)
209 int pid;
ca048722
RP
210 int step;
211 int signal;
212{
213 errno = 0;
214
de43d7d0
SG
215 if (pid == -1)
216 pid = inferior_pid;
217
ca048722
RP
218 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
219 it was. (If GDB wanted it to start some other way, we have already
220 written a new PC value to the child.) */
221
222 if (step)
202eca5e 223 ptrace (PT_SINGLE, pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
ca048722 224 else
202eca5e 225 ptrace (PT_CONTIN, pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
ca048722
RP
226
227 if (errno)
228 perror_with_name ("ptrace");
229}
230
ca048722
RP
231/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
232 to debugger memory starting at MYADDR. Copy to inferior if
233 WRITE is nonzero.
234
235 Returns the length copied, which is either the LEN argument or zero.
236 This xfer function does not do partial moves, since child_ops
237 doesn't allow memory operations to cross below us in the target stack
238 anyway. */
239
240int
241child_xfer_memory (memaddr, myaddr, len, write, target)
242 CORE_ADDR memaddr;
243 char *myaddr;
244 int len;
245 int write;
246 struct target_ops *target; /* ignored */
247{
248 register int i;
249 /* Round starting address down to longword boundary. */
250 register CORE_ADDR addr = memaddr & - sizeof (int);
251 /* Round ending address up; get number of longwords that makes. */
252 register int count
253 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
254 /* Allocate buffer of that many longwords. */
255 register int *buffer = (int *) alloca (count * sizeof (int));
256
257 if (write)
258 {
259 /* Fill start and end extra bytes of buffer with existing memory data. */
260
261 if (addr != memaddr || len < (int)sizeof (int)) {
262 /* Need part of initial word -- fetch it. */
9f739abd
SG
263 buffer[0] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
264 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
265 }
266
267 if (count > 1) /* FIXME, avoid if even boundary */
268 {
269 buffer[count - 1]
9f739abd 270 = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid,
ca048722
RP
271 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
272 0, 0);
273 }
274
275 /* Copy data to be written over corresponding part of buffer */
276
ade40d31 277 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
ca048722
RP
278
279 /* Write the entire buffer. */
280
281 for (i = 0; i < count; i++, addr += sizeof (int))
282 {
ca048722
RP
283/* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
284 segment. FIXME -- does it work to write into the data segment using
285 WIUSER, or do these idiots really expect us to figure out which segment
286 the address is in, so we can use a separate system call for it??! */
287 errno = 0;
9f739abd
SG
288 ptrace (addr < text_end ? PT_WIUSER : PT_WDUSER, inferior_pid,
289 (PTRACE_ARG3_TYPE) addr,
ca048722
RP
290 buffer[i], 0);
291 if (errno)
ca048722
RP
292 return 0;
293 }
294 }
295 else
296 {
297 /* Read all the longwords */
298 for (i = 0; i < count; i++, addr += sizeof (int))
299 {
300 errno = 0;
9f739abd
SG
301 buffer[i] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
302 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
303 if (errno)
304 return 0;
305 QUIT;
306 }
307
308 /* Copy appropriate bytes out of the buffer. */
ade40d31 309 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
ca048722
RP
310 }
311 return len;
312}
This page took 0.09248 seconds and 4 git commands to generate.