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