1 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
2 Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <sys/types.h>
29 #include <sys/param.h>
32 #include <sys/ioctl.h>
35 #ifdef PTRACE_IN_WRONG_PLACE
38 #include <sys/ptrace.h>
40 #endif /* NO_PTRACE_H */
42 #if !defined (PT_KILL)
46 #if !defined (PT_STEP)
55 #endif /* No PT_STEP. */
58 #define PT_ATTACH PTRACE_ATTACH
61 #define PT_DETACH PTRACE_DETACH
69 /* Don't think this is used anymore. On the sequent (not sure whether it's
70 dynix or ptx or both), it is included unconditionally by sys/user.h and
71 not protected against multiple inclusion. */
75 #if !defined (FETCH_INFERIOR_REGISTERS)
76 #include <sys/user.h> /* Probably need to poke the user structure */
77 #if defined (KERNEL_U_ADDR_BSD)
78 #include <a.out.h> /* For struct nlist */
79 #endif /* KERNEL_U_ADDR_BSD. */
80 #endif /* !FETCH_INFERIOR_REGISTERS */
83 /* This function simply calls ptrace with the given arguments.
84 It exists so that all calls to ptrace are isolated in this
85 machine-dependent file. */
87 call_ptrace (request
, pid
, addr
, data
)
89 PTRACE_ARG3_TYPE addr
;
92 return ptrace (request
, pid
, addr
, data
93 #if defined (FIVE_ARG_PTRACE)
94 /* Deal with HPUX 8.0 braindamage. We never use the
95 calls which require the fifth argument. */
101 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
102 /* For the rest of the file, use an extra level of indirection */
103 /* This lets us breakpoint usefully on call_ptrace. */
104 #define ptrace call_ptrace
110 if (inferior_pid
== 0)
112 /* ptrace PT_KILL only works if process is stopped!!! So stop it with
113 a real signal first, if we can. */
114 kill (inferior_pid
, SIGKILL
);
115 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
117 target_mourn_inferior ();
120 /* Resume execution of the inferior process.
121 If STEP is nonzero, single-step it.
122 If SIGNAL is nonzero, give it that signal. */
125 child_resume (pid
, step
, signal
)
133 #ifdef PIDGET /* XXX Lynx */
134 pid
= PIDGET (inferior_pid
);
139 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
140 it was. (If GDB wanted it to start some other way, we have already
141 written a new PC value to the child.)
143 If this system does not support PT_STEP, a higher level function will
144 have called single_step() to transmute the step request into a
145 continue request (by setting breakpoints on all possible successor
146 instructions), so we don't have to worry about that here. */
149 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
151 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
154 perror_with_name ("ptrace");
158 /* Start debugging the process whose number is PID. */
164 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
166 perror_with_name ("ptrace");
171 /* Stop debugging the process whose number is PID
172 and continue it with signal number SIGNAL.
173 SIGNAL = 0 means just continue it. */
180 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
182 perror_with_name ("ptrace");
185 #endif /* ATTACH_DETACH */
187 /* Default the type of the ptrace transfer to int. */
188 #ifndef PTRACE_XFER_TYPE
189 #define PTRACE_XFER_TYPE int
192 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
193 to get the offset in the core file of the register values. */
194 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
195 /* Get kernel_u_addr using BSD-style nlist(). */
196 CORE_ADDR kernel_u_addr
;
197 #endif /* KERNEL_U_ADDR_BSD. */
200 _initialize_kernel_u_addr ()
202 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
203 struct nlist names
[2];
205 names
[0].n_un
.n_name
= "_u";
206 names
[1].n_un
.n_name
= NULL
;
207 if (nlist ("/vmunix", names
) == 0)
208 kernel_u_addr
= names
[0].n_value
;
210 fatal ("Unable to get kernel u area address.");
211 #endif /* KERNEL_U_ADDR_BSD. */
214 #if !defined (FETCH_INFERIOR_REGISTERS)
216 #if !defined (offsetof)
217 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
220 /* U_REGS_OFFSET is the offset of the registers within the u area. */
221 #if !defined (U_REGS_OFFSET)
222 #define U_REGS_OFFSET \
223 ptrace (PT_READ_U, inferior_pid, \
224 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
228 /* Registers we shouldn't try to fetch. */
229 #if !defined (CANNOT_FETCH_REGISTER)
230 #define CANNOT_FETCH_REGISTER(regno) 0
233 /* Fetch one register. */
236 fetch_register (regno
)
239 register unsigned int regaddr
;
240 char buf
[MAX_REGISTER_RAW_SIZE
];
241 char mess
[128]; /* For messages */
244 /* Offset of registers within the u area. */
247 if (CANNOT_FETCH_REGISTER (regno
))
249 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
250 supply_register (regno
, buf
);
254 offset
= U_REGS_OFFSET
;
256 regaddr
= register_addr (regno
, offset
);
257 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
260 *(PTRACE_XFER_TYPE
*) &buf
[i
] = ptrace (PT_READ_U
, inferior_pid
,
261 (PTRACE_ARG3_TYPE
) regaddr
, 0);
262 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
265 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
266 perror_with_name (mess
);
269 supply_register (regno
, buf
);
273 /* Fetch all registers, or just one, from the child process. */
276 fetch_inferior_registers (regno
)
280 for (regno
= 0; regno
< NUM_REGS
; regno
++)
281 fetch_register (regno
);
283 fetch_register (regno
);
286 /* Registers we shouldn't try to store. */
287 #if !defined (CANNOT_STORE_REGISTER)
288 #define CANNOT_STORE_REGISTER(regno) 0
291 /* Store our register values back into the inferior.
292 If REGNO is -1, do this for all registers.
293 Otherwise, REGNO specifies which register (so we can save time). */
296 store_inferior_registers (regno
)
299 register unsigned int regaddr
;
303 unsigned int offset
= U_REGS_OFFSET
;
307 regaddr
= register_addr (regno
, offset
);
308 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(PTRACE_XFER_TYPE
))
311 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
312 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
]);
315 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
316 perror_with_name (buf
);
318 regaddr
+= sizeof(PTRACE_XFER_TYPE
);
323 for (regno
= 0; regno
< NUM_REGS
; regno
++)
325 if (CANNOT_STORE_REGISTER (regno
))
327 regaddr
= register_addr (regno
, offset
);
328 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(PTRACE_XFER_TYPE
))
331 ptrace (PT_WRITE_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
332 *(PTRACE_XFER_TYPE
*) ®isters
[REGISTER_BYTE (regno
) + i
]);
335 sprintf (buf
, "writing register number %d(%d)", regno
, i
);
336 perror_with_name (buf
);
338 regaddr
+= sizeof(PTRACE_XFER_TYPE
);
343 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
345 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
346 in the NEW_SUN_PTRACE case.
347 It ought to be straightforward. But it appears that writing did
348 not write the data that I specified. I cannot understand where
349 it got the data that it actually did write. */
351 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
352 to debugger memory starting at MYADDR. Copy to inferior if
355 Returns the length copied, which is either the LEN argument or zero.
356 This xfer function does not do partial moves, since child_ops
357 doesn't allow memory operations to cross below us in the target stack
361 child_xfer_memory (memaddr
, myaddr
, len
, write
, target
)
366 struct target_ops
*target
; /* ignored */
369 /* Round starting address down to longword boundary. */
370 register CORE_ADDR addr
= memaddr
& - sizeof (PTRACE_XFER_TYPE
);
371 /* Round ending address up; get number of longwords that makes. */
373 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
374 / sizeof (PTRACE_XFER_TYPE
);
375 /* Allocate buffer of that many longwords. */
376 register PTRACE_XFER_TYPE
*buffer
377 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
381 /* Fill start and end extra bytes of buffer with existing memory data. */
383 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
)) {
384 /* Need part of initial word -- fetch it. */
385 buffer
[0] = ptrace (PT_READ_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
389 if (count
> 1) /* FIXME, avoid if even boundary */
392 = ptrace (PT_READ_I
, inferior_pid
,
394 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
398 /* Copy data to be written over corresponding part of buffer */
400 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
404 /* Write the entire buffer. */
406 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
409 ptrace (PT_WRITE_D
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
413 /* Using the appropriate one (I or D) is necessary for
414 Gould NP1, at least. */
416 ptrace (PT_WRITE_I
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
,
425 /* Read all the longwords */
426 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
429 buffer
[i
] = ptrace (PT_READ_I
, inferior_pid
,
430 (PTRACE_ARG3_TYPE
) addr
, 0);
436 /* Copy appropriate bytes out of the buffer. */
438 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),