1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002, 2004, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
30 #include "gdb_assert.h"
32 #include "gdb_string.h"
34 #include <sys/param.h>
35 #include "gdb_dirent.h"
37 #include <sys/ioctl.h>
39 #include "gdb_ptrace.h"
41 #ifdef HAVE_SYS_FILE_H
45 #if !defined (FETCH_INFERIOR_REGISTERS)
46 #include <sys/user.h> /* Probably need to poke the user structure */
47 #endif /* !FETCH_INFERIOR_REGISTERS */
49 #if !defined (CHILD_XFER_MEMORY)
50 static void udot_info (char *, int);
53 void _initialize_infptrace (void);
57 call_ptrace (int request, int pid, PTRACE_TYPE_ARG3 addr, int data)
59 return ptrace (request, pid, addr, data);
62 /* Wait for a process to finish, possibly running a target-specific
63 hook before returning. */
65 /* NOTE: cagney: 2004-09-29: Dependant on the native configuration,
66 "hppah-nat.c" may either call this or infttrace.c's implementation
67 of ptrace_wait. See "hppahpux.mh". */
70 ptrace_wait (ptid_t ptid, int *status)
74 wstate = wait (status);
78 #ifndef DEPRECATED_KILL_INFERIOR
79 /* NOTE: cagney/2004-09-12: Instead of definining this macro, code
80 should call inf_ptrace_target to get a basic ptrace target and then
81 locally update any necessary methods. See ppcnbsd-nat.c. */
87 int pid = PIDGET (inferior_ptid);
92 /* This once used to call "kill" to kill the inferior just in case
93 the inferior was still running. As others have noted in the past
94 (kingdon) there shouldn't be any way to get here if the inferior
95 is still running -- else there's a major problem elsewere in gdb
96 and it needs to be fixed.
98 The kill call causes problems under hpux10, so it's been removed;
99 if this causes problems we'll deal with them as they arise. */
100 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
102 target_mourn_inferior ();
104 #endif /* DEPRECATED_KILL_INFERIOR */
106 #ifndef DEPRECATED_CHILD_RESUME
107 /* NOTE: cagney/2004-09-12: Instead of definining this macro, code
108 should call inf_ptrace_target to get a basic ptrace target and then
109 locally update any necessary methods. See ppcnbsd-nat.c. */
111 /* Resume execution of the inferior process.
112 If STEP is nonzero, single-step it.
113 If SIGNAL is nonzero, give it that signal. */
116 child_resume (ptid_t ptid, int step, enum target_signal signal)
118 int request = PT_CONTINUE;
119 int pid = PIDGET (ptid);
122 /* Resume all threads. */
123 /* I think this only gets used in the non-threaded case, where "resume
124 all threads" and "resume inferior_ptid" are the same. */
125 pid = PIDGET (inferior_ptid);
129 /* If this system does not support PT_STEP, a higher level
130 function will have called single_step() to transmute the step
131 request into a continue request (by setting breakpoints on
132 all possible successor instructions), so we don't have to
133 worry about that here. */
135 gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
139 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
140 where it was. If GDB wanted it to start some other way, we have
141 already written a new PC value to the child. */
144 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
146 perror_with_name (("ptrace"));
148 #endif /* DEPRECATED_CHILD_RESUME */
151 /* Start debugging the process whose number is PID. */
158 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
160 perror_with_name (("ptrace"));
164 error (_("This system does not support attaching to a process"));
168 /* Stop debugging the process whose number is PID and continue it with
169 signal number SIGNAL. SIGNAL = 0 means just continue it. */
175 int pid = PIDGET (inferior_ptid);
178 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal);
180 perror_with_name (("ptrace"));
183 error (_("This system does not support detaching from a process"));
188 #ifndef FETCH_INFERIOR_REGISTERS
190 /* U_REGS_OFFSET is the offset of the registers within the u area. */
191 #ifndef U_REGS_OFFSET
194 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
197 #define U_REGS_OFFSET \
198 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
199 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
203 /* Fetch register REGNUM from the inferior. */
206 fetch_register (int regnum)
210 PTRACE_TYPE_RET *buf;
213 if (CANNOT_FETCH_REGISTER (regnum))
215 regcache_raw_supply (current_regcache, regnum, NULL);
219 /* GNU/Linux LWP ID's are process ID's. */
220 tid = TIDGET (inferior_ptid);
222 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
224 /* This isn't really an address. But ptrace thinks of it as one. */
225 addr = register_addr (regnum, U_REGS_OFFSET);
226 size = register_size (current_gdbarch, regnum);
228 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
231 /* Read the register contents from the inferior a chuck at the time. */
232 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
235 buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0);
237 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regnum),
238 regnum, safe_strerror (errno));
240 addr += sizeof (PTRACE_TYPE_RET);
242 regcache_raw_supply (current_regcache, regnum, buf);
245 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
246 for all registers. */
249 fetch_inferior_registers (int regnum)
252 for (regnum = 0; regnum < NUM_REGS; regnum++)
253 fetch_register (regnum);
255 fetch_register (regnum);
258 /* Store register REGNUM into the inferior. */
261 store_register (int regnum)
265 PTRACE_TYPE_RET *buf;
268 if (CANNOT_STORE_REGISTER (regnum))
271 /* GNU/Linux LWP ID's are process ID's. */
272 tid = TIDGET (inferior_ptid);
274 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
276 /* This isn't really an address. But ptrace thinks of it as one. */
277 addr = register_addr (regnum, U_REGS_OFFSET);
278 size = register_size (current_gdbarch, regnum);
280 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
283 /* Write the register contents into the inferior a chunk at the time. */
284 regcache_raw_collect (current_regcache, regnum, buf);
285 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
288 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]);
290 error (_("Couldn't write register %s (#%d): %s."),
291 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
293 addr += sizeof (PTRACE_TYPE_RET);
297 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
298 this for all registers (including the floating point registers). */
301 store_inferior_registers (int regnum)
304 for (regnum = 0; regnum < NUM_REGS; regnum++)
305 store_register (regnum);
307 store_register (regnum);
310 #endif /* not FETCH_INFERIOR_REGISTERS. */
313 /* Set an upper limit on alloca. */
314 #ifndef GDB_MAX_ALLOCA
315 #define GDB_MAX_ALLOCA 0x1000
318 #if !defined (CHILD_XFER_MEMORY)
319 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
320 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
321 it appears that writing did not write the data that I specified. I
322 cannot understand where it got the data that it actually did write. */
324 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
325 debugger memory starting at MYADDR. Copy to inferior if WRITE is
326 nonzero. TARGET is ignored.
328 Returns the length copied, which is either the LEN argument or
329 zero. This xfer function does not do partial moves, since
330 deprecated_child_ops doesn't allow memory operations to cross below
331 us in the target stack anyway. */
334 child_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int write,
335 struct mem_attrib *attrib, struct target_ops *target)
338 /* Round starting address down to longword boundary. */
339 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
340 /* Round ending address up; get number of longwords that makes. */
341 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
342 / sizeof (PTRACE_TYPE_RET));
343 int alloc = count * sizeof (PTRACE_TYPE_RET);
344 PTRACE_TYPE_RET *buffer;
345 struct cleanup *old_chain = NULL;
348 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
349 that promises to be much more efficient in reading and writing
350 data in the traced process's address space. */
353 struct ptrace_io_desc piod;
355 /* NOTE: We assume that there are no distinct address spaces for
356 instruction and data. */
357 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
358 piod.piod_offs = (void *) memaddr;
359 piod.piod_addr = myaddr;
362 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
364 /* If the PT_IO request is somehow not supported, fallback on
365 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
366 to indicate failure. */
372 /* Return the actual number of bytes read or written. */
373 return piod.piod_len;
378 /* Allocate buffer of that many longwords. */
379 if (len < GDB_MAX_ALLOCA)
381 buffer = (PTRACE_TYPE_RET *) alloca (alloc);
385 buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
386 old_chain = make_cleanup (xfree, buffer);
391 /* Fill start and end extra bytes of buffer with existing memory
393 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
395 /* Need part of initial word -- fetch it. */
396 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
397 (PTRACE_TYPE_ARG3) addr, 0);
400 if (count > 1) /* FIXME, avoid if even boundary. */
403 ptrace (PT_READ_I, PIDGET (inferior_ptid),
405 (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
408 /* Copy data to be written over corresponding part of buffer. */
409 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
412 /* Write the entire buffer. */
413 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
416 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
417 (PTRACE_TYPE_ARG3) addr, buffer[i]);
420 /* Using the appropriate one (I or D) is necessary for
421 Gould NP1, at least. */
423 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
424 (PTRACE_TYPE_ARG3) addr, buffer[i]);
432 /* Read all the longwords. */
433 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
436 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
437 (PTRACE_TYPE_ARG3) addr, 0);
443 /* Copy appropriate bytes out of the buffer. */
445 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
449 if (old_chain != NULL)
450 do_cleanups (old_chain);
456 udot_info (char *dummy1, int dummy2)
458 #if defined (KERNEL_U_SIZE)
459 long udot_off; /* Offset into user struct */
460 int udot_val; /* Value from user struct at udot_off */
461 char mess[128]; /* For messages */
464 if (!target_has_execution)
466 error (_("The program is not being run."));
469 #if !defined (KERNEL_U_SIZE)
471 /* Adding support for this command is easy. Typically you just add a
472 routine, called "kernel_u_size" that returns the size of the user
473 struct, to the appropriate *-nat.c file and then add to the native
474 config file "#define KERNEL_U_SIZE kernel_u_size()" */
475 error (_("Don't know how large ``struct user'' is in this version of gdb."));
479 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
481 if ((udot_off % 24) == 0)
485 printf_filtered ("\n");
487 printf_filtered ("%s:", paddr (udot_off));
489 udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0);
492 sprintf (mess, "\nreading user struct at offset 0x%s",
493 paddr_nz (udot_off));
494 perror_with_name (mess);
496 /* Avoid using nonportable (?) "*" in print specs */
497 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
499 printf_filtered ("\n");
503 #endif /* !defined (CHILD_XFER_MEMORY). */
507 _initialize_infptrace (void)
509 #if !defined (CHILD_XFER_MEMORY)
510 add_info ("udot", udot_info,
511 _("Print contents of kernel ``struct user'' for current child."));