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