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