1 /* Fork a Unix child process, and set up to debug it, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994 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. */
23 #include "frame.h" /* required by inferior.h */
33 extern char **environ;
36 #define SHELL_FILE "/bin/sh"
39 /* Start an inferior Unix child process and sets inferior_pid to its pid.
40 EXEC_FILE is the file to run.
41 ALLARGS is a string containing the arguments to the program.
42 ENV is the environment vector to pass. SHELL_FILE is the shell file,
43 or NULL if we should pick one. Errors reported with error(). */
46 fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
51 void (*traceme_fun) PARAMS ((void));
52 void (*init_trace_fun) PARAMS ((int));
57 static char default_shell_file[] = SHELL_FILE;
59 /* Set debug_fork then attach to the child while it sleeps, to debug. */
60 static int debug_fork = 0;
61 /* This is set to the result of setpgrp, which if vforked, will be visible
62 to you in the parent process. It's only used by humans for debugging. */
63 static int debug_setpgrp = 657473;
66 /* If no exec file handed to us, get it from the exec-file command -- with
67 a good, common error message if none is specified. */
69 exec_file = get_exec_file(1);
71 /* The user might want tilde-expansion, and in general probably wants
72 the program to behave the same way as if run from
73 his/her favorite shell. So we let the shell run it for us.
74 FIXME-maybe, we might want a "set shell" command so the user can change
75 the shell from within GDB (if so, change callers which pass in a non-NULL
77 if (shell_file == NULL)
78 shell_file = getenv ("SHELL");
79 if (shell_file == NULL)
80 shell_file = default_shell_file;
82 /* Multiplying the length of exec_file by 4 is to account for the fact
83 that it may expand when quoted; it is a worst-case number based on
84 every character being '. */
85 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
86 /* If desired, concat something onto the front of ALLARGS.
87 SHELL_COMMAND is the result. */
88 #ifdef SHELL_COMMAND_CONCAT
89 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
90 strcpy (shell_command, SHELL_COMMAND_CONCAT);
92 shell_command = (char *) alloca (len);
93 shell_command[0] = '\0';
95 strcat (shell_command, "exec ");
97 /* Now add exec_file, quoting as necessary. */
102 /* Quoting in this style is said to work with all shells. But csh
103 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
137 strcat (shell_command, "'");
138 for (p = exec_file; *p != '\0'; ++p)
141 strcat (shell_command, "'\\''");
143 strncat (shell_command, p, 1);
145 strcat (shell_command, "'");
148 strcat (shell_command, exec_file);
151 strcat (shell_command, " ");
152 strcat (shell_command, allargs);
154 /* exec is said to fail if the executable is open. */
157 /* Retain a copy of our environment variables, since the child will
158 replace the value of environ and if we're vforked, we have to
160 save_our_env = environ;
162 /* Tell the terminal handling subsystem what tty we plan to run on;
163 it will just record the information for later. */
165 new_tty_prefork (inferior_io_terminal);
167 /* It is generally good practice to flush any possible pending stdio
168 output prior to doing a fork, to avoid the possibility of both the
169 parent and child flushing the same data after the fork. */
171 gdb_flush (gdb_stdout);
172 gdb_flush (gdb_stderr);
174 #if defined(USG) && !defined(HAVE_VFORK)
184 perror_with_name ("vfork");
191 /* Run inferior in a separate process group. */
192 debug_setpgrp = gdb_setpgid ();
193 if (debug_setpgrp == -1)
194 perror("setpgrp failed in child");
196 /* Ask the tty subsystem to switch to the one we specified earlier
197 (or to share the current terminal, if none was specified). */
201 /* Changing the signal handlers for the inferior after
202 a vfork can also change them for the superior, so we don't mess
203 with signals here. See comments in
204 initialize_signals for how we get the right signal handlers
207 /* "Trace me, Dr. Memory!" */
210 /* There is no execlpe call, so we have to set the environment
211 for our child in the global variable. If we've vforked, this
212 clobbers the parent, but environ is restored a few lines down
213 in the parent. By the way, yes we do need to look down the
214 path to find $SHELL. Rich Pixley says so, and I agree. */
216 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
218 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
219 safe_strerror (errno));
220 gdb_flush (gdb_stderr);
224 /* Restore our environment in case a vforked child clob'd it. */
225 environ = save_our_env;
229 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
231 /* Now that we have a child process, make it our target, and
232 initialize anything target-vector-specific that needs initializing. */
233 (*init_trace_fun)(pid);
235 /* We are now in the child process of interest, having exec'd the
236 correct program, and are poised at the first instruction of the
238 #ifdef SOLIB_CREATE_INFERIOR_HOOK
239 SOLIB_CREATE_INFERIOR_HOOK (pid);
243 /* Accept NTRAPS traps from the inferior. */
246 startup_inferior (ntraps)
249 int pending_execs = ntraps;
250 int terminal_initted;
252 /* The process was started by the fork that created it,
253 but it will have stopped one instruction after execing the shell.
254 Here we must get it up to actual execution of the real program. */
256 clear_proceed_status ();
258 init_wait_for_inferior ();
260 terminal_initted = 0;
264 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
265 wait_for_inferior ();
266 if (stop_signal != TARGET_SIGNAL_TRAP)
268 /* Let shell child handle its own signals in its own way */
269 /* FIXME, what if child has exit()ed? Must exit loop somehow */
270 resume (0, stop_signal);
274 /* We handle SIGTRAP, however; it means child did an exec. */
275 if (!terminal_initted)
277 /* Now that the child has exec'd we know it has already set its
278 process group. On POSIX systems, tcsetpgrp will fail with
279 EPERM if we try it before the child's setpgid. */
281 /* Set up the "saved terminal modes" of the inferior
282 based on what modes we are starting it with. */
283 target_terminal_init ();
285 /* Install inferior's terminal modes. */
286 target_terminal_inferior ();
288 terminal_initted = 1;
290 if (0 == --pending_execs)
292 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
295 stop_soon_quietly = 0;