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