*** empty log message ***
[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
91
39f77062 92/* Start an inferior Unix child process and sets inferior_ptid to its pid.
c906108c
SS
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
c65ecaf3
AC
98/* This function is NOT-REENTRANT. Some of the variables have been
99 made static to ensure that they survive the vfork() call. */
100
c906108c 101void
c65ecaf3 102fork_inferior (char *exec_file_arg, char *allargs, char **env,
073063d7 103 void (*traceme_fun) (void), void (*init_trace_fun) (int),
c65ecaf3 104 void (*pre_trace_fun) (void), char *shell_file_arg)
c906108c
SS
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;
c65ecaf3
AC
115 static char *shell_file;
116 static char *exec_file;
c906108c
SS
117 char **save_our_env;
118 int shell = 0;
c65ecaf3 119 static char **argv;
c906108c
SS
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. */
c65ecaf3 123 exec_file = exec_file_arg;
c906108c
SS
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 */
c65ecaf3 131 shell_file = shell_file_arg;
c906108c
SS
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
c906108c
SS
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 '. */
c5aa993b 145 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop */ 12;
c906108c
SS
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 */
c906108c
SS
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
c5aa993b
JM
178 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
179 to. */
c906108c
SS
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 '\n':
196 case '\t':
197 need_to_quote = 1;
198 goto end_scan;
199
200 case '\0':
201 need_to_quote = 0;
202 goto end_scan;
203
204 default:
205 break;
206 }
207 ++p;
208 }
209 end_scan:
210 if (need_to_quote)
211 {
212 strcat (shell_command, "'");
213 for (p = exec_file; *p != '\0'; ++p)
214 {
215 if (*p == '\'')
216 strcat (shell_command, "'\\''");
217 else
218 strncat (shell_command, p, 1);
219 }
220 strcat (shell_command, "'");
221 }
222 else
223 strcat (shell_command, exec_file);
224
225 strcat (shell_command, " ");
226 strcat (shell_command, allargs);
227
228 }
229
230 /* exec is said to fail if the executable is open. */
231 close_exec_file ();
232
233 /* Retain a copy of our environment variables, since the child will
234 replace the value of environ and if we're vforked, we have to
235 restore it. */
236 save_our_env = environ;
237
238 /* Tell the terminal handling subsystem what tty we plan to run on;
239 it will just record the information for later. */
240
241 new_tty_prefork (inferior_io_terminal);
242
243 /* It is generally good practice to flush any possible pending stdio
244 output prior to doing a fork, to avoid the possibility of both the
245 parent and child flushing the same data after the fork. */
246
247 gdb_flush (gdb_stdout);
248 gdb_flush (gdb_stderr);
249
250 /* If there's any initialization of the target layers that must happen
c5aa993b 251 to prepare to handle the child we're about fork, do it now...
c906108c
SS
252 */
253 if (pre_trace_fun != NULL)
254 (*pre_trace_fun) ();
255
7700434b
KB
256 /* Create the child process. Note that the apparent call to vfork()
257 below *might* actually be a call to fork() due to the fact that
258 autoconf will ``#define vfork fork'' on certain platforms. */
c906108c
SS
259 if (debug_fork)
260 pid = fork ();
261 else
262 pid = vfork ();
c906108c
SS
263
264 if (pid < 0)
265 perror_with_name ("vfork");
266
267 if (pid == 0)
268 {
269 if (debug_fork)
270 sleep (debug_fork);
271
272 /* Run inferior in a separate process group. */
273 debug_setpgrp = gdb_setpgid ();
274 if (debug_setpgrp == -1)
275 perror ("setpgrp failed in child");
276
277 /* Ask the tty subsystem to switch to the one we specified earlier
c5aa993b 278 (or to share the current terminal, if none was specified). */
c906108c
SS
279
280 new_tty ();
281
282 /* Changing the signal handlers for the inferior after
c5aa993b
JM
283 a vfork can also change them for the superior, so we don't mess
284 with signals here. See comments in
285 initialize_signals for how we get the right signal handlers
286 for the inferior. */
c906108c
SS
287
288 /* "Trace me, Dr. Memory!" */
289 (*traceme_fun) ();
290 /* The call above set this process (the "child") as debuggable
291 * by the original gdb process (the "parent"). Since processes
292 * (unlike people) can have only one parent, if you are
293 * debugging gdb itself (and your debugger is thus _already_ the
294 * controller/parent for this child), code from here on out
295 * is undebuggable. Indeed, you probably got an error message
296 * saying "not parent". Sorry--you'll have to use print statements!
297 */
298
299 /* There is no execlpe call, so we have to set the environment
c5aa993b
JM
300 for our child in the global variable. If we've vforked, this
301 clobbers the parent, but environ is restored a few lines down
302 in the parent. By the way, yes we do need to look down the
303 path to find $SHELL. Rich Pixley says so, and I agree. */
c906108c
SS
304 environ = env;
305
306 /* If we decided above to start up with a shell,
307 * we exec the shell,
308 * "-c" says to interpret the next arg as a shell command
309 * to execute, and this command is "exec <target-program> <args>".
310 * "-f" means "fast startup" to the c-shell, which means
311 * don't do .cshrc file. Doing .cshrc may cause fork/exec
312 * events which will confuse debugger start-up code.
313 */
314 if (shell)
315 {
c906108c 316 execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
c906108c
SS
317
318 /* If we get here, it's an error */
319 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
320 safe_strerror (errno));
321 gdb_flush (gdb_stderr);
322 _exit (0177);
323 }
324 else
325 {
326 /* Otherwise, we directly exec the target program with execvp. */
327 int i;
328 char *errstring;
cce74817 329
c906108c
SS
330 execvp (exec_file, argv);
331
332 /* If we get here, it's an error */
333 errstring = safe_strerror (errno);
334 fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
335
336 i = 1;
337 while (argv[i] != NULL)
338 {
339 if (i != 1)
340 fprintf_unfiltered (gdb_stderr, " ");
341 fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
342 i++;
343 }
344 fprintf_unfiltered (gdb_stderr, ".\n");
345 /* This extra info seems to be useless
c5aa993b
JM
346 fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
347 */
c906108c
SS
348 gdb_flush (gdb_stderr);
349 _exit (0177);
350 }
351 }
352
353 /* Restore our environment in case a vforked child clob'd it. */
354 environ = save_our_env;
355
356 init_thread_list ();
357
39f77062 358 inferior_ptid = pid_to_ptid (pid); /* Needed for wait_for_inferior stuff below */
c906108c
SS
359
360 /* Now that we have a child process, make it our target, and
361 initialize anything target-vector-specific that needs initializing. */
362
363 (*init_trace_fun) (pid);
364
365 /* We are now in the child process of interest, having exec'd the
366 correct program, and are poised at the first instruction of the
367 new program. */
368
b7d6b182 369 /* Allow target dependent code to play with the new process. This might be
c906108c
SS
370 used to have target-specific code initialize a variable in the new process
371 prior to executing the first instruction. */
372 TARGET_CREATE_INFERIOR_HOOK (pid);
373
374#ifdef SOLIB_CREATE_INFERIOR_HOOK
375 SOLIB_CREATE_INFERIOR_HOOK (pid);
376#endif
377}
378
379/* An inferior Unix process CHILD_PID has been created by a call to
380 fork() (or variants like vfork). It is presently stopped, and waiting
381 to be resumed. clone_and_follow_inferior will fork the debugger,
382 and that clone will "follow" (attach to) CHILD_PID. The original copy
383 of the debugger will not touch CHILD_PID again.
384
385 Also, the original debugger will set FOLLOWED_CHILD FALSE, while the
386 clone will set it TRUE.
c5aa993b 387 */
c906108c 388void
fba45db2 389clone_and_follow_inferior (int child_pid, int *followed_child)
c906108c 390{
c906108c
SS
391 int debugger_pid;
392 int status;
393 char pid_spelling[100]; /* Arbitrary but sufficient length. */
394
395 /* This semaphore is used to coordinate the two debuggers' handoff
396 of CHILD_PID. The original debugger will detach from CHILD_PID,
397 and then the clone debugger will attach to it. (It must be done
398 this way because on some targets, only one process at a time can
399 trace another. Thus, the original debugger must relinquish its
400 tracing rights before the clone can pick them up.)
c5aa993b 401 */
c906108c
SS
402#define SEM_TALK (1)
403#define SEM_LISTEN (0)
404 int handoff_semaphore[2]; /* Original "talks" to [1], clone "listens" to [0] */
405 int talk_value = 99;
406 int listen_value;
407
408 /* Set debug_fork then attach to the child while it sleeps, to debug. */
409 static int debug_fork = 0;
410
411 /* It is generally good practice to flush any possible pending stdio
412 output prior to doing a fork, to avoid the possibility of both the
413 parent and child flushing the same data after the fork. */
414
415 gdb_flush (gdb_stdout);
416 gdb_flush (gdb_stderr);
417
418 /* Open the semaphore pipes.
c5aa993b 419 */
c906108c
SS
420 status = pipe (handoff_semaphore);
421 if (status < 0)
422 error ("error getting pipe for handoff semaphore");
423
7700434b
KB
424 /* Clone the debugger. Note that the apparent call to vfork()
425 below *might* actually be a call to fork() due to the fact that
426 autoconf will ``#define vfork fork'' on certain platforms. */
c906108c
SS
427 if (debug_fork)
428 debugger_pid = fork ();
429 else
430 debugger_pid = vfork ();
c906108c
SS
431
432 if (debugger_pid < 0)
433 perror_with_name ("fork");
434
435 /* Are we the original debugger? If so, we must relinquish all claims
c5aa993b 436 to CHILD_PID. */
c906108c
SS
437 if (debugger_pid != 0)
438 {
c5aa993b 439 char signal_spelling[100]; /* Arbitrary but sufficient length */
c906108c
SS
440
441 /* Detach from CHILD_PID. Deliver a "stop" signal when we do, though,
c5aa993b
JM
442 so that it remains stopped until the clone debugger can attach
443 to it.
c906108c
SS
444 */
445 detach_breakpoints (child_pid);
446
447 sprintf (signal_spelling, "%d", target_signal_to_host (TARGET_SIGNAL_STOP));
448 target_require_detach (child_pid, signal_spelling, 1);
449
450 /* Notify the clone debugger that it should attach to CHILD_PID. */
451 write (handoff_semaphore[SEM_TALK], &talk_value, sizeof (talk_value));
452
453 *followed_child = 0;
454 }
455
456 /* We're the child. */
457 else
458 {
459 if (debug_fork)
460 sleep (debug_fork);
461
462 /* The child (i.e., the cloned debugger) must now attach to
39f77062 463 CHILD_PID. inferior_ptid is presently set to the parent process
c906108c
SS
464 of the fork, while CHILD_PID should be the child process of the
465 fork.
466
467 Wait until the original debugger relinquishes control of CHILD_PID,
468 though.
c5aa993b 469 */
c906108c
SS
470 read (handoff_semaphore[SEM_LISTEN], &listen_value, sizeof (listen_value));
471
39f77062 472 /* Note that we DON'T want to actually detach from inferior_ptid,
c906108c
SS
473 because that would allow it to run free. The original
474 debugger wants to retain control of the process. So, we
39f77062 475 just reset inferior_ptid to CHILD_PID, and then ensure that all
c906108c 476 breakpoints are really set in CHILD_PID.
c5aa993b 477 */
c906108c
SS
478 target_mourn_inferior ();
479
480 /* Ask the tty subsystem to switch to the one we specified earlier
481 (or to share the current terminal, if none was specified). */
482
483 new_tty ();
484
485 dont_repeat ();
486 sprintf (pid_spelling, "%d", child_pid);
487 target_require_attach (pid_spelling, 1);
488
489 /* Perform any necessary cleanup, after attachment. (This form
490 of attaching can behave differently on some targets than the
491 standard method, where a process formerly not under debugger
492 control was suddenly attached to..)
c5aa993b 493 */
c906108c
SS
494 target_post_follow_inferior_by_clone ();
495
496 *followed_child = 1;
497 }
498
499 /* Discard the handoff sempahore. */
500 (void) close (handoff_semaphore[SEM_LISTEN]);
501 (void) close (handoff_semaphore[SEM_TALK]);
502}
503
504/* Accept NTRAPS traps from the inferior. */
505
506void
fba45db2 507startup_inferior (int ntraps)
c906108c
SS
508{
509 int pending_execs = ntraps;
510 int terminal_initted;
511
512 /* The process was started by the fork that created it,
513 but it will have stopped one instruction after execing the shell.
514 Here we must get it up to actual execution of the real program. */
515
516 clear_proceed_status ();
517
518 init_wait_for_inferior ();
519
520 terminal_initted = 0;
521
522 if (STARTUP_WITH_SHELL)
523 inferior_ignoring_startup_exec_events = ntraps;
524 else
525 inferior_ignoring_startup_exec_events = 0;
526 inferior_ignoring_leading_exec_events =
527 target_reported_exec_events_per_exec_call () - 1;
528
529#ifdef STARTUP_INFERIOR
530 STARTUP_INFERIOR (pending_execs);
531#else
532 while (1)
533 {
534 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
535 wait_for_inferior ();
536 if (stop_signal != TARGET_SIGNAL_TRAP)
537 {
538 /* Let shell child handle its own signals in its own way */
539 /* FIXME, what if child has exit()ed? Must exit loop somehow */
540 resume (0, stop_signal);
541 }
542 else
543 {
544 /* We handle SIGTRAP, however; it means child did an exec. */
545 if (!terminal_initted)
546 {
547 /* Now that the child has exec'd we know it has already set its
c5aa993b
JM
548 process group. On POSIX systems, tcsetpgrp will fail with
549 EPERM if we try it before the child's setpgid. */
c906108c
SS
550
551 /* Set up the "saved terminal modes" of the inferior
c5aa993b 552 based on what modes we are starting it with. */
c906108c
SS
553 target_terminal_init ();
554
555 /* Install inferior's terminal modes. */
556 target_terminal_inferior ();
557
558 terminal_initted = 1;
559 }
560
561 pending_execs = pending_execs - 1;
562 if (0 == pending_execs)
563 break;
564
565 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
566 }
567 }
568#endif /* STARTUP_INFERIOR */
569 stop_soon_quietly = 0;
570}
This page took 0.16942 seconds and 4 git commands to generate.