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>
36 #include <sys/ioctl.h>
41 # ifdef HAVE_SYS_PTRACE_H
42 # include <sys/ptrace.h>
46 #if !defined (PT_READ_I)
47 #define PT_READ_I 1 /* Read word from text space */
49 #if !defined (PT_READ_D)
50 #define PT_READ_D 2 /* Read word from data space */
52 #if !defined (PT_READ_U)
53 #define PT_READ_U 3 /* Read word from kernel user struct */
55 #if !defined (PT_WRITE_I)
56 #define PT_WRITE_I 4 /* Write word to text space */
58 #if !defined (PT_WRITE_D)
59 #define PT_WRITE_D 5 /* Write word to data space */
61 #if !defined (PT_WRITE_U)
62 #define PT_WRITE_U 6 /* Write word to kernel user struct */
64 #if !defined (PT_CONTINUE)
65 #define PT_CONTINUE 7 /* Continue after signal */
67 #if !defined (PT_STEP)
68 #define PT_STEP 9 /* Set flag for single stepping */
70 #if !defined (PT_KILL)
71 #define PT_KILL 8 /* Send child a SIGKILL signal */
75 #define PT_ATTACH PTRACE_ATTACH
78 #define PT_DETACH PTRACE_DETACH
86 /* Don't think this is used anymore. On the sequent (not sure whether it's
87 dynix or ptx or both), it is included unconditionally by sys/user.h and
88 not protected against multiple inclusion. */
92 #if !defined (FETCH_INFERIOR_REGISTERS)
93 #include <sys/user.h> /* Probably need to poke the user structure */
94 #if defined (KERNEL_U_ADDR_BSD)
95 #include <a.out.h> /* For struct nlist */
96 #endif /* KERNEL_U_ADDR_BSD. */
97 #endif /* !FETCH_INFERIOR_REGISTERS */
99 #if !defined (CHILD_XFER_MEMORY)
100 static void udot_info PARAMS ((char *, int));
103 #if !defined (FETCH_INFERIOR_REGISTERS)
104 static void fetch_register PARAMS ((int));
105 static void store_register PARAMS ((int));
108 void _initialize_kernel_u_addr PARAMS ((void));
109 void _initialize_infptrace PARAMS ((void));
112 /* This function simply calls ptrace with the given arguments.
113 It exists so that all calls to ptrace are isolated in this
114 machine-dependent file. */
116 call_ptrace (request, pid, addr, data)
118 PTRACE_ARG3_TYPE addr;
126 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
127 request, pid, addr, data);
129 #if defined(PT_SETTRC)
130 /* If the parent can be told to attach to us, try to do it. */
131 if (request == PT_SETTRC) {
133 pt_status = ptrace (PT_SETTRC, pid, addr, data
134 #if defined (FIVE_ARG_PTRACE)
135 /* Deal with HPUX 8.0 braindamage. We never use the
136 calls which require the fifth argument. */
141 if (errno) perror_with_name ("ptrace");
143 printf (" = %d\n", pt_status);
148 return parent_attach_all (pid, addr, data);
152 #if defined(PT_CONTIN1)
153 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
154 signals. If it's available, use it. */
155 if (request == PT_CONTINUE)
156 request = PT_CONTIN1;
159 #if defined(PT_SINGLE1)
160 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
161 signals. If it's available, use it. */
162 if (request == PT_STEP)
163 request = PT_SINGLE1;
170 pt_status = ptrace (request, pid, addr, data
171 #if defined (FIVE_ARG_PTRACE)
172 /* Deal with HPUX 8.0 braindamage. We never use the
173 calls which require the fifth argument. */
179 printf (" [errno = %d]", errno);
182 printf (" = 0x%x\n", pt_status);
188 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
189 /* For the rest of the file, use an extra level of indirection */
190 /* This lets us breakpoint usefully on call_ptrace. */
191 #define ptrace call_ptrace
194 /* Wait for a process to finish, possibly running a target-specific
195 hook before returning. */
198 ptrace_wait (pid, status)
204 wstate = wait (status);
205 target_post_wait (wstate, *status);
214 if (inferior_pid == 0)
217 /* This once used to call "kill" to kill the inferior just in case
218 the inferior was still running. As others have noted in the past
219 (kingdon) there shouldn't be any way to get here if the inferior
220 is still running -- else there's a major problem elsewere in gdb
221 and it needs to be fixed.
223 The kill call causes problems under hpux10, so it's been removed;
224 if this causes problems we'll deal with them as they arise. */
225 ptrace (PT_KILL, inferior_pid, (PTRACE_ARG3_TYPE) 0, 0);
226 ptrace_wait (0, &status);
227 target_mourn_inferior ();
232 /* Resume execution of the inferior process.
233 If STEP is nonzero, single-step it.
234 If SIGNAL is nonzero, give it that signal. */
237 child_resume (pid, step, signal)
240 enum target_signal signal;
245 /* Resume all threads. */
246 /* I think this only gets used in the non-threaded case, where "resume
247 all threads" and "resume inferior_pid" are the same. */
250 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
251 it was. (If GDB wanted it to start some other way, we have already
252 written a new PC value to the child.)
254 If this system does not support PT_STEP, a higher level function will
255 have called single_step() to transmute the step request into a
256 continue request (by setting breakpoints on all possible successor
257 instructions), so we don't have to worry about that here. */
261 if (SOFTWARE_SINGLE_STEP_P)
262 abort(); /* Make sure this doesn't happen. */
264 ptrace (PT_STEP, pid, (PTRACE_ARG3_TYPE) 1,
265 target_signal_to_host (signal));
268 ptrace (PT_CONTINUE, pid, (PTRACE_ARG3_TYPE) 1,
269 target_signal_to_host (signal));
272 perror_with_name ("ptrace");
274 #endif /* CHILD_RESUME */
278 /* Start debugging the process whose number is PID. */
284 ptrace (PT_ATTACH, pid, (PTRACE_ARG3_TYPE) 0, 0);
286 perror_with_name ("ptrace");
291 /* Stop debugging the process whose number is PID
292 and continue it with signal number SIGNAL.
293 SIGNAL = 0 means just continue it. */
300 ptrace (PT_DETACH, inferior_pid, (PTRACE_ARG3_TYPE) 1, signal);
302 perror_with_name ("ptrace");
305 #endif /* ATTACH_DETACH */
307 /* Default the type of the ptrace transfer to int. */
308 #ifndef PTRACE_XFER_TYPE
309 #define PTRACE_XFER_TYPE int
312 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
313 to get the offset in the core file of the register values. */
314 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
315 /* Get kernel_u_addr using BSD-style nlist(). */
316 CORE_ADDR kernel_u_addr;
317 #endif /* KERNEL_U_ADDR_BSD. */
320 _initialize_kernel_u_addr ()
322 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
323 struct nlist names[2];
325 names[0].n_un.n_name = "_u";
326 names[1].n_un.n_name = NULL;
327 if (nlist ("/vmunix", names) == 0)
328 kernel_u_addr = names[0].n_value;
330 fatal ("Unable to get kernel u area address.");
331 #endif /* KERNEL_U_ADDR_BSD. */
334 #if !defined (FETCH_INFERIOR_REGISTERS)
336 #if !defined (offsetof)
337 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
340 /* U_REGS_OFFSET is the offset of the registers within the u area. */
341 #if !defined (U_REGS_OFFSET)
342 #define U_REGS_OFFSET \
343 ptrace (PT_READ_U, inferior_pid, \
344 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
348 /* Registers we shouldn't try to fetch. */
349 #if !defined (CANNOT_FETCH_REGISTER)
350 #define CANNOT_FETCH_REGISTER(regno) 0
353 /* Fetch one register. */
356 fetch_register (regno)
359 /* This isn't really an address. But ptrace thinks of it as one. */
361 char mess[128]; /* For messages */
363 unsigned int offset; /* Offset of registers within the u area. */
364 char buf[MAX_REGISTER_RAW_SIZE];
366 if (CANNOT_FETCH_REGISTER (regno))
368 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
369 supply_register (regno, buf);
373 offset = U_REGS_OFFSET;
375 regaddr = register_addr (regno, offset);
376 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
379 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, inferior_pid,
380 (PTRACE_ARG3_TYPE) regaddr, 0);
381 regaddr += sizeof (PTRACE_XFER_TYPE);
384 sprintf (mess, "reading register %s (#%d)", REGISTER_NAME (regno), regno);
385 perror_with_name (mess);
388 supply_register (regno, buf);
392 /* Fetch register values from the inferior.
393 If REGNO is negative, do this for all registers.
394 Otherwise, REGNO specifies which register (so we can save time). */
397 fetch_inferior_registers (regno)
402 fetch_register (regno);
406 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
408 fetch_register (regno);
413 /* Registers we shouldn't try to store. */
414 #if !defined (CANNOT_STORE_REGISTER)
415 #define CANNOT_STORE_REGISTER(regno) 0
418 /* Store one register. */
421 store_register (regno)
424 /* This isn't really an address. But ptrace thinks of it as one. */
426 char mess[128]; /* For messages */
428 unsigned int offset; /* Offset of registers within the u area. */
430 if (CANNOT_STORE_REGISTER (regno))
435 offset = U_REGS_OFFSET;
437 regaddr = register_addr (regno, offset);
438 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(PTRACE_XFER_TYPE))
441 ptrace (PT_WRITE_U, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
442 *(PTRACE_XFER_TYPE *) ®isters[REGISTER_BYTE (regno) + i]);
443 regaddr += sizeof (PTRACE_XFER_TYPE);
446 sprintf (mess, "writing register %s (#%d)", REGISTER_NAME (regno), regno);
447 perror_with_name (mess);
452 /* Store our register values back into the inferior.
453 If REGNO is negative, do this for all registers.
454 Otherwise, REGNO specifies which register (so we can save time). */
457 store_inferior_registers (regno)
462 store_register (regno);
466 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
468 store_register (regno);
472 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
475 #if !defined (CHILD_XFER_MEMORY)
476 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
477 in the NEW_SUN_PTRACE case.
478 It ought to be straightforward. But it appears that writing did
479 not write the data that I specified. I cannot understand where
480 it got the data that it actually did write. */
482 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
483 to debugger memory starting at MYADDR. Copy to inferior if
486 Returns the length copied, which is either the LEN argument or zero.
487 This xfer function does not do partial moves, since child_ops
488 doesn't allow memory operations to cross below us in the target stack
492 child_xfer_memory (memaddr, myaddr, len, write, target)
497 struct target_ops *target; /* ignored */
500 /* Round starting address down to longword boundary. */
501 register CORE_ADDR addr = memaddr & - sizeof (PTRACE_XFER_TYPE);
502 /* Round ending address up; get number of longwords that makes. */
504 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
505 / sizeof (PTRACE_XFER_TYPE);
506 /* Allocate buffer of that many longwords. */
507 register PTRACE_XFER_TYPE *buffer
508 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
512 /* Fill start and end extra bytes of buffer with existing memory data. */
514 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) {
515 /* Need part of initial word -- fetch it. */
516 buffer[0] = ptrace (PT_READ_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
520 if (count > 1) /* FIXME, avoid if even boundary */
523 = ptrace (PT_READ_I, inferior_pid,
525 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
529 /* Copy data to be written over corresponding part of buffer */
531 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
535 /* Write the entire buffer. */
537 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
540 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) addr,
544 /* Using the appropriate one (I or D) is necessary for
545 Gould NP1, at least. */
547 ptrace (PT_WRITE_I, inferior_pid, (PTRACE_ARG3_TYPE) addr,
553 #ifdef CLEAR_INSN_CACHE
559 /* Read all the longwords */
560 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
563 buffer[i] = ptrace (PT_READ_I, inferior_pid,
564 (PTRACE_ARG3_TYPE) addr, 0);
570 /* Copy appropriate bytes out of the buffer. */
572 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
580 udot_info (dummy1, dummy2)
584 #if defined (KERNEL_U_SIZE)
585 int udot_off; /* Offset into user struct */
586 int udot_val; /* Value from user struct at udot_off */
587 char mess[128]; /* For messages */
590 if (!target_has_execution)
592 error ("The program is not being run.");
595 #if !defined (KERNEL_U_SIZE)
597 /* Adding support for this command is easy. Typically you just add a
598 routine, called "kernel_u_size" that returns the size of the user
599 struct, to the appropriate *-nat.c file and then add to the native
600 config file "#define KERNEL_U_SIZE kernel_u_size()" */
601 error ("Don't know how large ``struct user'' is in this version of gdb.");
605 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
607 if ((udot_off % 24) == 0)
611 printf_filtered ("\n");
613 printf_filtered ("%04x:", udot_off);
615 udot_val = ptrace (PT_READ_U, inferior_pid, (PTRACE_ARG3_TYPE) udot_off, 0);
618 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
619 perror_with_name (mess);
621 /* Avoid using nonportable (?) "*" in print specs */
622 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
624 printf_filtered ("\n");
628 #endif /* !defined (CHILD_XFER_MEMORY). */
632 _initialize_infptrace ()
634 #if !defined (CHILD_XFER_MEMORY)
635 add_info ("udot", udot_info,
636 "Print contents of kernel ``struct user'' for current child.");