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>
37 extern struct symtab_and_line
*
38 child_enable_exception_callback
PARAMS ((enum exception_event_kind
, int));
40 extern struct exception_event_record
*
41 child_get_current_exception_event
PARAMS ((void));
43 extern void _initialize_inftarg
PARAMS ((void));
46 child_prepare_to_store
PARAMS ((void));
49 static int child_wait
PARAMS ((int, struct target_waitstatus
*));
50 #endif /* CHILD_WAIT */
52 #if !defined(CHILD_POST_WAIT)
54 child_post_wait
PARAMS ((int, int));
57 static void child_open
PARAMS ((char *, int));
60 child_files_info
PARAMS ((struct target_ops
*));
63 child_detach
PARAMS ((char *, int));
66 child_detach_from_process
PARAMS ((int, char *, int, int));
69 child_attach
PARAMS ((char *, int));
72 child_attach_to_process
PARAMS ((char *, int, int));
74 #if !defined(CHILD_POST_ATTACH)
75 extern void child_post_attach
PARAMS ((int));
79 child_require_attach
PARAMS ((char *, int));
82 child_require_detach
PARAMS ((int, char *, int));
85 ptrace_me
PARAMS ((void));
88 ptrace_him
PARAMS ((int));
91 child_create_inferior
PARAMS ((char *, char *, char **));
94 child_mourn_inferior
PARAMS ((void));
97 child_can_run
PARAMS ((void));
100 child_stop
PARAMS ((void));
102 #ifndef CHILD_THREAD_ALIVE
103 int child_thread_alive
PARAMS ((int));
106 static void init_child_ops
PARAMS ((void));
108 extern char **environ
;
110 struct target_ops child_ops
;
112 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
113 be a runnable target. Used by targets
114 that can sit atop inftarg, such as HPUX
120 /* Enable HACK for ttrace work. In
121 * infttrace.c/require_notification_of_events,
122 * this is set to 0 so that the loop in child_wait
125 int not_same_real_pid
= 1;
129 /* Wait for child to do something. Return pid of child, or -1 in case
130 of error; store status through argument pointer OURSTATUS. */
133 child_wait (pid
, ourstatus
)
135 struct target_waitstatus
*ourstatus
;
139 char *execd_pathname
= NULL
;
143 enum target_waitkind kind
;
147 set_sigint_trap (); /* Causes SIGINT to be passed on to the
151 pid
= ptrace_wait (inferior_pid
, &status
);
157 clear_sigint_trap ();
161 if (save_errno
== EINTR
)
164 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
165 safe_strerror (save_errno
));
167 /* Claim it exited with unknown signal. */
168 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
169 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
175 if (target_has_exited (pid
, status
, &exit_status
))
177 /* ??rehrauer: For now, ignore this. */
181 if (!target_thread_alive (pid
))
183 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
187 if (target_has_forked (pid
, &related_pid
)
188 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
190 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
191 ourstatus
->value
.related_pid
= related_pid
;
195 if (target_has_vforked (pid
, &related_pid
)
196 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
198 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
199 ourstatus
->value
.related_pid
= related_pid
;
203 if (target_has_execd (pid
, &execd_pathname
))
205 /* Are we ignoring initial exec events? (This is likely because
206 we're in the process of starting up the inferior, and another
207 (older) mechanism handles those.) If so, we'll report this
208 as a regular stop, not an exec.
210 if (inferior_ignoring_startup_exec_events
)
212 inferior_ignoring_startup_exec_events
--;
216 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
217 ourstatus
->value
.execd_pathname
= execd_pathname
;
222 /* All we must do with these is communicate their occurrence
223 to wait_for_inferior...
225 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
227 ourstatus
->kind
= kind
;
228 ourstatus
->value
.syscall_id
= syscall_id
;
232 /*## } while (pid != inferior_pid); ## *//* Some other child died or stopped */
233 /* hack for thread testing */
235 while ((pid
!= inferior_pid
) && not_same_real_pid
);
238 store_waitstatus (ourstatus
, status
);
241 #endif /* CHILD_WAIT */
243 #if !defined(CHILD_POST_WAIT)
245 child_post_wait (pid
, wait_status
)
249 /* This version of Unix doesn't require a meaningful "post wait"
256 #ifndef CHILD_THREAD_ALIVE
258 /* Check to see if the given thread is alive.
260 FIXME: Is kill() ever the right way to do this? I doubt it, but
261 for now we're going to try and be compatable with the old thread
264 child_thread_alive (pid
)
267 return (kill (pid
, 0) != -1);
273 child_attach_to_process (args
, from_tty
, after_fork
)
279 error_no_arg ("process-id to attach");
281 #ifndef ATTACH_DETACH
282 error ("Can't attach to a process on this machine.");
290 pid
= strtol (args
, &dummy
, 0);
291 /* Some targets don't set errno on errors, grrr! */
292 if ((pid
== 0) && (args
== dummy
))
293 error ("Illegal process-id: %s\n", args
);
295 if (pid
== getpid ()) /* Trying to masturbate? */
296 error ("I refuse to debug myself!");
300 exec_file
= (char *) get_exec_file (0);
303 printf_unfiltered ("Attaching after fork to %s\n",
304 target_pid_to_str (pid
));
306 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
307 target_pid_to_str (pid
));
309 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
311 gdb_flush (gdb_stdout
);
317 REQUIRE_ATTACH (pid
);
320 push_target (&child_ops
);
322 #endif /* ATTACH_DETACH */
326 /* Attach to process PID, then initialize for debugging it. */
329 child_attach (args
, from_tty
)
333 child_attach_to_process (args
, from_tty
, 0);
336 #if !defined(CHILD_POST_ATTACH)
338 child_post_attach (pid
)
341 /* This version of Unix doesn't require a meaningful "post attach"
342 operation by a debugger. */
347 child_require_attach (args
, from_tty
)
351 child_attach_to_process (args
, from_tty
, 1);
355 child_detach_from_process (pid
, args
, from_tty
, after_fork
)
367 char *exec_file
= get_exec_file (0);
371 printf_unfiltered ("Detaching after fork from %s\n",
372 target_pid_to_str (pid
));
374 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
375 target_pid_to_str (pid
));
376 gdb_flush (gdb_stdout
);
379 siggnal
= atoi (args
);
384 REQUIRE_DETACH (pid
, siggnal
);
387 error ("This version of Unix does not support detaching a process.");
391 /* Take a program previously attached to and detaches it.
392 The program resumes execution and will no longer stop
393 on signals, etc. We'd better not have left any breakpoints
394 in the program or it'll die when it hits one. For this
395 to work, it may be necessary for the process to have been
396 previously attached. It *might* work if the program was
397 started via the normal ptrace (PTRACE_TRACEME). */
400 child_detach (args
, from_tty
)
404 child_detach_from_process (inferior_pid
, args
, from_tty
, 0);
406 unpush_target (&child_ops
);
410 child_require_detach (pid
, args
, from_tty
)
415 child_detach_from_process (pid
, args
, from_tty
, 1);
419 /* Get ready to modify the registers array. On machines which store
420 individual registers, this doesn't need to do anything. On machines
421 which store all the registers in one fell swoop, this makes sure
422 that registers contains all the registers from the program being
426 child_prepare_to_store ()
428 #ifdef CHILD_PREPARE_TO_STORE
429 CHILD_PREPARE_TO_STORE ();
433 /* Print status information about what we're accessing. */
436 child_files_info (ignore
)
437 struct target_ops
*ignore
;
439 printf_unfiltered ("\tUsing the running image of %s %s.\n",
440 attach_flag
? "attached" : "child", target_pid_to_str (inferior_pid
));
445 child_open (arg
, from_tty
)
449 error ("Use the \"run\" command to start a Unix child process.");
452 /* Stub function which causes the inferior that runs it, to be ptrace-able
453 by its parent process. */
458 /* "Trace me, Dr. Memory!" */
459 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
462 /* Stub function which causes the GDB that runs it, to start ptrace-ing
463 the child process. */
469 push_target (&child_ops
);
471 /* On some targets, there must be some explicit synchronization
472 between the parent and child processes after the debugger
473 forks, and before the child execs the debuggee program. This
474 call basically gives permission for the child to exec.
477 target_acknowledge_created_inferior (pid
);
479 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
480 * and will be 1 or 2 depending on whether we're starting
481 * without or with a shell.
483 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
485 /* On some targets, there must be some explicit actions taken after
486 the inferior has been started up.
488 target_post_startup_inferior (pid
);
491 /* Start an inferior Unix child process and sets inferior_pid to its pid.
492 EXEC_FILE is the file to run.
493 ALLARGS is a string containing the arguments to the program.
494 ENV is the environment vector to pass. Errors reported with error(). */
497 child_create_inferior (exec_file
, allargs
, env
)
503 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
505 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
507 /* We are at the first instruction we care about. */
508 /* Pedal to the metal... */
509 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
512 #if !defined(CHILD_POST_STARTUP_INFERIOR)
514 child_post_startup_inferior (pid
)
517 /* This version of Unix doesn't require a meaningful "post startup inferior"
518 operation by a debugger.
523 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
525 child_acknowledge_created_inferior (pid
)
528 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
529 operation by a debugger.
536 child_clone_and_follow_inferior (child_pid
, followed_child
)
540 clone_and_follow_inferior (child_pid
, followed_child
);
542 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
543 the decision gets made elsewhere how to continue it.
548 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
550 child_post_follow_inferior_by_clone ()
552 /* This version of Unix doesn't require a meaningful "post follow inferior"
553 operation by a clone debugger.
558 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
560 child_insert_fork_catchpoint (pid
)
563 /* This version of Unix doesn't support notification of fork events. */
568 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
570 child_remove_fork_catchpoint (pid
)
573 /* This version of Unix doesn't support notification of fork events. */
578 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
580 child_insert_vfork_catchpoint (pid
)
583 /* This version of Unix doesn't support notification of vfork events. */
588 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
590 child_remove_vfork_catchpoint (pid
)
593 /* This version of Unix doesn't support notification of vfork events. */
598 #if !defined(CHILD_HAS_FORKED)
600 child_has_forked (pid
, child_pid
)
604 /* This version of Unix doesn't support notification of fork events. */
610 #if !defined(CHILD_HAS_VFORKED)
612 child_has_vforked (pid
, child_pid
)
616 /* This version of Unix doesn't support notification of vfork events.
623 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
625 child_can_follow_vfork_prior_to_exec ()
627 /* This version of Unix doesn't support notification of vfork events.
628 However, if it did, it probably wouldn't allow vforks to be followed
629 before the following exec.
636 #if !defined(CHILD_POST_FOLLOW_VFORK)
638 child_post_follow_vfork (parent_pid
, followed_parent
, child_pid
, followed_child
)
644 /* This version of Unix doesn't require a meaningful "post follow vfork"
645 operation by a clone debugger.
650 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
652 child_insert_exec_catchpoint (pid
)
655 /* This version of Unix doesn't support notification of exec events. */
660 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
662 child_remove_exec_catchpoint (pid
)
665 /* This version of Unix doesn't support notification of exec events. */
670 #if !defined(CHILD_HAS_EXECD)
672 child_has_execd (pid
, execd_pathname
)
674 char **execd_pathname
;
676 /* This version of Unix doesn't support notification of exec events.
683 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
685 child_reported_exec_events_per_exec_call ()
687 /* This version of Unix doesn't support notification of exec events.
694 #if !defined(CHILD_HAS_SYSCALL_EVENT)
696 child_has_syscall_event (pid
, kind
, syscall_id
)
698 enum target_waitkind
*kind
;
701 /* This version of Unix doesn't support notification of syscall events.
708 #if !defined(CHILD_HAS_EXITED)
710 child_has_exited (pid
, wait_status
, exit_status
)
715 if (WIFEXITED (wait_status
))
717 *exit_status
= WEXITSTATUS (wait_status
);
721 if (WIFSIGNALED (wait_status
))
723 *exit_status
= 0; /* ?? Don't know what else to say here. */
727 /* ?? Do we really need to consult the event state, too? Assume the
728 wait_state alone suffices.
736 child_mourn_inferior ()
738 unpush_target (&child_ops
);
739 generic_mourn_inferior ();
745 /* This variable is controlled by modules that sit atop inftarg that may layer
746 their own process structure atop that provided here. hpux-thread.c does
747 this because of the Hpux user-mode level thread model. */
749 return !child_suppress_run
;
752 /* Send a SIGINT to the process group. This acts just like the user typed a
753 ^C on the controlling terminal.
755 XXX - This may not be correct for all systems. Some may want to use
756 killpg() instead of kill (-pgrp). */
761 extern pid_t inferior_process_group
;
763 kill (-inferior_process_group
, SIGINT
);
766 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
767 struct symtab_and_line
*
768 child_enable_exception_callback (kind
, enable
)
769 enum exception_event_kind kind
;
772 return (struct symtab_and_line
*) NULL
;
776 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
777 struct exception_event_record
*
778 child_get_current_exception_event ()
780 return (struct exception_event_record
*) NULL
;
785 #if !defined(CHILD_PID_TO_EXEC_FILE)
787 child_pid_to_exec_file (pid
)
790 /* This version of Unix doesn't support translation of a process ID
791 to the filename of the executable file.
798 child_core_file_to_sym_file (core
)
801 /* The target stratum for a running executable need not support
808 #if !defined(CHILD_PID_TO_STR)
810 child_pid_to_str (pid
)
813 return normal_pid_to_str (pid
);
820 child_ops
.to_shortname
= "child";
821 child_ops
.to_longname
= "Unix child process";
822 child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
823 child_ops
.to_open
= child_open
;
824 child_ops
.to_attach
= child_attach
;
825 child_ops
.to_post_attach
= child_post_attach
;
826 child_ops
.to_require_attach
= child_require_attach
;
827 child_ops
.to_detach
= child_detach
;
828 child_ops
.to_require_detach
= child_require_detach
;
829 child_ops
.to_resume
= child_resume
;
830 child_ops
.to_wait
= child_wait
;
831 child_ops
.to_post_wait
= child_post_wait
;
832 child_ops
.to_fetch_registers
= fetch_inferior_registers
;
833 child_ops
.to_store_registers
= store_inferior_registers
;
834 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
835 child_ops
.to_xfer_memory
= child_xfer_memory
;
836 child_ops
.to_files_info
= child_files_info
;
837 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
838 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
839 child_ops
.to_terminal_init
= terminal_init_inferior
;
840 child_ops
.to_terminal_inferior
= terminal_inferior
;
841 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
842 child_ops
.to_terminal_ours
= terminal_ours
;
843 child_ops
.to_terminal_info
= child_terminal_info
;
844 child_ops
.to_kill
= kill_inferior
;
845 child_ops
.to_create_inferior
= child_create_inferior
;
846 child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
847 child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
848 child_ops
.to_clone_and_follow_inferior
= child_clone_and_follow_inferior
;
849 child_ops
.to_post_follow_inferior_by_clone
= child_post_follow_inferior_by_clone
;
850 child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
851 child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
852 child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
853 child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
854 child_ops
.to_has_forked
= child_has_forked
;
855 child_ops
.to_has_vforked
= child_has_vforked
;
856 child_ops
.to_can_follow_vfork_prior_to_exec
= child_can_follow_vfork_prior_to_exec
;
857 child_ops
.to_post_follow_vfork
= child_post_follow_vfork
;
858 child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
859 child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
860 child_ops
.to_has_execd
= child_has_execd
;
861 child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
862 child_ops
.to_has_syscall_event
= child_has_syscall_event
;
863 child_ops
.to_has_exited
= child_has_exited
;
864 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
865 child_ops
.to_can_run
= child_can_run
;
866 child_ops
.to_thread_alive
= child_thread_alive
;
867 child_ops
.to_pid_to_str
= child_pid_to_str
;
868 child_ops
.to_stop
= child_stop
;
869 child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
870 child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
871 child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
872 child_ops
.to_core_file_to_sym_file
= child_core_file_to_sym_file
;
873 child_ops
.to_stratum
= process_stratum
;
874 child_ops
.to_has_all_memory
= 1;
875 child_ops
.to_has_memory
= 1;
876 child_ops
.to_has_stack
= 1;
877 child_ops
.to_has_registers
= 1;
878 child_ops
.to_has_execution
= 1;
879 child_ops
.to_magic
= OPS_MAGIC
;
883 _initialize_inftarg ()
885 #ifdef HAVE_OPTIONAL_PROC_FS
889 /* If we have an optional /proc filesystem (e.g. under OSF/1),
890 don't add ptrace support if we can access the running GDB via /proc. */
891 #ifndef PROC_NAME_FMT
892 #define PROC_NAME_FMT "/proc/%05d"
894 sprintf (procname
, PROC_NAME_FMT
, getpid ());
895 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
903 add_target (&child_ops
);