3856b3694386d8adbc1fe75406ade36ec646d169
[binutils-gdb.git] / gdb / infptrace.c
1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10
11 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
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>
33 #ifndef USG
34 #ifdef PTRACE_IN_WRONG_PLACE
35 #include <ptrace.h>
36 #else
37 #include <sys/ptrace.h>
38 #endif
39 #endif
40
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
48 #define PT_READ_D 2
49 #define PT_WRITE_I 4
50 #define PT_WRITE_D 5
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"
61 #ifndef NO_SYS_FILE
62 #include <sys/file.h>
63 #endif
64 #include <sys/stat.h>
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 */
72
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. */
77 int
78 call_ptrace (request, pid, addr, data)
79 int request, pid;
80 PTRACE_ARG3_TYPE addr;
81 int data;
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
94 void
95 kill_inferior_fast ()
96 {
97 if (inferior_pid == 0)
98 return;
99 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
100 wait ((int *)0);
101 }
102
103 void
104 kill_inferior ()
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
114 void
115 child_resume (step, signal)
116 int step;
117 int signal;
118 {
119 errno = 0;
120
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
123 written a new PC value to the child.) */
124
125 if (step)
126 #ifdef NO_SINGLE_STEP
127 single_step (signal);
128 #else
129 ptrace (PT_STEP, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
130 #endif
131 else
132 #ifdef AIX_BUGGY_PTRACE_CONTINUE
133 AIX_BUGGY_PTRACE_CONTINUE;
134 #else
135 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
136 #endif
137
138 if (errno)
139 perror_with_name ("ptrace");
140 }
141 \f
142 #ifdef ATTACH_DETACH
143 /* Nonzero if we are debugging an attached process rather than
144 an inferior. */
145 extern int attach_flag;
146
147 /* Start debugging the process whose number is PID. */
148 int
149 attach (pid)
150 int pid;
151 {
152 errno = 0;
153 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
154 if (errno)
155 perror_with_name ("ptrace");
156 attach_flag = 1;
157 return pid;
158 }
159
160 /* Stop debugging the process whose number is PID
161 and continue it with signal number SIGNAL.
162 SIGNAL = 0 means just continue it. */
163
164 void
165 detach (signal)
166 int signal;
167 {
168 errno = 0;
169 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
170 if (errno)
171 perror_with_name ("ptrace");
172 attach_flag = 0;
173 }
174 #endif /* ATTACH_DETACH */
175 \f
176 #if !defined (FETCH_INFERIOR_REGISTERS)
177
178 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
179 to get the offset in the core file of the register values. */
180 #if defined (KERNEL_U_ADDR_BSD)
181 /* Get kernel_u_addr using BSD-style nlist(). */
182 CORE_ADDR kernel_u_addr;
183
184 void
185 _initialize_kernel_u_addr ()
186 {
187 struct nlist names[2];
188
189 names[0].n_un.n_name = "_u";
190 names[1].n_un.n_name = NULL;
191 if (nlist ("/vmunix", names) == 0)
192 kernel_u_addr = names[0].n_value;
193 else
194 fatal ("Unable to get kernel u area address.");
195 }
196 #endif /* KERNEL_U_ADDR_BSD. */
197
198 #if defined (KERNEL_U_ADDR_HPUX)
199 /* Get kernel_u_addr using HPUX-style nlist(). */
200 CORE_ADDR kernel_u_addr;
201
202 struct hpnlist {
203 char * n_name;
204 long n_value;
205 unsigned char n_type;
206 unsigned char n_length;
207 short n_almod;
208 short n_unused;
209 };
210 static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
211
212 /* read the value of the u area from the hp-ux kernel */
213 void _initialize_kernel_u_addr ()
214 {
215 nlist ("/hp-ux", &nl);
216 kernel_u_addr = nl[0].n_value;
217 }
218 #endif /* KERNEL_U_ADDR_HPUX. */
219
220 #if !defined (offsetof)
221 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
222 #endif
223
224 /* U_REGS_OFFSET is the offset of the registers within the u area. */
225 #if !defined (U_REGS_OFFSET)
226 #define U_REGS_OFFSET \
227 ptrace (PT_READ_U, inferior_pid, \
228 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
229 - KERNEL_U_ADDR
230 #endif
231
232 /* Registers we shouldn't try to fetch. */
233 #if !defined (CANNOT_FETCH_REGISTER)
234 #define CANNOT_FETCH_REGISTER(regno) 0
235 #endif
236
237 /* Fetch one register. */
238
239 static void
240 fetch_register (regno)
241 int regno;
242 {
243 register unsigned int regaddr;
244 char buf[MAX_REGISTER_RAW_SIZE];
245 char mess[128]; /* For messages */
246 register int i;
247
248 /* Offset of registers within the u area. */
249 unsigned int offset;
250
251 if (CANNOT_FETCH_REGISTER (regno))
252 {
253 bzero (buf, REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
254 supply_register (regno, buf);
255 return;
256 }
257
258 offset = U_REGS_OFFSET;
259
260 regaddr = register_addr (regno, offset);
261 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
262 {
263 errno = 0;
264 *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
265 (PTRACE_ARG3_TYPE) regaddr, 0);
266 regaddr += sizeof (int);
267 if (errno != 0)
268 {
269 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
270 perror_with_name (mess);
271 }
272 }
273 supply_register (regno, buf);
274 }
275
276
277 /* Fetch all registers, or just one, from the child process. */
278
279 void
280 fetch_inferior_registers (regno)
281 int regno;
282 {
283 if (regno == -1)
284 for (regno = 0; regno < NUM_REGS; regno++)
285 fetch_register (regno);
286 else
287 fetch_register (regno);
288 }
289
290 /* Registers we shouldn't try to store. */
291 #if !defined (CANNOT_STORE_REGISTER)
292 #define CANNOT_STORE_REGISTER(regno) 0
293 #endif
294
295 /* Store our register values back into the inferior.
296 If REGNO is -1, do this for all registers.
297 Otherwise, REGNO specifies which register (so we can save time). */
298
299 void
300 store_inferior_registers (regno)
301 int regno;
302 {
303 register unsigned int regaddr;
304 char buf[80];
305 extern char registers[];
306 register int i;
307
308 unsigned int offset = U_REGS_OFFSET;
309
310 if (regno >= 0)
311 {
312 regaddr = register_addr (regno, offset);
313 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
314 {
315 errno = 0;
316 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
317 *(int *) &registers[REGISTER_BYTE (regno) + i]);
318 if (errno != 0)
319 {
320 sprintf (buf, "writing register number %d(%d)", regno, i);
321 perror_with_name (buf);
322 }
323 regaddr += sizeof(int);
324 }
325 }
326 else
327 {
328 for (regno = 0; regno < NUM_REGS; regno++)
329 {
330 if (CANNOT_STORE_REGISTER (regno))
331 continue;
332 regaddr = register_addr (regno, offset);
333 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
334 {
335 errno = 0;
336 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
337 *(int *) &registers[REGISTER_BYTE (regno) + i]);
338 if (errno != 0)
339 {
340 sprintf (buf, "writing register number %d(%d)", regno, i);
341 perror_with_name (buf);
342 }
343 regaddr += sizeof(int);
344 }
345 }
346 }
347 }
348 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
349 \f
350 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
351 in the NEW_SUN_PTRACE case.
352 It ought to be straightforward. But it appears that writing did
353 not write the data that I specified. I cannot understand where
354 it got the data that it actually did write. */
355
356 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
357 to debugger memory starting at MYADDR. Copy to inferior if
358 WRITE is nonzero.
359
360 Returns the length copied, which is either the LEN argument or zero.
361 This xfer function does not do partial moves, since child_ops
362 doesn't allow memory operations to cross below us in the target stack
363 anyway. */
364
365 int
366 child_xfer_memory (memaddr, myaddr, len, write, target)
367 CORE_ADDR memaddr;
368 char *myaddr;
369 int len;
370 int write;
371 struct target_ops *target; /* ignored */
372 {
373 register int i;
374 /* Round starting address down to longword boundary. */
375 register CORE_ADDR addr = memaddr & - sizeof (int);
376 /* Round ending address up; get number of longwords that makes. */
377 register int count
378 = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
379 /* Allocate buffer of that many longwords. */
380 register int *buffer = (int *) alloca (count * sizeof (int));
381
382 if (write)
383 {
384 /* Fill start and end extra bytes of buffer with existing memory data. */
385
386 if (addr != memaddr || len < (int)sizeof (int)) {
387 /* Need part of initial word -- fetch it. */
388 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
389 0);
390 }
391
392 if (count > 1) /* FIXME, avoid if even boundary */
393 {
394 buffer[count - 1]
395 = ptrace (PT_READ_I, inferior_pid,
396 (PTRACE_ARG3_TYPE) (addr + (count - 1) * sizeof (int)),
397 0);
398 }
399
400 /* Copy data to be written over corresponding part of buffer */
401
402 memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
403
404 /* Write the entire buffer. */
405
406 for (i = 0; i < count; i++, addr += sizeof (int))
407 {
408 errno = 0;
409 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
410 buffer[i]);
411 if (errno)
412 {
413 /* Using the appropriate one (I or D) is necessary for
414 Gould NP1, at least. */
415 errno = 0;
416 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
417 buffer[i]);
418 }
419 if (errno)
420 return 0;
421 }
422 }
423 else
424 {
425 /* Read all the longwords */
426 for (i = 0; i < count; i++, addr += sizeof (int))
427 {
428 errno = 0;
429 buffer[i] = ptrace (PT_READ_I, inferior_pid,
430 (PTRACE_ARG3_TYPE) addr, 0);
431 if (errno)
432 return 0;
433 QUIT;
434 }
435
436 /* Copy appropriate bytes out of the buffer. */
437 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
438 }
439 return len;
440 }