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 #ifdef SET_STACK_LIMIT_HUGE
35 #include <sys/resource.h>
37 extern int original_stack_limit
;
38 #endif /* SET_STACK_LIMIT_HUGE */
40 extern char **environ
;
43 #define SHELL_FILE "/bin/sh"
46 /* Start an inferior Unix child process and sets inferior_pid to its pid.
47 EXEC_FILE is the file to run.
48 ALLARGS is a string containing the arguments to the program.
49 ENV is the environment vector to pass. SHELL_FILE is the shell file,
50 or NULL if we should pick one. Errors reported with error(). */
53 fork_inferior (exec_file
, allargs
, env
, traceme_fun
, init_trace_fun
,
58 void (*traceme_fun
) PARAMS ((void));
59 void (*init_trace_fun
) PARAMS ((int));
64 static char default_shell_file
[] = SHELL_FILE
;
66 /* Set debug_fork then attach to the child while it sleeps, to debug. */
67 static int debug_fork
= 0;
68 /* This is set to the result of setpgrp, which if vforked, will be visible
69 to you in the parent process. It's only used by humans for debugging. */
70 static int debug_setpgrp
= 657473;
73 /* If no exec file handed to us, get it from the exec-file command -- with
74 a good, common error message if none is specified. */
76 exec_file
= get_exec_file(1);
78 /* The user might want tilde-expansion, and in general probably wants
79 the program to behave the same way as if run from
80 his/her favorite shell. So we let the shell run it for us.
81 FIXME-maybe, we might want a "set shell" command so the user can change
82 the shell from within GDB (if so, change callers which pass in a non-NULL
84 if (shell_file
== NULL
)
85 shell_file
= getenv ("SHELL");
86 if (shell_file
== NULL
)
87 shell_file
= default_shell_file
;
89 /* Multiplying the length of exec_file by 4 is to account for the fact
90 that it may expand when quoted; it is a worst-case number based on
91 every character being '. */
92 len
= 5 + 4 * strlen (exec_file
) + 1 + strlen (allargs
) + 1 + /*slop*/ 12;
93 /* If desired, concat something onto the front of ALLARGS.
94 SHELL_COMMAND is the result. */
95 #ifdef SHELL_COMMAND_CONCAT
96 shell_command
= (char *) alloca (strlen (SHELL_COMMAND_CONCAT
) + len
);
97 strcpy (shell_command
, SHELL_COMMAND_CONCAT
);
99 shell_command
= (char *) alloca (len
);
100 shell_command
[0] = '\0';
102 strcat (shell_command
, "exec ");
104 /* Now add exec_file, quoting as necessary. */
109 /* Quoting in this style is said to work with all shells. But csh
110 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
144 strcat (shell_command
, "'");
145 for (p
= exec_file
; *p
!= '\0'; ++p
)
148 strcat (shell_command
, "'\\''");
150 strncat (shell_command
, p
, 1);
152 strcat (shell_command
, "'");
155 strcat (shell_command
, exec_file
);
158 strcat (shell_command
, " ");
159 strcat (shell_command
, allargs
);
161 /* exec is said to fail if the executable is open. */
164 /* Retain a copy of our environment variables, since the child will
165 replace the value of environ and if we're vforked, we have to
167 save_our_env
= environ
;
169 /* Tell the terminal handling subsystem what tty we plan to run on;
170 it will just record the information for later. */
172 new_tty_prefork (inferior_io_terminal
);
174 /* It is generally good practice to flush any possible pending stdio
175 output prior to doing a fork, to avoid the possibility of both the
176 parent and child flushing the same data after the fork. */
178 gdb_flush (gdb_stdout
);
179 gdb_flush (gdb_stderr
);
181 #if defined(USG) && !defined(HAVE_VFORK)
191 perror_with_name ("vfork");
198 /* Run inferior in a separate process group. */
199 debug_setpgrp
= gdb_setpgid ();
200 if (debug_setpgrp
== -1)
201 perror("setpgrp failed in child");
203 #ifdef SET_STACK_LIMIT_HUGE
204 /* Reset the stack limit back to what it was. */
208 getrlimit (RLIMIT_STACK
, &rlim
);
209 rlim
.rlim_cur
= original_stack_limit
;
210 setrlimit (RLIMIT_STACK
, &rlim
);
212 #endif /* SET_STACK_LIMIT_HUGE */
214 /* Ask the tty subsystem to switch to the one we specified earlier
215 (or to share the current terminal, if none was specified). */
219 /* Changing the signal handlers for the inferior after
220 a vfork can also change them for the superior, so we don't mess
221 with signals here. See comments in
222 initialize_signals for how we get the right signal handlers
225 /* "Trace me, Dr. Memory!" */
228 /* There is no execlpe call, so we have to set the environment
229 for our child in the global variable. If we've vforked, this
230 clobbers the parent, but environ is restored a few lines down
231 in the parent. By the way, yes we do need to look down the
232 path to find $SHELL. Rich Pixley says so, and I agree. */
234 execlp (shell_file
, shell_file
, "-c", shell_command
, (char *)0);
236 fprintf_unfiltered (gdb_stderr
, "Cannot exec %s: %s.\n", shell_file
,
237 safe_strerror (errno
));
238 gdb_flush (gdb_stderr
);
242 /* Restore our environment in case a vforked child clob'd it. */
243 environ
= save_our_env
;
247 inferior_pid
= pid
; /* Needed for wait_for_inferior stuff below */
249 /* Now that we have a child process, make it our target, and
250 initialize anything target-vector-specific that needs initializing. */
251 (*init_trace_fun
)(pid
);
253 /* We are now in the child process of interest, having exec'd the
254 correct program, and are poised at the first instruction of the
256 #ifdef SOLIB_CREATE_INFERIOR_HOOK
257 SOLIB_CREATE_INFERIOR_HOOK (pid
);
261 /* Accept NTRAPS traps from the inferior. */
264 startup_inferior (ntraps
)
267 int pending_execs
= ntraps
;
268 int terminal_initted
;
270 /* The process was started by the fork that created it,
271 but it will have stopped one instruction after execing the shell.
272 Here we must get it up to actual execution of the real program. */
274 clear_proceed_status ();
276 init_wait_for_inferior ();
278 terminal_initted
= 0;
282 stop_soon_quietly
= 1; /* Make wait_for_inferior be quiet */
283 wait_for_inferior ();
284 if (stop_signal
!= TARGET_SIGNAL_TRAP
)
286 /* Let shell child handle its own signals in its own way */
287 /* FIXME, what if child has exit()ed? Must exit loop somehow */
288 resume (0, stop_signal
);
292 /* We handle SIGTRAP, however; it means child did an exec. */
293 if (!terminal_initted
)
295 /* Now that the child has exec'd we know it has already set its
296 process group. On POSIX systems, tcsetpgrp will fail with
297 EPERM if we try it before the child's setpgid. */
299 /* Set up the "saved terminal modes" of the inferior
300 based on what modes we are starting it with. */
301 target_terminal_init ();
303 /* Install inferior's terminal modes. */
304 target_terminal_inferior ();
306 terminal_initted
= 1;
308 if (0 == --pending_execs
)
310 resume (0, TARGET_SIGNAL_0
); /* Just make it go on */
313 stop_soon_quietly
= 0;
This page took 0.035326 seconds and 4 git commands to generate.