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> */
45 #include <sys/unistd.h>
47 extern struct symtab_and_line
*
48 child_enable_exception_callback
PARAMS ((enum exception_event_kind
, int));
50 extern struct exception_event_record
*
51 child_get_current_exception_event
PARAMS ((void));
54 child_prepare_to_store
PARAMS ((void));
57 static int child_wait
PARAMS ((int, struct target_waitstatus
*));
58 #endif /* CHILD_WAIT */
60 #if !defined(CHILD_POST_WAIT)
62 child_post_wait
PARAMS ((int, int));
65 static void child_open
PARAMS ((char *, int));
68 child_files_info
PARAMS ((struct target_ops
*));
71 child_detach
PARAMS ((char *, int));
74 child_detach_from_process
PARAMS ((int, char *, int, int));
77 child_attach
PARAMS ((char *, int));
80 child_attach_to_process
PARAMS ((char *, int, int));
82 #if !defined(CHILD_POST_ATTACH)
84 child_post_attach
PARAMS ((int));
88 child_require_attach
PARAMS ((char *, int));
91 child_require_detach
PARAMS ((int, char *, int));
94 ptrace_me
PARAMS ((void));
97 ptrace_him
PARAMS ((int));
100 child_create_inferior
PARAMS ((char *, char *, char **));
103 child_mourn_inferior
PARAMS ((void));
106 child_can_run
PARAMS ((void));
109 child_stop
PARAMS ((void));
111 #ifndef CHILD_THREAD_ALIVE
112 int child_thread_alive
PARAMS ((int));
115 extern char **environ
;
117 /* Forward declaration */
118 extern struct target_ops child_ops
;
120 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
121 be a runnable target. Used by targets
122 that can sit atop inftarg, such as HPUX
128 /* Enable HACK for ttrace work. In
129 * infttrace.c/require_notification_of_events,
130 * this is set to 0 so that the loop in child_wait
133 int not_same_real_pid
= 1;
137 /* Wait for child to do something. Return pid of child, or -1 in case
138 of error; store status through argument pointer OURSTATUS. */
141 child_wait (pid
, ourstatus
)
143 struct target_waitstatus
*ourstatus
;
147 char * execd_pathname
;
151 enum target_waitkind kind
;
154 set_sigint_trap(); /* Causes SIGINT to be passed on to the
158 pid
= proc_wait (inferior_pid
, &status
);
168 if (save_errno
== EINTR
)
171 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
172 safe_strerror (save_errno
));
174 /* Claim it exited with unknown signal. */
175 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
176 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
182 if (target_has_exited (pid
, status
, &exit_status
))
184 /* ??rehrauer: For now, ignore this. */
188 if (!target_thread_alive (pid
))
190 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
194 if (target_has_forked (pid
, &related_pid
)
195 && (pid
== inferior_pid
) || (related_pid
== inferior_pid
))
197 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
198 ourstatus
->value
.related_pid
= related_pid
;
202 if (target_has_vforked (pid
, &related_pid
)
203 && (pid
== inferior_pid
) || (related_pid
== inferior_pid
))
205 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
206 ourstatus
->value
.related_pid
= related_pid
;
210 if (target_has_execd (pid
, &execd_pathname
))
212 /* Are we ignoring initial exec events? (This is likely because
213 we're in the process of starting up the inferior, and another
214 (older) mechanism handles those.) If so, we'll report this
215 as a regular stop, not an exec.
217 if (inferior_ignoring_startup_exec_events
)
219 inferior_ignoring_startup_exec_events
--;
223 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
224 ourstatus
->value
.execd_pathname
= execd_pathname
;
229 /* All we must do with these is communicate their occurrence
230 to wait_for_inferior...
232 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
234 ourstatus
->kind
= kind
;
235 ourstatus
->value
.syscall_id
= syscall_id
;
239 /*## } while (pid != inferior_pid); ##*/ /* Some other child died or stopped */
240 /* hack for thread testing */
241 } while( (pid
!= inferior_pid
) && not_same_real_pid
);
244 store_waitstatus (ourstatus
, status
);
247 #endif /* CHILD_WAIT */
249 #if !defined(CHILD_POST_WAIT)
251 child_post_wait (pid
, wait_status
)
255 /* This version of Unix doesn't require a meaningful "post wait"
262 #ifndef CHILD_THREAD_ALIVE
264 /* Check to see if the given thread is alive.
266 FIXME: Is kill() ever the right way to do this? I doubt it, but
267 for now we're going to try and be compatable with the old thread
270 child_thread_alive (pid
)
273 return (kill (pid
, 0) != -1);
279 child_attach_to_process (args
, from_tty
, after_fork
)
285 error_no_arg ("process-id to attach");
287 #ifndef ATTACH_DETACH
288 error ("Can't attach to a process on this machine.");
293 struct target_waitstatus
*wstatus
;
297 pid
= strtol (args
, &dummy
, 0);
298 /* Some targets don't set errno on errors, grrr! */
299 if ((pid
== 0) && (args
== dummy
))
300 error ("Illegal process-id: %s\n", args
);
302 if (pid
== getpid()) /* Trying to masturbate? */
303 error ("I refuse to debug myself!");
307 exec_file
= (char *) get_exec_file (0);
310 printf_unfiltered ("Attaching after fork to %s\n",
311 target_pid_to_str (pid
));
313 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
314 target_pid_to_str (pid
));
316 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
318 gdb_flush (gdb_stdout
);
324 REQUIRE_ATTACH (pid
);
327 push_target (&child_ops
);
329 #endif /* ATTACH_DETACH */
333 /* Attach to process PID, then initialize for debugging it. */
336 child_attach (args
, from_tty
)
340 child_attach_to_process (args
, from_tty
, 0);
343 #if !defined(CHILD_POST_ATTACH)
345 child_post_attach (pid
)
348 /* This version of Unix doesn't require a meaningful "post attach"
349 operation by a debugger.
356 child_require_attach (args
, from_tty
)
360 child_attach_to_process (args
, from_tty
, 1);
364 child_detach_from_process (pid
, args
, from_tty
, after_fork
)
376 char *exec_file
= get_exec_file (0);
380 printf_unfiltered ("Detaching after fork from %s\n",
381 target_pid_to_str (pid
));
383 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
384 target_pid_to_str (pid
));
385 gdb_flush (gdb_stdout
);
388 siggnal
= atoi (args
);
393 REQUIRE_DETACH (pid
, siggnal
);
396 error ("This version of Unix does not support detaching a process.");
400 /* Take a program previously attached to and detaches it.
401 The program resumes execution and will no longer stop
402 on signals, etc. We'd better not have left any breakpoints
403 in the program or it'll die when it hits one. For this
404 to work, it may be necessary for the process to have been
405 previously attached. It *might* work if the program was
406 started via the normal ptrace (PTRACE_TRACEME). */
409 child_detach (args
, from_tty
)
413 child_detach_from_process (inferior_pid
, args
, from_tty
, 0);
415 unpush_target (&child_ops
);
419 child_require_detach (pid
, args
, from_tty
)
424 child_detach_from_process (pid
, args
, from_tty
, 1);
428 /* Get ready to modify the registers array. On machines which store
429 individual registers, this doesn't need to do anything. On machines
430 which store all the registers in one fell swoop, this makes sure
431 that registers contains all the registers from the program being
435 child_prepare_to_store ()
437 #ifdef CHILD_PREPARE_TO_STORE
438 CHILD_PREPARE_TO_STORE ();
442 /* Print status information about what we're accessing. */
445 child_files_info (ignore
)
446 struct target_ops
*ignore
;
448 printf_unfiltered ("\tUsing the running image of %s %s.\n",
449 attach_flag
? "attached": "child", target_pid_to_str (inferior_pid
));
454 child_open (arg
, from_tty
)
458 error ("Use the \"run\" command to start a Unix child process.");
461 /* Stub function which causes the inferior that runs it, to be ptrace-able
462 by its parent process. */
467 /* "Trace me, Dr. Memory!" */
468 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
471 /* Stub function which causes the GDB that runs it, to start ptrace-ing
472 the child process. */
478 push_target (&child_ops
);
480 /* On some targets, there must be some explicit synchronization
481 between the parent and child processes after the debugger
482 forks, and before the child execs the debuggee program. This
483 call basically gives permission for the child to exec.
486 target_acknowledge_created_inferior (pid
);
488 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
489 * and will be 1 or 2 depending on whether we're starting
490 * without or with a shell.
492 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
494 /* On some targets, there must be some explicit actions taken after
495 the inferior has been started up.
497 target_post_startup_inferior (pid
);
500 /* Start an inferior Unix child process and sets inferior_pid to its pid.
501 EXEC_FILE is the file to run.
502 ALLARGS is a string containing the arguments to the program.
503 ENV is the environment vector to pass. Errors reported with error(). */
506 child_create_inferior (exec_file
, allargs
, env
)
517 char *path
= getenv ("PATH");
521 /* On HP-UX, we have a possible bad interaction between
522 * the start-up-with-shell code and our catch-fork/catch-exec
523 * logic. To avoid the bad interaction, we start up with the
524 * C shell ("csh") and pass it the "-f" flag (fast start-up,
525 * don't run .cshrc code).
526 * See further comments in inferior.h toward the bottom
527 * (STARTUP_WITH_SHELL flag) and in fork-child.c
530 /* Rather than passing in a hard-wired path like "/bin/csh",
531 * we look down the PATH to find csh. I took this code from
532 * procfs.c, which is the file in the Sun-specific part of GDB
533 * analogous to inftarg.c. See procfs.c for more detailed
538 path
= "/bin:/usr/bin";
539 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
540 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
542 p1
= strchr (p
, ':');
547 strncpy (tryname
, p
, len
);
549 strcat (tryname
, "/");
550 strcat (tryname
, shell_file
);
551 if (access (tryname
, X_OK
) < 0)
553 if (stat (tryname
, &statbuf
) < 0)
555 if (!S_ISREG (statbuf
.st_mode
))
556 /* We certainly need to reject directories. I'm not quite
557 as sure about FIFOs, sockets, etc., but I kind of doubt
558 that people want to exec() these things. */
563 /* Not found. I replaced the error() which existed in procfs.c
564 * with simply passing in NULL and hoping fork_inferior()
565 * can deal with it. - RT
567 /* error ("Can't find shell %s in PATH", shell_file); */
570 shell_file
= tryname
;
572 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, shell_file
);
574 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
576 /* We are at the first instruction we care about. */
577 /* Pedal to the metal... */
578 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
581 #if !defined(CHILD_POST_STARTUP_INFERIOR)
583 child_post_startup_inferior (pid
)
586 /* This version of Unix doesn't require a meaningful "post startup inferior"
587 operation by a debugger.
592 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
594 child_acknowledge_created_inferior (pid
)
597 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
598 operation by a debugger.
605 child_clone_and_follow_inferior (child_pid
, followed_child
)
609 clone_and_follow_inferior (child_pid
, followed_child
);
611 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
612 the decision gets made elsewhere how to continue it.
617 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
619 child_post_follow_inferior_by_clone ()
621 /* This version of Unix doesn't require a meaningful "post follow inferior"
622 operation by a clone debugger.
628 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
630 child_insert_fork_catchpoint (pid
)
633 /* This version of Unix doesn't support notification of fork events.
639 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
641 child_remove_fork_catchpoint (pid
)
644 /* This version of Unix doesn't support notification of fork events.
650 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
652 child_insert_vfork_catchpoint (pid
)
655 /* This version of Unix doesn't support notification of vfork events.
661 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
663 child_remove_vfork_catchpoint (pid
)
666 /* This version of Unix doesn't support notification of vfork events.
672 #if !defined(CHILD_HAS_FORKED)
674 child_has_forked (pid
, child_pid
)
678 /* This version of Unix doesn't support notification of fork events.
685 #if !defined(CHILD_HAS_VFORKED)
687 child_has_vforked (pid
, child_pid
)
691 /* This version of Unix doesn't support notification of vfork events.
698 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
700 child_can_follow_vfork_prior_to_exec ()
702 /* This version of Unix doesn't support notification of vfork events.
703 However, if it did, it probably wouldn't allow vforks to be followed
704 before the following exec.
711 #if !defined(CHILD_POST_FOLLOW_VFORK)
713 child_post_follow_vfork (parent_pid
, followed_parent
, child_pid
, followed_child
)
719 /* This version of Unix doesn't require a meaningful "post follow vfork"
720 operation by a clone debugger.
725 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
727 child_insert_exec_catchpoint (pid
)
730 /* This version of Unix doesn't support notification of exec events.
736 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
738 child_remove_exec_catchpoint (pid
)
741 /* This version of Unix doesn't support notification of exec events.
747 #if !defined(CHILD_HAS_EXECD)
749 child_has_execd (pid
, execd_pathname
)
751 char ** execd_pathname
;
753 /* This version of Unix doesn't support notification of exec events.
760 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
762 child_reported_exec_events_per_exec_call ()
764 /* This version of Unix doesn't support notification of exec events.
771 #if !defined(CHILD_HAS_SYSCALL_EVENT)
773 child_has_syscall_event (pid
, kind
, syscall_id
)
775 enum target_waitkind
* kind
;
778 /* This version of Unix doesn't support notification of syscall events.
785 #if !defined(CHILD_HAS_EXITED)
787 child_has_exited (pid
, wait_status
, exit_status
)
792 if (WIFEXITED (wait_status
))
794 *exit_status
= WEXITSTATUS (wait_status
);
798 if (WIFSIGNALED (wait_status
))
800 *exit_status
= 0; /* ?? Don't know what else to say here. */
804 /* ?? Do we really need to consult the event state, too? Assume the
805 wait_state alone suffices.
813 child_mourn_inferior ()
815 /* FIXME: Should be in a header file */
816 extern void proc_remove_foreign
PARAMS ((int));
818 unpush_target (&child_ops
);
819 proc_remove_foreign (inferior_pid
);
820 generic_mourn_inferior ();
826 /* This variable is controlled by modules that sit atop inftarg that may layer
827 their own process structure atop that provided here. hpux-thread.c does
828 this because of the Hpux user-mode level thread model. */
830 return !child_suppress_run
;
833 /* Send a SIGINT to the process group. This acts just like the user typed a
834 ^C on the controlling terminal.
836 XXX - This may not be correct for all systems. Some may want to use
837 killpg() instead of kill (-pgrp). */
842 extern pid_t inferior_process_group
;
844 kill (-inferior_process_group
, SIGINT
);
847 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
848 struct symtab_and_line
*
849 child_enable_exception_callback (kind
, enable
)
850 enum exception_event_kind kind
;
853 return (struct symtab_and_line
*) NULL
;
857 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
858 struct exception_event_record
*
859 child_get_current_exception_event ()
861 return (struct exception_event_record
*) NULL
;
866 #if !defined(CHILD_PID_TO_EXEC_FILE)
868 child_pid_to_exec_file (pid
)
871 /* This version of Unix doesn't support translation of a process ID
872 to the filename of the executable file.
879 child_core_file_to_sym_file (core
)
882 /* The target stratum for a running executable need not support
890 struct target_ops child_ops
= {
891 "child", /* to_shortname */
892 "Unix child process", /* to_longname */
893 "Unix child process (started by the \"run\" command).", /* to_doc */
894 child_open
, /* to_open */
896 child_attach
, /* to_attach */
897 child_post_attach
, /* to_post_attach */
898 child_require_attach
, /* to_require_attach */
899 child_detach
, /* to_detach */
900 child_require_detach
, /* to_require_detach */
901 child_resume
, /* to_resume */
902 child_wait
, /* to_wait */
903 child_post_wait
, /* to_post_wait */
904 fetch_inferior_registers
, /* to_fetch_registers */
905 store_inferior_registers
, /* to_store_registers */
906 child_prepare_to_store
, /* to_prepare_to_store */
907 child_xfer_memory
, /* to_xfer_memory */
908 child_files_info
, /* to_files_info */
909 memory_insert_breakpoint
, /* to_insert_breakpoint */
910 memory_remove_breakpoint
, /* to_remove_breakpoint */
911 terminal_init_inferior
, /* to_terminal_init */
912 terminal_inferior
, /* to_terminal_inferior */
913 terminal_ours_for_output
, /* to_terminal_ours_for_output */
914 terminal_ours
, /* to_terminal_ours */
915 child_terminal_info
, /* to_terminal_info */
916 kill_inferior
, /* to_kill */
918 0, /* to_lookup_symbol */
919 child_create_inferior
, /* to_create_inferior */
920 child_post_startup_inferior
, /* to_post_startup_inferior */
921 child_acknowledge_created_inferior
, /* to_acknowledge_created_inferior */
922 child_clone_and_follow_inferior
, /* to_clone_and_follow_inferior */
923 child_post_follow_inferior_by_clone
, /* to_post_follow_inferior_by_clone */
924 child_insert_fork_catchpoint
, /* to_insert_fork_catchpoint */
925 child_remove_fork_catchpoint
, /* to_remove_fork_catchpoint */
926 child_insert_vfork_catchpoint
, /* to_insert_vfork_catchpoint */
927 child_remove_vfork_catchpoint
, /* to_remove_vfork_catchpoint */
928 child_has_forked
, /* to_has_forked */
929 child_has_vforked
, /* to_has_vforked */
930 child_can_follow_vfork_prior_to_exec
, /* to_can_follow_vfork_prior_to_exec */
931 child_post_follow_vfork
, /* to_post_follow_vfork */
932 child_insert_exec_catchpoint
, /* to_insert_exec_catchpoint */
933 child_remove_exec_catchpoint
, /* to_remove_exec_catchpoint */
934 child_has_execd
, /* to_has_execd */
935 child_reported_exec_events_per_exec_call
, /* to_reported_exec_events_per_exec_call */
936 child_has_syscall_event
, /* to_has_syscall_event */
937 child_has_exited
, /* to_has_exited */
938 child_mourn_inferior
, /* to_mourn_inferior */
939 child_can_run
, /* to_can_run */
940 0, /* to_notice_signals */
941 child_thread_alive
, /* to_thread_alive */
942 child_stop
, /* to_stop */
943 child_enable_exception_callback
, /* to_enable_exception_callback */
944 child_get_current_exception_event
, /* to_get_current_exception_event */
945 child_pid_to_exec_file
, /* to_pid_to_exec_file */
946 child_core_file_to_sym_file
, /* to_core_file_to_sym_file */
947 process_stratum
, /* to_stratum */
949 1, /* to_has_all_memory */
950 1, /* to_has_memory */
951 1, /* to_has_stack */
952 1, /* to_has_registers */
953 1, /* to_has_execution */
955 0, /* to_sections_end */
956 OPS_MAGIC
/* to_magic */
960 _initialize_inftarg ()
962 #ifdef HAVE_OPTIONAL_PROC_FS
966 /* If we have an optional /proc filesystem (e.g. under OSF/1),
967 don't add ptrace support if we can access the running GDB via /proc. */
968 #ifndef PROC_NAME_FMT
969 #define PROC_NAME_FMT "/proc/%05d"
971 sprintf (procname
, PROC_NAME_FMT
, getpid ());
972 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
979 add_target (&child_ops
);