1 /* Fork a Unix child process, and set up to debug it, for GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "frame.h" /* required by inferior.h */
27 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
31 #ifdef SET_STACK_LIMIT_HUGE
33 #include <sys/resource.h>
35 extern int original_stack_limit
;
36 #endif /* SET_STACK_LIMIT_HUGE */
38 extern char **environ
;
40 /* Start an inferior Unix child process and sets inferior_pid to its pid.
41 EXEC_FILE is the file to run.
42 ALLARGS is a string containing the arguments to the program.
43 ENV is the environment vector to pass. Errors reported with error(). */
46 #define SHELL_FILE "/bin/sh"
50 fork_inferior (exec_file
, allargs
, env
, traceme_fun
, init_trace_fun
)
54 void (*traceme_fun
) PARAMS ((void));
55 void (*init_trace_fun
) PARAMS ((int));
60 static char default_shell_file
[] = SHELL_FILE
;
63 /* Set debug_fork then attach to the child while it sleeps, to debug. */
64 static int debug_fork
= 0;
65 /* This is set to the result of setpgrp, which if vforked, will be visible
66 to you in the parent process. It's only used by humans for debugging. */
67 static int debug_setpgrp
= 657473;
70 /* If no exec file handed to us, get it from the exec-file command -- with
71 a good, common error message if none is specified. */
73 exec_file
= get_exec_file(1);
75 /* The user might want tilde-expansion, and in general probably wants
76 the program to behave the same way as if run from
77 his/her favorite shell. So we let the shell run it for us.
78 FIXME, this should probably search the local environment (as
79 modified by the setenv command), not the env gdb inherited. */
80 shell_file
= getenv ("SHELL");
81 if (shell_file
== NULL
)
82 shell_file
= default_shell_file
;
84 /* Multiplying the length of exec_file by 4 is to account for the fact
85 that it may expand when quoted; it is a worst-case number based on
86 every character being '. */
87 len
= 5 + 4 * strlen (exec_file
) + 1 + strlen (allargs
) + 1 + /*slop*/ 12;
88 /* If desired, concat something onto the front of ALLARGS.
89 SHELL_COMMAND is the result. */
90 #ifdef SHELL_COMMAND_CONCAT
91 shell_command
= (char *) alloca (strlen (SHELL_COMMAND_CONCAT
) + len
);
92 strcpy (shell_command
, SHELL_COMMAND_CONCAT
);
94 shell_command
= (char *) alloca (len
);
95 shell_command
[0] = '\0';
97 strcat (shell_command
, "exec ");
99 /* Now add exec_file, quoting as necessary. Quoting in this style is
100 said to work with all shells. */
104 strcat (shell_command
, "'");
105 for (p
= exec_file
; *p
!= '\0'; ++p
)
108 strcat (shell_command
, "'\\''");
110 strncat (shell_command
, p
, 1);
112 strcat (shell_command
, "'");
115 strcat (shell_command
, " ");
116 strcat (shell_command
, allargs
);
118 /* exec is said to fail if the executable is open. */
121 /* Retain a copy of our environment variables, since the child will
122 replace the value of environ and if we're vforked, we have to
124 save_our_env
= environ
;
126 /* Tell the terminal handling subsystem what tty we plan to run on;
127 it will just record the information for later. */
129 new_tty_prefork (inferior_io_terminal
);
131 /* It is generally good practice to flush any possible pending stdio
132 output prior to doing a fork, to avoid the possibility of both the
133 parent and child flushing the same data after the fork. */
138 #if defined(USG) && !defined(HAVE_VFORK)
148 perror_with_name ("vfork");
156 /* Run inferior in a separate process group. */
157 #ifdef NEED_POSIX_SETPGID
158 debug_setpgrp
= setpgid (0, 0);
160 #if defined(USG) && !defined(SETPGRP_ARGS)
161 debug_setpgrp
= setpgrp ();
163 debug_setpgrp
= setpgrp (getpid (), getpid ());
165 #endif /* NEED_POSIX_SETPGID */
166 if (debug_setpgrp
== -1)
167 perror("setpgrp failed in child");
168 #endif /* TIOCGPGRP */
170 #ifdef SET_STACK_LIMIT_HUGE
171 /* Reset the stack limit back to what it was. */
175 getrlimit (RLIMIT_STACK
, &rlim
);
176 rlim
.rlim_cur
= original_stack_limit
;
177 setrlimit (RLIMIT_STACK
, &rlim
);
179 #endif /* SET_STACK_LIMIT_HUGE */
181 /* Ask the tty subsystem to switch to the one we specified earlier
182 (or to share the current terminal, if none was specified). */
186 /* Changing the signal handlers for the inferior after
187 a vfork can also change them for the superior, so we don't mess
188 with signals here. See comments in
189 initialize_signals for how we get the right signal handlers
192 /* "Trace me, Dr. Memory!" */
195 /* There is no execlpe call, so we have to set the environment
196 for our child in the global variable. If we've vforked, this
197 clobbers the parent, but environ is restored a few lines down
198 in the parent. By the way, yes we do need to look down the
199 path to find $SHELL. Rich Pixley says so, and I agree. */
201 execlp (shell_file
, shell_file
, "-c", shell_command
, (char *)0);
203 fprintf (stderr
, "Cannot exec %s: %s.\n", shell_file
,
204 safe_strerror (errno
));
209 /* Restore our environment in case a vforked child clob'd it. */
210 environ
= save_our_env
;
212 /* Now that we have a child process, make it our target, and
213 initialize anything target-vector-specific that needs initializing. */
214 (*init_trace_fun
)(pid
);
216 #ifdef CREATE_INFERIOR_HOOK
217 CREATE_INFERIOR_HOOK (pid
);
220 /* The process was started by the fork that created it,
221 but it will have stopped one instruction after execing the shell.
222 Here we must get it up to actual execution of the real program. */
224 inferior_pid
= pid
; /* Needed for wait_for_inferior stuff below */
226 clear_proceed_status ();
228 /* We will get a trace trap after one instruction.
229 Continue it automatically. Eventually (after shell does an exec)
230 it will get another trace trap. Then insert breakpoints and continue. */
232 #ifdef START_INFERIOR_TRAPS_EXPECTED
233 pending_execs
= START_INFERIOR_TRAPS_EXPECTED
;
238 init_wait_for_inferior ();
240 /* Set up the "saved terminal modes" of the inferior
241 based on what modes we are starting it with. */
242 target_terminal_init ();
244 /* Install inferior's terminal modes. */
245 target_terminal_inferior ();
249 stop_soon_quietly
= 1; /* Make wait_for_inferior be quiet */
250 wait_for_inferior ();
251 if (stop_signal
!= SIGTRAP
)
253 /* Let shell child handle its own signals in its own way */
254 /* FIXME, what if child has exit()ed? Must exit loop somehow */
255 resume (0, stop_signal
);
259 /* We handle SIGTRAP, however; it means child did an exec. */
260 if (0 == --pending_execs
)
262 resume (0, 0); /* Just make it go on */
265 stop_soon_quietly
= 0;
267 /* We are now in the child process of interest, having exec'd the
268 correct program, and are poised at the first instruction of the
270 #ifdef SOLIB_CREATE_INFERIOR_HOOK
271 SOLIB_CREATE_INFERIOR_HOOK (pid
);