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