fix scrambled changelog
[deliverable/binutils-gdb.git] / gdb / fork-child.c
CommitLineData
c906108c 1/* Fork a Unix child process, and set up to debug it, for GDB.
b6ba6518
KB
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
c906108c
SS
4 Contributed by Cygnus Support.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "gdb_string.h"
25#include "frame.h" /* required by inferior.h */
26#include "inferior.h"
27#include "target.h"
03f2053f 28#include "gdb_wait.h"
74c1b268 29#include "gdb_vfork.h"
c906108c
SS
30#include "gdbcore.h"
31#include "terminal.h"
32#include "gdbthread.h"
c2d11a7d 33#include "command.h" /* for dont_repeat () */
c906108c
SS
34
35#include <signal.h>
c906108c 36
c906108c
SS
37/* This just gets used as a default if we can't find SHELL */
38#ifndef SHELL_FILE
39#define SHELL_FILE "/bin/sh"
40#endif
41
42extern char **environ;
43
44/* This function breaks up an argument string into an argument
45 * vector suitable for passing to execvp().
46 * E.g., on "run a b c d" this routine would get as input
47 * the string "a b c d", and as output it would fill in argv with
48 * the four arguments "a", "b", "c", "d".
49 */
50static void
fba45db2 51breakup_args (char *scratch, char **argv)
c906108c
SS
52{
53 char *cp = scratch;
54
c906108c
SS
55 for (;;)
56 {
57
58 /* Scan past leading separators */
59 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
60 {
61 cp++;
62 }
63
64 /* Break if at end of string */
65 if (*cp == '\0')
66 break;
67
68 /* Take an arg */
69 *argv++ = cp;
70
71 /* Scan for next arg separator */
72 cp = strchr (cp, ' ');
73 if (cp == NULL)
74 cp = strchr (cp, '\t');
75 if (cp == NULL)
76 cp = strchr (cp, '\n');
77
78 /* No separators => end of string => break */
79 if (cp == NULL)
80 break;
81
82 /* Replace the separator with a terminator */
83 *cp++ = '\0';
84 }
85
86 /* execv requires a null-terminated arg vector */
87 *argv = NULL;
88
89}
90
6037b830
JB
91/* When executing a command under the given shell, return non-zero
92 if the '!' character should be escaped when embedded in a quoted
93 command-line argument. */
94
95static int
96escape_bang_in_quoted_argument (const char *shell_file)
97{
98 const int shell_file_len = strlen (shell_file);
99
100 /* Bang should be escaped only in C Shells. For now, simply check
101 that the shell name ends with 'csh', which covers at least csh
102 and tcsh. This should be good enough for now. */
103
104 if (shell_file_len < 3)
105 return 0;
106
107 if (shell_file[shell_file_len - 3] == 'c'
108 && shell_file[shell_file_len - 2] == 's'
109 && shell_file[shell_file_len - 1] == 'h')
110 return 1;
111
112 return 0;
113}
c906108c 114
39f77062 115/* Start an inferior Unix child process and sets inferior_ptid to its pid.
c906108c
SS
116 EXEC_FILE is the file to run.
117 ALLARGS is a string containing the arguments to the program.
118 ENV is the environment vector to pass. SHELL_FILE is the shell file,
119 or NULL if we should pick one. Errors reported with error(). */
120
c65ecaf3
AC
121/* This function is NOT-REENTRANT. Some of the variables have been
122 made static to ensure that they survive the vfork() call. */
123
c906108c 124void
c65ecaf3 125fork_inferior (char *exec_file_arg, char *allargs, char **env,
073063d7 126 void (*traceme_fun) (void), void (*init_trace_fun) (int),
c65ecaf3 127 void (*pre_trace_fun) (void), char *shell_file_arg)
c906108c
SS
128{
129 int pid;
130 char *shell_command;
131 static char default_shell_file[] = SHELL_FILE;
132 int len;
133 /* Set debug_fork then attach to the child while it sleeps, to debug. */
134 static int debug_fork = 0;
135 /* This is set to the result of setpgrp, which if vforked, will be visible
136 to you in the parent process. It's only used by humans for debugging. */
137 static int debug_setpgrp = 657473;
c65ecaf3
AC
138 static char *shell_file;
139 static char *exec_file;
c906108c
SS
140 char **save_our_env;
141 int shell = 0;
c65ecaf3 142 static char **argv;
c906108c
SS
143
144 /* If no exec file handed to us, get it from the exec-file command -- with
145 a good, common error message if none is specified. */
c65ecaf3 146 exec_file = exec_file_arg;
c906108c
SS
147 if (exec_file == 0)
148 exec_file = get_exec_file (1);
149
150 /* STARTUP_WITH_SHELL is defined in inferior.h.
151 * If 0, we'll just do a fork/exec, no shell, so don't
152 * bother figuring out what shell.
153 */
c65ecaf3 154 shell_file = shell_file_arg;
c906108c
SS
155 if (STARTUP_WITH_SHELL)
156 {
157 /* Figure out what shell to start up the user program under. */
158 if (shell_file == NULL)
159 shell_file = getenv ("SHELL");
160 if (shell_file == NULL)
161 shell_file = default_shell_file;
162 shell = 1;
163 }
164
c906108c
SS
165 /* Multiplying the length of exec_file by 4 is to account for the fact
166 that it may expand when quoted; it is a worst-case number based on
167 every character being '. */
c5aa993b 168 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
c906108c
SS
169 /* If desired, concat something onto the front of ALLARGS.
170 SHELL_COMMAND is the result. */
171#ifdef SHELL_COMMAND_CONCAT
172 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
173 strcpy (shell_command, SHELL_COMMAND_CONCAT);
174#else
175 shell_command = (char *) alloca (len);
176 shell_command[0] = '\0';
177#endif
178
179 if (!shell)
180 {
181 /* We're going to call execvp. Create argv */
182 /* Largest case: every other character is a separate arg */
c906108c
SS
183 argv = (char **) xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) * sizeof (*argv));
184 argv[0] = exec_file;
185 breakup_args (allargs, &argv[1]);
186
187 }
188 else
189 {
190
191 /* We're going to call a shell */
192
193 /* Now add exec_file, quoting as necessary. */
194
195 char *p;
196 int need_to_quote;
6037b830 197 const int escape_bang = escape_bang_in_quoted_argument (shell_file);
c906108c
SS
198
199 strcat (shell_command, "exec ");
200
201 /* Quoting in this style is said to work with all shells. But csh
c5aa993b
JM
202 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
203 to. */
c906108c
SS
204 p = exec_file;
205 while (1)
206 {
207 switch (*p)
208 {
209 case '\'':
d8849953 210 case '!':
c906108c
SS
211 case '"':
212 case '(':
213 case ')':
214 case '$':
215 case '&':
216 case ';':
217 case '<':
218 case '>':
219 case ' ':
220 case '\n':
221 case '\t':
222 need_to_quote = 1;
223 goto end_scan;
224
225 case '\0':
226 need_to_quote = 0;
227 goto end_scan;
228
229 default:
230 break;
231 }
232 ++p;
233 }
234 end_scan:
235 if (need_to_quote)
236 {
237 strcat (shell_command, "'");
238 for (p = exec_file; *p != '\0'; ++p)
239 {
240 if (*p == '\'')
241 strcat (shell_command, "'\\''");
6037b830 242 else if (*p == '!' && escape_bang)
d8849953 243 strcat (shell_command, "\\!");
c906108c
SS
244 else
245 strncat (shell_command, p, 1);
246 }
247 strcat (shell_command, "'");
248 }
249 else
250 strcat (shell_command, exec_file);
251
252 strcat (shell_command, " ");
253 strcat (shell_command, allargs);
254
255 }
256
257 /* exec is said to fail if the executable is open. */
258 close_exec_file ();
259
260 /* Retain a copy of our environment variables, since the child will
261 replace the value of environ and if we're vforked, we have to
262 restore it. */
263 save_our_env = environ;
264
265 /* Tell the terminal handling subsystem what tty we plan to run on;
266 it will just record the information for later. */
267
268 new_tty_prefork (inferior_io_terminal);
269
270 /* It is generally good practice to flush any possible pending stdio
271 output prior to doing a fork, to avoid the possibility of both the
272 parent and child flushing the same data after the fork. */
273
274 gdb_flush (gdb_stdout);
275 gdb_flush (gdb_stderr);
276
277 /* If there's any initialization of the target layers that must happen
c5aa993b 278 to prepare to handle the child we're about fork, do it now...
c906108c
SS
279 */
280 if (pre_trace_fun != NULL)
281 (*pre_trace_fun) ();
282
7700434b
KB
283 /* Create the child process. Note that the apparent call to vfork()
284 below *might* actually be a call to fork() due to the fact that
285 autoconf will ``#define vfork fork'' on certain platforms. */
c906108c
SS
286 if (debug_fork)
287 pid = fork ();
288 else
289 pid = vfork ();
c906108c
SS
290
291 if (pid < 0)
292 perror_with_name ("vfork");
293
294 if (pid == 0)
295 {
296 if (debug_fork)
297 sleep (debug_fork);
298
299 /* Run inferior in a separate process group. */
300 debug_setpgrp = gdb_setpgid ();
301 if (debug_setpgrp == -1)
302 perror ("setpgrp failed in child");
303
304 /* Ask the tty subsystem to switch to the one we specified earlier
c5aa993b 305 (or to share the current terminal, if none was specified). */
c906108c
SS
306
307 new_tty ();
308
309 /* Changing the signal handlers for the inferior after
c5aa993b
JM
310 a vfork can also change them for the superior, so we don't mess
311 with signals here. See comments in
312 initialize_signals for how we get the right signal handlers
313 for the inferior. */
c906108c
SS
314
315 /* "Trace me, Dr. Memory!" */
316 (*traceme_fun) ();
317 /* The call above set this process (the "child") as debuggable
318 * by the original gdb process (the "parent"). Since processes
319 * (unlike people) can have only one parent, if you are
320 * debugging gdb itself (and your debugger is thus _already_ the
321 * controller/parent for this child), code from here on out
322 * is undebuggable. Indeed, you probably got an error message
323 * saying "not parent". Sorry--you'll have to use print statements!
324 */
325
326 /* There is no execlpe call, so we have to set the environment
c5aa993b
JM
327 for our child in the global variable. If we've vforked, this
328 clobbers the parent, but environ is restored a few lines down
329 in the parent. By the way, yes we do need to look down the
330 path to find $SHELL. Rich Pixley says so, and I agree. */
c906108c
SS
331 environ = env;
332
333 /* If we decided above to start up with a shell,
334 * we exec the shell,
335 * "-c" says to interpret the next arg as a shell command
336 * to execute, and this command is "exec <target-program> <args>".
337 * "-f" means "fast startup" to the c-shell, which means
338 * don't do .cshrc file. Doing .cshrc may cause fork/exec
339 * events which will confuse debugger start-up code.
340 */
341 if (shell)
342 {
c906108c 343 execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
c906108c
SS
344
345 /* If we get here, it's an error */
346 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
347 safe_strerror (errno));
348 gdb_flush (gdb_stderr);
349 _exit (0177);
350 }
351 else
352 {
353 /* Otherwise, we directly exec the target program with execvp. */
354 int i;
355 char *errstring;
cce74817 356
c906108c
SS
357 execvp (exec_file, argv);
358
359 /* If we get here, it's an error */
360 errstring = safe_strerror (errno);
361 fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
362
363 i = 1;
364 while (argv[i] != NULL)
365 {
366 if (i != 1)
367 fprintf_unfiltered (gdb_stderr, " ");
368 fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
369 i++;
370 }
371 fprintf_unfiltered (gdb_stderr, ".\n");
372 /* This extra info seems to be useless
c5aa993b
JM
373 fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
374 */
c906108c
SS
375 gdb_flush (gdb_stderr);
376 _exit (0177);
377 }
378 }
379
380 /* Restore our environment in case a vforked child clob'd it. */
381 environ = save_our_env;
382
383 init_thread_list ();
384
39f77062 385 inferior_ptid = pid_to_ptid (pid); /* Needed for wait_for_inferior stuff below */
c906108c
SS
386
387 /* Now that we have a child process, make it our target, and
388 initialize anything target-vector-specific that needs initializing. */
389
390 (*init_trace_fun) (pid);
391
392 /* We are now in the child process of interest, having exec'd the
393 correct program, and are poised at the first instruction of the
394 new program. */
395
b7d6b182 396 /* Allow target dependent code to play with the new process. This might be
c906108c
SS
397 used to have target-specific code initialize a variable in the new process
398 prior to executing the first instruction. */
399 TARGET_CREATE_INFERIOR_HOOK (pid);
400
401#ifdef SOLIB_CREATE_INFERIOR_HOOK
402 SOLIB_CREATE_INFERIOR_HOOK (pid);
403#endif
404}
405
c906108c
SS
406/* Accept NTRAPS traps from the inferior. */
407
408void
fba45db2 409startup_inferior (int ntraps)
c906108c
SS
410{
411 int pending_execs = ntraps;
412 int terminal_initted;
413
414 /* The process was started by the fork that created it,
415 but it will have stopped one instruction after execing the shell.
416 Here we must get it up to actual execution of the real program. */
417
418 clear_proceed_status ();
419
420 init_wait_for_inferior ();
421
422 terminal_initted = 0;
423
424 if (STARTUP_WITH_SHELL)
425 inferior_ignoring_startup_exec_events = ntraps;
426 else
427 inferior_ignoring_startup_exec_events = 0;
428 inferior_ignoring_leading_exec_events =
429 target_reported_exec_events_per_exec_call () - 1;
430
c906108c
SS
431 while (1)
432 {
c54cfec8 433 /* Make wait_for_inferior be quiet */
c0236d92 434 stop_soon = STOP_QUIETLY;
c906108c
SS
435 wait_for_inferior ();
436 if (stop_signal != TARGET_SIGNAL_TRAP)
437 {
438 /* Let shell child handle its own signals in its own way */
439 /* FIXME, what if child has exit()ed? Must exit loop somehow */
440 resume (0, stop_signal);
441 }
442 else
443 {
444 /* We handle SIGTRAP, however; it means child did an exec. */
445 if (!terminal_initted)
446 {
447 /* Now that the child has exec'd we know it has already set its
c5aa993b
JM
448 process group. On POSIX systems, tcsetpgrp will fail with
449 EPERM if we try it before the child's setpgid. */
c906108c
SS
450
451 /* Set up the "saved terminal modes" of the inferior
c5aa993b 452 based on what modes we are starting it with. */
c906108c
SS
453 target_terminal_init ();
454
455 /* Install inferior's terminal modes. */
456 target_terminal_inferior ();
457
458 terminal_initted = 1;
459 }
460
461 pending_execs = pending_execs - 1;
462 if (0 == pending_execs)
463 break;
464
465 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
466 }
467 }
c0236d92 468 stop_soon = NO_STOP_QUIETLY;
c906108c 469}
This page took 0.455071 seconds and 4 git commands to generate.