1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
6 ## Contains temporary hacks..
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "frame.h" /* required by inferior.h */
32 #include <sys/types.h>
38 # ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
43 /* "wait.h" fills in the gaps left by <wait.h> */
49 extern struct symtab_and_line
*
50 child_enable_exception_callback
PARAMS ((enum exception_event_kind
, int));
52 extern struct exception_event_record
*
53 child_get_current_exception_event
PARAMS ((void));
56 child_prepare_to_store
PARAMS ((void));
59 static int child_wait
PARAMS ((int, struct target_waitstatus
*));
60 #endif /* CHILD_WAIT */
62 #if !defined(CHILD_POST_WAIT)
64 child_post_wait
PARAMS ((int, int));
67 static void child_open
PARAMS ((char *, int));
70 child_files_info
PARAMS ((struct target_ops
*));
73 child_detach
PARAMS ((char *, int));
76 child_detach_from_process
PARAMS ((int, char *, int, int));
79 child_attach
PARAMS ((char *, int));
82 child_attach_to_process
PARAMS ((char *, int, int));
84 #if !defined(CHILD_POST_ATTACH)
86 child_post_attach
PARAMS ((int));
90 child_require_attach
PARAMS ((char *, int));
93 child_require_detach
PARAMS ((int, char *, int));
96 ptrace_me
PARAMS ((void));
99 ptrace_him
PARAMS ((int));
102 child_create_inferior
PARAMS ((char *, char *, char **));
105 child_mourn_inferior
PARAMS ((void));
108 child_can_run
PARAMS ((void));
111 child_stop
PARAMS ((void));
113 #ifndef CHILD_THREAD_ALIVE
114 int child_thread_alive
PARAMS ((int));
117 extern char **environ
;
119 /* Forward declaration */
120 extern struct target_ops child_ops
;
122 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
123 be a runnable target. Used by targets
124 that can sit atop inftarg, such as HPUX
130 /* Enable HACK for ttrace work. In
131 * infttrace.c/require_notification_of_events,
132 * this is set to 0 so that the loop in child_wait
135 int not_same_real_pid
= 1;
139 /* Wait for child to do something. Return pid of child, or -1 in case
140 of error; store status through argument pointer OURSTATUS. */
143 child_wait (pid
, ourstatus
)
145 struct target_waitstatus
*ourstatus
;
149 char * execd_pathname
;
153 enum target_waitkind kind
;
156 set_sigint_trap(); /* Causes SIGINT to be passed on to the
160 pid
= proc_wait (inferior_pid
, &status
);
170 if (save_errno
== EINTR
)
173 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
174 safe_strerror (save_errno
));
176 /* Claim it exited with unknown signal. */
177 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
178 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
184 if (target_has_exited (pid
, status
, &exit_status
))
186 /* ??rehrauer: For now, ignore this. */
190 if (!target_thread_alive (pid
))
192 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
196 if (target_has_forked (pid
, &related_pid
)
197 && (pid
== inferior_pid
) || (related_pid
== inferior_pid
))
199 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
200 ourstatus
->value
.related_pid
= related_pid
;
204 if (target_has_vforked (pid
, &related_pid
)
205 && (pid
== inferior_pid
) || (related_pid
== inferior_pid
))
207 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
208 ourstatus
->value
.related_pid
= related_pid
;
212 if (target_has_execd (pid
, &execd_pathname
))
214 /* Are we ignoring initial exec events? (This is likely because
215 we're in the process of starting up the inferior, and another
216 (older) mechanism handles those.) If so, we'll report this
217 as a regular stop, not an exec.
219 if (inferior_ignoring_startup_exec_events
)
221 inferior_ignoring_startup_exec_events
--;
225 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
226 ourstatus
->value
.execd_pathname
= execd_pathname
;
231 /* All we must do with these is communicate their occurrence
232 to wait_for_inferior...
234 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
236 ourstatus
->kind
= kind
;
237 ourstatus
->value
.syscall_id
= syscall_id
;
241 /*## } while (pid != inferior_pid); ##*/ /* Some other child died or stopped */
242 /* hack for thread testing */
243 } while( (pid
!= inferior_pid
) && not_same_real_pid
);
246 store_waitstatus (ourstatus
, status
);
249 #endif /* CHILD_WAIT */
251 #if !defined(CHILD_POST_WAIT)
253 child_post_wait (pid
, wait_status
)
257 /* This version of Unix doesn't require a meaningful "post wait"
264 #ifndef CHILD_THREAD_ALIVE
266 /* Check to see if the given thread is alive.
268 FIXME: Is kill() ever the right way to do this? I doubt it, but
269 for now we're going to try and be compatable with the old thread
272 child_thread_alive (pid
)
275 return (kill (pid
, 0) != -1);
281 child_attach_to_process (args
, from_tty
, after_fork
)
287 error_no_arg ("process-id to attach");
289 #ifndef ATTACH_DETACH
290 error ("Can't attach to a process on this machine.");
295 struct target_waitstatus
*wstatus
;
299 pid
= strtol (args
, &dummy
, 0);
300 /* Some targets don't set errno on errors, grrr! */
301 if ((pid
== 0) && (args
== dummy
))
302 error ("Illegal process-id: %s\n", args
);
304 if (pid
== getpid()) /* Trying to masturbate? */
305 error ("I refuse to debug myself!");
309 exec_file
= (char *) get_exec_file (0);
312 printf_unfiltered ("Attaching after fork to %s\n",
313 target_pid_to_str (pid
));
315 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
316 target_pid_to_str (pid
));
318 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
320 gdb_flush (gdb_stdout
);
326 REQUIRE_ATTACH (pid
);
329 push_target (&child_ops
);
331 #endif /* ATTACH_DETACH */
335 /* Attach to process PID, then initialize for debugging it. */
338 child_attach (args
, from_tty
)
342 child_attach_to_process (args
, from_tty
, 0);
345 #if !defined(CHILD_POST_ATTACH)
347 child_post_attach (pid
)
350 /* This version of Unix doesn't require a meaningful "post attach"
351 operation by a debugger.
358 child_require_attach (args
, from_tty
)
362 child_attach_to_process (args
, from_tty
, 1);
366 child_detach_from_process (pid
, args
, from_tty
, after_fork
)
378 char *exec_file
= get_exec_file (0);
382 printf_unfiltered ("Detaching after fork from %s\n",
383 target_pid_to_str (pid
));
385 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
386 target_pid_to_str (pid
));
387 gdb_flush (gdb_stdout
);
390 siggnal
= atoi (args
);
395 REQUIRE_DETACH (pid
, siggnal
);
398 error ("This version of Unix does not support detaching a process.");
402 /* Take a program previously attached to and detaches it.
403 The program resumes execution and will no longer stop
404 on signals, etc. We'd better not have left any breakpoints
405 in the program or it'll die when it hits one. For this
406 to work, it may be necessary for the process to have been
407 previously attached. It *might* work if the program was
408 started via the normal ptrace (PTRACE_TRACEME). */
411 child_detach (args
, from_tty
)
415 child_detach_from_process (inferior_pid
, args
, from_tty
, 0);
417 unpush_target (&child_ops
);
421 child_require_detach (pid
, args
, from_tty
)
426 child_detach_from_process (pid
, args
, from_tty
, 1);
430 /* Get ready to modify the registers array. On machines which store
431 individual registers, this doesn't need to do anything. On machines
432 which store all the registers in one fell swoop, this makes sure
433 that registers contains all the registers from the program being
437 child_prepare_to_store ()
439 #ifdef CHILD_PREPARE_TO_STORE
440 CHILD_PREPARE_TO_STORE ();
444 /* Print status information about what we're accessing. */
447 child_files_info (ignore
)
448 struct target_ops
*ignore
;
450 printf_unfiltered ("\tUsing the running image of %s %s.\n",
451 attach_flag
? "attached": "child", target_pid_to_str (inferior_pid
));
456 child_open (arg
, from_tty
)
460 error ("Use the \"run\" command to start a Unix child process.");
463 /* Stub function which causes the inferior that runs it, to be ptrace-able
464 by its parent process. */
469 /* "Trace me, Dr. Memory!" */
470 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
473 /* Stub function which causes the GDB that runs it, to start ptrace-ing
474 the child process. */
480 push_target (&child_ops
);
482 /* On some targets, there must be some explicit synchronization
483 between the parent and child processes after the debugger
484 forks, and before the child execs the debuggee program. This
485 call basically gives permission for the child to exec.
488 target_acknowledge_created_inferior (pid
);
490 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
491 * and will be 1 or 2 depending on whether we're starting
492 * without or with a shell.
494 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
496 /* On some targets, there must be some explicit actions taken after
497 the inferior has been started up.
499 target_post_startup_inferior (pid
);
502 /* Start an inferior Unix child process and sets inferior_pid to its pid.
503 EXEC_FILE is the file to run.
504 ALLARGS is a string containing the arguments to the program.
505 ENV is the environment vector to pass. Errors reported with error(). */
508 child_create_inferior (exec_file
, allargs
, env
)
519 char *path
= getenv ("PATH");
523 /* On HP-UX, we have a possible bad interaction between
524 * the start-up-with-shell code and our catch-fork/catch-exec
525 * logic. To avoid the bad interaction, we start up with the
526 * C shell ("csh") and pass it the "-f" flag (fast start-up,
527 * don't run .cshrc code).
528 * See further comments in inferior.h toward the bottom
529 * (STARTUP_WITH_SHELL flag) and in fork-child.c
532 /* Rather than passing in a hard-wired path like "/bin/csh",
533 * we look down the PATH to find csh. I took this code from
534 * procfs.c, which is the file in the Sun-specific part of GDB
535 * analogous to inftarg.c. See procfs.c for more detailed
540 path
= "/bin:/usr/bin";
541 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
542 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
544 p1
= strchr (p
, ':');
549 strncpy (tryname
, p
, len
);
551 strcat (tryname
, "/");
552 strcat (tryname
, shell_file
);
553 if (access (tryname
, X_OK
) < 0)
555 if (stat (tryname
, &statbuf
) < 0)
557 if (!S_ISREG (statbuf
.st_mode
))
558 /* We certainly need to reject directories. I'm not quite
559 as sure about FIFOs, sockets, etc., but I kind of doubt
560 that people want to exec() these things. */
565 /* Not found. I replaced the error() which existed in procfs.c
566 * with simply passing in NULL and hoping fork_inferior()
567 * can deal with it. - RT
569 /* error ("Can't find shell %s in PATH", shell_file); */
572 shell_file
= tryname
;
574 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
576 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
578 /* We are at the first instruction we care about. */
579 /* Pedal to the metal... */
580 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
583 #if !defined(CHILD_POST_STARTUP_INFERIOR)
585 child_post_startup_inferior (pid
)
588 /* This version of Unix doesn't require a meaningful "post startup inferior"
589 operation by a debugger.
594 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
596 child_acknowledge_created_inferior (pid
)
599 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
600 operation by a debugger.
607 child_clone_and_follow_inferior (child_pid
, followed_child
)
611 clone_and_follow_inferior (child_pid
, followed_child
);
613 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
614 the decision gets made elsewhere how to continue it.
619 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
621 child_post_follow_inferior_by_clone ()
623 /* This version of Unix doesn't require a meaningful "post follow inferior"
624 operation by a clone debugger.
630 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
632 child_insert_fork_catchpoint (pid
)
635 /* This version of Unix doesn't support notification of fork events.
641 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
643 child_remove_fork_catchpoint (pid
)
646 /* This version of Unix doesn't support notification of fork events.
652 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
654 child_insert_vfork_catchpoint (pid
)
657 /* This version of Unix doesn't support notification of vfork events.
663 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
665 child_remove_vfork_catchpoint (pid
)
668 /* This version of Unix doesn't support notification of vfork events.
674 #if !defined(CHILD_HAS_FORKED)
676 child_has_forked (pid
, child_pid
)
680 /* This version of Unix doesn't support notification of fork events.
687 #if !defined(CHILD_HAS_VFORKED)
689 child_has_vforked (pid
, child_pid
)
693 /* This version of Unix doesn't support notification of vfork events.
700 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
702 child_can_follow_vfork_prior_to_exec ()
704 /* This version of Unix doesn't support notification of vfork events.
705 However, if it did, it probably wouldn't allow vforks to be followed
706 before the following exec.
713 #if !defined(CHILD_POST_FOLLOW_VFORK)
715 child_post_follow_vfork (parent_pid
, followed_parent
, child_pid
, followed_child
)
721 /* This version of Unix doesn't require a meaningful "post follow vfork"
722 operation by a clone debugger.
727 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
729 child_insert_exec_catchpoint (pid
)
732 /* This version of Unix doesn't support notification of exec events.
738 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
740 child_remove_exec_catchpoint (pid
)
743 /* This version of Unix doesn't support notification of exec events.
749 #if !defined(CHILD_HAS_EXECD)
751 child_has_execd (pid
, execd_pathname
)
753 char ** execd_pathname
;
755 /* This version of Unix doesn't support notification of exec events.
762 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
764 child_reported_exec_events_per_exec_call ()
766 /* This version of Unix doesn't support notification of exec events.
773 #if !defined(CHILD_HAS_SYSCALL_EVENT)
775 child_has_syscall_event (pid
, kind
, syscall_id
)
777 enum target_waitkind
* kind
;
780 /* This version of Unix doesn't support notification of syscall events.
787 #if !defined(CHILD_HAS_EXITED)
789 child_has_exited (pid
, wait_status
, exit_status
)
794 if (WIFEXITED (wait_status
))
796 *exit_status
= WEXITSTATUS (wait_status
);
800 if (WIFSIGNALED (wait_status
))
802 *exit_status
= 0; /* ?? Don't know what else to say here. */
806 /* ?? Do we really need to consult the event state, too? Assume the
807 wait_state alone suffices.
815 child_mourn_inferior ()
817 /* FIXME: Should be in a header file */
818 extern void proc_remove_foreign
PARAMS ((int));
820 unpush_target (&child_ops
);
821 proc_remove_foreign (inferior_pid
);
822 generic_mourn_inferior ();
828 /* This variable is controlled by modules that sit atop inftarg that may layer
829 their own process structure atop that provided here. hpux-thread.c does
830 this because of the Hpux user-mode level thread model. */
832 return !child_suppress_run
;
835 /* Send a SIGINT to the process group. This acts just like the user typed a
836 ^C on the controlling terminal.
838 XXX - This may not be correct for all systems. Some may want to use
839 killpg() instead of kill (-pgrp). */
844 extern pid_t inferior_process_group
;
846 kill (-inferior_process_group
, SIGINT
);
849 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
850 struct symtab_and_line
*
851 child_enable_exception_callback (kind
, enable
)
852 enum exception_event_kind kind
;
855 return (struct symtab_and_line
*) NULL
;
859 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
860 struct exception_event_record
*
861 child_get_current_exception_event ()
863 return (struct exception_event_record
*) NULL
;
868 #if !defined(CHILD_PID_TO_EXEC_FILE)
870 child_pid_to_exec_file (pid
)
873 /* This version of Unix doesn't support translation of a process ID
874 to the filename of the executable file.
881 child_core_file_to_sym_file (core
)
884 /* The target stratum for a running executable need not support
892 struct target_ops child_ops
= {
893 "child", /* to_shortname */
894 "Unix child process", /* to_longname */
895 "Unix child process (started by the \"run\" command).", /* to_doc */
896 child_open
, /* to_open */
898 child_attach
, /* to_attach */
899 child_post_attach
, /* to_post_attach */
900 child_require_attach
, /* to_require_attach */
901 child_detach
, /* to_detach */
902 child_require_detach
, /* to_require_detach */
903 child_resume
, /* to_resume */
904 child_wait
, /* to_wait */
905 child_post_wait
, /* to_post_wait */
906 fetch_inferior_registers
, /* to_fetch_registers */
907 store_inferior_registers
, /* to_store_registers */
908 child_prepare_to_store
, /* to_prepare_to_store */
909 child_xfer_memory
, /* to_xfer_memory */
910 child_files_info
, /* to_files_info */
911 memory_insert_breakpoint
, /* to_insert_breakpoint */
912 memory_remove_breakpoint
, /* to_remove_breakpoint */
913 terminal_init_inferior
, /* to_terminal_init */
914 terminal_inferior
, /* to_terminal_inferior */
915 terminal_ours_for_output
, /* to_terminal_ours_for_output */
916 terminal_ours
, /* to_terminal_ours */
917 child_terminal_info
, /* to_terminal_info */
918 kill_inferior
, /* to_kill */
920 0, /* to_lookup_symbol */
921 child_create_inferior
, /* to_create_inferior */
922 child_post_startup_inferior
, /* to_post_startup_inferior */
923 child_acknowledge_created_inferior
, /* to_acknowledge_created_inferior */
924 child_clone_and_follow_inferior
, /* to_clone_and_follow_inferior */
925 child_post_follow_inferior_by_clone
, /* to_post_follow_inferior_by_clone */
926 child_insert_fork_catchpoint
, /* to_insert_fork_catchpoint */
927 child_remove_fork_catchpoint
, /* to_remove_fork_catchpoint */
928 child_insert_vfork_catchpoint
, /* to_insert_vfork_catchpoint */
929 child_remove_vfork_catchpoint
, /* to_remove_vfork_catchpoint */
930 child_has_forked
, /* to_has_forked */
931 child_has_vforked
, /* to_has_vforked */
932 child_can_follow_vfork_prior_to_exec
, /* to_can_follow_vfork_prior_to_exec */
933 child_post_follow_vfork
, /* to_post_follow_vfork */
934 child_insert_exec_catchpoint
, /* to_insert_exec_catchpoint */
935 child_remove_exec_catchpoint
, /* to_remove_exec_catchpoint */
936 child_has_execd
, /* to_has_execd */
937 child_reported_exec_events_per_exec_call
, /* to_reported_exec_events_per_exec_call */
938 child_has_syscall_event
, /* to_has_syscall_event */
939 child_has_exited
, /* to_has_exited */
940 child_mourn_inferior
, /* to_mourn_inferior */
941 child_can_run
, /* to_can_run */
942 0, /* to_notice_signals */
943 child_thread_alive
, /* to_thread_alive */
944 child_stop
, /* to_stop */
945 child_enable_exception_callback
, /* to_enable_exception_callback */
946 child_get_current_exception_event
, /* to_get_current_exception_event */
947 child_pid_to_exec_file
, /* to_pid_to_exec_file */
948 child_core_file_to_sym_file
, /* to_core_file_to_sym_file */
949 process_stratum
, /* to_stratum */
951 1, /* to_has_all_memory */
952 1, /* to_has_memory */
953 1, /* to_has_stack */
954 1, /* to_has_registers */
955 1, /* to_has_execution */
957 0, /* to_sections_end */
958 OPS_MAGIC
/* to_magic */
962 _initialize_inftarg ()
964 #ifdef HAVE_OPTIONAL_PROC_FS
968 /* If we have an optional /proc filesystem (e.g. under OSF/1),
969 don't add ptrace support if we can access the running GDB via /proc. */
970 #ifndef PROC_NAME_FMT
971 #define PROC_NAME_FMT "/proc/%05d"
973 sprintf (procname
, PROC_NAME_FMT
, getpid ());
974 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
981 add_target (&child_ops
);