1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990-1996, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 ## Contains temporary hacks..
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "frame.h" /* required by inferior.h */
32 #include <sys/types.h>
38 #ifdef HAVE_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));
55 extern void _initialize_inftarg
PARAMS ((void));
58 child_prepare_to_store
PARAMS ((void));
61 static int child_wait
PARAMS ((int, struct target_waitstatus
*));
62 #endif /* CHILD_WAIT */
64 #if !defined(CHILD_POST_WAIT)
66 child_post_wait
PARAMS ((int, int));
69 static void child_open
PARAMS ((char *, int));
72 child_files_info
PARAMS ((struct target_ops
*));
75 child_detach
PARAMS ((char *, int));
78 child_detach_from_process
PARAMS ((int, char *, int, int));
81 child_attach
PARAMS ((char *, int));
84 child_attach_to_process
PARAMS ((char *, int, int));
86 #if !defined(CHILD_POST_ATTACH)
87 extern void child_post_attach
PARAMS ((int));
91 child_require_attach
PARAMS ((char *, int));
94 child_require_detach
PARAMS ((int, char *, int));
97 ptrace_me
PARAMS ((void));
100 ptrace_him
PARAMS ((int));
103 child_create_inferior
PARAMS ((char *, char *, char **));
106 child_mourn_inferior
PARAMS ((void));
109 child_can_run
PARAMS ((void));
112 child_stop
PARAMS ((void));
114 #ifndef CHILD_THREAD_ALIVE
115 int child_thread_alive
PARAMS ((int));
118 static void init_child_ops
PARAMS ((void));
120 extern char **environ
;
122 struct target_ops child_ops
;
124 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
125 be a runnable target. Used by targets
126 that can sit atop inftarg, such as HPUX
132 /* Enable HACK for ttrace work. In
133 * infttrace.c/require_notification_of_events,
134 * this is set to 0 so that the loop in child_wait
137 int not_same_real_pid
= 1;
141 /* Wait for child to do something. Return pid of child, or -1 in case
142 of error; store status through argument pointer OURSTATUS. */
145 child_wait (pid
, ourstatus
)
147 struct target_waitstatus
*ourstatus
;
151 char *execd_pathname
= NULL
;
155 enum target_waitkind kind
;
159 set_sigint_trap (); /* Causes SIGINT to be passed on to the
163 pid
= ptrace_wait (inferior_pid
, &status
);
169 clear_sigint_trap ();
173 if (save_errno
== EINTR
)
176 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
177 safe_strerror (save_errno
));
179 /* Claim it exited with unknown signal. */
180 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
181 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
187 if (target_has_exited (pid
, status
, &exit_status
))
189 /* ??rehrauer: For now, ignore this. */
193 if (!target_thread_alive (pid
))
195 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
199 if (target_has_forked (pid
, &related_pid
)
200 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
202 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
203 ourstatus
->value
.related_pid
= related_pid
;
207 if (target_has_vforked (pid
, &related_pid
)
208 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
210 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
211 ourstatus
->value
.related_pid
= related_pid
;
215 if (target_has_execd (pid
, &execd_pathname
))
217 /* Are we ignoring initial exec events? (This is likely because
218 we're in the process of starting up the inferior, and another
219 (older) mechanism handles those.) If so, we'll report this
220 as a regular stop, not an exec.
222 if (inferior_ignoring_startup_exec_events
)
224 inferior_ignoring_startup_exec_events
--;
228 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
229 ourstatus
->value
.execd_pathname
= execd_pathname
;
234 /* All we must do with these is communicate their occurrence
235 to wait_for_inferior...
237 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
239 ourstatus
->kind
= kind
;
240 ourstatus
->value
.syscall_id
= syscall_id
;
244 /*## } while (pid != inferior_pid); ## *//* Some other child died or stopped */
245 /* hack for thread testing */
247 while ((pid
!= inferior_pid
) && not_same_real_pid
);
250 store_waitstatus (ourstatus
, status
);
253 #endif /* CHILD_WAIT */
255 #if !defined(CHILD_POST_WAIT)
257 child_post_wait (pid
, wait_status
)
261 /* This version of Unix doesn't require a meaningful "post wait"
268 #ifndef CHILD_THREAD_ALIVE
270 /* Check to see if the given thread is alive.
272 FIXME: Is kill() ever the right way to do this? I doubt it, but
273 for now we're going to try and be compatable with the old thread
276 child_thread_alive (pid
)
279 return (kill (pid
, 0) != -1);
285 child_attach_to_process (args
, from_tty
, after_fork
)
291 error_no_arg ("process-id to attach");
293 #ifndef ATTACH_DETACH
294 error ("Can't attach to a process on this machine.");
302 pid
= strtol (args
, &dummy
, 0);
303 /* Some targets don't set errno on errors, grrr! */
304 if ((pid
== 0) && (args
== dummy
))
305 error ("Illegal process-id: %s\n", args
);
307 if (pid
== getpid ()) /* Trying to masturbate? */
308 error ("I refuse to debug myself!");
312 exec_file
= (char *) get_exec_file (0);
315 printf_unfiltered ("Attaching after fork to %s\n",
316 target_pid_to_str (pid
));
318 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
319 target_pid_to_str (pid
));
321 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
323 gdb_flush (gdb_stdout
);
329 REQUIRE_ATTACH (pid
);
332 push_target (&child_ops
);
334 #endif /* ATTACH_DETACH */
338 /* Attach to process PID, then initialize for debugging it. */
341 child_attach (args
, from_tty
)
345 child_attach_to_process (args
, from_tty
, 0);
348 #if !defined(CHILD_POST_ATTACH)
350 child_post_attach (pid
)
353 /* This version of Unix doesn't require a meaningful "post attach"
354 operation by a debugger. */
359 child_require_attach (args
, from_tty
)
363 child_attach_to_process (args
, from_tty
, 1);
367 child_detach_from_process (pid
, args
, from_tty
, after_fork
)
379 char *exec_file
= get_exec_file (0);
383 printf_unfiltered ("Detaching after fork from %s\n",
384 target_pid_to_str (pid
));
386 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
387 target_pid_to_str (pid
));
388 gdb_flush (gdb_stdout
);
391 siggnal
= atoi (args
);
396 REQUIRE_DETACH (pid
, siggnal
);
399 error ("This version of Unix does not support detaching a process.");
403 /* Take a program previously attached to and detaches it.
404 The program resumes execution and will no longer stop
405 on signals, etc. We'd better not have left any breakpoints
406 in the program or it'll die when it hits one. For this
407 to work, it may be necessary for the process to have been
408 previously attached. It *might* work if the program was
409 started via the normal ptrace (PTRACE_TRACEME). */
412 child_detach (args
, from_tty
)
416 child_detach_from_process (inferior_pid
, args
, from_tty
, 0);
418 unpush_target (&child_ops
);
422 child_require_detach (pid
, args
, from_tty
)
427 child_detach_from_process (pid
, args
, from_tty
, 1);
431 /* Get ready to modify the registers array. On machines which store
432 individual registers, this doesn't need to do anything. On machines
433 which store all the registers in one fell swoop, this makes sure
434 that registers contains all the registers from the program being
438 child_prepare_to_store ()
440 #ifdef CHILD_PREPARE_TO_STORE
441 CHILD_PREPARE_TO_STORE ();
445 /* Print status information about what we're accessing. */
448 child_files_info (ignore
)
449 struct target_ops
*ignore
;
451 printf_unfiltered ("\tUsing the running image of %s %s.\n",
452 attach_flag
? "attached" : "child", target_pid_to_str (inferior_pid
));
457 child_open (arg
, from_tty
)
461 error ("Use the \"run\" command to start a Unix child process.");
464 /* Stub function which causes the inferior that runs it, to be ptrace-able
465 by its parent process. */
470 /* "Trace me, Dr. Memory!" */
471 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
474 /* Stub function which causes the GDB that runs it, to start ptrace-ing
475 the child process. */
481 push_target (&child_ops
);
483 /* On some targets, there must be some explicit synchronization
484 between the parent and child processes after the debugger
485 forks, and before the child execs the debuggee program. This
486 call basically gives permission for the child to exec.
489 target_acknowledge_created_inferior (pid
);
491 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
492 * and will be 1 or 2 depending on whether we're starting
493 * without or with a shell.
495 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
497 /* On some targets, there must be some explicit actions taken after
498 the inferior has been started up.
500 target_post_startup_inferior (pid
);
503 /* Start an inferior Unix child process and sets inferior_pid to its pid.
504 EXEC_FILE is the file to run.
505 ALLARGS is a string containing the arguments to the program.
506 ENV is the environment vector to pass. Errors reported with error(). */
509 child_create_inferior (exec_file
, allargs
, env
)
515 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
517 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
519 /* We are at the first instruction we care about. */
520 /* Pedal to the metal... */
521 proceed ((CORE_ADDR
) - 1, TARGET_SIGNAL_0
, 0);
524 #if !defined(CHILD_POST_STARTUP_INFERIOR)
526 child_post_startup_inferior (pid
)
529 /* This version of Unix doesn't require a meaningful "post startup inferior"
530 operation by a debugger.
535 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
537 child_acknowledge_created_inferior (pid
)
540 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
541 operation by a debugger.
548 child_clone_and_follow_inferior (child_pid
, followed_child
)
552 clone_and_follow_inferior (child_pid
, followed_child
);
554 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
555 the decision gets made elsewhere how to continue it.
560 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
562 child_post_follow_inferior_by_clone ()
564 /* This version of Unix doesn't require a meaningful "post follow inferior"
565 operation by a clone debugger.
570 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
572 child_insert_fork_catchpoint (pid
)
575 /* This version of Unix doesn't support notification of fork events. */
580 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
582 child_remove_fork_catchpoint (pid
)
585 /* This version of Unix doesn't support notification of fork events. */
590 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
592 child_insert_vfork_catchpoint (pid
)
595 /* This version of Unix doesn't support notification of vfork events. */
600 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
602 child_remove_vfork_catchpoint (pid
)
605 /* This version of Unix doesn't support notification of vfork events. */
610 #if !defined(CHILD_HAS_FORKED)
612 child_has_forked (pid
, child_pid
)
616 /* This version of Unix doesn't support notification of fork events. */
622 #if !defined(CHILD_HAS_VFORKED)
624 child_has_vforked (pid
, child_pid
)
628 /* This version of Unix doesn't support notification of vfork events.
635 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
637 child_can_follow_vfork_prior_to_exec ()
639 /* This version of Unix doesn't support notification of vfork events.
640 However, if it did, it probably wouldn't allow vforks to be followed
641 before the following exec.
648 #if !defined(CHILD_POST_FOLLOW_VFORK)
650 child_post_follow_vfork (parent_pid
, followed_parent
, child_pid
, followed_child
)
656 /* This version of Unix doesn't require a meaningful "post follow vfork"
657 operation by a clone debugger.
662 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
664 child_insert_exec_catchpoint (pid
)
667 /* This version of Unix doesn't support notification of exec events. */
672 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
674 child_remove_exec_catchpoint (pid
)
677 /* This version of Unix doesn't support notification of exec events. */
682 #if !defined(CHILD_HAS_EXECD)
684 child_has_execd (pid
, execd_pathname
)
686 char **execd_pathname
;
688 /* This version of Unix doesn't support notification of exec events.
695 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
697 child_reported_exec_events_per_exec_call ()
699 /* This version of Unix doesn't support notification of exec events.
706 #if !defined(CHILD_HAS_SYSCALL_EVENT)
708 child_has_syscall_event (pid
, kind
, syscall_id
)
710 enum target_waitkind
*kind
;
713 /* This version of Unix doesn't support notification of syscall events.
720 #if !defined(CHILD_HAS_EXITED)
722 child_has_exited (pid
, wait_status
, exit_status
)
727 if (WIFEXITED (wait_status
))
729 *exit_status
= WEXITSTATUS (wait_status
);
733 if (WIFSIGNALED (wait_status
))
735 *exit_status
= 0; /* ?? Don't know what else to say here. */
739 /* ?? Do we really need to consult the event state, too? Assume the
740 wait_state alone suffices.
748 child_mourn_inferior ()
750 unpush_target (&child_ops
);
751 generic_mourn_inferior ();
757 /* This variable is controlled by modules that sit atop inftarg that may layer
758 their own process structure atop that provided here. hpux-thread.c does
759 this because of the Hpux user-mode level thread model. */
761 return !child_suppress_run
;
764 /* Send a SIGINT to the process group. This acts just like the user typed a
765 ^C on the controlling terminal.
767 XXX - This may not be correct for all systems. Some may want to use
768 killpg() instead of kill (-pgrp). */
773 extern pid_t inferior_process_group
;
775 kill (-inferior_process_group
, SIGINT
);
778 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
779 struct symtab_and_line
*
780 child_enable_exception_callback (kind
, enable
)
781 enum exception_event_kind kind
;
784 return (struct symtab_and_line
*) NULL
;
788 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
789 struct exception_event_record
*
790 child_get_current_exception_event ()
792 return (struct exception_event_record
*) NULL
;
797 #if !defined(CHILD_PID_TO_EXEC_FILE)
799 child_pid_to_exec_file (pid
)
802 /* This version of Unix doesn't support translation of a process ID
803 to the filename of the executable file.
810 child_core_file_to_sym_file (core
)
813 /* The target stratum for a running executable need not support
824 child_ops
.to_shortname
= "child";
825 child_ops
.to_longname
= "Unix child process";
826 child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
827 child_ops
.to_open
= child_open
;
828 child_ops
.to_attach
= child_attach
;
829 child_ops
.to_post_attach
= child_post_attach
;
830 child_ops
.to_require_attach
= child_require_attach
;
831 child_ops
.to_detach
= child_detach
;
832 child_ops
.to_require_detach
= child_require_detach
;
833 child_ops
.to_resume
= child_resume
;
834 child_ops
.to_wait
= child_wait
;
835 child_ops
.to_post_wait
= child_post_wait
;
836 child_ops
.to_fetch_registers
= fetch_inferior_registers
;
837 child_ops
.to_store_registers
= store_inferior_registers
;
838 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
839 child_ops
.to_xfer_memory
= child_xfer_memory
;
840 child_ops
.to_files_info
= child_files_info
;
841 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
842 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
843 child_ops
.to_terminal_init
= terminal_init_inferior
;
844 child_ops
.to_terminal_inferior
= terminal_inferior
;
845 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
846 child_ops
.to_terminal_ours
= terminal_ours
;
847 child_ops
.to_terminal_info
= child_terminal_info
;
848 child_ops
.to_kill
= kill_inferior
;
849 child_ops
.to_create_inferior
= child_create_inferior
;
850 child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
851 child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
852 child_ops
.to_clone_and_follow_inferior
= child_clone_and_follow_inferior
;
853 child_ops
.to_post_follow_inferior_by_clone
= child_post_follow_inferior_by_clone
;
854 child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
855 child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
856 child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
857 child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
858 child_ops
.to_has_forked
= child_has_forked
;
859 child_ops
.to_has_vforked
= child_has_vforked
;
860 child_ops
.to_can_follow_vfork_prior_to_exec
= child_can_follow_vfork_prior_to_exec
;
861 child_ops
.to_post_follow_vfork
= child_post_follow_vfork
;
862 child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
863 child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
864 child_ops
.to_has_execd
= child_has_execd
;
865 child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
866 child_ops
.to_has_syscall_event
= child_has_syscall_event
;
867 child_ops
.to_has_exited
= child_has_exited
;
868 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
869 child_ops
.to_can_run
= child_can_run
;
870 child_ops
.to_thread_alive
= child_thread_alive
;
871 child_ops
.to_stop
= child_stop
;
872 child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
873 child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
874 child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
875 child_ops
.to_core_file_to_sym_file
= child_core_file_to_sym_file
;
876 child_ops
.to_stratum
= process_stratum
;
877 child_ops
.to_has_all_memory
= 1;
878 child_ops
.to_has_memory
= 1;
879 child_ops
.to_has_stack
= 1;
880 child_ops
.to_has_registers
= 1;
881 child_ops
.to_has_execution
= 1;
882 child_ops
.to_magic
= OPS_MAGIC
;
886 _initialize_inftarg ()
888 #ifdef HAVE_OPTIONAL_PROC_FS
892 /* If we have an optional /proc filesystem (e.g. under OSF/1),
893 don't add ptrace support if we can access the running GDB via /proc. */
894 #ifndef PROC_NAME_FMT
895 #define PROC_NAME_FMT "/proc/%05d"
897 sprintf (procname
, PROC_NAME_FMT
, getpid ());
898 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
906 add_target (&child_ops
);