* config/m68k/monitor.mt (TDEPFILE): Add remote-es.o.
[deliverable/binutils-gdb.git] / gdb / fork-child.c
CommitLineData
3fbdd536
JG
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.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "defs.h"
22#include "frame.h" /* required by inferior.h */
23#include "inferior.h"
24#include "target.h"
25#include "wait.h"
26#include "gdbcore.h"
c18b613a 27#include "terminal.h"
100f92e2 28#include "thread.h"
3fbdd536
JG
29
30#include <signal.h>
31
32#ifdef SET_STACK_LIMIT_HUGE
33#include <sys/time.h>
34#include <sys/resource.h>
35
36extern int original_stack_limit;
37#endif /* SET_STACK_LIMIT_HUGE */
38
39extern char **environ;
40
3fbdd536
JG
41#ifndef SHELL_FILE
42#define SHELL_FILE "/bin/sh"
43#endif
44
08f74b92
JK
45/* Start an inferior Unix child process and sets inferior_pid to its pid.
46 EXEC_FILE is the file to run.
47 ALLARGS is a string containing the arguments to the program.
48 ENV is the environment vector to pass. SHELL_FILE is the shell file,
49 or NULL if we should pick one. Errors reported with error(). */
50
3fbdd536 51void
08f74b92
JK
52fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
53 shell_file)
3fbdd536
JG
54 char *exec_file;
55 char *allargs;
56 char **env;
57 void (*traceme_fun) PARAMS ((void));
58 void (*init_trace_fun) PARAMS ((int));
08f74b92 59 char *shell_file;
3fbdd536
JG
60{
61 int pid;
62 char *shell_command;
3fbdd536
JG
63 static char default_shell_file[] = SHELL_FILE;
64 int len;
3fbdd536
JG
65 /* Set debug_fork then attach to the child while it sleeps, to debug. */
66 static int debug_fork = 0;
67 /* This is set to the result of setpgrp, which if vforked, will be visible
68 to you in the parent process. It's only used by humans for debugging. */
69 static int debug_setpgrp = 657473;
70 char **save_our_env;
71
72 /* If no exec file handed to us, get it from the exec-file command -- with
73 a good, common error message if none is specified. */
74 if (exec_file == 0)
75 exec_file = get_exec_file(1);
76
77 /* The user might want tilde-expansion, and in general probably wants
78 the program to behave the same way as if run from
79 his/her favorite shell. So we let the shell run it for us.
4142aab8 80 FIXME-maybe, we might want a "set shell" command so the user can change
08f74b92
JK
81 the shell from within GDB (if so, change callers which pass in a non-NULL
82 shell_file too). */
83 if (shell_file == NULL)
84 shell_file = getenv ("SHELL");
3fbdd536
JG
85 if (shell_file == NULL)
86 shell_file = default_shell_file;
3768398d
JK
87
88 /* Multiplying the length of exec_file by 4 is to account for the fact
89 that it may expand when quoted; it is a worst-case number based on
90 every character being '. */
91 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
3fbdd536
JG
92 /* If desired, concat something onto the front of ALLARGS.
93 SHELL_COMMAND is the result. */
94#ifdef SHELL_COMMAND_CONCAT
95 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
96 strcpy (shell_command, SHELL_COMMAND_CONCAT);
97#else
98 shell_command = (char *) alloca (len);
99 shell_command[0] = '\0';
100#endif
101 strcat (shell_command, "exec ");
3768398d 102
38bbfd37 103 /* Now add exec_file, quoting as necessary. */
3768398d
JK
104 {
105 char *p;
38bbfd37 106 int need_to_quote;
3768398d 107
38bbfd37
JK
108 /* Quoting in this style is said to work with all shells. But csh
109 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
110 to. */
111 p = exec_file;
112 while (1)
3768398d 113 {
38bbfd37
JK
114 switch (*p)
115 {
116 case '\'':
117 case '"':
118 case '(':
119 case ')':
120 case '$':
121 case '&':
122 case ';':
123 case '<':
124 case '>':
125 case ' ':
126 case '\n':
127 case '\t':
128 need_to_quote = 1;
129 goto end_scan;
130
131 case '\0':
132 need_to_quote = 0;
133 goto end_scan;
134
135 default:
136 break;
137 }
138 ++p;
3768398d 139 }
38bbfd37
JK
140 end_scan:
141 if (need_to_quote)
142 {
143 strcat (shell_command, "'");
144 for (p = exec_file; *p != '\0'; ++p)
145 {
146 if (*p == '\'')
147 strcat (shell_command, "'\\''");
148 else
149 strncat (shell_command, p, 1);
150 }
151 strcat (shell_command, "'");
152 }
153 else
154 strcat (shell_command, exec_file);
3768398d
JK
155 }
156
3fbdd536
JG
157 strcat (shell_command, " ");
158 strcat (shell_command, allargs);
159
160 /* exec is said to fail if the executable is open. */
161 close_exec_file ();
162
163 /* Retain a copy of our environment variables, since the child will
164 replace the value of environ and if we're vforked, we have to
165 restore it. */
166 save_our_env = environ;
167
168 /* Tell the terminal handling subsystem what tty we plan to run on;
169 it will just record the information for later. */
170
171 new_tty_prefork (inferior_io_terminal);
172
173 /* It is generally good practice to flush any possible pending stdio
174 output prior to doing a fork, to avoid the possibility of both the
175 parent and child flushing the same data after the fork. */
176
199b2450
TL
177 gdb_flush (gdb_stdout);
178 gdb_flush (gdb_stderr);
3fbdd536
JG
179
180#if defined(USG) && !defined(HAVE_VFORK)
181 pid = fork ();
182#else
183 if (debug_fork)
184 pid = fork ();
185 else
186 pid = vfork ();
187#endif
188
189 if (pid < 0)
190 perror_with_name ("vfork");
191
192 if (pid == 0)
193 {
194 if (debug_fork)
195 sleep (debug_fork);
196
3fbdd536 197 /* Run inferior in a separate process group. */
c2e247c4 198 debug_setpgrp = gdb_setpgid ();
3fbdd536
JG
199 if (debug_setpgrp == -1)
200 perror("setpgrp failed in child");
3fbdd536
JG
201
202#ifdef SET_STACK_LIMIT_HUGE
203 /* Reset the stack limit back to what it was. */
204 {
205 struct rlimit rlim;
206
207 getrlimit (RLIMIT_STACK, &rlim);
208 rlim.rlim_cur = original_stack_limit;
209 setrlimit (RLIMIT_STACK, &rlim);
210 }
211#endif /* SET_STACK_LIMIT_HUGE */
212
213 /* Ask the tty subsystem to switch to the one we specified earlier
214 (or to share the current terminal, if none was specified). */
215
216 new_tty ();
217
218 /* Changing the signal handlers for the inferior after
219 a vfork can also change them for the superior, so we don't mess
220 with signals here. See comments in
221 initialize_signals for how we get the right signal handlers
222 for the inferior. */
223
224 /* "Trace me, Dr. Memory!" */
225 (*traceme_fun) ();
226
227 /* There is no execlpe call, so we have to set the environment
228 for our child in the global variable. If we've vforked, this
229 clobbers the parent, but environ is restored a few lines down
230 in the parent. By the way, yes we do need to look down the
231 path to find $SHELL. Rich Pixley says so, and I agree. */
232 environ = env;
233 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
234
199b2450 235 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
3fbdd536 236 safe_strerror (errno));
199b2450 237 gdb_flush (gdb_stderr);
3fbdd536
JG
238 _exit (0177);
239 }
240
241 /* Restore our environment in case a vforked child clob'd it. */
242 environ = save_our_env;
243
de43d7d0
SG
244 init_thread_list();
245
bc28a06c
JK
246 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
247
3fbdd536
JG
248 /* Now that we have a child process, make it our target, and
249 initialize anything target-vector-specific that needs initializing. */
250 (*init_trace_fun)(pid);
251
bc28a06c
JK
252 /* We are now in the child process of interest, having exec'd the
253 correct program, and are poised at the first instruction of the
254 new program. */
255#ifdef SOLIB_CREATE_INFERIOR_HOOK
256 SOLIB_CREATE_INFERIOR_HOOK (pid);
257#endif
258}
259
260/* Accept NTRAPS traps from the inferior. */
261
262void
263startup_inferior (ntraps)
264 int ntraps;
265{
266 int pending_execs = ntraps;
267 int terminal_initted;
268
5d76c8e6
JK
269 /* The process was started by the fork that created it,
270 but it will have stopped one instruction after execing the shell.
271 Here we must get it up to actual execution of the real program. */
3fbdd536 272
3fbdd536
JG
273 clear_proceed_status ();
274
3fbdd536
JG
275 init_wait_for_inferior ();
276
01dfd415 277 terminal_initted = 0;
3fbdd536
JG
278
279 while (1)
280 {
281 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
282 wait_for_inferior ();
67ac9759 283 if (stop_signal != TARGET_SIGNAL_TRAP)
3fbdd536
JG
284 {
285 /* Let shell child handle its own signals in its own way */
286 /* FIXME, what if child has exit()ed? Must exit loop somehow */
287 resume (0, stop_signal);
288 }
289 else
290 {
291 /* We handle SIGTRAP, however; it means child did an exec. */
01dfd415
JK
292 if (!terminal_initted)
293 {
294 /* Now that the child has exec'd we know it has already set its
295 process group. On POSIX systems, tcsetpgrp will fail with
296 EPERM if we try it before the child's setpgid. */
297
298 /* Set up the "saved terminal modes" of the inferior
299 based on what modes we are starting it with. */
300 target_terminal_init ();
301
302 /* Install inferior's terminal modes. */
303 target_terminal_inferior ();
304
305 terminal_initted = 1;
306 }
3fbdd536
JG
307 if (0 == --pending_execs)
308 break;
67ac9759 309 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
3fbdd536
JG
310 }
311 }
312 stop_soon_quietly = 0;
3fbdd536 313}
This page took 0.103941 seconds and 4 git commands to generate.