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"
36 #include <sys/types.h>
39 #include <sys/param.h>
40 #include "gdb_dirent.h"
42 #include <sys/ioctl.h>
44 #include "gdb_ptrace.h"
46 #ifdef HAVE_SYS_FILE_H
50 #if !defined (FETCH_INFERIOR_REGISTERS)
51 #include <sys/user.h> /* Probably need to poke the user structure */
52 #endif /* !FETCH_INFERIOR_REGISTERS */
54 #if !defined (CHILD_XFER_MEMORY)
55 static void udot_info (char *, int);
58 void _initialize_infptrace (void);
61 /* This function simply calls ptrace with the given arguments.
62 It exists so that all calls to ptrace are isolated in this
63 machine-dependent file. */
65 call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
72 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
73 request, pid, addr, data);
75 #if defined(PT_SETTRC)
76 /* If the parent can be told to attach to us, try to do it. */
77 if (request == PT_SETTRC)
80 #ifndef PTRACE_TYPE_ARG5
81 pt_status = ptrace (PT_SETTRC, pid, addr, data);
83 /* Deal with HPUX 8.0 braindamage. We never use the
84 calls which require the fifth argument. */
85 pt_status = ptrace (PT_SETTRC, pid, addr, data, 0);
88 perror_with_name ("ptrace");
90 printf (" = %d\n", pt_status);
95 return parent_attach_all (pid, addr, data);
99 #if defined(PT_CONTIN1)
100 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
101 signals. If it's available, use it. */
102 if (request == PT_CONTINUE)
103 request = PT_CONTIN1;
106 #if defined(PT_SINGLE1)
107 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
108 signals. If it's available, use it. */
109 if (request == PT_STEP)
110 request = PT_SINGLE1;
117 #ifndef PTRACE_TYPE_ARG5
118 pt_status = ptrace (request, pid, addr, data);
120 /* Deal with HPUX 8.0 braindamage. We never use the
121 calls which require the fifth argument. */
122 pt_status = ptrace (request, pid, addr, data, 0);
127 printf (" [errno = %d]", errno);
130 printf (" = 0x%x\n", pt_status);
136 #if defined (DEBUG_PTRACE) || defined (PTRACE_TYPE_ARG5)
137 /* For the rest of the file, use an extra level of indirection */
138 /* This lets us breakpoint usefully on call_ptrace. */
139 #define ptrace call_ptrace
142 /* Wait for a process to finish, possibly running a target-specific
143 hook before returning. */
146 ptrace_wait (ptid_t ptid, int *status)
150 wstate = wait (status);
151 target_post_wait (pid_to_ptid (wstate), *status);
155 #ifndef KILL_INFERIOR
160 int pid = PIDGET (inferior_ptid);
165 /* This once used to call "kill" to kill the inferior just in case
166 the inferior was still running. As others have noted in the past
167 (kingdon) there shouldn't be any way to get here if the inferior
168 is still running -- else there's a major problem elsewere in gdb
169 and it needs to be fixed.
171 The kill call causes problems under hpux10, so it's been removed;
172 if this causes problems we'll deal with them as they arise. */
173 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
174 ptrace_wait (null_ptid, &status);
175 target_mourn_inferior ();
177 #endif /* KILL_INFERIOR */
181 /* Resume execution of the inferior process.
182 If STEP is nonzero, single-step it.
183 If SIGNAL is nonzero, give it that signal. */
186 child_resume (ptid_t ptid, int step, enum target_signal signal)
188 int request = PT_CONTINUE;
189 int pid = PIDGET (ptid);
192 /* Resume all threads. */
193 /* I think this only gets used in the non-threaded case, where "resume
194 all threads" and "resume inferior_ptid" are the same. */
195 pid = PIDGET (inferior_ptid);
199 /* If this system does not support PT_STEP, a higher level
200 function will have called single_step() to transmute the step
201 request into a continue request (by setting breakpoints on
202 all possible successor instructions), so we don't have to
203 worry about that here. */
205 gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
209 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
210 where it was. If GDB wanted it to start some other way, we have
211 already written a new PC value to the child. */
214 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
216 perror_with_name ("ptrace");
218 #endif /* CHILD_RESUME */
221 /* Start debugging the process whose number is PID. */
228 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
230 perror_with_name ("ptrace");
234 error ("This system does not support attaching to a process");
238 /* Stop debugging the process whose number is PID and continue it with
239 signal number SIGNAL. SIGNAL = 0 means just continue it. */
245 int pid = PIDGET (inferior_ptid);
248 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal);
250 perror_with_name ("ptrace");
253 error ("This system does not support detaching from a process");
258 #ifndef FETCH_INFERIOR_REGISTERS
260 /* U_REGS_OFFSET is the offset of the registers within the u area. */
261 #ifndef U_REGS_OFFSET
264 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
267 #define U_REGS_OFFSET \
268 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
269 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
273 /* Fetch register REGNUM from the inferior. */
276 fetch_register (int regnum)
280 PTRACE_TYPE_RET *buf;
283 if (CANNOT_FETCH_REGISTER (regnum))
285 regcache_raw_supply (current_regcache, regnum, NULL);
289 /* GNU/Linux LWP ID's are process ID's. */
290 tid = TIDGET (inferior_ptid);
292 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
294 /* This isn't really an address. But ptrace thinks of it as one. */
295 addr = register_addr (regnum, U_REGS_OFFSET);
296 size = register_size (current_gdbarch, regnum);
298 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
301 /* Read the register contents from the inferior a chuck at the time. */
302 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
305 buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0);
307 error ("Couldn't read register %s (#%d): %s.", REGISTER_NAME (regnum),
308 regnum, safe_strerror (errno));
310 addr += sizeof (PTRACE_TYPE_RET);
312 regcache_raw_supply (current_regcache, regnum, buf);
315 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
316 for all registers. */
319 fetch_inferior_registers (int regnum)
322 for (regnum = 0; regnum < NUM_REGS; regnum++)
323 fetch_register (regnum);
325 fetch_register (regnum);
328 /* Store register REGNUM into the inferior. */
331 store_register (int regnum)
335 PTRACE_TYPE_RET *buf;
338 if (CANNOT_STORE_REGISTER (regnum))
341 /* GNU/Linux LWP ID's are process ID's. */
342 tid = TIDGET (inferior_ptid);
344 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
346 /* This isn't really an address. But ptrace thinks of it as one. */
347 addr = register_addr (regnum, U_REGS_OFFSET);
348 size = register_size (current_gdbarch, regnum);
350 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
353 /* Write the register contents into the inferior a chunk at the time. */
354 regcache_raw_collect (current_regcache, regnum, buf);
355 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
358 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]);
360 error ("Couldn't write register %s (#%d): %s.", REGISTER_NAME (regnum),
361 regnum, safe_strerror (errno));
363 addr += sizeof (PTRACE_TYPE_RET);
367 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
368 this for all registers (including the floating point registers). */
371 store_inferior_registers (int regnum)
374 for (regnum = 0; regnum < NUM_REGS; regnum++)
375 store_register (regnum);
377 store_register (regnum);
380 #endif /* not FETCH_INFERIOR_REGISTERS. */
383 /* Set an upper limit on alloca. */
384 #ifndef GDB_MAX_ALLOCA
385 #define GDB_MAX_ALLOCA 0x1000
388 #if !defined (CHILD_XFER_MEMORY)
389 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
390 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
391 it appears that writing did not write the data that I specified. I
392 cannot understand where it got the data that it actually did write. */
394 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
395 debugger memory starting at MYADDR. Copy to inferior if WRITE is
396 nonzero. TARGET is ignored.
398 Returns the length copied, which is either the LEN argument or
399 zero. This xfer function does not do partial moves, since
400 child_ops doesn't allow memory operations to cross below us in the
401 target stack anyway. */
404 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
405 struct mem_attrib *attrib, struct target_ops *target)
408 /* Round starting address down to longword boundary. */
409 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
410 /* Round ending address up; get number of longwords that makes. */
411 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
412 / sizeof (PTRACE_TYPE_RET));
413 int alloc = count * sizeof (PTRACE_TYPE_RET);
414 PTRACE_TYPE_RET *buffer;
415 struct cleanup *old_chain = NULL;
418 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
419 that promises to be much more efficient in reading and writing
420 data in the traced process's address space. */
423 struct ptrace_io_desc piod;
425 /* NOTE: We assume that there are no distinct address spaces for
426 instruction and data. */
427 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
428 piod.piod_offs = (void *) memaddr;
429 piod.piod_addr = myaddr;
432 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
434 /* If the PT_IO request is somehow not supported, fallback on
435 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
436 to indicate failure. */
442 /* Return the actual number of bytes read or written. */
443 return piod.piod_len;
448 /* Allocate buffer of that many longwords. */
449 if (len < GDB_MAX_ALLOCA)
451 buffer = (PTRACE_TYPE_RET *) alloca (alloc);
455 buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
456 old_chain = make_cleanup (xfree, buffer);
461 /* Fill start and end extra bytes of buffer with existing memory
463 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
465 /* Need part of initial word -- fetch it. */
466 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
467 (PTRACE_TYPE_ARG3) addr, 0);
470 if (count > 1) /* FIXME, avoid if even boundary. */
473 ptrace (PT_READ_I, PIDGET (inferior_ptid),
475 (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
478 /* Copy data to be written over corresponding part of buffer. */
479 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
482 /* Write the entire buffer. */
483 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
486 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
487 (PTRACE_TYPE_ARG3) addr, buffer[i]);
490 /* Using the appropriate one (I or D) is necessary for
491 Gould NP1, at least. */
493 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
494 (PTRACE_TYPE_ARG3) addr, buffer[i]);
502 /* Read all the longwords. */
503 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
506 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
507 (PTRACE_TYPE_ARG3) addr, 0);
513 /* Copy appropriate bytes out of the buffer. */
515 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
519 if (old_chain != NULL)
520 do_cleanups (old_chain);
526 udot_info (char *dummy1, int dummy2)
528 #if defined (KERNEL_U_SIZE)
529 long udot_off; /* Offset into user struct */
530 int udot_val; /* Value from user struct at udot_off */
531 char mess[128]; /* For messages */
534 if (!target_has_execution)
536 error ("The program is not being run.");
539 #if !defined (KERNEL_U_SIZE)
541 /* Adding support for this command is easy. Typically you just add a
542 routine, called "kernel_u_size" that returns the size of the user
543 struct, to the appropriate *-nat.c file and then add to the native
544 config file "#define KERNEL_U_SIZE kernel_u_size()" */
545 error ("Don't know how large ``struct user'' is in this version of gdb.");
549 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
551 if ((udot_off % 24) == 0)
555 printf_filtered ("\n");
557 printf_filtered ("%s:", paddr (udot_off));
559 udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0);
562 sprintf (mess, "\nreading user struct at offset 0x%s",
563 paddr_nz (udot_off));
564 perror_with_name (mess);
566 /* Avoid using nonportable (?) "*" in print specs */
567 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
569 printf_filtered ("\n");
573 #endif /* !defined (CHILD_XFER_MEMORY). */
577 _initialize_infptrace (void)
579 #if !defined (CHILD_XFER_MEMORY)
580 add_info ("udot", udot_info,
581 "Print contents of kernel ``struct user'' for current child.");