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