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, 2004
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 #ifdef HAVE_SYS_FILE_H
84 /* Don't think this is used anymore. On the sequent (not sure whether it's
85 dynix or ptx or both), it is included unconditionally by sys/user.h and
86 not protected against multiple inclusion. */
90 #if !defined (FETCH_INFERIOR_REGISTERS)
91 #include <sys/user.h> /* Probably need to poke the user structure */
92 #endif /* !FETCH_INFERIOR_REGISTERS */
94 #if !defined (CHILD_XFER_MEMORY)
95 static void udot_info (char *, int);
98 #if !defined (FETCH_INFERIOR_REGISTERS)
99 static void fetch_register (int);
100 static void store_register (int);
103 void _initialize_infptrace (void);
106 /* This function simply calls ptrace with the given arguments.
107 It exists so that all calls to ptrace are isolated in this
108 machine-dependent file. */
110 call_ptrace (int request
, int pid
, PTRACE_ARG3_TYPE addr
, int data
)
117 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
118 request
, pid
, addr
, data
);
120 #if defined(PT_SETTRC)
121 /* If the parent can be told to attach to us, try to do it. */
122 if (request
== PT_SETTRC
)
125 #ifndef PTRACE_TYPE_ARG5
126 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
);
128 /* Deal with HPUX 8.0 braindamage. We never use the
129 calls which require the fifth argument. */
130 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
, 0);
133 perror_with_name ("ptrace");
135 printf (" = %d\n", pt_status
);
140 return parent_attach_all (pid
, addr
, data
);
144 #if defined(PT_CONTIN1)
145 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
146 signals. If it's available, use it. */
147 if (request
== PT_CONTINUE
)
148 request
= PT_CONTIN1
;
151 #if defined(PT_SINGLE1)
152 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
153 signals. If it's available, use it. */
154 if (request
== PT_STEP
)
155 request
= PT_SINGLE1
;
162 #ifndef PTRACE_TYPE_ARG5
163 pt_status
= ptrace (request
, pid
, addr
, data
);
165 /* Deal with HPUX 8.0 braindamage. We never use the
166 calls which require the fifth argument. */
167 pt_status
= ptrace (request
, pid
, addr
, data
, 0);
172 printf (" [errno = %d]", errno
);
175 printf (" = 0x%x\n", pt_status
);
181 #if defined (DEBUG_PTRACE) || defined (PTRACE_TYPE_ARG5)
182 /* For the rest of the file, use an extra level of indirection */
183 /* This lets us breakpoint usefully on call_ptrace. */
184 #define ptrace call_ptrace
187 /* Wait for a process to finish, possibly running a target-specific
188 hook before returning. */
191 ptrace_wait (ptid_t ptid
, int *status
)
195 wstate
= wait (status
);
196 target_post_wait (pid_to_ptid (wstate
), *status
);
200 #ifndef KILL_INFERIOR
205 int pid
= PIDGET (inferior_ptid
);
210 /* This once used to call "kill" to kill the inferior just in case
211 the inferior was still running. As others have noted in the past
212 (kingdon) there shouldn't be any way to get here if the inferior
213 is still running -- else there's a major problem elsewere in gdb
214 and it needs to be fixed.
216 The kill call causes problems under hpux10, so it's been removed;
217 if this causes problems we'll deal with them as they arise. */
218 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
219 ptrace_wait (null_ptid
, &status
);
220 target_mourn_inferior ();
222 #endif /* KILL_INFERIOR */
226 /* Resume execution of the inferior process.
227 If STEP is nonzero, single-step it.
228 If SIGNAL is nonzero, give it that signal. */
231 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
233 int pid
= PIDGET (ptid
);
238 /* Resume all threads. */
239 /* I think this only gets used in the non-threaded case, where "resume
240 all threads" and "resume inferior_ptid" are the same. */
241 pid
= PIDGET (inferior_ptid
);
243 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from where
244 it was. (If GDB wanted it to start some other way, we have already
245 written a new PC value to the child.)
247 If this system does not support PT_STEP, a higher level function will
248 have called single_step() to transmute the step request into a
249 continue request (by setting breakpoints on all possible successor
250 instructions), so we don't have to worry about that here. */
254 if (SOFTWARE_SINGLE_STEP_P ())
255 internal_error (__FILE__
, __LINE__
, "failed internal consistency check"); /* Make sure this doesn't happen. */
257 ptrace (PT_STEP
, pid
, (PTRACE_TYPE_ARG3
) 1,
258 target_signal_to_host (signal
));
261 ptrace (PT_CONTINUE
, pid
, (PTRACE_TYPE_ARG3
) 1,
262 target_signal_to_host (signal
));
266 perror_with_name ("ptrace");
269 #endif /* CHILD_RESUME */
271 /* Start debugging the process whose number is PID. */
278 #define PT_ATTACH PTRACE_ATTACH
282 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
284 perror_with_name ("ptrace");
288 error ("This system does not support attaching to a process");
292 /* Stop debugging the process whose number is PID
293 and continue it with signal number SIGNAL.
294 SIGNAL = 0 means just continue it. */
302 #define PT_DETACH PTRACE_DETACH
306 ptrace (PT_DETACH
, PIDGET (inferior_ptid
), (PTRACE_TYPE_ARG3
) 1,
309 print_sys_errmsg ("ptrace", errno
);
312 error ("This system does not support detaching from a process");
316 /* Default the type of the ptrace transfer to int. */
317 #ifndef PTRACE_XFER_TYPE
318 #define PTRACE_XFER_TYPE int
321 #if !defined (FETCH_INFERIOR_REGISTERS)
323 #if !defined (offsetof)
324 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
327 /* U_REGS_OFFSET is the offset of the registers within the u area. */
328 #if !defined (U_REGS_OFFSET)
329 #define U_REGS_OFFSET \
330 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
331 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
335 /* Fetch one register. */
338 fetch_register (int regno
)
340 /* This isn't really an address. But ptrace thinks of it as one. */
342 char mess
[128]; /* For messages */
344 unsigned int offset
; /* Offset of registers within the u area. */
345 char buf
[MAX_REGISTER_SIZE
];
348 if (CANNOT_FETCH_REGISTER (regno
))
350 regcache_raw_supply (current_regcache
, regno
, NULL
);
354 /* Overload thread id onto process id */
355 if ((tid
= TIDGET (inferior_ptid
)) == 0)
356 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
358 offset
= U_REGS_OFFSET
;
360 regaddr
= register_addr (regno
, offset
);
361 for (i
= 0; i
< register_size (current_gdbarch
, regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
364 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
365 (PTRACE_TYPE_ARG3
) regaddr
, 0);
366 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
369 sprintf (mess
, "reading register %s (#%d)",
370 REGISTER_NAME (regno
), regno
);
371 perror_with_name (mess
);
374 regcache_raw_supply (current_regcache
, regno
, buf
);
378 /* Fetch register values from the inferior.
379 If REGNO is negative, do this for all registers.
380 Otherwise, REGNO specifies which register (so we can save time). */
383 fetch_inferior_registers (int regno
)
387 fetch_register (regno
);
391 for (regno
= 0; regno
< NUM_REGS
; regno
++)
393 fetch_register (regno
);
398 /* Store one register. */
401 store_register (int regno
)
403 /* This isn't really an address. But ptrace thinks of it as one. */
405 char mess
[128]; /* For messages */
407 unsigned int offset
; /* Offset of registers within the u area. */
409 char buf
[MAX_REGISTER_SIZE
];
411 if (CANNOT_STORE_REGISTER (regno
))
416 /* Overload thread id onto process id */
417 if ((tid
= TIDGET (inferior_ptid
)) == 0)
418 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
420 offset
= U_REGS_OFFSET
;
422 regaddr
= register_addr (regno
, offset
);
424 /* Put the contents of regno into a local buffer */
425 regcache_raw_collect (current_regcache
, regno
, buf
);
427 /* Store the local buffer into the inferior a chunk at the time. */
428 for (i
= 0; i
< register_size (current_gdbarch
, regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
431 ptrace (PT_WRITE_U
, tid
, (PTRACE_TYPE_ARG3
) regaddr
,
432 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
433 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
436 sprintf (mess
, "writing register %s (#%d)",
437 REGISTER_NAME (regno
), regno
);
438 perror_with_name (mess
);
443 /* Store our register values back into the inferior.
444 If REGNO is negative, do this for all registers.
445 Otherwise, REGNO specifies which register (so we can save time). */
448 store_inferior_registers (int regno
)
452 store_register (regno
);
456 for (regno
= 0; regno
< NUM_REGS
; regno
++)
458 store_register (regno
);
462 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
465 /* Set an upper limit on alloca. */
466 #ifndef GDB_MAX_ALLOCA
467 #define GDB_MAX_ALLOCA 0x1000
470 #if !defined (CHILD_XFER_MEMORY)
471 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
472 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
473 it appears that writing did not write the data that I specified. I
474 cannot understand where it got the data that it actually did write. */
476 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
477 debugger memory starting at MYADDR. Copy to inferior if WRITE is
478 nonzero. TARGET is ignored.
480 Returns the length copied, which is either the LEN argument or
481 zero. This xfer function does not do partial moves, since
482 child_ops doesn't allow memory operations to cross below us in the
483 target stack anyway. */
486 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
487 struct mem_attrib
*attrib
, struct target_ops
*target
)
490 /* Round starting address down to longword boundary. */
491 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_TYPE_RET
);
492 /* Round ending address up; get number of longwords that makes. */
493 int count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_TYPE_RET
) - 1)
494 / sizeof (PTRACE_TYPE_RET
));
495 int alloc
= count
* sizeof (PTRACE_TYPE_RET
);
496 PTRACE_TYPE_RET
*buffer
;
497 struct cleanup
*old_chain
= NULL
;
500 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
501 that promises to be much more efficient in reading and writing
502 data in the traced process's address space. */
505 struct ptrace_io_desc piod
;
507 /* NOTE: We assume that there are no distinct address spaces for
508 instruction and data. */
509 piod
.piod_op
= write
? PIOD_WRITE_D
: PIOD_READ_D
;
510 piod
.piod_offs
= (void *) memaddr
;
511 piod
.piod_addr
= myaddr
;
514 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == -1)
516 /* If the PT_IO request is somehow not supported, fallback on
517 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
518 to indicate failure. */
524 /* Return the actual number of bytes read or written. */
525 return piod
.piod_len
;
530 /* Allocate buffer of that many longwords. */
531 if (len
< GDB_MAX_ALLOCA
)
533 buffer
= (PTRACE_TYPE_RET
*) alloca (alloc
);
537 buffer
= (PTRACE_TYPE_RET
*) xmalloc (alloc
);
538 old_chain
= make_cleanup (xfree
, buffer
);
543 /* Fill start and end extra bytes of buffer with existing memory
545 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_TYPE_RET
))
547 /* Need part of initial word -- fetch it. */
548 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
549 (PTRACE_TYPE_ARG3
) addr
, 0);
552 if (count
> 1) /* FIXME, avoid if even boundary. */
555 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
557 (addr
+ (count
- 1) * sizeof (PTRACE_TYPE_RET
))), 0);
560 /* Copy data to be written over corresponding part of buffer. */
561 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
564 /* Write the entire buffer. */
565 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
568 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
569 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
572 /* Using the appropriate one (I or D) is necessary for
573 Gould NP1, at least. */
575 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
576 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
584 /* Read all the longwords. */
585 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
588 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
589 (PTRACE_TYPE_ARG3
) addr
, 0);
595 /* Copy appropriate bytes out of the buffer. */
597 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
601 if (old_chain
!= NULL
)
602 do_cleanups (old_chain
);
608 udot_info (char *dummy1
, int dummy2
)
610 #if defined (KERNEL_U_SIZE)
611 long udot_off
; /* Offset into user struct */
612 int udot_val
; /* Value from user struct at udot_off */
613 char mess
[128]; /* For messages */
616 if (!target_has_execution
)
618 error ("The program is not being run.");
621 #if !defined (KERNEL_U_SIZE)
623 /* Adding support for this command is easy. Typically you just add a
624 routine, called "kernel_u_size" that returns the size of the user
625 struct, to the appropriate *-nat.c file and then add to the native
626 config file "#define KERNEL_U_SIZE kernel_u_size()" */
627 error ("Don't know how large ``struct user'' is in this version of gdb.");
631 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
633 if ((udot_off
% 24) == 0)
637 printf_filtered ("\n");
639 printf_filtered ("%s:", paddr (udot_off
));
641 udot_val
= ptrace (PT_READ_U
, PIDGET (inferior_ptid
), (PTRACE_TYPE_ARG3
) udot_off
, 0);
644 sprintf (mess
, "\nreading user struct at offset 0x%s",
645 paddr_nz (udot_off
));
646 perror_with_name (mess
);
648 /* Avoid using nonportable (?) "*" in print specs */
649 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
651 printf_filtered ("\n");
655 #endif /* !defined (CHILD_XFER_MEMORY). */
659 _initialize_infptrace (void)
661 #if !defined (CHILD_XFER_MEMORY)
662 add_info ("udot", udot_info
,
663 "Print contents of kernel ``struct user'' for current child.");
This page took 0.065214 seconds and 5 git commands to generate.