* hppa-pinsn.c (print_insn): Improve handling of be and ble
[deliverable/binutils-gdb.git] / gdb / hppah-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 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
26 #include "defs.h"
27 #include "inferior.h"
28 #include "target.h"
29 #include <sys/ptrace.h>
30 #include <sys/param.h>
31 #include <sys/user.h>
32
33 extern CORE_ADDR text_end;
34
35 static void fetch_register ();
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 int
41 call_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 }
48
49 void
50 kill_inferior ()
51 {
52 if (inferior_pid == 0)
53 return;
54 ptrace (PT_EXIT, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
55 wait ((int *)0);
56 target_mourn_inferior ();
57 }
58
59 /* Start debugging the process whose number is PID. */
60 int
61 attach (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
76 void
77 detach (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 }
86
87 /* Fetch all registers, or just one, from the child process. */
88
89 void
90 fetch_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
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
104 void
105 store_inferior_registers (regno)
106 int regno;
107 {
108 register unsigned int regaddr;
109 char buf[80];
110 extern char registers[];
111 register int i;
112 unsigned int offset = U_REGS_OFFSET;
113 int scratch;
114
115 if (regno >= 0)
116 {
117 regaddr = register_addr (regno, offset);
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 }
143 }
144 else
145 {
146 for (regno = 0; regno < NUM_REGS; regno++)
147 {
148 if (CANNOT_STORE_REGISTER (regno))
149 continue;
150 regaddr = register_addr (regno, offset);
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 }
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. */
183
184 /* Get kernel_u_addr using HPUX-style nlist(). */
185 CORE_ADDR kernel_u_addr;
186
187 struct 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 };
195 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
196
197 /* read the value of the u area from the hp-ux kernel */
198 void _initialize_kernel_u_addr ()
199 {
200 struct user u;
201 nlist ("/hp-ux", &nl);
202 kernel_u_addr = nl[0].n_value;
203 }
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
217 /* Fetch one register. */
218
219 static void
220 fetch_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
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 }
246 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
247 buf[3] &= ~0x3;
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
257 void
258 child_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
292 int
293 child_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. */
315 buffer[0] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
316 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
317 }
318
319 if (count > 1) /* FIXME, avoid if even boundary */
320 {
321 buffer[count - 1]
322 = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid,
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 {
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;
340 ptrace (addr < text_end ? PT_WIUSER : PT_WDUSER, inferior_pid,
341 (PTRACE_ARG3_TYPE) addr,
342 buffer[i], 0);
343 if (errno)
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;
353 buffer[i] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
354 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
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.039029 seconds and 4 git commands to generate.