]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fork-child.c
import gdb-1999-08-30 snapshot
[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, 91, 92, 93, 94, 1996, 1999 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "frame.h" /* required by inferior.h */
25 #include "inferior.h"
26 #include "target.h"
27 #include "wait.h"
28 #include "gdbcore.h"
29 #include "terminal.h"
30 #include "gdbthread.h"
31
32 #include <signal.h>
33
34 /* This just gets used as a default if we can't find SHELL */
35 #ifndef SHELL_FILE
36 #define SHELL_FILE "/bin/sh"
37 #endif
38
39 extern char **environ;
40
41 /* This function breaks up an argument string into an argument
42 * vector suitable for passing to execvp().
43 * E.g., on "run a b c d" this routine would get as input
44 * the string "a b c d", and as output it would fill in argv with
45 * the four arguments "a", "b", "c", "d".
46 */
47 static void
48 breakup_args (
49 scratch,
50 argv)
51 char *scratch;
52 char **argv;
53 {
54 char *cp = scratch;
55
56 for (;;)
57 {
58
59 /* Scan past leading separators */
60 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
61 {
62 cp++;
63 }
64
65 /* Break if at end of string */
66 if (*cp == '\0')
67 break;
68
69 /* Take an arg */
70 *argv++ = cp;
71
72 /* Scan for next arg separator */
73 cp = strchr (cp, ' ');
74 if (cp == NULL)
75 cp = strchr (cp, '\t');
76 if (cp == NULL)
77 cp = strchr (cp, '\n');
78
79 /* No separators => end of string => break */
80 if (cp == NULL)
81 break;
82
83 /* Replace the separator with a terminator */
84 *cp++ = '\0';
85 }
86
87 /* execv requires a null-terminated arg vector */
88 *argv = NULL;
89
90 }
91
92
93 /* Start an inferior Unix child process and sets inferior_pid to its pid.
94 EXEC_FILE is the file to run.
95 ALLARGS is a string containing the arguments to the program.
96 ENV is the environment vector to pass. SHELL_FILE is the shell file,
97 or NULL if we should pick one. Errors reported with error(). */
98
99 void
100 fork_inferior (exec_file, allargs, env, traceme_fun, init_trace_fun,
101 pre_trace_fun, shell_file)
102 char *exec_file;
103 char *allargs;
104 char **env;
105 void (*traceme_fun) PARAMS ((void));
106 void (*init_trace_fun) PARAMS ((int));
107 void (*pre_trace_fun) PARAMS ((void));
108 char *shell_file;
109 {
110 int pid;
111 char *shell_command;
112 static char default_shell_file[] = SHELL_FILE;
113 int len;
114 /* Set debug_fork then attach to the child while it sleeps, to debug. */
115 static int debug_fork = 0;
116 /* This is set to the result of setpgrp, which if vforked, will be visible
117 to you in the parent process. It's only used by humans for debugging. */
118 static int debug_setpgrp = 657473;
119 char **save_our_env;
120 int shell = 0;
121 char **argv;
122
123 /* If no exec file handed to us, get it from the exec-file command -- with
124 a good, common error message if none is specified. */
125 if (exec_file == 0)
126 exec_file = get_exec_file (1);
127
128 /* STARTUP_WITH_SHELL is defined in inferior.h.
129 * If 0, we'll just do a fork/exec, no shell, so don't
130 * bother figuring out what shell.
131 */
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 '\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
251 to prepare to handle the child we're about fork, do it now...
252 */
253 if (pre_trace_fun != NULL)
254 (*pre_trace_fun) ();
255
256 #if defined(USG) && !defined(HAVE_VFORK)
257 pid = fork ();
258 #else
259 if (debug_fork)
260 pid = fork ();
261 else
262 pid = vfork ();
263 #endif
264
265 if (pid < 0)
266 perror_with_name ("vfork");
267
268 if (pid == 0)
269 {
270 if (debug_fork)
271 sleep (debug_fork);
272
273 /* Run inferior in a separate process group. */
274 debug_setpgrp = gdb_setpgid ();
275 if (debug_setpgrp == -1)
276 perror ("setpgrp failed in child");
277
278 /* Ask the tty subsystem to switch to the one we specified earlier
279 (or to share the current terminal, if none was specified). */
280
281 new_tty ();
282
283 /* Changing the signal handlers for the inferior after
284 a vfork can also change them for the superior, so we don't mess
285 with signals here. See comments in
286 initialize_signals for how we get the right signal handlers
287 for the inferior. */
288
289 /* "Trace me, Dr. Memory!" */
290 (*traceme_fun) ();
291 /* The call above set this process (the "child") as debuggable
292 * by the original gdb process (the "parent"). Since processes
293 * (unlike people) can have only one parent, if you are
294 * debugging gdb itself (and your debugger is thus _already_ the
295 * controller/parent for this child), code from here on out
296 * is undebuggable. Indeed, you probably got an error message
297 * saying "not parent". Sorry--you'll have to use print statements!
298 */
299
300 /* There is no execlpe call, so we have to set the environment
301 for our child in the global variable. If we've vforked, this
302 clobbers the parent, but environ is restored a few lines down
303 in the parent. By the way, yes we do need to look down the
304 path to find $SHELL. Rich Pixley says so, and I agree. */
305 environ = env;
306
307 /* If we decided above to start up with a shell,
308 * we exec the shell,
309 * "-c" says to interpret the next arg as a shell command
310 * to execute, and this command is "exec <target-program> <args>".
311 * "-f" means "fast startup" to the c-shell, which means
312 * don't do .cshrc file. Doing .cshrc may cause fork/exec
313 * events which will confuse debugger start-up code.
314 */
315 if (shell)
316 {
317 execlp (shell_file, shell_file, "-c", shell_command, (char *) 0);
318
319 /* If we get here, it's an error */
320 fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file,
321 safe_strerror (errno));
322 gdb_flush (gdb_stderr);
323 _exit (0177);
324 }
325 else
326 {
327 /* Otherwise, we directly exec the target program with execvp. */
328 int i;
329 char *errstring;
330
331 execvp (exec_file, argv);
332
333 /* If we get here, it's an error */
334 errstring = safe_strerror (errno);
335 fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file);
336
337 i = 1;
338 while (argv[i] != NULL)
339 {
340 if (i != 1)
341 fprintf_unfiltered (gdb_stderr, " ");
342 fprintf_unfiltered (gdb_stderr, "%s", argv[i]);
343 i++;
344 }
345 fprintf_unfiltered (gdb_stderr, ".\n");
346 /* This extra info seems to be useless
347 fprintf_unfiltered (gdb_stderr, "Got error %s.\n", errstring);
348 */
349 gdb_flush (gdb_stderr);
350 _exit (0177);
351 }
352 }
353
354 /* Restore our environment in case a vforked child clob'd it. */
355 environ = save_our_env;
356
357 init_thread_list ();
358
359 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
360
361 /* Now that we have a child process, make it our target, and
362 initialize anything target-vector-specific that needs initializing. */
363
364 (*init_trace_fun) (pid);
365
366 /* We are now in the child process of interest, having exec'd the
367 correct program, and are poised at the first instruction of the
368 new program. */
369
370 /* Allow target dependant code to play with the new process. This might be
371 used to have target-specific code initialize a variable in the new process
372 prior to executing the first instruction. */
373 TARGET_CREATE_INFERIOR_HOOK (pid);
374
375 #ifdef SOLIB_CREATE_INFERIOR_HOOK
376 SOLIB_CREATE_INFERIOR_HOOK (pid);
377 #endif
378 }
379
380 /* An inferior Unix process CHILD_PID has been created by a call to
381 fork() (or variants like vfork). It is presently stopped, and waiting
382 to be resumed. clone_and_follow_inferior will fork the debugger,
383 and that clone will "follow" (attach to) CHILD_PID. The original copy
384 of the debugger will not touch CHILD_PID again.
385
386 Also, the original debugger will set FOLLOWED_CHILD FALSE, while the
387 clone will set it TRUE.
388 */
389 void
390 clone_and_follow_inferior (child_pid, followed_child)
391 int child_pid;
392 int *followed_child;
393 {
394 extern int auto_solib_add;
395
396 int debugger_pid;
397 int status;
398 char pid_spelling[100]; /* Arbitrary but sufficient length. */
399
400 /* This semaphore is used to coordinate the two debuggers' handoff
401 of CHILD_PID. The original debugger will detach from CHILD_PID,
402 and then the clone debugger will attach to it. (It must be done
403 this way because on some targets, only one process at a time can
404 trace another. Thus, the original debugger must relinquish its
405 tracing rights before the clone can pick them up.)
406 */
407 #define SEM_TALK (1)
408 #define SEM_LISTEN (0)
409 int handoff_semaphore[2]; /* Original "talks" to [1], clone "listens" to [0] */
410 int talk_value = 99;
411 int listen_value;
412
413 /* Set debug_fork then attach to the child while it sleeps, to debug. */
414 static int debug_fork = 0;
415
416 /* It is generally good practice to flush any possible pending stdio
417 output prior to doing a fork, to avoid the possibility of both the
418 parent and child flushing the same data after the fork. */
419
420 gdb_flush (gdb_stdout);
421 gdb_flush (gdb_stderr);
422
423 /* Open the semaphore pipes.
424 */
425 status = pipe (handoff_semaphore);
426 if (status < 0)
427 error ("error getting pipe for handoff semaphore");
428
429 /* Clone the debugger. */
430 #if defined(USG) && !defined(HAVE_VFORK)
431 debugger_pid = fork ();
432 #else
433 if (debug_fork)
434 debugger_pid = fork ();
435 else
436 debugger_pid = vfork ();
437 #endif
438
439 if (debugger_pid < 0)
440 perror_with_name ("fork");
441
442 /* Are we the original debugger? If so, we must relinquish all claims
443 to CHILD_PID. */
444 if (debugger_pid != 0)
445 {
446 char signal_spelling[100]; /* Arbitrary but sufficient length */
447
448 /* Detach from CHILD_PID. Deliver a "stop" signal when we do, though,
449 so that it remains stopped until the clone debugger can attach
450 to it.
451 */
452 detach_breakpoints (child_pid);
453
454 sprintf (signal_spelling, "%d", target_signal_to_host (TARGET_SIGNAL_STOP));
455 target_require_detach (child_pid, signal_spelling, 1);
456
457 /* Notify the clone debugger that it should attach to CHILD_PID. */
458 write (handoff_semaphore[SEM_TALK], &talk_value, sizeof (talk_value));
459
460 *followed_child = 0;
461 }
462
463 /* We're the child. */
464 else
465 {
466 if (debug_fork)
467 sleep (debug_fork);
468
469 /* The child (i.e., the cloned debugger) must now attach to
470 CHILD_PID. inferior_pid is presently set to the parent process
471 of the fork, while CHILD_PID should be the child process of the
472 fork.
473
474 Wait until the original debugger relinquishes control of CHILD_PID,
475 though.
476 */
477 read (handoff_semaphore[SEM_LISTEN], &listen_value, sizeof (listen_value));
478
479 /* Note that we DON'T want to actually detach from inferior_pid,
480 because that would allow it to run free. The original
481 debugger wants to retain control of the process. So, we
482 just reset inferior_pid to CHILD_PID, and then ensure that all
483 breakpoints are really set in CHILD_PID.
484 */
485 target_mourn_inferior ();
486
487 /* Ask the tty subsystem to switch to the one we specified earlier
488 (or to share the current terminal, if none was specified). */
489
490 new_tty ();
491
492 dont_repeat ();
493 sprintf (pid_spelling, "%d", child_pid);
494 target_require_attach (pid_spelling, 1);
495
496 /* Perform any necessary cleanup, after attachment. (This form
497 of attaching can behave differently on some targets than the
498 standard method, where a process formerly not under debugger
499 control was suddenly attached to..)
500 */
501 target_post_follow_inferior_by_clone ();
502
503 *followed_child = 1;
504 }
505
506 /* Discard the handoff sempahore. */
507 (void) close (handoff_semaphore[SEM_LISTEN]);
508 (void) close (handoff_semaphore[SEM_TALK]);
509 }
510
511 /* Accept NTRAPS traps from the inferior. */
512
513 void
514 startup_inferior (ntraps)
515 int ntraps;
516 {
517 int pending_execs = ntraps;
518 int terminal_initted;
519
520 /* The process was started by the fork that created it,
521 but it will have stopped one instruction after execing the shell.
522 Here we must get it up to actual execution of the real program. */
523
524 clear_proceed_status ();
525
526 init_wait_for_inferior ();
527
528 terminal_initted = 0;
529
530 if (STARTUP_WITH_SHELL)
531 inferior_ignoring_startup_exec_events = ntraps;
532 else
533 inferior_ignoring_startup_exec_events = 0;
534 inferior_ignoring_leading_exec_events =
535 target_reported_exec_events_per_exec_call () - 1;
536
537 #ifdef STARTUP_INFERIOR
538 STARTUP_INFERIOR (pending_execs);
539 #else
540 while (1)
541 {
542 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
543 wait_for_inferior ();
544 if (stop_signal != TARGET_SIGNAL_TRAP)
545 {
546 /* Let shell child handle its own signals in its own way */
547 /* FIXME, what if child has exit()ed? Must exit loop somehow */
548 resume (0, stop_signal);
549 }
550 else
551 {
552 /* We handle SIGTRAP, however; it means child did an exec. */
553 if (!terminal_initted)
554 {
555 /* Now that the child has exec'd we know it has already set its
556 process group. On POSIX systems, tcsetpgrp will fail with
557 EPERM if we try it before the child's setpgid. */
558
559 /* Set up the "saved terminal modes" of the inferior
560 based on what modes we are starting it with. */
561 target_terminal_init ();
562
563 /* Install inferior's terminal modes. */
564 target_terminal_inferior ();
565
566 terminal_initted = 1;
567 }
568
569 pending_execs = pending_execs - 1;
570 if (0 == pending_execs)
571 break;
572
573 resume (0, TARGET_SIGNAL_0); /* Just make it go on */
574 }
575 }
576 #endif /* STARTUP_INFERIOR */
577 stop_soon_quietly = 0;
578 }