1 /* Target-vector operations for controlling Unix child processes, for GDB.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
6 Contributed by Cygnus Support.
8 ## Contains temporary hacks..
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. */
28 #include "frame.h" /* required by inferior.h */
35 #include <sys/types.h>
41 extern struct symtab_and_line
*child_enable_exception_callback (enum
45 extern struct exception_event_record
46 *child_get_current_exception_event (void);
48 extern void _initialize_inftarg (void);
50 static void child_prepare_to_store (void);
53 static ptid_t
child_wait (ptid_t
, struct target_waitstatus
*);
54 #endif /* CHILD_WAIT */
56 #if !defined(CHILD_POST_WAIT)
57 void child_post_wait (ptid_t
, int);
60 static void child_open (char *, int);
62 static void child_files_info (struct target_ops
*);
64 static void child_detach (char *, int);
66 static void child_attach (char *, int);
68 #if !defined(CHILD_POST_ATTACH)
69 extern void child_post_attach (int);
72 static void ptrace_me (void);
74 static void ptrace_him (int);
76 static void child_create_inferior (char *, char *, char **, int);
78 static void child_mourn_inferior (void);
80 static int child_can_run (void);
82 static void child_stop (void);
84 #ifndef CHILD_THREAD_ALIVE
85 int child_thread_alive (ptid_t
);
88 static void init_child_ops (void);
90 extern char **environ
;
92 struct target_ops child_ops
;
94 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
95 be a runnable target. Used by targets
96 that can sit atop inftarg, such as HPUX
101 /* Wait for child to do something. Return pid of child, or -1 in case
102 of error; store status through argument pointer OURSTATUS. */
105 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
109 char *execd_pathname
= NULL
;
113 enum target_waitkind kind
;
118 set_sigint_trap (); /* Causes SIGINT to be passed on to the
122 pid
= ptrace_wait (inferior_ptid
, &status
);
128 clear_sigint_trap ();
132 if (save_errno
== EINTR
)
135 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
136 safe_strerror (save_errno
));
138 /* Claim it exited with unknown signal. */
139 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
140 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
141 return pid_to_ptid (-1);
146 if (target_has_exited (pid
, status
, &exit_status
))
148 /* ??rehrauer: For now, ignore this. */
152 if (!target_thread_alive (pid_to_ptid (pid
)))
154 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
155 return pid_to_ptid (pid
);
157 } while (pid
!= PIDGET (inferior_ptid
)); /* Some other child died or stopped */
159 store_waitstatus (ourstatus
, status
);
160 return pid_to_ptid (pid
);
162 #endif /* CHILD_WAIT */
164 #if !defined(CHILD_POST_WAIT)
166 child_post_wait (ptid_t ptid
, int wait_status
)
168 /* This version of Unix doesn't require a meaningful "post wait"
175 #ifndef CHILD_THREAD_ALIVE
177 /* Check to see if the given thread is alive.
179 FIXME: Is kill() ever the right way to do this? I doubt it, but
180 for now we're going to try and be compatable with the old thread
183 child_thread_alive (ptid_t ptid
)
185 pid_t pid
= PIDGET (ptid
);
187 return (kill (pid
, 0) != -1);
192 /* Attach to process PID, then initialize for debugging it. */
195 child_attach (char *args
, int from_tty
)
198 error_no_arg ("process-id to attach");
200 #ifndef ATTACH_DETACH
201 error ("Can't attach to a process on this machine.");
209 pid
= strtol (args
, &dummy
, 0);
210 /* Some targets don't set errno on errors, grrr! */
211 if ((pid
== 0) && (args
== dummy
))
212 error ("Illegal process-id: %s\n", args
);
214 if (pid
== getpid ()) /* Trying to masturbate? */
215 error ("I refuse to debug myself!");
219 exec_file
= (char *) get_exec_file (0);
222 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
223 target_pid_to_str (pid_to_ptid (pid
)));
225 printf_unfiltered ("Attaching to %s\n",
226 target_pid_to_str (pid_to_ptid (pid
)));
228 gdb_flush (gdb_stdout
);
233 inferior_ptid
= pid_to_ptid (pid
);
234 push_target (&child_ops
);
236 /* Do this first, before anything has had a chance to query the
237 inferiors symbol table or similar. */
238 observer_notify_inferior_created (¤t_target
, from_tty
);
240 #endif /* ATTACH_DETACH */
243 #if !defined(CHILD_POST_ATTACH)
245 child_post_attach (int pid
)
247 /* This version of Unix doesn't require a meaningful "post attach"
248 operation by a debugger. */
252 /* Take a program previously attached to and detaches it.
253 The program resumes execution and will no longer stop
254 on signals, etc. We'd better not have left any breakpoints
255 in the program or it'll die when it hits one. For this
256 to work, it may be necessary for the process to have been
257 previously attached. It *might* work if the program was
258 started via the normal ptrace (PTRACE_TRACEME). */
261 child_detach (char *args
, int from_tty
)
266 int pid
= PIDGET (inferior_ptid
);
270 char *exec_file
= get_exec_file (0);
273 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
274 target_pid_to_str (pid_to_ptid (pid
)));
275 gdb_flush (gdb_stdout
);
278 siggnal
= atoi (args
);
282 inferior_ptid
= null_ptid
;
283 unpush_target (&child_ops
);
286 error ("This version of Unix does not support detaching a process.");
290 /* Get ready to modify the registers array. On machines which store
291 individual registers, this doesn't need to do anything. On machines
292 which store all the registers in one fell swoop, this makes sure
293 that registers contains all the registers from the program being
297 child_prepare_to_store (void)
299 #ifdef CHILD_PREPARE_TO_STORE
300 CHILD_PREPARE_TO_STORE ();
304 /* Print status information about what we're accessing. */
307 child_files_info (struct target_ops
*ignore
)
309 printf_unfiltered ("\tUsing the running image of %s %s.\n",
310 attach_flag
? "attached" : "child", target_pid_to_str (inferior_ptid
));
314 child_open (char *arg
, int from_tty
)
316 error ("Use the \"run\" command to start a Unix child process.");
319 /* Stub function which causes the inferior that runs it, to be ptrace-able
320 by its parent process. */
325 /* "Trace me, Dr. Memory!" */
326 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
329 /* Stub function which causes the GDB that runs it, to start ptrace-ing
330 the child process. */
335 push_target (&child_ops
);
337 /* On some targets, there must be some explicit synchronization
338 between the parent and child processes after the debugger
339 forks, and before the child execs the debuggee program. This
340 call basically gives permission for the child to exec.
343 target_acknowledge_created_inferior (pid
);
345 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
346 * and will be 1 or 2 depending on whether we're starting
347 * without or with a shell.
349 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
351 /* On some targets, there must be some explicit actions taken after
352 the inferior has been started up.
354 target_post_startup_inferior (pid_to_ptid (pid
));
357 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
358 EXEC_FILE is the file to run.
359 ALLARGS is a string containing the arguments to the program.
360 ENV is the environment vector to pass. Errors reported with error(). */
363 child_create_inferior (char *exec_file
, char *allargs
, char **env
,
367 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
369 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
371 /* We are at the first instruction we care about. */
372 observer_notify_inferior_created (¤t_target
, from_tty
);
373 /* Pedal to the metal... */
374 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
377 #if !defined(CHILD_POST_STARTUP_INFERIOR)
379 child_post_startup_inferior (ptid_t ptid
)
381 /* This version of Unix doesn't require a meaningful "post startup inferior"
382 operation by a debugger.
387 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
389 child_acknowledge_created_inferior (int pid
)
391 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
392 operation by a debugger.
398 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
400 child_insert_fork_catchpoint (int pid
)
402 /* This version of Unix doesn't support notification of fork events. */
407 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
409 child_remove_fork_catchpoint (int pid
)
411 /* This version of Unix doesn't support notification of fork events. */
416 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
418 child_insert_vfork_catchpoint (int pid
)
420 /* This version of Unix doesn't support notification of vfork events. */
425 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
427 child_remove_vfork_catchpoint (int pid
)
429 /* This version of Unix doesn't support notification of vfork events. */
434 #if !defined(CHILD_FOLLOW_FORK)
436 child_follow_fork (int follow_child
)
438 /* This version of Unix doesn't support following fork or vfork events. */
443 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
445 child_insert_exec_catchpoint (int pid
)
447 /* This version of Unix doesn't support notification of exec events. */
452 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
454 child_remove_exec_catchpoint (int pid
)
456 /* This version of Unix doesn't support notification of exec events. */
461 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
463 child_reported_exec_events_per_exec_call (void)
465 /* This version of Unix doesn't support notification of exec events.
471 #if !defined(CHILD_HAS_EXITED)
473 child_has_exited (int pid
, int wait_status
, int *exit_status
)
475 if (WIFEXITED (wait_status
))
477 *exit_status
= WEXITSTATUS (wait_status
);
481 if (WIFSIGNALED (wait_status
))
483 *exit_status
= 0; /* ?? Don't know what else to say here. */
487 /* ?? Do we really need to consult the event state, too? Assume the
488 wait_state alone suffices.
496 child_mourn_inferior (void)
498 unpush_target (&child_ops
);
499 generic_mourn_inferior ();
505 /* This variable is controlled by modules that sit atop inftarg that may layer
506 their own process structure atop that provided here. hpux-thread.c does
507 this because of the Hpux user-mode level thread model. */
509 return !child_suppress_run
;
512 /* Send a SIGINT to the process group. This acts just like the user typed a
513 ^C on the controlling terminal.
515 XXX - This may not be correct for all systems. Some may want to use
516 killpg() instead of kill (-pgrp). */
521 kill (-inferior_process_group
, SIGINT
);
524 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
525 struct symtab_and_line
*
526 child_enable_exception_callback (enum exception_event_kind kind
, int enable
)
528 return (struct symtab_and_line
*) NULL
;
532 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
533 struct exception_event_record
*
534 child_get_current_exception_event (void)
536 return (struct exception_event_record
*) NULL
;
541 #if !defined(CHILD_PID_TO_EXEC_FILE)
543 child_pid_to_exec_file (int pid
)
545 /* This version of Unix doesn't support translation of a process ID
546 to the filename of the executable file.
553 child_core_file_to_sym_file (char *core
)
555 /* The target stratum for a running executable need not support
561 /* Perform a partial transfer to/from the specified object. For
562 memory transfers, fall back to the old memory xfer functions. */
565 child_xfer_partial (struct target_ops
*ops
, enum target_object object
,
566 const char *annex
, void *readbuf
,
567 const void *writebuf
, ULONGEST offset
, LONGEST len
)
571 case TARGET_OBJECT_MEMORY
:
573 return child_xfer_memory (offset
, readbuf
, len
, 0/*write*/,
576 return child_xfer_memory (offset
, readbuf
, len
, 1/*write*/,
580 case TARGET_OBJECT_UNWIND_TABLE
:
581 #ifndef NATIVE_XFER_UNWIND_TABLE
582 #define NATIVE_XFER_UNWIND_TABLE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
584 return NATIVE_XFER_UNWIND_TABLE (ops
, object
, annex
, readbuf
, writebuf
,
587 case TARGET_OBJECT_AUXV
:
588 #ifndef NATIVE_XFER_AUXV
589 #define NATIVE_XFER_AUXV(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
591 return NATIVE_XFER_AUXV (ops
, object
, annex
, readbuf
, writebuf
,
594 case TARGET_OBJECT_WCOOKIE
:
595 #ifndef NATIVE_XFER_WCOOKIE
596 #define NATIVE_XFER_WCOOKIE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1)
598 return NATIVE_XFER_WCOOKIE (ops
, object
, annex
, readbuf
, writebuf
,
606 #if !defined(CHILD_PID_TO_STR)
608 child_pid_to_str (ptid_t ptid
)
610 return normal_pid_to_str (ptid
);
615 init_child_ops (void)
617 child_ops
.to_shortname
= "child";
618 child_ops
.to_longname
= "Unix child process";
619 child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
620 child_ops
.to_open
= child_open
;
621 child_ops
.to_attach
= child_attach
;
622 child_ops
.to_post_attach
= child_post_attach
;
623 child_ops
.to_detach
= child_detach
;
624 child_ops
.to_resume
= child_resume
;
625 child_ops
.to_wait
= child_wait
;
626 child_ops
.to_post_wait
= child_post_wait
;
627 child_ops
.to_fetch_registers
= fetch_inferior_registers
;
628 child_ops
.to_store_registers
= store_inferior_registers
;
629 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
630 child_ops
.to_xfer_memory
= child_xfer_memory
;
631 child_ops
.to_xfer_partial
= child_xfer_partial
;
632 child_ops
.to_files_info
= child_files_info
;
633 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
634 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
635 child_ops
.to_terminal_init
= terminal_init_inferior
;
636 child_ops
.to_terminal_inferior
= terminal_inferior
;
637 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
638 child_ops
.to_terminal_save_ours
= terminal_save_ours
;
639 child_ops
.to_terminal_ours
= terminal_ours
;
640 child_ops
.to_terminal_info
= child_terminal_info
;
641 child_ops
.to_kill
= kill_inferior
;
642 child_ops
.to_create_inferior
= child_create_inferior
;
643 child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
644 child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
645 child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
646 child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
647 child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
648 child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
649 child_ops
.to_follow_fork
= child_follow_fork
;
650 child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
651 child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
652 child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
653 child_ops
.to_has_exited
= child_has_exited
;
654 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
655 child_ops
.to_can_run
= child_can_run
;
656 child_ops
.to_thread_alive
= child_thread_alive
;
657 child_ops
.to_pid_to_str
= child_pid_to_str
;
658 child_ops
.to_stop
= child_stop
;
659 child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
660 child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
661 child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
662 child_ops
.to_stratum
= process_stratum
;
663 child_ops
.to_has_all_memory
= 1;
664 child_ops
.to_has_memory
= 1;
665 child_ops
.to_has_stack
= 1;
666 child_ops
.to_has_registers
= 1;
667 child_ops
.to_has_execution
= 1;
668 child_ops
.to_magic
= OPS_MAGIC
;
671 /* Take over the 'find_mapped_memory' vector from inftarg.c. */
673 inftarg_set_find_memory_regions (int (*func
) (int (*) (CORE_ADDR
,
679 child_ops
.to_find_memory_regions
= func
;
682 /* Take over the 'make_corefile_notes' vector from inftarg.c. */
684 inftarg_set_make_corefile_notes (char * (*func
) (bfd
*, int *))
686 child_ops
.to_make_corefile_notes
= func
;
690 _initialize_inftarg (void)
692 #ifdef HAVE_OPTIONAL_PROC_FS
696 /* If we have an optional /proc filesystem (e.g. under OSF/1),
697 don't add ptrace support if we can access the running GDB via /proc. */
698 #ifndef PROC_NAME_FMT
699 #define PROC_NAME_FMT "/proc/%05d"
701 sprintf (procname
, PROC_NAME_FMT
, getpid ());
702 fd
= open (procname
, O_RDONLY
);
711 add_target (&child_ops
);