]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/infptrace.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / infptrace.c
CommitLineData
c906108c 1/* Low level Unix child interface to ptrace, for GDB when running under Unix.
6aba47ca
DJ
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
3 1999, 2000, 2001, 2002, 2004, 2007 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
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
197e01b6
EZ
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
c906108c
SS
21
22#include "defs.h"
8cbba7c8 23#include "command.h"
c906108c 24#include "frame.h"
8cbba7c8 25#include "gdbcore.h"
c906108c 26#include "inferior.h"
4e052eda 27#include "regcache.h"
8cbba7c8 28#include "target.h"
ed9a39eb 29
8cbba7c8 30#include "gdb_assert.h"
03f2053f 31#include "gdb_wait.h"
8cbba7c8 32#include "gdb_string.h"
c906108c 33
c906108c 34#include <sys/param.h>
4b14d3e4 35#include "gdb_dirent.h"
c906108c
SS
36#include <signal.h>
37#include <sys/ioctl.h>
38
11003ae3 39#include "gdb_ptrace.h"
c906108c 40
c0ccb908 41#ifdef HAVE_SYS_FILE_H
c906108c
SS
42#include <sys/file.h>
43#endif
652fc137 44
c906108c
SS
45#if !defined (FETCH_INFERIOR_REGISTERS)
46#include <sys/user.h> /* Probably need to poke the user structure */
c906108c
SS
47#endif /* !FETCH_INFERIOR_REGISTERS */
48
49#if !defined (CHILD_XFER_MEMORY)
a14ed312 50static void udot_info (char *, int);
c906108c
SS
51#endif
52
a14ed312 53void _initialize_infptrace (void);
c906108c 54\f
c5aa993b 55
c906108c 56int
f8707cac 57call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
c906108c 58{
f1bc22da 59 return ptrace (request, pid, addr, data);
c906108c
SS
60}
61
c906108c
SS
62/* Wait for a process to finish, possibly running a target-specific
63 hook before returning. */
64
ee21b650
AC
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". */
68
c906108c 69int
39f77062 70ptrace_wait (ptid_t ptid, int *status)
c906108c
SS
71{
72 int wstate;
73
74 wstate = wait (status);
c906108c
SS
75 return wstate;
76}
77
adbef1f0
AC
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. */
82
c906108c 83void
fba45db2 84kill_inferior (void)
c906108c
SS
85{
86 int status;
39f77062 87 int pid = PIDGET (inferior_ptid);
c906108c 88
39f77062 89 if (pid == 0)
c906108c
SS
90 return;
91
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.
97
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. */
655c5466 100 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
d3e05d0d 101 wait (&status);
c906108c
SS
102 target_mourn_inferior ();
103}
adbef1f0 104#endif /* DEPRECATED_KILL_INFERIOR */
c906108c 105
adbef1f0
AC
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. */
c906108c
SS
110
111/* Resume execution of the inferior process.
112 If STEP is nonzero, single-step it.
113 If SIGNAL is nonzero, give it that signal. */
114
115void
39f77062 116child_resume (ptid_t ptid, int step, enum target_signal signal)
c906108c 117{
8cbba7c8 118 int request = PT_CONTINUE;
39f77062
KB
119 int pid = PIDGET (ptid);
120
c906108c
SS
121 if (pid == -1)
122 /* Resume all threads. */
123 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
124 all threads" and "resume inferior_ptid" are the same. */
125 pid = PIDGET (inferior_ptid);
c906108c 126
c906108c
SS
127 if (step)
128 {
8cbba7c8
MK
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. */
134
135 gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
136 request = PT_STEP;
c906108c 137 }
c906108c 138
8cbba7c8
MK
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. */
142
143 errno = 0;
144 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
145 if (errno != 0)
e2e0b3e5 146 perror_with_name (("ptrace"));
c906108c 147}
adbef1f0 148#endif /* DEPRECATED_CHILD_RESUME */
c906108c 149\f
8cbba7c8 150
c906108c 151/* Start debugging the process whose number is PID. */
8cbba7c8 152
c906108c 153int
fba45db2 154attach (int pid)
c906108c 155{
d966f0cb 156#ifdef PT_ATTACH
11003ae3 157 errno = 0;
655c5466 158 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
8cbba7c8 159 if (errno != 0)
e2e0b3e5 160 perror_with_name (("ptrace"));
c906108c
SS
161 attach_flag = 1;
162 return pid;
d966f0cb 163#else
8a3fe4f8 164 error (_("This system does not support attaching to a process"));
d966f0cb 165#endif
c906108c
SS
166}
167
8cbba7c8
MK
168/* Stop debugging the process whose number is PID and continue it with
169 signal number SIGNAL. SIGNAL = 0 means just continue it. */
c906108c
SS
170
171void
fba45db2 172detach (int signal)
c906108c 173{
d966f0cb 174#ifdef PT_DETACH
8cbba7c8
MK
175 int pid = PIDGET (inferior_ptid);
176
11003ae3 177 errno = 0;
8cbba7c8
MK
178 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal);
179 if (errno != 0)
e2e0b3e5 180 perror_with_name (("ptrace"));
c906108c 181 attach_flag = 0;
d966f0cb 182#else
8a3fe4f8 183 error (_("This system does not support detaching from a process"));
d966f0cb 184#endif
c906108c 185}
c906108c 186\f
c906108c 187
652fc137 188#ifndef FETCH_INFERIOR_REGISTERS
c906108c 189
652fc137
MK
190/* U_REGS_OFFSET is the offset of the registers within the u area. */
191#ifndef U_REGS_OFFSET
192
193#ifndef offsetof
c906108c
SS
194#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
195#endif
196
c906108c 197#define U_REGS_OFFSET \
39f77062 198 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
655c5466 199 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
c906108c
SS
200 - KERNEL_U_ADDR
201#endif
202
652fc137 203/* Fetch register REGNUM from the inferior. */
c906108c
SS
204
205static void
652fc137 206fetch_register (int regnum)
c906108c 207{
652fc137
MK
208 CORE_ADDR addr;
209 size_t size;
210 PTRACE_TYPE_RET *buf;
211 int tid, i;
212
213 if (CANNOT_FETCH_REGISTER (regnum))
c906108c 214 {
652fc137 215 regcache_raw_supply (current_regcache, regnum, NULL);
c906108c
SS
216 return;
217 }
218
652fc137
MK
219 /* GNU/Linux LWP ID's are process ID's. */
220 tid = TIDGET (inferior_ptid);
221 if (tid == 0)
222 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
223
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);
ed9a39eb 227
652fc137
MK
228 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
229 buf = alloca (size);
c906108c 230
652fc137
MK
231 /* Read the register contents from the inferior a chuck at the time. */
232 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
c906108c
SS
233 {
234 errno = 0;
652fc137 235 buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0);
c906108c 236 if (errno != 0)
8a3fe4f8 237 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regnum),
652fc137
MK
238 regnum, safe_strerror (errno));
239
240 addr += sizeof (PTRACE_TYPE_RET);
c906108c 241 }
652fc137 242 regcache_raw_supply (current_regcache, regnum, buf);
c906108c
SS
243}
244
652fc137
MK
245/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
246 for all registers. */
c906108c
SS
247
248void
652fc137 249fetch_inferior_registers (int regnum)
c906108c 250{
652fc137
MK
251 if (regnum == -1)
252 for (regnum = 0; regnum < NUM_REGS; regnum++)
253 fetch_register (regnum);
c906108c 254 else
652fc137 255 fetch_register (regnum);
c906108c
SS
256}
257
652fc137 258/* Store register REGNUM into the inferior. */
c906108c
SS
259
260static void
652fc137 261store_register (int regnum)
c906108c 262{
652fc137
MK
263 CORE_ADDR addr;
264 size_t size;
265 PTRACE_TYPE_RET *buf;
266 int tid, i;
c906108c 267
652fc137
MK
268 if (CANNOT_STORE_REGISTER (regnum))
269 return;
ed9a39eb 270
652fc137
MK
271 /* GNU/Linux LWP ID's are process ID's. */
272 tid = TIDGET (inferior_ptid);
273 if (tid == 0)
274 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
c906108c 275
652fc137
MK
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);
8b6f1f3a 279
652fc137
MK
280 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
281 buf = alloca (size);
8b6f1f3a 282
652fc137
MK
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++)
c906108c
SS
286 {
287 errno = 0;
652fc137 288 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]);
c906108c 289 if (errno != 0)
3470a02f
AC
290 error (_("Couldn't write register %s (#%d): %s."),
291 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
652fc137
MK
292
293 addr += sizeof (PTRACE_TYPE_RET);
c906108c
SS
294 }
295}
296
652fc137
MK
297/* Store register REGNUM back into the inferior. If REGNUM is -1, do
298 this for all registers (including the floating point registers). */
c906108c
SS
299
300void
652fc137 301store_inferior_registers (int regnum)
c906108c 302{
652fc137
MK
303 if (regnum == -1)
304 for (regnum = 0; regnum < NUM_REGS; regnum++)
305 store_register (regnum);
c906108c 306 else
652fc137 307 store_register (regnum);
c906108c 308}
652fc137
MK
309
310#endif /* not FETCH_INFERIOR_REGISTERS. */
c906108c
SS
311\f
312
94cd915f
MS
313/* Set an upper limit on alloca. */
314#ifndef GDB_MAX_ALLOCA
315#define GDB_MAX_ALLOCA 0x1000
316#endif
317
c906108c
SS
318#if !defined (CHILD_XFER_MEMORY)
319/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
3c2fb7bd
MK
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. */
c906108c 323
3c2fb7bd
MK
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.
c5aa993b 327
3c2fb7bd
MK
328 Returns the length copied, which is either the LEN argument or
329 zero. This xfer function does not do partial moves, since
1df84f13
AC
330 deprecated_child_ops doesn't allow memory operations to cross below
331 us in the target stack anyway. */
c906108c
SS
332
333int
6c932e54 334child_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int write,
0a65a603 335 struct mem_attrib *attrib, struct target_ops *target)
c906108c 336{
3c2fb7bd 337 int i;
c906108c 338 /* Round starting address down to longword boundary. */
88800403 339 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
c906108c 340 /* Round ending address up; get number of longwords that makes. */
88800403
MK
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;
94cd915f
MS
345 struct cleanup *old_chain = NULL;
346
371a6e84
MK
347#ifdef PT_IO
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. */
351
352 {
353 struct ptrace_io_desc piod;
354
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;
360 piod.piod_len = len;
361
362 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
363 {
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. */
367 if (errno != EINVAL)
368 return 0;
369 }
370 else
371 {
372 /* Return the actual number of bytes read or written. */
373 return piod.piod_len;
374 }
375 }
376#endif
377
c906108c 378 /* Allocate buffer of that many longwords. */
94cd915f
MS
379 if (len < GDB_MAX_ALLOCA)
380 {
88800403 381 buffer = (PTRACE_TYPE_RET *) alloca (alloc);
94cd915f
MS
382 }
383 else
384 {
88800403 385 buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
94cd915f
MS
386 old_chain = make_cleanup (xfree, buffer);
387 }
c906108c
SS
388
389 if (write)
390 {
3c2fb7bd
MK
391 /* Fill start and end extra bytes of buffer with existing memory
392 data. */
88800403 393 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
c5aa993b
JM
394 {
395 /* Need part of initial word -- fetch it. */
39f77062 396 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 397 (PTRACE_TYPE_ARG3) addr, 0);
c5aa993b 398 }
c906108c 399
3c2fb7bd 400 if (count > 1) /* FIXME, avoid if even boundary. */
c906108c 401 {
3c2fb7bd
MK
402 buffer[count - 1] =
403 ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 404 ((PTRACE_TYPE_ARG3)
88800403 405 (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
c906108c
SS
406 }
407
3c2fb7bd 408 /* Copy data to be written over corresponding part of buffer. */
88800403 409 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
3c2fb7bd 410 myaddr, len);
c906108c
SS
411
412 /* Write the entire buffer. */
88800403 413 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
c906108c
SS
414 {
415 errno = 0;
39f77062 416 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
655c5466 417 (PTRACE_TYPE_ARG3) addr, buffer[i]);
c906108c 418 if (errno)
c5aa993b 419 {
c906108c 420 /* Using the appropriate one (I or D) is necessary for
c5aa993b 421 Gould NP1, at least. */
c906108c 422 errno = 0;
39f77062 423 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
655c5466 424 (PTRACE_TYPE_ARG3) addr, buffer[i]);
c906108c
SS
425 }
426 if (errno)
427 return 0;
428 }
c906108c
SS
429 }
430 else
431 {
3c2fb7bd 432 /* Read all the longwords. */
88800403 433 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
c906108c
SS
434 {
435 errno = 0;
39f77062 436 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 437 (PTRACE_TYPE_ARG3) addr, 0);
c906108c
SS
438 if (errno)
439 return 0;
440 QUIT;
441 }
442
443 /* Copy appropriate bytes out of the buffer. */
444 memcpy (myaddr,
88800403 445 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
c906108c
SS
446 len);
447 }
3c2fb7bd 448
94cd915f
MS
449 if (old_chain != NULL)
450 do_cleanups (old_chain);
c906108c
SS
451 return len;
452}
c906108c 453\f
c5aa993b 454
c906108c 455static void
fba45db2 456udot_info (char *dummy1, int dummy2)
c906108c
SS
457{
458#if defined (KERNEL_U_SIZE)
7343d46a 459 long udot_off; /* Offset into user struct */
c5aa993b
JM
460 int udot_val; /* Value from user struct at udot_off */
461 char mess[128]; /* For messages */
c906108c
SS
462#endif
463
c5aa993b
JM
464 if (!target_has_execution)
465 {
8a3fe4f8 466 error (_("The program is not being run."));
c5aa993b 467 }
c906108c
SS
468
469#if !defined (KERNEL_U_SIZE)
470
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()" */
8a3fe4f8 475 error (_("Don't know how large ``struct user'' is in this version of gdb."));
c906108c
SS
476
477#else
478
479 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
480 {
481 if ((udot_off % 24) == 0)
482 {
483 if (udot_off > 0)
484 {
485 printf_filtered ("\n");
486 }
7343d46a 487 printf_filtered ("%s:", paddr (udot_off));
c906108c 488 }
655c5466 489 udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0);
c906108c
SS
490 if (errno != 0)
491 {
7343d46a
AC
492 sprintf (mess, "\nreading user struct at offset 0x%s",
493 paddr_nz (udot_off));
c906108c
SS
494 perror_with_name (mess);
495 }
496 /* Avoid using nonportable (?) "*" in print specs */
497 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
498 }
499 printf_filtered ("\n");
500
501#endif
502}
503#endif /* !defined (CHILD_XFER_MEMORY). */
c906108c 504\f
c5aa993b 505
c906108c 506void
fba45db2 507_initialize_infptrace (void)
c906108c
SS
508{
509#if !defined (CHILD_XFER_MEMORY)
510 add_info ("udot", udot_info,
1bedd215 511 _("Print contents of kernel ``struct user'' for current child."));
c906108c
SS
512#endif
513}