v850 files that weren't being removed if !keep-v850
[deliverable/binutils-gdb.git] / gdb / infptrace.c
CommitLineData
bd5635a1 1/* Low level Unix child interface to ptrace, for GDB when running under Unix.
7531f36e 2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 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 17along with this program; if not, write to the Free Software
7531f36e 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1
RP
21#include "frame.h"
22#include "inferior.h"
23#include "target.h"
2b576293 24#include "gdb_string.h"
b52cac6b
FF
25#include "wait.h"
26#include "command.h"
bd5635a1
RP
27
28#ifdef USG
29#include <sys/types.h>
30#endif
31
32#include <sys/param.h>
33#include <sys/dir.h>
34#include <signal.h>
35#include <sys/ioctl.h>
ef6f3a8b 36
0626f40d 37#ifndef NO_PTRACE_H
a0f9783e
SG
38#ifdef PTRACE_IN_WRONG_PLACE
39#include <ptrace.h>
40#else
bd5635a1 41#include <sys/ptrace.h>
8ffd75c8 42#endif
0626f40d 43#endif /* NO_PTRACE_H */
8ffd75c8 44
7531f36e
FF
45#if !defined (PT_READ_I)
46#define PT_READ_I 1 /* Read word from text space */
47#endif
48#if !defined (PT_READ_D)
49#define PT_READ_D 2 /* Read word from data space */
50#endif
51#if !defined (PT_READ_U)
52#define PT_READ_U 3 /* Read word from kernel user struct */
53#endif
54#if !defined (PT_WRITE_I)
55#define PT_WRITE_I 4 /* Write word to text space */
56#endif
57#if !defined (PT_WRITE_D)
58#define PT_WRITE_D 5 /* Write word to data space */
59#endif
60#if !defined (PT_WRITE_U)
61#define PT_WRITE_U 6 /* Write word to kernel user struct */
62#endif
63#if !defined (PT_CONTINUE)
64#define PT_CONTINUE 7 /* Continue after signal */
5090e82c 65#endif
5090e82c 66#if !defined (PT_STEP)
7531f36e
FF
67#define PT_STEP 9 /* Set flag for single stepping */
68#endif
69#if !defined (PT_KILL)
70#define PT_KILL 8 /* Send child a SIGKILL signal */
71#endif
bd5635a1
RP
72
73#ifndef PT_ATTACH
74#define PT_ATTACH PTRACE_ATTACH
75#endif
76#ifndef PT_DETACH
77#define PT_DETACH PTRACE_DETACH
78#endif
79
80#include "gdbcore.h"
ee0613d1 81#ifndef NO_SYS_FILE
bd5635a1 82#include <sys/file.h>
ee0613d1 83#endif
5090e82c
SG
84#if 0
85/* Don't think this is used anymore. On the sequent (not sure whether it's
86 dynix or ptx or both), it is included unconditionally by sys/user.h and
87 not protected against multiple inclusion. */
2b576293 88#include "gdb_stat.h"
0626f40d
JK
89#endif
90
44ff4c96
JG
91#if !defined (FETCH_INFERIOR_REGISTERS)
92#include <sys/user.h> /* Probably need to poke the user structure */
93#if defined (KERNEL_U_ADDR_BSD)
94#include <a.out.h> /* For struct nlist */
95#endif /* KERNEL_U_ADDR_BSD. */
96#endif /* !FETCH_INFERIOR_REGISTERS */
e676a15f 97
bd5635a1
RP
98\f
99/* This function simply calls ptrace with the given arguments.
100 It exists so that all calls to ptrace are isolated in this
101 machine-dependent file. */
102int
103call_ptrace (request, pid, addr, data)
e676a15f
FF
104 int request, pid;
105 PTRACE_ARG3_TYPE addr;
106 int data;
bd5635a1 107{
5090e82c
SG
108 return ptrace (request, pid, addr, data
109#if defined (FIVE_ARG_PTRACE)
110 /* Deal with HPUX 8.0 braindamage. We never use the
111 calls which require the fifth argument. */
112 , 0
113#endif
114 );
bd5635a1
RP
115}
116
5090e82c 117#if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
bd5635a1
RP
118/* For the rest of the file, use an extra level of indirection */
119/* This lets us breakpoint usefully on call_ptrace. */
120#define ptrace call_ptrace
121#endif
122
bd5635a1 123void
c9c23412 124kill_inferior ()
bd5635a1
RP
125{
126 if (inferior_pid == 0)
127 return;
c9c23412 128 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
2b576293
C
129 a real signal first, if we can. FIXME: This is bogus. When the inferior
130 is not stopped, GDB should just be waiting for it. Either the following
131 line is unecessary, or there is some problem elsewhere in GDB which
132 causes us to get here when the inferior is not stopped. */
c9c23412 133 kill (inferior_pid, SIGKILL);
e676a15f 134 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
bd5635a1 135 wait ((int *)0);
bd5635a1
RP
136 target_mourn_inferior ();
137}
138
2b576293
C
139#ifndef CHILD_RESUME
140
bd5635a1
RP
141/* Resume execution of the inferior process.
142 If STEP is nonzero, single-step it.
143 If SIGNAL is nonzero, give it that signal. */
144
145void
5090e82c
SG
146child_resume (pid, step, signal)
147 int pid;
bd5635a1 148 int step;
918fea3e 149 enum target_signal signal;
bd5635a1
RP
150{
151 errno = 0;
d11c44f1 152
5090e82c 153 if (pid == -1)
918fea3e 154 /* Resume all threads. */
918fea3e
JL
155 /* I think this only gets used in the non-threaded case, where "resume
156 all threads" and "resume inferior_pid" are the same. */
5090e82c 157 pid = inferior_pid;
5090e82c 158
e676a15f
FF
159 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
160 it was. (If GDB wanted it to start some other way, we have already
997cc2c0
JG
161 written a new PC value to the child.)
162
163 If this system does not support PT_STEP, a higher level function will
164 have called single_step() to transmute the step request into a
165 continue request (by setting breakpoints on all possible successor
166 instructions), so we don't have to worry about that here. */
d11c44f1 167
bd5635a1 168 if (step)
918fea3e
JL
169 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
170 target_signal_to_host (signal));
bd5635a1 171 else
918fea3e
JL
172 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
173 target_signal_to_host (signal));
d11c44f1 174
bd5635a1
RP
175 if (errno)
176 perror_with_name ("ptrace");
177}
2b576293
C
178#endif /* CHILD_RESUME */
179
bd5635a1
RP
180\f
181#ifdef ATTACH_DETACH
bd5635a1
RP
182/* Start debugging the process whose number is PID. */
183int
184attach (pid)
185 int pid;
186{
187 errno = 0;
e676a15f 188 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
bd5635a1
RP
189 if (errno)
190 perror_with_name ("ptrace");
191 attach_flag = 1;
192 return pid;
193}
194
195/* Stop debugging the process whose number is PID
196 and continue it with signal number SIGNAL.
197 SIGNAL = 0 means just continue it. */
198
199void
200detach (signal)
201 int signal;
202{
203 errno = 0;
e676a15f 204 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
bd5635a1
RP
205 if (errno)
206 perror_with_name ("ptrace");
207 attach_flag = 0;
208}
209#endif /* ATTACH_DETACH */
210\f
5090e82c
SG
211/* Default the type of the ptrace transfer to int. */
212#ifndef PTRACE_XFER_TYPE
213#define PTRACE_XFER_TYPE int
214#endif
bd5635a1
RP
215
216/* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
217 to get the offset in the core file of the register values. */
5090e82c 218#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
bd5635a1
RP
219/* Get kernel_u_addr using BSD-style nlist(). */
220CORE_ADDR kernel_u_addr;
5090e82c 221#endif /* KERNEL_U_ADDR_BSD. */
bd5635a1
RP
222
223void
224_initialize_kernel_u_addr ()
225{
5090e82c 226#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
bd5635a1
RP
227 struct nlist names[2];
228
229 names[0].n_un.n_name = "_u";
230 names[1].n_un.n_name = NULL;
231 if (nlist ("/vmunix", names) == 0)
232 kernel_u_addr = names[0].n_value;
233 else
234 fatal ("Unable to get kernel u area address.");
bd5635a1 235#endif /* KERNEL_U_ADDR_BSD. */
bd5635a1 236}
5090e82c
SG
237
238#if !defined (FETCH_INFERIOR_REGISTERS)
bd5635a1
RP
239
240#if !defined (offsetof)
241#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
242#endif
243
244/* U_REGS_OFFSET is the offset of the registers within the u area. */
245#if !defined (U_REGS_OFFSET)
246#define U_REGS_OFFSET \
247 ptrace (PT_READ_U, inferior_pid, \
e676a15f
FF
248 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
249 - KERNEL_U_ADDR
bd5635a1
RP
250#endif
251
44ff4c96
JG
252/* Registers we shouldn't try to fetch. */
253#if !defined (CANNOT_FETCH_REGISTER)
254#define CANNOT_FETCH_REGISTER(regno) 0
255#endif
256
bd5635a1 257/* Fetch one register. */
44ff4c96 258
bd5635a1
RP
259static void
260fetch_register (regno)
261 int regno;
262{
2b576293
C
263 /* This isn't really an address. But ptrace thinks of it as one. */
264 CORE_ADDR regaddr;
bd5635a1 265 char buf[MAX_REGISTER_RAW_SIZE];
44ff4c96 266 char mess[128]; /* For messages */
bd5635a1
RP
267 register int i;
268
269 /* Offset of registers within the u area. */
44ff4c96
JG
270 unsigned int offset;
271
272 if (CANNOT_FETCH_REGISTER (regno))
273 {
5090e82c 274 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
44ff4c96
JG
275 supply_register (regno, buf);
276 return;
277 }
278
279 offset = U_REGS_OFFSET;
bd5635a1
RP
280
281 regaddr = register_addr (regno, offset);
5090e82c 282 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
bd5635a1 283 {
44ff4c96 284 errno = 0;
5090e82c
SG
285 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
286 (PTRACE_ARG3_TYPE) regaddr, 0);
287 regaddr += sizeof (PTRACE_XFER_TYPE);
44ff4c96
JG
288 if (errno != 0)
289 {
290 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
291 perror_with_name (mess);
292 }
bd5635a1
RP
293 }
294 supply_register (regno, buf);
295}
296
44ff4c96 297
5594d534 298/* Fetch all registers, or just one, from the child process. */
bd5635a1 299
5594d534 300void
bd5635a1
RP
301fetch_inferior_registers (regno)
302 int regno;
303{
2b576293
C
304 int numregs;
305
bd5635a1 306 if (regno == -1)
2b576293
C
307 {
308 numregs = ARCH_NUM_REGS;
309 for (regno = 0; regno < numregs; regno++)
310 fetch_register (regno);
311 }
bd5635a1
RP
312 else
313 fetch_register (regno);
bd5635a1
RP
314}
315
316/* Registers we shouldn't try to store. */
317#if !defined (CANNOT_STORE_REGISTER)
318#define CANNOT_STORE_REGISTER(regno) 0
319#endif
320
321/* Store our register values back into the inferior.
322 If REGNO is -1, do this for all registers.
323 Otherwise, REGNO specifies which register (so we can save time). */
324
e676a15f 325void
bd5635a1
RP
326store_inferior_registers (regno)
327 int regno;
328{
2b576293
C
329 /* This isn't really an address. But ptrace thinks of it as one. */
330 CORE_ADDR regaddr;
bd5635a1 331 char buf[80];
2b576293 332 register int i, numregs;
bd5635a1
RP
333
334 unsigned int offset = U_REGS_OFFSET;
335
336 if (regno >= 0)
337 {
338 regaddr = register_addr (regno, offset);
5090e82c 339 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
bd5635a1
RP
340 {
341 errno = 0;
e676a15f 342 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
5090e82c 343 *(PTRACE_XFER_TYPE *) &registers[REGISTER_BYTE (regno) + i]);
bd5635a1
RP
344 if (errno != 0)
345 {
346 sprintf (buf, "writing register number %d(%d)", regno, i);
347 perror_with_name (buf);
bd5635a1 348 }
5090e82c 349 regaddr += sizeof(PTRACE_XFER_TYPE);
bd5635a1
RP
350 }
351 }
352 else
353 {
2b576293
C
354 numregs = ARCH_NUM_REGS;
355 for (regno = 0; regno < numregs; regno++)
bd5635a1
RP
356 {
357 if (CANNOT_STORE_REGISTER (regno))
358 continue;
359 regaddr = register_addr (regno, offset);
5090e82c 360 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
bd5635a1
RP
361 {
362 errno = 0;
e676a15f 363 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
5090e82c 364 *(PTRACE_XFER_TYPE *) &registers[REGISTER_BYTE (regno) + i]);
bd5635a1
RP
365 if (errno != 0)
366 {
367 sprintf (buf, "writing register number %d(%d)", regno, i);
368 perror_with_name (buf);
bd5635a1 369 }
5090e82c 370 regaddr += sizeof(PTRACE_XFER_TYPE);
bd5635a1
RP
371 }
372 }
373 }
bd5635a1
RP
374}
375#endif /* !defined (FETCH_INFERIOR_REGISTERS). */
376\f
918fea3e
JL
377
378#if !defined (CHILD_XFER_MEMORY)
bd5635a1
RP
379/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
380 in the NEW_SUN_PTRACE case.
381 It ought to be straightforward. But it appears that writing did
382 not write the data that I specified. I cannot understand where
383 it got the data that it actually did write. */
384
385/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
386 to debugger memory starting at MYADDR. Copy to inferior if
387 WRITE is nonzero.
388
389 Returns the length copied, which is either the LEN argument or zero.
390 This xfer function does not do partial moves, since child_ops
391 doesn't allow memory operations to cross below us in the target stack
392 anyway. */
393
394int
b6de2014 395child_xfer_memory (memaddr, myaddr, len, write, target)
bd5635a1
RP
396 CORE_ADDR memaddr;
397 char *myaddr;
398 int len;
399 int write;
ee0613d1 400 struct target_ops *target; /* ignored */
bd5635a1
RP
401{
402 register int i;
403 /* Round starting address down to longword boundary. */
5090e82c 404 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
bd5635a1
RP
405 /* Round ending address up; get number of longwords that makes. */
406 register int count
5090e82c
SG
407 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
408 / sizeof (PTRACE_XFER_TYPE);
bd5635a1 409 /* Allocate buffer of that many longwords. */
5090e82c
SG
410 register PTRACE_XFER_TYPE *buffer
411 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
bd5635a1
RP
412
413 if (write)
414 {
415 /* Fill start and end extra bytes of buffer with existing memory data. */
416
5090e82c 417 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
bd5635a1 418 /* Need part of initial word -- fetch it. */
e676a15f
FF
419 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
420 0);
bd5635a1
RP
421 }
422
423 if (count > 1) /* FIXME, avoid if even boundary */
424 {
425 buffer[count - 1]
426 = ptrace (PT_READ_I, inferior_pid,
5090e82c
SG
427 ((PTRACE_ARG3_TYPE)
428 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
e676a15f 429 0);
bd5635a1
RP
430 }
431
432 /* Copy data to be written over corresponding part of buffer */
433
5090e82c
SG
434 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
435 myaddr,
436 len);
bd5635a1
RP
437
438 /* Write the entire buffer. */
439
5090e82c 440 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
bd5635a1
RP
441 {
442 errno = 0;
e676a15f
FF
443 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
444 buffer[i]);
bd5635a1
RP
445 if (errno)
446 {
447 /* Using the appropriate one (I or D) is necessary for
448 Gould NP1, at least. */
449 errno = 0;
e676a15f
FF
450 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
451 buffer[i]);
bd5635a1
RP
452 }
453 if (errno)
454 return 0;
455 }
456 }
457 else
458 {
459 /* Read all the longwords */
5090e82c 460 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
bd5635a1
RP
461 {
462 errno = 0;
e676a15f
FF
463 buffer[i] = ptrace (PT_READ_I, inferior_pid,
464 (PTRACE_ARG3_TYPE) addr, 0);
bd5635a1
RP
465 if (errno)
466 return 0;
467 QUIT;
468 }
469
470 /* Copy appropriate bytes out of the buffer. */
5090e82c
SG
471 memcpy (myaddr,
472 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
473 len);
bd5635a1
RP
474 }
475 return len;
476}
7531f36e
FF
477
478\f
479static void
480udot_info ()
481{
482 int udot_off; /* Offset into user struct */
483 int udot_val; /* Value from user struct at udot_off */
484 char mess[128]; /* For messages */
485
486 if (!target_has_execution)
487 {
488 error ("The program is not being run.");
489 }
490
491#if !defined (KERNEL_U_SIZE)
492
493 /* Adding support for this command is easy. Typically you just add a
494 routine, called "kernel_u_size" that returns the size of the user
495 struct, to the appropriate *-nat.c file and then add to the native
496 config file "#define KERNEL_U_SIZE kernel_u_size()" */
497 error ("Don't know how large ``struct user'' is in this version of gdb.");
498
499#else
500
501 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
502 {
503 if ((udot_off % 24) == 0)
504 {
505 if (udot_off > 0)
506 {
507 printf_filtered ("\n");
508 }
509 printf_filtered ("%04x:", udot_off);
510 }
511 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
512 if (errno != 0)
513 {
514 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
515 perror_with_name (mess);
516 }
517 /* Avoid using nonportable (?) "*" in print specs */
518 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
519 }
520 printf_filtered ("\n");
521
522#endif
523}
b52cac6b
FF
524#endif /* !defined (CHILD_XFER_MEMORY). */
525
7531f36e
FF
526\f
527void
528_initialize_infptrace ()
529{
b52cac6b 530#if !defined (CHILD_XFER_MEMORY)
7531f36e
FF
531 add_info ("udot", udot_info,
532 "Print contents of kernel ``struct user'' for current child.");
b52cac6b 533#endif
7531f36e 534}
This page took 0.325053 seconds and 4 git commands to generate.