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