1999-01-19 Fernando Nasser <fnasser@totem.to.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / fork-child.c
CommitLineData
3fbdd536 1/* Fork a Unix child process, and set up to debug it, for GDB.
8fc2b417 2 Copyright 1990, 1991, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
3fbdd536
JG
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
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
3fbdd536
JG
20
21#include "defs.h"
2b576293 22#include "gdb_string.h"
3fbdd536
JG
23#include "frame.h" /* required by inferior.h */
24#include "inferior.h"
25#include "target.h"
26#include "wait.h"
27#include "gdbcore.h"
c18b613a 28#include "terminal.h"
fdfa3315 29#include "gdbthread.h"
3fbdd536
JG
30
31#include <signal.h>
1a494973
C
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
3fbdd536 35
65b07ddc 36#define DEBUGGING 0
3fbdd536 37
65b07ddc 38/* This just gets used as a default if we can't find SHELL */
3fbdd536
JG
39#ifndef SHELL_FILE
40#define SHELL_FILE "/bin/sh"
41#endif
42
65b07ddc
DT
43extern char **environ;
44
45/* This function breaks up an argument string into an argument
46 * vector suitable for passing to execvp().
47 * E.g., on "run a b c d" this routine would get as input
48 * the string "a b c d", and as output it would fill in argv with
49 * the four arguments "a", "b", "c", "d".
50 */
51static void
52breakup_args (
53 scratch,
54 argv)
55char *scratch;
56char **argv;
57{
58 char *cp = scratch;
59
60#if DEBUGGING
61 printf("breakup_args: input = %s\n", scratch);
62#endif
63 for (;;) {
64
65 /* Scan past leading separators */
66 while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
67 cp++;
68 }
69
70 /* Break if at end of string */
71 if (*cp == '\0') break;
72
73 /* Take an arg */
74 *argv++ = cp;
75
76 /* Scan for next arg separator */
77 cp = strchr (cp, ' ');
78 if (cp == NULL)
79 cp = strchr (cp, '\t');
80 if (cp == NULL)
81 cp = strchr (cp, '\n');
82
83 /* No separators => end of string => break */
84 if (cp == NULL)
85 break;
86
87 /* Replace the separator with a terminator */
88 *cp++ = '\0';
89 }
90
91 /* execv requires a null-terminated arg vector */
92 *argv = NULL;
93
94}
95
96
08f74b92
JK
97/* Start an inferior Unix child process and sets inferior_pid to its pid.
98 EXEC_FILE is the file to run.
99 ALLARGS is a string containing the arguments to the program.
100 ENV is the environment vector to pass. SHELL_FILE is the shell file,
101 or NULL if we should pick one. Errors reported with error(). */
102
3fbdd536 103void
08f74b92 104fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
65b07ddc 105 pre_trace_fun, shell_file)
3fbdd536
JG
106 char *exec_file;
107 char *allargs;
108 char **env;
109 void (*traceme_fun) PARAMS ((void));
65b07ddc
DT
110 void (*init_trace_fun) PARAMS ((int));
111 void (*pre_trace_fun) PARAMS ((void));
08f74b92 112 char *shell_file;
3fbdd536
JG
113{
114 int pid;
115 char *shell_command;
3fbdd536
JG
116 static char default_shell_file[] = SHELL_FILE;
117 int len;
3fbdd536
JG
118 /* Set debug_fork then attach to the child while it sleeps, to debug. */
119 static int debug_fork = 0;
120 /* This is set to the result of setpgrp, which if vforked, will be visible
121 to you in the parent process. It's only used by humans for debugging. */
122 static int debug_setpgrp = 657473;
123 char **save_our_env;
65b07ddc
DT
124 int shell = 0;
125 char **argv;
126 char *tryname;
3fbdd536
JG
127
128 /* If no exec file handed to us, get it from the exec-file command -- with
129 a good, common error message if none is specified. */
130 if (exec_file == 0)
131 exec_file = get_exec_file(1);
132
65b07ddc
DT
133 /* STARTUP_WITH_SHELL is defined in inferior.h.
134 * If 0, we'll just do a fork/exec, no shell, so don't
135 * bother figuring out what shell.
136 */
137 if (STARTUP_WITH_SHELL) {
138 /* Figure out what shell to start up the user program under. */
139 if (shell_file == NULL)
140 shell_file = getenv ("SHELL");
141 if (shell_file == NULL)
142 shell_file = default_shell_file;
143 shell = 1;
144 }
145
146#if DEBUGGING
147 printf("shell is %s\n", shell_file);
148#endif
3768398d
JK
149
150 /* Multiplying the length of exec_file by 4 is to account for the fact
151 that it may expand when quoted; it is a worst-case number based on
152 every character being '. */
153 len = 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 12;
3fbdd536
JG
154 /* If desired, concat something onto the front of ALLARGS.
155 SHELL_COMMAND is the result. */
156#ifdef SHELL_COMMAND_CONCAT
157 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
158 strcpy (shell_command, SHELL_COMMAND_CONCAT);
159#else
160 shell_command = (char *) alloca (len);
161 shell_command[0] = '\0';
162#endif
3768398d 163
65b07ddc
DT
164 if (!shell) {
165 /* We're going to call execvp. Create argv */
166 /* Largest case: every other character is a separate arg */
167#if DEBUGGING
168 printf("allocating argv, length = %d\n",
169 (
170 (strlen (allargs) + 1) / (unsigned) 2
171 + 2
172 ) * sizeof (*argv)
173 );
174#endif
175 argv = (char **) xmalloc(((strlen (allargs) + 1) / (unsigned) 2 + 2) * sizeof (*argv));
176 argv[0] = exec_file;
177 breakup_args(allargs, &argv[1]);
178
179 } else {
180
181 /* We're going to call a shell */
182
183 /* Now add exec_file, quoting as necessary. */
184
3768398d 185 char *p;
38bbfd37 186 int need_to_quote;
3768398d 187
65b07ddc
DT
188 strcat (shell_command, "exec ");
189
38bbfd37
JK
190 /* Quoting in this style is said to work with all shells. But csh
191 on IRIX 4.0.1 can't deal with it. So we only quote it if we need
192 to. */
193 p = exec_file;
194 while (1)
3768398d 195 {
65b07ddc 196 switch (*p)
38bbfd37
JK
197 {
198 case '\'':
199 case '"':
200 case '(':
201 case ')':
202 case '$':
203 case '&':
204 case ';':
205 case '<':
206 case '>':
207 case ' ':
208 case '\n':
209 case '\t':
210 need_to_quote = 1;
211 goto end_scan;
212
213 case '\0':
214 need_to_quote = 0;
215 goto end_scan;
216
217 default:
218 break;
219 }
220 ++p;
3768398d 221 }
38bbfd37
JK
222 end_scan:
223 if (need_to_quote)
224 {
65b07ddc
DT
225 strcat (shell_command, "'");
226 for (p = exec_file; *p != '\0'; ++p)
38bbfd37
JK
227 {
228 if (*p == '\'')
229 strcat (shell_command, "'\\''");
230 else
231 strncat (shell_command, p, 1);
232 }
233 strcat (shell_command, "'");
234 }
235 else
236 strcat (shell_command, exec_file);
65b07ddc
DT
237
238 strcat (shell_command, " ");
239 strcat (shell_command, allargs);
3768398d 240
65b07ddc 241 }
3fbdd536
JG
242
243 /* exec is said to fail if the executable is open. */
244 close_exec_file ();
245
246 /* Retain a copy of our environment variables, since the child will
247 replace the value of environ and if we're vforked, we have to
248 restore it. */
249 save_our_env = environ;
250
251 /* Tell the terminal handling subsystem what tty we plan to run on;
252 it will just record the information for later. */
253
254 new_tty_prefork (inferior_io_terminal);
255
256 /* It is generally good practice to flush any possible pending stdio
257 output prior to doing a fork, to avoid the possibility of both the
258 parent and child flushing the same data after the fork. */
259
199b2450
TL
260 gdb_flush (gdb_stdout);
261 gdb_flush (gdb_stderr);
3fbdd536 262
65b07ddc
DT
263 /* If there's any initialization of the target layers that must happen
264 to prepare to handle the child we're about fork, do it now...
265 */
266 if (pre_trace_fun != NULL)
267 (*pre_trace_fun) ();
268
3fbdd536
JG
269#if defined(USG) && !defined(HAVE_VFORK)
270 pid = fork ();
271#else
272 if (debug_fork)
273 pid = fork ();
274 else
275 pid = vfork ();
276#endif
277
278 if (pid < 0)
279 perror_with_name ("vfork");
280
281 if (pid == 0)
282 {
283 if (debug_fork)
284 sleep (debug_fork);
285
3fbdd536 286 /* Run inferior in a separate process group. */
c2e247c4 287 debug_setpgrp = gdb_setpgid ();
3fbdd536
JG
288 if (debug_setpgrp == -1)
289 perror("setpgrp failed in child");
3fbdd536 290
3fbdd536
JG
291 /* Ask the tty subsystem to switch to the one we specified earlier
292 (or to share the current terminal, if none was specified). */
293
294 new_tty ();
295
296 /* Changing the signal handlers for the inferior after
297 a vfork can also change them for the superior, so we don't mess
298 with signals here. See comments in
299 initialize_signals for how we get the right signal handlers
300 for the inferior. */
301
302 /* "Trace me, Dr. Memory!" */
303 (*traceme_fun) ();
65b07ddc
DT
304 /* The call above set this process (the "child") as debuggable
305 * by the original gdb process (the "parent"). Since processes
306 * (unlike people) can have only one parent, if you are
307 * debugging gdb itself (and your debugger is thus _already_ the
308 * controller/parent for this child), code from here on out
309 * is undebuggable. Indeed, you probably got an error message
310 * saying "not parent". Sorry--you'll have to use print statements!
311 */
3fbdd536
JG
312
313 /* There is no execlpe call, so we have to set the environment
314 for our child in the global variable. If we've vforked, this
315 clobbers the parent, but environ is restored a few lines down
316 in the parent. By the way, yes we do need to look down the
317 path to find $SHELL. Rich Pixley says so, and I agree. */
318 environ = env;
3fbdd536 319
65b07ddc
DT
320 /* If we decided above to start up with a shell,
321 * we exec the shell,
322 * "-c" says to interpret the next arg as a shell command
323 * to execute, and this command is "exec <target-program> <args>".
324 * "-f" means "fast startup" to the c-shell, which means
325 * don't do .cshrc file. Doing .cshrc may cause fork/exec
326 * events which will confuse debugger start-up code.
327 */
328 if (shell) {
329#if 0
330
331 /* HP change is problematic. The -f option has different meanings
332 for different shells. It is particularly inappropriate for
333 bourne shells. */
334 execlp (shell_file, shell_file, "-f", "-c", shell_command, (char *)0);
335#else
336 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
337#endif
338
339
340 /* If we get here, it's an error */
341 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
342 safe_strerror (errno));
343 gdb_flush (gdb_stderr);
344 _exit (0177);
345 } else {
346 /* Otherwise, we directly exec the target program with execvp. */
347 int i;
348 char * errstring;
349#if DEBUGGING
350 printf("about to exec target, exec_file = %s\n", exec_file);
351 i = 0;
352 while (argv[i] != NULL) {
353 printf("strlen(argv[%d]) is %d\n", i, strlen(argv[i]));
354 printf("argv[%d] is %s\n", i, argv[i]);
355 i++;
356 }
357#endif
358 execvp (exec_file, argv);
359
360 /* If we get here, it's an error */
361 errstring = safe_strerror (errno);
362 fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
363
364 i = 1;
365 while (argv[i] != NULL) {
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
373 fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
374 */
375 gdb_flush (gdb_stderr);
376 _exit (0177);
377 }
3fbdd536
JG
378 }
379
380 /* Restore our environment in case a vforked child clob'd it. */
381 environ = save_our_env;
382
de43d7d0
SG
383 init_thread_list();
384
bc28a06c
JK
385 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
386
3fbdd536
JG
387 /* Now that we have a child process, make it our target, and
388 initialize anything target-vector-specific that needs initializing. */
8fc2b417 389
65b07ddc 390 (*init_trace_fun)(pid);
3fbdd536 391
bc28a06c
JK
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. */
188c635f
SG
395
396 /* Allow target dependant code to play with the new process. This might be
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
bc28a06c
JK
401#ifdef SOLIB_CREATE_INFERIOR_HOOK
402 SOLIB_CREATE_INFERIOR_HOOK (pid);
403#endif
404}
405
65b07ddc
DT
406/* An inferior Unix process CHILD_PID has been created by a call to
407 fork() (or variants like vfork). It is presently stopped, and waiting
408 to be resumed. clone_and_follow_inferior will fork the debugger,
409 and that clone will "follow" (attach to) CHILD_PID. The original copy
410 of the debugger will not touch CHILD_PID again.
411
412 Also, the original debugger will set FOLLOWED_CHILD FALSE, while the
413 clone will set it TRUE.
414 */
415void
416clone_and_follow_inferior (child_pid, followed_child)
417 int child_pid;
418 int *followed_child;
419{
420 extern int auto_solib_add;
421
422 int debugger_pid;
423 int status;
424 char pid_spelling [100]; /* Arbitrary but sufficient length. */
425
426 /* This semaphore is used to coordinate the two debuggers' handoff
427 of CHILD_PID. The original debugger will detach from CHILD_PID,
428 and then the clone debugger will attach to it. (It must be done
429 this way because on some targets, only one process at a time can
430 trace another. Thus, the original debugger must relinquish its
431 tracing rights before the clone can pick them up.)
432 */
433#define SEM_TALK (1)
434#define SEM_LISTEN (0)
435 int handoff_semaphore[2]; /* Original "talks" to [1], clone "listens" to [0] */
436 int talk_value = 99;
437 int listen_value;
438
439 /* Set debug_fork then attach to the child while it sleeps, to debug. */
440 static int debug_fork = 0;
441
442 /* It is generally good practice to flush any possible pending stdio
443 output prior to doing a fork, to avoid the possibility of both the
444 parent and child flushing the same data after the fork. */
445
446 gdb_flush (gdb_stdout);
447 gdb_flush (gdb_stderr);
448
449 /* Open the semaphore pipes.
450 */
451 status = pipe (handoff_semaphore);
452 if (status < 0)
453 error ("error getting pipe for handoff semaphore");
454
455 /* Clone the debugger. */
456#if defined(USG) && !defined(HAVE_VFORK)
457 debugger_pid = fork ();
458#else
459 if (debug_fork)
460 debugger_pid = fork ();
461 else
462 debugger_pid = vfork ();
463#endif
464
465 if (debugger_pid < 0)
466 perror_with_name ("fork");
467
468 /* Are we the original debugger? If so, we must relinquish all claims
469 to CHILD_PID. */
470 if (debugger_pid != 0)
471 {
472 char signal_spelling[100]; /* Arbitrary but sufficient length */
473
474 /* Detach from CHILD_PID. Deliver a "stop" signal when we do, though,
475 so that it remains stopped until the clone debugger can attach
476 to it.
477 */
478 detach_breakpoints (child_pid);
479
480 sprintf (signal_spelling, "%d", target_signal_to_host (TARGET_SIGNAL_STOP));
481 target_require_detach (child_pid, signal_spelling, 1);
482
483 /* Notify the clone debugger that it should attach to CHILD_PID. */
484 write (handoff_semaphore[SEM_TALK], &talk_value, sizeof (talk_value));
485
486 *followed_child = 0;
487 }
488
489 /* We're the child. */
490 else
491 {
492 if (debug_fork)
493 sleep (debug_fork);
494
495 /* The child (i.e., the cloned debugger) must now attach to
496 CHILD_PID. inferior_pid is presently set to the parent process
497 of the fork, while CHILD_PID should be the child process of the
498 fork.
499
500 Wait until the original debugger relinquishes control of CHILD_PID,
501 though.
502 */
503 read (handoff_semaphore[SEM_LISTEN], &listen_value, sizeof (listen_value));
504
505 /* Note that we DON'T want to actually detach from inferior_pid,
506 because that would allow it to run free. The original
507 debugger wants to retain control of the process. So, we
508 just reset inferior_pid to CHILD_PID, and then ensure that all
509 breakpoints are really set in CHILD_PID.
510 */
511 target_mourn_inferior ();
512
513 /* Ask the tty subsystem to switch to the one we specified earlier
514 (or to share the current terminal, if none was specified). */
515
516 new_tty ();
517
518 dont_repeat ();
519 sprintf(pid_spelling, "%d", child_pid);
520 target_require_attach (pid_spelling, 1);
521
522 /* Perform any necessary cleanup, after attachment. (This form
523 of attaching can behave differently on some targets than the
524 standard method, where a process formerly not under debugger
525 control was suddenly attached to..)
526 */
527 target_post_follow_inferior_by_clone ();
528
529 *followed_child = 1;
530 }
531
532 /* Discard the handoff sempahore. */
533 (void) close (handoff_semaphore[SEM_LISTEN]);
534 (void) close (handoff_semaphore[SEM_TALK]);
535}
536
bc28a06c
JK
537/* Accept NTRAPS traps from the inferior. */
538
539void
540startup_inferior (ntraps)
541 int ntraps;
542{
543 int pending_execs = ntraps;
544 int terminal_initted;
545
5d76c8e6
JK
546 /* The process was started by the fork that created it,
547 but it will have stopped one instruction after execing the shell.
548 Here we must get it up to actual execution of the real program. */
3fbdd536 549
3fbdd536
JG
550 clear_proceed_status ();
551
3fbdd536
JG
552 init_wait_for_inferior ();
553
01dfd415 554 terminal_initted = 0;
3fbdd536 555
65b07ddc
DT
556 if (STARTUP_WITH_SHELL)
557 inferior_ignoring_startup_exec_events = ntraps;
558 else
559 inferior_ignoring_startup_exec_events = 0;
560 inferior_ignoring_leading_exec_events =
561 target_reported_exec_events_per_exec_call () - 1;
562
9f8219f1
SS
563#ifdef STARTUP_INFERIOR
564 STARTUP_INFERIOR (pending_execs);
565#else
3fbdd536
JG
566 while (1)
567 {
568 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
569 wait_for_inferior ();
67ac9759 570 if (stop_signal != TARGET_SIGNAL_TRAP)
3fbdd536
JG
571 {
572 /* Let shell child handle its own signals in its own way */
573 /* FIXME, what if child has exit()ed? Must exit loop somehow */
574 resume (0, stop_signal);
575 }
576 else
577 {
578 /* We handle SIGTRAP, however; it means child did an exec. */
01dfd415
JK
579 if (!terminal_initted)
580 {
581 /* Now that the child has exec'd we know it has already set its
582 process group. On POSIX systems, tcsetpgrp will fail with
583 EPERM if we try it before the child's setpgid. */
584
585 /* Set up the "saved terminal modes" of the inferior
586 based on what modes we are starting it with. */
587 target_terminal_init ();
588
589 /* Install inferior's terminal modes. */
590 target_terminal_inferior ();
591
592 terminal_initted = 1;
593 }
65b07ddc
DT
594
595 pending_execs = pending_execs - 1;
596 if (0 == pending_execs)
3fbdd536 597 break;
65b07ddc
DT
598
599 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
3fbdd536
JG
600 }
601 }
9f8219f1 602#endif /* STARTUP_INFERIOR */
3fbdd536 603 stop_soon_quietly = 0;
3fbdd536 604}
This page took 0.308291 seconds and 4 git commands to generate.