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