]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fork-child.c
2003-04-08 Elena Zannoni <ezannoni@redhat.com>
[thirdparty/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, 1995, 1996, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
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"
28 #include "gdb_wait.h"
29 #include "gdb_vfork.h"
30 #include "gdbcore.h"
31 #include "terminal.h"
32 #include "gdbthread.h"
33 #include "command.h" /* for dont_repeat () */
34
35 #include <signal.h>
36
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
42 extern 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 */
50 static void
51 breakup_args (char *scratch, char **argv)
52 {
53 char *cp = scratch;
54
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
91
92 /* Start an inferior Unix child process and sets inferior_ptid to its pid.
93 EXEC_FILE is the file to run.
94 ALLARGS is a string containing the arguments to the program.
95 ENV is the environment vector to pass. SHELL_FILE is the shell file,
96 or NULL if we should pick one. Errors reported with error(). */
97
98 /* This function is NOT-REENTRANT. Some of the variables have been
99 made static to ensure that they survive the vfork() call. */
100
101 void
102 fork_inferior (char *exec_file_arg, char *allargs, char **env,
103 void (*traceme_fun) (void), void (*init_trace_fun) (int),
104 void (*pre_trace_fun) (void), char *shell_file_arg)
105 {
106 int pid;
107 char *shell_command;
108 static char default_shell_file[] = SHELL_FILE;
109 int len;
110 /* Set debug_fork then attach to the child while it sleeps, to debug. */
111 static int debug_fork = 0;
112 /* This is set to the result of setpgrp, which if vforked, will be visible
113 to you in the parent process. It's only used by humans for debugging. */
114 static int debug_setpgrp = 657473;
115 static char *shell_file;
116 static char *exec_file;
117 char **save_our_env;
118 int shell = 0;
119 static char **argv;
120
121 /* If no exec file handed to us, get it from the exec-file command -- with
122 a good, common error message if none is specified. */
123 exec_file = exec_file_arg;
124 if (exec_file == 0)
125 exec_file = get_exec_file (1);
126
127 /* STARTUP_WITH_SHELL is defined in inferior.h.
128 * If 0, we'll just do a fork/exec, no shell, so don't
129 * bother figuring out what shell.
130 */
131 shell_file = shell_file_arg;
132 if (STARTUP_WITH_SHELL)
133 {
134 /* Figure out what shell to start up the user program under. */
135 if (shell_file == NULL)
136 shell_file = getenv ("SHELL");
137 if (shell_file == NULL)
138 shell_file = default_shell_file;
139 shell = 1;
140 }
141
142 /* Multiplying the length of exec_file by 4 is to account for the fact
143 that it may expand when quoted; it is a worst-case number based on
144 every character being '. */
145 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
146 /* If desired, concat something onto the front of ALLARGS.
147 SHELL_COMMAND is the result. */
148 #ifdef SHELL_COMMAND_CONCAT
149 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
150 strcpy (shell_command, SHELL_COMMAND_CONCAT);
151 #else
152 shell_command = (char *) alloca (len);
153 shell_command[0] = '\0';
154 #endif
155
156 if (!shell)
157 {
158 /* We're going to call execvp. Create argv */
159 /* Largest case: every other character is a separate arg */
160 argv = (char **) xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) * sizeof (*argv));
161 argv[0] = exec_file;
162 breakup_args (allargs, &argv[1]);
163
164 }
165 else
166 {
167
168 /* We're going to call a shell */
169
170 /* Now add exec_file, quoting as necessary. */
171
172 char *p;
173 int need_to_quote;
174
175 strcat (shell_command, "exec ");
176
177 /* Quoting in this style is said to work with all shells. But csh
178 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
179 to. */
180 p = exec_file;
181 while (1)
182 {
183 switch (*p)
184 {
185 case '\'':
186 case '!':
187 case '"':
188 case '(':
189 case ')':
190 case '$':
191 case '&':
192 case ';':
193 case '<':
194 case '>':
195 case ' ':
196 case '\n':
197 case '\t':
198 need_to_quote = 1;
199 goto end_scan;
200
201 case '\0':
202 need_to_quote = 0;
203 goto end_scan;
204
205 default:
206 break;
207 }
208 ++p;
209 }
210 end_scan:
211 if (need_to_quote)
212 {
213 strcat (shell_command, "'");
214 for (p = exec_file; *p != '\0'; ++p)
215 {
216 if (*p == '\'')
217 strcat (shell_command, "'\\''");
218 else if (*p == '!')
219 strcat (shell_command, "\\!");
220 else
221 strncat (shell_command, p, 1);
222 }
223 strcat (shell_command, "'");
224 }
225 else
226 strcat (shell_command, exec_file);
227
228 strcat (shell_command, " ");
229 strcat (shell_command, allargs);
230
231 }
232
233 /* exec is said to fail if the executable is open. */
234 close_exec_file ();
235
236 /* Retain a copy of our environment variables, since the child will
237 replace the value of environ and if we're vforked, we have to
238 restore it. */
239 save_our_env = environ;
240
241 /* Tell the terminal handling subsystem what tty we plan to run on;
242 it will just record the information for later. */
243
244 new_tty_prefork (inferior_io_terminal);
245
246 /* It is generally good practice to flush any possible pending stdio
247 output prior to doing a fork, to avoid the possibility of both the
248 parent and child flushing the same data after the fork. */
249
250 gdb_flush (gdb_stdout);
251 gdb_flush (gdb_stderr);
252
253 /* If there's any initialization of the target layers that must happen
254 to prepare to handle the child we're about fork, do it now...
255 */
256 if (pre_trace_fun != NULL)
257 (*pre_trace_fun) ();
258
259 /* Create the child process. Note that the apparent call to vfork()
260 below *might* actually be a call to fork() due to the fact that
261 autoconf will ``#define vfork fork'' on certain platforms. */
262 if (debug_fork)
263 pid = fork ();
264 else
265 pid = vfork ();
266
267 if (pid < 0)
268 perror_with_name ("vfork");
269
270 if (pid == 0)
271 {
272 if (debug_fork)
273 sleep (debug_fork);
274
275 /* Run inferior in a separate process group. */
276 debug_setpgrp = gdb_setpgid ();
277 if (debug_setpgrp == -1)
278 perror ("setpgrp failed in child");
279
280 /* Ask the tty subsystem to switch to the one we specified earlier
281 (or to share the current terminal, if none was specified). */
282
283 new_tty ();
284
285 /* Changing the signal handlers for the inferior after
286 a vfork can also change them for the superior, so we don't mess
287 with signals here. See comments in
288 initialize_signals for how we get the right signal handlers
289 for the inferior. */
290
291 /* "Trace me, Dr. Memory!" */
292 (*traceme_fun) ();
293 /* The call above set this process (the "child") as debuggable
294 * by the original gdb process (the "parent"). Since processes
295 * (unlike people) can have only one parent, if you are
296 * debugging gdb itself (and your debugger is thus _already_ the
297 * controller/parent for this child), code from here on out
298 * is undebuggable. Indeed, you probably got an error message
299 * saying "not parent". Sorry--you'll have to use print statements!
300 */
301
302 /* There is no execlpe call, so we have to set the environment
303 for our child in the global variable. If we've vforked, this
304 clobbers the parent, but environ is restored a few lines down
305 in the parent. By the way, yes we do need to look down the
306 path to find $SHELL. Rich Pixley says so, and I agree. */
307 environ = env;
308
309 /* If we decided above to start up with a shell,
310 * we exec the shell,
311 * "-c" says to interpret the next arg as a shell command
312 * to execute, and this command is "exec <target-program> <args>".
313 * "-f" means "fast startup" to the c-shell, which means
314 * don't do .cshrc file. Doing .cshrc may cause fork/exec
315 * events which will confuse debugger start-up code.
316 */
317 if (shell)
318 {
319 execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
320
321 /* If we get here, it's an error */
322 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
323 safe_strerror (errno));
324 gdb_flush (gdb_stderr);
325 _exit (0177);
326 }
327 else
328 {
329 /* Otherwise, we directly exec the target program with execvp. */
330 int i;
331 char *errstring;
332
333 execvp (exec_file, argv);
334
335 /* If we get here, it's an error */
336 errstring = safe_strerror (errno);
337 fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
338
339 i = 1;
340 while (argv[i] != NULL)
341 {
342 if (i != 1)
343 fprintf_unfiltered (gdb_stderr, " ");
344 fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
345 i++;
346 }
347 fprintf_unfiltered (gdb_stderr, ".\n");
348 /* This extra info seems to be useless
349 fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
350 */
351 gdb_flush (gdb_stderr);
352 _exit (0177);
353 }
354 }
355
356 /* Restore our environment in case a vforked child clob'd it. */
357 environ = save_our_env;
358
359 init_thread_list ();
360
361 inferior_ptid = pid_to_ptid (pid); /* Needed for wait_for_inferior stuff below */
362
363 /* Now that we have a child process, make it our target, and
364 initialize anything target-vector-specific that needs initializing. */
365
366 (*init_trace_fun) (pid);
367
368 /* We are now in the child process of interest, having exec'd the
369 correct program, and are poised at the first instruction of the
370 new program. */
371
372 /* Allow target dependent code to play with the new process. This might be
373 used to have target-specific code initialize a variable in the new process
374 prior to executing the first instruction. */
375 TARGET_CREATE_INFERIOR_HOOK (pid);
376
377 #ifdef SOLIB_CREATE_INFERIOR_HOOK
378 SOLIB_CREATE_INFERIOR_HOOK (pid);
379 #endif
380 }
381
382 /* Accept NTRAPS traps from the inferior. */
383
384 void
385 startup_inferior (int ntraps)
386 {
387 int pending_execs = ntraps;
388 int terminal_initted;
389
390 /* The process was started by the fork that created it,
391 but it will have stopped one instruction after execing the shell.
392 Here we must get it up to actual execution of the real program. */
393
394 clear_proceed_status ();
395
396 init_wait_for_inferior ();
397
398 terminal_initted = 0;
399
400 if (STARTUP_WITH_SHELL)
401 inferior_ignoring_startup_exec_events = ntraps;
402 else
403 inferior_ignoring_startup_exec_events = 0;
404 inferior_ignoring_leading_exec_events =
405 target_reported_exec_events_per_exec_call () - 1;
406
407 #ifdef STARTUP_INFERIOR
408 STARTUP_INFERIOR (pending_execs);
409 #else
410 while (1)
411 {
412 /* Make wait_for_inferior be quiet */
413 stop_soon = STOP_QUIETLY;
414 wait_for_inferior ();
415 if (stop_signal != TARGET_SIGNAL_TRAP)
416 {
417 /* Let shell child handle its own signals in its own way */
418 /* FIXME, what if child has exit()ed? Must exit loop somehow */
419 resume (0, stop_signal);
420 }
421 else
422 {
423 /* We handle SIGTRAP, however; it means child did an exec. */
424 if (!terminal_initted)
425 {
426 /* Now that the child has exec'd we know it has already set its
427 process group. On POSIX systems, tcsetpgrp will fail with
428 EPERM if we try it before the child's setpgid. */
429
430 /* Set up the "saved terminal modes" of the inferior
431 based on what modes we are starting it with. */
432 target_terminal_init ();
433
434 /* Install inferior's terminal modes. */
435 target_terminal_inferior ();
436
437 terminal_initted = 1;
438 }
439
440 pending_execs = pending_execs - 1;
441 if (0 == pending_execs)
442 break;
443
444 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
445 }
446 }
447 #endif /* STARTUP_INFERIOR */
448 stop_soon = NO_STOP_QUIETLY;
449 }