* utils.c (query): Change syntax of query annotations to be
[deliverable/binutils-gdb.git] / gdb / fork-child.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
20
21 #include "defs.h"
22 #include <string.h>
23 #include "frame.h" /* required by inferior.h */
24 #include "inferior.h"
25 #include "target.h"
26 #include "wait.h"
27 #include "gdbcore.h"
28 #include "terminal.h"
29 #include "thread.h"
30
31 #include <signal.h>
32
33 #ifdef SET_STACK_LIMIT_HUGE
34 #include <sys/time.h>
35 #include <sys/resource.h>
36
37 extern int original_stack_limit;
38 #endif /* SET_STACK_LIMIT_HUGE */
39
40 extern char **environ;
41
42 #ifndef SHELL_FILE
43 #define SHELL_FILE "/bin/sh"
44 #endif
45
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(). */
51
52 void
53 fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
54 shell_file)
55 char *exec_file;
56 char *allargs;
57 char **env;
58 void (*traceme_fun) PARAMS ((void));
59 void (*init_trace_fun) PARAMS ((int));
60 char *shell_file;
61 {
62 int pid;
63 char *shell_command;
64 static char default_shell_file[] = SHELL_FILE;
65 int len;
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;
71 char **save_our_env;
72
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. */
75 if (exec_file == 0)
76 exec_file = get_exec_file(1);
77
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
83 shell_file too). */
84 if (shell_file == NULL)
85 shell_file = getenv ("SHELL");
86 if (shell_file == NULL)
87 shell_file = default_shell_file;
88
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);
98 #else
99 shell_command = (char *) alloca (len);
100 shell_command[0] = '\0';
101 #endif
102 strcat (shell_command, "exec ");
103
104 /* Now add exec_file, quoting as necessary. */
105 {
106 char *p;
107 int need_to_quote;
108
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
111 to. */
112 p = exec_file;
113 while (1)
114 {
115 switch (*p)
116 {
117 case '\'':
118 case '"':
119 case '(':
120 case ')':
121 case '$':
122 case '&':
123 case ';':
124 case '<':
125 case '>':
126 case ' ':
127 case '\n':
128 case '\t':
129 need_to_quote = 1;
130 goto end_scan;
131
132 case '\0':
133 need_to_quote = 0;
134 goto end_scan;
135
136 default:
137 break;
138 }
139 ++p;
140 }
141 end_scan:
142 if (need_to_quote)
143 {
144 strcat (shell_command, "'");
145 for (p = exec_file; *p != '\0'; ++p)
146 {
147 if (*p == '\'')
148 strcat (shell_command, "'\\''");
149 else
150 strncat (shell_command, p, 1);
151 }
152 strcat (shell_command, "'");
153 }
154 else
155 strcat (shell_command, exec_file);
156 }
157
158 strcat (shell_command, " ");
159 strcat (shell_command, allargs);
160
161 /* exec is said to fail if the executable is open. */
162 close_exec_file ();
163
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
166 restore it. */
167 save_our_env = environ;
168
169 /* Tell the terminal handling subsystem what tty we plan to run on;
170 it will just record the information for later. */
171
172 new_tty_prefork (inferior_io_terminal);
173
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. */
177
178 gdb_flush (gdb_stdout);
179 gdb_flush (gdb_stderr);
180
181 #if defined(USG) && !defined(HAVE_VFORK)
182 pid = fork ();
183 #else
184 if (debug_fork)
185 pid = fork ();
186 else
187 pid = vfork ();
188 #endif
189
190 if (pid < 0)
191 perror_with_name ("vfork");
192
193 if (pid == 0)
194 {
195 if (debug_fork)
196 sleep (debug_fork);
197
198 /* Run inferior in a separate process group. */
199 debug_setpgrp = gdb_setpgid ();
200 if (debug_setpgrp == -1)
201 perror("setpgrp failed in child");
202
203 #ifdef SET_STACK_LIMIT_HUGE
204 /* Reset the stack limit back to what it was. */
205 {
206 struct rlimit rlim;
207
208 getrlimit (RLIMIT_STACK, &rlim);
209 rlim.rlim_cur = original_stack_limit;
210 setrlimit (RLIMIT_STACK, &rlim);
211 }
212 #endif /* SET_STACK_LIMIT_HUGE */
213
214 /* Ask the tty subsystem to switch to the one we specified earlier
215 (or to share the current terminal, if none was specified). */
216
217 new_tty ();
218
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
223 for the inferior. */
224
225 /* "Trace me, Dr. Memory!" */
226 (*traceme_fun) ();
227
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. */
233 environ = env;
234 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
235
236 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
237 safe_strerror (errno));
238 gdb_flush (gdb_stderr);
239 _exit (0177);
240 }
241
242 /* Restore our environment in case a vforked child clob'd it. */
243 environ = save_our_env;
244
245 init_thread_list();
246
247 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
248
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);
252
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
255 new program. */
256 #ifdef SOLIB_CREATE_INFERIOR_HOOK
257 SOLIB_CREATE_INFERIOR_HOOK (pid);
258 #endif
259 }
260
261 /* Accept NTRAPS traps from the inferior. */
262
263 void
264 startup_inferior (ntraps)
265 int ntraps;
266 {
267 int pending_execs = ntraps;
268 int terminal_initted;
269
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. */
273
274 clear_proceed_status ();
275
276 init_wait_for_inferior ();
277
278 terminal_initted = 0;
279
280 while (1)
281 {
282 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
283 wait_for_inferior ();
284 if (stop_signal != TARGET_SIGNAL_TRAP)
285 {
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);
289 }
290 else
291 {
292 /* We handle SIGTRAP, however; it means child did an exec. */
293 if (!terminal_initted)
294 {
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. */
298
299 /* Set up the "saved terminal modes" of the inferior
300 based on what modes we are starting it with. */
301 target_terminal_init ();
302
303 /* Install inferior's terminal modes. */
304 target_terminal_inferior ();
305
306 terminal_initted = 1;
307 }
308 if (0 == --pending_execs)
309 break;
310 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
311 }
312 }
313 stop_soon_quietly = 0;
314 }
This page took 0.035326 seconds and 4 git commands to generate.