Host/target/native split for sun4.
[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
c9c23412
RP
25#include "nat.h"
26
bd5635a1
RP
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>
8ffd75c8 35#ifndef USG
a0f9783e
SG
36#ifdef PTRACE_IN_WRONG_PLACE
37#include <ptrace.h>
38#else
bd5635a1 39#include <sys/ptrace.h>
8ffd75c8 40#endif
a0f9783e 41#endif
8ffd75c8 42
bd5635a1
RP
43#if !defined (PT_KILL)
44#define PT_KILL 8
45#define PT_STEP 9
46#define PT_CONTINUE 7
47#define PT_READ_U 3
48#define PT_WRITE_U 6
49#define PT_READ_I 1
8ffd75c8 50#define PT_READ_D 2
bd5635a1 51#define PT_WRITE_I 4
8ffd75c8 52#define PT_WRITE_D 5
bd5635a1
RP
53#endif /* No PT_KILL. */
54
55#ifndef PT_ATTACH
56#define PT_ATTACH PTRACE_ATTACH
57#endif
58#ifndef PT_DETACH
59#define PT_DETACH PTRACE_DETACH
60#endif
61
62#include "gdbcore.h"
ee0613d1 63#ifndef NO_SYS_FILE
bd5635a1 64#include <sys/file.h>
ee0613d1 65#endif
bd5635a1 66#include <sys/stat.h>
44ff4c96
JG
67
68#if !defined (FETCH_INFERIOR_REGISTERS)
69#include <sys/user.h> /* Probably need to poke the user structure */
70#if defined (KERNEL_U_ADDR_BSD)
71#include <a.out.h> /* For struct nlist */
72#endif /* KERNEL_U_ADDR_BSD. */
73#endif /* !FETCH_INFERIOR_REGISTERS */
e676a15f 74
bd5635a1
RP
75\f
76/* This function simply calls ptrace with the given arguments.
77 It exists so that all calls to ptrace are isolated in this
78 machine-dependent file. */
79int
80call_ptrace (request, pid, addr, data)
e676a15f
FF
81 int request, pid;
82 PTRACE_ARG3_TYPE addr;
83 int data;
bd5635a1
RP
84{
85 return ptrace (request, pid, addr, data);
86}
87
88#ifdef DEBUG_PTRACE
89/* For the rest of the file, use an extra level of indirection */
90/* This lets us breakpoint usefully on call_ptrace. */
91#define ptrace call_ptrace
92#endif
93
bd5635a1 94void
c9c23412 95kill_inferior ()
bd5635a1
RP
96{
97 if (inferior_pid == 0)
98 return;
c9c23412
RP
99 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
100 a real signal first, if we can. */
101 kill (inferior_pid, SIGKILL);
e676a15f 102 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
bd5635a1 103 wait ((int *)0);
bd5635a1
RP
104 target_mourn_inferior ();
105}
106
107/* Resume execution of the inferior process.
108 If STEP is nonzero, single-step it.
109 If SIGNAL is nonzero, give it that signal. */
110
111void
112child_resume (step, signal)
113 int step;
114 int signal;
115{
116 errno = 0;
d11c44f1 117
e676a15f
FF
118 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
119 it was. (If GDB wanted it to start some other way, we have already
997cc2c0
JG
120 written a new PC value to the child.)
121
122 If this system does not support PT_STEP, a higher level function will
123 have called single_step() to transmute the step request into a
124 continue request (by setting breakpoints on all possible successor
125 instructions), so we don't have to worry about that here. */
d11c44f1 126
bd5635a1 127 if (step)
6bb40269 128 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
bd5635a1 129 else
e676a15f 130 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
d11c44f1 131
bd5635a1
RP
132 if (errno)
133 perror_with_name ("ptrace");
134}
135\f
136#ifdef ATTACH_DETACH
137/* Nonzero if we are debugging an attached process rather than
138 an inferior. */
139extern int attach_flag;
140
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.111115 seconds and 4 git commands to generate.