* complaints.c: New file, code moved from utils.c.
[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
31 static void fetch_register ();
32
33 /* This function simply calls ptrace with the given arguments.
34 It exists so that all calls to ptrace are isolated in this
35 machine-dependent file. */
36 int
37 call_ptrace (request, pid, addr, data)
38 int request, pid;
39 PTRACE_ARG3_TYPE addr;
40 int data;
41 {
42 return ptrace (request, pid, addr, data, 0);
43 }
44
45 void
46 kill_inferior ()
47 {
48 if (inferior_pid == 0)
49 return;
50 ptrace (PT_EXIT, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
51 wait ((int *)0);
52 target_mourn_inferior ();
53 }
54
55 /* Start debugging the process whose number is PID. */
56 int
57 attach (pid)
58 int pid;
59 {
60 errno = 0;
61 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0, 0);
62 if (errno)
63 perror_with_name ("ptrace");
64 attach_flag = 1;
65 return pid;
66 }
67
68 /* Stop debugging the process whose number is PID
69 and continue it with signal number SIGNAL.
70 SIGNAL = 0 means just continue it. */
71
72 void
73 detach (signal)
74 int signal;
75 {
76 errno = 0;
77 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
78 if (errno)
79 perror_with_name ("ptrace");
80 attach_flag = 0;
81 }
82
83 /* Fetch all registers, or just one, from the child process. */
84
85 void
86 fetch_inferior_registers (regno)
87 int regno;
88 {
89 if (regno == -1)
90 for (regno = 0; regno < NUM_REGS; regno++)
91 fetch_register (regno);
92 else
93 fetch_register (regno);
94 }
95
96 /* Store our register values back into the inferior.
97 If REGNO is -1, do this for all registers.
98 Otherwise, REGNO specifies which register (so we can save time). */
99
100 void
101 store_inferior_registers (regno)
102 int regno;
103 {
104 register unsigned int regaddr;
105 char buf[80];
106 extern char registers[];
107 register int i;
108
109 unsigned int offset = U_REGS_OFFSET;
110
111 if (regno >= 0)
112 {
113 regaddr = register_addr (regno, offset);
114 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
115 {
116 errno = 0;
117 ptrace (PT_WUAREA, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
118 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
119 if (errno != 0)
120 {
121 sprintf (buf, "writing register number %d(%d)", regno, i);
122 perror_with_name (buf);
123 }
124 regaddr += sizeof(int);
125 }
126 }
127 else
128 {
129 for (regno = 0; regno < NUM_REGS; regno++)
130 {
131 regaddr = register_addr (regno, offset);
132 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
133 {
134 errno = 0;
135 ptrace (PT_WUAREA, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
136 *(int *) &registers[REGISTER_BYTE (regno) + i], 0);
137 if (errno != 0)
138 {
139 sprintf (buf, "writing register number %d(%d)", regno, i);
140 perror_with_name (buf);
141 }
142 regaddr += sizeof(int);
143 }
144 }
145 }
146 return;
147 }
148
149 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
150 to get the offset in the core file of the register values. */
151
152 /* Get kernel_u_addr using HPUX-style nlist(). */
153 CORE_ADDR kernel_u_addr;
154
155 struct hpnlist {
156 char * n_name;
157 long n_value;
158 unsigned char n_type;
159 unsigned char n_length;
160 short n_almod;
161 short n_unused;
162 };
163 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
164
165 /* read the value of the u area from the hp-ux kernel */
166 void _initialize_kernel_u_addr ()
167 {
168 #if 0
169 struct user u;
170 #endif
171 nlist ("/hp-ux", &nl);
172 kernel_u_addr = nl[0].n_value;
173 }
174
175 #if !defined (offsetof)
176 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
177 #endif
178
179 /* U_REGS_OFFSET is the offset of the registers within the u area. */
180 #if !defined (U_REGS_OFFSET)
181 #define U_REGS_OFFSET \
182 ptrace (PT_READ_U, inferior_pid, \
183 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0, 0) \
184 - KERNEL_U_ADDR
185 #endif
186
187 /* Fetch one register. */
188
189 static void
190 fetch_register (regno)
191 int regno;
192 {
193 register unsigned int regaddr;
194 char buf[MAX_REGISTER_RAW_SIZE];
195 char mess[128]; /* For messages */
196 register int i;
197
198 /* Offset of registers within the u area. */
199 unsigned int offset;
200
201 offset = U_REGS_OFFSET;
202
203 regaddr = register_addr (regno, offset);
204 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
205 {
206 errno = 0;
207 *(int *) &buf[i] = ptrace (PT_RUREGS, inferior_pid,
208 (PTRACE_ARG3_TYPE) regaddr, 0, 0);
209 regaddr += sizeof (int);
210 if (errno != 0)
211 {
212 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
213 perror_with_name (mess);
214 }
215 }
216 supply_register (regno, buf);
217 }
218
219
220
221 /* Resume execution of the inferior process.
222 If STEP is nonzero, single-step it.
223 If SIGNAL is nonzero, give it that signal. */
224
225 void
226 child_resume (step, signal)
227 int step;
228 int signal;
229 {
230 errno = 0;
231
232 /* An address of (PTRACE_ARG3_TYPE) 1 tells ptrace to continue from where
233 it was. (If GDB wanted it to start some other way, we have already
234 written a new PC value to the child.) */
235
236 if (step)
237 ptrace (PT_SINGLE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
238 else
239 ptrace (PT_CONTIN, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal, 0);
240
241 if (errno)
242 perror_with_name ("ptrace");
243 }
244
245 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
246 in the NEW_SUN_PTRACE case.
247 It ought to be straightforward. But it appears that writing did
248 not write the data that I specified. I cannot understand where
249 it got the data that it actually did write. */
250
251 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
252 to debugger memory starting at MYADDR. Copy to inferior if
253 WRITE is nonzero.
254
255 Returns the length copied, which is either the LEN argument or zero.
256 This xfer function does not do partial moves, since child_ops
257 doesn't allow memory operations to cross below us in the target stack
258 anyway. */
259
260 int
261 child_xfer_memory (memaddr, myaddr, len, write, target)
262 CORE_ADDR memaddr;
263 char *myaddr;
264 int len;
265 int write;
266 struct target_ops *target; /* ignored */
267 {
268 register int i;
269 /* Round starting address down to longword boundary. */
270 register CORE_ADDR addr = memaddr & - sizeof (int);
271 /* Round ending address up; get number of longwords that makes. */
272 register int count
273 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
274 /* Allocate buffer of that many longwords. */
275 register int *buffer = (int *) alloca (count * sizeof (int));
276
277 if (write)
278 {
279 /* Fill start and end extra bytes of buffer with existing memory data. */
280
281 if (addr != memaddr || len < (int)sizeof (int)) {
282 /* Need part of initial word -- fetch it. */
283 buffer[0] = ptrace (PT_RIUSER, inferior_pid,
284 (PTRACE_ARG3_TYPE) addr, 0, 0);
285 }
286
287 if (count > 1) /* FIXME, avoid if even boundary */
288 {
289 buffer[count - 1]
290 = ptrace (PT_RIUSER, inferior_pid,
291 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
292 0, 0);
293 }
294
295 /* Copy data to be written over corresponding part of buffer */
296
297 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
298
299 /* Write the entire buffer. */
300
301 for (i = 0; i < count; i++, addr += sizeof (int))
302 {
303 #if 0
304 /* The HP-UX kernel crashes if you use PT_WDUSER to write into the text
305 segment. FIXME -- does it work to write into the data segment using
306 WIUSER, or do these idiots really expect us to figure out which segment
307 the address is in, so we can use a separate system call for it??! */
308 errno = 0;
309 ptrace (PT_WDUSER, inferior_pid, (PTRACE_ARG3_TYPE) addr,
310 buffer[i], 0);
311 if (errno)
312 #endif
313 {
314 /* Using the appropriate one (I or D) is necessary for
315 Gould NP1, at least. */
316 errno = 0;
317 ptrace (PT_WIUSER, inferior_pid, (PTRACE_ARG3_TYPE) addr,
318 buffer[i], 0);
319 }
320 if (errno)
321 return 0;
322 }
323 }
324 else
325 {
326 /* Read all the longwords */
327 for (i = 0; i < count; i++, addr += sizeof (int))
328 {
329 errno = 0;
330 buffer[i] = ptrace (PT_RIUSER, inferior_pid,
331 (PTRACE_ARG3_TYPE) addr, 0, 0);
332 if (errno)
333 return 0;
334 QUIT;
335 }
336
337 /* Copy appropriate bytes out of the buffer. */
338 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
339 }
340 return len;
341 }
342
This page took 0.05612 seconds and 4 git commands to generate.