Initial revision
[deliverable/binutils-gdb.git] / gdb / infptrace.c
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
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
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>
35 #include <sys/ptrace.h>
36 #if !defined (PT_KILL)
37 #define PT_KILL 8
38 #define PT_STEP 9
39 #define PT_CONTINUE 7
40 #define PT_READ_U 3
41 #define PT_WRITE_U 6
42 #define PT_READ_I 1
43 #define PT_WRITE_I 4
44 /* The Following Change is for a Sun */
45 #define PT_WRITE_D 4
46 #endif /* No PT_KILL. */
47
48 #ifndef PT_ATTACH
49 #define PT_ATTACH PTRACE_ATTACH
50 #endif
51 #ifndef PT_DETACH
52 #define PT_DETACH PTRACE_DETACH
53 #endif
54
55 #include "gdbcore.h"
56 #include <sys/user.h> /* After a.out.h */
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 \f
60 /* This function simply calls ptrace with the given arguments.
61 It exists so that all calls to ptrace are isolated in this
62 machine-dependent file. */
63 int
64 call_ptrace (request, pid, addr, data)
65 int request, pid, *addr, data;
66 {
67 return ptrace (request, pid, addr, data);
68 }
69
70 #ifdef DEBUG_PTRACE
71 /* For the rest of the file, use an extra level of indirection */
72 /* This lets us breakpoint usefully on call_ptrace. */
73 #define ptrace call_ptrace
74 #endif
75
76 /* This is used when GDB is exiting. It gives less chance of error.*/
77
78 void
79 kill_inferior_fast ()
80 {
81 if (inferior_pid == 0)
82 return;
83 ptrace (PT_KILL, inferior_pid, 0, 0);
84 wait ((int *)0);
85 }
86
87 void
88 kill_inferior (args, from_tty)
89 char *args;
90 int from_tty;
91 {
92 kill_inferior_fast ();
93 target_mourn_inferior ();
94 }
95
96 /* Resume execution of the inferior process.
97 If STEP is nonzero, single-step it.
98 If SIGNAL is nonzero, give it that signal. */
99
100 void
101 child_resume (step, signal)
102 int step;
103 int signal;
104 {
105 errno = 0;
106 /* An address of (int *)1 tells it to continue from where it was.
107 (If GDB wanted it to start some other way, we have already written
108 a new PC value to the child.) */
109 if (step)
110 {
111 #if defined (NO_SINGLE_STEP)
112 single_step (signal);
113 #else /* Have single step. */
114 ptrace (PT_STEP, inferior_pid, (int *)1, signal);
115 #endif /* Have single step. */
116 }
117 else
118 ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
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. */
126 extern int attach_flag;
127
128 /* Start debugging the process whose number is PID. */
129 int
130 attach (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
145 void
146 detach (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(). */
163 CORE_ADDR kernel_u_addr;
164
165 void
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(). */
181 CORE_ADDR kernel_u_addr;
182
183 struct 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 };
191 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
192
193 /* read the value of the u area from the hp-ux kernel */
194 void _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. */
214 static void
215 fetch_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
234 /* Fetch all registers, or just one, from the child process.
235 We should check for errors, but we don't. FIXME. */
236
237 int
238 fetch_inferior_registers (regno)
239 int regno;
240 {
241 if (regno == -1)
242 for (regno = 0; regno < NUM_REGS; regno++)
243 fetch_register (regno);
244 else
245 fetch_register (regno);
246 return 0;
247 }
248
249 /* Registers we shouldn't try to store. */
250 #if !defined (CANNOT_STORE_REGISTER)
251 #define CANNOT_STORE_REGISTER(regno) 0
252 #endif
253
254 /* Store our register values back into the inferior.
255 If REGNO is -1, do this for all registers.
256 Otherwise, REGNO specifies which register (so we can save time). */
257
258 int
259 store_inferior_registers (regno)
260 int regno;
261 {
262 register unsigned int regaddr;
263 char buf[80];
264 extern char registers[];
265 register int i;
266 int result = 0;
267
268 unsigned int offset = U_REGS_OFFSET;
269
270 if (regno >= 0)
271 {
272 regaddr = register_addr (regno, offset);
273 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
274 {
275 errno = 0;
276 ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
277 *(int *) &registers[REGISTER_BYTE (regno) + i]);
278 if (errno != 0)
279 {
280 sprintf (buf, "writing register number %d(%d)", regno, i);
281 perror_with_name (buf);
282 result = -1;
283 }
284 regaddr += sizeof(int);
285 }
286 }
287 else
288 {
289 for (regno = 0; regno < NUM_REGS; regno++)
290 {
291 if (CANNOT_STORE_REGISTER (regno))
292 continue;
293 regaddr = register_addr (regno, offset);
294 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
295 {
296 errno = 0;
297 ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
298 *(int *) &registers[REGISTER_BYTE (regno) + i]);
299 if (errno != 0)
300 {
301 sprintf (buf, "writing register number %d(%d)", regno, i);
302 perror_with_name (buf);
303 result = -1;
304 }
305 regaddr += sizeof(int);
306 }
307 }
308 }
309 return result;
310 }
311 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
312 \f
313 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
314 in the NEW_SUN_PTRACE case.
315 It ought to be straightforward. But it appears that writing did
316 not write the data that I specified. I cannot understand where
317 it got the data that it actually did write. */
318
319 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
320 to debugger memory starting at MYADDR. Copy to inferior if
321 WRITE is nonzero.
322
323 Returns the length copied, which is either the LEN argument or zero.
324 This xfer function does not do partial moves, since child_ops
325 doesn't allow memory operations to cross below us in the target stack
326 anyway. */
327
328 int
329 child_xfer_memory (memaddr, myaddr, len, write)
330 CORE_ADDR memaddr;
331 char *myaddr;
332 int len;
333 int write;
334 {
335 register int i;
336 /* Round starting address down to longword boundary. */
337 register CORE_ADDR addr = memaddr & - sizeof (int);
338 /* Round ending address up; get number of longwords that makes. */
339 register int count
340 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
341 /* Allocate buffer of that many longwords. */
342 register int *buffer = (int *) alloca (count * sizeof (int));
343
344 if (write)
345 {
346 /* Fill start and end extra bytes of buffer with existing memory data. */
347
348 if (addr != memaddr || len < (int)sizeof (int)) {
349 /* Need part of initial word -- fetch it. */
350 buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
351 }
352
353 if (count > 1) /* FIXME, avoid if even boundary */
354 {
355 buffer[count - 1]
356 = ptrace (PT_READ_I, inferior_pid,
357 (int *)(addr + (count - 1) * sizeof (int)), 0);
358 }
359
360 /* Copy data to be written over corresponding part of buffer */
361
362 bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
363
364 /* Write the entire buffer. */
365
366 for (i = 0; i < count; i++, addr += sizeof (int))
367 {
368 errno = 0;
369 ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
370 if (errno)
371 {
372 /* Using the appropriate one (I or D) is necessary for
373 Gould NP1, at least. */
374 errno = 0;
375 ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
376 }
377 if (errno)
378 return 0;
379 }
380 }
381 else
382 {
383 /* Read all the longwords */
384 for (i = 0; i < count; i++, addr += sizeof (int))
385 {
386 errno = 0;
387 buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
388 if (errno)
389 return 0;
390 QUIT;
391 }
392
393 /* Copy appropriate bytes out of the buffer. */
394 bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
395 }
396 return len;
397 }
This page took 0.037625 seconds and 4 git commands to generate.