1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
35 #include <sys/types.h>
38 #include <sys/param.h>
39 #include "gdb_dirent.h"
41 #include <sys/ioctl.h>
46 #ifdef HAVE_SYS_PTRACE_H
47 #include <sys/ptrace.h>
51 #if !defined (PT_READ_I)
52 #define PT_READ_I 1 /* Read word from text space */
54 #if !defined (PT_READ_D)
55 #define PT_READ_D 2 /* Read word from data space */
57 #if !defined (PT_READ_U)
58 #define PT_READ_U 3 /* Read word from kernel user struct */
60 #if !defined (PT_WRITE_I)
61 #define PT_WRITE_I 4 /* Write word to text space */
63 #if !defined (PT_WRITE_D)
64 #define PT_WRITE_D 5 /* Write word to data space */
66 #if !defined (PT_WRITE_U)
67 #define PT_WRITE_U 6 /* Write word to kernel user struct */
69 #if !defined (PT_CONTINUE)
70 #define PT_CONTINUE 7 /* Continue after signal */
72 #if !defined (PT_STEP)
73 #define PT_STEP 9 /* Set flag for single stepping */
75 #if !defined (PT_KILL)
76 #define PT_KILL 8 /* Send child a SIGKILL signal */
80 #define PT_ATTACH PTRACE_ATTACH
83 #define PT_DETACH PTRACE_DETACH
91 /* Don't think this is used anymore. On the sequent (not sure whether it's
92 dynix or ptx or both), it is included unconditionally by sys/user.h and
93 not protected against multiple inclusion. */
97 #if !defined (FETCH_INFERIOR_REGISTERS)
98 #include <sys/user.h> /* Probably need to poke the user structure */
99 #if defined (KERNEL_U_ADDR_BSD)
100 #include <a.out.h> /* For struct nlist */
101 #endif /* KERNEL_U_ADDR_BSD. */
102 #endif /* !FETCH_INFERIOR_REGISTERS */
104 #if !defined (CHILD_XFER_MEMORY)
105 static void udot_info (char *, int);
108 #if !defined (FETCH_INFERIOR_REGISTERS)
109 static void fetch_register (int);
110 static void store_register (int);
113 void _initialize_kernel_u_addr (void);
114 void _initialize_infptrace (void);
117 /* This function simply calls ptrace with the given arguments.
118 It exists so that all calls to ptrace are isolated in this
119 machine-dependent file. */
121 call_ptrace (int request
, int pid
, PTRACE_ARG3_TYPE addr
, int data
)
128 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
129 request
, pid
, addr
, data
);
131 #if defined(PT_SETTRC)
132 /* If the parent can be told to attach to us, try to do it. */
133 if (request
== PT_SETTRC
)
136 #if !defined (FIVE_ARG_PTRACE)
137 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
);
139 /* Deal with HPUX 8.0 braindamage. We never use the
140 calls which require the fifth argument. */
141 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
, 0);
144 perror_with_name ("ptrace");
146 printf (" = %d\n", pt_status
);
151 return parent_attach_all (pid
, addr
, data
);
155 #if defined(PT_CONTIN1)
156 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
157 signals. If it's available, use it. */
158 if (request
== PT_CONTINUE
)
159 request
= PT_CONTIN1
;
162 #if defined(PT_SINGLE1)
163 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
164 signals. If it's available, use it. */
165 if (request
== PT_STEP
)
166 request
= PT_SINGLE1
;
173 #if !defined (FIVE_ARG_PTRACE)
174 pt_status
= ptrace (request
, pid
, addr
, data
);
176 /* Deal with HPUX 8.0 braindamage. We never use the
177 calls which require the fifth argument. */
178 pt_status
= ptrace (request
, pid
, addr
, data
, 0);
183 printf (" [errno = %d]", errno
);
186 printf (" = 0x%x\n", pt_status
);
192 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
193 /* For the rest of the file, use an extra level of indirection */
194 /* This lets us breakpoint usefully on call_ptrace. */
195 #define ptrace call_ptrace
198 /* Wait for a process to finish, possibly running a target-specific
199 hook before returning. */
202 ptrace_wait (ptid_t ptid
, int *status
)
206 wstate
= wait (status
);
207 target_post_wait (pid_to_ptid (wstate
), *status
);
215 int pid
= PIDGET (inferior_ptid
);
220 /* This once used to call "kill" to kill the inferior just in case
221 the inferior was still running. As others have noted in the past
222 (kingdon) there shouldn't be any way to get here if the inferior
223 is still running -- else there's a major problem elsewere in gdb
224 and it needs to be fixed.
226 The kill call causes problems under hpux10, so it's been removed;
227 if this causes problems we'll deal with them as they arise. */
228 ptrace (PT_KILL
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
229 ptrace_wait (null_ptid
, &status
);
230 target_mourn_inferior ();
235 /* Resume execution of the inferior process.
236 If STEP is nonzero, single-step it.
237 If SIGNAL is nonzero, give it that signal. */
240 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
242 int pid
= PIDGET (ptid
);
247 /* Resume all threads. */
248 /* I think this only gets used in the non-threaded case, where "resume
249 all threads" and "resume inferior_ptid" are the same. */
250 pid
= PIDGET (inferior_ptid
);
252 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
253 it was. (If GDB wanted it to start some other way, we have already
254 written a new PC value to the child.)
256 If this system does not support PT_STEP, a higher level function will
257 have called single_step() to transmute the step request into a
258 continue request (by setting breakpoints on all possible successor
259 instructions), so we don't have to worry about that here. */
263 if (SOFTWARE_SINGLE_STEP_P ())
264 internal_error (__FILE__
, __LINE__
, "failed internal consistency check"); /* Make sure this doesn't happen. */
266 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
267 target_signal_to_host (signal
));
270 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
271 target_signal_to_host (signal
));
275 perror_with_name ("ptrace");
278 #endif /* CHILD_RESUME */
282 /* Start debugging the process whose number is PID. */
287 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
289 perror_with_name ("ptrace");
294 /* Stop debugging the process whose number is PID
295 and continue it with signal number SIGNAL.
296 SIGNAL = 0 means just continue it. */
302 ptrace (PT_DETACH
, PIDGET (inferior_ptid
), (PTRACE_ARG3_TYPE
) 1,
305 print_sys_errmsg ("ptrace", errno
);
308 #endif /* ATTACH_DETACH */
310 /* Default the type of the ptrace transfer to int. */
311 #ifndef PTRACE_XFER_TYPE
312 #define PTRACE_XFER_TYPE int
315 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
316 to get the offset in the core file of the register values. */
317 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
318 /* Get kernel_u_addr using BSD-style nlist(). */
319 CORE_ADDR kernel_u_addr
;
320 #endif /* KERNEL_U_ADDR_BSD. */
323 _initialize_kernel_u_addr (void)
325 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
326 struct nlist names
[2];
328 names
[0].n_un
.n_name
= "_u";
329 names
[1].n_un
.n_name
= NULL
;
330 if (nlist ("/vmunix", names
) == 0)
331 kernel_u_addr
= names
[0].n_value
;
333 internal_error (__FILE__
, __LINE__
,
334 "Unable to get kernel u area address.");
335 #endif /* KERNEL_U_ADDR_BSD. */
338 #if !defined (FETCH_INFERIOR_REGISTERS)
340 #if !defined (offsetof)
341 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
344 /* U_REGS_OFFSET is the offset of the registers within the u area. */
345 #if !defined (U_REGS_OFFSET)
346 #define U_REGS_OFFSET \
347 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
348 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
352 /* Fetch one register. */
355 fetch_register (int regno
)
357 /* This isn't really an address. But ptrace thinks of it as one. */
359 char mess
[128]; /* For messages */
361 unsigned int offset
; /* Offset of registers within the u area. */
362 char buf
[MAX_REGISTER_SIZE
];
365 if (CANNOT_FETCH_REGISTER (regno
))
367 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
368 supply_register (regno
, buf
);
372 /* Overload thread id onto process id */
373 if ((tid
= TIDGET (inferior_ptid
)) == 0)
374 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
376 offset
= U_REGS_OFFSET
;
378 regaddr
= register_addr (regno
, offset
);
379 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
382 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
383 (PTRACE_ARG3_TYPE
) regaddr
, 0);
384 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
387 sprintf (mess
, "reading register %s (#%d)",
388 REGISTER_NAME (regno
), regno
);
389 perror_with_name (mess
);
392 supply_register (regno
, buf
);
396 /* Fetch register values from the inferior.
397 If REGNO is negative, do this for all registers.
398 Otherwise, REGNO specifies which register (so we can save time). */
401 fetch_inferior_registers (int regno
)
405 fetch_register (regno
);
409 for (regno
= 0; regno
< NUM_REGS
; regno
++)
411 fetch_register (regno
);
416 /* Store one register. */
419 store_register (int regno
)
421 /* This isn't really an address. But ptrace thinks of it as one. */
423 char mess
[128]; /* For messages */
425 unsigned int offset
; /* Offset of registers within the u area. */
427 char buf
[MAX_REGISTER_SIZE
];
429 if (CANNOT_STORE_REGISTER (regno
))
434 /* Overload thread id onto process id */
435 if ((tid
= TIDGET (inferior_ptid
)) == 0)
436 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
438 offset
= U_REGS_OFFSET
;
440 regaddr
= register_addr (regno
, offset
);
442 /* Put the contents of regno into a local buffer */
443 regcache_collect (regno
, buf
);
445 /* Store the local buffer into the inferior a chunk at the time. */
446 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
449 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
450 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
451 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
454 sprintf (mess
, "writing register %s (#%d)",
455 REGISTER_NAME (regno
), regno
);
456 perror_with_name (mess
);
461 /* Store our register values back into the inferior.
462 If REGNO is negative, do this for all registers.
463 Otherwise, REGNO specifies which register (so we can save time). */
466 store_inferior_registers (int regno
)
470 store_register (regno
);
474 for (regno
= 0; regno
< NUM_REGS
; regno
++)
476 store_register (regno
);
480 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
483 /* Set an upper limit on alloca. */
484 #ifndef GDB_MAX_ALLOCA
485 #define GDB_MAX_ALLOCA 0x1000
488 #if !defined (CHILD_XFER_MEMORY)
489 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
490 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
491 it appears that writing did not write the data that I specified. I
492 cannot understand where it got the data that it actually did write. */
494 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
495 debugger memory starting at MYADDR. Copy to inferior if WRITE is
496 nonzero. TARGET is ignored.
498 Returns the length copied, which is either the LEN argument or
499 zero. This xfer function does not do partial moves, since
500 child_ops doesn't allow memory operations to cross below us in the
501 target stack anyway. */
504 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
505 struct mem_attrib
*attrib
, struct target_ops
*target
)
508 /* Round starting address down to longword boundary. */
509 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
510 /* Round ending address up; get number of longwords that makes. */
511 int count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
512 / sizeof (PTRACE_XFER_TYPE
));
513 int alloc
= count
* sizeof (PTRACE_XFER_TYPE
);
514 PTRACE_XFER_TYPE
*buffer
;
515 struct cleanup
*old_chain
= NULL
;
518 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
519 that promises to be much more efficient in reading and writing
520 data in the traced process's address space. */
523 struct ptrace_io_desc piod
;
525 /* NOTE: We assume that there are no distinct address spaces for
526 instruction and data. */
527 piod
.piod_op
= write
? PIOD_WRITE_D
: PIOD_READ_D
;
528 piod
.piod_offs
= (void *) memaddr
;
529 piod
.piod_addr
= myaddr
;
532 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == -1)
534 /* If the PT_IO request is somehow not supported, fallback on
535 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
536 to indicate failure. */
542 /* Return the actual number of bytes read or written. */
543 return piod
.piod_len
;
548 /* Allocate buffer of that many longwords. */
549 if (len
< GDB_MAX_ALLOCA
)
551 buffer
= (PTRACE_XFER_TYPE
*) alloca (alloc
);
555 buffer
= (PTRACE_XFER_TYPE
*) xmalloc (alloc
);
556 old_chain
= make_cleanup (xfree
, buffer
);
561 /* Fill start and end extra bytes of buffer with existing memory
563 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
565 /* Need part of initial word -- fetch it. */
566 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
567 (PTRACE_ARG3_TYPE
) addr
, 0);
570 if (count
> 1) /* FIXME, avoid if even boundary. */
573 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
575 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))), 0);
578 /* Copy data to be written over corresponding part of buffer. */
579 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
582 /* Write the entire buffer. */
583 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
586 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
587 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
590 /* Using the appropriate one (I or D) is necessary for
591 Gould NP1, at least. */
593 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
594 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
599 #ifdef CLEAR_INSN_CACHE
605 /* Read all the longwords. */
606 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
609 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
610 (PTRACE_ARG3_TYPE
) addr
, 0);
616 /* Copy appropriate bytes out of the buffer. */
618 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
622 if (old_chain
!= NULL
)
623 do_cleanups (old_chain
);
629 udot_info (char *dummy1
, int dummy2
)
631 #if defined (KERNEL_U_SIZE)
632 long udot_off
; /* Offset into user struct */
633 int udot_val
; /* Value from user struct at udot_off */
634 char mess
[128]; /* For messages */
637 if (!target_has_execution
)
639 error ("The program is not being run.");
642 #if !defined (KERNEL_U_SIZE)
644 /* Adding support for this command is easy. Typically you just add a
645 routine, called "kernel_u_size" that returns the size of the user
646 struct, to the appropriate *-nat.c file and then add to the native
647 config file "#define KERNEL_U_SIZE kernel_u_size()" */
648 error ("Don't know how large ``struct user'' is in this version of gdb.");
652 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
654 if ((udot_off
% 24) == 0)
658 printf_filtered ("\n");
660 printf_filtered ("%s:", paddr (udot_off
));
662 udot_val
= ptrace (PT_READ_U
, PIDGET (inferior_ptid
), (PTRACE_ARG3_TYPE
) udot_off
, 0);
665 sprintf (mess
, "\nreading user struct at offset 0x%s",
666 paddr_nz (udot_off
));
667 perror_with_name (mess
);
669 /* Avoid using nonportable (?) "*" in print specs */
670 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
672 printf_filtered ("\n");
676 #endif /* !defined (CHILD_XFER_MEMORY). */
680 _initialize_infptrace (void)
682 #if !defined (CHILD_XFER_MEMORY)
683 add_info ("udot", udot_info
,
684 "Print contents of kernel ``struct user'' for current child.");
This page took 0.043279 seconds and 4 git commands to generate.