2005-01-10 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gdb / inf-ptrace.c
CommitLineData
2c4a536d 1/* Low-level child interface to ptrace.
5bf970f9
AC
2
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1998, 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23#include "defs.h"
5bf970f9 24#include "command.h"
2c4a536d
MK
25#include "inferior.h"
26#include "inflow.h"
5bf970f9 27#include "gdbcore.h"
2c4a536d 28#include "observer.h"
5bf970f9 29
2c4a536d
MK
30#include "gdb_string.h"
31#include "gdb_ptrace.h"
34a17005 32#include "gdb_wait.h"
5bf970f9
AC
33#include <signal.h>
34
2c4a536d
MK
35#include "inf-child.h"
36
37/* HACK: Save the ptrace ops returned by inf_ptrace_target. */
5bf970f9
AC
38static struct target_ops *ptrace_ops_hack;
39
40static void
41inf_ptrace_kill_inferior (void)
42{
43 int status;
44 int pid = PIDGET (inferior_ptid);
45
46 if (pid == 0)
47 return;
48
49 /* This once used to call "kill" to kill the inferior just in case
50 the inferior was still running. As others have noted in the past
51 (kingdon) there shouldn't be any way to get here if the inferior
f6ffd89b 52 is still running -- else there's a major problem elsewere in GDB
5bf970f9
AC
53 and it needs to be fixed.
54
f6ffd89b
MK
55 The kill call causes problems under HP-UX 10, so it's been
56 removed; if this causes problems we'll deal with them as they
57 arise. */
62ece330
MK
58 ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3) 0, 0);
59 wait (&status);
5bf970f9
AC
60 target_mourn_inferior ();
61}
62
63/* Resume execution of the inferior process. If STEP is nonzero,
64 single-step it. If SIGNAL is nonzero, give it that signal. */
65
66static void
67inf_ptrace_resume (ptid_t ptid, int step, enum target_signal signal)
68{
69 int request = PT_CONTINUE;
70 int pid = PIDGET (ptid);
71
72 if (pid == -1)
73 /* Resume all threads. */
74 /* I think this only gets used in the non-threaded case, where
75 "resume all threads" and "resume inferior_ptid" are the
76 same. */
77 pid = PIDGET (inferior_ptid);
78
79 if (step)
80 {
81 /* If this system does not support PT_STEP, a higher level
82 function will have called single_step() to transmute the step
83 request into a continue request (by setting breakpoints on
84 all possible successor instructions), so we don't have to
85 worry about that here. */
86 request = PT_STEP;
87 }
88
89 /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
90 where it was. If GDB wanted it to start some other way, we have
91 already written a new PC value to the child. */
92 errno = 0;
93 ptrace (request, pid, (PTRACE_TYPE_ARG3) 1, target_signal_to_host (signal));
94 if (errno != 0)
95 perror_with_name ("ptrace");
96}
97
5bf970f9
AC
98/* Wait for child to do something. Return pid of child, or -1 in case
99 of error; store status through argument pointer OURSTATUS. */
100
101static ptid_t
102inf_ptrace_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
103{
104 int save_errno;
105 int status;
106 char *execd_pathname = NULL;
107 int exit_status;
108 int related_pid;
109 int syscall_id;
110 enum target_waitkind kind;
111 int pid;
112
113 do
114 {
115 set_sigint_trap (); /* Causes SIGINT to be passed on to the
116 attached process. */
117 set_sigio_trap ();
118
62ece330 119 pid = wait (&status);
5bf970f9
AC
120
121 save_errno = errno;
122
123 clear_sigio_trap ();
124
125 clear_sigint_trap ();
126
127 if (pid == -1)
128 {
129 if (save_errno == EINTR)
130 continue;
131
132 fprintf_unfiltered (gdb_stderr,
133 "Child process unexpectedly missing: %s.\n",
134 safe_strerror (save_errno));
135
136 /* Claim it exited with unknown signal. */
137 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
138 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
139 return pid_to_ptid (-1);
140 }
141
f6ffd89b 142 /* Did it exit? */
5bf970f9
AC
143 if (target_has_exited (pid, status, &exit_status))
144 {
145 /* ??rehrauer: For now, ignore this. */
146 continue;
147 }
148
149 if (!target_thread_alive (pid_to_ptid (pid)))
150 {
151 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
152 return pid_to_ptid (pid);
153 }
154 }
f6ffd89b
MK
155 while (pid != PIDGET (inferior_ptid)); /* Some other child died or
156 stopped. */
5bf970f9
AC
157
158 store_waitstatus (ourstatus, status);
159 return pid_to_ptid (pid);
160}
161
5bf970f9
AC
162/* Check to see if the given thread is alive.
163
164 FIXME: Is kill() ever the right way to do this? I doubt it, but
165 for now we're going to try and be compatable with the old thread
166 code. */
167
168static int
169inf_ptrace_thread_alive (ptid_t ptid)
170{
171 pid_t pid = PIDGET (ptid);
172
173 return (kill (pid, 0) != -1);
174}
175
176/* Attach to process PID, then initialize for debugging it. */
177
178static void
179inf_ptrace_attach (char *args, int from_tty)
180{
181 char *exec_file;
182 int pid;
183 char *dummy;
184
185 if (!args)
186 error_no_arg ("process-id to attach");
187
188 dummy = args;
189 pid = strtol (args, &dummy, 0);
f6ffd89b 190 /* Some targets don't set errno on errors, grrr! */
6e1e94ea 191 if (pid == 0 && args == dummy)
5bf970f9
AC
192 error ("Illegal process-id: %s\n", args);
193
f6ffd89b 194 if (pid == getpid ()) /* Trying to masturbate? */
5bf970f9
AC
195 error ("I refuse to debug myself!");
196
197 if (from_tty)
198 {
199 exec_file = (char *) get_exec_file (0);
200
201 if (exec_file)
202 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file,
203 target_pid_to_str (pid_to_ptid (pid)));
204 else
205 printf_unfiltered ("Attaching to %s\n",
206 target_pid_to_str (pid_to_ptid (pid)));
207
208 gdb_flush (gdb_stdout);
209 }
210
6e1e94ea
MK
211#ifdef PT_ATTACH
212 errno = 0;
213 ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3) 0, 0);
214 if (errno != 0)
215 perror_with_name ("ptrace");
216 attach_flag = 1;
217#else
218 error ("This system does not support attaching to a process");
219#endif
5bf970f9
AC
220
221 inferior_ptid = pid_to_ptid (pid);
222 push_target (ptrace_ops_hack);
12b8a2cb
DJ
223
224 /* Do this first, before anything has had a chance to query the
225 inferior's symbol table or similar. */
226 observer_notify_inferior_created (&current_target, from_tty);
5bf970f9
AC
227}
228
229static void
230inf_ptrace_post_attach (int pid)
231{
232 /* This version of Unix doesn't require a meaningful "post attach"
233 operation by a debugger. */
234}
235
236/* Take a program previously attached to and detaches it. The program
237 resumes execution and will no longer stop on signals, etc. We'd
238 better not have left any breakpoints in the program or it'll die
239 when it hits one. For this to work, it may be necessary for the
240 process to have been previously attached. It *might* work if the
241 program was started via the normal ptrace (PTRACE_TRACEME). */
242
243static void
244inf_ptrace_detach (char *args, int from_tty)
245{
6e1e94ea 246 int sig = 0;
5bf970f9
AC
247 int pid = PIDGET (inferior_ptid);
248
249 if (from_tty)
250 {
251 char *exec_file = get_exec_file (0);
252 if (exec_file == 0)
253 exec_file = "";
254 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file,
255 target_pid_to_str (pid_to_ptid (pid)));
256 gdb_flush (gdb_stdout);
257 }
258 if (args)
6e1e94ea 259 sig = atoi (args);
5bf970f9 260
6e1e94ea
MK
261#ifdef PT_DETACH
262 errno = 0;
263 ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3) 1, sig);
264 if (errno != 0)
265 perror_with_name ("ptrace");
266 attach_flag = 0;
267#else
268 error ("This system does not support detaching from a process");
269#endif
5bf970f9
AC
270
271 inferior_ptid = null_ptid;
272 unpush_target (ptrace_ops_hack);
273}
274
275/* Get ready to modify the registers array. On machines which store
276 individual registers, this doesn't need to do anything. On
277 machines which store all the registers in one fell swoop, this
278 makes sure that registers contains all the registers from the
279 program being debugged. */
280
281static void
282inf_ptrace_prepare_to_store (void)
283{
284}
285
286/* Print status information about what we're accessing. */
287
288static void
289inf_ptrace_files_info (struct target_ops *ignore)
290{
291 printf_unfiltered ("\tUsing the running image of %s %s.\n",
292 attach_flag ? "attached" : "child",
293 target_pid_to_str (inferior_ptid));
294}
295
296static void
297inf_ptrace_open (char *arg, int from_tty)
298{
299 error ("Use the \"run\" command to start a Unix child process.");
300}
301
302/* Stub function which causes the inferior that runs it, to be ptrace-able
303 by its parent process. */
304
305static void
306inf_ptrace_me (void)
307{
f6ffd89b 308 /* "Trace me, Dr. Memory!" */
62ece330 309 ptrace (0, 0, (PTRACE_TYPE_ARG3) 0, 0);
5bf970f9
AC
310}
311
312/* Stub function which causes the GDB that runs it, to start ptrace-ing
313 the child process. */
314
315static void
316inf_ptrace_him (int pid)
317{
318 push_target (ptrace_ops_hack);
319
320 /* On some targets, there must be some explicit synchronization
321 between the parent and child processes after the debugger
322 forks, and before the child execs the debuggee program. This
f6ffd89b 323 call basically gives permission for the child to exec. */
5bf970f9
AC
324
325 target_acknowledge_created_inferior (pid);
326
f6ffd89b
MK
327 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h, and will
328 be 1 or 2 depending on whether we're starting without or with a
329 shell. */
5bf970f9
AC
330 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
331
332 /* On some targets, there must be some explicit actions taken after
f6ffd89b 333 the inferior has been started up. */
5bf970f9
AC
334 target_post_startup_inferior (pid_to_ptid (pid));
335}
336
337/* Start an inferior Unix child process and sets inferior_ptid to its
338 pid. EXEC_FILE is the file to run. ALLARGS is a string containing
339 the arguments to the program. ENV is the environment vector to
340 pass. Errors reported with error(). */
341
342static void
343inf_ptrace_create_inferior (char *exec_file, char *allargs, char **env,
344 int from_tty)
345{
346 fork_inferior (exec_file, allargs, env, inf_ptrace_me, inf_ptrace_him,
347 NULL, NULL);
348 /* We are at the first instruction we care about. */
349 observer_notify_inferior_created (&current_target, from_tty);
f6ffd89b 350 /* Pedal to the metal... */
5bf970f9
AC
351 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
352}
353
354static void
355inf_ptrace_post_startup_inferior (ptid_t ptid)
356{
f6ffd89b
MK
357 /* This version of Unix doesn't require a meaningful "post startup
358 inferior" operation by a debugger. */
5bf970f9
AC
359}
360
361static void
362inf_ptrace_acknowledge_created_inferior (int pid)
363{
f6ffd89b
MK
364 /* This version of Unix doesn't require a meaningful "acknowledge
365 created inferior" operation by a debugger. */
5bf970f9
AC
366}
367
368static int
369inf_ptrace_insert_fork_catchpoint (int pid)
370{
371 /* This version of Unix doesn't support notification of fork events. */
372 return 0;
373}
374
375static int
376inf_ptrace_remove_fork_catchpoint (int pid)
377{
378 /* This version of Unix doesn't support notification of fork events. */
379 return 0;
380}
381
382static int
383inf_ptrace_insert_vfork_catchpoint (int pid)
384{
385 /* This version of Unix doesn't support notification of vfork events. */
386 return 0;
387}
388
389static int
390inf_ptrace_remove_vfork_catchpoint (int pid)
391{
392 /* This version of Unix doesn't support notification of vfork events. */
393 return 0;
394}
395
396static int
397inf_ptrace_follow_fork (int follow_child)
398{
399 /* This version of Unix doesn't support following fork or vfork events. */
400 return 0;
401}
402
403static int
404inf_ptrace_insert_exec_catchpoint (int pid)
405{
406 /* This version of Unix doesn't support notification of exec events. */
407 return 0;
408}
409
410static int
411inf_ptrace_remove_exec_catchpoint (int pid)
412{
413 /* This version of Unix doesn't support notification of exec events. */
414 return 0;
415}
416
417static int
418inf_ptrace_reported_exec_events_per_exec_call (void)
419{
f6ffd89b 420 /* This version of Unix doesn't support notification of exec events. */
5bf970f9
AC
421 return 1;
422}
423
424static int
425inf_ptrace_has_exited (int pid, int wait_status, int *exit_status)
426{
427 if (WIFEXITED (wait_status))
428 {
429 *exit_status = WEXITSTATUS (wait_status);
430 return 1;
431 }
432
433 if (WIFSIGNALED (wait_status))
434 {
435 *exit_status = 0; /* ?? Don't know what else to say here. */
436 return 1;
437 }
438
f6ffd89b
MK
439 /* ??? Do we really need to consult the event state, too?
440 Assume the wait_state alone suffices. */
5bf970f9
AC
441 return 0;
442}
443
444static void
445inf_ptrace_mourn_inferior (void)
446{
447 unpush_target (ptrace_ops_hack);
448 generic_mourn_inferior ();
449}
450
451static int
452inf_ptrace_can_run (void)
453{
454 return 1;
455}
456
457/* Send a SIGINT to the process group. This acts just like the user
458 typed a ^C on the controlling terminal.
459
f6ffd89b
MK
460 FIXME: This may not be correct for all systems. Some may want to
461 use killpg() instead of kill (-pgrp). */
5bf970f9
AC
462
463static void
464inf_ptrace_stop (void)
465{
466 kill (-inferior_process_group, SIGINT);
467}
468
469/* Perform a partial transfer to/from the specified object. For
470 memory transfers, fall back to the old memory xfer functions. */
471
472static LONGEST
473inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
474 const char *annex, void *readbuf,
475 const void *writebuf, ULONGEST offset, LONGEST len)
476{
477 switch (object)
478 {
479 case TARGET_OBJECT_MEMORY:
f929a579
AC
480#ifdef PT_IO
481 /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
482 request that promises to be much more efficient in reading
483 and writing data in the traced process's address space. */
484 {
485 struct ptrace_io_desc piod;
486
487 /* NOTE: We assume that there are no distinct address spaces
488 for instruction and data. */
489 piod.piod_op = writebuf ? PIOD_WRITE_D : PIOD_READ_D;
490 piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
491 piod.piod_offs = (void *) (long) offset;
492 piod.piod_len = len;
493
494 errno = 0;
495 if (ptrace (PT_IO, PIDGET (inferior_ptid), (caddr_t) &piod, 0) == 0)
496 /* Return the actual number of bytes read or written. */
497 return piod.piod_len;
498 /* If the PT_IO request is somehow not supported, fallback on
499 using PT_WRITE_D/PT_READ_D. Otherwise we will return zero
500 to indicate failure. */
501 if (errno != EINVAL)
502 return 0;
503 }
504#endif
505 {
506 union
507 {
508 PTRACE_TYPE_RET word;
509 unsigned char byte[sizeof (PTRACE_TYPE_RET)];
510 } buffer;
511 ULONGEST rounded_offset;
512 LONGEST partial_len;
513
cb85a953
AC
514 /* Round the start offset down to the next long word
515 boundary. */
f929a579
AC
516 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
517
cb85a953
AC
518 /* Since ptrace will transfer a single word starting at that
519 rounded_offset the partial_len needs to be adjusted down to
520 that (remember this function only does a single transfer).
521 Should the required length be even less, adjust it down
522 again. */
523 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
524 if (partial_len > len)
f929a579
AC
525 partial_len = len;
526
527 if (writebuf)
528 {
cb85a953
AC
529 /* If OFFSET:PARTIAL_LEN is smaller than
530 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
531 be needed. Read in the entire word. */
f929a579 532 if (rounded_offset < offset
cb85a953
AC
533 || (offset + partial_len
534 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
f929a579
AC
535 /* Need part of initial word -- fetch it. */
536 buffer.word = ptrace (PT_READ_I, PIDGET (inferior_ptid),
537 (PTRACE_TYPE_ARG3) (long) rounded_offset,
538 0);
539
540 /* Copy data to be written over corresponding part of
541 buffer. */
f6ffd89b
MK
542 memcpy (buffer.byte + (offset - rounded_offset),
543 writebuf, partial_len);
f929a579
AC
544
545 errno = 0;
546 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
547 (PTRACE_TYPE_ARG3) (long) rounded_offset,
cb85a953 548 buffer.word);
f929a579
AC
549 if (errno)
550 {
551 /* Using the appropriate one (I or D) is necessary for
552 Gould NP1, at least. */
553 errno = 0;
554 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
555 (PTRACE_TYPE_ARG3) (long) rounded_offset,
cb85a953 556 buffer.word);
f929a579
AC
557 if (errno)
558 return 0;
559 }
560 }
561 if (readbuf)
562 {
563 errno = 0;
564 buffer.word = ptrace (PT_READ_I, PIDGET (inferior_ptid),
565 (PTRACE_TYPE_ARG3) (long) rounded_offset, 0);
566 if (errno)
567 return 0;
568 /* Copy appropriate bytes out of the buffer. */
569 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
570 partial_len);
571 }
572 return partial_len;
573 }
5bf970f9
AC
574
575 case TARGET_OBJECT_UNWIND_TABLE:
576 return -1;
577
578 case TARGET_OBJECT_AUXV:
579 return -1;
580
581 case TARGET_OBJECT_WCOOKIE:
582 return -1;
583
584 default:
585 return -1;
586 }
587}
588
589static char *
590inf_ptrace_pid_to_str (ptid_t ptid)
591{
592 return normal_pid_to_str (ptid);
593}
594
595struct target_ops *
596inf_ptrace_target (void)
597{
598 struct target_ops *t = inf_child_target ();
599 t->to_open = inf_ptrace_open;
600 t->to_attach = inf_ptrace_attach;
601 t->to_post_attach = inf_ptrace_post_attach;
602 t->to_detach = inf_ptrace_detach;
603 t->to_resume = inf_ptrace_resume;
604 t->to_wait = inf_ptrace_wait;
5bf970f9 605 t->to_prepare_to_store = inf_ptrace_prepare_to_store;
5bf970f9
AC
606 t->to_xfer_partial = inf_ptrace_xfer_partial;
607 t->to_files_info = inf_ptrace_files_info;
608 t->to_kill = inf_ptrace_kill_inferior;
609 t->to_create_inferior = inf_ptrace_create_inferior;
610 t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
611 t->to_acknowledge_created_inferior =
612 inf_ptrace_acknowledge_created_inferior;
613 t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
614 t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
615 t->to_insert_vfork_catchpoint = inf_ptrace_insert_vfork_catchpoint;
616 t->to_remove_vfork_catchpoint = inf_ptrace_remove_vfork_catchpoint;
617 t->to_follow_fork = inf_ptrace_follow_fork;
618 t->to_insert_exec_catchpoint = inf_ptrace_insert_exec_catchpoint;
619 t->to_remove_exec_catchpoint = inf_ptrace_remove_exec_catchpoint;
620 t->to_reported_exec_events_per_exec_call =
621 inf_ptrace_reported_exec_events_per_exec_call;
622 t->to_has_exited = inf_ptrace_has_exited;
623 t->to_mourn_inferior = inf_ptrace_mourn_inferior;
624 t->to_can_run = inf_ptrace_can_run;
625 t->to_thread_alive = inf_ptrace_thread_alive;
626 t->to_pid_to_str = inf_ptrace_pid_to_str;
627 t->to_stop = inf_ptrace_stop;
628 t->to_stratum = process_stratum;
629 t->to_has_all_memory = 1;
630 t->to_has_memory = 1;
631 t->to_has_stack = 1;
632 t->to_has_registers = 1;
633 t->to_has_execution = 1;
634 t->to_magic = OPS_MAGIC;
635 ptrace_ops_hack = t;
636 return t;
637}
This page took 0.073186 seconds and 4 git commands to generate.