4841e9c6a0aae2b55d6853a2beacf5a8453bb723
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, 2001
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,
20 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
33 #include <sys/types.h>
36 #include <sys/param.h>
37 #include "gdb_dirent.h"
39 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_PTRACE_H
45 #include <sys/ptrace.h>
49 #if !defined (PT_READ_I)
50 #define PT_READ_I 1 /* Read word from text space */
52 #if !defined (PT_READ_D)
53 #define PT_READ_D 2 /* Read word from data space */
55 #if !defined (PT_READ_U)
56 #define PT_READ_U 3 /* Read word from kernel user struct */
58 #if !defined (PT_WRITE_I)
59 #define PT_WRITE_I 4 /* Write word to text space */
61 #if !defined (PT_WRITE_D)
62 #define PT_WRITE_D 5 /* Write word to data space */
64 #if !defined (PT_WRITE_U)
65 #define PT_WRITE_U 6 /* Write word to kernel user struct */
67 #if !defined (PT_CONTINUE)
68 #define PT_CONTINUE 7 /* Continue after signal */
70 #if !defined (PT_STEP)
71 #define PT_STEP 9 /* Set flag for single stepping */
73 #if !defined (PT_KILL)
74 #define PT_KILL 8 /* Send child a SIGKILL signal */
78 #define PT_ATTACH PTRACE_ATTACH
81 #define PT_DETACH PTRACE_DETACH
89 /* Don't think this is used anymore. On the sequent (not sure whether it's
90 dynix or ptx or both), it is included unconditionally by sys/user.h and
91 not protected against multiple inclusion. */
95 #if !defined (FETCH_INFERIOR_REGISTERS)
96 #include <sys/user.h> /* Probably need to poke the user structure */
97 #if defined (KERNEL_U_ADDR_BSD)
98 #include <a.out.h> /* For struct nlist */
99 #endif /* KERNEL_U_ADDR_BSD. */
100 #endif /* !FETCH_INFERIOR_REGISTERS */
102 #if !defined (CHILD_XFER_MEMORY)
103 static void udot_info (char *, int);
106 #if !defined (FETCH_INFERIOR_REGISTERS)
107 static void fetch_register (int);
108 static void store_register (int);
112 * Some systems (Linux) may have threads implemented as pseudo-processes,
113 * in which case we may be tracing more than one process at a time.
114 * In that case, inferior_pid will contain the main process ID and the
115 * individual thread (process) id mashed together. These macros are
116 * used to separate them out. The definitions may be overridden in tm.h
118 * NOTE: default definitions here are for systems with no threads.
119 * Useful definitions MUST be provided in tm.h
122 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
123 #define PIDGET(PID) PID
124 #define TIDGET(PID) 0
127 void _initialize_kernel_u_addr (void);
128 void _initialize_infptrace (void);
131 /* This function simply calls ptrace with the given arguments.
132 It exists so that all calls to ptrace are isolated in this
133 machine-dependent file. */
135 call_ptrace (int request
, int pid
, PTRACE_ARG3_TYPE addr
, int data
)
142 printf ("call_ptrace(request=%d, pid=%d, addr=0x%x, data=0x%x)",
143 request
, pid
, addr
, data
);
145 #if defined(PT_SETTRC)
146 /* If the parent can be told to attach to us, try to do it. */
147 if (request
== PT_SETTRC
)
150 #if !defined (FIVE_ARG_PTRACE)
151 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
);
153 /* Deal with HPUX 8.0 braindamage. We never use the
154 calls which require the fifth argument. */
155 pt_status
= ptrace (PT_SETTRC
, pid
, addr
, data
, 0);
158 perror_with_name ("ptrace");
160 printf (" = %d\n", pt_status
);
165 return parent_attach_all (pid
, addr
, data
);
169 #if defined(PT_CONTIN1)
170 /* On HPUX, PT_CONTIN1 is a form of continue that preserves pending
171 signals. If it's available, use it. */
172 if (request
== PT_CONTINUE
)
173 request
= PT_CONTIN1
;
176 #if defined(PT_SINGLE1)
177 /* On HPUX, PT_SINGLE1 is a form of step that preserves pending
178 signals. If it's available, use it. */
179 if (request
== PT_STEP
)
180 request
= PT_SINGLE1
;
187 #if !defined (FIVE_ARG_PTRACE)
188 pt_status
= ptrace (request
, pid
, addr
, data
);
190 /* Deal with HPUX 8.0 braindamage. We never use the
191 calls which require the fifth argument. */
192 pt_status
= ptrace (request
, pid
, addr
, data
, 0);
197 printf (" [errno = %d]", errno
);
200 printf (" = 0x%x\n", pt_status
);
206 #if defined (DEBUG_PTRACE) || defined (FIVE_ARG_PTRACE)
207 /* For the rest of the file, use an extra level of indirection */
208 /* This lets us breakpoint usefully on call_ptrace. */
209 #define ptrace call_ptrace
212 /* Wait for a process to finish, possibly running a target-specific
213 hook before returning. */
216 ptrace_wait (int pid
, int *status
)
220 wstate
= wait (status
);
221 target_post_wait (wstate
, *status
);
230 if (inferior_pid
== 0)
233 /* This once used to call "kill" to kill the inferior just in case
234 the inferior was still running. As others have noted in the past
235 (kingdon) there shouldn't be any way to get here if the inferior
236 is still running -- else there's a major problem elsewere in gdb
237 and it needs to be fixed.
239 The kill call causes problems under hpux10, so it's been removed;
240 if this causes problems we'll deal with them as they arise. */
241 ptrace (PT_KILL
, inferior_pid
, (PTRACE_ARG3_TYPE
) 0, 0);
242 ptrace_wait (0, &status
);
243 target_mourn_inferior ();
248 /* Resume execution of the inferior process.
249 If STEP is nonzero, single-step it.
250 If SIGNAL is nonzero, give it that signal. */
253 child_resume (int pid
, int step
, enum target_signal signal
)
258 /* Resume all threads. */
259 /* I think this only gets used in the non-threaded case, where "resume
260 all threads" and "resume inferior_pid" are the same. */
263 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
264 it was. (If GDB wanted it to start some other way, we have already
265 written a new PC value to the child.)
267 If this system does not support PT_STEP, a higher level function will
268 have called single_step() to transmute the step request into a
269 continue request (by setting breakpoints on all possible successor
270 instructions), so we don't have to worry about that here. */
274 if (SOFTWARE_SINGLE_STEP_P
)
275 abort (); /* Make sure this doesn't happen. */
277 ptrace (PT_STEP
, pid
, (PTRACE_ARG3_TYPE
) 1,
278 target_signal_to_host (signal
));
281 ptrace (PT_CONTINUE
, pid
, (PTRACE_ARG3_TYPE
) 1,
282 target_signal_to_host (signal
));
286 perror_with_name ("ptrace");
289 #endif /* CHILD_RESUME */
293 /* Start debugging the process whose number is PID. */
298 ptrace (PT_ATTACH
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
300 perror_with_name ("ptrace");
305 /* Stop debugging the process whose number is PID
306 and continue it with signal number SIGNAL.
307 SIGNAL = 0 means just continue it. */
313 ptrace (PT_DETACH
, inferior_pid
, (PTRACE_ARG3_TYPE
) 1, signal
);
315 perror_with_name ("ptrace");
318 #endif /* ATTACH_DETACH */
320 /* Default the type of the ptrace transfer to int. */
321 #ifndef PTRACE_XFER_TYPE
322 #define PTRACE_XFER_TYPE int
325 /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
326 to get the offset in the core file of the register values. */
327 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
328 /* Get kernel_u_addr using BSD-style nlist(). */
329 CORE_ADDR kernel_u_addr
;
330 #endif /* KERNEL_U_ADDR_BSD. */
333 _initialize_kernel_u_addr (void)
335 #if defined (KERNEL_U_ADDR_BSD) && !defined (FETCH_INFERIOR_REGISTERS)
336 struct nlist names
[2];
338 names
[0].n_un
.n_name
= "_u";
339 names
[1].n_un
.n_name
= NULL
;
340 if (nlist ("/vmunix", names
) == 0)
341 kernel_u_addr
= names
[0].n_value
;
343 internal_error (__FILE__
, __LINE__
,
344 "Unable to get kernel u area address.");
345 #endif /* KERNEL_U_ADDR_BSD. */
348 #if !defined (FETCH_INFERIOR_REGISTERS)
350 #if !defined (offsetof)
351 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
354 /* U_REGS_OFFSET is the offset of the registers within the u area. */
355 #if !defined (U_REGS_OFFSET)
356 #define U_REGS_OFFSET \
357 ptrace (PT_READ_U, inferior_pid, \
358 (PTRACE_ARG3_TYPE) (offsetof (struct user, u_ar0)), 0) \
362 /* Registers we shouldn't try to fetch. */
363 #if !defined (CANNOT_FETCH_REGISTER)
364 #define CANNOT_FETCH_REGISTER(regno) 0
367 /* Fetch one register. */
370 fetch_register (int regno
)
372 /* This isn't really an address. But ptrace thinks of it as one. */
374 char mess
[128]; /* For messages */
376 unsigned int offset
; /* Offset of registers within the u area. */
377 char buf
[MAX_REGISTER_RAW_SIZE
];
380 if (CANNOT_FETCH_REGISTER (regno
))
382 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
383 supply_register (regno
, buf
);
387 /* Overload thread id onto process id */
388 if ((tid
= TIDGET (inferior_pid
)) == 0)
389 tid
= inferior_pid
; /* no thread id, just use process id */
391 offset
= U_REGS_OFFSET
;
393 regaddr
= register_addr (regno
, offset
);
394 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
397 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
398 (PTRACE_ARG3_TYPE
) regaddr
, 0);
399 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
402 sprintf (mess
, "reading register %s (#%d)",
403 REGISTER_NAME (regno
), regno
);
404 perror_with_name (mess
);
407 supply_register (regno
, buf
);
411 /* Fetch register values from the inferior.
412 If REGNO is negative, do this for all registers.
413 Otherwise, REGNO specifies which register (so we can save time). */
416 fetch_inferior_registers (int regno
)
420 fetch_register (regno
);
424 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
426 fetch_register (regno
);
431 /* Registers we shouldn't try to store. */
432 #if !defined (CANNOT_STORE_REGISTER)
433 #define CANNOT_STORE_REGISTER(regno) 0
436 /* Store one register. */
439 store_register (int regno
)
441 /* This isn't really an address. But ptrace thinks of it as one. */
443 char mess
[128]; /* For messages */
445 unsigned int offset
; /* Offset of registers within the u area. */
448 if (CANNOT_STORE_REGISTER (regno
))
453 /* Overload thread id onto process id */
454 if ((tid
= TIDGET (inferior_pid
)) == 0)
455 tid
= inferior_pid
; /* no thread id, just use process id */
457 offset
= U_REGS_OFFSET
;
459 regaddr
= register_addr (regno
, offset
);
460 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
463 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
464 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
465 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
468 sprintf (mess
, "writing register %s (#%d)",
469 REGISTER_NAME (regno
), regno
);
470 perror_with_name (mess
);
475 /* Store our register values back into the inferior.
476 If REGNO is negative, do this for all registers.
477 Otherwise, REGNO specifies which register (so we can save time). */
480 store_inferior_registers (int regno
)
484 store_register (regno
);
488 for (regno
= 0; regno
< ARCH_NUM_REGS
; regno
++)
490 store_register (regno
);
494 #endif /* !defined (FETCH_INFERIOR_REGISTERS). */
497 #if !defined (CHILD_XFER_MEMORY)
498 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
499 in the NEW_SUN_PTRACE case.
500 It ought to be straightforward. But it appears that writing did
501 not write the data that I specified. I cannot understand where
502 it got the data that it actually did write. */
504 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
505 to debugger memory starting at MYADDR. Copy to inferior if
506 WRITE is nonzero. TARGET is ignored.
508 Returns the length copied, which is either the LEN argument or zero.
509 This xfer function does not do partial moves, since child_ops
510 doesn't allow memory operations to cross below us in the target stack
514 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
515 struct mem_attrib
*attrib ATTRIBUTE_UNUSED
,
516 struct target_ops
*target
)
519 /* Round starting address down to longword boundary. */
520 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
521 /* Round ending address up; get number of longwords that makes. */
523 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
524 / sizeof (PTRACE_XFER_TYPE
);
525 /* Allocate buffer of that many longwords. */
526 register PTRACE_XFER_TYPE
*buffer
527 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
531 /* Fill start and end extra bytes of buffer with existing memory data. */
533 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
535 /* Need part of initial word -- fetch it. */
536 buffer
[0] = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
537 (PTRACE_ARG3_TYPE
) addr
, 0);
540 if (count
> 1) /* FIXME, avoid if even boundary */
543 = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
545 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
549 /* Copy data to be written over corresponding part of buffer */
551 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
555 /* Write the entire buffer. */
557 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
560 ptrace (PT_WRITE_D
, PIDGET (inferior_pid
),
561 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
564 /* Using the appropriate one (I or D) is necessary for
565 Gould NP1, at least. */
567 ptrace (PT_WRITE_I
, PIDGET (inferior_pid
),
568 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
573 #ifdef CLEAR_INSN_CACHE
579 /* Read all the longwords */
580 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
583 buffer
[i
] = ptrace (PT_READ_I
, PIDGET (inferior_pid
),
584 (PTRACE_ARG3_TYPE
) addr
, 0);
590 /* Copy appropriate bytes out of the buffer. */
592 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
600 udot_info (char *dummy1
, int dummy2
)
602 #if defined (KERNEL_U_SIZE)
603 int udot_off
; /* Offset into user struct */
604 int udot_val
; /* Value from user struct at udot_off */
605 char mess
[128]; /* For messages */
608 if (!target_has_execution
)
610 error ("The program is not being run.");
613 #if !defined (KERNEL_U_SIZE)
615 /* Adding support for this command is easy. Typically you just add a
616 routine, called "kernel_u_size" that returns the size of the user
617 struct, to the appropriate *-nat.c file and then add to the native
618 config file "#define KERNEL_U_SIZE kernel_u_size()" */
619 error ("Don't know how large ``struct user'' is in this version of gdb.");
623 for (udot_off
= 0; udot_off
< KERNEL_U_SIZE
; udot_off
+= sizeof (udot_val
))
625 if ((udot_off
% 24) == 0)
629 printf_filtered ("\n");
631 printf_filtered ("%04x:", udot_off
);
633 udot_val
= ptrace (PT_READ_U
, inferior_pid
, (PTRACE_ARG3_TYPE
) udot_off
, 0);
636 sprintf (mess
, "\nreading user struct at offset 0x%x", udot_off
);
637 perror_with_name (mess
);
639 /* Avoid using nonportable (?) "*" in print specs */
640 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val
);
642 printf_filtered ("\n");
646 #endif /* !defined (CHILD_XFER_MEMORY). */
650 _initialize_infptrace (void)
652 #if !defined (CHILD_XFER_MEMORY)
653 add_info ("udot", udot_info
,
654 "Print contents of kernel ``struct user'' for current child.");
This page took 0.048378 seconds and 4 git commands to generate.