*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / infptrace.c
CommitLineData
c906108c 1/* Low level Unix child interface to ptrace, for GDB when running under Unix.
197e01b6 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
2689df5a 3 1998, 1999, 2000, 2001, 2002, 2004
c906108c
SS
4 Free Software Foundation, Inc.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
8cbba7c8 24#include "command.h"
c906108c 25#include "frame.h"
8cbba7c8 26#include "gdbcore.h"
c906108c 27#include "inferior.h"
4e052eda 28#include "regcache.h"
8cbba7c8 29#include "target.h"
ed9a39eb 30
8cbba7c8 31#include "gdb_assert.h"
03f2053f 32#include "gdb_wait.h"
8cbba7c8 33#include "gdb_string.h"
c906108c 34
c906108c 35#include <sys/param.h>
4b14d3e4 36#include "gdb_dirent.h"
c906108c
SS
37#include <signal.h>
38#include <sys/ioctl.h>
39
11003ae3 40#include "gdb_ptrace.h"
c906108c 41
c0ccb908 42#ifdef HAVE_SYS_FILE_H
c906108c
SS
43#include <sys/file.h>
44#endif
652fc137 45
c906108c
SS
46#if !defined (FETCH_INFERIOR_REGISTERS)
47#include <sys/user.h> /* Probably need to poke the user structure */
c906108c
SS
48#endif /* !FETCH_INFERIOR_REGISTERS */
49
50#if !defined (CHILD_XFER_MEMORY)
a14ed312 51static void udot_info (char *, int);
c906108c
SS
52#endif
53
a14ed312 54void _initialize_infptrace (void);
c906108c 55\f
c5aa993b 56
c906108c 57int
f8707cac 58call_ptrace (int request, int pid, PTRACE_ARG3_TYPE addr, int data)
c906108c 59{
f1bc22da 60 return ptrace (request, pid, addr, data);
c906108c
SS
61}
62
c906108c
SS
63/* Wait for a process to finish, possibly running a target-specific
64 hook before returning. */
65
ee21b650
AC
66/* NOTE: cagney: 2004-09-29: Dependant on the native configuration,
67 "hppah-nat.c" may either call this or infttrace.c's implementation
68 of ptrace_wait. See "hppahpux.mh". */
69
c906108c 70int
39f77062 71ptrace_wait (ptid_t ptid, int *status)
c906108c
SS
72{
73 int wstate;
74
75 wstate = wait (status);
c906108c
SS
76 return wstate;
77}
78
adbef1f0
AC
79#ifndef DEPRECATED_KILL_INFERIOR
80/* NOTE: cagney/2004-09-12: Instead of definining this macro, code
81 should call inf_ptrace_target to get a basic ptrace target and then
82 locally update any necessary methods. See ppcnbsd-nat.c. */
83
c906108c 84void
fba45db2 85kill_inferior (void)
c906108c
SS
86{
87 int status;
39f77062 88 int pid = PIDGET (inferior_ptid);
c906108c 89
39f77062 90 if (pid == 0)
c906108c
SS
91 return;
92
93 /* This once used to call "kill" to kill the inferior just in case
94 the inferior was still running. As others have noted in the past
95 (kingdon) there shouldn't be any way to get here if the inferior
96 is still running -- else there's a major problem elsewere in gdb
97 and it needs to be fixed.
98
99 The kill call causes problems under hpux10, so it's been removed;
100 if this causes problems we'll deal with them as they arise. */
655c5466 101 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
d3e05d0d 102 wait (&status);
c906108c
SS
103 target_mourn_inferior ();
104}
adbef1f0 105#endif /* DEPRECATED_KILL_INFERIOR */
c906108c 106
adbef1f0
AC
107#ifndef DEPRECATED_CHILD_RESUME
108/* NOTE: cagney/2004-09-12: Instead of definining this macro, code
109 should call inf_ptrace_target to get a basic ptrace target and then
110 locally update any necessary methods. See ppcnbsd-nat.c. */
c906108c
SS
111
112/* Resume execution of the inferior process.
113 If STEP is nonzero, single-step it.
114 If SIGNAL is nonzero, give it that signal. */
115
116void
39f77062 117child_resume (ptid_t ptid, int step, enum target_signal signal)
c906108c 118{
8cbba7c8 119 int request = PT_CONTINUE;
39f77062
KB
120 int pid = PIDGET (ptid);
121
c906108c
SS
122 if (pid == -1)
123 /* Resume all threads. */
124 /* I think this only gets used in the non-threaded case, where "resume
39f77062
KB
125 all threads" and "resume inferior_ptid" are the same. */
126 pid = PIDGET (inferior_ptid);
c906108c 127
c906108c
SS
128 if (step)
129 {
8cbba7c8
MK
130 /* If this system does not support PT_STEP, a higher level
131 function will have called single_step() to transmute the step
132 request into a continue request (by setting breakpoints on
133 all possible successor instructions), so we don't have to
134 worry about that here. */
135
136 gdb_assert (!SOFTWARE_SINGLE_STEP_P ());
137 request = PT_STEP;
c906108c 138 }
c906108c 139
8cbba7c8
MK
140 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
141 where it was. If GDB wanted it to start some other way, we have
142 already written a new PC value to the child. */
143
144 errno = 0;
145 ptrace (request, pid, (PTRACE_TYPE_ARG3)1, target_signal_to_host (signal));
146 if (errno != 0)
e2e0b3e5 147 perror_with_name (("ptrace"));
c906108c 148}
adbef1f0 149#endif /* DEPRECATED_CHILD_RESUME */
c906108c 150\f
8cbba7c8 151
c906108c 152/* Start debugging the process whose number is PID. */
8cbba7c8 153
c906108c 154int
fba45db2 155attach (int pid)
c906108c 156{
d966f0cb 157#ifdef PT_ATTACH
11003ae3 158 errno = 0;
655c5466 159 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
8cbba7c8 160 if (errno != 0)
e2e0b3e5 161 perror_with_name (("ptrace"));
c906108c
SS
162 attach_flag = 1;
163 return pid;
d966f0cb 164#else
8a3fe4f8 165 error (_("This system does not support attaching to a process"));
d966f0cb 166#endif
c906108c
SS
167}
168
8cbba7c8
MK
169/* Stop debugging the process whose number is PID and continue it with
170 signal number SIGNAL. SIGNAL = 0 means just continue it. */
c906108c
SS
171
172void
fba45db2 173detach (int signal)
c906108c 174{
d966f0cb 175#ifdef PT_DETACH
8cbba7c8
MK
176 int pid = PIDGET (inferior_ptid);
177
11003ae3 178 errno = 0;
8cbba7c8
MK
179 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, signal);
180 if (errno != 0)
e2e0b3e5 181 perror_with_name (("ptrace"));
c906108c 182 attach_flag = 0;
d966f0cb 183#else
8a3fe4f8 184 error (_("This system does not support detaching from a process"));
d966f0cb 185#endif
c906108c 186}
c906108c 187\f
c906108c 188
652fc137 189#ifndef FETCH_INFERIOR_REGISTERS
c906108c 190
652fc137
MK
191/* U_REGS_OFFSET is the offset of the registers within the u area. */
192#ifndef U_REGS_OFFSET
193
194#ifndef offsetof
c906108c
SS
195#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
196#endif
197
c906108c 198#define U_REGS_OFFSET \
39f77062 199 ptrace (PT_READ_U, PIDGET (inferior_ptid), \
655c5466 200 (PTRACE_TYPE_ARG3) (offsetof (struct user, u_ar0)), 0) \
c906108c
SS
201 - KERNEL_U_ADDR
202#endif
203
652fc137 204/* Fetch register REGNUM from the inferior. */
c906108c
SS
205
206static void
652fc137 207fetch_register (int regnum)
c906108c 208{
652fc137
MK
209 CORE_ADDR addr;
210 size_t size;
211 PTRACE_TYPE_RET *buf;
212 int tid, i;
213
214 if (CANNOT_FETCH_REGISTER (regnum))
c906108c 215 {
652fc137 216 regcache_raw_supply (current_regcache, regnum, NULL);
c906108c
SS
217 return;
218 }
219
652fc137
MK
220 /* GNU/Linux LWP ID's are process ID's. */
221 tid = TIDGET (inferior_ptid);
222 if (tid == 0)
223 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
224
225 /* This isn't really an address. But ptrace thinks of it as one. */
226 addr = register_addr (regnum, U_REGS_OFFSET);
227 size = register_size (current_gdbarch, regnum);
ed9a39eb 228
652fc137
MK
229 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
230 buf = alloca (size);
c906108c 231
652fc137
MK
232 /* Read the register contents from the inferior a chuck at the time. */
233 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
c906108c
SS
234 {
235 errno = 0;
652fc137 236 buf[i] = ptrace (PT_READ_U, tid, (PTRACE_TYPE_ARG3) addr, 0);
c906108c 237 if (errno != 0)
8a3fe4f8 238 error (_("Couldn't read register %s (#%d): %s."), REGISTER_NAME (regnum),
652fc137
MK
239 regnum, safe_strerror (errno));
240
241 addr += sizeof (PTRACE_TYPE_RET);
c906108c 242 }
652fc137 243 regcache_raw_supply (current_regcache, regnum, buf);
c906108c
SS
244}
245
652fc137
MK
246/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
247 for all registers. */
c906108c
SS
248
249void
652fc137 250fetch_inferior_registers (int regnum)
c906108c 251{
652fc137
MK
252 if (regnum == -1)
253 for (regnum = 0; regnum < NUM_REGS; regnum++)
254 fetch_register (regnum);
c906108c 255 else
652fc137 256 fetch_register (regnum);
c906108c
SS
257}
258
652fc137 259/* Store register REGNUM into the inferior. */
c906108c
SS
260
261static void
652fc137 262store_register (int regnum)
c906108c 263{
652fc137
MK
264 CORE_ADDR addr;
265 size_t size;
266 PTRACE_TYPE_RET *buf;
267 int tid, i;
c906108c 268
652fc137
MK
269 if (CANNOT_STORE_REGISTER (regnum))
270 return;
ed9a39eb 271
652fc137
MK
272 /* GNU/Linux LWP ID's are process ID's. */
273 tid = TIDGET (inferior_ptid);
274 if (tid == 0)
275 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
c906108c 276
652fc137
MK
277 /* This isn't really an address. But ptrace thinks of it as one. */
278 addr = register_addr (regnum, U_REGS_OFFSET);
279 size = register_size (current_gdbarch, regnum);
8b6f1f3a 280
652fc137
MK
281 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
282 buf = alloca (size);
8b6f1f3a 283
652fc137
MK
284 /* Write the register contents into the inferior a chunk at the time. */
285 regcache_raw_collect (current_regcache, regnum, buf);
286 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
c906108c
SS
287 {
288 errno = 0;
652fc137 289 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) addr, buf[i]);
c906108c 290 if (errno != 0)
3470a02f
AC
291 error (_("Couldn't write register %s (#%d): %s."),
292 REGISTER_NAME (regnum), regnum, safe_strerror (errno));
652fc137
MK
293
294 addr += sizeof (PTRACE_TYPE_RET);
c906108c
SS
295 }
296}
297
652fc137
MK
298/* Store register REGNUM back into the inferior. If REGNUM is -1, do
299 this for all registers (including the floating point registers). */
c906108c
SS
300
301void
652fc137 302store_inferior_registers (int regnum)
c906108c 303{
652fc137
MK
304 if (regnum == -1)
305 for (regnum = 0; regnum < NUM_REGS; regnum++)
306 store_register (regnum);
c906108c 307 else
652fc137 308 store_register (regnum);
c906108c 309}
652fc137
MK
310
311#endif /* not FETCH_INFERIOR_REGISTERS. */
c906108c
SS
312\f
313
94cd915f
MS
314/* Set an upper limit on alloca. */
315#ifndef GDB_MAX_ALLOCA
316#define GDB_MAX_ALLOCA 0x1000
317#endif
318
c906108c
SS
319#if !defined (CHILD_XFER_MEMORY)
320/* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
3c2fb7bd
MK
321 in the NEW_SUN_PTRACE case. It ought to be straightforward. But
322 it appears that writing did not write the data that I specified. I
323 cannot understand where it got the data that it actually did write. */
c906108c 324
3c2fb7bd
MK
325/* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
326 debugger memory starting at MYADDR. Copy to inferior if WRITE is
327 nonzero. TARGET is ignored.
c5aa993b 328
3c2fb7bd
MK
329 Returns the length copied, which is either the LEN argument or
330 zero. This xfer function does not do partial moves, since
1df84f13
AC
331 deprecated_child_ops doesn't allow memory operations to cross below
332 us in the target stack anyway. */
c906108c
SS
333
334int
6c932e54 335child_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int write,
0a65a603 336 struct mem_attrib *attrib, struct target_ops *target)
c906108c 337{
3c2fb7bd 338 int i;
c906108c 339 /* Round starting address down to longword boundary. */
88800403 340 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_TYPE_RET);
c906108c 341 /* Round ending address up; get number of longwords that makes. */
88800403
MK
342 int count = ((((memaddr + len) - addr) + sizeof (PTRACE_TYPE_RET) - 1)
343 / sizeof (PTRACE_TYPE_RET));
344 int alloc = count * sizeof (PTRACE_TYPE_RET);
345 PTRACE_TYPE_RET *buffer;
94cd915f
MS
346 struct cleanup *old_chain = NULL;
347
371a6e84
MK
348#ifdef PT_IO
349 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO request
350 that promises to be much more efficient in reading and writing
351 data in the traced process's address space. */
352
353 {
354 struct ptrace_io_desc piod;
355
356 /* NOTE: We assume that there are no distinct address spaces for
357 instruction and data. */
358 piod.piod_op = write ? PIOD_WRITE_D : PIOD_READ_D;
359 piod.piod_offs = (void *) memaddr;
360 piod.piod_addr = myaddr;
361 piod.piod_len = len;
362
363 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == -1)
364 {
365 /* If the PT_IO request is somehow not supported, fallback on
366 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
367 to indicate failure. */
368 if (errno != EINVAL)
369 return 0;
370 }
371 else
372 {
373 /* Return the actual number of bytes read or written. */
374 return piod.piod_len;
375 }
376 }
377#endif
378
c906108c 379 /* Allocate buffer of that many longwords. */
94cd915f
MS
380 if (len < GDB_MAX_ALLOCA)
381 {
88800403 382 buffer = (PTRACE_TYPE_RET *) alloca (alloc);
94cd915f
MS
383 }
384 else
385 {
88800403 386 buffer = (PTRACE_TYPE_RET *) xmalloc (alloc);
94cd915f
MS
387 old_chain = make_cleanup (xfree, buffer);
388 }
c906108c
SS
389
390 if (write)
391 {
3c2fb7bd
MK
392 /* Fill start and end extra bytes of buffer with existing memory
393 data. */
88800403 394 if (addr != memaddr || len < (int) sizeof (PTRACE_TYPE_RET))
c5aa993b
JM
395 {
396 /* Need part of initial word -- fetch it. */
39f77062 397 buffer[0] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 398 (PTRACE_TYPE_ARG3) addr, 0);
c5aa993b 399 }
c906108c 400
3c2fb7bd 401 if (count > 1) /* FIXME, avoid if even boundary. */
c906108c 402 {
3c2fb7bd
MK
403 buffer[count - 1] =
404 ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 405 ((PTRACE_TYPE_ARG3)
88800403 406 (addr + (count - 1) * sizeof (PTRACE_TYPE_RET))), 0);
c906108c
SS
407 }
408
3c2fb7bd 409 /* Copy data to be written over corresponding part of buffer. */
88800403 410 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
3c2fb7bd 411 myaddr, len);
c906108c
SS
412
413 /* Write the entire buffer. */
88800403 414 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
c906108c
SS
415 {
416 errno = 0;
39f77062 417 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
655c5466 418 (PTRACE_TYPE_ARG3) addr, buffer[i]);
c906108c 419 if (errno)
c5aa993b 420 {
c906108c 421 /* Using the appropriate one (I or D) is necessary for
c5aa993b 422 Gould NP1, at least. */
c906108c 423 errno = 0;
39f77062 424 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
655c5466 425 (PTRACE_TYPE_ARG3) addr, buffer[i]);
c906108c
SS
426 }
427 if (errno)
428 return 0;
429 }
c906108c
SS
430 }
431 else
432 {
3c2fb7bd 433 /* Read all the longwords. */
88800403 434 for (i = 0; i < count; i++, addr += sizeof (PTRACE_TYPE_RET))
c906108c
SS
435 {
436 errno = 0;
39f77062 437 buffer[i] = ptrace (PT_READ_I, PIDGET (inferior_ptid),
655c5466 438 (PTRACE_TYPE_ARG3) addr, 0);
c906108c
SS
439 if (errno)
440 return 0;
441 QUIT;
442 }
443
444 /* Copy appropriate bytes out of the buffer. */
445 memcpy (myaddr,
88800403 446 (char *) buffer + (memaddr & (sizeof (PTRACE_TYPE_RET) - 1)),
c906108c
SS
447 len);
448 }
3c2fb7bd 449
94cd915f
MS
450 if (old_chain != NULL)
451 do_cleanups (old_chain);
c906108c
SS
452 return len;
453}
c906108c 454\f
c5aa993b 455
c906108c 456static void
fba45db2 457udot_info (char *dummy1, int dummy2)
c906108c
SS
458{
459#if defined (KERNEL_U_SIZE)
7343d46a 460 long udot_off; /* Offset into user struct */
c5aa993b
JM
461 int udot_val; /* Value from user struct at udot_off */
462 char mess[128]; /* For messages */
c906108c
SS
463#endif
464
c5aa993b
JM
465 if (!target_has_execution)
466 {
8a3fe4f8 467 error (_("The program is not being run."));
c5aa993b 468 }
c906108c
SS
469
470#if !defined (KERNEL_U_SIZE)
471
472 /* Adding support for this command is easy. Typically you just add a
473 routine, called "kernel_u_size" that returns the size of the user
474 struct, to the appropriate *-nat.c file and then add to the native
475 config file "#define KERNEL_U_SIZE kernel_u_size()" */
8a3fe4f8 476 error (_("Don't know how large ``struct user'' is in this version of gdb."));
c906108c
SS
477
478#else
479
480 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
481 {
482 if ((udot_off % 24) == 0)
483 {
484 if (udot_off > 0)
485 {
486 printf_filtered ("\n");
487 }
7343d46a 488 printf_filtered ("%s:", paddr (udot_off));
c906108c 489 }
655c5466 490 udot_val = ptrace (PT_READ_U, PIDGET (inferior_ptid), (PTRACE_TYPE_ARG3) udot_off, 0);
c906108c
SS
491 if (errno != 0)
492 {
7343d46a
AC
493 sprintf (mess, "\nreading user struct at offset 0x%s",
494 paddr_nz (udot_off));
c906108c
SS
495 perror_with_name (mess);
496 }
497 /* Avoid using nonportable (?) "*" in print specs */
498 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
499 }
500 printf_filtered ("\n");
501
502#endif
503}
504#endif /* !defined (CHILD_XFER_MEMORY). */
c906108c 505\f
c5aa993b 506
c906108c 507void
fba45db2 508_initialize_infptrace (void)
c906108c
SS
509{
510#if !defined (CHILD_XFER_MEMORY)
511 add_info ("udot", udot_info,
1bedd215 512 _("Print contents of kernel ``struct user'' for current child."));
c906108c
SS
513#endif
514}
This page took 0.812073 seconds and 4 git commands to generate.