1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 89, 90, 91, 92, 93, 94, 95, 96, 1998
3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
30 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/ioctl.h>
40 # ifdef HAVE_SYS_PTRACE_H
41 # include <sys/ptrace.h>
45 #if !defined (PT_READ_I)
46 #define PT_READ_I 1 /* Read word from text space */
48 #if !defined (PT_READ_D)
49 #define PT_READ_D 2 /* Read word from data space */
51 #if !defined (PT_READ_U)
52 #define PT_READ_U 3 /* Read word from kernel user struct */
54 #if !defined (PT_WRITE_I)
55 #define PT_WRITE_I 4 /* Write word to text space */
57 #if !defined (PT_WRITE_D)
58 #define PT_WRITE_D 5 /* Write word to data space */
60 #if !defined (PT_WRITE_U)
61 #define PT_WRITE_U 6 /* Write word to kernel user struct */
63 #if !defined (PT_CONTINUE)
64 #define PT_CONTINUE 7 /* Continue after signal */
66 #if !defined (PT_STEP)
67 #define PT_STEP 9 /* Set flag for single stepping */
69 #if !defined (PT_KILL)
70 #define PT_KILL 8 /* Send child a SIGKILL signal */
74 #define PT_ATTACH PTRACE_ATTACH
77 #define PT_DETACH PTRACE_DETACH
85 /* Don't think this is used anymore. On the sequent (not sure whether it's
86 dynix or ptx or both), it is included unconditionally by sys/user.h and
87 not protected against multiple inclusion. */
91 #if !defined (FETCH_INFERIOR_REGISTERS)
92 #include <sys/user.h> /* Probably need to poke the user structure */
93 #if defined (KERNEL_U_ADDR_BSD)
94 #include <a.out.h> /* For struct nlist */
95 #endif /* KERNEL_U_ADDR_BSD. */
96 #endif /* !FETCH_INFERIOR_REGISTERS */
98 #if !defined (CHILD_XFER_MEMORY)
99 static void udot_info PARAMS ((char *, int));
102 #if !defined (FETCH_INFERIOR_REGISTERS)
103 static void fetch_register PARAMS ((int));
104 static void store_register PARAMS ((int));
107 void _initialize_kernel_u_addr PARAMS ((void));
108 void _initialize_infptrace PARAMS ((void));
111 /* This function simply calls ptrace with the given arguments.
112 It exists so that all calls to ptrace are isolated in this
113 machine-dependent file. */
115 call_ptrace (request, pid, addr, data)
117 PTRACE_ARG3_TYPE addr;
125 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
126 request, pid, addr, data);
128 #if defined(PT_SETTRC)
129 /* If the parent can be told to attach to us, try to do it. */
130 if (request == PT_SETTRC) {
132 pt_status = ptrace (PT_SETTRC, pid, addr, data
133 #if defined (FIVE_ARG_PTRACE)
134 /* Deal with HPUX 8.0 braindamage. We never use the
135 calls which require the fifth argument. */
140 if (errno) perror_with_name ("ptrace");
142 printf (" = %d\n", pt_status);
147 return parent_attach_all (pid, addr, data);
151 #if defined(PT_CONTIN1)
152 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
153 signals. If it's available, use it. */
154 if (request == PT_CONTINUE)
155 request = PT_CONTIN1;
158 #if defined(PT_SINGLE1)
159 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
160 signals. If it's available, use it. */
161 if (request == PT_STEP)
162 request = PT_SINGLE1;
169 pt_status = ptrace (request, pid, addr, data
170 #if defined (FIVE_ARG_PTRACE)
171 /* Deal with HPUX 8.0 braindamage. We never use the
172 calls which require the fifth argument. */
178 printf (" [errno = %d]", errno);
181 printf (" = 0x%x\n", pt_status);
187 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
188 /* For the rest of the file, use an extra level of indirection */
189 /* This lets us breakpoint usefully on call_ptrace. */
190 #define ptrace call_ptrace
193 /* Wait for a process to finish, possibly running a target-specific
194 hook before returning. */
197 ptrace_wait (pid, status)
203 wstate = wait (status);
204 target_post_wait (wstate, *status);
213 if (inferior_pid == 0)
216 /* This once used to call "kill" to kill the inferior just in case
217 the inferior was still running. As others have noted in the past
218 (kingdon) there shouldn't be any way to get here if the inferior
219 is still running -- else there's a major problem elsewere in gdb
220 and it needs to be fixed.
222 The kill call causes problems under hpux10, so it's been removed;
223 if this causes problems we'll deal with them as they arise. */
224 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
225 ptrace_wait (0, &status);
226 target_mourn_inferior ();
231 /* Resume execution of the inferior process.
232 If STEP is nonzero, single-step it.
233 If SIGNAL is nonzero, give it that signal. */
236 child_resume (pid, step, signal)
239 enum target_signal signal;
244 /* Resume all threads. */
245 /* I think this only gets used in the non-threaded case, where "resume
246 all threads" and "resume inferior_pid" are the same. */
249 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
250 it was. (If GDB wanted it to start some other way, we have already
251 written a new PC value to the child.)
253 If this system does not support PT_STEP, a higher level function will
254 have called single_step() to transmute the step request into a
255 continue request (by setting breakpoints on all possible successor
256 instructions), so we don't have to worry about that here. */
260 if (SOFTWARE_SINGLE_STEP_P)
261 abort(); /* Make sure this doesn't happen. */
263 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
264 target_signal_to_host (signal));
267 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
268 target_signal_to_host (signal));
271 perror_with_name ("ptrace");
273 #endif /* CHILD_RESUME */
277 /* Start debugging the process whose number is PID. */
283 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
285 perror_with_name ("ptrace");
290 /* Stop debugging the process whose number is PID
291 and continue it with signal number SIGNAL.
292 SIGNAL = 0 means just continue it. */
299 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
301 perror_with_name ("ptrace");
304 #endif /* ATTACH_DETACH */
306 /* Default the type of the ptrace transfer to int. */
307 #ifndef PTRACE_XFER_TYPE
308 #define PTRACE_XFER_TYPE int
311 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
312 to get the offset in the core file of the register values. */
313 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
314 /* Get kernel_u_addr using BSD-style nlist(). */
315 CORE_ADDR kernel_u_addr;
316 #endif /* KERNEL_U_ADDR_BSD. */
319 _initialize_kernel_u_addr ()
321 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
322 struct nlist names[2];
324 names[0].n_un.n_name = "_u";
325 names[1].n_un.n_name = NULL;
326 if (nlist ("/vmunix", names) == 0)
327 kernel_u_addr = names[0].n_value;
329 fatal ("Unable to get kernel u area address.");
330 #endif /* KERNEL_U_ADDR_BSD. */
333 #if !defined (FETCH_INFERIOR_REGISTERS)
335 #if !defined (offsetof)
336 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
339 /* U_REGS_OFFSET is the offset of the registers within the u area. */
340 #if !defined (U_REGS_OFFSET)
341 #define U_REGS_OFFSET \
342 ptrace (PT_READ_U, inferior_pid, \
343 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
347 /* Registers we shouldn't try to fetch. */
348 #if !defined (CANNOT_FETCH_REGISTER)
349 #define CANNOT_FETCH_REGISTER(regno) 0
352 /* Fetch one register. */
355 fetch_register (regno)
358 /* This isn't really an address. But ptrace thinks of it as one. */
360 char mess[128]; /* For messages */
362 unsigned int offset; /* Offset of registers within the u area. */
363 char buf[MAX_REGISTER_RAW_SIZE];
365 if (CANNOT_FETCH_REGISTER (regno))
367 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
368 supply_register (regno, buf);
372 offset = U_REGS_OFFSET;
374 regaddr = register_addr (regno, offset);
375 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
378 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
379 (PTRACE_ARG3_TYPE) regaddr, 0);
380 regaddr += sizeof (PTRACE_XFER_TYPE);
383 sprintf (mess, "reading register %s (#%d)", REGISTER_NAME (regno), regno);
384 perror_with_name (mess);
387 supply_register (regno, buf);
391 /* Fetch register values from the inferior.
392 If REGNO is negative, do this for all registers.
393 Otherwise, REGNO specifies which register (so we can save time). */
396 fetch_inferior_registers (regno)
401 fetch_register (regno);
405 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
407 fetch_register (regno);
412 /* Registers we shouldn't try to store. */
413 #if !defined (CANNOT_STORE_REGISTER)
414 #define CANNOT_STORE_REGISTER(regno) 0
417 /* Store one register. */
420 store_register (regno)
423 /* This isn't really an address. But ptrace thinks of it as one. */
425 char mess[128]; /* For messages */
427 unsigned int offset; /* Offset of registers within the u area. */
429 if (CANNOT_STORE_REGISTER (regno))
434 offset = U_REGS_OFFSET;
436 regaddr = register_addr (regno, offset);
437 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
440 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
441 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
442 regaddr += sizeof (PTRACE_XFER_TYPE);
445 sprintf (mess, "writing register %s (#%d)", REGISTER_NAME (regno), regno);
446 perror_with_name (mess);
451 /* Store our register values back into the inferior.
452 If REGNO is negative, do this for all registers.
453 Otherwise, REGNO specifies which register (so we can save time). */
456 store_inferior_registers (regno)
461 store_register (regno);
465 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
467 store_register (regno);
471 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
474 #if !defined (CHILD_XFER_MEMORY)
475 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
476 in the NEW_SUN_PTRACE case.
477 It ought to be straightforward. But it appears that writing did
478 not write the data that I specified. I cannot understand where
479 it got the data that it actually did write. */
481 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
482 to debugger memory starting at MYADDR. Copy to inferior if
485 Returns the length copied, which is either the LEN argument or zero.
486 This xfer function does not do partial moves, since child_ops
487 doesn't allow memory operations to cross below us in the target stack
491 child_xfer_memory (memaddr, myaddr, len, write, target)
496 struct target_ops *target; /* ignored */
499 /* Round starting address down to longword boundary. */
500 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
501 /* Round ending address up; get number of longwords that makes. */
503 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
504 / sizeof (PTRACE_XFER_TYPE);
505 /* Allocate buffer of that many longwords. */
506 register PTRACE_XFER_TYPE *buffer
507 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
511 /* Fill start and end extra bytes of buffer with existing memory data. */
513 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
514 /* Need part of initial word -- fetch it. */
515 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
519 if (count > 1) /* FIXME, avoid if even boundary */
522 = ptrace (PT_READ_I, inferior_pid,
524 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
528 /* Copy data to be written over corresponding part of buffer */
530 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
534 /* Write the entire buffer. */
536 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
539 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
543 /* Using the appropriate one (I or D) is necessary for
544 Gould NP1, at least. */
546 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
552 #ifdef CLEAR_INSN_CACHE
558 /* Read all the longwords */
559 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
562 buffer[i] = ptrace (PT_READ_I, inferior_pid,
563 (PTRACE_ARG3_TYPE) addr, 0);
569 /* Copy appropriate bytes out of the buffer. */
571 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
579 udot_info (dummy1, dummy2)
583 #if defined (KERNEL_U_SIZE)
584 int udot_off; /* Offset into user struct */
585 int udot_val; /* Value from user struct at udot_off */
586 char mess[128]; /* For messages */
589 if (!target_has_execution)
591 error ("The program is not being run.");
594 #if !defined (KERNEL_U_SIZE)
596 /* Adding support for this command is easy. Typically you just add a
597 routine, called "kernel_u_size" that returns the size of the user
598 struct, to the appropriate *-nat.c file and then add to the native
599 config file "#define KERNEL_U_SIZE kernel_u_size()" */
600 error ("Don't know how large ``struct user'' is in this version of gdb.");
604 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
606 if ((udot_off % 24) == 0)
610 printf_filtered ("\n");
612 printf_filtered ("%04x:", udot_off);
614 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
617 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
618 perror_with_name (mess);
620 /* Avoid using nonportable (?) "*" in print specs */
621 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
623 printf_filtered ("\n");
627 #endif /* !defined (CHILD_XFER_MEMORY). */
631 _initialize_infptrace ()
633 #if !defined (CHILD_XFER_MEMORY)
634 add_info ("udot", udot_info,
635 "Print contents of kernel ``struct user'' for current child.");