* infptrace.c (child_xfer_memory): Only use if CHILD_XFER_MEMORY
[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 34
ca048722
RP
35void
36fetch_inferior_registers (regno)
37 int regno;
38{
39 if (regno == -1)
40 for (regno = 0; regno < NUM_REGS; regno++)
41 fetch_register (regno);
42 else
43 fetch_register (regno);
44}
45
ca048722
RP
46/* Store our register values back into the inferior.
47 If REGNO is -1, do this for all registers.
48 Otherwise, REGNO specifies which register (so we can save time). */
49
50void
51store_inferior_registers (regno)
52 int regno;
53{
54 register unsigned int regaddr;
55 char buf[80];
56 extern char registers[];
57 register int i;
ca048722 58 unsigned int offset = U_REGS_OFFSET;
9f739abd 59 int scratch;
ca048722
RP
60
61 if (regno >= 0)
62 {
63 regaddr = register_addr (regno, offset);
9f739abd
SG
64 errno = 0;
65 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
66 {
67 scratch = *(int *) &registers[REGISTER_BYTE (regno)] | 0x3;
68 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
69 scratch, 0);
70 if (errno != 0)
71 {
72943ad0
JK
72 /* Error, even if attached. Failing to write these two
73 registers is pretty serious. */
bf5b632d 74 sprintf (buf, "writing register number %d", regno);
9f739abd
SG
75 perror_with_name (buf);
76 }
77 }
78 else
79 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
80 {
81 errno = 0;
82 ptrace (PT_WUREGS, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
83 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
84 if (errno != 0)
85 {
72943ad0
JK
86 /* Warning, not error, in case we are attached; sometimes the
87 kernel doesn't let us at the registers. */
88 char *err = safe_strerror (errno);
89 char *msg = alloca (strlen (err) + 128);
90 sprintf (msg, "writing register %s: %s",
91 reg_names[regno], err);
92 warning (msg);
93 goto error_exit;
9f739abd
SG
94 }
95 regaddr += sizeof(int);
96 }
ca048722
RP
97 }
98 else
99 {
100 for (regno = 0; regno < NUM_REGS; regno++)
101 {
9f739abd
SG
102 if (CANNOT_STORE_REGISTER (regno))
103 continue;
72943ad0 104 store_inferior_registers (regno);
ca048722
RP
105 }
106 }
72943ad0 107 error_exit:
ca048722
RP
108 return;
109}
110
ca048722
RP
111/* Fetch one register. */
112
113static void
114fetch_register (regno)
115 int regno;
116{
117 register unsigned int regaddr;
118 char buf[MAX_REGISTER_RAW_SIZE];
119 char mess[128]; /* For messages */
120 register int i;
121
122 /* Offset of registers within the u area. */
123 unsigned int offset;
124
ca048722
RP
125 offset = U_REGS_OFFSET;
126
127 regaddr = register_addr (regno, offset);
128 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
129 {
130 errno = 0;
131 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
132 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
133 regaddr += sizeof (int);
134 if (errno != 0)
135 {
72943ad0
JK
136 /* Warning, not error, in case we are attached; sometimes the
137 kernel doesn't let us at the registers. */
138 char *err = safe_strerror (errno);
139 char *msg = alloca (strlen (err) + 128);
140 sprintf (msg, "reading register %s: %s", reg_names[regno], err);
141 warning (msg);
142 goto error_exit;
ca048722
RP
143 }
144 }
9f739abd
SG
145 if (regno == PCOQ_HEAD_REGNUM || regno == PCOQ_TAIL_REGNUM)
146 buf[3] &= ~0x3;
ca048722 147 supply_register (regno, buf);
72943ad0 148 error_exit:;
ca048722
RP
149}
150
ca048722
RP
151/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
152 to debugger memory starting at MYADDR. Copy to inferior if
153 WRITE is nonzero.
154
155 Returns the length copied, which is either the LEN argument or zero.
156 This xfer function does not do partial moves, since child_ops
157 doesn't allow memory operations to cross below us in the target stack
158 anyway. */
159
160int
161child_xfer_memory (memaddr, myaddr, len, write, target)
162 CORE_ADDR memaddr;
163 char *myaddr;
164 int len;
165 int write;
166 struct target_ops *target; /* ignored */
167{
168 register int i;
169 /* Round starting address down to longword boundary. */
170 register CORE_ADDR addr = memaddr & - sizeof (int);
171 /* Round ending address up; get number of longwords that makes. */
172 register int count
173 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
174 /* Allocate buffer of that many longwords. */
175 register int *buffer = (int *) alloca (count * sizeof (int));
176
177 if (write)
178 {
179 /* Fill start and end extra bytes of buffer with existing memory data. */
180
181 if (addr != memaddr || len < (int)sizeof (int)) {
182 /* Need part of initial word -- fetch it. */
9f739abd
SG
183 buffer[0] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
184 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
185 }
186
187 if (count > 1) /* FIXME, avoid if even boundary */
188 {
189 buffer[count - 1]
9f739abd 190 = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER, inferior_pid,
ca048722
RP
191 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
192 0, 0);
193 }
194
195 /* Copy data to be written over corresponding part of buffer */
196
ade40d31 197 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
ca048722
RP
198
199 /* Write the entire buffer. */
200
201 for (i = 0; i < count; i++, addr += sizeof (int))
202 {
ca048722
RP
203/* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
204 segment. FIXME -- does it work to write into the data segment using
205 WIUSER, or do these idiots really expect us to figure out which segment
206 the address is in, so we can use a separate system call for it??! */
207 errno = 0;
9f739abd
SG
208 ptrace (addr < text_end ? PT_WIUSER : PT_WDUSER, inferior_pid,
209 (PTRACE_ARG3_TYPE) addr,
ca048722
RP
210 buffer[i], 0);
211 if (errno)
ca048722
RP
212 return 0;
213 }
214 }
215 else
216 {
217 /* Read all the longwords */
218 for (i = 0; i < count; i++, addr += sizeof (int))
219 {
220 errno = 0;
9f739abd
SG
221 buffer[i] = ptrace (addr < text_end ? PT_RIUSER : PT_RDUSER,
222 inferior_pid, (PTRACE_ARG3_TYPE) addr, 0, 0);
ca048722
RP
223 if (errno)
224 return 0;
225 QUIT;
226 }
227
228 /* Copy appropriate bytes out of the buffer. */
ade40d31 229 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
ca048722
RP
230 }
231 return len;
232}
This page took 0.096471 seconds and 4 git commands to generate.