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 int child_wait (int, struct target_waitstatus
*);
51 #endif /* CHILD_WAIT */
53 #if !defined(CHILD_POST_WAIT)
54 void child_post_wait (int, 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 (int);
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 (int pid
, struct target_waitstatus
*ourstatus
)
124 char *execd_pathname
= NULL
;
128 enum target_waitkind kind
;
132 set_sigint_trap (); /* Causes SIGINT to be passed on to the
136 pid
= ptrace_wait (inferior_pid
, &status
);
142 clear_sigint_trap ();
146 if (save_errno
== EINTR
)
149 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
150 safe_strerror (save_errno
));
152 /* Claim it exited with unknown signal. */
153 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
154 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
160 if (target_has_exited (pid
, status
, &exit_status
))
162 /* ??rehrauer: For now, ignore this. */
166 if (!target_thread_alive (pid
))
168 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
172 if (target_has_forked (pid
, &related_pid
)
173 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
175 ourstatus
->kind
= TARGET_WAITKIND_FORKED
;
176 ourstatus
->value
.related_pid
= related_pid
;
180 if (target_has_vforked (pid
, &related_pid
)
181 && ((pid
== inferior_pid
) || (related_pid
== inferior_pid
)))
183 ourstatus
->kind
= TARGET_WAITKIND_VFORKED
;
184 ourstatus
->value
.related_pid
= related_pid
;
188 if (target_has_execd (pid
, &execd_pathname
))
190 /* Are we ignoring initial exec events? (This is likely because
191 we're in the process of starting up the inferior, and another
192 (older) mechanism handles those.) If so, we'll report this
193 as a regular stop, not an exec.
195 if (inferior_ignoring_startup_exec_events
)
197 inferior_ignoring_startup_exec_events
--;
201 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
202 ourstatus
->value
.execd_pathname
= execd_pathname
;
207 /* All we must do with these is communicate their occurrence
208 to wait_for_inferior...
210 if (target_has_syscall_event (pid
, &kind
, &syscall_id
))
212 ourstatus
->kind
= kind
;
213 ourstatus
->value
.syscall_id
= syscall_id
;
217 /*## } while (pid != inferior_pid); ## *//* Some other child died or stopped */
218 /* hack for thread testing */
220 while ((pid
!= inferior_pid
) && not_same_real_pid
);
223 store_waitstatus (ourstatus
, status
);
226 #endif /* CHILD_WAIT */
228 #if !defined(CHILD_POST_WAIT)
230 child_post_wait (int pid
, int wait_status
)
232 /* This version of Unix doesn't require a meaningful "post wait"
239 #ifndef CHILD_THREAD_ALIVE
241 /* Check to see if the given thread is alive.
243 FIXME: Is kill() ever the right way to do this? I doubt it, but
244 for now we're going to try and be compatable with the old thread
247 child_thread_alive (int pid
)
249 return (kill (pid
, 0) != -1);
255 child_attach_to_process (char *args
, int from_tty
, int after_fork
)
258 error_no_arg ("process-id to attach");
260 #ifndef ATTACH_DETACH
261 error ("Can't attach to a process on this machine.");
269 pid
= strtol (args
, &dummy
, 0);
270 /* Some targets don't set errno on errors, grrr! */
271 if ((pid
== 0) && (args
== dummy
))
272 error ("Illegal process-id: %s\n", args
);
274 if (pid
== getpid ()) /* Trying to masturbate? */
275 error ("I refuse to debug myself!");
279 exec_file
= (char *) get_exec_file (0);
282 printf_unfiltered ("Attaching after fork to %s\n",
283 target_pid_to_str (pid
));
285 printf_unfiltered ("Attaching to program: %s, %s\n", exec_file
,
286 target_pid_to_str (pid
));
288 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
290 gdb_flush (gdb_stdout
);
296 REQUIRE_ATTACH (pid
);
299 push_target (&child_ops
);
301 #endif /* ATTACH_DETACH */
305 /* Attach to process PID, then initialize for debugging it. */
308 child_attach (char *args
, int from_tty
)
310 child_attach_to_process (args
, from_tty
, 0);
313 #if !defined(CHILD_POST_ATTACH)
315 child_post_attach (int pid
)
317 /* This version of Unix doesn't require a meaningful "post attach"
318 operation by a debugger. */
323 child_require_attach (char *args
, int from_tty
)
325 child_attach_to_process (args
, from_tty
, 1);
329 child_detach_from_process (int pid
, char *args
, int from_tty
, int after_fork
)
337 char *exec_file
= get_exec_file (0);
341 printf_unfiltered ("Detaching after fork from %s\n",
342 target_pid_to_str (pid
));
344 printf_unfiltered ("Detaching from program: %s, %s\n", exec_file
,
345 target_pid_to_str (pid
));
346 gdb_flush (gdb_stdout
);
349 siggnal
= atoi (args
);
354 REQUIRE_DETACH (pid
, siggnal
);
357 error ("This version of Unix does not support detaching a process.");
361 /* Take a program previously attached to and detaches it.
362 The program resumes execution and will no longer stop
363 on signals, etc. We'd better not have left any breakpoints
364 in the program or it'll die when it hits one. For this
365 to work, it may be necessary for the process to have been
366 previously attached. It *might* work if the program was
367 started via the normal ptrace (PTRACE_TRACEME). */
370 child_detach (char *args
, int from_tty
)
372 child_detach_from_process (inferior_pid
, args
, from_tty
, 0);
374 unpush_target (&child_ops
);
378 child_require_detach (int pid
, char *args
, int from_tty
)
380 child_detach_from_process (pid
, args
, from_tty
, 1);
384 /* Get ready to modify the registers array. On machines which store
385 individual registers, this doesn't need to do anything. On machines
386 which store all the registers in one fell swoop, this makes sure
387 that registers contains all the registers from the program being
391 child_prepare_to_store (void)
393 #ifdef CHILD_PREPARE_TO_STORE
394 CHILD_PREPARE_TO_STORE ();
398 /* Print status information about what we're accessing. */
401 child_files_info (struct target_ops
*ignore
)
403 printf_unfiltered ("\tUsing the running image of %s %s.\n",
404 attach_flag
? "attached" : "child", target_pid_to_str (inferior_pid
));
409 child_open (char *arg
, int from_tty
)
411 error ("Use the \"run\" command to start a Unix child process.");
414 /* Stub function which causes the inferior that runs it, to be ptrace-able
415 by its parent process. */
420 /* "Trace me, Dr. Memory!" */
421 call_ptrace (0, 0, (PTRACE_ARG3_TYPE
) 0, 0);
424 /* Stub function which causes the GDB that runs it, to start ptrace-ing
425 the child process. */
430 push_target (&child_ops
);
432 /* On some targets, there must be some explicit synchronization
433 between the parent and child processes after the debugger
434 forks, and before the child execs the debuggee program. This
435 call basically gives permission for the child to exec.
438 target_acknowledge_created_inferior (pid
);
440 /* START_INFERIOR_TRAPS_EXPECTED is defined in inferior.h,
441 * and will be 1 or 2 depending on whether we're starting
442 * without or with a shell.
444 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
446 /* On some targets, there must be some explicit actions taken after
447 the inferior has been started up.
449 target_post_startup_inferior (pid
);
452 /* Start an inferior Unix child process and sets inferior_pid to its pid.
453 EXEC_FILE is the file to run.
454 ALLARGS is a string containing the arguments to the program.
455 ENV is the environment vector to pass. Errors reported with error(). */
458 child_create_inferior (char *exec_file
, char *allargs
, char **env
)
461 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, pre_fork_inferior
, NULL
);
463 fork_inferior (exec_file
, allargs
, env
, ptrace_me
, ptrace_him
, NULL
, NULL
);
465 /* We are at the first instruction we care about. */
466 /* Pedal to the metal... */
467 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
470 #if !defined(CHILD_POST_STARTUP_INFERIOR)
472 child_post_startup_inferior (int pid
)
474 /* This version of Unix doesn't require a meaningful "post startup inferior"
475 operation by a debugger.
480 #if !defined(CHILD_ACKNOWLEDGE_CREATED_INFERIOR)
482 child_acknowledge_created_inferior (int pid
)
484 /* This version of Unix doesn't require a meaningful "acknowledge created inferior"
485 operation by a debugger.
492 child_clone_and_follow_inferior (int child_pid
, int *followed_child
)
494 clone_and_follow_inferior (child_pid
, followed_child
);
496 /* Don't resume CHILD_PID; it's stopped where it ought to be, until
497 the decision gets made elsewhere how to continue it.
502 #if !defined(CHILD_POST_FOLLOW_INFERIOR_BY_CLONE)
504 child_post_follow_inferior_by_clone (void)
506 /* This version of Unix doesn't require a meaningful "post follow inferior"
507 operation by a clone debugger.
512 #if !defined(CHILD_INSERT_FORK_CATCHPOINT)
514 child_insert_fork_catchpoint (int pid
)
516 /* This version of Unix doesn't support notification of fork events. */
521 #if !defined(CHILD_REMOVE_FORK_CATCHPOINT)
523 child_remove_fork_catchpoint (int pid
)
525 /* This version of Unix doesn't support notification of fork events. */
530 #if !defined(CHILD_INSERT_VFORK_CATCHPOINT)
532 child_insert_vfork_catchpoint (int pid
)
534 /* This version of Unix doesn't support notification of vfork events. */
539 #if !defined(CHILD_REMOVE_VFORK_CATCHPOINT)
541 child_remove_vfork_catchpoint (int pid
)
543 /* This version of Unix doesn't support notification of vfork events. */
548 #if !defined(CHILD_HAS_FORKED)
550 child_has_forked (int pid
, int *child_pid
)
552 /* This version of Unix doesn't support notification of fork events. */
558 #if !defined(CHILD_HAS_VFORKED)
560 child_has_vforked (int pid
, int *child_pid
)
562 /* This version of Unix doesn't support notification of vfork events.
569 #if !defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
571 child_can_follow_vfork_prior_to_exec (void)
573 /* This version of Unix doesn't support notification of vfork events.
574 However, if it did, it probably wouldn't allow vforks to be followed
575 before the following exec.
582 #if !defined(CHILD_POST_FOLLOW_VFORK)
584 child_post_follow_vfork (int parent_pid
, int followed_parent
, int child_pid
,
587 /* This version of Unix doesn't require a meaningful "post follow vfork"
588 operation by a clone debugger.
593 #if !defined(CHILD_INSERT_EXEC_CATCHPOINT)
595 child_insert_exec_catchpoint (int pid
)
597 /* This version of Unix doesn't support notification of exec events. */
602 #if !defined(CHILD_REMOVE_EXEC_CATCHPOINT)
604 child_remove_exec_catchpoint (int pid
)
606 /* This version of Unix doesn't support notification of exec events. */
611 #if !defined(CHILD_HAS_EXECD)
613 child_has_execd (int pid
, char **execd_pathname
)
615 /* This version of Unix doesn't support notification of exec events.
622 #if !defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
624 child_reported_exec_events_per_exec_call (void)
626 /* This version of Unix doesn't support notification of exec events.
633 #if !defined(CHILD_HAS_SYSCALL_EVENT)
635 child_has_syscall_event (int pid
, enum target_waitkind
*kind
, int *syscall_id
)
637 /* This version of Unix doesn't support notification of syscall events.
644 #if !defined(CHILD_HAS_EXITED)
646 child_has_exited (int pid
, int wait_status
, int *exit_status
)
648 if (WIFEXITED (wait_status
))
650 *exit_status
= WEXITSTATUS (wait_status
);
654 if (WIFSIGNALED (wait_status
))
656 *exit_status
= 0; /* ?? Don't know what else to say here. */
660 /* ?? Do we really need to consult the event state, too? Assume the
661 wait_state alone suffices.
669 child_mourn_inferior (void)
671 unpush_target (&child_ops
);
672 generic_mourn_inferior ();
678 /* This variable is controlled by modules that sit atop inftarg that may layer
679 their own process structure atop that provided here. hpux-thread.c does
680 this because of the Hpux user-mode level thread model. */
682 return !child_suppress_run
;
685 /* Send a SIGINT to the process group. This acts just like the user typed a
686 ^C on the controlling terminal.
688 XXX - This may not be correct for all systems. Some may want to use
689 killpg() instead of kill (-pgrp). */
694 extern pid_t inferior_process_group
;
696 kill (-inferior_process_group
, SIGINT
);
699 #if !defined(CHILD_ENABLE_EXCEPTION_CALLBACK)
700 struct symtab_and_line
*
701 child_enable_exception_callback (enum exception_event_kind kind
, int enable
)
703 return (struct symtab_and_line
*) NULL
;
707 #if !defined(CHILD_GET_CURRENT_EXCEPTION_EVENT)
708 struct exception_event_record
*
709 child_get_current_exception_event (void)
711 return (struct exception_event_record
*) NULL
;
716 #if !defined(CHILD_PID_TO_EXEC_FILE)
718 child_pid_to_exec_file (int pid
)
720 /* This version of Unix doesn't support translation of a process ID
721 to the filename of the executable file.
728 child_core_file_to_sym_file (char *core
)
730 /* The target stratum for a running executable need not support
737 #if !defined(CHILD_PID_TO_STR)
739 child_pid_to_str (int pid
)
741 return normal_pid_to_str (pid
);
746 init_child_ops (void)
748 child_ops
.to_shortname
= "child";
749 child_ops
.to_longname
= "Unix child process";
750 child_ops
.to_doc
= "Unix child process (started by the \"run\" command).";
751 child_ops
.to_open
= child_open
;
752 child_ops
.to_attach
= child_attach
;
753 child_ops
.to_post_attach
= child_post_attach
;
754 child_ops
.to_require_attach
= child_require_attach
;
755 child_ops
.to_detach
= child_detach
;
756 child_ops
.to_require_detach
= child_require_detach
;
757 child_ops
.to_resume
= child_resume
;
758 child_ops
.to_wait
= child_wait
;
759 child_ops
.to_post_wait
= child_post_wait
;
760 child_ops
.to_fetch_registers
= fetch_inferior_registers
;
761 child_ops
.to_store_registers
= store_inferior_registers
;
762 child_ops
.to_prepare_to_store
= child_prepare_to_store
;
763 child_ops
.to_xfer_memory
= child_xfer_memory
;
764 child_ops
.to_files_info
= child_files_info
;
765 child_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
766 child_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
767 child_ops
.to_terminal_init
= terminal_init_inferior
;
768 child_ops
.to_terminal_inferior
= terminal_inferior
;
769 child_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
770 child_ops
.to_terminal_ours
= terminal_ours
;
771 child_ops
.to_terminal_info
= child_terminal_info
;
772 child_ops
.to_kill
= kill_inferior
;
773 child_ops
.to_create_inferior
= child_create_inferior
;
774 child_ops
.to_post_startup_inferior
= child_post_startup_inferior
;
775 child_ops
.to_acknowledge_created_inferior
= child_acknowledge_created_inferior
;
776 child_ops
.to_clone_and_follow_inferior
= child_clone_and_follow_inferior
;
777 child_ops
.to_post_follow_inferior_by_clone
= child_post_follow_inferior_by_clone
;
778 child_ops
.to_insert_fork_catchpoint
= child_insert_fork_catchpoint
;
779 child_ops
.to_remove_fork_catchpoint
= child_remove_fork_catchpoint
;
780 child_ops
.to_insert_vfork_catchpoint
= child_insert_vfork_catchpoint
;
781 child_ops
.to_remove_vfork_catchpoint
= child_remove_vfork_catchpoint
;
782 child_ops
.to_has_forked
= child_has_forked
;
783 child_ops
.to_has_vforked
= child_has_vforked
;
784 child_ops
.to_can_follow_vfork_prior_to_exec
= child_can_follow_vfork_prior_to_exec
;
785 child_ops
.to_post_follow_vfork
= child_post_follow_vfork
;
786 child_ops
.to_insert_exec_catchpoint
= child_insert_exec_catchpoint
;
787 child_ops
.to_remove_exec_catchpoint
= child_remove_exec_catchpoint
;
788 child_ops
.to_has_execd
= child_has_execd
;
789 child_ops
.to_reported_exec_events_per_exec_call
= child_reported_exec_events_per_exec_call
;
790 child_ops
.to_has_syscall_event
= child_has_syscall_event
;
791 child_ops
.to_has_exited
= child_has_exited
;
792 child_ops
.to_mourn_inferior
= child_mourn_inferior
;
793 child_ops
.to_can_run
= child_can_run
;
794 child_ops
.to_thread_alive
= child_thread_alive
;
795 child_ops
.to_pid_to_str
= child_pid_to_str
;
796 child_ops
.to_stop
= child_stop
;
797 child_ops
.to_enable_exception_callback
= child_enable_exception_callback
;
798 child_ops
.to_get_current_exception_event
= child_get_current_exception_event
;
799 child_ops
.to_pid_to_exec_file
= child_pid_to_exec_file
;
800 child_ops
.to_stratum
= process_stratum
;
801 child_ops
.to_has_all_memory
= 1;
802 child_ops
.to_has_memory
= 1;
803 child_ops
.to_has_stack
= 1;
804 child_ops
.to_has_registers
= 1;
805 child_ops
.to_has_execution
= 1;
806 child_ops
.to_magic
= OPS_MAGIC
;
810 _initialize_inftarg (void)
812 #ifdef HAVE_OPTIONAL_PROC_FS
816 /* If we have an optional /proc filesystem (e.g. under OSF/1),
817 don't add ptrace support if we can access the running GDB via /proc. */
818 #ifndef PROC_NAME_FMT
819 #define PROC_NAME_FMT "/proc/%05d"
821 sprintf (procname
, PROC_NAME_FMT
, getpid ());
822 if ((fd
= open (procname
, O_RDONLY
)) >= 0)
830 add_target (&child_ops
);