* hppa-pinsn.c (print_insn): Improve handling of be and ble
[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
251
252
253/* Resume execution of the inferior process.
254 If STEP is nonzero, single-step it.
255 If SIGNAL is nonzero, give it that signal. */
256
257void
258child_resume (step, signal)
259 int step;
260 int signal;
261{
262 errno = 0;
263
264 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
265 it was. (If GDB wanted it to start some other way, we have already
266 written a new PC value to the child.) */
267
268 if (step)
269 ptrace (PT_SINGLE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
270 else
271 ptrace (PT_CONTIN, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
272
273 if (errno)
274 perror_with_name ("ptrace");
275}
276
277/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
278 in the NEW_SUN_PTRACE case.
279 It ought to be straightforward. But it appears that writing did
280 not write the data that I specified. I cannot understand where
281 it got the data that it actually did write. */
282
283/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
284 to debugger memory starting at MYADDR. Copy to inferior if
285 WRITE is nonzero.
286
287 Returns the length copied, which is either the LEN argument or zero.
288 This xfer function does not do partial moves, since child_ops
289 doesn't allow memory operations to cross below us in the target stack
290 anyway. */
291
292int
293child_xfer_memory (memaddr, myaddr, len, write, target)
294 CORE_ADDR memaddr;
295 char *myaddr;
296 int len;
297 int write;
298 struct target_ops *target; /* ignored */
299{
300 register int i;
301 /* Round starting address down to longword boundary. */
302 register CORE_ADDR addr = memaddr & - sizeof (int);
303 /* Round ending address up; get number of longwords that makes. */
304 register int count
305 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
306 /* Allocate buffer of that many longwords. */
307 register int *buffer = (int *) alloca (count * sizeof (int));
308
309 if (write)
310 {
311 /* Fill start and end extra bytes of buffer with existing memory data. */
312
313 if (addr != memaddr || len < (int)sizeof (int)) {
314 /* Need part of initial word -- fetch it. */
9f739abd
SG
315 buffer[0] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
316 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
317 }
318
319 if (count > 1) /* FIXME, avoid if even boundary */
320 {
321 buffer[count - 1]
9f739abd 322 = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid,
ca048722
RP
323 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
324 0, 0);
325 }
326
327 /* Copy data to be written over corresponding part of buffer */
328
329 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
330
331 /* Write the entire buffer. */
332
333 for (i = 0; i < count; i++, addr += sizeof (int))
334 {
ca048722
RP
335/* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
336 segment. FIXME -- does it work to write into the data segment using
337 WIUSER, or do these idiots really expect us to figure out which segment
338 the address is in, so we can use a separate system call for it??! */
339 errno = 0;
9f739abd
SG
340 ptrace (addr < text_end ? PT_WIUSER : PT_WDUSER, inferior_pid,
341 (PTRACE_ARG3_TYPE) addr,
ca048722
RP
342 buffer[i], 0);
343 if (errno)
ca048722
RP
344 return 0;
345 }
346 }
347 else
348 {
349 /* Read all the longwords */
350 for (i = 0; i < count; i++, addr += sizeof (int))
351 {
352 errno = 0;
9f739abd
SG
353 buffer[i] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
354 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
355 if (errno)
356 return 0;
357 QUIT;
358 }
359
360 /* Copy appropriate bytes out of the buffer. */
361 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
362 }
363 return len;
364}
This page took 0.041145 seconds and 4 git commands to generate.