]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/infptrace.c
Protoization.
[thirdparty/binutils-gdb.git] / gdb / infptrace.c
CommitLineData
c906108c
SS
1/* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "target.h"
26#include "gdb_string.h"
ed9a39eb 27
03f2053f 28#include "gdb_wait.h"
ed9a39eb 29
c906108c
SS
30#include "command.h"
31
32#ifdef USG
33#include <sys/types.h>
34#endif
35
36#include <sys/param.h>
4b14d3e4 37#include "gdb_dirent.h"
c906108c
SS
38#include <signal.h>
39#include <sys/ioctl.h>
40
41#ifdef HAVE_PTRACE_H
c5aa993b 42#include <ptrace.h>
c906108c 43#else
c5aa993b
JM
44#ifdef HAVE_SYS_PTRACE_H
45#include <sys/ptrace.h>
46#endif
c906108c
SS
47#endif
48
49#if !defined (PT_READ_I)
50#define PT_READ_I 1 /* Read word from text space */
51#endif
52#if !defined (PT_READ_D)
53#define PT_READ_D 2 /* Read word from data space */
54#endif
55#if !defined (PT_READ_U)
56#define PT_READ_U 3 /* Read word from kernel user struct */
57#endif
58#if !defined (PT_WRITE_I)
59#define PT_WRITE_I 4 /* Write word to text space */
60#endif
61#if !defined (PT_WRITE_D)
62#define PT_WRITE_D 5 /* Write word to data space */
63#endif
64#if !defined (PT_WRITE_U)
65#define PT_WRITE_U 6 /* Write word to kernel user struct */
66#endif
67#if !defined (PT_CONTINUE)
68#define PT_CONTINUE 7 /* Continue after signal */
69#endif
70#if !defined (PT_STEP)
71#define PT_STEP 9 /* Set flag for single stepping */
72#endif
73#if !defined (PT_KILL)
74#define PT_KILL 8 /* Send child a SIGKILL signal */
75#endif
76
77#ifndef PT_ATTACH
78#define PT_ATTACH PTRACE_ATTACH
79#endif
80#ifndef PT_DETACH
81#define PT_DETACH PTRACE_DETACH
82#endif
83
84#include "gdbcore.h"
85#ifndef NO_SYS_FILE
86#include <sys/file.h>
87#endif
88#if 0
89/* Don't think this is used anymore. On the sequent (not sure whether it's
90 dynix or ptx or both), it is included unconditionally by sys/user.h and
91 not protected against multiple inclusion. */
92#include "gdb_stat.h"
93#endif
94
95#if !defined (FETCH_INFERIOR_REGISTERS)
96#include <sys/user.h> /* Probably need to poke the user structure */
97#if defined (KERNEL_U_ADDR_BSD)
98#include <a.out.h> /* For struct nlist */
99#endif /* KERNEL_U_ADDR_BSD. */
100#endif /* !FETCH_INFERIOR_REGISTERS */
101
102#if !defined (CHILD_XFER_MEMORY)
a14ed312 103static void udot_info (char *, int);
c906108c
SS
104#endif
105
106#if !defined (FETCH_INFERIOR_REGISTERS)
a14ed312
KB
107static void fetch_register (int);
108static void store_register (int);
c906108c
SS
109#endif
110
ed9a39eb
JM
111/*
112 * Some systems (Linux) may have threads implemented as pseudo-processes,
113 * in which case we may be tracing more than one process at a time.
114 * In that case, inferior_pid will contain the main process ID and the
115 * individual thread (process) id mashed together. These macros are
116 * used to separate them out. The definitions may be overridden in tm.h
117 *
118 * NOTE: default definitions here are for systems with no threads.
119 * Useful definitions MUST be provided in tm.h
120 */
121
122#if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
123#define PIDGET(PID) PID
124#define TIDGET(PID) 0
125#endif
126
a14ed312
KB
127void _initialize_kernel_u_addr (void);
128void _initialize_infptrace (void);
c906108c 129\f
c5aa993b 130
c906108c
SS
131/* This function simply calls ptrace with the given arguments.
132 It exists so that all calls to ptrace are isolated in this
133 machine-dependent file. */
134int
fba45db2 135call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
c906108c
SS
136{
137 int pt_status = 0;
138
139#if 0
140 int saved_errno;
141
142 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
143 request, pid, addr, data);
144#endif
145#if defined(PT_SETTRC)
146 /* If the parent can be told to attach to us, try to do it. */
c5aa993b
JM
147 if (request == PT_SETTRC)
148 {
149 errno = 0;
ed9a39eb
JM
150#if !defined (FIVE_ARG_PTRACE)
151 pt_status = ptrace (PT_SETTRC, pid, addr, data);
152#else
c5aa993b
JM
153 /* Deal with HPUX 8.0 braindamage. We never use the
154 calls which require the fifth argument. */
ed9a39eb 155 pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
c906108c 156#endif
c5aa993b
JM
157 if (errno)
158 perror_with_name ("ptrace");
c906108c 159#if 0
c5aa993b 160 printf (" = %d\n", pt_status);
c906108c 161#endif
c5aa993b
JM
162 if (pt_status < 0)
163 return pt_status;
164 else
165 return parent_attach_all (pid, addr, data);
166 }
c906108c
SS
167#endif
168
169#if defined(PT_CONTIN1)
170 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
171 signals. If it's available, use it. */
172 if (request == PT_CONTINUE)
173 request = PT_CONTIN1;
174#endif
175
176#if defined(PT_SINGLE1)
177 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
178 signals. If it's available, use it. */
179 if (request == PT_STEP)
180 request = PT_SINGLE1;
181#endif
182
183#if 0
184 saved_errno = errno;
185 errno = 0;
186#endif
ed9a39eb
JM
187#if !defined (FIVE_ARG_PTRACE)
188 pt_status = ptrace (request, pid, addr, data);
189#else
c5aa993b
JM
190 /* Deal with HPUX 8.0 braindamage. We never use the
191 calls which require the fifth argument. */
ed9a39eb 192 pt_status = ptrace (request, pid, addr, data, 0);
c906108c 193#endif
ed9a39eb 194
c906108c
SS
195#if 0
196 if (errno)
197 printf (" [errno = %d]", errno);
198
199 errno = saved_errno;
200 printf (" = 0x%x\n", pt_status);
201#endif
202 return pt_status;
203}
204
205
206#if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
207/* For the rest of the file, use an extra level of indirection */
208/* This lets us breakpoint usefully on call_ptrace. */
209#define ptrace call_ptrace
210#endif
211
212/* Wait for a process to finish, possibly running a target-specific
213 hook before returning. */
214
215int
fba45db2 216ptrace_wait (int pid, int *status)
c906108c
SS
217{
218 int wstate;
219
220 wstate = wait (status);
221 target_post_wait (wstate, *status);
222 return wstate;
223}
224
225void
fba45db2 226kill_inferior (void)
c906108c
SS
227{
228 int status;
229
230 if (inferior_pid == 0)
231 return;
232
233 /* This once used to call "kill" to kill the inferior just in case
234 the inferior was still running. As others have noted in the past
235 (kingdon) there shouldn't be any way to get here if the inferior
236 is still running -- else there's a major problem elsewere in gdb
237 and it needs to be fixed.
238
239 The kill call causes problems under hpux10, so it's been removed;
240 if this causes problems we'll deal with them as they arise. */
241 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
242 ptrace_wait (0, &status);
243 target_mourn_inferior ();
244}
245
246#ifndef CHILD_RESUME
247
248/* Resume execution of the inferior process.
249 If STEP is nonzero, single-step it.
250 If SIGNAL is nonzero, give it that signal. */
251
252void
fba45db2 253child_resume (int pid, int step, enum target_signal signal)
c906108c
SS
254{
255 errno = 0;
256
257 if (pid == -1)
258 /* Resume all threads. */
259 /* I think this only gets used in the non-threaded case, where "resume
260 all threads" and "resume inferior_pid" are the same. */
261 pid = inferior_pid;
262
263 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
264 it was. (If GDB wanted it to start some other way, we have already
265 written a new PC value to the child.)
266
267 If this system does not support PT_STEP, a higher level function will
268 have called single_step() to transmute the step request into a
269 continue request (by setting breakpoints on all possible successor
270 instructions), so we don't have to worry about that here. */
271
272 if (step)
273 {
274 if (SOFTWARE_SINGLE_STEP_P)
c5aa993b 275 abort (); /* Make sure this doesn't happen. */
c906108c 276 else
c5aa993b 277 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
c906108c
SS
278 target_signal_to_host (signal));
279 }
280 else
281 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
282 target_signal_to_host (signal));
283
284 if (errno)
ed9a39eb
JM
285 {
286 perror_with_name ("ptrace");
287 }
c906108c
SS
288}
289#endif /* CHILD_RESUME */
c906108c 290\f
c5aa993b 291
c906108c
SS
292#ifdef ATTACH_DETACH
293/* Start debugging the process whose number is PID. */
294int
fba45db2 295attach (int pid)
c906108c
SS
296{
297 errno = 0;
298 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
299 if (errno)
300 perror_with_name ("ptrace");
301 attach_flag = 1;
302 return pid;
303}
304
305/* Stop debugging the process whose number is PID
306 and continue it with signal number SIGNAL.
307 SIGNAL = 0 means just continue it. */
308
309void
fba45db2 310detach (int signal)
c906108c
SS
311{
312 errno = 0;
313 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
314 if (errno)
315 perror_with_name ("ptrace");
316 attach_flag = 0;
317}
318#endif /* ATTACH_DETACH */
319\f
320/* Default the type of the ptrace transfer to int. */
321#ifndef PTRACE_XFER_TYPE
322#define PTRACE_XFER_TYPE int
323#endif
324
325/* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
326 to get the offset in the core file of the register values. */
327#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
328/* Get kernel_u_addr using BSD-style nlist(). */
329CORE_ADDR kernel_u_addr;
330#endif /* KERNEL_U_ADDR_BSD. */
331
332void
fba45db2 333_initialize_kernel_u_addr (void)
c906108c
SS
334{
335#if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
336 struct nlist names[2];
337
338 names[0].n_un.n_name = "_u";
339 names[1].n_un.n_name = NULL;
340 if (nlist ("/vmunix", names) == 0)
341 kernel_u_addr = names[0].n_value;
342 else
96baa820 343 internal_error ("Unable to get kernel u area address.");
c906108c
SS
344#endif /* KERNEL_U_ADDR_BSD. */
345}
346
347#if !defined (FETCH_INFERIOR_REGISTERS)
348
349#if !defined (offsetof)
350#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
351#endif
352
353/* U_REGS_OFFSET is the offset of the registers within the u area. */
354#if !defined (U_REGS_OFFSET)
355#define U_REGS_OFFSET \
356 ptrace (PT_READ_U, inferior_pid, \
357 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
358 - KERNEL_U_ADDR
359#endif
360
361/* Registers we shouldn't try to fetch. */
362#if !defined (CANNOT_FETCH_REGISTER)
363#define CANNOT_FETCH_REGISTER(regno) 0
364#endif
365
366/* Fetch one register. */
367
368static void
fba45db2 369fetch_register (int regno)
c906108c
SS
370{
371 /* This isn't really an address. But ptrace thinks of it as one. */
372 CORE_ADDR regaddr;
c5aa993b 373 char mess[128]; /* For messages */
c906108c 374 register int i;
c5aa993b 375 unsigned int offset; /* Offset of registers within the u area. */
c906108c 376 char buf[MAX_REGISTER_RAW_SIZE];
ed9a39eb 377 int tid;
c906108c
SS
378
379 if (CANNOT_FETCH_REGISTER (regno))
380 {
381 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
382 supply_register (regno, buf);
383 return;
384 }
385
ed9a39eb
JM
386 /* Overload thread id onto process id */
387 if ((tid = TIDGET (inferior_pid)) == 0)
388 tid = inferior_pid; /* no thread id, just use process id */
389
c906108c
SS
390 offset = U_REGS_OFFSET;
391
392 regaddr = register_addr (regno, offset);
393 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
394 {
395 errno = 0;
ed9a39eb
JM
396 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
397 (PTRACE_ARG3_TYPE) regaddr, 0);
c906108c
SS
398 regaddr += sizeof (PTRACE_XFER_TYPE);
399 if (errno != 0)
400 {
ed9a39eb
JM
401 sprintf (mess, "reading register %s (#%d)",
402 REGISTER_NAME (regno), regno);
c906108c
SS
403 perror_with_name (mess);
404 }
405 }
406 supply_register (regno, buf);
407}
408
409
410/* Fetch register values from the inferior.
411 If REGNO is negative, do this for all registers.
412 Otherwise, REGNO specifies which register (so we can save time). */
413
414void
fba45db2 415fetch_inferior_registers (int regno)
c906108c
SS
416{
417 if (regno >= 0)
418 {
419 fetch_register (regno);
420 }
421 else
422 {
423 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
424 {
425 fetch_register (regno);
426 }
427 }
428}
429
430/* Registers we shouldn't try to store. */
431#if !defined (CANNOT_STORE_REGISTER)
432#define CANNOT_STORE_REGISTER(regno) 0
433#endif
434
435/* Store one register. */
436
437static void
fba45db2 438store_register (int regno)
c906108c
SS
439{
440 /* This isn't really an address. But ptrace thinks of it as one. */
441 CORE_ADDR regaddr;
c5aa993b 442 char mess[128]; /* For messages */
c906108c 443 register int i;
c5aa993b 444 unsigned int offset; /* Offset of registers within the u area. */
ed9a39eb 445 int tid;
c906108c
SS
446
447 if (CANNOT_STORE_REGISTER (regno))
448 {
449 return;
450 }
451
ed9a39eb
JM
452 /* Overload thread id onto process id */
453 if ((tid = TIDGET (inferior_pid)) == 0)
454 tid = inferior_pid; /* no thread id, just use process id */
455
c906108c
SS
456 offset = U_REGS_OFFSET;
457
458 regaddr = register_addr (regno, offset);
c5aa993b 459 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
c906108c
SS
460 {
461 errno = 0;
ed9a39eb 462 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
c5aa993b 463 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
c906108c
SS
464 regaddr += sizeof (PTRACE_XFER_TYPE);
465 if (errno != 0)
466 {
ed9a39eb
JM
467 sprintf (mess, "writing register %s (#%d)",
468 REGISTER_NAME (regno), regno);
c906108c
SS
469 perror_with_name (mess);
470 }
471 }
472}
473
474/* Store our register values back into the inferior.
475 If REGNO is negative, do this for all registers.
476 Otherwise, REGNO specifies which register (so we can save time). */
477
478void
fba45db2 479store_inferior_registers (int regno)
c906108c
SS
480{
481 if (regno >= 0)
482 {
483 store_register (regno);
484 }
485 else
486 {
487 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
488 {
489 store_register (regno);
490 }
491 }
492}
493#endif /* !defined (FETCH_INFERIOR_REGISTERS). */
494\f
495
496#if !defined (CHILD_XFER_MEMORY)
497/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
498 in the NEW_SUN_PTRACE case.
499 It ought to be straightforward. But it appears that writing did
500 not write the data that I specified. I cannot understand where
501 it got the data that it actually did write. */
502
503/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
504 to debugger memory starting at MYADDR. Copy to inferior if
505 WRITE is nonzero.
c5aa993b 506
c906108c
SS
507 Returns the length copied, which is either the LEN argument or zero.
508 This xfer function does not do partial moves, since child_ops
509 doesn't allow memory operations to cross below us in the target stack
510 anyway. */
511
512int
513child_xfer_memory (memaddr, myaddr, len, write, target)
514 CORE_ADDR memaddr;
515 char *myaddr;
516 int len;
517 int write;
c5aa993b 518 struct target_ops *target; /* ignored */
c906108c
SS
519{
520 register int i;
521 /* Round starting address down to longword boundary. */
c5aa993b 522 register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
c906108c
SS
523 /* Round ending address up; get number of longwords that makes. */
524 register int count
c5aa993b
JM
525 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
526 / sizeof (PTRACE_XFER_TYPE);
c906108c
SS
527 /* Allocate buffer of that many longwords. */
528 register PTRACE_XFER_TYPE *buffer
c5aa993b 529 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
c906108c
SS
530
531 if (write)
532 {
533 /* Fill start and end extra bytes of buffer with existing memory data. */
534
c5aa993b
JM
535 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
536 {
537 /* Need part of initial word -- fetch it. */
ed9a39eb
JM
538 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_pid),
539 (PTRACE_ARG3_TYPE) addr, 0);
c5aa993b 540 }
c906108c
SS
541
542 if (count > 1) /* FIXME, avoid if even boundary */
543 {
ed9a39eb
JM
544 buffer[count - 1]
545 = ptrace (PT_READ_I, PIDGET (inferior_pid),
c906108c
SS
546 ((PTRACE_ARG3_TYPE)
547 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
548 0);
549 }
550
551 /* Copy data to be written over corresponding part of buffer */
552
553 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
554 myaddr,
555 len);
556
557 /* Write the entire buffer. */
558
559 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
560 {
561 errno = 0;
ed9a39eb
JM
562 ptrace (PT_WRITE_D, PIDGET (inferior_pid),
563 (PTRACE_ARG3_TYPE) addr, buffer[i]);
c906108c 564 if (errno)
c5aa993b 565 {
c906108c 566 /* Using the appropriate one (I or D) is necessary for
c5aa993b 567 Gould NP1, at least. */
c906108c 568 errno = 0;
ed9a39eb
JM
569 ptrace (PT_WRITE_I, PIDGET (inferior_pid),
570 (PTRACE_ARG3_TYPE) addr, buffer[i]);
c906108c
SS
571 }
572 if (errno)
573 return 0;
574 }
575#ifdef CLEAR_INSN_CACHE
c5aa993b 576 CLEAR_INSN_CACHE ();
c906108c
SS
577#endif
578 }
579 else
580 {
581 /* Read all the longwords */
582 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
583 {
584 errno = 0;
ed9a39eb 585 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_pid),
c906108c
SS
586 (PTRACE_ARG3_TYPE) addr, 0);
587 if (errno)
588 return 0;
589 QUIT;
590 }
591
592 /* Copy appropriate bytes out of the buffer. */
593 memcpy (myaddr,
594 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
595 len);
596 }
597 return len;
598}
c906108c 599\f
c5aa993b 600
c906108c 601static void
fba45db2 602udot_info (char *dummy1, int dummy2)
c906108c
SS
603{
604#if defined (KERNEL_U_SIZE)
c5aa993b
JM
605 int udot_off; /* Offset into user struct */
606 int udot_val; /* Value from user struct at udot_off */
607 char mess[128]; /* For messages */
c906108c
SS
608#endif
609
c5aa993b
JM
610 if (!target_has_execution)
611 {
612 error ("The program is not being run.");
613 }
c906108c
SS
614
615#if !defined (KERNEL_U_SIZE)
616
617 /* Adding support for this command is easy. Typically you just add a
618 routine, called "kernel_u_size" that returns the size of the user
619 struct, to the appropriate *-nat.c file and then add to the native
620 config file "#define KERNEL_U_SIZE kernel_u_size()" */
621 error ("Don't know how large ``struct user'' is in this version of gdb.");
622
623#else
624
625 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
626 {
627 if ((udot_off % 24) == 0)
628 {
629 if (udot_off > 0)
630 {
631 printf_filtered ("\n");
632 }
633 printf_filtered ("%04x:", udot_off);
634 }
635 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
636 if (errno != 0)
637 {
638 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
639 perror_with_name (mess);
640 }
641 /* Avoid using nonportable (?) "*" in print specs */
642 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
643 }
644 printf_filtered ("\n");
645
646#endif
647}
648#endif /* !defined (CHILD_XFER_MEMORY). */
c906108c 649\f
c5aa993b 650
c906108c 651void
fba45db2 652_initialize_infptrace (void)
c906108c
SS
653{
654#if !defined (CHILD_XFER_MEMORY)
655 add_info ("udot", udot_info,
656 "Print contents of kernel ``struct user'' for current child.");
657#endif
658}