1 /* Target-vector operations for controlling Unix child processes, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000
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,
23 Boston, MA 02111-1307, USA. */
26 #include "frame.h" /* required by inferior.h */
33 #include <sys/types.h>
38 extern struct symtab_and_line
*child_enable_exception_callback (enum
42 extern struct exception_event_record
43 *child_get_current_exception_event (void);
45 extern void _initialize_inftarg (void);
47 static void child_prepare_to_store (void);
50 static ptid_t
child_wait (ptid_t
, struct target_waitstatus
*);
51 #endif /* CHILD_WAIT */
53 #if !defined(CHILD_POST_WAIT)
54 void child_post_wait (ptid_t
, int);
57 static void child_open (char *, int);
59 static void child_files_info (struct target_ops
*);
61 static void child_detach (char *, int);
63 static void child_detach_from_process (int, char *, int, int);
65 static void child_attach (char *, int);
67 static void child_attach_to_process (char *, int, int);
69 #if !defined(CHILD_POST_ATTACH)
70 extern void child_post_attach (int);
73 static void child_require_attach (char *, int);
75 static void child_require_detach (int, char *, int);
77 static void ptrace_me (void);
79 static void ptrace_him (int);
81 static void child_create_inferior (char *, char *, char **);
83 static void child_mourn_inferior (void);
85 static int child_can_run (void);
87 static void child_stop (void);
89 #ifndef CHILD_THREAD_ALIVE
90 int child_thread_alive (ptid_t
);
93 static void init_child_ops (void);
95 extern char **environ
;
97 struct target_ops child_ops
;
99 int child_suppress_run
= 0; /* Non-zero if inftarg should pretend not to
100 be a runnable target. Used by targets
101 that can sit atop inftarg, such as HPUX
107 /* Enable HACK for ttrace work. In
108 * infttrace.c/require_notification_of_events,
109 * this is set to 0 so that the loop in child_wait
112 int not_same_real_pid
= 1;
116 /* Wait for child to do something. Return pid of child, or -1 in case
117 of error; store status through argument pointer OURSTATUS. */
120 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
124 char *execd_pathname
= NULL
;
128 enum target_waitkind kind
;
133 set_sigint_trap (); /* Causes SIGINT to be passed on to the
137 pid
= ptrace_wait (inferior_ptid
, &status
);
143 clear_sigint_trap ();
147 if (save_errno
== EINTR
)
150 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
151 safe_strerror (save_errno
));
153 /* Claim it exited with unknown signal. */
154 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
155 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
156 return pid_to_ptid (-1);
161 if (target_has_exited (pid
, status
, &exit_status
))
163 /* ??rehrauer: For now, ignore this. */
167 if (!target_thread_alive (pid_to_ptid (pid
)))
169 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
170 return pid_to_ptid (pid
);
173 if (target_has_forked (pid
, &related_pid
)
174 && ((pid
== PIDGET (inferior_ptid
))
175 || (related_pid
== PIDGET (inferior_ptid
))))
177 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
178 ourstatus
->value
.related_pid
= related_pid
;
179 return pid_to_ptid (pid
);
182 if (target_has_vforked (pid
, &related_pid
)
183 && ((pid
== PIDGET (inferior_ptid
))
184 || (related_pid
== PIDGET (inferior_ptid
))))
186 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
187 ourstatus
->value
.related_pid
= related_pid
;
188 return pid_to_ptid (pid
);
191 if (target_has_execd (pid
, &execd_pathname
))
193 /* Are we ignoring initial exec events? (This is likely because
194 we're in the process of starting up the inferior, and another
195 (older) mechanism handles those.) If so, we'll report this
196 as a regular stop, not an exec.
198 if (inferior_ignoring_startup_exec_events
)
200 inferior_ignoring_startup_exec_events
--;
204 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
205 ourstatus
->value
.execd_pathname
= execd_pathname
;
206 return pid_to_ptid (pid
);
210 /* All we must do with these is communicate their occurrence
211 to wait_for_inferior...
213 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
215 ourstatus
->kind
= kind
;
216 ourstatus
->value
.syscall_id
= syscall_id
;
217 return pid_to_ptid (pid
);
220 /*## } while (pid != PIDGET (inferior_ptid)); ## *//* Some other child died or stopped */
221 /* hack for thread testing */
223 while ((pid
!= PIDGET (inferior_ptid
)) && not_same_real_pid
);
226 store_waitstatus (ourstatus
, status
);
227 return pid_to_ptid (pid
);
229 #endif /* CHILD_WAIT */
231 #if !defined(CHILD_POST_WAIT)
233 child_post_wait (ptid_t ptid
, int wait_status
)
235 /* This version of Unix doesn't require a meaningful "post wait"
242 #ifndef CHILD_THREAD_ALIVE
244 /* Check to see if the given thread is alive.
246 FIXME: Is kill() ever the right way to do this? I doubt it, but
247 for now we're going to try and be compatable with the old thread
250 child_thread_alive (ptid_t ptid
)
252 pid_t pid
= PIDGET (ptid
);
254 return (kill (pid
, 0) != -1);
260 child_attach_to_process (char *args
, int from_tty
, int after_fork
)
263 error_no_arg ("process-id to attach");
265 #ifndef ATTACH_DETACH
266 error ("Can't attach to a process on this machine.");
274 pid
= strtol (args
, &dummy
, 0);
275 /* Some targets don't set errno on errors, grrr! */
276 if ((pid
== 0) && (args
== dummy
))
277 error ("Illegal process-id: %s\n", args
);
279 if (pid
== getpid ()) /* Trying to masturbate? */
280 error ("I refuse to debug myself!");
284 exec_file
= (char *) get_exec_file (0);
287 printf_unfiltered ("Attaching after fork to %s\n",
288 target_pid_to_str (pid_to_ptid (pid
)));
290 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
291 target_pid_to_str (pid_to_ptid (pid
)));
293 printf_unfiltered ("Attaching to %s\n",
294 target_pid_to_str (pid_to_ptid (pid
)));
296 gdb_flush (gdb_stdout
);
302 REQUIRE_ATTACH (pid
);
304 inferior_ptid
= pid_to_ptid (pid
);
305 push_target (&child_ops
);
307 #endif /* ATTACH_DETACH */
311 /* Attach to process PID, then initialize for debugging it. */
314 child_attach (char *args
, int from_tty
)
316 child_attach_to_process (args
, from_tty
, 0);
319 #if !defined(CHILD_POST_ATTACH)
321 child_post_attach (int pid
)
323 /* This version of Unix doesn't require a meaningful "post attach"
324 operation by a debugger. */
329 child_require_attach (char *args
, int from_tty
)
331 child_attach_to_process (args
, from_tty
, 1);
335 child_detach_from_process (int pid
, char *args
, int from_tty
, int after_fork
)
343 char *exec_file
= get_exec_file (0);
347 printf_unfiltered ("Detaching after fork from %s\n",
348 target_pid_to_str (pid_to_ptid (pid
)));
350 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
351 target_pid_to_str (pid_to_ptid (pid
)));
352 gdb_flush (gdb_stdout
);
355 siggnal
= atoi (args
);
360 REQUIRE_DETACH (pid
, siggnal
);
363 error ("This version of Unix does not support detaching a process.");
367 /* Take a program previously attached to and detaches it.
368 The program resumes execution and will no longer stop
369 on signals, etc. We'd better not have left any breakpoints
370 in the program or it'll die when it hits one. For this
371 to work, it may be necessary for the process to have been
372 previously attached. It *might* work if the program was
373 started via the normal ptrace (PTRACE_TRACEME). */
376 child_detach (char *args
, int from_tty
)
378 child_detach_from_process (PIDGET (inferior_ptid
), args
, from_tty
, 0);
379 inferior_ptid
= null_ptid
;
380 unpush_target (&child_ops
);
384 child_require_detach (int pid
, char *args
, int from_tty
)
386 child_detach_from_process (pid
, args
, from_tty
, 1);
390 /* Get ready to modify the registers array. On machines which store
391 individual registers, this doesn't need to do anything. On machines
392 which store all the registers in one fell swoop, this makes sure
393 that registers contains all the registers from the program being
397 child_prepare_to_store (void)
399 #ifdef CHILD_PREPARE_TO_STORE
400 CHILD_PREPARE_TO_STORE ();
404 /* Print status information about what we're accessing. */
407 child_files_info (struct target_ops
*ignore
)
409 printf_unfiltered ("\tUsing the running image of %s %s.\n",
410 attach_flag
? "attached" : "child", target_pid_to_str (inferior_ptid
));
415 child_open (char *arg
, int from_tty
)
417 error ("Use the \"run\" command to start a Unix child process.");
420 /* Stub function which causes the inferior that runs it, to be ptrace-able
421 by its parent process. */
426 /* "Trace me, Dr. Memory!" */
427 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
430 /* Stub function which causes the GDB that runs it, to start ptrace-ing
431 the child process. */
436 push_target (&child_ops
);
438 /* On some targets, there must be some explicit synchronization
439 between the parent and child processes after the debugger
440 forks, and before the child execs the debuggee program. This
441 call basically gives permission for the child to exec.
444 target_acknowledge_created_inferior (pid
);
446 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
447 * and will be 1 or 2 depending on whether we're starting
448 * without or with a shell.
450 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
452 /* On some targets, there must be some explicit actions taken after
453 the inferior has been started up.
455 target_post_startup_inferior (pid_to_ptid (pid
));
458 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
459 EXEC_FILE is the file to run.
460 ALLARGS is a string containing the arguments to the program.
461 ENV is the environment vector to pass. Errors reported with error(). */
464 child_create_inferior (char *exec_file
, char *allargs
, char **env
)
467 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
469 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
471 /* We are at the first instruction we care about. */
472 /* Pedal to the metal... */
473 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
476 #if !defined(CHILD_POST_STARTUP_INFERIOR)
478 child_post_startup_inferior (ptid_t ptid
)
480 /* This version of Unix doesn't require a meaningful "post startup inferior"
481 operation by a debugger.
486 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
488 child_acknowledge_created_inferior (int pid
)
490 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
491 operation by a debugger.
498 child_clone_and_follow_inferior (int child_pid
, int *followed_child
)
500 clone_and_follow_inferior (child_pid
, followed_child
);
502 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
503 the decision gets made elsewhere how to continue it.
508 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
510 child_post_follow_inferior_by_clone (void)
512 /* This version of Unix doesn't require a meaningful "post follow inferior"
513 operation by a clone debugger.
518 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
520 child_insert_fork_catchpoint (int pid
)
522 /* This version of Unix doesn't support notification of fork events. */
527 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
529 child_remove_fork_catchpoint (int pid
)
531 /* This version of Unix doesn't support notification of fork events. */
536 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
538 child_insert_vfork_catchpoint (int pid
)
540 /* This version of Unix doesn't support notification of vfork events. */
545 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
547 child_remove_vfork_catchpoint (int pid
)
549 /* This version of Unix doesn't support notification of vfork events. */
554 #if !defined(CHILD_HAS_FORKED)
556 child_has_forked (int pid
, int *child_pid
)
558 /* This version of Unix doesn't support notification of fork events. */
564 #if !defined(CHILD_HAS_VFORKED)
566 child_has_vforked (int pid
, int *child_pid
)
568 /* This version of Unix doesn't support notification of vfork events.
575 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
577 child_can_follow_vfork_prior_to_exec (void)
579 /* This version of Unix doesn't support notification of vfork events.
580 However, if it did, it probably wouldn't allow vforks to be followed
581 before the following exec.
588 #if !defined(CHILD_POST_FOLLOW_VFORK)
590 child_post_follow_vfork (int parent_pid
, int followed_parent
, int child_pid
,
593 /* This version of Unix doesn't require a meaningful "post follow vfork"
594 operation by a clone debugger.
599 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
601 child_insert_exec_catchpoint (int pid
)
603 /* This version of Unix doesn't support notification of exec events. */
608 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
610 child_remove_exec_catchpoint (int pid
)
612 /* This version of Unix doesn't support notification of exec events. */
617 #if !defined(CHILD_HAS_EXECD)
619 child_has_execd (int pid
, char **execd_pathname
)
621 /* This version of Unix doesn't support notification of exec events.
628 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
630 child_reported_exec_events_per_exec_call (void)
632 /* This version of Unix doesn't support notification of exec events.
639 #if !defined(CHILD_HAS_SYSCALL_EVENT)
641 child_has_syscall_event (int pid
, enum target_waitkind
*kind
, int *syscall_id
)
643 /* This version of Unix doesn't support notification of syscall events.
650 #if !defined(CHILD_HAS_EXITED)
652 child_has_exited (int pid
, int wait_status
, int *exit_status
)
654 if (WIFEXITED (wait_status
))
656 *exit_status
= WEXITSTATUS (wait_status
);
660 if (WIFSIGNALED (wait_status
))
662 *exit_status
= 0; /* ?? Don't know what else to say here. */
666 /* ?? Do we really need to consult the event state, too? Assume the
667 wait_state alone suffices.
675 child_mourn_inferior (void)
677 unpush_target (&child_ops
);
678 generic_mourn_inferior ();
684 /* This variable is controlled by modules that sit atop inftarg that may layer
685 their own process structure atop that provided here. hpux-thread.c does
686 this because of the Hpux user-mode level thread model. */
688 return !child_suppress_run
;
691 /* Send a SIGINT to the process group. This acts just like the user typed a
692 ^C on the controlling terminal.
694 XXX - This may not be correct for all systems. Some may want to use
695 killpg() instead of kill (-pgrp). */
700 extern pid_t inferior_process_group
;
702 kill (-inferior_process_group
, SIGINT
);
705 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
706 struct symtab_and_line
*
707 child_enable_exception_callback (enum exception_event_kind kind
, int enable
)
709 return (struct symtab_and_line
*) NULL
;
713 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
714 struct exception_event_record
*
715 child_get_current_exception_event (void)
717 return (struct exception_event_record
*) NULL
;
722 #if !defined(CHILD_PID_TO_EXEC_FILE)
724 child_pid_to_exec_file (int pid
)
726 /* This version of Unix doesn't support translation of a process ID
727 to the filename of the executable file.
734 child_core_file_to_sym_file (char *core
)
736 /* The target stratum for a running executable need not support
743 #if !defined(CHILD_PID_TO_STR)
745 child_pid_to_str (ptid_t ptid
)
747 return normal_pid_to_str (ptid
);
752 init_child_ops (void)
754 child_ops
.to_shortname
= "child";
755 child_ops
.to_longname
= "Unix child process";
756 child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
757 child_ops
.to_open
= child_open
;
758 child_ops
.to_attach
= child_attach
;
759 child_ops
.to_post_attach
= child_post_attach
;
760 child_ops
.to_require_attach
= child_require_attach
;
761 child_ops
.to_detach
= child_detach
;
762 child_ops
.to_require_detach
= child_require_detach
;
763 child_ops
.to_resume
= child_resume
;
764 child_ops
.to_wait
= child_wait
;
765 child_ops
.to_post_wait
= child_post_wait
;
766 child_ops
.to_fetch_registers
= fetch_inferior_registers
;
767 child_ops
.to_store_registers
= store_inferior_registers
;
768 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
769 child_ops
.to_xfer_memory
= child_xfer_memory
;
770 child_ops
.to_files_info
= child_files_info
;
771 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
772 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
773 child_ops
.to_terminal_init
= terminal_init_inferior
;
774 child_ops
.to_terminal_inferior
= terminal_inferior
;
775 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
776 child_ops
.to_terminal_ours
= terminal_ours
;
777 child_ops
.to_terminal_info
= child_terminal_info
;
778 child_ops
.to_kill
= kill_inferior
;
779 child_ops
.to_create_inferior
= child_create_inferior
;
780 child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
781 child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
782 child_ops
.to_clone_and_follow_inferior
= child_clone_and_follow_inferior
;
783 child_ops
.to_post_follow_inferior_by_clone
= child_post_follow_inferior_by_clone
;
784 child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
785 child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
786 child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
787 child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
788 child_ops
.to_has_forked
= child_has_forked
;
789 child_ops
.to_has_vforked
= child_has_vforked
;
790 child_ops
.to_can_follow_vfork_prior_to_exec
= child_can_follow_vfork_prior_to_exec
;
791 child_ops
.to_post_follow_vfork
= child_post_follow_vfork
;
792 child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
793 child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
794 child_ops
.to_has_execd
= child_has_execd
;
795 child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
796 child_ops
.to_has_syscall_event
= child_has_syscall_event
;
797 child_ops
.to_has_exited
= child_has_exited
;
798 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
799 child_ops
.to_can_run
= child_can_run
;
800 child_ops
.to_thread_alive
= child_thread_alive
;
801 child_ops
.to_pid_to_str
= child_pid_to_str
;
802 child_ops
.to_stop
= child_stop
;
803 child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
804 child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
805 child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
806 child_ops
.to_stratum
= process_stratum
;
807 child_ops
.to_has_all_memory
= 1;
808 child_ops
.to_has_memory
= 1;
809 child_ops
.to_has_stack
= 1;
810 child_ops
.to_has_registers
= 1;
811 child_ops
.to_has_execution
= 1;
812 child_ops
.to_magic
= OPS_MAGIC
;
816 _initialize_inftarg (void)
818 #ifdef HAVE_OPTIONAL_PROC_FS
822 /* If we have an optional /proc filesystem (e.g. under OSF/1),
823 don't add ptrace support if we can access the running GDB via /proc. */
824 #ifndef PROC_NAME_FMT
825 #define PROC_NAME_FMT "/proc/%05d"
827 sprintf (procname
, PROC_NAME_FMT
, getpid ());
828 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
836 add_target (&child_ops
);