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. */
31 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 #include <sys/param.h>
36 #include "gdb_dirent.h"
38 #include <sys/ioctl.h>
40 #include "gdb_ptrace.h"
42 #ifdef HAVE_SYS_FILE_H
46 #if !defined (FETCH_INFERIOR_REGISTERS)
47 #include <sys/user.h> /* Probably need to poke the user structure */
48 #endif /* !FETCH_INFERIOR_REGISTERS */
50 #if !defined (CHILD_XFER_MEMORY)
51 static void udot_info (char *, int);
54 void _initialize_infptrace (void);
57 /* This function simply calls ptrace with the given arguments.
58 It exists so that all calls to ptrace are isolated in this
59 machine-dependent file. */
61 call_ptrace (int request
, int pid
, PTRACE_ARG3_TYPE addr
, int data
)
68 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
69 request
, pid
, addr
, data
);
71 #if defined(PT_SETTRC)
72 /* If the parent can be told to attach to us, try to do it. */
73 if (request
== PT_SETTRC
)
76 #ifndef PTRACE_TYPE_ARG5
77 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
);
79 /* Deal with HPUX 8.0 braindamage. We never use the
80 calls which require the fifth argument. */
81 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
, 0);
84 perror_with_name ("ptrace");
86 printf (" = %d\n", pt_status
);
91 return parent_attach_all (pid
, addr
, data
);
95 #if defined(PT_CONTIN1)
96 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
97 signals. If it's available, use it. */
98 if (request
== PT_CONTINUE
)
102 #if defined(PT_SINGLE1)
103 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
104 signals. If it's available, use it. */
105 if (request
== PT_STEP
)
106 request
= PT_SINGLE1
;
113 #ifndef PTRACE_TYPE_ARG5
114 pt_status
= ptrace (request
, pid
, addr
, data
);
116 /* Deal with HPUX 8.0 braindamage. We never use the
117 calls which require the fifth argument. */
118 pt_status
= ptrace (request
, pid
, addr
, data
, 0);
123 printf (" [errno = %d]", errno
);
126 printf (" = 0x%x\n", pt_status
);
132 #if defined (DEBUG_PTRACE) || defined (PTRACE_TYPE_ARG5)
133 /* For the rest of the file, use an extra level of indirection */
134 /* This lets us breakpoint usefully on call_ptrace. */
135 #define ptrace call_ptrace
138 /* Wait for a process to finish, possibly running a target-specific
139 hook before returning. */
142 ptrace_wait (ptid_t ptid
, int *status
)
146 wstate
= wait (status
);
147 target_post_wait (pid_to_ptid (wstate
), *status
);
151 #ifndef KILL_INFERIOR
156 int pid
= PIDGET (inferior_ptid
);
161 /* This once used to call "kill" to kill the inferior just in case
162 the inferior was still running. As others have noted in the past
163 (kingdon) there shouldn't be any way to get here if the inferior
164 is still running -- else there's a major problem elsewere in gdb
165 and it needs to be fixed.
167 The kill call causes problems under hpux10, so it's been removed;
168 if this causes problems we'll deal with them as they arise. */
169 ptrace (PT_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
170 ptrace_wait (null_ptid
, &status
);
171 target_mourn_inferior ();
173 #endif /* KILL_INFERIOR */
177 /* Resume execution of the inferior process.
178 If STEP is nonzero, single-step it.
179 If SIGNAL is nonzero, give it that signal. */
182 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
184 int request
= PT_CONTINUE
;
185 int pid
= PIDGET (ptid
);
188 /* Resume all threads. */
189 /* I think this only gets used in the non-threaded case, where "resume
190 all threads" and "resume inferior_ptid" are the same. */
191 pid
= PIDGET (inferior_ptid
);
195 /* If this system does not support PT_STEP, a higher level
196 function will have called single_step() to transmute the step
197 request into a continue request (by setting breakpoints on
198 all possible successor instructions), so we don't have to
199 worry about that here. */
201 gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
205 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
206 where it was. If GDB wanted it to start some other way, we have
207 already written a new PC value to the child. */
210 ptrace (request
, pid
, (PTRACE_TYPE_ARG3
)1, target_signal_to_host (signal
));
212 perror_with_name ("ptrace");
214 #endif /* CHILD_RESUME */
217 /* Start debugging the process whose number is PID. */
224 ptrace (PT_ATTACH
, pid
, (PTRACE_TYPE_ARG3
) 0, 0);
226 perror_with_name ("ptrace");
230 error ("This system does not support attaching to a process");
234 /* Stop debugging the process whose number is PID and continue it with
235 signal number SIGNAL. SIGNAL = 0 means just continue it. */
241 int pid
= PIDGET (inferior_ptid
);
244 ptrace (PT_DETACH
, pid
, (PTRACE_TYPE_ARG3
) 1, signal
);
246 perror_with_name ("ptrace");
249 error ("This system does not support detaching from a process");
254 #ifndef FETCH_INFERIOR_REGISTERS
256 /* U_REGS_OFFSET is the offset of the registers within the u area. */
257 #ifndef U_REGS_OFFSET
260 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
263 #define U_REGS_OFFSET \
264 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
265 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
269 /* Fetch register REGNUM from the inferior. */
272 fetch_register (int regnum
)
276 PTRACE_TYPE_RET
*buf
;
279 if (CANNOT_FETCH_REGISTER (regnum
))
281 regcache_raw_supply (current_regcache
, regnum
, NULL
);
285 /* GNU/Linux LWP ID's are process ID's. */
286 tid
= TIDGET (inferior_ptid
);
288 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
290 /* This isn't really an address. But ptrace thinks of it as one. */
291 addr
= register_addr (regnum
, U_REGS_OFFSET
);
292 size
= register_size (current_gdbarch
, regnum
);
294 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
297 /* Read the register contents from the inferior a chuck at the time. */
298 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
301 buf
[i
] = ptrace (PT_READ_U
, tid
, (PTRACE_TYPE_ARG3
) addr
, 0);
303 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regnum
),
304 regnum
, safe_strerror (errno
));
306 addr
+= sizeof (PTRACE_TYPE_RET
);
308 regcache_raw_supply (current_regcache
, regnum
, buf
);
311 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
312 for all registers. */
315 fetch_inferior_registers (int regnum
)
318 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
319 fetch_register (regnum
);
321 fetch_register (regnum
);
324 /* Store register REGNUM into the inferior. */
327 store_register (int regnum
)
331 PTRACE_TYPE_RET
*buf
;
334 if (CANNOT_STORE_REGISTER (regnum
))
337 /* GNU/Linux LWP ID's are process ID's. */
338 tid
= TIDGET (inferior_ptid
);
340 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
342 /* This isn't really an address. But ptrace thinks of it as one. */
343 addr
= register_addr (regnum
, U_REGS_OFFSET
);
344 size
= register_size (current_gdbarch
, regnum
);
346 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
349 /* Write the register contents into the inferior a chunk at the time. */
350 regcache_raw_collect (current_regcache
, regnum
, buf
);
351 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
354 ptrace (PT_WRITE_U
, tid
, (PTRACE_TYPE_ARG3
) addr
, buf
[i
]);
356 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regnum
),
357 regnum
, safe_strerror (errno
));
359 addr
+= sizeof (PTRACE_TYPE_RET
);
363 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
364 this for all registers (including the floating point registers). */
367 store_inferior_registers (int regnum
)
370 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
371 store_register (regnum
);
373 store_register (regnum
);
376 #endif /* not FETCH_INFERIOR_REGISTERS. */
379 /* Set an upper limit on alloca. */
380 #ifndef GDB_MAX_ALLOCA
381 #define GDB_MAX_ALLOCA 0x1000
384 #if !defined (CHILD_XFER_MEMORY)
385 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
386 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
387 it appears that writing did not write the data that I specified. I
388 cannot understand where it got the data that it actually did write. */
390 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
391 debugger memory starting at MYADDR. Copy to inferior if WRITE is
392 nonzero. TARGET is ignored.
394 Returns the length copied, which is either the LEN argument or
395 zero. This xfer function does not do partial moves, since
396 child_ops doesn't allow memory operations to cross below us in the
397 target stack anyway. */
400 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
401 struct mem_attrib
*attrib
, struct target_ops
*target
)
404 /* Round starting address down to longword boundary. */
405 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_TYPE_RET
);
406 /* Round ending address up; get number of longwords that makes. */
407 int count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_TYPE_RET
) - 1)
408 / sizeof (PTRACE_TYPE_RET
));
409 int alloc
= count
* sizeof (PTRACE_TYPE_RET
);
410 PTRACE_TYPE_RET
*buffer
;
411 struct cleanup
*old_chain
= NULL
;
414 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
415 that promises to be much more efficient in reading and writing
416 data in the traced process's address space. */
419 struct ptrace_io_desc piod
;
421 /* NOTE: We assume that there are no distinct address spaces for
422 instruction and data. */
423 piod
.piod_op
= write
? PIOD_WRITE_D
: PIOD_READ_D
;
424 piod
.piod_offs
= (void *) memaddr
;
425 piod
.piod_addr
= myaddr
;
428 if (ptrace (PT_IO
, PIDGET (inferior_ptid
), (caddr_t
) &piod
, 0) == -1)
430 /* If the PT_IO request is somehow not supported, fallback on
431 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
432 to indicate failure. */
438 /* Return the actual number of bytes read or written. */
439 return piod
.piod_len
;
444 /* Allocate buffer of that many longwords. */
445 if (len
< GDB_MAX_ALLOCA
)
447 buffer
= (PTRACE_TYPE_RET
*) alloca (alloc
);
451 buffer
= (PTRACE_TYPE_RET
*) xmalloc (alloc
);
452 old_chain
= make_cleanup (xfree
, buffer
);
457 /* Fill start and end extra bytes of buffer with existing memory
459 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_TYPE_RET
))
461 /* Need part of initial word -- fetch it. */
462 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
463 (PTRACE_TYPE_ARG3
) addr
, 0);
466 if (count
> 1) /* FIXME, avoid if even boundary. */
469 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
471 (addr
+ (count
- 1) * sizeof (PTRACE_TYPE_RET
))), 0);
474 /* Copy data to be written over corresponding part of buffer. */
475 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
478 /* Write the entire buffer. */
479 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
482 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
483 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
486 /* Using the appropriate one (I or D) is necessary for
487 Gould NP1, at least. */
489 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
490 (PTRACE_TYPE_ARG3
) addr
, buffer
[i
]);
498 /* Read all the longwords. */
499 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_TYPE_RET
))
502 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
503 (PTRACE_TYPE_ARG3
) addr
, 0);
509 /* Copy appropriate bytes out of the buffer. */
511 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_TYPE_RET
) - 1)),
515 if (old_chain
!= NULL
)
516 do_cleanups (old_chain
);
522 udot_info (char *dummy1
, int dummy2
)
524 #if defined (KERNEL_U_SIZE)
525 long udot_off
; /* Offset into user struct */
526 int udot_val
; /* Value from user struct at udot_off */
527 char mess
[128]; /* For messages */
530 if (!target_has_execution
)
532 error ("The program is not being run.");
535 #if !defined (KERNEL_U_SIZE)
537 /* Adding support for this command is easy. Typically you just add a
538 routine, called "kernel_u_size" that returns the size of the user
539 struct, to the appropriate *-nat.c file and then add to the native
540 config file "#define KERNEL_U_SIZE kernel_u_size()" */
541 error ("Don't know how large ``struct user'' is in this version of gdb.");
545 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
547 if ((udot_off
% 24) == 0)
551 printf_filtered ("\n");
553 printf_filtered ("%s:", paddr (udot_off
));
555 udot_val
= ptrace (PT_READ_U
, PIDGET (inferior_ptid
), (PTRACE_TYPE_ARG3
) udot_off
, 0);
558 sprintf (mess
, "\nreading user struct at offset 0x%s",
559 paddr_nz (udot_off
));
560 perror_with_name (mess
);
562 /* Avoid using nonportable (?) "*" in print specs */
563 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
565 printf_filtered ("\n");
569 #endif /* !defined (CHILD_XFER_MEMORY). */
573 _initialize_infptrace (void)
575 #if !defined (CHILD_XFER_MEMORY)
576 add_info ("udot", udot_info
,
577 "Print contents of kernel ``struct user'' for current child.");
This page took 0.045572 seconds and 4 git commands to generate.