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