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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
23 #include "frame.h" /* required by inferior.h */
36 extern char **environ
;
39 #define SHELL_FILE "/bin/sh"
42 /* Start an inferior Unix child process and sets inferior_pid to its pid.
43 EXEC_FILE is the file to run.
44 ALLARGS is a string containing the arguments to the program.
45 ENV is the environment vector to pass. SHELL_FILE is the shell file,
46 or NULL if we should pick one. Errors reported with error(). */
49 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
;
62 /* Set debug_fork then attach to the child while it sleeps, to debug. */
63 static int debug_fork
= 0;
64 /* This is set to the result of setpgrp, which if vforked, will be visible
65 to you in the parent process. It's only used by humans for debugging. */
66 static int debug_setpgrp
= 657473;
69 /* If no exec file handed to us, get it from the exec-file command -- with
70 a good, common error message if none is specified. */
72 exec_file
= get_exec_file(1);
74 /* The user might want tilde-expansion, and in general probably wants
75 the program to behave the same way as if run from
76 his/her favorite shell. So we let the shell run it for us.
77 FIXME-maybe, we might want a "set shell" command so the user can change
78 the shell from within GDB (if so, change callers which pass in a non-NULL
80 if (shell_file
== NULL
)
81 shell_file
= getenv ("SHELL");
82 if (shell_file
== NULL
)
83 shell_file
= default_shell_file
;
85 /* Multiplying the length of exec_file by 4 is to account for the fact
86 that it may expand when quoted; it is a worst-case number based on
87 every character being '. */
88 len
= 5 + 4 * strlen (exec_file
) + 1 + strlen (allargs
) + 1 + /*slop*/ 12;
89 /* If desired, concat something onto the front of ALLARGS.
90 SHELL_COMMAND is the result. */
91 #ifdef SHELL_COMMAND_CONCAT
92 shell_command
= (char *) alloca (strlen (SHELL_COMMAND_CONCAT
) + len
);
93 strcpy (shell_command
, SHELL_COMMAND_CONCAT
);
95 shell_command
= (char *) alloca (len
);
96 shell_command
[0] = '\0';
98 strcat (shell_command
, "exec ");
100 /* Now add exec_file, quoting as necessary. */
105 /* Quoting in this style is said to work with all shells. But csh
106 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
140 strcat (shell_command
, "'");
141 for (p
= exec_file
; *p
!= '\0'; ++p
)
144 strcat (shell_command
, "'\\''");
146 strncat (shell_command
, p
, 1);
148 strcat (shell_command
, "'");
151 strcat (shell_command
, exec_file
);
154 strcat (shell_command
, " ");
155 strcat (shell_command
, allargs
);
157 /* exec is said to fail if the executable is open. */
160 /* Retain a copy of our environment variables, since the child will
161 replace the value of environ and if we're vforked, we have to
163 save_our_env
= environ
;
165 /* Tell the terminal handling subsystem what tty we plan to run on;
166 it will just record the information for later. */
168 new_tty_prefork (inferior_io_terminal
);
170 /* It is generally good practice to flush any possible pending stdio
171 output prior to doing a fork, to avoid the possibility of both the
172 parent and child flushing the same data after the fork. */
174 gdb_flush (gdb_stdout
);
175 gdb_flush (gdb_stderr
);
177 #if defined(USG) && !defined(HAVE_VFORK)
187 perror_with_name ("vfork");
194 /* Run inferior in a separate process group. */
195 debug_setpgrp
= gdb_setpgid ();
196 if (debug_setpgrp
== -1)
197 perror("setpgrp failed in child");
199 /* Ask the tty subsystem to switch to the one we specified earlier
200 (or to share the current terminal, if none was specified). */
204 /* Changing the signal handlers for the inferior after
205 a vfork can also change them for the superior, so we don't mess
206 with signals here. See comments in
207 initialize_signals for how we get the right signal handlers
210 /* "Trace me, Dr. Memory!" */
213 /* There is no execlpe call, so we have to set the environment
214 for our child in the global variable. If we've vforked, this
215 clobbers the parent, but environ is restored a few lines down
216 in the parent. By the way, yes we do need to look down the
217 path to find $SHELL. Rich Pixley says so, and I agree. */
219 execlp (shell_file
, shell_file
, "-c", shell_command
, (char *)0);
221 fprintf_unfiltered (gdb_stderr
, "Cannot exec %s: %s.\n", shell_file
,
222 safe_strerror (errno
));
223 gdb_flush (gdb_stderr
);
227 /* Restore our environment in case a vforked child clob'd it. */
228 environ
= save_our_env
;
232 inferior_pid
= pid
; /* Needed for wait_for_inferior stuff below */
234 /* Now that we have a child process, make it our target, and
235 initialize anything target-vector-specific that needs initializing. */
236 (*init_trace_fun
)(pid
);
238 /* We are now in the child process of interest, having exec'd the
239 correct program, and are poised at the first instruction of the
242 /* Allow target dependant code to play with the new process. This might be
243 used to have target-specific code initialize a variable in the new process
244 prior to executing the first instruction. */
245 TARGET_CREATE_INFERIOR_HOOK (pid
);
247 #ifdef SOLIB_CREATE_INFERIOR_HOOK
248 SOLIB_CREATE_INFERIOR_HOOK (pid
);
252 /* Accept NTRAPS traps from the inferior. */
255 startup_inferior (ntraps
)
258 int pending_execs
= ntraps
;
259 int terminal_initted
;
261 /* The process was started by the fork that created it,
262 but it will have stopped one instruction after execing the shell.
263 Here we must get it up to actual execution of the real program. */
265 clear_proceed_status ();
267 init_wait_for_inferior ();
269 terminal_initted
= 0;
271 #ifdef STARTUP_INFERIOR
272 STARTUP_INFERIOR (pending_execs
);
276 stop_soon_quietly
= 1; /* Make wait_for_inferior be quiet */
277 wait_for_inferior ();
278 if (stop_signal
!= TARGET_SIGNAL_TRAP
)
280 /* Let shell child handle its own signals in its own way */
281 /* FIXME, what if child has exit()ed? Must exit loop somehow */
282 resume (0, stop_signal
);
286 /* We handle SIGTRAP, however; it means child did an exec. */
287 if (!terminal_initted
)
289 /* Now that the child has exec'd we know it has already set its
290 process group. On POSIX systems, tcsetpgrp will fail with
291 EPERM if we try it before the child's setpgid. */
293 /* Set up the "saved terminal modes" of the inferior
294 based on what modes we are starting it with. */
295 target_terminal_init ();
297 /* Install inferior's terminal modes. */
298 target_terminal_inferior ();
300 terminal_initted
= 1;
302 if (0 == --pending_execs
)
304 resume (0, TARGET_SIGNAL_0
); /* Just make it go on */
307 #endif /* STARTUP_INFERIOR */
308 stop_soon_quietly
= 0;
This page took 0.0366610000000001 seconds and 4 git commands to generate.