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